From 0c1458f21f6142a707b59f3923aba4f0b578bf93 Mon Sep 17 00:00:00 2001 From: starocean999 <40539150+starocean999@users.noreply.github.com> Date: Wed, 8 Nov 2023 20:46:29 +0800 Subject: [PATCH] [fix](planner)isnull predicate can't be safely constant folded in inlineview (#25377) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit disable is null predicate constant fold rule for inline view consider sql select c.* from ( select a.*, b.x from test_insert a left join (select 'some_const_str' x from test_insert) b on true ) c where c.x is null; when push “c.x is null” into c, after folding constant rule, it will get empty result. Because x is 'some_const_str' and "x is null" will be evaluated to false. This is wrong. --- .../java/org/apache/doris/analysis/IsNullPredicate.java | 2 +- .../java/org/apache/doris/rewrite/FoldConstantsRule.java | 6 +++++- .../suites/query_p0/literal_view/lietral_test.groovy | 6 ++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/IsNullPredicate.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/IsNullPredicate.java index bf81cb4810..c67ca1b060 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/IsNullPredicate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/IsNullPredicate.java @@ -160,7 +160,7 @@ public class IsNullPredicate extends Predicate { // after outer join recursiveResetChildrenResult(!forPushDownPredicatesToView); final Expr childValue = getChild(0); - if (!(childValue instanceof LiteralExpr)) { + if (forPushDownPredicatesToView || !(childValue instanceof LiteralExpr)) { return this; } return childValue instanceof NullLiteral ? new BoolLiteral(!isNotNull) : new BoolLiteral(isNotNull); diff --git a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FoldConstantsRule.java b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FoldConstantsRule.java index 509e78ffb8..e28feabbf4 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/rewrite/FoldConstantsRule.java +++ b/fe/fe-core/src/main/java/org/apache/doris/rewrite/FoldConstantsRule.java @@ -29,6 +29,7 @@ import org.apache.doris.analysis.Expr; import org.apache.doris.analysis.InformationFunction; import org.apache.doris.analysis.LiteralExpr; import org.apache.doris.analysis.NullLiteral; +import org.apache.doris.analysis.SlotRef; import org.apache.doris.analysis.VariableExpr; import org.apache.doris.catalog.Env; import org.apache.doris.catalog.PrimitiveType; @@ -124,7 +125,10 @@ public class FoldConstantsRule implements ExprRewriteRule { return expr; } } - return expr.getResultValue(false); + // it may be wrong to fold constant value in inline view + // so pass the info to getResultValue method to let predicate itself + // to decide if it can fold constant value safely + return expr.getResultValue(expr instanceof SlotRef ? false : analyzer.isInlineViewAnalyzer()); } /** 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 905ed6696d..b19f33e293 100644 --- a/regression-test/suites/query_p0/literal_view/lietral_test.groovy +++ b/regression-test/suites/query_p0/literal_view/lietral_test.groovy @@ -140,4 +140,10 @@ suite("literal_view_test") { sql "select * from (select null as top) t where top = 5" result ([]) } + + sql """set enable_nereids_planner=false;""" + explain { + sql """ select c.* from ( select a.*, '' x from test_insert a left join test_insert b on true ) c where c.x is null; """ + notContains("VEMPTYSET") + } }