← Back to Pull requests

fix(native): Fix BIND expression loss for captured lambdas in NativeExpressionOptimizer #27593

Closed
unknown closed this pull request without merging on May 05, 2026 02:00 UTC
View on GitHub
unknown unknown commented on Apr 15, 2026 00:30 UTC
Author

Description


Fixes Field not found errors when the native expression optimizer processes lambda expressions that capture outer-scope variables i.e., lambdas inside BIND expressions.

Motivation and Context


Resolves https://github.com/prestodb/presto/issues/27449.

When a query contains a higher-order function (e.g., transform, filter) whose lambda body references a column from an outer scope, Presto's planner desugars this into a BIND special-form expression, eg: BIND(capturedVar, Lambda(extraArg, x, body)) indicates capturedVar is supplied as the first argument of the lambda at call time.

The native sidecar optimizer receives this BIND expression and in the process of converting it to a Velox expression, discards the BIND wrapper, inlines the captured variable references directly into the lambda body, and returns a plain LambdaTypedExpr. After optimization, VeloxToPrestoExpr::getLambdaExpression converts the result back to Presto RowExpression, but it previously returned a plain LambdaDefinitionExpression with no BIND around it.

The resulting expression had dangling free variable references (e.g., field_0) with nothing to supply them. These were no longer visible to the Presto planner as explicit BIND arguments, so the planner could prune the column from the input row. At native execution time, Velox then failed with VeloxUserError: Field not found: field_0. Available fields are: expr, field.

Impact


Queries that use higher-order functions with lambda expressions capturing outer-scope columns (e.g., SELECT transform(some_array, x -> x + outer_col) FROM t) would fail at runtime when the native expression optimizer was enabled. This is a correctness fix with no performance impact.

Test Plan


Added e2e tests.

Release Notes

== NO RELEASE NOTE ==

Summary by Sourcery

Ensure native expression conversion preserves captured variables in lambdas by wrapping them in BIND expressions when needed.

Bug Fixes:

  • Fix loss of captured outer variables in lambda expressions during Velox-to-Presto RowExpression conversion by introducing BIND wrappers for lambdas with free variables.
  • Tests:

  • Extend native expression optimizer tests to cover queries where lambdas capture outer columns, verifying BIND expressions are preserved.
  • Summary by Sourcery

    Preserve captured outer-scope variables when converting Velox lambdas to Presto RowExpressions by wrapping lambdas with BIND expressions when free variables are present.

    Bug Fixes:

  • Fix loss of captured variables in lambda expressions during Velox-to-Presto conversion that caused Field not found errors at runtime.
  • Tests:

  • Extend native expression optimizer end-to-end tests to cover queries where lambdas capture outer columns and verify successful execution.
  • Diff: 355 lines (+266 / -89) across 4 files Reviewer: pramodsatya Review duration: 28890m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.43) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 355 lines diff across 4 files
    0.43
    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%
    56%
    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.351
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #27593"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 42.5% (MEDIUM) · Higher = larger blast radius
    42.5%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 18.4% · Safe to merge
    18.4%