fix(native): Fix BIND expression loss for captured lambdas in NativeExpressionOptimizer #27593
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
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.351
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #27593"
general
weight: 0.35
pramodsatya
Description
Fixes
Field not founderrors when the native expression optimizer processes lambda expressions that capture outer-scope variables i.e., lambdas insideBINDexpressions.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 aBINDspecial-form expression, eg:BIND(capturedVar, Lambda(extraArg, x, body))indicatescapturedVaris supplied as the first argument of the lambda at call time.The native sidecar optimizer receives this
BINDexpression and in the process of converting it to a Velox expression, discards theBINDwrapper, inlines the captured variable references directly into the lambda body, and returns a plainLambdaTypedExpr. After optimization,VeloxToPrestoExpr::getLambdaExpressionconverts the result back to PrestoRowExpression, but it previously returned a plainLambdaDefinitionExpressionwith noBINDaround 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 explicitBINDarguments, so the planner could prune the column from the input row. At native execution time, Velox then failed withVeloxUserError: 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
Summary by Sourcery
Ensure native expression conversion preserves captured variables in lambdas by wrapping them in BIND expressions when needed.
Bug Fixes:
Tests:
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:
Field not founderrors at runtime.Tests: