|
20 | 20 | import org.apache.doris.nereids.exceptions.AnalysisException; |
21 | 21 | import org.apache.doris.nereids.hint.DistributeHint; |
22 | 22 | import org.apache.doris.nereids.properties.OrderKey; |
23 | | -import org.apache.doris.nereids.trees.expressions.Alias; |
24 | 23 | import org.apache.doris.nereids.trees.expressions.ExprId; |
25 | 24 | import org.apache.doris.nereids.trees.expressions.Expression; |
26 | 25 | import org.apache.doris.nereids.trees.expressions.MarkJoinSlotReference; |
@@ -113,22 +112,24 @@ public Plan visitLogicalCatalogRelation(LogicalCatalogRelation catalogRelation, |
113 | 112 | newRelation = ((LogicalOlapScan) newRelation).withPartitionPruned(false); |
114 | 113 | } |
115 | 114 | updateReplaceMapWithOutput(catalogRelation, newRelation, context.exprIdReplaceMap); |
116 | | - List<NamedExpression> virtualColumns = catalogRelation.getVirtualColumns().stream() |
117 | | - .map(e -> { |
118 | | - if (e instanceof Alias) { |
119 | | - return new Alias(((Alias) e).child(), e.getName()); |
120 | | - } |
121 | | - return e; |
122 | | - }) |
123 | | - .collect(ImmutableList.toImmutableList()); |
124 | | - for (int i = 0; i < virtualColumns.size(); i++) { |
125 | | - context.exprIdReplaceMap.put(catalogRelation.getVirtualColumns().get(i).getExprId(), |
126 | | - virtualColumns.get(i).getExprId()); |
| 115 | + if (!catalogRelation.getVirtualColumns().isEmpty()) { |
| 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 | + } |
| 120 | + List<NamedExpression> virtualColumns = catalogRelation.getVirtualColumns().stream() |
| 121 | + .map(column -> (NamedExpression) ExpressionDeepCopier.INSTANCE.deepCopy(column, context)) |
| 122 | + .collect(ImmutableList.toImmutableList()); |
| 123 | + newRelation = newRelation.withVirtualColumns(virtualColumns); |
| 124 | + if (newRelation instanceof LogicalOlapScan) { |
| 125 | + // The score and ANN order keys sort by virtual columns. |
| 126 | + LogicalOlapScan olapScan = (LogicalOlapScan) newRelation; |
| 127 | + newRelation = olapScan.appendVirtualColumnsAndTopN(ImmutableList.of(), |
| 128 | + copyOrderKeys(olapScan.getAnnOrderKeys(), context), olapScan.getAnnLimit(), |
| 129 | + copyOrderKeys(olapScan.getScoreOrderKeys(), context), olapScan.getScoreLimit(), |
| 130 | + olapScan.getScoreRangeInfo()); |
| 131 | + } |
127 | 132 | } |
128 | | - virtualColumns = virtualColumns.stream() |
129 | | - .map(o -> (NamedExpression) ExpressionDeepCopier.INSTANCE.deepCopy(o, context)) |
130 | | - .collect(ImmutableList.toImmutableList()); |
131 | | - newRelation = newRelation.withVirtualColumns(virtualColumns); |
132 | 133 | if (catalogRelation instanceof LogicalSchemaScan |
133 | 134 | && ((LogicalSchemaScan) catalogRelation).isFilterPushed()) { |
134 | 135 | LogicalSchemaScan oldSchemaScan = (LogicalSchemaScan) catalogRelation; |
@@ -273,11 +274,15 @@ public Plan visitLogicalSort(LogicalSort<? extends Plan> sort, DeepCopierContext |
273 | 274 | @Override |
274 | 275 | public Plan visitLogicalTopN(LogicalTopN<? extends Plan> topN, DeepCopierContext context) { |
275 | 276 | Plan child = topN.child().accept(this, context); |
276 | | - List<OrderKey> orderKeys = topN.getOrderKeys().stream() |
| 277 | + List<OrderKey> orderKeys = copyOrderKeys(topN.getOrderKeys(), context); |
| 278 | + return new LogicalTopN<>(orderKeys, topN.getLimit(), topN.getOffset(), child); |
| 279 | + } |
| 280 | + |
| 281 | + private static List<OrderKey> copyOrderKeys(List<OrderKey> orderKeys, DeepCopierContext context) { |
| 282 | + return orderKeys.stream() |
277 | 283 | .map(o -> new OrderKey(ExpressionDeepCopier.INSTANCE.deepCopy(o.getExpr(), context), |
278 | 284 | o.isAsc(), o.isNullFirst())) |
279 | 285 | .collect(ImmutableList.toImmutableList()); |
280 | | - return new LogicalTopN<>(orderKeys, topN.getLimit(), topN.getOffset(), child); |
281 | 286 | } |
282 | 287 |
|
283 | 288 | @Override |
|
0 commit comments