Skip to content

Commit

Permalink
Fix NULL handling for FLOOR and CEIL math functions (#49644)
Browse files Browse the repository at this point in the history
(cherry picked from commit 034f4cf)
  • Loading branch information
astefan committed Dec 3, 2019
1 parent e14d00c commit 311867e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
6 changes: 6 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/math.sql-spec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ SELECT ATAN(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY
mathCeil
// H2 returns CEIL as a double despite the value being an integer; we return a long as the other DBs
SELECT CAST(CEIL(emp_no) AS INT) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
mathCeilWithNulls
SELECT CAST(CEIL(languages) AS INT) m FROM "test_emp" ORDER BY emp_no;
mathCos
SELECT COS(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
mathCosh
Expand All @@ -31,6 +33,8 @@ mathExpm1
SELECT EXP(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
mathFloor
SELECT CAST(FLOOR(emp_no) AS INT) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
mathFloorWithNulls
SELECT CAST(FLOOR(languages) AS INT) m FROM "test_emp" ORDER BY emp_no;
mathLog
SELECT LOG(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
mathLog10
Expand All @@ -49,6 +53,8 @@ mathSqrt
SELECT SQRT(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
mathTan
SELECT TAN(emp_no) m, first_name FROM "test_emp" WHERE emp_no < 10010 ORDER BY emp_no;
mathFloorAndCeilWithNullLiteral
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;

//
// Combined methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ protected Ceil replaceChild(Expression newChild) {

@Override
public Number fold() {
return DataTypeConversion.toInteger((double) super.fold(), dataType());
Object result = super.fold();
if (result == null) {
return null;
}
return DataTypeConversion.toInteger((double) result, dataType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ protected Floor replaceChild(Expression newChild) {

@Override
public Object fold() {
return DataTypeConversion.toInteger((double) super.fold(), dataType());
Object result = super.fold();
if (result == null) {
return null;
}
return DataTypeConversion.toInteger((double) result, dataType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,22 @@ public void testRandom() {
assertNotNull(proc.process(null));
assertNotNull(proc.process(randomLong()));
}

public void testFloor() {
MathProcessor proc = new MathProcessor(MathOperation.FLOOR);
assertNull(proc.process(null));
assertNotNull(proc.process(randomLong()));
assertEquals(3.0, proc.process(3.3));
assertEquals(3.0, proc.process(3.9));
assertEquals(-13.0, proc.process(-12.1));
}

public void testCeil() {
MathProcessor proc = new MathProcessor(MathOperation.CEIL);
assertNull(proc.process(null));
assertNotNull(proc.process(randomLong()));
assertEquals(4.0, proc.process(3.3));
assertEquals(4.0, proc.process(3.9));
assertEquals(-12.0, proc.process(-12.1));
}
}

0 comments on commit 311867e

Please sign in to comment.