englefly commented on PR #69111:
URL: https://github.com/apache/doris/pull/69111#issuecomment-6210468926

   Implemented the framework variant (`45a495f18b6`), as agreed: the consumer 
maps are refreshed instead of keeping the producer outputs stable.
   
   `RewriteCteChildren.visitLogicalCTEAnchor` now compares the producer outputs 
before and after the rewrite of the producer body and refreshes both output 
maps of every consumer of that CTE:
   
   ```text
   producer outputs [k#0, v#1, cnt#3] -> [k#10, v#1, cnt#3]
   consumer maps       c2p={k#4=k#0, ...}, p2c={k#0=[k#4], ...}   =>   
c2p={k#4=k#10, ...}, p2c={k#10=[k#4], ...}
   ```
   
   The correspondence is derived, not published by the rule, so it also covers 
renames done by any other rule of the producer body:
   
   * a column which keeps its exprId needs no mapping;
   * a renamed column is the one whose exprId disappeared from the outputs, it 
is matched by name (a rule which renames an output keeps its name) and the 
producer slot of the maps is replaced by the new one;
   * a pruned column (e.g. `ColumnPruning`, see 
`CteLimitPushdownPlanTest#testPushLimitBeforeProducerOutputPruning`) is not 
needed by any consumer and gets no mapping.
   
   The refreshed consumers are written back to `RewrittenCteConsumer` and the 
`CteIdToConsumers` collection is refreshed as well, so a later reuse of the 
same CTE does not resurrect the stale maps. This runs inside the CTE subtree 
rewrite, i.e. before `AdjustNullable` and before the CBO builds the CTE 
consumer groups from these maps.
   
   With that, the guard is gone: `ConstantPropagation` names the constant of a 
producer output like anywhere else and the consumers follow it. 
`ConstantPropagationFreshExprIdTest#testMaterializedCteConsumerMapsStayValid` 
asserts both directions now - the producer really outputs different exprIds 
than in the analyzed plan, and every producer slot a consumer maps to is an 
output of the producer - plus a `checkPlannerResult` on the query.
   
   Verification: 33 FE unit test classes (constant propagation, constant cte, 
cte inline, cte limit pushdown, rewrite cte children limit pushdown, search cte 
rewrite, data trait, sql plan suite, column pruning, ...) rerun: 403 tests, 0 
failures; checkstyle passes.
   
   Note for the earlier discussion: this replaces the "keep the producer output 
exprId" approach of `8af10b7bee3` and `fe4ae780bbf`; the CTE part of those 
commits is superseded, the sink part (P2) stays.


-- 
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]

Reply via email to