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 issue with CAST and NULL checking. #50371

Merged
merged 2 commits into from
Dec 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
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Nullability nullable() {
if (from().isNull()) {
return Nullability.TRUE;
}
return field().nullable();
return Nullability.UNKNOWN;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,9 @@
import org.elasticsearch.common.io.stream.Writeable.Reader;
import org.elasticsearch.test.AbstractWireSerializingTestCase;
import org.elasticsearch.xpack.sql.expression.function.scalar.Processors;
import org.elasticsearch.xpack.sql.expression.gen.processor.ConstantProcessor;
import org.elasticsearch.xpack.sql.expression.gen.processor.Processor;

public class CheckNullProcessorTests extends AbstractWireSerializingTestCase<CheckNullProcessor> {

private static final Processor FALSE = new ConstantProcessor(false);
private static final Processor TRUE = new ConstantProcessor(true);
private static final Processor NULL = new ConstantProcessor((Object) null);

public static CheckNullProcessor randomProcessor() {
return new CheckNullProcessor(randomFrom(CheckNullProcessor.CheckNullOperation.values()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.elasticsearch.xpack.sql.optimizer;

import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xpack.sql.SqlIllegalArgumentException;
import org.elasticsearch.xpack.sql.analysis.analyzer.Analyzer.PruneSubqueryAliases;
import org.elasticsearch.xpack.sql.analysis.index.EsIndex;
import org.elasticsearch.xpack.sql.expression.Alias;
Expand Down Expand Up @@ -438,10 +439,46 @@ public void testNullFoldingIsNull() {
assertEquals(false, foldNull.rule(new IsNull(EMPTY, TRUE)).fold());
}

public void testNullFoldingIsNullWithCast() {
FoldNull foldNull = new FoldNull();

Cast cast = new Cast(EMPTY, L("foo"), DataType.DATE);
IsNull isNull = new IsNull(EMPTY, cast);
final IsNull isNullOpt = (IsNull) foldNull.rule(isNull);
assertEquals(isNull, isNullOpt);

SqlIllegalArgumentException sqlIAE =
expectThrows(SqlIllegalArgumentException.class, () -> isNullOpt.asPipe().asProcessor().process(null));
assertEquals("cannot cast [foo] to [date]: Text 'foo' could not be parsed at index 0", sqlIAE.getMessage());

isNull = new IsNull(EMPTY, new Cast(EMPTY, NULL, randomFrom(DataType.values())));
assertTrue((Boolean) ((IsNull) foldNull.rule(isNull)).asPipe().asProcessor().process(null));
}

public void testNullFoldingIsNotNull() {
FoldNull foldNull = new FoldNull();
assertEquals(true, foldNull.rule(new IsNotNull(EMPTY, TRUE)).fold());
assertEquals(false, foldNull.rule(new IsNotNull(EMPTY, NULL)).fold());

Cast cast = new Cast(EMPTY, L("foo"), DataType.DATE);
IsNotNull isNotNull = new IsNotNull(EMPTY, cast);
assertEquals(isNotNull, foldNull.rule(isNotNull));
}

public void testNullFoldingIsNotNullWithCast() {
FoldNull foldNull = new FoldNull();

Cast cast = new Cast(EMPTY, L("foo"), DataType.DATE);
IsNotNull isNotNull = new IsNotNull(EMPTY, cast);
final IsNotNull isNotNullOpt = (IsNotNull) foldNull.rule(isNotNull);
assertEquals(isNotNull, isNotNullOpt);

SqlIllegalArgumentException sqlIAE =
expectThrows(SqlIllegalArgumentException.class, () -> isNotNullOpt.asPipe().asProcessor().process(null));
assertEquals("cannot cast [foo] to [date]: Text 'foo' could not be parsed at index 0", sqlIAE.getMessage());

isNotNull = new IsNotNull(EMPTY, new Cast(EMPTY, NULL, randomFrom(DataType.values())));
assertFalse((Boolean) ((IsNotNull) foldNull.rule(isNotNull)).asPipe().asProcessor().process(null));
}

public void testGenericNullableExpression() {
Expand All @@ -461,6 +498,18 @@ public void testGenericNullableExpression() {
assertNullLiteral(rule.rule(new RLike(EMPTY, NULL, "123")));
}

public void testNullFoldingOnCast() {
FoldNull foldNull = new FoldNull();

Cast cast = new Cast(EMPTY, NULL, randomFrom(DataType.values()));
assertEquals(Nullability.TRUE, cast.nullable());
assertNull(foldNull.rule(cast).fold());

cast = new Cast(EMPTY, L("foo"), DataType.DATE);
assertEquals(Nullability.UNKNOWN, cast.nullable());
assertEquals(cast, foldNull.rule(cast));
}

public void testNullFoldingDoesNotApplyOnLogicalExpressions() {
FoldNull rule = new FoldNull();

Expand Down Expand Up @@ -1684,4 +1733,4 @@ public void testReplaceAttributesWithTarget() {
gt = (GreaterThan) and.left();
assertEquals(a, gt.left());
}
}
}