Repository navigation
[fix](fe) Preserve column identity for forwarded USING keys #69388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...re/src/test/java/org/apache/doris/nereids/trees/plans/logical/LogicalCTEConsumerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // 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.trees.plans.logical; | ||
|
|
||
| import org.apache.doris.nereids.rules.rewrite.AdjustNullable; | ||
| import org.apache.doris.nereids.rules.rewrite.EliminateGroupByKey; | ||
| import org.apache.doris.nereids.trees.expressions.CTEId; | ||
| import org.apache.doris.nereids.trees.expressions.ExprId; | ||
| import org.apache.doris.nereids.trees.expressions.Slot; | ||
| import org.apache.doris.nereids.trees.expressions.SlotReference; | ||
| import org.apache.doris.nereids.trees.plans.RelationId; | ||
| import org.apache.doris.nereids.types.IntegerType; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| class LogicalCTEConsumerTest { | ||
| private LogicalCTEConsumer newConsumer() { | ||
| Slot key = new SlotReference("x", IntegerType.INSTANCE, false); | ||
| Slot payload = new SlotReference("y", IntegerType.INSTANCE, false); | ||
| LogicalEmptyRelation producer = new LogicalEmptyRelation(new RelationId(0), | ||
| ImmutableList.of(key, payload, key.withName("z"))); | ||
| return new LogicalCTEConsumer(new RelationId(1), new CTEId(0), "q", producer); | ||
| } | ||
|
|
||
| private void assertColumnOrder(LogicalCTEConsumer consumer) { | ||
| List<Slot> output = consumer.getOutput(); | ||
| Assertions.assertEquals(List.of("x", "y", "z"), output.stream() | ||
| .map(Slot::getName).collect(Collectors.toList())); | ||
| Assertions.assertEquals(consumer.getProducerSlot(output.get(0)).getExprId(), | ||
| consumer.getProducerSlot(output.get(2)).getExprId()); | ||
| Assertions.assertNotEquals(consumer.getProducerSlot(output.get(0)).getExprId(), | ||
| consumer.getProducerSlot(output.get(1)).getExprId()); | ||
| } | ||
|
|
||
| @Test | ||
| void testRepeatedProducerSlotKeepsColumnOrder() { | ||
| LogicalCTEConsumer consumer = newConsumer(); | ||
| assertColumnOrder(consumer); | ||
| assertColumnOrder((LogicalCTEConsumer) consumer.withGroupExpression(Optional.empty())); | ||
| assertColumnOrder((LogicalCTEConsumer) consumer.withGroupExprLogicalPropChildren( | ||
| Optional.empty(), Optional.empty(), ImmutableList.of())); | ||
| } | ||
|
|
||
| @Test | ||
| void testPruneOutputsKeepsColumnOrder() { | ||
| LogicalCTEConsumer consumer = newConsumer(); | ||
| List<Slot> output = ImmutableList.copyOf(consumer.getConsumerToProducerOutputMap().keySet()); | ||
| LogicalCTEConsumer pruned = (LogicalCTEConsumer) consumer.pruneOutputs( | ||
| ImmutableList.of(output.get(1), output.get(2))); | ||
| Assertions.assertEquals(List.of("y", "z"), pruned.getOutput().stream() | ||
| .map(Slot::getName).collect(Collectors.toList())); | ||
| } | ||
|
|
||
| @Test | ||
| void testAdjustNullableKeepsColumnOrder() { | ||
| LogicalCTEConsumer consumer = newConsumer(); | ||
| Slot producer = consumer.getConsumerToProducerOutputMap().values().iterator().next(); | ||
| Map<ExprId, Slot> replaceMap = new HashMap<>(); | ||
| replaceMap.put(producer.getExprId(), producer.withNullable(true)); | ||
| LogicalCTEConsumer updated = (LogicalCTEConsumer) new AdjustNullable(false) | ||
| .visitLogicalCTEConsumer(consumer, replaceMap); | ||
| assertColumnOrder(updated); | ||
| Assertions.assertTrue(updated.getOutput().get(0).nullable()); | ||
| Assertions.assertTrue(updated.getOutput().get(2).nullable()); | ||
| Assertions.assertFalse(updated.getOutput().get(1).nullable()); | ||
| } | ||
|
|
||
| @Test | ||
| void testReplaceProducerIdKeepsColumnOrder() { | ||
| LogicalCTEConsumer consumer = newConsumer(); | ||
| Slot producer = consumer.getConsumerToProducerOutputMap().values().iterator().next(); | ||
| ExprId newId = new ExprId(999); | ||
| Map<ExprId, ExprId> replaceMap = new HashMap<>(); | ||
| replaceMap.put(producer.getExprId(), newId); | ||
| LogicalCTEConsumer updated = (LogicalCTEConsumer) new EliminateGroupByKey() | ||
| .visitLogicalCTEConsumer(consumer, replaceMap); | ||
| assertColumnOrder(updated); | ||
| Assertions.assertEquals(newId, updated.getProducerSlot(updated.getOutput().get(0)).getExprId()); | ||
| Assertions.assertEquals(newId, updated.getProducerSlot(updated.getOutput().get(2)).getExprId()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Preserve CTE column order when sharing the USING-key ExprId
WITH q(x,y,z) AS (SELECT a,t1.b,t1.a FROM t1 JOIN t2 USING(a)) SELECT * FROM qnow exposes its columns in the wrong order. The producer becomes:AnalyzeCTEwraps this with column names[x#L,y#B,z#L], retaining the ExprIds.LogicalCTEConsumerthen keysproducerToConsumerOutputMapby producer Slot, whose equality compares only ExprId, andcomputeOutput()returns that multimap's groupedvalues():[x,z,y]. Star expansion binds this order before CTE inlining, which does not repair it. With a matchingt1(a,b)=(1,10)row, the result is(1,1,10)instead of(1,10,1); positional consumers such as UNION or INSERT SELECT can consequently receive wrong values. Previously the merged Alias had a separate ExprId, so this query preserved its column order. The right-preserved branch has the same issue.Preserve producer column positions in the CTE consumer alongside this identity change, and add a result-checking regression with merged and qualified keys separated by a non-key column.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed locally: the reported SQL fails on the original PR head with expected column order [x,y,z] but actual [x,z,y]. Fixed in 363d34b.
LogicalCTEConsumer now takes its output from the insertion-ordered consumer-to-producer map, whose distinct consumer keys preserve each producer position. Nullability adjustment, producer ExprId replacement and variant subpath pruning also retain that order when rebuilding the map. The USING-key identity fix remains intact.
Added inner/left/right USING CTE order checks with full planning, an actual FE-computed repeated-column result check for (1,10,1), and consumer copy/pruning/nullability/ExprId replacement tests. All 73 focused FE tests passed through run-fe-ut.sh; the full FE build and Checkstyle also passed. The result check uses the FE result-computation path; no BE execution is claimed.