From 9a10769af04274d2aeba2bbb6e3af0d377db70f1 Mon Sep 17 00:00:00 2001 From: morrySnow Date: Tue, 19 Mar 2024 20:48:19 +0800 Subject: [PATCH] [fix](Nereids) filter-limit-project translate to wrong plan --- .../translator/PhysicalPlanTranslator.java | 9 ++- .../nereids_p0/limit/filterLimitProject.out | 8 +++ .../limit/filterLimitProject.groovy | 58 +++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 regression-test/data/nereids_p0/limit/filterLimitProject.out create mode 100644 regression-test/suites/nereids_p0/limit/filterLimitProject.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java index 4151f7d957a69d..d9f1b4b2d5d02a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/glue/translator/PhysicalPlanTranslator.java @@ -1183,7 +1183,14 @@ public PlanFragment visitPhysicalFilter(PhysicalFilter filter, P } PlanNode planNode = inputFragment.getPlanRoot(); - if (planNode instanceof ExchangeNode || planNode instanceof SortNode || planNode instanceof UnionNode) { + Plan child = filter.child(); + while (child instanceof PhysicalLimit) { + child = ((PhysicalLimit) child).child(); + } + if (planNode instanceof ExchangeNode || planNode instanceof SortNode || planNode instanceof UnionNode + // this means we have filter->limit->project, need a SelectNode + || (child instanceof PhysicalProject + && !((PhysicalProject) child).hasPushedDownToProjectionFunctions())) { // the three nodes don't support conjuncts, need create a SelectNode to filter data SelectNode selectNode = new SelectNode(context.nextPlanNodeId(), planNode); selectNode.setNereidsId(filter.getId()); diff --git a/regression-test/data/nereids_p0/limit/filterLimitProject.out b/regression-test/data/nereids_p0/limit/filterLimitProject.out new file mode 100644 index 00000000000000..29359255a3f16c --- /dev/null +++ b/regression-test/data/nereids_p0/limit/filterLimitProject.out @@ -0,0 +1,8 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !test_translator -- +0 +0 +0 +0 +0 + diff --git a/regression-test/suites/nereids_p0/limit/filterLimitProject.groovy b/regression-test/suites/nereids_p0/limit/filterLimitProject.groovy new file mode 100644 index 00000000000000..e30285a196ef0d --- /dev/null +++ b/regression-test/suites/nereids_p0/limit/filterLimitProject.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_filter_limit_project_translate", "query") { + sql "SET enable_nereids_planner=true" + sql "SET enable_fallback_to_original_planner=false" + + sql """ + drop table if exists t1; + """ + + sql """ + create table t1 ( + pk int, + c1 int , + c2 varchar(10) + ) engine=olap + UNIQUE KEY(pk) + distributed by hash(pk) buckets 10 + properties("replication_num" = "1"); + """ + + sql """ + insert into t1(pk,c1,c2) values (0,null,'k'),(1,2,'him'),(2,2,'think'),(3,null,'not'),(4,1,'k'),(5,null,'so'),(6,null,'j'),(7,5,'p'),(8,0,'did'); + """ + + qt_test_translator """ + WITH cte1 AS ( + SELECT FIRST_VALUE (t1.`pk`) OVER( + ORDER BY t1.`pk` + ) as pk + FROM t1 + LIMIT 66666666 + ), cte2 AS ( + SELECT MIN (t1.`pk`) AS `pk` + FROM t1 + ) + SELECT cte1.`pk` AS pk1 + FROM cte1 + WHERE cte1.`pk` < 4 + ORDER BY 1 ASC + LIMIT 100 OFFSET 4; + """ +}