From d918886e50085d2359f38b5404bb2469132dd76c Mon Sep 17 00:00:00 2001 From: mch_ucchi <41606806+sohardforaname@users.noreply.github.com> Date: Mon, 27 Mar 2023 17:29:14 +0800 Subject: [PATCH] [fix](planner) fix targetTypeDef NPE when value is null (#18072) sql like: select * from (select *, null as top from v1)t where top = 5; select * from (select *, null as top from v1)t where top is not null; will cause NPE because targetTypeDef is null when value is null. Now we use cast target type to the targetTypeDef. --- .../java/org/apache/doris/analysis/CastExpr.java | 6 +++++- .../query_p0/literal_view/lietral_test.groovy | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java index 486e14ac7228d5..d2cb8ce4421673 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CastExpr.java @@ -417,7 +417,11 @@ public Expr getResultValue(boolean inView) throws AnalysisException { private Expr castTo(LiteralExpr value) throws AnalysisException { if (value instanceof NullLiteral) { - return NullLiteral.create(targetTypeDef.getType()); + if (targetTypeDef != null) { + return NullLiteral.create(targetTypeDef.getType()); + } else { + return NullLiteral.create(type); + } } else if (type.isIntegerType()) { return new IntLiteral(value.getLongValue(), type); } else if (type.isLargeIntType()) { diff --git a/regression-test/suites/query_p0/literal_view/lietral_test.groovy b/regression-test/suites/query_p0/literal_view/lietral_test.groovy index 0307b0fce8bf92..0c5f8bcec0019e 100644 --- a/regression-test/suites/query_p0/literal_view/lietral_test.groovy +++ b/regression-test/suites/query_p0/literal_view/lietral_test.groovy @@ -116,4 +116,19 @@ suite("literal_view_test") { ) a where name != '1234'; """ + + test { + sql "select * from (select null as top) t where top is not null" + result ([]) + } + + test { + sql "select * from (select null as top) t where top is null" + result ([[null]]) + } + + test { + sql "select * from (select null as top) t where top = 5" + result ([]) + } }