Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e11826

Browse files
morrySnowgnehil
authored andcommitted
[fix](Nereids) could not run query with repeat node in cte (apache#26330) (apache#26531)
pick from master PR: apache#26330 commit id: a89477e ExpressionDeepCopier not process VirtualReference, so we generate inline plan with mistake.
1 parent eeee508 commit 3e11826

4 files changed

Lines changed: 52 additions & 12 deletions

File tree

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/copier/ExpressionDeepCopier.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@
2626
import org.apache.doris.nereids.trees.expressions.ScalarSubquery;
2727
import org.apache.doris.nereids.trees.expressions.Slot;
2828
import org.apache.doris.nereids.trees.expressions.SlotReference;
29+
import org.apache.doris.nereids.trees.expressions.StatementScopeIdGenerator;
30+
import org.apache.doris.nereids.trees.expressions.VirtualSlotReference;
31+
import org.apache.doris.nereids.trees.expressions.functions.scalar.GroupingScalarFunction;
2932
import org.apache.doris.nereids.trees.expressions.visitor.DefaultExpressionRewriter;
33+
import org.apache.doris.nereids.trees.plans.algebra.Repeat.GroupingSetShapes;
3034
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
3135

36+
import com.google.common.base.Function;
37+
3238
import java.util.List;
3339
import java.util.Map;
3440
import java.util.Optional;
@@ -75,6 +81,30 @@ public Expression visitSlotReference(SlotReference slotReference, DeepCopierCont
7581
}
7682
}
7783

84+
@Override
85+
public Expression visitVirtualReference(VirtualSlotReference virtualSlotReference, DeepCopierContext context) {
86+
Map<ExprId, ExprId> exprIdReplaceMap = context.exprIdReplaceMap;
87+
ExprId newExprId;
88+
if (exprIdReplaceMap.containsKey(virtualSlotReference.getExprId())) {
89+
newExprId = exprIdReplaceMap.get(virtualSlotReference.getExprId());
90+
} else {
91+
newExprId = StatementScopeIdGenerator.newExprId();
92+
}
93+
// according to VirtualReference generating logic in Repeat.java
94+
// generateVirtualGroupingIdSlot and generateVirtualSlotByFunction
95+
Optional<GroupingScalarFunction> newOriginExpression = virtualSlotReference.getOriginExpression()
96+
.map(func -> (GroupingScalarFunction) func.accept(this, context));
97+
Function<GroupingSetShapes, List<Long>> newFunction = newOriginExpression
98+
.<Function<GroupingSetShapes, List<Long>>>map(f -> f::computeVirtualSlotValue)
99+
.orElseGet(() -> GroupingSetShapes::computeVirtualGroupingIdValue);
100+
VirtualSlotReference newOne = new VirtualSlotReference(newExprId,
101+
virtualSlotReference.getName(), virtualSlotReference.getDataType(),
102+
virtualSlotReference.nullable(), virtualSlotReference.getQualifier(),
103+
newOriginExpression, newFunction);
104+
exprIdReplaceMap.put(virtualSlotReference.getExprId(), newOne.getExprId());
105+
return newOne;
106+
}
107+
78108
@Override
79109
public Expression visitExistsSubquery(Exists exists, DeepCopierContext context) {
80110
LogicalPlan logicalPlan = LogicalPlanDeepCopier.INSTANCE.deepCopy(exists.getQueryPlan(), context);

fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/algebra/Repeat.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ default List<Expression> getGroupByExpressions() {
6060

6161
static VirtualSlotReference generateVirtualGroupingIdSlot() {
6262
return new VirtualSlotReference(COL_GROUPING_ID, BigIntType.INSTANCE, Optional.empty(),
63-
shapes -> shapes.computeVirtualGroupingIdValue());
63+
GroupingSetShapes::computeVirtualGroupingIdValue);
6464
}
6565

6666
static VirtualSlotReference generateVirtualSlotByFunction(GroupingScalarFunction function) {
6767
return new VirtualSlotReference(
6868
generateVirtualSlotName(function), function.getDataType(), Optional.of(function),
69-
shapes -> function.computeVirtualSlotValue(shapes));
69+
function::computeVirtualSlotValue);
7070
}
7171

7272
/**
@@ -175,7 +175,7 @@ default List<Set<Integer>> getGroupingSetsIndexesInOutput() {
175175
if (index == null) {
176176
throw new AnalysisException("Can not find grouping set expression in output: " + expression);
177177
}
178-
if (groupingSetsIndex.contains(index)) {
178+
if (groupingSetIndex.contains(index)) {
179179
throw new AnalysisException("expression duplicate in grouping set: " + expression);
180180
}
181181
groupingSetIndex.add(index);
@@ -228,14 +228,6 @@ public GroupingSetShapes(Set<Expression> flattenGroupingSetExpression, List<Grou
228228
this.shapes = ImmutableList.copyOf(shapes);
229229
}
230230

231-
public GroupingSetShape getGroupingSetShape(int index) {
232-
return shapes.get(index);
233-
}
234-
235-
public Expression getExpression(int index) {
236-
return flattenGroupingSetExpression.get(index);
237-
}
238-
239231
// compute a long value that backend need to fill to the GROUPING_ID slot
240232
public List<Long> computeVirtualGroupingIdValue() {
241233
return shapes.stream()

regression-test/data/nereids_syntax_p0/cte.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ ASIA 1
8282
15
8383
29
8484

85+
-- !cte_with_repeat --
86+
\N \N 1
87+
\N 1 1
88+
\N 2 1
89+
\N 6 1
90+
1309892 \N 0
91+
1309892 1 0
92+
1309892 2 0
93+
1310179 \N 0
94+
1310179 6 0
95+
8596
-- !test --
8697
1 2023-08-25 00:00:00 10 10
8798
1 2023-08-25 01:00:00 20 30

regression-test/suites/nereids_syntax_p0/cte.groovy

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ suite("cte") {
253253
ORDER BY dd.s_suppkey;
254254
"""
255255

256-
sql "set experimental_enable_pipeline_engine=true"
256+
sql "set enable_pipeline_engine=true"
257257

258258
qt_cte14 """
259259
SELECT abs(dd.s_suppkey)
@@ -306,6 +306,13 @@ suite("cte") {
306306
notContains "MultiCastDataSinks"
307307
}
308308

309+
sql "WITH cte_0 AS ( SELECT 1 AS a ) SELECT * from cte_0 t1 LIMIT 10 UNION SELECT * from cte_0 t1 LIMIT 10"
310+
311+
qt_cte_with_repeat """
312+
with cte_0 as (select lo_orderkey, lo_linenumber, grouping_id(lo_orderkey) as id from lineorder group by cube(lo_orderkey, lo_linenumber))
313+
select * from cte_0 order by lo_orderkey, lo_linenumber, id
314+
"""
315+
309316
qt_test """
310317
SELECT * FROM (
311318
WITH temptable as (

0 commit comments

Comments
 (0)