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

SQL: Fix bug with optimization of null related conditionals #41355

Merged
merged 3 commits into from
Apr 19, 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
7 changes: 7 additions & 0 deletions x-pack/plugin/sql/qa/src/main/resources/null.csv-spec
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ c:i
;

coalesceMixed
SELECT COALESCE(null, 123, null, 321);

COALESCE(null, 123, null, 321):i
123
;

coalesceMixedWithAlias
SELECT COALESCE(null, 123, null, 321) AS c;

c:i
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@
*/
public abstract class ConditionalFunction extends ScalarFunction {

protected DataType dataType = DataType.NULL;
protected DataType dataType = null;

ConditionalFunction(Source source, List<Expression> fields) {
super(source, fields);
}

@Override
public DataType dataType() {
if (dataType == null) {
dataType = DataType.NULL;
for (Expression exp : children()) {
dataType = DataTypeConversion.commonType(dataType, exp.dataType());
}
}
return dataType;
}

Expand Down Expand Up @@ -61,7 +67,6 @@ protected TypeResolution resolveType() {
child.dataType().typeName));
}
}
dataType = DataTypeConversion.commonType(dataType, child.dataType());
}
return TypeResolution.TYPE_RESOLVED;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ public void testSimplifyCoalesceRandomNullsWithValue() {
randomListOfNulls())));
assertEquals(1, e.children().size());
assertEquals(TRUE, e.children().get(0));
assertEquals(DataType.BOOLEAN, e.dataType());
}

private List<Expression> randomListOfNulls() {
Expand All @@ -511,6 +512,7 @@ public void testSimplifyCoalesceFirstLiteral() {
assertEquals(Coalesce.class, e.getClass());
assertEquals(1, e.children().size());
assertEquals(TRUE, e.children().get(0));
assertEquals(DataType.BOOLEAN, e.dataType());
}

public void testSimplifyIfNullNulls() {
Expand All @@ -524,11 +526,13 @@ public void testSimplifyIfNullWithNullAndValue() {
assertEquals(IfNull.class, e.getClass());
assertEquals(1, e.children().size());
assertEquals(ONE, e.children().get(0));
assertEquals(DataType.INTEGER, e.dataType());

e = new SimplifyConditional().rule(new IfNull(EMPTY, ONE, NULL));
assertEquals(IfNull.class, e.getClass());
assertEquals(1, e.children().size());
assertEquals(ONE, e.children().get(0));
assertEquals(DataType.INTEGER, e.dataType());
}

public void testFoldNullNotAppliedOnNullIf() {
Expand Down Expand Up @@ -556,6 +560,7 @@ public void testSimplifyGreatestRandomNullsWithValue() {
assertEquals(2, e.children().size());
assertEquals(ONE, e.children().get(0));
assertEquals(TWO, e.children().get(1));
assertEquals(DataType.INTEGER, e.dataType());
}

public void testSimplifyLeastNulls() {
Expand All @@ -577,6 +582,7 @@ public void testSimplifyLeastRandomNullsWithValue() {
assertEquals(2, e.children().size());
assertEquals(ONE, e.children().get(0));
assertEquals(TWO, e.children().get(1));
assertEquals(DataType.INTEGER, e.dataType());
}

public void testConcatFoldingIsNotNull() {
Expand Down