SQL: Fix Nullability of DATEADD (#47921)

Previously, Nullability was set to UNKNOWN instead of TRUE which
resulted on QueryFolder not correctly folding to NULL if any of the args
was null.

Remove the overriding nullable() also for DatePart/DateTrunc to allow
delegation the parent class.

(cherry picked from commit 05a7108e133b5ae7bec2257db5ae2d30ad926ee2)
This commit is contained in:
Marios Trivyzas 2019-10-11 19:10:01 +02:00
parent ac209c142c
commit 65717f6f42
6 changed files with 45 additions and 47 deletions

View File

@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.Nullability;
import org.elasticsearch.xpack.sql.expression.function.scalar.BinaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
@ -79,11 +78,6 @@ public abstract class BinaryDateTimeFunction extends BinaryScalarFunction {
protected abstract Pipe createPipe(Pipe left, Pipe right, ZoneId zoneId);
@Override
public Nullability nullable() {
return Nullability.TRUE;
}
@Override
protected ScriptTemplate asScriptFrom(ScriptTemplate leftScript, ScriptTemplate rightScript) {
return new ScriptTemplate(

View File

@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.Nullability;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
import org.elasticsearch.xpack.sql.tree.Source;
@ -136,11 +135,6 @@ public class DateAdd extends ThreeArgsDateTimeFunction {
return NodeInfo.create(this, DateAdd::new, first(), second(), third(), zoneId());
}
@Override
public Nullability nullable() {
return Nullability.UNKNOWN;
}
@Override
protected Pipe createPipe(Pipe first, Pipe second, Pipe third, ZoneId zoneId) {
return new DateAddPipe(source(), this, first, second, third, zoneId);

View File

@ -6,7 +6,6 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Nullability;
import org.elasticsearch.xpack.sql.expression.function.scalar.BinaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTimeProcessor.DateTimeExtractor;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
@ -97,11 +96,6 @@ public class DatePart extends BinaryDateTimeFunction {
return NodeInfo.create(this, DatePart::new, left(), right(), zoneId());
}
@Override
public Nullability nullable() {
return Nullability.TRUE;
}
@Override
protected String scriptMethodName() {
return "datePart";

View File

@ -6,7 +6,6 @@
package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Nullability;
import org.elasticsearch.xpack.sql.expression.function.scalar.BinaryScalarFunction;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.tree.NodeInfo;
@ -153,11 +152,6 @@ public class DateTrunc extends BinaryDateTimeFunction {
return NodeInfo.create(this, DateTrunc::new, left(), right(), zoneId());
}
@Override
public Nullability nullable() {
return Nullability.TRUE;
}
@Override
protected String scriptMethodName() {
return "dateTrunc";

View File

@ -7,7 +7,6 @@ package org.elasticsearch.xpack.sql.expression.function.scalar.datetime;
import org.elasticsearch.xpack.sql.expression.Expression;
import org.elasticsearch.xpack.sql.expression.Expressions;
import org.elasticsearch.xpack.sql.expression.Nullability;
import org.elasticsearch.xpack.sql.expression.function.scalar.ScalarFunction;
import org.elasticsearch.xpack.sql.expression.gen.pipeline.Pipe;
import org.elasticsearch.xpack.sql.expression.gen.script.ScriptTemplate;
@ -69,11 +68,6 @@ public abstract class ThreeArgsDateTimeFunction extends ScalarFunction {
protected abstract Pipe createPipe(Pipe first, Pipe second, Pipe third, ZoneId zoneId);
@Override
public Nullability nullable() {
return Nullability.TRUE;
}
@Override
public boolean foldable() {
return first().foldable() && second().foldable() && third().foldable();

View File

@ -35,6 +35,9 @@ import org.elasticsearch.xpack.sql.expression.function.aggregate.Sum;
import org.elasticsearch.xpack.sql.expression.function.aggregate.SumOfSquares;
import org.elasticsearch.xpack.sql.expression.function.aggregate.VarPop;
import org.elasticsearch.xpack.sql.expression.function.scalar.Cast;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateAdd;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DatePart;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DateTrunc;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DayName;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DayOfMonth;
import org.elasticsearch.xpack.sql.expression.function.scalar.datetime.DayOfYear;
@ -622,6 +625,31 @@ public class OptimizerTests extends ESTestCase {
assertEquals(StringUtils.EMPTY, foldNull.rule(new Concat(EMPTY, NULL, NULL)).fold());
}
public void testFoldNullDateAdd() {
FoldNull foldNull = new FoldNull();
assertNullLiteral(foldNull.rule(new DateAdd(EMPTY, NULL, TWO, THREE, UTC)));
assertNullLiteral(foldNull.rule(new DateAdd(EMPTY, ONE, NULL, THREE, UTC)));
assertNullLiteral(foldNull.rule(new DateAdd(EMPTY, ONE, TWO, NULL, UTC)));
assertNullLiteral(foldNull.rule(new DateAdd(EMPTY, NULL, NULL, NULL, UTC)));
assertTrue(foldNull.rule(new DateAdd(EMPTY, ONE, TWO, THREE, UTC)) instanceof DateAdd);
}
public void testFoldNullDatePart() {
FoldNull foldNull = new FoldNull();
assertNullLiteral(foldNull.rule(new DatePart(EMPTY, NULL, TWO, UTC)));
assertNullLiteral(foldNull.rule(new DatePart(EMPTY, ONE, NULL, UTC)));
assertNullLiteral(foldNull.rule(new DatePart(EMPTY, NULL, NULL, UTC)));
assertTrue(foldNull.rule(new DatePart(EMPTY, ONE, TWO, UTC)) instanceof DatePart);
}
public void testFoldNullDateTrunc() {
FoldNull foldNull = new FoldNull();
assertNullLiteral(foldNull.rule(new DateTrunc(EMPTY, NULL, TWO, UTC)));
assertNullLiteral(foldNull.rule(new DateTrunc(EMPTY, ONE, NULL, UTC)));
assertNullLiteral(foldNull.rule(new DateTrunc(EMPTY, NULL, NULL, UTC)));
assertTrue(foldNull.rule(new DateTrunc(EMPTY, ONE, TWO, UTC)) instanceof DateTrunc);
}
public void testSimplifyCaseConditionsFoldWhenFalse() {
// CASE WHEN a = 1 THEN 'foo1'
// WHEN 1 = 2 THEN 'bar1'