morningman commented on code in PR #69194: URL: https://github.com/apache/doris/pull/69194#discussion_r4294350625
########## fe/fe-core/src/main/java/org/apache/doris/datasource/VariantWritePlanValidator.java: ########## @@ -0,0 +1,448 @@ +// 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.datasource; + +import org.apache.doris.catalog.Column; +import org.apache.doris.nereids.CTEContext; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.trees.expressions.CTEId; +import org.apache.doris.nereids.trees.expressions.Cast; +import org.apache.doris.nereids.trees.expressions.ExprId; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.ScalarSubquery; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.WhenClause; +import org.apache.doris.nereids.trees.expressions.functions.scalar.If; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.RecursiveCte; +import org.apache.doris.nereids.trees.plans.algebra.SetOperation; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalGenerate; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; +import org.apache.doris.nereids.types.ArrayType; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.MapType; +import org.apache.doris.nereids.types.StructField; +import org.apache.doris.nereids.types.StructType; +import org.apache.doris.nereids.types.VariantType; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** Shared source-plan validation for external Variant sinks. */ +public final class VariantWritePlanValidator { + private VariantWritePlanValidator() { + } + + /** + * Rejects an implicit Variant-to-non-Variant cast in the lineage of a Variant target column. + * + * <p>Common-type analysis for UNION, IF and CASE runs before sink binding. Without this + * check, an object or array Variant can become SQL NULL while being cast to a scalar, and the + * sink will only see that scalar/NULL and encode it back as Variant. Explicit casts remain an + * intentional user conversion and are not rejected. Only the expressions whose values can reach + * the target are inspected: the condition of an IF or of a CASE branch only chooses a value, so a + * Variant path compared with a scalar there loses nothing that is written.</p> + */ + public static void validateNoLossyCoercion(List<Column> targetColumns, Plan sourcePlan) { + validateNoLossyCoercion(targetColumns, sourcePlan, Optional.empty()); + } + + /** Validates a sink while its enclosing CTE producers still live in the analyzer context. */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, CTEContext cteContext) { + validateNoLossyCoercion(targetColumns, sourcePlan, Optional.of(cteContext)); + } + + private static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, Optional<CTEContext> cteContext) { + if (targetColumns.size() != sourcePlan.getOutput().size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + List<Integer> sourceOrdinals = new ArrayList<>(targetColumns.size()); + for (int i = 0; i < targetColumns.size(); i++) { + sourceOrdinals.add(i); + } + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, cteContext); + } + + /** + * Validates target columns that are embedded in a wider source output, such as row-level DML + * where the operation and row-id routing columns precede the table data columns. + */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals) { + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, Optional.empty()); + } + + /** Validates mapped source ordinals with analyzer-time CTE producer visibility. */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals, + CTEContext cteContext) { + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, Optional.of(cteContext)); + } + + private static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals, + Optional<CTEContext> cteContext) { + if (targetColumns.size() != sourceOrdinals.size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + + List<Integer> variantTargetOrdinals = new ArrayList<>(); + for (int i = 0; i < targetColumns.size(); i++) { + DataType targetType = DataType.fromCatalogType(targetColumns.get(i).getType()); + if (VariantType.containsVariant(targetType)) { + variantTargetOrdinals.add(i); + } + } + if (variantTargetOrdinals.isEmpty()) { + return; + } + + TraceContext traceContext = new TraceContext(sourcePlan, cteContext); + List<Slot> sourceOutputs = sourcePlan.getOutput(); + for (int targetOrdinal : variantTargetOrdinals) { + int sourceOrdinal = sourceOrdinals.get(targetOrdinal); + if (sourceOrdinal < 0 || sourceOrdinal >= sourceOutputs.size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + Column targetColumn = targetColumns.get(targetOrdinal); + traceOutputLineage( + sourcePlan, + Collections.singleton(sourceOutputs.get(sourceOrdinal).getExprId()), + targetColumn.getName(), + traceContext); + } + } + + /** + * Validates the requested outputs and returns lineage ExprIds supplied by an outer scope. + * Scalar subqueries use the returned IDs to continue tracing correlated columns in the + * enclosing plan. + */ + private static Set<ExprId> traceOutputLineage( + Plan plan, Set<ExprId> requiredExprIds, String targetColumn, TraceContext context) { + if (requiredExprIds.isEmpty()) { + return Collections.emptySet(); + } + if (plan instanceof SetOperation) { + return traceSetOperation(plan, (SetOperation) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof RecursiveCte) { + return traceRecursiveCte(plan, (RecursiveCte) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof LogicalGenerate) { + return traceGenerate((LogicalGenerate<?>) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof LogicalCTEConsumer) { + return traceCteConsumer((LogicalCTEConsumer) plan, requiredExprIds, targetColumn, context); + } + + Set<ExprId> unresolvedExprIds = new HashSet<>(requiredExprIds); + Set<ExprId> inputExprIds = new HashSet<>(); + for (Expression expression : plan.getExpressions()) { + if (!(expression instanceof NamedExpression)) { + continue; + } + NamedExpression namedExpression = (NamedExpression) expression; + if (!requiredExprIds.contains(namedExpression.getExprId())) { + continue; + } + inputExprIds.addAll(validateExpression(namedExpression, targetColumn, context)); + unresolvedExprIds.remove(namedExpression.getExprId()); + } + inputExprIds.addAll(unresolvedExprIds); + + Set<ExprId> externalExprIds = new HashSet<>(inputExprIds); + for (Plan child : plan.children()) { + Set<ExprId> childOutputExprIds = child.getOutputExprIdSet(); + Set<ExprId> childRequiredExprIds = new HashSet<>(inputExprIds); + childRequiredExprIds.retainAll(childOutputExprIds); + externalExprIds.removeAll(childOutputExprIds); + externalExprIds.addAll(traceOutputLineage(child, childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<ExprId> traceRecursiveCte( + Plan plan, RecursiveCte recursiveCte, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> externalExprIds = new HashSet<>(); + Set<Integer> requiredOrdinals = requiredOutputOrdinals(plan, requiredExprIds); + for (int childIndex = 0; childIndex < plan.arity(); childIndex++) { + List<SlotReference> childOutputs = recursiveCte.getRegularChildOutput(childIndex); + Set<ExprId> childRequiredExprIds = new HashSet<>(); + for (int ordinal : requiredOrdinals) { + if (ordinal < childOutputs.size()) { + childRequiredExprIds.add(childOutputs.get(ordinal).getExprId()); + } + } + externalExprIds.addAll(traceOutputLineage( + plan.child(childIndex), childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<ExprId> traceGenerate( + LogicalGenerate<?> generate, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> childRequiredExprIds = new HashSet<>(requiredExprIds); + List<Slot> generatorOutputs = generate.getGeneratorOutput(); + List<? extends Expression> generators = generate.getGenerators(); + for (int ordinal = 0; ordinal < generatorOutputs.size(); ordinal++) { + Slot generatorOutput = generatorOutputs.get(ordinal); + if (!requiredExprIds.contains(generatorOutput.getExprId())) { + continue; + } + childRequiredExprIds.remove(generatorOutput.getExprId()); + childRequiredExprIds.addAll(validateExpression(generators.get(ordinal), targetColumn, context)); + } + Set<ExprId> childOutputExprIds = generate.child().getOutputExprIdSet(); + Set<ExprId> externalExprIds = new HashSet<>(childRequiredExprIds); + externalExprIds.removeAll(childOutputExprIds); + childRequiredExprIds.retainAll(childOutputExprIds); + externalExprIds.addAll(traceOutputLineage( + generate.child(), childRequiredExprIds, targetColumn, context)); + return externalExprIds; + } + + private static Set<ExprId> traceSetOperation( + Plan plan, SetOperation setOperation, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> externalExprIds = new HashSet<>(); + Set<Integer> requiredOrdinals = requiredOutputOrdinals(plan, requiredExprIds); + + if (plan instanceof LogicalUnion) { + for (List<NamedExpression> constantRow : ((LogicalUnion) plan).getConstantExprsList()) { + for (int ordinal : requiredOrdinals) { + if (ordinal < constantRow.size()) { + externalExprIds.addAll(validateExpression(constantRow.get(ordinal), targetColumn, context)); + } + } + } + } + + for (int childIndex = 0; childIndex < setOperation.getArity(); childIndex++) { + List<SlotReference> childOutputs = setOperation.getRegularChildOutput(childIndex); + Set<ExprId> childRequiredExprIds = new HashSet<>(); + for (int ordinal : requiredOrdinals) { + if (ordinal < childOutputs.size()) { + childRequiredExprIds.add(childOutputs.get(ordinal).getExprId()); + } + } + externalExprIds.addAll(traceOutputLineage( + plan.child(childIndex), childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<Integer> requiredOutputOrdinals(Plan plan, Set<ExprId> requiredExprIds) { + Set<Integer> requiredOrdinals = new HashSet<>(); + List<Slot> outputs = plan.getOutput(); + for (int i = 0; i < outputs.size(); i++) { + if (requiredExprIds.contains(outputs.get(i).getExprId())) { + requiredOrdinals.add(i); + } + } + return requiredOrdinals; + } + + private static Set<ExprId> traceCteConsumer( + LogicalCTEConsumer consumer, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + LogicalCTEProducer<?> producer = context.cteProducers.get(consumer.getCteId()); + Optional<Plan> producerPlan = producer == null + ? context.findAnalyzerCteProducer(consumer) + : Optional.of(producer.child()); + if (!producerPlan.isPresent() || !context.activeCteIds.add(consumer.getCteId())) { + return Collections.emptySet(); + } + try { + Set<ExprId> producerExprIds = new HashSet<>(); + for (Map.Entry<Slot, Slot> mapping : consumer.getConsumerToProducerOutputMap().entrySet()) { + if (requiredExprIds.contains(mapping.getKey().getExprId())) { + producerExprIds.add(mapping.getValue().getExprId()); + } + } + return traceOutputLineage(producerPlan.get(), producerExprIds, targetColumn, context); + } finally { + context.activeCteIds.remove(consumer.getCteId()); + } + } + + /** + * Validates the casts on the values {@code expression} writes and returns the ExprIds those + * values come from: the slots they read, and the correlated slots of their scalar subqueries, + * which an outer scope may supply. + */ + private static Set<ExprId> validateExpression( + Expression expression, String targetColumn, TraceContext context) { + List<Expression> valueNodes = valueNodes(expression); + validateLossyCast(valueNodes, targetColumn); + Set<ExprId> inputExprIds = new HashSet<>(); + for (Expression node : valueNodes) { + if (node instanceof Slot) { + inputExprIds.add(((Slot) node).getExprId()); + } else if (node instanceof ScalarSubquery) { + inputExprIds.addAll(traceScalarSubquery((ScalarSubquery) node, targetColumn, context)); + } + } + return inputExprIds; + } + + /** Traces a scalar subquery and returns its correlated slots that the subquery itself does not supply. */ + private static Set<ExprId> traceScalarSubquery( + ScalarSubquery scalarSubquery, String targetColumn, TraceContext context) { + scalarSubquery.getTypeCoercionExpr() + .ifPresent(coercion -> validateLossyCast(valueNodes(coercion), targetColumn)); + Plan queryPlan = scalarSubquery.getQueryPlan(); + context.collectCteProducers(queryPlan); + Set<ExprId> unresolvedExprIds = new HashSet<>(traceOutputLineage( + queryPlan, + Collections.singleton(queryPlan.getOutput().get(0).getExprId()), + targetColumn, + context)); + Set<ExprId> correlatedExprIds = new HashSet<>(); + for (Slot correlateSlot : scalarSubquery.getCorrelateSlots()) { + correlatedExprIds.add(correlateSlot.getExprId()); + } + unresolvedExprIds.retainAll(correlatedExprIds); + return unresolvedExprIds; + } + + /** + * The nodes of {@code expression} whose values can reach its result, in pre-order: every node + * except those under the condition of an IF or of a CASE branch, which only chooses a value. + */ + private static List<Expression> valueNodes(Expression expression) { + List<Expression> nodes = new ArrayList<>(); + collectValueNodes(expression, nodes); + return nodes; + } + + private static void collectValueNodes(Expression expression, List<Expression> nodes) { + nodes.add(expression); + if (expression instanceof If) { + collectValueNodes(((If) expression).getTrueValue(), nodes); + collectValueNodes(((If) expression).getFalseValue(), nodes); + } else if (expression instanceof WhenClause) { + collectValueNodes(((WhenClause) expression).getResult(), nodes); + } else { Review Comment: The mechanism is right. A target column whose type contains a VARIANT leaf is traced as a whole, so for `s STRUCT<matched: BOOLEAN, payload: VARIANT>` the implicit cast that only feeds `matched` (`v['n'] > 1`) is rejected along with the lineage of `payload`. We are keeping that rule in this PR: - It rejects; it never writes a wrong value. The statement fails with an error naming `s`, and the comparison written with an explicit cast, `CAST(v['n'] AS INT) > 1`, is accepted, since explicit casts are taken as intended. - Tracing only the Variant leaves needs the target field path carried through `named_struct` / `struct` / `array` / `map` constructors, field and element extraction, and aliases across plan nodes. That is a design change of its own. The validator here is ported from branch-4.1 / 4.2, whose check searches the whole written expression, IF and CASE conditions included. - Doris's Iceberg DDL declares VARIANT only as a top-level column, so such an Iceberg target is a table created outside Doris, for example with Spark. We will track tracing by field path for complex targets as a follow-up. ########## fe/fe-core/src/main/java/org/apache/doris/datasource/VariantWritePlanValidator.java: ########## @@ -0,0 +1,448 @@ +// 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.datasource; + +import org.apache.doris.catalog.Column; +import org.apache.doris.nereids.CTEContext; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.trees.expressions.CTEId; +import org.apache.doris.nereids.trees.expressions.Cast; +import org.apache.doris.nereids.trees.expressions.ExprId; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.ScalarSubquery; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.WhenClause; +import org.apache.doris.nereids.trees.expressions.functions.scalar.If; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.RecursiveCte; +import org.apache.doris.nereids.trees.plans.algebra.SetOperation; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalGenerate; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; +import org.apache.doris.nereids.types.ArrayType; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.MapType; +import org.apache.doris.nereids.types.StructField; +import org.apache.doris.nereids.types.StructType; +import org.apache.doris.nereids.types.VariantType; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** Shared source-plan validation for external Variant sinks. */ +public final class VariantWritePlanValidator { + private VariantWritePlanValidator() { + } + + /** + * Rejects an implicit Variant-to-non-Variant cast in the lineage of a Variant target column. + * + * <p>Common-type analysis for UNION, IF and CASE runs before sink binding. Without this + * check, an object or array Variant can become SQL NULL while being cast to a scalar, and the + * sink will only see that scalar/NULL and encode it back as Variant. Explicit casts remain an + * intentional user conversion and are not rejected. Only the expressions whose values can reach + * the target are inspected: the condition of an IF or of a CASE branch only chooses a value, so a + * Variant path compared with a scalar there loses nothing that is written.</p> + */ + public static void validateNoLossyCoercion(List<Column> targetColumns, Plan sourcePlan) { + validateNoLossyCoercion(targetColumns, sourcePlan, Optional.empty()); + } + + /** Validates a sink while its enclosing CTE producers still live in the analyzer context. */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, CTEContext cteContext) { + validateNoLossyCoercion(targetColumns, sourcePlan, Optional.of(cteContext)); + } + + private static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, Optional<CTEContext> cteContext) { + if (targetColumns.size() != sourcePlan.getOutput().size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + List<Integer> sourceOrdinals = new ArrayList<>(targetColumns.size()); + for (int i = 0; i < targetColumns.size(); i++) { + sourceOrdinals.add(i); + } + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, cteContext); + } + + /** + * Validates target columns that are embedded in a wider source output, such as row-level DML + * where the operation and row-id routing columns precede the table data columns. + */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals) { + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, Optional.empty()); + } + + /** Validates mapped source ordinals with analyzer-time CTE producer visibility. */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals, + CTEContext cteContext) { + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, Optional.of(cteContext)); + } + + private static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals, + Optional<CTEContext> cteContext) { + if (targetColumns.size() != sourceOrdinals.size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + + List<Integer> variantTargetOrdinals = new ArrayList<>(); + for (int i = 0; i < targetColumns.size(); i++) { + DataType targetType = DataType.fromCatalogType(targetColumns.get(i).getType()); + if (VariantType.containsVariant(targetType)) { + variantTargetOrdinals.add(i); + } + } + if (variantTargetOrdinals.isEmpty()) { + return; + } + + TraceContext traceContext = new TraceContext(sourcePlan, cteContext); + List<Slot> sourceOutputs = sourcePlan.getOutput(); + for (int targetOrdinal : variantTargetOrdinals) { + int sourceOrdinal = sourceOrdinals.get(targetOrdinal); + if (sourceOrdinal < 0 || sourceOrdinal >= sourceOutputs.size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + Column targetColumn = targetColumns.get(targetOrdinal); + traceOutputLineage( + sourcePlan, + Collections.singleton(sourceOutputs.get(sourceOrdinal).getExprId()), + targetColumn.getName(), + traceContext); + } + } + + /** + * Validates the requested outputs and returns lineage ExprIds supplied by an outer scope. + * Scalar subqueries use the returned IDs to continue tracing correlated columns in the + * enclosing plan. + */ + private static Set<ExprId> traceOutputLineage( + Plan plan, Set<ExprId> requiredExprIds, String targetColumn, TraceContext context) { + if (requiredExprIds.isEmpty()) { + return Collections.emptySet(); + } + if (plan instanceof SetOperation) { + return traceSetOperation(plan, (SetOperation) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof RecursiveCte) { + return traceRecursiveCte(plan, (RecursiveCte) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof LogicalGenerate) { + return traceGenerate((LogicalGenerate<?>) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof LogicalCTEConsumer) { + return traceCteConsumer((LogicalCTEConsumer) plan, requiredExprIds, targetColumn, context); + } + + Set<ExprId> unresolvedExprIds = new HashSet<>(requiredExprIds); + Set<ExprId> inputExprIds = new HashSet<>(); + for (Expression expression : plan.getExpressions()) { + if (!(expression instanceof NamedExpression)) { + continue; + } + NamedExpression namedExpression = (NamedExpression) expression; + if (!requiredExprIds.contains(namedExpression.getExprId())) { + continue; + } + inputExprIds.addAll(validateExpression(namedExpression, targetColumn, context)); + unresolvedExprIds.remove(namedExpression.getExprId()); + } + inputExprIds.addAll(unresolvedExprIds); + + Set<ExprId> externalExprIds = new HashSet<>(inputExprIds); + for (Plan child : plan.children()) { + Set<ExprId> childOutputExprIds = child.getOutputExprIdSet(); + Set<ExprId> childRequiredExprIds = new HashSet<>(inputExprIds); + childRequiredExprIds.retainAll(childOutputExprIds); + externalExprIds.removeAll(childOutputExprIds); + externalExprIds.addAll(traceOutputLineage(child, childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<ExprId> traceRecursiveCte( + Plan plan, RecursiveCte recursiveCte, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> externalExprIds = new HashSet<>(); + Set<Integer> requiredOrdinals = requiredOutputOrdinals(plan, requiredExprIds); + for (int childIndex = 0; childIndex < plan.arity(); childIndex++) { + List<SlotReference> childOutputs = recursiveCte.getRegularChildOutput(childIndex); + Set<ExprId> childRequiredExprIds = new HashSet<>(); + for (int ordinal : requiredOrdinals) { + if (ordinal < childOutputs.size()) { + childRequiredExprIds.add(childOutputs.get(ordinal).getExprId()); + } + } + externalExprIds.addAll(traceOutputLineage( Review Comment: Fixed in `4a6b39396e5`. Confirmed in the code: when the recursive term writes `payload` from the work table's `a`, a lossy implicit cast in the anchor's `a` reaches `payload` from the second row on. Each term was traced only for `payload`, and the work-table column the recursive term returned was dropped as unresolved, so the cast in the anchor's `a` was never checked. `traceRecursiveCte` now maps each work-table column to the CTE column it holds (the work table's output is the anchor's output, in order) and traces the newly reached columns in both terms until no new one is reached. Each column is traced once, and work-table columns are no longer returned to the caller. Tests: `VariantWritePlanValidatorTest.testRecursiveCteLossCarriedThroughAnotherColumnIsRejected` builds the recursive CTE the way `AnalyzeCTE` shapes it, with `a = IF(TRUE, CAST(v AS INT), 1)` in the anchor and `payload = a` in the recursive term, and expects the rejection; with the recursive term writing `payload` from `payload` it is accepted. Tracing only the requested column of each term turns it red. ########## fe/fe-core/src/main/java/org/apache/doris/datasource/VariantWritePlanValidator.java: ########## @@ -0,0 +1,448 @@ +// 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.datasource; + +import org.apache.doris.catalog.Column; +import org.apache.doris.nereids.CTEContext; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.trees.expressions.CTEId; +import org.apache.doris.nereids.trees.expressions.Cast; +import org.apache.doris.nereids.trees.expressions.ExprId; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.ScalarSubquery; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.WhenClause; +import org.apache.doris.nereids.trees.expressions.functions.scalar.If; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.RecursiveCte; +import org.apache.doris.nereids.trees.plans.algebra.SetOperation; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalGenerate; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; +import org.apache.doris.nereids.types.ArrayType; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.MapType; +import org.apache.doris.nereids.types.StructField; +import org.apache.doris.nereids.types.StructType; +import org.apache.doris.nereids.types.VariantType; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** Shared source-plan validation for external Variant sinks. */ +public final class VariantWritePlanValidator { + private VariantWritePlanValidator() { + } + + /** + * Rejects an implicit Variant-to-non-Variant cast in the lineage of a Variant target column. + * + * <p>Common-type analysis for UNION, IF and CASE runs before sink binding. Without this + * check, an object or array Variant can become SQL NULL while being cast to a scalar, and the + * sink will only see that scalar/NULL and encode it back as Variant. Explicit casts remain an + * intentional user conversion and are not rejected. Only the expressions whose values can reach + * the target are inspected: the condition of an IF or of a CASE branch only chooses a value, so a + * Variant path compared with a scalar there loses nothing that is written.</p> + */ + public static void validateNoLossyCoercion(List<Column> targetColumns, Plan sourcePlan) { + validateNoLossyCoercion(targetColumns, sourcePlan, Optional.empty()); + } + + /** Validates a sink while its enclosing CTE producers still live in the analyzer context. */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, CTEContext cteContext) { + validateNoLossyCoercion(targetColumns, sourcePlan, Optional.of(cteContext)); + } + + private static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, Optional<CTEContext> cteContext) { + if (targetColumns.size() != sourcePlan.getOutput().size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + List<Integer> sourceOrdinals = new ArrayList<>(targetColumns.size()); + for (int i = 0; i < targetColumns.size(); i++) { + sourceOrdinals.add(i); + } + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, cteContext); + } + + /** + * Validates target columns that are embedded in a wider source output, such as row-level DML + * where the operation and row-id routing columns precede the table data columns. + */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals) { + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, Optional.empty()); + } + + /** Validates mapped source ordinals with analyzer-time CTE producer visibility. */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals, + CTEContext cteContext) { + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, Optional.of(cteContext)); + } + + private static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals, + Optional<CTEContext> cteContext) { + if (targetColumns.size() != sourceOrdinals.size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + + List<Integer> variantTargetOrdinals = new ArrayList<>(); + for (int i = 0; i < targetColumns.size(); i++) { + DataType targetType = DataType.fromCatalogType(targetColumns.get(i).getType()); + if (VariantType.containsVariant(targetType)) { + variantTargetOrdinals.add(i); + } + } + if (variantTargetOrdinals.isEmpty()) { + return; + } + + TraceContext traceContext = new TraceContext(sourcePlan, cteContext); + List<Slot> sourceOutputs = sourcePlan.getOutput(); + for (int targetOrdinal : variantTargetOrdinals) { + int sourceOrdinal = sourceOrdinals.get(targetOrdinal); + if (sourceOrdinal < 0 || sourceOrdinal >= sourceOutputs.size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + Column targetColumn = targetColumns.get(targetOrdinal); + traceOutputLineage( + sourcePlan, + Collections.singleton(sourceOutputs.get(sourceOrdinal).getExprId()), + targetColumn.getName(), + traceContext); + } + } + + /** + * Validates the requested outputs and returns lineage ExprIds supplied by an outer scope. + * Scalar subqueries use the returned IDs to continue tracing correlated columns in the + * enclosing plan. + */ + private static Set<ExprId> traceOutputLineage( + Plan plan, Set<ExprId> requiredExprIds, String targetColumn, TraceContext context) { + if (requiredExprIds.isEmpty()) { + return Collections.emptySet(); + } + if (plan instanceof SetOperation) { + return traceSetOperation(plan, (SetOperation) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof RecursiveCte) { + return traceRecursiveCte(plan, (RecursiveCte) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof LogicalGenerate) { + return traceGenerate((LogicalGenerate<?>) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof LogicalCTEConsumer) { + return traceCteConsumer((LogicalCTEConsumer) plan, requiredExprIds, targetColumn, context); + } + + Set<ExprId> unresolvedExprIds = new HashSet<>(requiredExprIds); + Set<ExprId> inputExprIds = new HashSet<>(); + for (Expression expression : plan.getExpressions()) { + if (!(expression instanceof NamedExpression)) { + continue; + } + NamedExpression namedExpression = (NamedExpression) expression; + if (!requiredExprIds.contains(namedExpression.getExprId())) { + continue; + } + inputExprIds.addAll(validateExpression(namedExpression, targetColumn, context)); + unresolvedExprIds.remove(namedExpression.getExprId()); + } + inputExprIds.addAll(unresolvedExprIds); + + Set<ExprId> externalExprIds = new HashSet<>(inputExprIds); + for (Plan child : plan.children()) { + Set<ExprId> childOutputExprIds = child.getOutputExprIdSet(); + Set<ExprId> childRequiredExprIds = new HashSet<>(inputExprIds); + childRequiredExprIds.retainAll(childOutputExprIds); + externalExprIds.removeAll(childOutputExprIds); + externalExprIds.addAll(traceOutputLineage(child, childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<ExprId> traceRecursiveCte( + Plan plan, RecursiveCte recursiveCte, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> externalExprIds = new HashSet<>(); + Set<Integer> requiredOrdinals = requiredOutputOrdinals(plan, requiredExprIds); + for (int childIndex = 0; childIndex < plan.arity(); childIndex++) { + List<SlotReference> childOutputs = recursiveCte.getRegularChildOutput(childIndex); + Set<ExprId> childRequiredExprIds = new HashSet<>(); + for (int ordinal : requiredOrdinals) { + if (ordinal < childOutputs.size()) { + childRequiredExprIds.add(childOutputs.get(ordinal).getExprId()); + } + } + externalExprIds.addAll(traceOutputLineage( + plan.child(childIndex), childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<ExprId> traceGenerate( + LogicalGenerate<?> generate, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> childRequiredExprIds = new HashSet<>(requiredExprIds); + List<Slot> generatorOutputs = generate.getGeneratorOutput(); + List<? extends Expression> generators = generate.getGenerators(); + for (int ordinal = 0; ordinal < generatorOutputs.size(); ordinal++) { + Slot generatorOutput = generatorOutputs.get(ordinal); + if (!requiredExprIds.contains(generatorOutput.getExprId())) { + continue; + } + childRequiredExprIds.remove(generatorOutput.getExprId()); + childRequiredExprIds.addAll(validateExpression(generators.get(ordinal), targetColumn, context)); + } + Set<ExprId> childOutputExprIds = generate.child().getOutputExprIdSet(); + Set<ExprId> externalExprIds = new HashSet<>(childRequiredExprIds); + externalExprIds.removeAll(childOutputExprIds); + childRequiredExprIds.retainAll(childOutputExprIds); + externalExprIds.addAll(traceOutputLineage( + generate.child(), childRequiredExprIds, targetColumn, context)); + return externalExprIds; + } + + private static Set<ExprId> traceSetOperation( + Plan plan, SetOperation setOperation, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> externalExprIds = new HashSet<>(); + Set<Integer> requiredOrdinals = requiredOutputOrdinals(plan, requiredExprIds); + + if (plan instanceof LogicalUnion) { + for (List<NamedExpression> constantRow : ((LogicalUnion) plan).getConstantExprsList()) { + for (int ordinal : requiredOrdinals) { + if (ordinal < constantRow.size()) { + externalExprIds.addAll(validateExpression(constantRow.get(ordinal), targetColumn, context)); + } + } + } + } + + for (int childIndex = 0; childIndex < setOperation.getArity(); childIndex++) { + List<SlotReference> childOutputs = setOperation.getRegularChildOutput(childIndex); + Set<ExprId> childRequiredExprIds = new HashSet<>(); + for (int ordinal : requiredOrdinals) { + if (ordinal < childOutputs.size()) { + childRequiredExprIds.add(childOutputs.get(ordinal).getExprId()); + } + } + externalExprIds.addAll(traceOutputLineage( + plan.child(childIndex), childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<Integer> requiredOutputOrdinals(Plan plan, Set<ExprId> requiredExprIds) { + Set<Integer> requiredOrdinals = new HashSet<>(); + List<Slot> outputs = plan.getOutput(); + for (int i = 0; i < outputs.size(); i++) { + if (requiredExprIds.contains(outputs.get(i).getExprId())) { + requiredOrdinals.add(i); + } + } + return requiredOrdinals; + } + + private static Set<ExprId> traceCteConsumer( + LogicalCTEConsumer consumer, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + LogicalCTEProducer<?> producer = context.cteProducers.get(consumer.getCteId()); + Optional<Plan> producerPlan = producer == null + ? context.findAnalyzerCteProducer(consumer) + : Optional.of(producer.child()); + if (!producerPlan.isPresent() || !context.activeCteIds.add(consumer.getCteId())) { + return Collections.emptySet(); + } + try { + Set<ExprId> producerExprIds = new HashSet<>(); + for (Map.Entry<Slot, Slot> mapping : consumer.getConsumerToProducerOutputMap().entrySet()) { + if (requiredExprIds.contains(mapping.getKey().getExprId())) { + producerExprIds.add(mapping.getValue().getExprId()); + } + } + return traceOutputLineage(producerPlan.get(), producerExprIds, targetColumn, context); + } finally { Review Comment: Fixed in `4a6b39396e5`. Agreed: `activeCteIds` only guards against cycles, so every consumer of a shared producer walked it again, and a chain of CTEs that each read the previous one twice walked the first producer 2^N times. `TraceContext` now keeps each completed producer trace, keyed by the CTE and the requested producer outputs, together with the ExprIds it returned, and a consumer asking for the same outputs reuses it. The cycle guard is unchanged, and a producer with a lossy cast still fails the statement at its first consumer. Tests: `testSharedCteProducerIsTracedOncePerRequestedOutputs` validates a 30-level chain, `cN AS (SELECT payload FROM cN-1 UNION ALL SELECT payload FROM cN-1)`, within a 30-second timeout. Without the cache it times out. ########## fe/fe-core/src/main/java/org/apache/doris/datasource/VariantWritePlanValidator.java: ########## @@ -0,0 +1,448 @@ +// 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.datasource; + +import org.apache.doris.catalog.Column; +import org.apache.doris.nereids.CTEContext; +import org.apache.doris.nereids.exceptions.AnalysisException; +import org.apache.doris.nereids.trees.expressions.CTEId; +import org.apache.doris.nereids.trees.expressions.Cast; +import org.apache.doris.nereids.trees.expressions.ExprId; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.NamedExpression; +import org.apache.doris.nereids.trees.expressions.ScalarSubquery; +import org.apache.doris.nereids.trees.expressions.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.expressions.WhenClause; +import org.apache.doris.nereids.trees.expressions.functions.scalar.If; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.RecursiveCte; +import org.apache.doris.nereids.trees.plans.algebra.SetOperation; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEConsumer; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEProducer; +import org.apache.doris.nereids.trees.plans.logical.LogicalGenerate; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; +import org.apache.doris.nereids.types.ArrayType; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.MapType; +import org.apache.doris.nereids.types.StructField; +import org.apache.doris.nereids.types.StructType; +import org.apache.doris.nereids.types.VariantType; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +/** Shared source-plan validation for external Variant sinks. */ +public final class VariantWritePlanValidator { + private VariantWritePlanValidator() { + } + + /** + * Rejects an implicit Variant-to-non-Variant cast in the lineage of a Variant target column. + * + * <p>Common-type analysis for UNION, IF and CASE runs before sink binding. Without this + * check, an object or array Variant can become SQL NULL while being cast to a scalar, and the + * sink will only see that scalar/NULL and encode it back as Variant. Explicit casts remain an + * intentional user conversion and are not rejected. Only the expressions whose values can reach + * the target are inspected: the condition of an IF or of a CASE branch only chooses a value, so a + * Variant path compared with a scalar there loses nothing that is written.</p> + */ + public static void validateNoLossyCoercion(List<Column> targetColumns, Plan sourcePlan) { + validateNoLossyCoercion(targetColumns, sourcePlan, Optional.empty()); + } + + /** Validates a sink while its enclosing CTE producers still live in the analyzer context. */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, CTEContext cteContext) { + validateNoLossyCoercion(targetColumns, sourcePlan, Optional.of(cteContext)); + } + + private static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, Optional<CTEContext> cteContext) { + if (targetColumns.size() != sourcePlan.getOutput().size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + List<Integer> sourceOrdinals = new ArrayList<>(targetColumns.size()); + for (int i = 0; i < targetColumns.size(); i++) { + sourceOrdinals.add(i); + } + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, cteContext); + } + + /** + * Validates target columns that are embedded in a wider source output, such as row-level DML + * where the operation and row-id routing columns precede the table data columns. + */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals) { + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, Optional.empty()); + } + + /** Validates mapped source ordinals with analyzer-time CTE producer visibility. */ + public static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals, + CTEContext cteContext) { + validateNoLossyCoercion(targetColumns, sourcePlan, sourceOrdinals, Optional.of(cteContext)); + } + + private static void validateNoLossyCoercion( + List<Column> targetColumns, Plan sourcePlan, List<Integer> sourceOrdinals, + Optional<CTEContext> cteContext) { + if (targetColumns.size() != sourceOrdinals.size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + + List<Integer> variantTargetOrdinals = new ArrayList<>(); + for (int i = 0; i < targetColumns.size(); i++) { + DataType targetType = DataType.fromCatalogType(targetColumns.get(i).getType()); + if (VariantType.containsVariant(targetType)) { + variantTargetOrdinals.add(i); + } + } + if (variantTargetOrdinals.isEmpty()) { + return; + } + + TraceContext traceContext = new TraceContext(sourcePlan, cteContext); + List<Slot> sourceOutputs = sourcePlan.getOutput(); + for (int targetOrdinal : variantTargetOrdinals) { + int sourceOrdinal = sourceOrdinals.get(targetOrdinal); + if (sourceOrdinal < 0 || sourceOrdinal >= sourceOutputs.size()) { + throw new AnalysisException("VARIANT write target and source columns are not aligned"); + } + Column targetColumn = targetColumns.get(targetOrdinal); + traceOutputLineage( + sourcePlan, + Collections.singleton(sourceOutputs.get(sourceOrdinal).getExprId()), + targetColumn.getName(), + traceContext); + } + } + + /** + * Validates the requested outputs and returns lineage ExprIds supplied by an outer scope. + * Scalar subqueries use the returned IDs to continue tracing correlated columns in the + * enclosing plan. + */ + private static Set<ExprId> traceOutputLineage( + Plan plan, Set<ExprId> requiredExprIds, String targetColumn, TraceContext context) { + if (requiredExprIds.isEmpty()) { + return Collections.emptySet(); + } + if (plan instanceof SetOperation) { + return traceSetOperation(plan, (SetOperation) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof RecursiveCte) { + return traceRecursiveCte(plan, (RecursiveCte) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof LogicalGenerate) { + return traceGenerate((LogicalGenerate<?>) plan, requiredExprIds, targetColumn, context); + } + if (plan instanceof LogicalCTEConsumer) { + return traceCteConsumer((LogicalCTEConsumer) plan, requiredExprIds, targetColumn, context); + } + + Set<ExprId> unresolvedExprIds = new HashSet<>(requiredExprIds); + Set<ExprId> inputExprIds = new HashSet<>(); + for (Expression expression : plan.getExpressions()) { + if (!(expression instanceof NamedExpression)) { + continue; + } + NamedExpression namedExpression = (NamedExpression) expression; + if (!requiredExprIds.contains(namedExpression.getExprId())) { + continue; + } + inputExprIds.addAll(validateExpression(namedExpression, targetColumn, context)); + unresolvedExprIds.remove(namedExpression.getExprId()); + } + inputExprIds.addAll(unresolvedExprIds); + + Set<ExprId> externalExprIds = new HashSet<>(inputExprIds); + for (Plan child : plan.children()) { + Set<ExprId> childOutputExprIds = child.getOutputExprIdSet(); + Set<ExprId> childRequiredExprIds = new HashSet<>(inputExprIds); + childRequiredExprIds.retainAll(childOutputExprIds); + externalExprIds.removeAll(childOutputExprIds); + externalExprIds.addAll(traceOutputLineage(child, childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<ExprId> traceRecursiveCte( + Plan plan, RecursiveCte recursiveCte, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> externalExprIds = new HashSet<>(); + Set<Integer> requiredOrdinals = requiredOutputOrdinals(plan, requiredExprIds); + for (int childIndex = 0; childIndex < plan.arity(); childIndex++) { + List<SlotReference> childOutputs = recursiveCte.getRegularChildOutput(childIndex); + Set<ExprId> childRequiredExprIds = new HashSet<>(); + for (int ordinal : requiredOrdinals) { + if (ordinal < childOutputs.size()) { + childRequiredExprIds.add(childOutputs.get(ordinal).getExprId()); + } + } + externalExprIds.addAll(traceOutputLineage( + plan.child(childIndex), childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<ExprId> traceGenerate( + LogicalGenerate<?> generate, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> childRequiredExprIds = new HashSet<>(requiredExprIds); + List<Slot> generatorOutputs = generate.getGeneratorOutput(); + List<? extends Expression> generators = generate.getGenerators(); + for (int ordinal = 0; ordinal < generatorOutputs.size(); ordinal++) { + Slot generatorOutput = generatorOutputs.get(ordinal); + if (!requiredExprIds.contains(generatorOutput.getExprId())) { + continue; + } + childRequiredExprIds.remove(generatorOutput.getExprId()); + childRequiredExprIds.addAll(validateExpression(generators.get(ordinal), targetColumn, context)); + } + Set<ExprId> childOutputExprIds = generate.child().getOutputExprIdSet(); + Set<ExprId> externalExprIds = new HashSet<>(childRequiredExprIds); + externalExprIds.removeAll(childOutputExprIds); + childRequiredExprIds.retainAll(childOutputExprIds); + externalExprIds.addAll(traceOutputLineage( + generate.child(), childRequiredExprIds, targetColumn, context)); + return externalExprIds; + } + + private static Set<ExprId> traceSetOperation( + Plan plan, SetOperation setOperation, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + Set<ExprId> externalExprIds = new HashSet<>(); + Set<Integer> requiredOrdinals = requiredOutputOrdinals(plan, requiredExprIds); + + if (plan instanceof LogicalUnion) { + for (List<NamedExpression> constantRow : ((LogicalUnion) plan).getConstantExprsList()) { + for (int ordinal : requiredOrdinals) { + if (ordinal < constantRow.size()) { + externalExprIds.addAll(validateExpression(constantRow.get(ordinal), targetColumn, context)); + } + } + } + } + + for (int childIndex = 0; childIndex < setOperation.getArity(); childIndex++) { + List<SlotReference> childOutputs = setOperation.getRegularChildOutput(childIndex); + Set<ExprId> childRequiredExprIds = new HashSet<>(); + for (int ordinal : requiredOrdinals) { + if (ordinal < childOutputs.size()) { + childRequiredExprIds.add(childOutputs.get(ordinal).getExprId()); + } + } + externalExprIds.addAll(traceOutputLineage( + plan.child(childIndex), childRequiredExprIds, targetColumn, context)); + } + return externalExprIds; + } + + private static Set<Integer> requiredOutputOrdinals(Plan plan, Set<ExprId> requiredExprIds) { + Set<Integer> requiredOrdinals = new HashSet<>(); + List<Slot> outputs = plan.getOutput(); + for (int i = 0; i < outputs.size(); i++) { + if (requiredExprIds.contains(outputs.get(i).getExprId())) { + requiredOrdinals.add(i); + } + } + return requiredOrdinals; + } + + private static Set<ExprId> traceCteConsumer( + LogicalCTEConsumer consumer, Set<ExprId> requiredExprIds, + String targetColumn, TraceContext context) { + LogicalCTEProducer<?> producer = context.cteProducers.get(consumer.getCteId()); + Optional<Plan> producerPlan = producer == null Review Comment: Fixed in `4a6b39396e5`. Confirmed: a leading WITH binds its sink before the outer anchors are built, so the producer of `outer_source` comes from the analyzer context, and the `inner_source` producer nested in it was never collected. Its consumer resolved to nothing, and the lossy `IF` was accepted. The producers in a plan taken from the analyzer context are now collected before it is traced, as the scalar subquery path already does for its query plan. Tests: `testWithNestedInAnalyzerContextProducerIsTraced` analyzes `WITH inner_source AS (SELECT IF(TRUE, parse_to_variant('{"lost":true}'), 1) AS payload) SELECT payload FROM inner_source`, hands it to the validator as the analyzer-context producer of `outer_source`, and expects the rejection. Tracing the fetched producer without collecting its nested producers turns it red. ########## be/src/format/table/iceberg/arrow_schema_util.cpp: ########## @@ -108,6 +110,13 @@ Status IcebergArrowSchemaConvertor::convert_to_arrow_field( arrow_type = arrow::binary(); break; + case iceberg::TypeID::VARIANT: + arrow_type = arrow::extension::variant(arrow::struct_({ Review Comment: Confirmed. `PARSE_TO_VARIANT` encodes a block with one encoder, so its rows share one dictionary, and `write_column_to_iceberg_arrow` passes `compact_metadata=false`, so every row appends the whole dictionary. With many distinct keys in one batch, the metadata column passes the 2 GiB limit of Arrow's `BinaryBuilder` and the INSERT fails; it fails rather than writing anything wrong. That serde came with #68883, and Paimon's `write_binary_variant_arrow` appends the same shared dictionary for every row on master today. The fix therefore belongs in the shared serde, for both table writers, rather than in this PR's schema mapping. We will fix it in a separate PR for Iceberg and Paimon: compact the dictionary of a row that does not use all of its keys, as the `compact_metadata=true` branch already does for Flight, with a BE test that bounds the metadata bytes of a high-cardinality batch. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
