Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NULL handling for FLOOR and CEIL functions #49644

Merged
merged 2 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}