diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java index 7931705cc31d02..3737093d023f04 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Analyzer.java @@ -1335,6 +1335,14 @@ public void registerConjuncts(Expr e, boolean fromHavingClause) throws AnalysisE public void registerConjuncts(Expr e, boolean fromHavingClause, List ids) throws AnalysisException { for (Expr conjunct : e.getConjuncts()) { registerConjunct(conjunct); + if (!e.isConstant()) { + ArrayList tupleIds = Lists.newArrayList(); + ArrayList slotIds = Lists.newArrayList(); + e.getIds(tupleIds, slotIds); + if (tupleIds.isEmpty() && slotIds.isEmpty()) { + e.setBoundTupleIds(ids); + } + } if (ids != null) { for (TupleId id : ids) { registerConstantConjunct(id, conjunct); diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java index 6c614db71ca9d2..c087107e5e0e45 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/Expr.java @@ -298,6 +298,8 @@ public boolean apply(Expr arg) { protected boolean printSqlInParens = false; protected Optional exprName = Optional.empty(); + protected List boundTupleIds = null; + protected Expr() { super(); type = Type.INVALID; @@ -1293,6 +1295,9 @@ public boolean isBound(TupleId tid) { * Returns true if expr is fully bound by tids, otherwise false. */ public boolean isBoundByTupleIds(List tids) { + if (boundTupleIds != null && !boundTupleIds.isEmpty()) { + return boundTupleIds.stream().anyMatch(id -> tids.contains(id)); + } for (Expr child : children) { if (!child.isBoundByTupleIds(tids)) { return false; @@ -1301,6 +1306,10 @@ public boolean isBoundByTupleIds(List tids) { return true; } + public void setBoundTupleIds(List tids) { + boundTupleIds = tids; + } + /** * Returns true if expr have child bound by tids, otherwise false. diff --git a/regression-test/suites/correctness_p0/test_rand_filter.groovy b/regression-test/suites/correctness_p0/test_rand_filter.groovy new file mode 100644 index 00000000000000..ccad3161caf2ce --- /dev/null +++ b/regression-test/suites/correctness_p0/test_rand_filter.groovy @@ -0,0 +1,36 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +suite("test_rand_filter") { + sql"""set enable_nereids_planner=false;""" + sql """ DROP TABLE IF EXISTS test_rand_filter_t """ + sql """ + CREATE TABLE test_rand_filter_t ( + a int + ) + DUPLICATE KEY(a) + DISTRIBUTED BY HASH(a) BUCKETS 1 + PROPERTIES ( + "replication_num" = "1" + ) + """ + explain { + sql("""select * from test_rand_filter_t where rand() < 0.5 union all select * from test_rand_filter_t where rand() > 0.3;""") + notContains("AND") + } + sql """ DROP TABLE IF EXISTS test_rand_filter_t """ +}