← Back to Pull requests

[fix](nereids) Skip lambda variables when collecting filters above CTE consumers #71318

Closed
unknown closed this pull request without merging on Dec 03, 2026 23:57 UTC
View on GitHub
unknown unknown commented on Dec 02, 2026 19:18 UTC
Author

Background

When a CTE is referenced more than once, Nereids keeps it materialized: one LogicalCTEProducer computes it, and each reference reads it through a LogicalCTEConsumer whose output slots map to the producer's. To shrink the shared result, CollectFilterAboveConsumer collects the filters sitting directly on each consumer, rewritten onto the producer's slots, and RewriteCteChildren pushes them into the producer when every consumer has a matching one. A lambda such as x -> trim(x) binds its parameter x as an ArrayItemSlot, which is marked SlotNotFromChildren because 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 exist when 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 shared is referenced twice, and f0.c > 0 is pushed through f0's projection onto its consumer:

WITH shared AS (SELECT '1' AS id),
     f0 AS (SELECT id, array_size(array_map(x -> trim(x), split_by_string('a;b', ';'))) AS c FROM shared),
     f1 AS (SELECT id FROM shared)
SELECT count(*) FROM f0, f1 WHERE f0.c > 0;

| Before the fix | With the fix |
|---|---|
| ERROR 1105 (HY000): errCode = 2, detailMessage = Required producerslot for :x#11 doesn't exist | count() = 1 |

Details

  • Frequency: Always, when a CTE referenced more than once (so materialized, with the default inline_cte_referenced_threshold = 1) has a filter containing a lambda directly on one of its consumers, as in the example.

  • Root cause: CollectFilterAboveConsumer rewrites every Slot in the filter with LogicalCTEConsumer.getProducerSlot, whose precondition fails for a slot that is not a consumer output. The lambda parameter is an ArrayItemSlot bound by the Lambda in the same expression, so it is never in the consumer's map.

  • Why existing tests missed it: Unit tests: CollectFilterAboveConsumerTest only checks that unique-function conjuncts are skipped, with no lambda. Regression: the CTE suites in query_p0/cte and nereids_rules_p0/cte, such as test_cte_filter_pushdown, put no lambda in a filter on a consumer.

  • Fix: Skip slots that implement SlotNotFromChildren in the rewrite, as CheckAfterRewrite, NormalizeAggregate and ProjectJoinReorderHelper already do. The collected filter keeps the Lambda that binds x, so it stays valid if pushed into the producer. Consumer outputs are always plain SlotReferences (LogicalCTEConsumer.generateConsumerSlot) and SlotReference.equals compares classes, so no real consumer slot is skipped; on master only ArrayItemSlot carries the marker.

  • Other paths and branches: The other users of the consumer-to-producer map, such as CTEInline, CTEInliner, StatsDerive, StatsCalculator, RuntimeFilterGenerator and LogicalPlanDeepCopier, map only consumer outputs or use getInputSlots(), which leaves out ArrayItemSlot, so they are not affected. branch-3.1, branch-4.0, branch-4.1 and branch-4.2 have the same code. The cherry-picks for branch-4.1 and branch-3.1 are ready on the author's fork.
  • Testing

  • Manual, reported by the author, on a 3.1-based build whose CollectFilterAboveConsumer differs from master's only in the guard before the rewrite: the example query fails with the error above without the fix and returns 1 with it.

  • New regression suite 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 expecting count() = 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 exist when a CTE referenced more than once is filtered with a lambda expression, such as array_map(x -> ...).

    Check List (For Author)

  • Behavior changed: No

  • Does this need documentation: No

  • Pre-submission status: Manual test on 3.1 cluster but this file hasn't changed in master or 4.1
  • AI Disclosure

  • Tool: Claude Code (Claude Opus 5.5)

  • Generated: the fix, the regression test, and this description

  • Human review: Reviewed by the author

  • Prompts: Asked the agent why the query fails and whether master has the same bug, then to build images with and without the fix for a before-and-after run. Had subagents check whether the SlotNotFromChildren skip 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.
  • Diff: 618 lines (+463 / -155) across 7 files Reviewer: morrySnow Review duration: 1718m
    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
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 50.2% (MEDIUM) · Higher = larger blast radius
    50.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 19.8% · Safe to merge
    19.8%
    Repository