[fix](nereids) Skip lambda variables when collecting filters above CTE consumers #71318
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 618 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.385
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #71318"
general
weight: 0.35
morrySnow
Background
When a CTE is referenced more than once, Nereids keeps it materialized: one
LogicalCTEProducercomputes it, and each reference reads it through aLogicalCTEConsumerwhose output slots map to the producer's. To shrink the shared result,CollectFilterAboveConsumercollects the filters sitting directly on each consumer, rewritten onto the producer's slots, andRewriteCteChildrenpushes them into the producer when every consumer has a matching one. A lambda such asx -> trim(x)binds its parameterxas anArrayItemSlot, which is markedSlotNotFromChildrenbecause it comes from the lambda, not from a child plan.What problem does this PR solve?
Issue Number: None
Related PR: #70627 (same fix in
ProjectJoinReorderHelper), #19934 (introduced the slot mapping)PR Type: Bugfix
Problem Summary:
A query fails during planning with
Required producerslot for :x#N doesn't existwhen a filter containing a lambda ends up directly on a consumer of a materialized CTE. This PR leaves lambda parameters as they are when the filter is rewritten onto the producer's slots, so the query plans and runs.In this query
sharedis referenced twice, andf0.c > 0is pushed throughf0's projection onto its consumer:| Before the fix | With the fix |
|---|---|
|
ERROR 1105 (HY000): errCode = 2, detailMessage = Required producerslot for :x#11 doesn't exist|count()=1|Details
inline_cte_referenced_threshold = 1) has a filter containing a lambda directly on one of its consumers, as in the example.CollectFilterAboveConsumerrewrites everySlotin the filter withLogicalCTEConsumer.getProducerSlot, whose precondition fails for a slot that is not a consumer output. The lambda parameter is anArrayItemSlotbound by theLambdain the same expression, so it is never in the consumer's map.CollectFilterAboveConsumerTestonly checks that unique-function conjuncts are skipped, with no lambda. Regression: the CTE suites inquery_p0/cteandnereids_rules_p0/cte, such astest_cte_filter_pushdown, put no lambda in a filter on a consumer.SlotNotFromChildrenin the rewrite, asCheckAfterRewrite,NormalizeAggregateandProjectJoinReorderHelperalready do. The collected filter keeps theLambdathat bindsx, so it stays valid if pushed into the producer. Consumer outputs are always plainSlotReferences (LogicalCTEConsumer.generateConsumerSlot) andSlotReference.equalscompares classes, so no real consumer slot is skipped; on master onlyArrayItemSlotcarries the marker.CTEInline,CTEInliner,StatsDerive,StatsCalculator,RuntimeFilterGeneratorandLogicalPlanDeepCopier, map only consumer outputs or usegetInputSlots(), which leaves outArrayItemSlot, so they are not affected.branch-3.1,branch-4.0,branch-4.1andbranch-4.2have the same code. The cherry-picks forbranch-4.1andbranch-3.1are ready on the author's fork.Testing
CollectFilterAboveConsumerdiffers from master's only in the guard before the rewrite: the example query fails with the error above without the fix and returns1with it.query_p0/cte/test_cte_filter_with_lambda.groovy: the example query, and one where the lambda also uses a CTE column (concat(x, id)), both expectingcount()=1. Not run locally; the P0 pipeline runs it, since its directory is not excluded.Release note
Fix queries failing with
Required producerslot for :x#N doesn't existwhen a CTE referenced more than once is filtered with a lambda expression, such asarray_map(x -> ...).Check List (For Author)
AI Disclosure
SlotNotFromChildrenskip could drop slots that should be mapped, and what else the rule still lets through; chose to fix only what is still broken on master.