From 17a1ce5ed3bccba3a5ffa5808e327bc6d33d0e98 Mon Sep 17 00:00:00 2001 From: starocean999 <40539150+starocean999@users.noreply.github.com> Date: Wed, 22 Mar 2023 11:19:32 +0800 Subject: [PATCH] [fix](nereids) add a project node above sort node to eliminate unused order by keys (#17913) if the order by keys are not simple slot in sort node, the order by exprs have to been added to sort node's output tuple. In that case, we need add a project node above sort node to eliminate the unused order by exprs. for example: ```sql WITH t0 AS (SELECT DATE_FORMAT(date, '%Y%m%d') AS date FROM cir_1756_t1 ), t3 AS (SELECT date_format(date, '%Y%m%d') AS `date` FROM `cir_1756_t2` GROUP BY date_format(date, '%Y%m%d') **ORDER BY date_format(date, '%Y%m%d')** ) SELECT t0.date FROM t0 LEFT JOIN t3 ON t0.date = t3.date; ``` before: ``` +--------------------------------------------------------------------------------------------------------------------------------------------------+ | Explain String | +--------------------------------------------------------------------------------------------------------------------------------------------------+ | LogicalProject[159] ( distinct=false, projects=[date#1], excepts=[], canEliminate=true ) | | +--LogicalJoin[158] ( type=LEFT_OUTER_JOIN, markJoinSlotReference=Optional.empty, hashJoinConjuncts=[(date#1 = date#3)], otherJoinConjuncts=[] ) | | |--LogicalProject[151] ( distinct=false, projects=[date_format(date#0, '%Y%m%d') AS `date`#1], excepts=[], canEliminate=true ) | | | +--LogicalOlapScan ( qualified=default_cluster:bugfix.cir_1756_t1, indexName=cir_1756_t1, selectedIndexId=412339, preAgg=ON ) | | +--LogicalSort[157] ( orderKeys=[date_format(cast(date#3 as DATETIME), '%Y%m%d') asc null first] ) | | +--LogicalAggregate[156] ( groupByExpr=[date#3], outputExpr=[date#3], hasRepeat=false ) | | +--LogicalProject[155] ( distinct=false, projects=[date_format(date#2, '%Y%m%d') AS `date`#3], excepts=[], canEliminate=true ) | | +--LogicalOlapScan ( qualified=default_cluster:bugfix.cir_1756_t2, indexName=cir_1756_t2, selectedIndexId=412352, preAgg=ON ) | +--------------------------------------------------------------------------------------------------------------------------------------------------+ ``` after: ``` +--------------------------------------------------------------------------------------------------------------------------------------------------+ | Explain String | +--------------------------------------------------------------------------------------------------------------------------------------------------+ | LogicalProject[171] ( distinct=false, projects=[date#2], excepts=[], canEliminate=true ) | | +--LogicalJoin[170] ( type=LEFT_OUTER_JOIN, markJoinSlotReference=Optional.empty, hashJoinConjuncts=[(date#2 = date#4)], otherJoinConjuncts=[] ) | | |--LogicalProject[162] ( distinct=false, projects=[date_format(date#0, '%Y%m%d') AS `date`#2], excepts=[], canEliminate=true ) | | | +--LogicalOlapScan ( qualified=default_cluster:bugfix.cir_1756_t1, indexName=cir_1756_t1, selectedIndexId=1049812, preAgg=ON ) | | +--LogicalProject[169] ( distinct=false, projects=[date#4], excepts=[], canEliminate=false ) | | +--LogicalSort[168] ( orderKeys=[date_format(cast(date#4 as DATETIME), '%Y%m%d') asc null first] ) | | +--LogicalAggregate[167] ( groupByExpr=[date#4], outputExpr=[date#4], hasRepeat=false ) | | +--LogicalProject[166] ( distinct=false, projects=[date_format(date#3, '%Y%m%d') AS `date`#4], excepts=[], canEliminate=true ) | | +--LogicalOlapScan ( qualified=default_cluster:bugfix.cir_1756_t2, indexName=cir_1756_t2, selectedIndexId=1049825, preAgg=ON ) | +--------------------------------------------------------------------------------------------------------------------------------------------------+ ``` --- .../apache/doris/nereids/rules/RuleType.java | 1 + .../expression/rewrite/ExpressionRewrite.java | 2 +- .../rules/rewrite/logical/ColumnPruning.java | 1 + .../ExtractAndNormalizeWindowExpression.java | 10 +-- .../rules/rewrite/logical/MergeProjects.java | 6 +- .../rewrite/logical/PruneSortColumns.java | 45 ++++++++++ ...latedFilterUnderApplyAggregateProject.java | 2 +- .../logical/PullUpProjectUnderApply.java | 2 +- .../logical/PushdownAliasThroughJoin.java | 6 +- .../logical/PushdownFilterThroughProject.java | 4 +- .../logical/PushdownProjectThroughLimit.java | 2 +- .../UnCorrelatedApplyProjectFilter.java | 2 +- .../trees/plans/logical/LogicalProject.java | 5 ++ .../trees/plans/logical/LogicalSort.java | 30 ++++++- .../subquery/test_inlineview_with_project.out | 4 + .../test_inlineview_with_project.groovy | 82 +++++++++++++++++++ 16 files changed, 182 insertions(+), 22 deletions(-) create mode 100644 fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneSortColumns.java create mode 100644 regression-test/data/nereids_p0/subquery/test_inlineview_with_project.out create mode 100644 regression-test/suites/nereids_p0/subquery/test_inlineview_with_project.groovy diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java index 9b696f8235c0f3..1053393f0c2b46 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/RuleType.java @@ -133,6 +133,7 @@ public enum RuleType { COLUMN_PRUNE_FILTER_CHILD(RuleTypeClass.REWRITE), PRUNE_ONE_ROW_RELATION_COLUMN(RuleTypeClass.REWRITE), COLUMN_PRUNE_SORT_CHILD(RuleTypeClass.REWRITE), + COLUMN_PRUNE_SORT(RuleTypeClass.REWRITE), COLUMN_PRUNE_JOIN_CHILD(RuleTypeClass.REWRITE), COLUMN_PRUNE_REPEAT_CHILD(RuleTypeClass.REWRITE), // expression of plan rewrite diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/ExpressionRewrite.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/ExpressionRewrite.java index a8d1a831cb5b1f..114e244c3a5621 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/ExpressionRewrite.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rewrite/ExpressionRewrite.java @@ -130,7 +130,7 @@ public Rule build() { if (projects.equals(newProjects)) { return project; } - return new LogicalProject<>(newProjects, project.child()); + return project.withProjectsAndChild(newProjects, project.child()); }).toRule(RuleType.REWRITE_PROJECT_EXPRESSION); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/ColumnPruning.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/ColumnPruning.java index fabcb00b36cf85..96a4d8cbc24486 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/ColumnPruning.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/ColumnPruning.java @@ -35,6 +35,7 @@ public List buildRules() { new PruneFilterChildColumns().build(), new PruneAggChildColumns().build(), new PruneJoinChildrenColumns().build(), + new PruneSortColumns().build(), new PruneSortChildColumns().build(), new MergeProjects().build(), new PruneRepeatChildColumns().build() diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/ExtractAndNormalizeWindowExpression.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/ExtractAndNormalizeWindowExpression.java index 75dba0467a9cb2..718d6acc6e8b55 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/ExtractAndNormalizeWindowExpression.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/ExtractAndNormalizeWindowExpression.java @@ -28,7 +28,6 @@ import org.apache.doris.nereids.trees.expressions.functions.agg.AggregateFunction; import org.apache.doris.nereids.trees.plans.Plan; import org.apache.doris.nereids.trees.plans.logical.LogicalAggregate; -import org.apache.doris.nereids.trees.plans.logical.LogicalProject; import org.apache.doris.nereids.trees.plans.logical.LogicalWindow; import org.apache.doris.nereids.util.ExpressionUtils; @@ -65,10 +64,11 @@ public Rule build() { boolean needAggregate = bottomProjects.stream().anyMatch(expr -> expr.anyMatch(AggregateFunction.class::isInstance)); if (needAggregate) { - normalizedChild = new LogicalAggregate<>( - ImmutableList.of(), ImmutableList.copyOf(bottomProjects), project.child()); + normalizedChild = new LogicalAggregate<>(ImmutableList.of(), + ImmutableList.copyOf(bottomProjects), project.child()); } else { - normalizedChild = new LogicalProject<>(ImmutableList.copyOf(bottomProjects), project.child()); + normalizedChild = project.withProjectsAndChild( + ImmutableList.copyOf(bottomProjects), project.child()); } } @@ -89,7 +89,7 @@ public Rule build() { // 3. handle top projects List topProjects = ctxForWindows.normalizeToUseSlotRef(normalizedOutputs1); - return new LogicalProject<>(topProjects, normalizedLogicalWindow); + return project.withProjectsAndChild(topProjects, normalizedLogicalWindow); }).toRule(RuleType.EXTRACT_AND_NORMALIZE_WINDOW_EXPRESSIONS); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeProjects.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeProjects.java index 4afbbba883bc36..427a86be63723b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeProjects.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/MergeProjects.java @@ -21,7 +21,6 @@ import org.apache.doris.nereids.rules.RuleType; import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; import org.apache.doris.nereids.trees.expressions.NamedExpression; -import org.apache.doris.nereids.trees.plans.Plan; import org.apache.doris.nereids.trees.plans.logical.LogicalProject; import java.util.List; @@ -47,9 +46,10 @@ public class MergeProjects extends OneRewriteRuleFactory { @Override public Rule build() { return logicalProject(logicalProject()).then(project -> { - LogicalProject childProject = project.child(); + LogicalProject childProject = project.child(); List projectExpressions = project.mergeProjections(childProject); - return new LogicalProject<>(projectExpressions, childProject.child(0)); + LogicalProject newProject = childProject.canEliminate() ? project : childProject; + return newProject.withProjectsAndChild(projectExpressions, childProject.child(0)); }).toRule(RuleType.MERGE_PROJECTS); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneSortColumns.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneSortColumns.java new file mode 100644 index 00000000000000..f77a16b6624bf9 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PruneSortColumns.java @@ -0,0 +1,45 @@ +// 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. + +package org.apache.doris.nereids.rules.rewrite.logical; + +import org.apache.doris.nereids.rules.Rule; +import org.apache.doris.nereids.rules.RuleType; +import org.apache.doris.nereids.rules.rewrite.OneRewriteRuleFactory; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; + +import com.google.common.collect.ImmutableList; + +import java.util.stream.Collectors; + +/** + the sort node will create new slots for order by keys if the order by keys is not in the output + so need create a project above sort node to prune the unnecessary order by keys + */ +public class PruneSortColumns extends OneRewriteRuleFactory { + @Override + public Rule build() { + return logicalSort() + .when(sort -> !sort.isOrderKeysPruned() && !sort.getOutputSet() + .containsAll(sort.getOrderKeys().stream() + .map(orderKey -> orderKey.getExpr()).collect(Collectors.toSet()))) + .then(sort -> { + return new LogicalProject(sort.getOutput(), ImmutableList.of(), false, + sort.withOrderKeysPruned(true)); + }).toRule(RuleType.COLUMN_PRUNE_SORT); + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PullUpCorrelatedFilterUnderApplyAggregateProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PullUpCorrelatedFilterUnderApplyAggregateProject.java index e1cf58c789cbf8..9c2b22aa3e8aaa 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PullUpCorrelatedFilterUnderApplyAggregateProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PullUpCorrelatedFilterUnderApplyAggregateProject.java @@ -75,7 +75,7 @@ public Rule build() { } }); - LogicalProject newProject = new LogicalProject<>(newProjects, filter.child()); + LogicalProject newProject = project.withProjectsAndChild(newProjects, filter.child()); LogicalFilter newFilter = new LogicalFilter<>(filter.getConjuncts(), newProject); LogicalAggregate newAgg = agg.withChildren(ImmutableList.of(newFilter)); return new LogicalApply<>(apply.getCorrelationSlot(), apply.getSubqueryExpr(), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PullUpProjectUnderApply.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PullUpProjectUnderApply.java index a6bb7ce924dc18..bca11e2351bda2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PullUpProjectUnderApply.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PullUpProjectUnderApply.java @@ -64,7 +64,7 @@ public Rule build() { if (apply.getSubqueryExpr() instanceof ScalarSubquery) { newProjects.add(project.getProjects().get(0)); } - return new LogicalProject(newProjects, newCorrelate); + return project.withProjectsAndChild(newProjects, newCorrelate); }).toRule(RuleType.PULL_UP_PROJECT_UNDER_APPLY); } } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownAliasThroughJoin.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownAliasThroughJoin.java index b5c027df44a91f..e35493797e145a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownAliasThroughJoin.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownAliasThroughJoin.java @@ -90,12 +90,12 @@ public Rule build() { if (leftOutput.equals(leftProjects)) { left = join.left(); } else { - left = new LogicalProject<>(leftProjects, join.left()); + left = project.withProjectsAndChild(leftProjects, join.left()); } if (rightOutput.equals(rightProjects)) { right = join.right(); } else { - right = new LogicalProject<>(rightProjects, join.right()); + right = project.withProjectsAndChild(rightProjects, join.right()); } // If condition use alias slot, we should replace condition @@ -109,7 +109,7 @@ public Rule build() { List newOther = replaceJoinConjuncts(join.getOtherJoinConjuncts(), replaceMap); Plan newJoin = join.withConjunctsChildren(newHash, newOther, left, right); - return new LogicalProject<>(newProjects, newJoin); + return project.withProjectsAndChild(newProjects, newJoin); }).toRule(RuleType.PUSHDOWN_ALIAS_THROUGH_JOIN); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownFilterThroughProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownFilterThroughProject.java index 8712340f7f79e5..12cae4772a8171 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownFilterThroughProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownFilterThroughProject.java @@ -44,7 +44,7 @@ public List buildRules() { RuleType.PUSHDOWN_FILTER_THROUGH_PROJECT.build( logicalFilter(logicalProject()).then(filter -> { LogicalProject project = filter.child(); - return new LogicalProject<>( + return project.withProjectsAndChild( project.getProjects(), new LogicalFilter<>( ExpressionUtils.replace(filter.getConjuncts(), project.getAliasToProducer()), @@ -60,7 +60,7 @@ public List buildRules() { LogicalLimit> limit = filter.child(); LogicalProject project = limit.child(); - return new LogicalProject<>( + return project.withProjectsAndChild( project.getProjects(), new LogicalFilter<>( ExpressionUtils.replace(filter.getConjuncts(), project.getAliasToProducer()), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownProjectThroughLimit.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownProjectThroughLimit.java index c1705250a54a30..c8818310ab23ea 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownProjectThroughLimit.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/PushdownProjectThroughLimit.java @@ -52,7 +52,7 @@ public Rule build() { LogicalProject> logicalProject = ctx.root; LogicalLimit logicalLimit = logicalProject.child(); return new LogicalLimit<>(logicalLimit.getLimit(), logicalLimit.getOffset(), - logicalLimit.getPhase(), new LogicalProject<>(logicalProject.getProjects(), + logicalLimit.getPhase(), logicalProject.withProjectsAndChild(logicalProject.getProjects(), logicalLimit.child())); }).toRule(RuleType.PUSHDOWN_PROJECT_THROUGH_LIMIT); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/UnCorrelatedApplyProjectFilter.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/UnCorrelatedApplyProjectFilter.java index 0028c53bc7ec9d..c4cff232930b03 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/UnCorrelatedApplyProjectFilter.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/rewrite/logical/UnCorrelatedApplyProjectFilter.java @@ -87,7 +87,7 @@ public Rule build() { .filter(e -> !projects.contains(e)) .map(NamedExpression.class::cast) .forEach(projects::add); - LogicalProject newProject = new LogicalProject(projects, child); + LogicalProject newProject = project.withProjectsAndChild(projects, child); return new LogicalApply<>(apply.getCorrelationSlot(), apply.getSubqueryExpr(), ExpressionUtils.optionalAnd(correlatedPredicate), apply.getMarkJoinSlotReference(), apply.getSubCorrespondingConject(), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalProject.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalProject.java index ab18b72c1c05b8..8961f3211a9aac 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalProject.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalProject.java @@ -175,6 +175,11 @@ public LogicalProject withChildren(List children) { return new LogicalProject<>(projects, excepts, canEliminate, children.get(0), isDistinct); } + public LogicalProject withProjectsAndChild(List projects, Plan child) { + return new LogicalProject<>(projects, excepts, canEliminate, + Optional.empty(), Optional.empty(), child, isDistinct); + } + @Override public LogicalProject withGroupExpression(Optional groupExpression) { return new LogicalProject<>(projects, excepts, canEliminate, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSort.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSort.java index 50986e30fb0c78..1fb19b722cba4a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSort.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/logical/LogicalSort.java @@ -46,17 +46,29 @@ public class LogicalSort extends LogicalUnary orderKeys; + private final boolean orderKeysPruned; + public LogicalSort(List orderKeys, CHILD_TYPE child) { this(orderKeys, Optional.empty(), Optional.empty(), child); } + public LogicalSort(List orderKeys, CHILD_TYPE child, boolean orderKeysPruned) { + this(orderKeys, Optional.empty(), Optional.empty(), child, orderKeysPruned); + } + /** * Constructor for LogicalSort. */ public LogicalSort(List orderKeys, Optional groupExpression, Optional logicalProperties, CHILD_TYPE child) { + this(orderKeys, groupExpression, logicalProperties, child, false); + } + + public LogicalSort(List orderKeys, Optional groupExpression, + Optional logicalProperties, CHILD_TYPE child, boolean orderKeysPruned) { super(PlanType.LOGICAL_SORT, groupExpression, logicalProperties, child); this.orderKeys = ImmutableList.copyOf(Objects.requireNonNull(orderKeys, "orderKeys can not be null")); + this.orderKeysPruned = orderKeysPruned; } @Override @@ -68,6 +80,10 @@ public List getOrderKeys() { return orderKeys; } + public boolean isOrderKeysPruned() { + return orderKeysPruned; + } + @Override public String toString() { return Utils.toSqlString("LogicalSort[" + id.asInt() + "]", @@ -106,21 +122,27 @@ public List getExpressions() { @Override public LogicalSort withChildren(List children) { Preconditions.checkArgument(children.size() == 1); - return new LogicalSort<>(orderKeys, children.get(0)); + return new LogicalSort<>(orderKeys, children.get(0), orderKeysPruned); } @Override public LogicalSort withGroupExpression(Optional groupExpression) { - return new LogicalSort<>(orderKeys, groupExpression, Optional.of(getLogicalProperties()), child()); + return new LogicalSort<>(orderKeys, groupExpression, Optional.of(getLogicalProperties()), child(), + orderKeysPruned); } @Override public LogicalSort withLogicalProperties(Optional logicalProperties) { - return new LogicalSort<>(orderKeys, Optional.empty(), logicalProperties, child()); + return new LogicalSort<>(orderKeys, Optional.empty(), logicalProperties, child(), false); } public LogicalSort withOrderKeys(List orderKeys) { return new LogicalSort<>(orderKeys, Optional.empty(), - Optional.of(getLogicalProperties()), child()); + Optional.of(getLogicalProperties()), child(), false); + } + + public LogicalSort withOrderKeysPruned(boolean orderKeysPruned) { + return new LogicalSort<>(orderKeys, groupExpression, Optional.of(getLogicalProperties()), child(), + orderKeysPruned); } } diff --git a/regression-test/data/nereids_p0/subquery/test_inlineview_with_project.out b/regression-test/data/nereids_p0/subquery/test_inlineview_with_project.out new file mode 100644 index 00000000000000..f6fa08907ec294 --- /dev/null +++ b/regression-test/data/nereids_p0/subquery/test_inlineview_with_project.out @@ -0,0 +1,4 @@ +-- This file is automatically generated. You should know what you did if you want to edit this +-- !select -- +20200202 + diff --git a/regression-test/suites/nereids_p0/subquery/test_inlineview_with_project.groovy b/regression-test/suites/nereids_p0/subquery/test_inlineview_with_project.groovy new file mode 100644 index 00000000000000..b58c86807c7b4a --- /dev/null +++ b/regression-test/suites/nereids_p0/subquery/test_inlineview_with_project.groovy @@ -0,0 +1,82 @@ +// 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("inlineview_with_project") { + sql "SET enable_nereids_planner=true" + sql "SET enable_fallback_to_original_planner=false" + sql """ + drop table if exists cir_1756_t1; + """ + + sql """ + drop table if exists cir_1756_t2; + """ + + sql """ + create table cir_1756_t1 (`date` date not null) + ENGINE=OLAP + DISTRIBUTED BY HASH(`date`) BUCKETS 5 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", + "storage_format" = "V2" + ); + """ + + sql """ + create table cir_1756_t2 ( `date` date not null ) + ENGINE=OLAP + DISTRIBUTED BY HASH(`date`) BUCKETS 5 + PROPERTIES ( + "replication_allocation" = "tag.location.default: 1", + "in_memory" = "false", + "storage_format" = "V2" + ); + """ + + sql """ + insert into cir_1756_t1 values("2020-02-02"); + """ + + sql """ + insert into cir_1756_t2 values("2020-02-02"); + """ + + qt_select """ + WITH t0 AS + (SELECT DATE_FORMAT(date, + '%Y%m%d') AS date + FROM cir_1756_t1 ), t3 AS + (SELECT date_format(date, + '%Y%m%d') AS `date` + FROM `cir_1756_t2` + GROUP BY date_format(date, '%Y%m%d') + ORDER BY date_format(date, '%Y%m%d') ) + SELECT t0.date + FROM t0 + LEFT JOIN t3 + ON t0.date = t3.date; + """ + + sql """ + drop table if exists cir_1756_t1; + """ + + sql """ + drop table if exists cir_1756_t2; + """ +}