Skip to content

Commit 034f4cf

Browse files
authored
Fix NULL handling for FLOOR and CEIL math functions (elastic#49644)
1 parent dd3aeb8 commit 034f4cf

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

x-pack/plugin/sql/qa/src/main/resources/math.sql-spec

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ SELECT ATAN(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY
1717
mathCeil
1818
// H2 returns CEIL as a double despite the value being an integer; we return a long as the other DBs
1919
SELECT CAST(CEIL(emp_no) AS INT) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
20+
mathCeilWithNulls
21+
SELECT CAST(CEIL(languages) AS INT) m FROM "test_emp" ORDER BY emp_no;
2022
mathCos
2123
SELECT COS(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
2224
mathCosh
@@ -31,6 +33,8 @@ mathExpm1
3133
SELECT EXP(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
3234
mathFloor
3335
SELECT CAST(FLOOR(emp_no) AS INT) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
36+
mathFloorWithNulls
37+
SELECT CAST(FLOOR(languages) AS INT) m FROM "test_emp" ORDER BY emp_no;
3438
mathLog
3539
SELECT LOG(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
3640
mathLog10
@@ -49,6 +53,8 @@ mathSqrt
4953
SELECT SQRT(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
5054
mathTan
5155
SELECT TAN(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
56+
mathFloorAndCeilWithNullLiteral
57+
SELECT CAST(FLOOR(CAST(NULL AS DOUBLE)) AS INT) fnull, CAST(CEIL(CAST(NULL AS LONG)) AS INT) cnull, gender FROM "test_emp" ORDER BY emp_no;
5258

5359
//
5460
// Combined methods

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/Ceil.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ protected Ceil replaceChild(Expression newChild) {
3333

3434
@Override
3535
public Number fold() {
36-
return DataTypeConversion.toInteger((double) super.fold(), dataType());
36+
Object result = super.fold();
37+
if (result == null) {
38+
return null;
39+
}
40+
return DataTypeConversion.toInteger((double) result, dataType());
3741
}
3842

3943
@Override

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/Floor.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ protected Floor replaceChild(Expression newChild) {
3333

3434
@Override
3535
public Object fold() {
36-
return DataTypeConversion.toInteger((double) super.fold(), dataType());
36+
Object result = super.fold();
37+
if (result == null) {
38+
return null;
39+
}
40+
return DataTypeConversion.toInteger((double) result, dataType());
3741
}
3842

3943
@Override

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/expression/function/scalar/math/MathFunctionProcessorTests.java

+18
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,22 @@ public void testRandom() {
5555
assertNotNull(proc.process(null));
5656
assertNotNull(proc.process(randomLong()));
5757
}
58+
59+
public void testFloor() {
60+
MathProcessor proc = new MathProcessor(MathOperation.FLOOR);
61+
assertNull(proc.process(null));
62+
assertNotNull(proc.process(randomLong()));
63+
assertEquals(3.0, proc.process(3.3));
64+
assertEquals(3.0, proc.process(3.9));
65+
assertEquals(-13.0, proc.process(-12.1));
66+
}
67+
68+
public void testCeil() {
69+
MathProcessor proc = new MathProcessor(MathOperation.CEIL);
70+
assertNull(proc.process(null));
71+
assertNotNull(proc.process(randomLong()));
72+
assertEquals(4.0, proc.process(3.3));
73+
assertEquals(4.0, proc.process(3.9));
74+
assertEquals(-12.0, proc.process(-12.1));
75+
}
5876
}

0 commit comments

Comments
 (0)