Gabriel39 commented on code in PR #71224:
URL: https://github.com/apache/doris/pull/71224#discussion_r4769341263
##########
fe/fe-core/src/main/java/org/apache/doris/datasource/connector/converter/ConnectorSpatialWriteNormalizer.java:
##########
@@ -225,7 +248,190 @@ public static LogicalPlan normalize(List<Column> targets,
LogicalPlan source,
}
}
}
- return changed ? new LogicalProject<>(outputs, normalizedSource) :
normalizedSource;
+ LogicalPlan result = changed ? new LogicalProject<>(outputs,
normalizedSource) : normalizedSource;
+ return normalizeForwardedNulls(targets, result, sourceOrdinals,
cteContext);
+ }
+
+ /** Keeps bound outer definitions reachable when an anchored CTE shadows
their name. */
+ private static final class AnchoredCteContext extends CTEContext {
+ private final CTEContext outer;
+
+ private AnchoredCteContext(CTEId id, LogicalSubQueryAlias<Plan> alias,
CTEContext outer) {
+ super(id, alias, outer);
+ this.outer = outer;
+ }
+ }
+
+ private static Optional<CTEContext> findCteContext(CTEContext context,
String name, CTEId id) {
+ while (true) {
+ Optional<CTEContext> binding =
context.findCTEContext(name).filter(cte -> cte.getCteId().equals(id));
+ if (binding.isPresent() || !(context instanceof
AnchoredCteContext)) {
+ return binding;
+ }
+ context = ((AnchoredCteContext) context).outer;
+ }
Review Comment:
Fixed in 6cba9b36edc. CTEContext now retains its parent scope and provides
CTEId-based lookup with a lazy ancestor-binding index. The normalizer and
AnalyzeCTE both use that lookup; AnchoredCteContext and the separate analysis
binding map are removed. Name-based visibility remains unchanged, while
already-bound consumers retain access to their original definitions.
The reported INSERT fails locally in modes 1/2 before the fix and plans
successfully afterward in modes 0/1/2. Unit tests cover binding updates,
nearest scopes, recursion and unknown ids. The full Iceberg regression verifies
both WITH placements, shared UNION ALL and duplicate targets in all three
modes: 36 new rows/27 groups. All 143 related FE tests, ordinary CTE
comparison, full spatial generation/comparison and Checkstyle pass locally.
##########
fe/fe-core/src/main/java/org/apache/doris/datasource/connector/converter/ConnectorSpatialWriteNormalizer.java:
##########
@@ -225,7 +248,190 @@ public static LogicalPlan normalize(List<Column> targets,
LogicalPlan source,
}
}
}
- return changed ? new LogicalProject<>(outputs, normalizedSource) :
normalizedSource;
+ LogicalPlan result = changed ? new LogicalProject<>(outputs,
normalizedSource) : normalizedSource;
+ return normalizeForwardedNulls(targets, result, sourceOrdinals,
cteContext);
+ }
+
+ /** Keeps bound outer definitions reachable when an anchored CTE shadows
their name. */
+ private static final class AnchoredCteContext extends CTEContext {
+ private final CTEContext outer;
+
+ private AnchoredCteContext(CTEId id, LogicalSubQueryAlias<Plan> alias,
CTEContext outer) {
+ super(id, alias, outer);
+ this.outer = outer;
+ }
+ }
+
+ private static Optional<CTEContext> findCteContext(CTEContext context,
String name, CTEId id) {
+ while (true) {
+ Optional<CTEContext> binding =
context.findCTEContext(name).filter(cte -> cte.getCteId().equals(id));
+ if (binding.isPresent() || !(context instanceof
AnchoredCteContext)) {
+ return binding;
+ }
+ context = ((AnchoredCteContext) context).outer;
+ }
+ }
+
+ private static LogicalPlan normalizeForwardedNulls(List<Column> targets,
LogicalPlan source,
+ List<Integer> sourceOrdinals, CTEContext context) {
+ ForwardedNulls forwarder = null;
+ LogicalPlan result = source;
+ List<NamedExpression> projected = new ArrayList<>(source.getOutput());
+ for (int i = 0; i < targets.size(); i++) {
+ if (!targets.get(i).getType().typeContainsSpatialType()) {
+ continue;
+ }
+ DataType target =
DataType.fromCatalogType(targets.get(i).getType());
+ int ordinal = sourceOrdinals.get(i);
+ Slot slot = projected.get(ordinal).toSlot();
+ if
(slot.getDataType().equals(spatialLiteralType(slot.getDataType(), target))) {
+ continue;
+ }
+ if (forwarder == null) {
+ forwarder = new ForwardedNulls(new Resolver(source, context));
+ }
+ if (!canRetargetSpatialNulls(forwarder.resolver.resolve(slot,
result), target, true)) {
+ continue;
+ }
+ Optional<Pair<LogicalPlan, Slot>> appended =
forwarder.append(result, slot, target);
+ if (appended.isPresent()) {
+ projected.set(ordinal, appended.get().second);
+ result = appended.get().first;
+ }
+ }
+ // Restore the sink's width once, instead of adding another forwarding
layer per target.
+ return result == source ? source : new LogicalProject<>(projected,
result);
+ }
+
+ /** Adds a typed output without changing the old output used by other
projections/CTE consumers. */
+ private static final class ForwardedNulls {
+ private final Resolver resolver;
+ private final Set<CTEId> activeCtes = new HashSet<>();
+
+ private ForwardedNulls(Resolver resolver) {
+ this.resolver = resolver;
+ }
+
+ private Optional<Pair<LogicalPlan, Slot>> append(LogicalPlan source,
Slot slot, DataType target) {
+ if (source instanceof LogicalProject && !((LogicalProject<?>)
source).isDistinct()) {
+ LogicalProject<?> project = (LogicalProject<?>) source;
+ for (int i = 0; i < project.getProjects().size(); i++) {
+ NamedExpression output = project.getProjects().get(i);
+ if (output.getExprId().equals(slot.getExprId())) {
+ return appendProject(project, i, output instanceof
Alias ? output.child(0) : output, target);
+ }
+ }
+ } else if (source instanceof LogicalSubQueryAlias || source
instanceof LogicalSort
+ || source instanceof LogicalLimit || source instanceof
LogicalFilter) {
+ return append((LogicalPlan) source.child(0), slot,
target).map(result -> {
+ LogicalPlan wrapper = (LogicalPlan)
source.withChildren(ImmutableList.of(result.first));
+ Slot output = wrapper.getOutput().stream()
+ .filter(value ->
value.getExprId().equals(result.second.getExprId())).findFirst().get();
+ return Pair.of(wrapper, output);
+ });
+ } else if (source instanceof LogicalCTEConsumer) {
+ return appendConsumer((LogicalCTEConsumer) source, slot,
target);
+ } else if (source instanceof LogicalCTEAnchor) {
+ LogicalCTEAnchor<?, ?> anchor = (LogicalCTEAnchor<?, ?>)
source;
+ return append((LogicalPlan) anchor.right(), slot,
target).map(result -> {
+ LogicalCTEProducer<?> producer = (LogicalCTEProducer<?>)
anchor.left();
+ Plan updated =
producer.withChildren(ImmutableList.of(resolver.producers.get(anchor.getCteId())));
+ return Pair.of((LogicalPlan) anchor.withChildren(updated,
result.first), result.second);
Review Comment:
Fixed in 6cba9b36edc. Both anchored rewrite paths now use the same helper: a
changed producer is installed in StatementContext as well as the emitted
anchor, and an unchanged definition retains the original producer object.
Tests check registry identity on both direct and wrapped anchors, and verify
that passing through an unrelated inner anchor still normalizes the target type
while preserving the unrelated producer reference. The baseline fails these
checks; the final 143-test FE run and full Iceberg regression pass.
##########
fe/fe-core/src/main/java/org/apache/doris/datasource/connector/converter/ConnectorSpatialWriteNormalizer.java:
##########
@@ -225,7 +248,190 @@ public static LogicalPlan normalize(List<Column> targets,
LogicalPlan source,
}
}
}
- return changed ? new LogicalProject<>(outputs, normalizedSource) :
normalizedSource;
+ LogicalPlan result = changed ? new LogicalProject<>(outputs,
normalizedSource) : normalizedSource;
+ return normalizeForwardedNulls(targets, result, sourceOrdinals,
cteContext);
+ }
+
+ /** Keeps bound outer definitions reachable when an anchored CTE shadows
their name. */
+ private static final class AnchoredCteContext extends CTEContext {
+ private final CTEContext outer;
+
+ private AnchoredCteContext(CTEId id, LogicalSubQueryAlias<Plan> alias,
CTEContext outer) {
+ super(id, alias, outer);
+ this.outer = outer;
+ }
+ }
+
+ private static Optional<CTEContext> findCteContext(CTEContext context,
String name, CTEId id) {
+ while (true) {
+ Optional<CTEContext> binding =
context.findCTEContext(name).filter(cte -> cte.getCteId().equals(id));
+ if (binding.isPresent() || !(context instanceof
AnchoredCteContext)) {
+ return binding;
+ }
+ context = ((AnchoredCteContext) context).outer;
+ }
+ }
+
+ private static LogicalPlan normalizeForwardedNulls(List<Column> targets,
LogicalPlan source,
+ List<Integer> sourceOrdinals, CTEContext context) {
+ ForwardedNulls forwarder = null;
+ LogicalPlan result = source;
+ List<NamedExpression> projected = new ArrayList<>(source.getOutput());
+ for (int i = 0; i < targets.size(); i++) {
+ if (!targets.get(i).getType().typeContainsSpatialType()) {
+ continue;
+ }
+ DataType target =
DataType.fromCatalogType(targets.get(i).getType());
+ int ordinal = sourceOrdinals.get(i);
+ Slot slot = projected.get(ordinal).toSlot();
+ if
(slot.getDataType().equals(spatialLiteralType(slot.getDataType(), target))) {
+ continue;
+ }
+ if (forwarder == null) {
+ forwarder = new ForwardedNulls(new Resolver(source, context));
+ }
+ if (!canRetargetSpatialNulls(forwarder.resolver.resolve(slot,
result), target, true)) {
+ continue;
+ }
+ Optional<Pair<LogicalPlan, Slot>> appended =
forwarder.append(result, slot, target);
+ if (appended.isPresent()) {
+ projected.set(ordinal, appended.get().second);
+ result = appended.get().first;
Review Comment:
Fixed in 6cba9b36edc. Typed outputs are cached by the original producer slot
ExprId and full target DataType. One normalization state shares the CTE-output
cache across UNION ALL branches; forwarding projections also reuse their
appended outputs. Different consumers retain independent ExprIds, and different
spatial target types remain separate.
The shared UNION producer now has 2 outputs instead of 3. The repeated
geometry/geography target test has 3 outputs instead of 5, preserving the
ordinary output. Native duplicate-target writes pass in modes 0/1/2, and the
full regression preserves NULL shapes, ordinary fields and shared volatile
evaluation counts. No performance benchmark is claimed.
--
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]