hubgeter commented on code in PR #66620: URL: https://github.com/apache/doris/pull/66620#discussion_r3764571185
########## fe/fe-core/src/main/java/org/apache/doris/datasource/VariantWritePlanValidator.java: ########## @@ -0,0 +1,273 @@ +// 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.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.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.plans.Plan; +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.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.</p> + */ + public static void validateNoLossyCoercion( + String sinkName, List<Column> targetColumns, Plan sourcePlan) { + if (targetColumns.size() != sourcePlan.getOutput().size()) { + throw new AnalysisException( + sinkName + " Variant write target and source columns are not aligned"); + } + + List<Integer> variantOrdinals = new ArrayList<>(); + for (int i = 0; i < targetColumns.size(); i++) { + DataType targetType = DataType.fromCatalogType(targetColumns.get(i).getType()); + if (VariantType.containsVariant(targetType)) { + variantOrdinals.add(i); + } + } + if (variantOrdinals.isEmpty()) { + return; + } + + TraceContext traceContext = new TraceContext(sourcePlan); + for (int ordinal : variantOrdinals) { + Column targetColumn = targetColumns.get(ordinal); + traceOutputLineage( + sourcePlan, + Collections.singleton(sourcePlan.getOutput().get(ordinal).getExprId()), + sinkName, + targetColumn.getName(), + traceContext); + } + } + + private static void traceOutputLineage( + Plan plan, Set<ExprId> requiredExprIds, String sinkName, String targetColumn, + TraceContext context) { + if (requiredExprIds.isEmpty()) { + return; + } + if (plan instanceof SetOperation) { Review Comment: Fixed in f89f87f4a9f: Variant sink lineage validation now traces RecursiveCte outputs (including analyzer-time CTE context), with rejection coverage for both Iceberg and Paimon sinks. ########## fe/fe-core/src/main/java/org/apache/doris/datasource/VariantWritePlanValidator.java: ########## @@ -0,0 +1,273 @@ +// 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.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.Slot; +import org.apache.doris.nereids.trees.expressions.SlotReference; +import org.apache.doris.nereids.trees.plans.Plan; +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.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.</p> + */ + public static void validateNoLossyCoercion( + String sinkName, List<Column> targetColumns, Plan sourcePlan) { + if (targetColumns.size() != sourcePlan.getOutput().size()) { + throw new AnalysisException( + sinkName + " Variant write target and source columns are not aligned"); + } + + List<Integer> variantOrdinals = new ArrayList<>(); + for (int i = 0; i < targetColumns.size(); i++) { + DataType targetType = DataType.fromCatalogType(targetColumns.get(i).getType()); + if (VariantType.containsVariant(targetType)) { + variantOrdinals.add(i); + } + } + if (variantOrdinals.isEmpty()) { + return; + } + + TraceContext traceContext = new TraceContext(sourcePlan); + for (int ordinal : variantOrdinals) { + Column targetColumn = targetColumns.get(ordinal); + traceOutputLineage( + sourcePlan, + Collections.singleton(sourcePlan.getOutput().get(ordinal).getExprId()), + sinkName, + targetColumn.getName(), + traceContext); + } + } + + private static void traceOutputLineage( + Plan plan, Set<ExprId> requiredExprIds, String sinkName, String targetColumn, + TraceContext context) { + if (requiredExprIds.isEmpty()) { + return; + } + if (plan instanceof SetOperation) { + traceSetOperation( + plan, (SetOperation) plan, requiredExprIds, sinkName, targetColumn, context); + return; + } + if (plan instanceof LogicalCTEConsumer) { + traceCteConsumer( + (LogicalCTEConsumer) plan, requiredExprIds, sinkName, targetColumn, context); + return; + } + + Set<ExprId> unresolvedExprIds = new HashSet<>(requiredExprIds); + Set<ExprId> inputExprIds = new HashSet<>(); + for (Expression expression : plan.getExpressions()) { Review Comment: Fixed in f89f87f4a9f: Variant sink lineage validation now maps LogicalGenerate output slots back to their generator expressions, with rejection coverage for both Iceberg and Paimon sinks. -- 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]
