Keep TRY over a scalar subquery together with its subquery in MERGE action conditions and error messages #357
All Vouch review confidence checks passed
Residual risk 0.29 = Change Risk (0.50) × (1 − Review Confidence (0.43)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 635 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 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.386
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #357"
general
weight: 0.35
kryonix
On main, a
MERGE INTOaction condition orERRORmessage usingTRYover a scalar subquery can raise an error where the equivalentSELECTreturns NULL. A delete withTRYdirectly over the subquery can use its value as the row id: aBIGINTcan delete the wrong row, while anINTEGERtriggers an INT64/INT32 internal error and invalidates the database. DuckDB 2.0.0 rejects this at bind time.The bind-time
TRYlowering in duckdb/duckdb#27386 and duckdb/duckdb#28225 interacted with two separate paths.ProjectionBindermoved action-condition subqueries into a shared projection below the merge operator, leaving column references in the condition and separating the subqueries from theirTRY. Captured errors were then re-raised through__internal_try_rethrowin the projection, with noTRYto catch them.TRYdirectly over a subquery bypassed the projection binder, leaving the subquery in the action. The dependent join planner then added a cross product above the projection, shifting the columns thatPhysicalDeleteandPhysicalUpdateread by position for the row id.We now push
TRYinto the subquery when we plan it. The binder keepsTRY(bound_subquery_expression(...))and marks the binder of a scalar subquery underTRYonly whereTRYreaches it, in the argument ofTRYorHAS_ERRORand in the select item of a subquery underTRY; subqueries inWHERE,IN,EXISTSand aggregate or window arguments are bound and planned as they are. Only the argument ofTRYitself binds its node withoutTRY, so an aggregate of the outer query inside a subquery nested in the argument is rejected with "aggregates are not allowed inside the TRY expression", as on main.Binder::PlanSubquerieswalks the expression and tellsBinder::PlanSubqueryhowTRYreaches each scalar subquery: directly over it, nested in the argument ofTRYorHAS_ERROR, or nested in the select item of a subquery underTRY.Binder::PlanTrySubquerylowersTRYonto the subquery plan before flattening, either directly or with an error flag that is re-raised where the result is used. This keepsHAS_ERROR(1 + (SELECT 'bad'::INTEGER))true andTRY((SELECT 'bad'::INTEGER) IS NULL)NULL. Planning of subqueries in the select item of a subquery underTRYis left until this point as well, because only then do we know whether the containing subquery is the direct argument ofTRY. A bare subquery item thatGROUP BY 1, an alias orGROUP BY ALLmoves into a group is planned with the item too, so it takes the direct lowering through the group, as on main.ProjectionBindernow projects aTRYorHAS_ERRORtogether with its argument when that argument holds a subquery. InMERGE, this keeps the subquery under itsTRYin the projection, leaves no subquery in the action and keeps the row id columns last. ATRYwithout a subquery stays in the action as before. A subquery left in an action after binding raises an internal error in every build, since the actions read the projection by position. The change removesExpressionBinder::BindTrySubquery, the exposed-flag rethrow in the action and the argument-slot carrier, and moves the lowering code tosrc/planner/binder/query_node/plan_try_subquery.cpp. It also allows a volatile expression outside the maskable positions of a nested subquery underTRY, as the direct form already did. UnderGROUP BY ALL, a select item without columns of the subquery now masks its nested subquery as it does elsewhere. A subquery in a lambda body of aMERGEcondition is now rejected when the condition is bound, with "subqueries in lambda expressions are not supported" as inSELECT; main accepted an uncorrelated one in a condition by moving it into the projection and raised an internal error for one over a lambda parameter. A subquery thatTRYcannot reach, such as a set operation as the bare select item of a subquery underTRY, is now planned as it is instead of being rejected at bind time.test/sql/merge/merge_into_try_subquery.testfails on the base and passes with the change, covering both failures, correlation, every action kind,HAS_ERROR,TRYover expressions that tolerate NULL, result modifiers under direct and nestedTRY, error messages, macros and lambdas.test/sql/subquery/scalar/test_try_subquery.testpins the lowering inSELECT, includingTRYover a subquery in the conditions of inner and outer joins. The reldebug unit test suite passes apart from a pre-existing harness artifact;test/sql/merge,test/sql/subquery,test/sql/error,test/sql/join,test/sql/function/list/lambdasandtest/sql/optimizerpass on reldebug and on a relassert build with ASan and UBSan.Fixes duckdblabs/bot-issue-tracker#759
To clarify, I'm an autonomous bot following workflows described by the DuckDB maintainers. Take what I say and do with a grain of salt. You can find more information, or report concerns about my behavior, in this repository: https://github.com/duckdb-bot/guidelines
agent_version:0a24a835181abc2cf036c87f0649c386df0b96a7