From 83acdc14f47a63c2bf7934f7859be75d79030d8b Mon Sep 17 00:00:00 2001 From: morrySnow <101034200+morrySnow@users.noreply.github.com> Date: Thu, 22 Aug 2024 13:57:39 +0800 Subject: [PATCH] [fix](Nereids) should push project through limit after eliminate union node (#39640) (#39755) pick from master #39640 otherwise: push limit through union could generate plan: limit +-- union |-- limit | +-- empty relation +-- limit +-- project and then eliminate union will generate plan: +-- limit +- project +-- limit +-- project it could not be processed by tranlator correctly --- .../doris/nereids/jobs/executor/Rewriter.java | 13 +++++++++++- .../push_limit_with_eliminate_union.groovy | 21 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 regression-test/suites/nereids_p0/union/push_limit_with_eliminate_union.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java index 9df26fce31f769..413a410ac59932 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/jobs/executor/Rewriter.java @@ -114,6 +114,7 @@ import org.apache.doris.nereids.rules.rewrite.PushDownLimit; import org.apache.doris.nereids.rules.rewrite.PushDownLimitDistinctThroughJoin; import org.apache.doris.nereids.rules.rewrite.PushDownLimitDistinctThroughUnion; +import org.apache.doris.nereids.rules.rewrite.PushDownProjectThroughLimit; import org.apache.doris.nereids.rules.rewrite.PushDownTopNDistinctThroughJoin; import org.apache.doris.nereids.rules.rewrite.PushDownTopNDistinctThroughUnion; import org.apache.doris.nereids.rules.rewrite.PushDownTopNThroughJoin; @@ -416,7 +417,17 @@ public class Rewriter extends AbstractBatchJobExecutor { topic("eliminate", // SORT_PRUNING should be applied after mergeLimit custom(RuleType.ELIMINATE_SORT, EliminateSort::new), - bottomUp(new EliminateEmptyRelation()) + bottomUp( + new EliminateEmptyRelation(), + // after eliminate empty relation under union, we could get + // limit + // +-- project + // +-- limit + // + project + // so, we need push project through limit to satisfy translator's assumptions + new PushDownFilterThroughProject(), + new PushDownProjectThroughLimit(), + new MergeProjects()) ), topic("agg rewrite", // these rules should be put after mv optimization to avoid mv matching fail diff --git a/regression-test/suites/nereids_p0/union/push_limit_with_eliminate_union.groovy b/regression-test/suites/nereids_p0/union/push_limit_with_eliminate_union.groovy new file mode 100644 index 00000000000000..1c9604a5ad6e24 --- /dev/null +++ b/regression-test/suites/nereids_p0/union/push_limit_with_eliminate_union.groovy @@ -0,0 +1,21 @@ +// 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("push_limit_with_eliminate_union") { + // this should not failed for [INTERNAL_ERROR]VSlotRef have invalid slot id + sql """(select 1 limit 0 union all select count() + 1) limit 1;""" +}