Skip to content

Commit 5be9ff5

Browse files
airborne12claude
andcommitted
[fix](nereids) Give all virtual columns their new ids before copying them
### What problem does this PR solve? Issue Number: None Related PR: apache#69399 Problem Summary: With experimental_enable_virtual_slot_for_cse=true, a virtual column can refer to another one, and PushDownVirtualColumnsIntoOlapScan does not list them in dependency order, e.g. [v2 := v1 * v1, v1 := x + y]. The copier copied the virtual columns in list order. When it copied v2, the id of v1 still mapped to itself, so the copy of v2 referred to the old v1, which the copied scan does not output. The copier now maps every virtual column to its new id first and then copies their expressions, which reuse those ids. ### Release note None ### Check List (For Author) - Test: Unit Test - LogicalPlanDeepCopierTest.testDeepCopyOlapScanWithDependentVirtualColumns copies a scan whose virtual columns refer to each other, in both orders. It fails with the previous commit, where "(v1#10002 * v1#10002) AS v2" refers to the old v1, and passes now. LogicalPlanDeepCopierTest, CTEInlineTest, OrExpansionTest, SplitMultiDistinctTest, DecomposeRepeatWithPreAggregationTest, LogicalOlapScanTest, IvmDeltaRewriteHelperTest and PushDownVirtualColumnsIntoOlapScanTest pass. Checkstyle passes. The regression test was not run again: it creates no virtual columns that refer to each other. - Behavior changed: No - Does this need documentation: No Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent be40091 commit 5be9ff5

2 files changed

Lines changed: 24 additions & 10 deletions

File tree

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import org.apache.doris.nereids.exceptions.AnalysisException;
2121
import org.apache.doris.nereids.hint.DistributeHint;
2222
import org.apache.doris.nereids.properties.OrderKey;
23-
import org.apache.doris.nereids.trees.expressions.Alias;
2423
import org.apache.doris.nereids.trees.expressions.ExprId;
2524
import org.apache.doris.nereids.trees.expressions.Expression;
2625
import org.apache.doris.nereids.trees.expressions.MarkJoinSlotReference;
@@ -114,16 +113,12 @@ public Plan visitLogicalCatalogRelation(LogicalCatalogRelation catalogRelation,
114113
}
115114
updateReplaceMapWithOutput(catalogRelation, newRelation, context.exprIdReplaceMap);
116115
if (!catalogRelation.getVirtualColumns().isEmpty()) {
117-
// Each virtual column gets a new id, and its old slot maps to that id.
116+
// A virtual column can refer to the others, so each one gets its new id before any is copied.
117+
for (NamedExpression column : catalogRelation.getVirtualColumns()) {
118+
context.exprIdReplaceMap.put(column.getExprId(), StatementScopeIdGenerator.newExprId());
119+
}
118120
List<NamedExpression> virtualColumns = catalogRelation.getVirtualColumns().stream()
119-
.map(column -> {
120-
NamedExpression newColumn = column instanceof Alias
121-
? new Alias(ExpressionDeepCopier.INSTANCE.deepCopy(((Alias) column).child(),
122-
context), column.getName())
123-
: (NamedExpression) ExpressionDeepCopier.INSTANCE.deepCopy(column, context);
124-
context.exprIdReplaceMap.put(column.getExprId(), newColumn.getExprId());
125-
return newColumn;
126-
})
121+
.map(column -> (NamedExpression) ExpressionDeepCopier.INSTANCE.deepCopy(column, context))
127122
.collect(ImmutableList.toImmutableList());
128123
newRelation = newRelation.withVirtualColumns(virtualColumns);
129124
if (newRelation instanceof LogicalOlapScan) {

‎fe/fe-core/src/test/java/org/apache/doris/nereids/trees/copier/LogicalPlanDeepCopierTest.java‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919

2020
import org.apache.doris.catalog.SchemaTable;
2121
import org.apache.doris.nereids.properties.OrderKey;
22+
import org.apache.doris.nereids.trees.expressions.Add;
2223
import org.apache.doris.nereids.trees.expressions.Alias;
2324
import org.apache.doris.nereids.trees.expressions.EqualTo;
2425
import org.apache.doris.nereids.trees.expressions.Expression;
26+
import org.apache.doris.nereids.trees.expressions.Multiply;
2527
import org.apache.doris.nereids.trees.expressions.NamedExpression;
2628
import org.apache.doris.nereids.trees.expressions.Slot;
2729
import org.apache.doris.nereids.trees.expressions.SlotReference;
@@ -111,6 +113,23 @@ public void testDeepCopyOlapScanWithVirtualColumn() {
111113
}
112114
}
113115

116+
@Test
117+
public void testDeepCopyOlapScanWithDependentVirtualColumns() {
118+
LogicalOlapScan scan = PlanConstructor.newLogicalOlapScan(0, "a", 0);
119+
Slot id = scan.getOutput().get(0);
120+
Alias sum = new Alias(new Add(id, id), "v1");
121+
Alias product = new Alias(new Multiply(sum.toSlot(), sum.toSlot()), "v2");
122+
// A virtual column can refer to one that comes after it.
123+
for (List<NamedExpression> virtualColumns : ImmutableList.<List<NamedExpression>>of(
124+
ImmutableList.of(sum, product), ImmutableList.of(product, sum))) {
125+
LogicalOlapScan copy = (LogicalOlapScan) scan.withVirtualColumns(virtualColumns)
126+
.accept(LogicalPlanDeepCopier.INSTANCE, new DeepCopierContext());
127+
for (NamedExpression column : copy.getVirtualColumns()) {
128+
Assertions.assertTrue(copy.getOutputSet().containsAll(column.getInputSlots()), column.toString());
129+
}
130+
}
131+
}
132+
114133
@Test
115134
public void testDeepCopyOlapScanWithNonFirstOperativeSlot() {
116135
LogicalOlapScan relationPlan = PlanConstructor.newLogicalOlapScan(0, "a", 0);

0 commit comments

Comments
 (0)