Skip to content

Fix wide key-only broadcast joins and reset pending matches - #33250

Merged
XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
LeftHandCold:fix/33246-broadcast-join-keys-reset
Nov 25, 2026
Merged

XuPeng-SH merged 3 commits into
matrixorigin:mainfrom
LeftHandCold:fix/33246-broadcast-join-keys-reset

Conversation

@LeftHandCold

Copy link
Copy Markdown
Collaborator

What type of PR is this?

  • BUG

Which issue(s) does this PR fix?

Fixes #33246

Follow-up to #32917. The 4.2-dev backport is #33242.

What changed?

Broadcast HashBuild stages input while constructing the map even when the probe needs only keys. Apply the existing byte limit to those temporary batches. Build the ordinal-to-batch directory only for consumers that retain payload. Keep shuffle recovery and dedup rewrite batching on their current paths.

Discard the probe's remaining matches when releasing the JoinMap that owns them. This also covers an upstream LIMIT ending a probe before it drains the current group.

Validation

  • Both new regression cases failed before the fix. The wide-key build exceeded the allocator limit, and resetting after 11 of 13 wide matches retained two pending selections. Both pass with the fix.
  • Single CN over MySQL, default per-allocation limit and an 8 GiB query budget. The build side had 8,192 keys of 262,150 bytes each. The client checked every source ID and every key byte on both tables. The key-only LEFT JOIN returned the expected count of 24,556 and the build-column control returned the expected sum of 33,558,528.
  • Executed one prepared LEFT JOIN four times on the same connection, alternating a one-row LIMIT over duplicate matches and a single matching build key. Both full executions returned 16,384 distinct probe rows with the correct nine matches and NULL-extended rows.
  • Eight related packages passed with vet. Three repetitions of the focused race tests passed. The MySQL hard-budget regression passed and the incremental linter reported zero issues.

@qodo-code-review

Copy link
Copy Markdown

Qodo reviews are paused for this user.

Troubleshooting steps vary by plan Learn more →

On a Teams plan?
Reviews resume once this user has a paid seat and their Git account is linked in Qodo.
Link Git account →

Using GitHub Enterprise Server, GitLab Self-Managed, or Bitbucket Data Center?
These require an Enterprise plan - Contact us
Contact us →

@mergify

mergify Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Queue requested — the merge queue status continues in this comment ↓.

@XuPeng-SH

XuPeng-SH commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 1cff71ff0e74d537d6ce22b95bd6dbff52e1ebe1

Review completed. Read the assessment.

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 1cff71ff0e74d537d6ce22b95bd6dbff52e1ebe1

Assessment: request_changes

The change reuses the existing bounded batch writer and clears selections with their owning join map. However, enabling that writer for key-only broadcast builds introduces a substantial regression for reordered string batches.

Model: gpt-6.1-terra / max

Session: 01a211d7-e427-70e2-a993-c62f1960cb9e · slot 3 · generation 3

  • P2 pkg/sql/colexec/hashbuild/build.go:87: A key-only broadcast hash build receives a reordered, non-inline string vector, such as output from an Order or Top operator.
    Impact: The newly enabled bounded-copy path sorts the entire source vector for every 8,192-row copied range. In a 131,072-row test with 24-byte keys, hash-build time increased from approximately 9 ms to 204–209 ms, and Go heap allocation increased from about 49 KB to 34 MB per build. Larger sorted batches repeat this work across more ranges.
    Evidence: The changed flag routes key-only builds through CopyIntoBoundedBatches; join_util.go:272 and :323 call VarlenaAreaIsDisjoint for each range, which sorts the full vector at vector.go:934 and :983. The same-binary comparison disabled only this flag for the baseline, matching the merge-base behavior for NeedBatches=false; both variants found every key. See key-only-large-benchmark.log, key-only-lookup-v2-test.log and their receipts. A review-only selected-range check reduced the candidate to about 10 ms and 53 KB; see key-only-large-linear-benchmark.log. Avoid repeatedly sorting the full vector, while preserving the allocation limit.

Validation:

  • Private /tmp and loopback preflight passed; native library input trees and checksums matched the assessed source.

  • Focused hash-build and hash-join tests passed. Each changed suite also passed 100 focused race repetitions; see hashbuild-race100.jsonl and hashjoin-race100.jsonl.

  • A reviewer-only single-CN MySQL test passed key-only count and build-payload controls, plus repeated prepared queries alternating an early LIMIT with a full result; see public-sql-v3-test.log.

  • The reordered-key benchmark ran three repetitions per variant, with integer and ordered-string controls. A separate probe verified every key in both benchmark variants.

Limitations:

  • The combined owning-package run passed hashbuild but timed out after 300 seconds while the existing hashjoin spill matrix was still progressing; the full hashjoin package is not counted as a pass.

  • A separate merge-base source-overlay benchmark timed out during compilation. The reported comparison instead reproduces the merge-base flag condition in the same binary; whole-query performance was not measured.

  • The PR’s reported 2 GiB SQL workload was not independently rerun. The focused allocation-boundary test uses a reduced limit, and the public SQL control uses smaller data.

  • Initial reviewer-only probes had fixture and oracle errors; the corrected TCP-only SQL and unique-map lookup probes passed.

@LeftHandCold

Copy link
Copy Markdown
Collaborator Author

Addressed the reordered-string HashBuild regression from the review of 1cff71f in 29c4e08.

The bounded writer previously checked every descriptor in the input vector for each 8,192-row copy. Reordered strings caused that check to allocate and sort the full set of ranges every time. The writer now uses a constant-time upper bound when sufficient and otherwise sums external payload bytes only for the rows it will copy. Repeated references are counted once per copy. The allocation limit is unchanged.

I compared the key-only broadcast HashBuild with the pre-PR build path, the reviewed head, and the fix. Each input had 131,072 distinct keys in one batch. These are medians of four benchmark samples per variant, five builds per sample, on the same machine. All 131,072 keys resolved to their expected groups after every build.

Input Pre-PR build path Reviewed head 29c4e08
Reordered 24-byte strings 10.17 ms 291.34 ms 10.99 ms
Ordered 24-byte strings 8.00 ms 14.06 ms 9.54 ms
INT64 2.01 ms 1.90 ms 2.04 ms

For reordered keys, median Go heap allocation fell from 34,115,308 to 27,713 bytes per build. These timings cover the HashBuild operator. The pre-PR control uses the merge-base build path with the same benchmark harness.

  • The new regression test fails on the reviewed head and passes on the fix. It checks range sizing without heap allocation, including an inline key, a NULL and two references to the same 120-byte value. Budgets of 200 and 240 bytes must accept three and four rows respectively.
  • The complete colexec, hashbuild and hashjoin packages passed, including the spill cases. Focused race tests passed three times, and the incremental linter reported zero issues.
  • Through single-CN MySQL, the build input contained 8,192 keys of 262,150 bytes each, crossing 2 GiB in total. Both source tables passed a byte-for-byte readback. The left join returned the expected count of 24,556 and build-ID sum of 33,558,528. Four alternating executions of the prepared LIMIT/full-result query also returned the expected counts.

@XuPeng-SH

XuPeng-SH commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

mo-bot review · mo-bot-local · done

Commit: 29c4e08464b5fc17ae96a34ca9de3620e0597fa2

Review completed. Read the assessment.

XuPeng-SH pushed a commit that referenced this pull request Nov 25, 2026
## What type of PR is this?

- [x] BUG

## Which issue(s) does this PR fix?

Fixes #32143

Backport of #32917 to `4.2-dev`.

## What changed?

A broadcast hash join could have enough query memory and still fail when
the retained values in one variable-length column crossed the MPool
single-allocation limit. Packing 8,192 rows into one batch was
sufficient to trigger it. The same limit matters when projecting
duplicate matches into the output.

- Split retained build batches by row count and physical bytes per
column. Create an allocation-accounted row directory only when an
interior batch is short. Hashing, probing, and right-join finalization
resolve logical ordinals against those batches.
- Bound output for the selected rows and resume partially emitted match
groups. Preserve 4.2's import-memory accounting across split batches and
its hard query budget.
- Port the broadcast path available on 4.2. The patch does not require a
catalog migration, a new RPC, or a configuration change.

## Validation

- On unpatched target commit `d2bc81d9f0`, the scaled MySQL reproducer
returned error 20101 for 8,192 values of 4,100 bytes with a 512 MiB
query budget (33,585,150-byte allocation limit, 33,587,200 bytes
required). Both controls passed. The backport passed all three cases
with the same fixture.
- Single CN over the MySQL protocol, default per-allocation limit
(2,147,483,631 bytes), 8 GiB query budget. A LEFT JOIN with 8,192 build
values of 262,150 bytes each returned all 8,192 matches and all 16,383
NULL-extended rows. The client checked every ID for duplicates and
compared each complete value. The 8,191-row boundary control and the
three-column control also passed.
- Operator tests cover duplicate and unique keys, residual predicates,
RIGHT JOIN finalization, an unmatched wide value, import-memory output
continuation, and budget failure and release. All nine related packages
passed. Focused race tests in three packages passed three times. The SQL
hard-budget test checked the error and continued on the same connection.
- A local single-CN narrow-column comparison on `ef9dac896b` used 65,536
probe rows with 64-byte build values. Across two runs per version and 16
timed queries per case, median latency was 8.45 → 8.66 ms for unique
keys and 13.07 → 13.44 ms for duplicate keys. All counts and checksums
matched. This measures those two local workloads.


## Follow-up for #33246

This branch includes the two broadcast JOIN fixes from the main
counterpart #33250. Key-only builds now use bounded temporary batches. A
probe that stops within a duplicate group drops its pending matches when
it releases the owning join map.

- Both new regression cases failed on the previous PR head and passed on
this head. On both branches, a single CN using the MySQL protocol with
the default allocation limit returned the expected count of 24,556 and
control sum of 33,558,528. The client independently checked every source
ID and key byte.
- Four alternating executions of one prepared LEFT JOIN on one
connection returned the expected rows and NULLs. The hard query-budget
error and connection reuse also passed. Eight related packages passed
with vet, focused race tests passed three times, and the incremental
linter reported zero issues.
- A local ABBA comparison against the previous PR head 5ad4415 used
65,536 probe rows and 8,192 build rows, with single-CN DOP 1. Each query
returned the expected 131,072 matches and checksum. Across 16 timed
queries per version and key type, median latency for integer keys moved
from 5.875 to 5.998 ms (+2.10%) and for 64-byte string keys from 8.351
to 8.671 ms (+3.84%). These timings cover this workload only.

---------

Co-authored-by: lin <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>

@XuPeng-SH XuPeng-SH left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mo-bot review · mo-bot-local · review

Commit: 29c4e08464b5fc17ae96a34ca9de3620e0597fa2

Assessment: approve

The fix reuses the bounded batch writer for key-only builds and clears pending selections with their owning JoinMap. The prior repeated-sorting regression is resolved, and no actionable defects remain.

Model: gpt-6.1-terra / max

Session: 01a21244-6c6c-7240-bf19-37c0cdedc87d · slot 1 · generation 15

Validation:

  • Preflight passed. Nine focused tests passed across colexec, hashbuild, hashjoin and rightdedupjoin; the three changed regression tests each passed three times under race.

  • Review probes passed for ordered and overlapping varlen copies, and for duplicate, missing and NULL key lookups across reduced-limit build batches, with retained payload as a control.

  • A single-CN MySQL fixture passed the key-only count, build-payload sum, and repeated prepared LIMIT/full-result checks on this head.

  • The prior same-PR benchmark used production logic matching this head apart from comments: the reordered-key candidate measured about 10.3 ms per build, versus 8.9–9.2 ms for the pre-PR key-only path. Receipts and logs were reconciled in final-audit.log.

Limitations:

  • The author-reported 2 GiB SQL workload was not independently rerun; the allocation-boundary checks used a reduced limit.

  • The retained benchmark measures the HashBuild component and does not establish whole-query performance.

@mergify mergify Bot added the queued label Nov 25, 2026
@mergify

mergify Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

🚫 Left the queue · pull request merged manually

1 h 4 min in the queue, with no time running CI.

So far

  • ✅ Entered the queue Wed 25 Nov, 14:54 · rule main · triggered by rule Automatic queue on approval for main
  • ⏹️ Testing stopped · in place
  • 🚫 Left the queue 15:58 · e81e89a

Reason

Pull request #33250 has been merged manually at 2b0038f

Tick the box to put this pull request back in the merge queue (same as @mergifyio queue).

  • Requeue this pull request

Times in UTC · dashboard · how the merge queue works

This branch was previously deployed

1 inactive deployment
ci — e81e89a5 Deployed Nov 25, 2026 by mergify[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Something isn't working size/S Denotes a PR that changes [10,99] lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Broadcast hash join fails on wide key-only builds and retains pending matches after reset

3 participants