Skip to content

Commit 724071d

Browse files
morningmanclaude
andcommitted
[fix](fe) Keep DEFAULT(column) in a derived table of INSERT ... SELECT on its own FROM clause
### What problem does this PR solve? Issue Number: None Related PR: #69488 Problem Summary: For a target with write-only defaults (Iceberg), `InsertUtils.normalizePlan` resolves `DEFAULT(column)` in the select lists of an `INSERT ... SELECT` by name against the target's pinned writer schema, before the source relations are bound. It walked the whole query plan with `rewriteUp`, so it also rewrote the projections of a derived table in `FROM`, which is a query of its own. With a source `src(c INT DEFAULT 7)` and a target `t(c INT)` whose write-default is 9, INSERT INTO t(c) SELECT v FROM (SELECT DEFAULT(c) AS v FROM src) q wrote 9 instead of 7, and `DEFAULT(src.c)` inside the derived table failed with "column in assignment list is invalid", because its qualifier was checked against the target. branch-4.2's `resolveIcebergSelectDefaultReferences` walks the plan the same way. The walk now stops at `LogicalSubQueryAlias`. `DEFAULT(column)` inside a derived table is left to binding and names a column of that table's own `FROM` clause, as it does on master. The select lists of the INSERT's own query, set operation branches included, are resolved as before, still bottom-up. ### Release note None ### Check List (For Author) - Test: Unit Test - New `InsertUtilsTest#writeOnlyDefaultInDerivedTableNamesItsOwnSource` covers an unqualified and a qualified `DEFAULT(score)` inside a derived table (left to binding while the outer `DEFAULT(name)` resolves to the pinned default), and a UNION whose first branch resolves and whose second branch reads a derived table. It fails with the previous `rewriteUp` walk. - `InsertUtilsTest`, `ConnectorWriteSchemaUtilsTest`, `RewriteDefaultExpressionTest`, `IvmDeltaRewriterTest` and `IvmLinearDeltaHandlerTest` pass, as does the fe-core checkstyle. - Behavior changed: Yes. Within this PR, `DEFAULT(column)` in a derived table of an `INSERT ... SELECT` into an Iceberg table no longer resolves against the target; it keeps master's meaning. - Does this need documentation: No Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent fc049a2 commit 724071d

2 files changed

Lines changed: 64 additions & 17 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtils.java‎

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import org.apache.doris.nereids.trees.plans.logical.LogicalInlineTable;
6969
import org.apache.doris.nereids.trees.plans.logical.LogicalPlan;
7070
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
71+
import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias;
7172
import org.apache.doris.nereids.trees.plans.logical.UnboundLogicalSink;
7273
import org.apache.doris.nereids.types.AggStateType;
7374
import org.apache.doris.nereids.types.DataType;
@@ -526,26 +527,31 @@ private static NamedExpression resolveDefaultReferences(NamedExpression value,
526527
/**
527528
* Resolve DEFAULT(column) in the select lists of an INSERT ... SELECT by name against the target's pinned
528529
* writer schema, before the source relations are bound, so it names a target column whatever the source is.
530+
* A derived table in FROM is a query of its own: DEFAULT(column) in it names a column of its own FROM clause,
531+
* as without this resolution, so it is left alone.
529532
*/
530533
private static Plan resolveSelectDefaultReferences(Plan query, Optional<List<Column>> writeOnlyDefaultColumns,
531534
TableIf table, List<String> targetNameParts) {
532-
return query.rewriteUp(plan -> {
533-
if (plan instanceof LogicalProject) {
534-
LogicalProject<?> project = (LogicalProject<?>) plan;
535-
return project.withProjects(project.getProjects().stream()
536-
.map(projection -> resolveDefaultReferences(
537-
projection, writeOnlyDefaultColumns, table, targetNameParts))
538-
.collect(ImmutableList.toImmutableList()));
539-
}
540-
if (plan instanceof UnboundOneRowRelation) {
541-
UnboundOneRowRelation relation = (UnboundOneRowRelation) plan;
542-
return new UnboundOneRowRelation(relation.getRelationId(), relation.getProjects().stream()
543-
.map(projection -> resolveDefaultReferences(
544-
projection, writeOnlyDefaultColumns, table, targetNameParts))
545-
.collect(ImmutableList.toImmutableList()));
546-
}
547-
return plan;
548-
});
535+
if (query instanceof LogicalSubQueryAlias) {
536+
return query;
537+
}
538+
Plan plan = query.withChildren(child -> resolveSelectDefaultReferences(
539+
child, writeOnlyDefaultColumns, table, targetNameParts));
540+
if (plan instanceof LogicalProject) {
541+
LogicalProject<?> project = (LogicalProject<?>) plan;
542+
return project.withProjects(project.getProjects().stream()
543+
.map(projection -> resolveDefaultReferences(
544+
projection, writeOnlyDefaultColumns, table, targetNameParts))
545+
.collect(ImmutableList.toImmutableList()));
546+
}
547+
if (plan instanceof UnboundOneRowRelation) {
548+
UnboundOneRowRelation relation = (UnboundOneRowRelation) plan;
549+
return new UnboundOneRowRelation(relation.getRelationId(), relation.getProjects().stream()
550+
.map(projection -> resolveDefaultReferences(
551+
projection, writeOnlyDefaultColumns, table, targetNameParts))
552+
.collect(ImmutableList.toImmutableList()));
553+
}
554+
return plan;
549555
}
550556

551557
static void pinConnectorWriteSchema(StatementContext statementContext, TableIf targetTableIf,

‎fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/insert/InsertUtilsTest.java‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import org.apache.doris.nereids.trees.plans.RelationId;
4444
import org.apache.doris.nereids.trees.plans.logical.LogicalInlineTable;
4545
import org.apache.doris.nereids.trees.plans.logical.LogicalProject;
46+
import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias;
47+
import org.apache.doris.nereids.trees.plans.logical.LogicalUnion;
4648
import org.apache.doris.nereids.trees.plans.logical.UnboundLogicalSink;
4749
import org.apache.doris.nereids.types.IntegerType;
4850
import org.apache.doris.nereids.util.MemoTestUtils;
@@ -508,6 +510,45 @@ public void writeOnlyDefaultInSelectResolvesByTargetColumnName() {
508510
}
509511
}
510512

513+
@Test
514+
public void writeOnlyDefaultInDerivedTableNamesItsOwnSource() {
515+
// INSERT INTO t(score, name) SELECT v, DEFAULT(name) FROM (SELECT DEFAULT(score) AS v FROM src) q: the
516+
// derived table is a query of its own, so its DEFAULT(score) names src.score, whose default can differ from
517+
// the target's 9, and is left to binding, qualified or not. DEFAULT in the INSERT's own select lists, set
518+
// operation branches included, still names the target column.
519+
ConnectContext ctx = MemoTestUtils.createConnectContext();
520+
try {
521+
PluginDrivenExternalTable table = pinWriterSchema(ctx, true);
522+
Expression writeDefault = PARSER.parseExpression("'write-name'");
523+
for (String derived : ImmutableList.of("(select DEFAULT(score) as v from src) q",
524+
"(select DEFAULT(s.score) as v from src s) q")) {
525+
Plan normalized = InsertUtils.normalizePlan(connectorSink(ImmutableList.of("score", "name"),
526+
PARSER.parseSingle("select v, DEFAULT(name) from " + derived).child(0)),
527+
table, Optional.empty(), Optional.empty());
528+
LogicalProject<?> outer = (LogicalProject<?>) normalized.child(0);
529+
Assertions.assertEquals(writeDefault, outer.getProjects().get(1).child(0), derived);
530+
assertDerivedDefaultLeftToBinding(outer.child(0), derived);
531+
}
532+
533+
Plan union = InsertUtils.normalizePlan(connectorSink(ImmutableList.of("name"),
534+
PARSER.parseSingle("select DEFAULT(name) union all "
535+
+ "select v from (select DEFAULT(name) as v from src) q").child(0)),
536+
table, Optional.empty(), Optional.empty());
537+
LogicalUnion setOperation = (LogicalUnion) union.child(0);
538+
UnboundOneRowRelation oneRow = (UnboundOneRowRelation) setOperation.child(0);
539+
Assertions.assertEquals(writeDefault, oneRow.getProjects().get(0).child(0));
540+
assertDerivedDefaultLeftToBinding(setOperation.child(1).child(0), "union branch");
541+
} finally {
542+
ConnectContext.remove();
543+
}
544+
}
545+
546+
private static void assertDerivedDefaultLeftToBinding(Plan derivedTable, String message) {
547+
Assertions.assertInstanceOf(LogicalSubQueryAlias.class, derivedTable, message);
548+
LogicalProject<?> inner = (LogicalProject<?>) derivedTable.child(0);
549+
Assertions.assertInstanceOf(Default.class, inner.getProjects().get(0).child(0), message);
550+
}
551+
511552
@Test
512553
public void defaultReferenceIsLeftToBindingWithoutWriteOnlyDefaults() {
513554
// A connector whose read schema carries its defaults (hive) keeps the existing DEFAULT(column) handling.

0 commit comments

Comments
 (0)