← Back to Pull requests

Keep TRY over a scalar subquery together with its subquery in MERGE action conditions and error messages #357

Open
unknown opened this pull request with 635 lines diff across 7 files
View on GitHub
unknown unknown commented on Nov 04, 2026 00:54 UTC
Author

On main, a MERGE INTO action condition or ERROR message using TRY over a scalar subquery can raise an error where the equivalent SELECT returns NULL. A delete with TRY directly over the subquery can use its value as the row id: a BIGINT can delete the wrong row, while an INTEGER triggers an INT64/INT32 internal error and invalidates the database. DuckDB 2.0.0 rejects this at bind time.

The bind-time TRY lowering in duckdb/duckdb#27386 and duckdb/duckdb#28225 interacted with two separate paths. ProjectionBinder moved action-condition subqueries into a shared projection below the merge operator, leaving column references in the condition and separating the subqueries from their TRY. Captured errors were then re-raised through __internal_try_rethrow in the projection, with no TRY to catch them. TRY directly 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 that PhysicalDelete and PhysicalUpdate read by position for the row id.

We now push TRY into the subquery when we plan it. The binder keeps TRY(bound_subquery_expression(...)) and marks the binder of a scalar subquery under TRY only where TRY reaches it, in the argument of TRY or HAS_ERROR and in the select item of a subquery under TRY; subqueries in WHERE, IN, EXISTS and aggregate or window arguments are bound and planned as they are. Only the argument of TRY itself binds its node without TRY, 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::PlanSubqueries walks the expression and tells Binder::PlanSubquery how TRY reaches each scalar subquery: directly over it, nested in the argument of TRY or HAS_ERROR, or nested in the select item of a subquery under TRY. Binder::PlanTrySubquery lowers TRY onto the subquery plan before flattening, either directly or with an error flag that is re-raised where the result is used. This keeps HAS_ERROR(1 + (SELECT 'bad'::INTEGER)) true and TRY((SELECT 'bad'::INTEGER) IS NULL) NULL. Planning of subqueries in the select item of a subquery under TRY is left until this point as well, because only then do we know whether the containing subquery is the direct argument of TRY. A bare subquery item that GROUP BY 1, an alias or GROUP BY ALL moves into a group is planned with the item too, so it takes the direct lowering through the group, as on main. ProjectionBinder now projects a TRY or HAS_ERROR together with its argument when that argument holds a subquery. In MERGE, this keeps the subquery under its TRY in the projection, leaves no subquery in the action and keeps the row id columns last. A TRY without 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 removes ExpressionBinder::BindTrySubquery, the exposed-flag rethrow in the action and the argument-slot carrier, and moves the lowering code to src/planner/binder/query_node/plan_try_subquery.cpp. It also allows a volatile expression outside the maskable positions of a nested subquery under TRY, as the direct form already did. Under GROUP 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 a MERGE condition is now rejected when the condition is bound, with "subqueries in lambda expressions are not supported" as in SELECT; 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 that TRY cannot reach, such as a set operation as the bare select item of a subquery under TRY, is now planned as it is instead of being rejected at bind time. test/sql/merge/merge_into_try_subquery.test fails on the base and passes with the change, covering both failures, correlation, every action kind, HAS_ERROR, TRY over expressions that tolerate NULL, result modifiers under direct and nested TRY, error messages, macros and lambdas. test/sql/subquery/scalar/test_try_subquery.test pins the lowering in SELECT, including TRY over 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/lambdas and test/sql/optimizer pass 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

Diff: 635 lines (+476 / -159) across 7 files Reviewer: kryonix Review duration: 30040m
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
0.50
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
0.02
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
42%
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.386
Shallow Review Depth shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #357"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 43.0% 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.4% (MEDIUM) · Higher = larger blast radius
50.4%
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: 43.0% · Lower = rushed or shallow review
43.0%
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: 28.8% · Safe to merge
28.8%