Gabriel39 commented on code in PR #69032: URL: https://github.com/apache/doris/pull/69032#discussion_r4749287017
########## fe/fe-core/src/main/java/org/apache/doris/datasource/connector/converter/ConnectorSpatialWriteNormalizer.java: ########## @@ -0,0 +1,787 @@ +// 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.connector.converter; + +import org.apache.doris.catalog.Column; +import org.apache.doris.nereids.CTEContext; +import org.apache.doris.nereids.analyzer.UnboundFunction; +import org.apache.doris.nereids.properties.OrderKey; +import org.apache.doris.nereids.rules.analysis.ExpressionAnalyzer; +import org.apache.doris.nereids.rules.rewrite.PushProjectThroughUnion; +import org.apache.doris.nereids.trees.expressions.Alias; +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.expressions.StatementScopeIdGenerator; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Array; +import org.apache.doris.nereids.trees.expressions.functions.scalar.Cardinality; +import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateMap; +import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateNamedStruct; +import org.apache.doris.nereids.trees.expressions.functions.scalar.CreateStruct; +import org.apache.doris.nereids.trees.expressions.functions.scalar.MapContainsKey; +import org.apache.doris.nereids.trees.expressions.functions.scalar.MapKeys; +import org.apache.doris.nereids.trees.expressions.functions.scalar.MapSize; +import org.apache.doris.nereids.trees.expressions.literal.ArrayLiteral; +import org.apache.doris.nereids.trees.expressions.literal.Literal; +import org.apache.doris.nereids.trees.expressions.literal.MapLiteral; +import org.apache.doris.nereids.trees.expressions.literal.NullLiteral; +import org.apache.doris.nereids.trees.expressions.literal.StringLikeLiteral; +import org.apache.doris.nereids.trees.expressions.literal.StructLiteral; +import org.apache.doris.nereids.trees.plans.Plan; +import org.apache.doris.nereids.trees.plans.algebra.SetOperation.Qualifier; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTE; +import org.apache.doris.nereids.trees.plans.logical.LogicalCTEAnchor; +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.LogicalFilter; +import org.apache.doris.nereids.trees.plans.logical.LogicalLimit; +import org.apache.doris.nereids.trees.plans.logical.LogicalOneRowRelation; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.nereids.trees.plans.logical.LogicalProject; +import org.apache.doris.nereids.trees.plans.logical.LogicalSetOperation; +import org.apache.doris.nereids.trees.plans.logical.LogicalSort; +import org.apache.doris.nereids.trees.plans.logical.LogicalSubQueryAlias; +import org.apache.doris.nereids.trees.plans.logical.LogicalUnion; +import org.apache.doris.nereids.trees.plans.logical.ProjectProcessor; +import org.apache.doris.nereids.types.ArrayType; +import org.apache.doris.nereids.types.DataType; +import org.apache.doris.nereids.types.GeographyType; +import org.apache.doris.nereids.types.GeometryType; +import org.apache.doris.nereids.types.MapType; +import org.apache.doris.nereids.types.StructType; +import org.apache.doris.nereids.util.ExpressionUtils; +import org.apache.doris.nereids.util.TypeCoercionUtils; + +import com.google.common.collect.ImmutableList; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.IntStream; + +/** Restores proven NULL producer values before spatial sink coercion. */ +public final class ConnectorSpatialWriteNormalizer { + private ConnectorSpatialWriteNormalizer() { + } + + public static LogicalPlan normalize(List<Column> targets, LogicalPlan source, CTEContext cteContext) { + return normalize(targets, source, IntStream.range(0, targets.size()).boxed() + .collect(ImmutableList.toImmutableList()), cteContext); + } + + /** Aligns data targets with source positions when a row-level sink also carries routing columns. */ + public static LogicalPlan normalize(List<Column> targets, LogicalPlan source, + List<Integer> sourceOrdinals, CTEContext cteContext) { + if (targets.stream().noneMatch(column -> column.getType().typeContainsSpatialType())) { + return source; + } + if (source instanceof LogicalUnion && ((LogicalUnion) source).getQualifier() == Qualifier.ALL) { + return normalizeUnionAll(targets, (LogicalUnion) source, sourceOrdinals, cteContext); + } + if (source instanceof LogicalSubQueryAlias) { + LogicalPlan child = (LogicalPlan) source.child(0); + LogicalPlan normalized = normalize(targets, child, sourceOrdinals, cteContext); + return normalized == child ? source : (LogicalPlan) source.withChildren(ImmutableList.of(normalized)); + } + if (source instanceof LogicalProject && isForwardingProject((LogicalProject<?>) source)) { + Plan input = source.child(0); + while (input instanceof LogicalSubQueryAlias) { + input = input.child(0); + } + Plan boundary = input; + while (boundary instanceof LogicalSubQueryAlias + || (boundary instanceof LogicalProject && isForwardingProject((LogicalProject<?>) boundary))) { + boundary = boundary.child(0); + } + if (boundary instanceof LogicalUnion && ((LogicalUnion) boundary).getQualifier() == Qualifier.ALL) { + // Reuse the existing safe forwarding/cast projection rule, including its ExprId handling. + Optional<Plan> pushed = ProjectProcessor.tryProcessProject( + ((LogicalProject<?>) source).getProjects(), input); + // Forwarding aliases can also repeat or prune outputs without moving calculations. + if (!pushed.isPresent() && input instanceof LogicalUnion + && ((LogicalProject<?>) source).getProjects().stream() + .allMatch(project -> project instanceof Slot + || (project instanceof Alias && project.child(0) instanceof Slot))) { + pushed = Optional.of(PushProjectThroughUnion.doPushProject( + ((LogicalProject<?>) source).getProjects(), (LogicalUnion) input)); + } + if (pushed.isPresent()) { + return normalize(targets, (LogicalPlan) pushed.get(), sourceOrdinals, cteContext); + } + } + } + Resolver resolver = new Resolver(source, cteContext); + List<NamedExpression> outputs = new ArrayList<>(source.getOutput()); + List<LogicalPlan> wrappers = new ArrayList<>(); + Set<ExprId> sortInputs = new HashSet<>(); + LogicalPlan producerInput = source; + while (producerInput instanceof LogicalSort || producerInput instanceof LogicalLimit) { + wrappers.add(producerInput); + for (Expression expression : producerInput.getExpressions()) { + collectSortInputs(expression, sortInputs); + } + producerInput = (LogicalPlan) producerInput.child(0); + } + LogicalProject<?> producer = producerInput instanceof LogicalProject + && !((LogicalProject<?>) producerInput).isDistinct() ? (LogicalProject<?>) producerInput : null; + List<NamedExpression> producerOutputs = null; + Map<ExprId, Slot> replacedSlots = new HashMap<>(); + boolean changed = false; + for (int i = 0; i < targets.size(); i++) { + Column target = targets.get(i); + if (!target.getType().typeContainsSpatialType()) { + continue; + } + int sourceOrdinal = sourceOrdinals.get(i); + Slot slot = source.getOutput().get(sourceOrdinal); + Expression value = resolver.resolve(slot, source); + DataType targetType = DataType.fromCatalogType(target.getType()); + boolean literal = isLiteralConstruction(value) || canRetargetSpatialNulls(value, targetType); + // SELECT/UPDATE reach the bound sink with CAST(ARRAY(NULL) AS ARRAY<INT>). + // Reuse MERGE's NULL-only proof before validation, without lifting arbitrary producers. + if (literal) { + value = normalizeSpatialNullsBeforeBinding(value, targetType); + } + if (!literal && producer != null && !sortInputs.contains(slot.getExprId()) + && sourceOrdinals.indexOf(sourceOrdinal) == sourceOrdinals.lastIndexOf(sourceOrdinal)) { + // Retag a constructor where it is computed, so its dynamic ordinary siblings + // still use the same inputs and are evaluated only once. Do not follow slots + // through joins, other producers, or a DISTINCT projection. + NamedExpression project = producer.getProjects().get(sourceOrdinal); + if (project instanceof Alias && canRetargetSpatialNulls(project.child(0), targetType, true)) { Review Comment: Fix in next PR -- 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]
