924060929 commented on code in PR #71224:
URL: https://github.com/apache/doris/pull/71224#discussion_r4766663623
##########
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:
[P3] When multiple `UNION ALL` branches share the same CTE producer, or
multiple sink target columns of the same spatial type reference the same
forwarded slot (e.g., `INSERT INTO t(g1, g2) SELECT s, s FROM q`), each call
appends a duplicate typed spatial column to the underlying producer.
Caching appended outputs by `(producerSlotExprId, targetDataType)` (and
sharing that cache across `UNION ALL` branches) would let consumers reuse the
already-appended spatial column instead of duplicating the container
construction.
##########
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:
[P2] Two minor consistency issues on the anchored CTE rewrite path (here and
lines 129-130):
1. `AnalyzeCTE` updates `StatementContext.setCteProducer(...)` whenever a
producer is rewritten, whereas the anchored paths here only update the plan
tree and leave `StatementContext` pointing to the old `LogicalCTEProducer`
until later rewrite stages refresh it.
2. Here `producer.withChildren(...)` is called unconditionally even when
`resolver.producers.get(anchor.getCteId())` is identical to `producer.child()`
(e.g., when passing through an unrelated inner anchor). Skipping the rebuild
when unchanged avoids unnecessary node reconstruction.
##########
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:
[P2] `findCteContext` only walks `AnchoredCteContext.outer`, so it misses
plain `CTEContext` shadowing.
Under `lower_case_table_names = 1` or `2`, consider:
```sql
WITH c AS (
SELECT id, named_struct('k', id, 'shape', CAST(NULL AS INT)) AS s FROM
base
),
C AS (
SELECT id, s FROM c
)
INSERT INTO spatial_target(id, g)
SELECT id, s FROM C;
```
Here `AnalyzeCTE` passes a plain `CTEContext` (not `AnchoredCteContext`)
into `BindSink`. Because `CTEContext` indexes by folded name with
`buildKeepingLast()`, `C` overwrites `c` in `cteContextMap`. When
`Resolver.traceSlot` / `appendConsumer` looks up `c` by `(name="c",
cteId=c_id)`, `findCTEContext("c")` returns `C` (mismatching `cteId`), and
`context instanceof AnchoredCteContext` is `false`, so lookup returns empty and
the `INSERT` is rejected.
Instead of subclassing `CTEContext` with `AnchoredCteContext`, consider
adding a `CTEId`-based lookup (e.g., `findCTEContext(CTEId)` backed by a
`Map<CTEId, CTEContext>` or parent pointer) directly on `CTEContext`. That
handles both anchored and outer `WITH` scopes uniformly and removes
`AnchoredCteContext`.
--
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]