diff --git a/x-pack/plugin/sql/qa/src/main/resources/null.csv-spec b/x-pack/plugin/sql/qa/src/main/resources/null.csv-spec index 19541cf5d9f32..610217b233314 100644 --- a/x-pack/plugin/sql/qa/src/main/resources/null.csv-spec +++ b/x-pack/plugin/sql/qa/src/main/resources/null.csv-spec @@ -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 diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalFunction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalFunction.java index 3de85185e8a4f..b3841f09e825e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalFunction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/expression/predicate/conditional/ConditionalFunction.java @@ -25,7 +25,7 @@ */ public abstract class ConditionalFunction extends ScalarFunction { - protected DataType dataType = DataType.NULL; + protected DataType dataType = null; ConditionalFunction(Source source, List fields) { super(source, fields); @@ -33,6 +33,12 @@ public abstract class ConditionalFunction extends ScalarFunction { @Override public DataType dataType() { + if (dataType == null) { + dataType = DataType.NULL; + for (Expression exp : children()) { + dataType = DataTypeConversion.commonType(dataType, exp.dataType()); + } + } return dataType; } @@ -61,7 +67,6 @@ protected TypeResolution resolveType() { child.dataType().typeName)); } } - dataType = DataTypeConversion.commonType(dataType, child.dataType()); } return TypeResolution.TYPE_RESOLVED; } diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java index a23d88b595630..2506e3df70220 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/optimizer/OptimizerTests.java @@ -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 randomListOfNulls() { @@ -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() { @@ -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() { @@ -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() { @@ -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() {