Repository navigation
executor: add exact memory accounting for group, order, top, and fill - #27112
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? |
9a5db5f to
77b83ec
Compare
77b83ec to
9967e52
Compare
iamlinjunhong
left a comment
There was a problem hiding this comment.
Blocking finding:
- P1
pkg/container/vector/selection_codec.go:95: the selected-row spill codec drops dynamic binary-string provenance, so Group and aggregate results can depend on whether memory pressure triggers spill. See the inline comment for the verified write/read call chain and missing regression.
No additional P0-P3 findings were identified in the reviewed diff.
aunjgr
left a comment
There was a problem hiding this comment.
Request changes: selected-row spill loses binary-string provenance. In pkg/container/vector/selection_codec.go:95-171, the codec writes NULL/grouping/prepare-param flags and raw bytes, but never writes GetBinaryStringMetadataAt(row). UnmarshalSelectedRowsFrom calls CleanOnlyData() and restores no binary metadata, so a TEXT/VARCHAR vector that carries dynamic per-row binary semantics is restored as ordinary text. This codec is used by Group spill (pkg/sql/colexec/group/helper.go) and aggregate-state spill (pkg/sql/colexec/aggexec/aggState.go), so results can change only when memory pressure causes spill; the resident path preserves the metadata. Please encode uniform/row-exact binary provenance (including NULL-row alignment), restore it transactionally, and add a spill/reload regression alongside the existing non-spill coverage. The exact head 34e5b50 and CI were green when reviewed.
XuPeng-SH
left a comment
There was a problem hiding this comment.
[P1] Preserve dynamic binary-string provenance in the selected-row spill codec.
Deep review on exact head 34e5b50 confirms a pressure-dependent correctness break in pkg/container/vector/selection_codec.go. MarshalSelectedRowsTo records NULL, grouping, PrepareParamKind, and raw bytes, but never records GetBinaryStringMetadataAt(row). UnmarshalSelectedRowsFrom then calls CleanOnlyData(), which explicitly resetBinaryString(), and restores no binary metadata.
This is reachable through both changed spill closures: Group keys call appendSpillGroupByRows -> MarshalSelectedRowsTo and reload through unmarshalSpillGroupByRows; aggregate state calls writeSpillState -> MarshalSelectedFlagsTo and readSpillState -> UnmarshalSelectedRowsFrom. A T_text/T_varchar row with SetIsBinaryString(true) therefore preserves byte-string semantics while resident but becomes ordinary text after spill/reload, so query semantics can depend solely on memory pressure. The existing TestMergeGroupPreservesBinaryStringProvenance covers only the resident/partial path and does not exercise this codec.
Please extend the codec metadata with uniform and row-exact binary provenance, preserve NULL-row alignment, restore it transactionally with the other row metadata, and add direct codec plus forced spill/reload regressions. Exact-head CI being green does not cover this missing semantic axis.
iamlinjunhong
left a comment
There was a problem hiding this comment.
Reviewed exact head b3d98c2 against current origin/main.
The previous P1 about spill codec binary-string provenance is fixed by b3d98c2; the author replied with the matching fix/coverage and that thread is resolved.
Remaining finding:
- P2 pkg/container/vector/vector.go:4100: selected-batch preflight omits the binary-string provenance bitmap. Group commits the hash before publishing the preflighted key vectors, so a mixed binary/text key can hit a retryable capacity allocation only after mutation and abort instead of taking the existing spill/retry path. See the inline comment for the verified call chain and missing boundary test.
No P0/P1 findings remain. Exact-head checks are complete with no failures.
XuPeng-SH
left a comment
There was a problem hiding this comment.
[P2] Complete selected-batch allocation preflight before committing Group state.
On exact head b3d98c2, PreExtendSelectedBatch* reserves descriptors, NULL/grouping bitmaps, prepare-param metadata, and varlena area, then publishes a ready proof at pkg/container/vector/vector.go:4100. It does not call PreflightUnionBatchBinaryString; the supposedly preflighted unionBatch still performs that fallible allocation at line 6742.
I reproduced this with an accounted T_text destination and two selected source rows carrying mixed binary/text provenance. A wide-budget run used 48 bytes after preflight and 56 after publication. Re-running with the account capped at the preflight result made PreExtendSelectedBatch succeed, then UnionBatchPreflighted fail with allocation account capacity exceeded: used=48 requested=8 limit=48.
This is not only a vector protocol mismatch. Group.commitGroupByChunk commits TxnItr before appendGroupByBatch calls UnionBatchPreflighted, so this late capacity failure occurs after hash mutation and bypasses the existing pre-commit spill/retry path. A query that could recover by spilling its resident prefix instead aborts under the exact cap.
Please include binary-string provenance in selected-batch preflight, ensure publication performs no allocation, and add an exact-cap Group regression with mixed binary/text keys. The previous spill-codec provenance blocker is fixed; this is the remaining closure edge.
iamlinjunhong
left a comment
There was a problem hiding this comment.
Reviewed exact head 46c7983 against current origin/main a1e161a (merge-base 7e03000). The previous P2 about selected-batch binary-string provenance preflight was explicitly answered by the author and fixed in this head; the exact-capacity vector regression and post-hash-commit Group regression close the reported gap. I found no remaining P0-P3 issues in the current diff.
XuPeng-SH
left a comment
There was a problem hiding this comment.
Deep re-review of exact head 46c79833f279c2dd18c096c02253a145bd412be0: the previous selected-batch binary-string allocation blocker is closed.
The new preflight rejects unsafe varlena aliases before publishing a proof, runs PreflightUnionBatchBinaryString before that proof, and leaves UnionBatchPreflighted with no late capacity allocation. This restores the Group invariant that allocation failure can occur before hash commit and still enter spill/retry.
Focused exact-head tests passed: TestSelectedBatchBinaryStringPreflightClosesExactCapacity, TestSelectedBatchPreflightProtocol, and TestGroupSelectedBinaryPreflightAllocatesNothingAfterHashCommit.
Unhappy-path audit: Q1 ownership remains with the destination vector and retained capacity is released by its existing Free path; Q2 no new waits or dependency edges are introduced; Q3 retained memory remains bounded by the allocation account and vector lifetime. I found no remaining P0-P3 issue in the current diff.
|
Retrospective linkage for #27450. The high-cardinality GROUP BY failure was caused by allocating a new group batch before the duplicate-only fast path returned at an exact hash-group chunk boundary. That left an empty group batch without corresponding aggregate state, and later output could address a non-existent aggregate chunk. This was fixed by commit Current-main verification is pinned to |
What type of PR is this?
Which issue(s) this PR fixes:
issue #25866
What this PR does / why we need it:
Adds one exact physical-allocation accounting path for query execution:
Validation summary:
make static-check, CGo-enabled build/vet/unit tests, owning-package race tests, and critical boundary races repeated 100 times passed;