Repository navigation
fix(plan): support scalar subqueries in REPLACE values - #28335
Conversation
Qodo reviews are paused for this user.Troubleshooting steps vary by plan Learn more → On a Teams plan? Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center? |
aptend
left a comment
There was a problem hiding this comment.
Deep re-review at exact head 313b7d5 after reading all prior reviews, the resolved inline thread, and the full current diff. The previous source-order blocker is materially closed: the ordinal sort is before REPLACE keep-last arbitration, the deterministic fault-barrier integration test passed locally, and the full planner package plus focused race runs passed.
The latest literal batching also closes the specific case where one scalar subquery caused every otherwise-literal row to become a branch. However, it does not bound the new relational fan-out when each VALUES row contains a scalar subquery. An exact-head review-only 1,000-row counterexample produced 999 UNION_ALL nodes and 8,030 total plan nodes. Its planner benchmark measured 99.4 ms/op, 86,170,229 B/op, and 647,718 allocations/op, versus 3.43 ms/op, 4,482,901 B/op, and 67,983 allocations/op for the submitted one-subquery-plus-literals benchmark. These are normal concurrent UNION branches feeding a blocking ordinal sort, so accepted SQL can create an unbounded burst of subquery pipelines and large per-statement planning amplification. This remains a blocking CN resource-exhaustion path.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-reviewed all eight changed files and their binder, scalar-flattening, UNION/SORT, REPLACE arbitration and test-cleanup consumers at exact head 313b7d5 against base 585a38e.
The previous VALUES-order correctness issue is fixed: a stable ordinal is attached before branch execution, restored by an INTERNAL ascending SORT, and removed before the later materialized source/keep-last arbitration. The strengthened planner test checks that boundary and its step ordering. Public cases no longer use SLEEP; prepared multi-row coverage, barrier-driven PK/UK scenarios, scalar-cardinality failure atomicity and cancellation cleanup are added. Literal-only rows now share one RowsetData branch, and the duplicate expression-view allocation is confined to the subquery path. These improvements close the earlier specific requests; I am not requesting changes merely because more test evidence would be useful.
[P2] The all-subquery VALUES path still has no bounded branch admission
Location: pkg/sql/plan/bind_insert.go:5375 (per-subquery-row branch construction), and :5512 (left-deep UNION_ALL construction).
Trigger: an accepted REPLACE VALUES statement with N rows, each containing (SELECT MAX(n_nationkey) FROM nation WHERE n_nationkey = i), rather than one subquery plus N-1 literals. Every row takes the independent DUAL/flattened-subquery/PROJECT path; no literal batching applies and no subquery-row budget rejects or chunks the expansion. N rows produce N branches and N-1 UNION nodes before a blocking global ordinal sort. The compiler retains normal non-lazy UNION_ALL topology here (compile.go compileUnionAll), and Scope.MergeRun recursively submits all non-lazy PreScopes; the row limit of each scalar result does not bound active subquery pipelines or planning work.
There is exact-head independent counterexample evidence, not only a missing benchmark: aptend's recorded 1,000-row case produces 999 UNION_ALL / 8,030 total plan nodes, with 99.4 ms/op, 86,170,229 B/op and 647,718 allocations/op, compared with 3.43 ms/op / 4,482,901 B/op for the submitted one-subquery case. See #28335 (comment). I revalidated the source path; those measurements are attributed/reused, not a new local run. B/op is cumulative allocation, not peak RSS, and I am not claiming an observed OOM. The verified issue is unbounded per-statement concurrent branch expansion and substantial planning amplification on this newly accepted input, which creates CN capacity risk for large statements/concurrent sessions.
Please enforce a documented subquery-row/branch budget before creating the relational branches (preferably before expensive binding), or implement a genuinely bounded chunked execution strategy while retaining the ordinal contract. Cover the all-subquery case at/beyond the bound and cancellation/error cleanup. A balanced UNION tree alone does not bound active work. Do not globally force UNION_ALL lazy: the existing blocking-consumer dependency warning still applies.
Current substantive CI 34204423573 is green. Exact-head independent planner, focused race and deterministic integration evidence is reused where relevant; no fresh native test or claimed OOM experiment was performed here. Remaining optional test refinement: waiter count dropping proves departure from the synthetic WAIT, not downstream branch publication by itself; a publication/completion barrier would make the reverse-arrival oracle even stronger. This is non-blocking and is not the reason for REQUEST_CHANGES.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-review of exact head 67a8732: the remaining blocker from my 313b7d5 review is closed. APPROVE.
The new 32-subquery-row admission check runs at the start of buildValueScan, before expression binding and scalar-subquery relational node creation. It counts each row once, including a subquery nested in an expression, and rejects the 33rd subquery row. I traced the reused reflective AST walker: it handles pointer/interface/struct/list children and stops below a found Subquery; literal/default/parameter classification keeps the common simple path allocation-free. Literal rows still share the compact RowsetData branch, so the newly introduced VALUES fan-out is now bounded at 32 subquery branches plus at most one literal branch. This directly prevents the previously demonstrated 1,000-subquery-row expansion; it does not pretend that a balanced UNION alone bounds work.
The strengthened planner test checks acceptance at 32 with bounded plan nodes and 31 UNION_ALL nodes, rejection at 33 including nested COALESCE, and the public BVT adds the over-limit error. New benchmark shapes explicitly cover the all-subquery boundary and rejection of the 1,000-row case. The earlier stable-ordinal sort remains before REPLACE keep-last arbitration; no global lazy-UNION change is introduced. Reused the previously reviewed matching ordering, literal batching, SET defaults, prepared execution, scalar-cardinality atomicity and cancellation test paths; examined the current-head delta in binder/tests and the new BVT hunks.
Scope and evidence: this is a bound on the new per-VALUES-row expansion, not a universal cap on the inherent complexity of an individual SQL subquery. The earlier deterministic integration/normal/race evidence remains relevant to unchanged paths. Current canonical 13:46 UTC gate is green and the exact head was revalidated. I did not independently rerun native tests or publish new benchmark timings. More runtime evidence alone is not a reason to retain REQUEST_CHANGES once the verified source blocker is fixed. Non-blocking suggestion: mention the explicit 32-row limit in user-facing PR documentation so callers know when to split batches.
What type of PR is this?
Which issue(s) this PR fixes:
Fixes #28299
What this PR does / why we need it:
REPLACE ... SETandREPLACE ... VALUES, including prepared statements.RowsetDatafast path for literal-only values and retainREPLACE ... SETdefault-column semantics.Validation
go test ./pkg/sql/plango vet ./pkg/sql/planmo-testerreplace BVT: 499/499 successful, repeated on the same instance