From 07c2816cabe7c9b141491941a0aa5a601d8d8f45 Mon Sep 17 00:00:00 2001 From: minghong Date: Sun, 19 Nov 2023 20:14:36 +0800 Subject: [PATCH] [branch2.0](nereids)Pick #26873 #25769: partition prune fix (#27222) --- .../TryEliminateUninterestedPredicates.java | 5 +- .../rewrite/PruneOlapScanPartitionTest.java | 2 + .../test_partition_unique_model.groovy | 58 +++++++++++++++++++ 3 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 regression-test/suites/nereids_rules_p0/partition_prune/test_partition_unique_model.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/TryEliminateUninterestedPredicates.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/TryEliminateUninterestedPredicates.java index 79bd0a54bf5830..b9c9f3732e9512 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/TryEliminateUninterestedPredicates.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/TryEliminateUninterestedPredicates.java @@ -109,7 +109,8 @@ public Expression visit(Expression originExpr, Context parentContext) { public Expression visitAnd(And and, Context parentContext) { Expression left = and.left(); Context leftContext = new Context(); - Expression newLeft = this.visit(left, leftContext); + Expression newLeft = left.accept(this, leftContext); + if (leftContext.childrenContainsNonInterestedSlots) { newLeft = BooleanLiteral.TRUE; } @@ -122,7 +123,7 @@ public Expression visitAnd(And and, Context parentContext) { } Expression expr = new And(newLeft, newRight).accept(FoldConstantRuleOnFE.INSTANCE, expressionRewriteContext); parentContext.childrenContainsInterestedSlots = - rightContext.childrenContainsInterestedSlots || leftContext.childrenContainsInterestedSlots; + rightContext.childrenContainsInterestedSlots || leftContext.childrenContainsInterestedSlots; return expr; } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PruneOlapScanPartitionTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PruneOlapScanPartitionTest.java index 85fde57b517fa2..de54a05bc5e8c8 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PruneOlapScanPartitionTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/rewrite/PruneOlapScanPartitionTest.java @@ -228,6 +228,8 @@ public void prunePartitionWithOrPredicate() { public void canNotPruneComplexPredicate() { test("test_range_parts", "(part = 10) or (part + id = 1)", 4); test("test_range_parts", "(part + id = 1) and (part = 4)", 1); + test("test_range_parts", "(part = 2) and (part <> id)", 1); + test("test_range_parts", "(part = 2) or (part <> id)", 4); } @Test diff --git a/regression-test/suites/nereids_rules_p0/partition_prune/test_partition_unique_model.groovy b/regression-test/suites/nereids_rules_p0/partition_prune/test_partition_unique_model.groovy new file mode 100644 index 00000000000000..3fc77f2cf9cd20 --- /dev/null +++ b/regression-test/suites/nereids_rules_p0/partition_prune/test_partition_unique_model.groovy @@ -0,0 +1,58 @@ +// 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_partition_unique_model") { + // filter about invisible column "DORIS_DELETE_SIGN = 0" has no impaction on partition pruning + String db = context.config.getDbNameByFile(context.file) + sql "use ${db}" + sql "SET enable_nereids_planner=true" + sql "SET enable_fallback_to_original_planner=false" + sql "set partition_pruning_expand_threshold=10;" + sql "drop table if exists xinan;" + sql """ + create table xinan + ( + init_date int null + ) + engine=olap + unique key(init_date) + partition by range(init_date) + (partition p202209 values[("20220901"), ("20221001")), + partition p202210 values[("20221001"), ("20221101"))) + distributed by hash (init_date) buckets auto + properties( + "replication_allocation" = "tag.location.default: 1" + ); + """ + sql "insert into xinan values(20220901), (20221003);" + + explain { + sql "select * from xinan where init_date >=20221001;" + contains "partitions=1/2" + } + + explain { + sql "select * from xinan where init_date<20221101;" + contains "partitions=2/2" + } + + explain { + sql "select * from xinan where init_date >=20221001 and init_date<20221101;" + contains "partitions=1/2" + } + +} \ No newline at end of file