Repository navigation
fix(hashbuild): avoid false spill budget rejection for reused tail batches - #26178
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? |
XuPeng-SH
left a comment
There was a problem hiding this comment.
I still see one merge-blocking spill-accounting issue on the current head:
Null varlen tail batches can still be mischarged as multi-row payload and abort spill.
In pkg/sql/colexec/hashbuild/spill.go:188-224, spillProjectedSourceBytes skips only vec.IsConstNull(), then sums out-of-line varlen payload from every row in values. That misses the reused non-const tail-batch shape where the only published row is NULL but its slot still retains a stale non-inline varlena header from an earlier batch. We already have an explicit guard for this exact vector shape in pkg/container/vector/vector.go:2784-2798 (and unionbatch_fastpath_test.go:329-360), where null rows are excluded because reused varlen vectors can keep stale large headers in null slots.
With the new spill estimator, a one-row NULL tail can still be projected as DefaultBatchSize * staleLen, so ensureSpillScratchReservation in pkg/sql/colexec/hashbuild/build.go:265-279 can return hash build budget admission rejected before spill makes progress (or after the retry path rechecks the same batch). Please skip non-const null rows when summing external varlen payload, mirroring the existing vector pre-grow logic, and add a regression test for a reused one-row NULL varchar tail with a planted stale large header.
|
Addressed the stale-null varlena accounting issue in 50a797f. |
XuPeng-SH
left a comment
There was a problem hiding this comment.
I took another pass and found one more independent blocker on the current head:
Large const varlen values are still budgeted as if their out-of-line payload were copied once per row.
spillSourceBytes / spillProjectedSourceBytes still multiply a large const varlen payload by rows / targetRows (pkg/sql/colexec/hashbuild/spill.go:94-105, :216-224). But the actual const-varlen materialization path does not duplicate the area per row: Union calls BuildVarlenaFromVarlena once and then fillSlice fans out the descriptor (pkg/container/vector/vector.go:2758-2769, 5341-5348), BuildVarlenaNoInline appends the bytes once (vector.go:5296-5313), and marshal writes area once (vector.go:773-777). So a shuffled/tail batch with a large const varchar/blob can still be overcharged by orders of magnitude and falsely fail spill admission.
The new regression at pkg/sql/colexec/hashbuild/spill_test.go:888-930 uses a ~30-byte const string, so it never exercises the out-of-line path. Please charge one live out-of-line payload copy for const varlen vectors and add a regression with a MiB-scale const payload.
50a797f to
fd3e5db
Compare
XuPeng-SH
left a comment
There was a problem hiding this comment.
Latest-head re-review from scratch: one correction and one current blocker.
Correction on my previous exact-8192 const-retained comment: that point was based on the wrong closure. CopyIntoBatches does call Batch.Dup, but Batch.Dup goes through Batch.Clone -> CloneTo -> Vector.UnionBatch (pkg/container/batch/batch.go:580-613), so the retained batch is not preserved as a const vector via Vector.Dup. Please disregard that earlier exact-8192 const-retained concern from me.
Current blocker: startSpill() drains retained batches while the current ingress batch is still live, but the drain path is only admitted against the retained-batch scratch proof. The overlap state is under-budgeted.
spillBudgetBytesfor a direct spill explicitly includes the live upstream batch viauint64(bat.Allocated())(pkg/sql/colexec/hashbuild/spill.go:183-195).- But
startSpill()drains retained copies withspillBatchBounded(..., sourceAlreadyCharged=true)(pkg/sql/colexec/hashbuild/build.go:213-245), and that path subtracts the retained source from the scratch proof (spill.go:197-212,468-507). startSpill()is called frombuild()beforeresult.Batchis spilled or released (build.go:266-293), so the current ingress batch is still resident while the retained queue is being drained.
That means the real recovery peak is current ingress source bytes + retained-drain scratch, but the code only proves either the current direct spill path or the retained-drain path in isolation. A budget that is sufficient for the retained drain alone can therefore pass admission and still exceed the query/CN budget during recovery.
Please make the spill transition reserve/prove that overlap state explicitly while draining retained batches, and add a regression that exercises startSpill() with one live ingress batch plus a retained queue under a cap that separates the isolated proofs from the combined peak.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Re-evaluated the overlap-accounting finding against its production impact and the scope of this PR. The gap is real, but it is limited to the spill-transition overlap window, has no known result/data-correctness impact, and should not expand or block the #26174 fix in this PR.
Tracked separately in #26186 with the required overlap proof and regression boundaries. Superseding my earlier merge-blocking position: this PR is okay to merge first.
Merge Queue Status
This pull request spent 57 seconds in the queue, with no time running CI. Waiting for
All conditions
ReasonPull request #26178 has been dequeued Pull request from fork cannot be queued. This pull request comes from a fork, and Mergify needs the author's permission to update its branch.
HintYou should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it. Tick the box to put this pull request back in the merge queue (same as
|
…tches (matrixorigin#26178) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [x] Code Refactoring ## Which issue(s) this PR fixes: fixes matrixorigin#25782 ## What this PR does / why we need it: HashBuild previously used one estimate for two materially different spill sources: the current upstream batch and a future batch retained by `CopyIntoBatches`. That conflated physical input capacity, selected-batch materialization, and marshal peak. Reused tail batches and const vectors could therefore be extrapolated into false multi-gigabyte requests and return `hash build budget admission rejected` before spill made progress. For the failing `generate_series(3840001)` plus constant-varchar BVT shape, the old path requested 18.72 GB against a 10 GB query cap while only about 1.29 GB was in use. This change replaces the estimator patch chain with a two-stage admission invariant: 1. Before handling an upstream shuffle batch, prove the actual direct-spill path. Direct const-varlen materialization follows `UnionInt32`: one live out-of-line payload plus per-row descriptors. 2. Before `CopyIntoBatches` may retain that batch, separately prove the largest future retained batch can be drained. The retained destination is non-const, so a later selection can copy payload per retained row and is budgeted accordingly. 3. If the retained proof is rejected, do not copy. Switch to the already-proven direct-spill path. 4. Every retained batch therefore carries a prior future-drain proof; existing retained batches can be drained to release headroom before retrying a larger direct input. The accounting explicitly separates upstream physical input, selected materialization, row scratch, and marshal growth, uses checked arithmetic, and skips stale varlen headers in NULL rows. Regression coverage includes: - reused one-row tails retaining a 32K-row allocation; - stale const logical length after shuffle; - stale non-inline headers in NULL rows; - MiB-scale const varlen direct materialization (one payload copy); - the real `CopyIntoBatches` const-to-nonconst transition and future selection; - retained-proof rejection falling back to successful direct spill; - direct-proof rejection while retained memory is live, followed by drain and retry; - arithmetic overflow and fail-closed admission boundaries; - reservation, mpool, spill-disk, and spill-FD cleanup. Validation on final rebased head: - `go test -count=1 -timeout=180s ./pkg/sql/colexec/hashbuild` - `go test -count=1 -timeout=180s ./pkg/sql/colexec/hashjoin` - focused `go test -race` over the new materialization and state-transition tests - `go build ./pkg/sql/colexec/hashbuild` - `go vet ./pkg/sql/colexec/hashbuild` - local CI-equivalent diff coverage: 82.02% (requested floor: 80%) - prior TKE BVT run https://github.com/matrixorigin/ci-test/actions/runs/30166184404: the three previously failing inserts succeeded in 1.355s, 1.431s, and 1.425s, with zero hash-build budget admission rejections. A fresh CI/TKE validation is required for this final refactored head. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…tches (matrixorigin#26178) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [x] Code Refactoring ## Which issue(s) this PR fixes: fixes matrixorigin#25782 ## What this PR does / why we need it: HashBuild previously used one estimate for two materially different spill sources: the current upstream batch and a future batch retained by `CopyIntoBatches`. That conflated physical input capacity, selected-batch materialization, and marshal peak. Reused tail batches and const vectors could therefore be extrapolated into false multi-gigabyte requests and return `hash build budget admission rejected` before spill made progress. For the failing `generate_series(3840001)` plus constant-varchar BVT shape, the old path requested 18.72 GB against a 10 GB query cap while only about 1.29 GB was in use. This change replaces the estimator patch chain with a two-stage admission invariant: 1. Before handling an upstream shuffle batch, prove the actual direct-spill path. Direct const-varlen materialization follows `UnionInt32`: one live out-of-line payload plus per-row descriptors. 2. Before `CopyIntoBatches` may retain that batch, separately prove the largest future retained batch can be drained. The retained destination is non-const, so a later selection can copy payload per retained row and is budgeted accordingly. 3. If the retained proof is rejected, do not copy. Switch to the already-proven direct-spill path. 4. Every retained batch therefore carries a prior future-drain proof; existing retained batches can be drained to release headroom before retrying a larger direct input. The accounting explicitly separates upstream physical input, selected materialization, row scratch, and marshal growth, uses checked arithmetic, and skips stale varlen headers in NULL rows. Regression coverage includes: - reused one-row tails retaining a 32K-row allocation; - stale const logical length after shuffle; - stale non-inline headers in NULL rows; - MiB-scale const varlen direct materialization (one payload copy); - the real `CopyIntoBatches` const-to-nonconst transition and future selection; - retained-proof rejection falling back to successful direct spill; - direct-proof rejection while retained memory is live, followed by drain and retry; - arithmetic overflow and fail-closed admission boundaries; - reservation, mpool, spill-disk, and spill-FD cleanup. Validation on final rebased head: - `go test -count=1 -timeout=180s ./pkg/sql/colexec/hashbuild` - `go test -count=1 -timeout=180s ./pkg/sql/colexec/hashjoin` - focused `go test -race` over the new materialization and state-transition tests - `go build ./pkg/sql/colexec/hashbuild` - `go vet ./pkg/sql/colexec/hashbuild` - local CI-equivalent diff coverage: 82.02% (requested floor: 80%) - prior TKE BVT run https://github.com/matrixorigin/ci-test/actions/runs/30166184404: the three previously failing inserts succeeded in 1.355s, 1.431s, and 1.425s, with zero hash-build budget admission rejections. A fresh CI/TKE validation is required for this final refactored head. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
…tches (matrixorigin#26178) ## What type of PR is this? - [ ] API-change - [x] BUG - [ ] Improvement - [ ] Documentation - [ ] Feature - [ ] Test and CI - [x] Code Refactoring ## Which issue(s) this PR fixes: fixes matrixorigin#25782 ## What this PR does / why we need it: HashBuild previously used one estimate for two materially different spill sources: the current upstream batch and a future batch retained by `CopyIntoBatches`. That conflated physical input capacity, selected-batch materialization, and marshal peak. Reused tail batches and const vectors could therefore be extrapolated into false multi-gigabyte requests and return `hash build budget admission rejected` before spill made progress. For the failing `generate_series(3840001)` plus constant-varchar BVT shape, the old path requested 18.72 GB against a 10 GB query cap while only about 1.29 GB was in use. This change replaces the estimator patch chain with a two-stage admission invariant: 1. Before handling an upstream shuffle batch, prove the actual direct-spill path. Direct const-varlen materialization follows `UnionInt32`: one live out-of-line payload plus per-row descriptors. 2. Before `CopyIntoBatches` may retain that batch, separately prove the largest future retained batch can be drained. The retained destination is non-const, so a later selection can copy payload per retained row and is budgeted accordingly. 3. If the retained proof is rejected, do not copy. Switch to the already-proven direct-spill path. 4. Every retained batch therefore carries a prior future-drain proof; existing retained batches can be drained to release headroom before retrying a larger direct input. The accounting explicitly separates upstream physical input, selected materialization, row scratch, and marshal growth, uses checked arithmetic, and skips stale varlen headers in NULL rows. Regression coverage includes: - reused one-row tails retaining a 32K-row allocation; - stale const logical length after shuffle; - stale non-inline headers in NULL rows; - MiB-scale const varlen direct materialization (one payload copy); - the real `CopyIntoBatches` const-to-nonconst transition and future selection; - retained-proof rejection falling back to successful direct spill; - direct-proof rejection while retained memory is live, followed by drain and retry; - arithmetic overflow and fail-closed admission boundaries; - reservation, mpool, spill-disk, and spill-FD cleanup. Validation on final rebased head: - `go test -count=1 -timeout=180s ./pkg/sql/colexec/hashbuild` - `go test -count=1 -timeout=180s ./pkg/sql/colexec/hashjoin` - focused `go test -race` over the new materialization and state-transition tests - `go build ./pkg/sql/colexec/hashbuild` - `go vet ./pkg/sql/colexec/hashbuild` - local CI-equivalent diff coverage: 82.02% (requested floor: 80%) - prior TKE BVT run https://github.com/matrixorigin/ci-test/actions/runs/30166184404: the three previously failing inserts succeeded in 1.355s, 1.431s, and 1.425s, with zero hash-build budget admission rejections. A fresh CI/TKE validation is required for this final refactored head. --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
What type of PR is this?
Which issue(s) this PR fixes:
fixes #25782
What this PR does / why we need it:
HashBuild previously used one estimate for two materially different spill sources: the current upstream batch and a future batch retained by
CopyIntoBatches. That conflated physical input capacity, selected-batch materialization, and marshal peak. Reused tail batches and const vectors could therefore be extrapolated into false multi-gigabyte requests and returnhash build budget admission rejectedbefore spill made progress.For the failing
generate_series(3840001)plus constant-varchar BVT shape, the old path requested 18.72 GB against a 10 GB query cap while only about 1.29 GB was in use.This change replaces the estimator patch chain with a two-stage admission invariant:
UnionInt32: one live out-of-line payload plus per-row descriptors.CopyIntoBatchesmay retain that batch, separately prove the largest future retained batch can be drained. The retained destination is non-const, so a later selection can copy payload per retained row and is budgeted accordingly.The accounting explicitly separates upstream physical input, selected materialization, row scratch, and marshal growth, uses checked arithmetic, and skips stale varlen headers in NULL rows.
Regression coverage includes:
CopyIntoBatchesconst-to-nonconst transition and future selection;Validation on final rebased head:
go test -count=1 -timeout=180s ./pkg/sql/colexec/hashbuildgo test -count=1 -timeout=180s ./pkg/sql/colexec/hashjoingo test -raceover the new materialization and state-transition testsgo build ./pkg/sql/colexec/hashbuildgo vet ./pkg/sql/colexec/hashbuild