Skip to content

Fix max_bytes_in_distinct / max_bytes_in_set: count string keys stored in the arena - #110120

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-set-bytes-string-pool
Jul 15, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-set-bytes-string-pool

Conversation

@skuznetsov-clickhouse

@skuznetsov-clickhouse skuznetsov-clickhouse commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix max_bytes_in_distinct and max_bytes_in_set: string keys are stored in an arena that was not counted by the limit checks, so DISTINCT / IN over string keys could hold memory exceeding the byte limit by the whole key payload (unbounded in the key length). The limits now account for the arena, matching their documentation; queries with string keys close to a byte limit may now trip it earlier (correctly).

Documentation entry for user-facing changes

The fixed behavior matches the existing documentation: max_bytes_in_set — "The maximum number of bytes (of uncompressed data) used by a set in the IN clause"; the getTotalByteCount() declaration comment also documents that the string pool must be counted. No docs changes needed.


Symptom. SetVariantsTemplate::getTotalByteCount() returns only the hash-table buffer size (data.getBufferSizeInBytes()). For string keys, every consumer — DistinctTransform, DistinctSortedTransform, DistinctSortedStreamTransform, and Set (IN) — copies the key payload into the string_pool arena, which was not counted. The byte limits only tripped once the ~24-bytes-per-key hash buffer itself crossed the limit:

-- 1 KB string keys, 10 MB limit, break: retained 139264 rows ≈ 139 MB of key payload
SELECT count() FROM (SELECT DISTINCT toString(number) || repeat('x', 1000) AS s FROM numbers(200000))
SETTINGS max_bytes_in_distinct = 10000000, distinct_overflow_mode = 'break', max_threads = 1, max_block_size = 8192;

-- 100000 distinct 1 KB keys = ~100 MB of state, 10 MB limit: no exception at all (buffer is ~6 MB)
SELECT count() FROM (SELECT DISTINCT toString(number) || repeat('x', 1000) AS s FROM numbers(100000))
SETTINGS max_bytes_in_distinct = 10000000, distinct_overflow_mode = 'throw', max_threads = 1, max_block_size = 8192;

-- Same for the IN set: ~100 MB set, 10 MB limit, throw: passes silently
SELECT count() FROM numbers(10) WHERE toString(number) IN (SELECT toString(number) || repeat('x', 1000) FROM numbers(100000))
SETTINGS max_bytes_in_set = 10000000, set_overflow_mode = 'throw', max_threads = 1;

Found while working on #110075 (an orthogonal defect: that PR fixes what happens when a DISTINCT limit trips; this PR fixes the byte limit not tripping for string keys). Surfaced by the AI review there.

Root cause. The declaration comment on getTotalByteCount() documents the intended contract — "Counts the size in bytes of the Set buffer and the size of the string_pool" — but the implementation never included the pool.

Fix. getTotalByteCount() now adds string_pool.allocatedBytes(), following the established pattern of HashJoin::getTotalByteCount(), which includes data->pool.allocatedBytes() for max_bytes_in_join. Fixed-size keys are unaffected (the arena stays at its initial allocation and is dwarfed by the hash buffer).

Behavior change. Queries with string keys running close to max_bytes_in_distinct / max_bytes_in_set will now trip the limit earlier — at the documented boundary instead of an under-counted one. With the repro above: break now stops at 8192 rows instead of 139264; both throw cases now raise SET_SIZE_LIMIT_EXCEEDED with honest byte counts (17.48 MiB / 69.98 MiB vs the 9.54 MiB limit).

Verification.

  • New stateless test 04512_max_bytes_limits_count_string_arena: DISTINCT throw/break and IN throw over ~1 KB string keys where the hash buffer alone stays under the limit — fails without the fix (nothing trips), passes with it; plus a fixed-size-key control query pinning that numeric DISTINCT under the same limit is unaffected.
  • Existing limit/overflow-mode stateless tests pass on the fixed build (01134_set_overflow_mode, 02382/02383_join_and_filtering_set, 02494_query_cache_bugs, 02581_share_big_sets_between_mutation_tasks, 02841/02861/02907 filter-pushdown, 03359, 03532, 04007, 04201; 04204 needs a CI cluster, 02383's sed scripting is GNU-only — both environment, covered by CI here).
  • Falsified locally: with the one-line accounting change reverted, the new test fails (no limit trips) and the repro numbers revert to 139264 / no-throw / no-throw.

Version info

  • Merged into: 26.7.1.974 (included in 26.7 and later)

…d in the arena

Symptom: with string keys, the byte limits only tripped once the
~24-bytes-per-key hash buffer crossed the limit, because
SetVariantsTemplate::getTotalByteCount() returned only the buffer size
while the key payload lives in the string_pool arena. DISTINCT / IN
could hold memory exceeding the limit by the whole payload, unbounded
in the key length (measured: 139 MB retained under a 10 MB 'break'
limit; a ~100 MB state under 'throw' never raised).

Root cause: the declaration comment documents that the string pool must
be counted, but the implementation never included it.

Fix: add string_pool.allocatedBytes(), following the
HashJoin::getTotalByteCount() pattern used for max_bytes_in_join.
Fixed-size keys are unaffected.

Verification: new stateless test fails without the fix (nothing trips)
and passes with it, 5/5 runs; existing overflow-mode tests pass.
Remaining risk: queries with string keys near a byte limit now trip it
earlier (at the documented boundary) - intended behavior change.
@clickhouse-gh

clickhouse-gh Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c905bc3]

Summary: ✅


AI Review

Summary

This PR fixes the undercount in SetVariantsTemplate::getTotalByteCount by adding the string_pool arena to the tracked bytes, which is the missing piece behind max_bytes_in_distinct / max_bytes_in_set silently allowing oversized string-key states. The change is localized to the shared accounting helper, the new stateless test covers DISTINCT in both throw and break modes plus IN with string keys, and I did not find a correctness or compatibility issue in the current diff.

Final Verdict

✅ No blocking issues found.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 11, 2026
@yakov-olkhovskiy yakov-olkhovskiy self-assigned this Jul 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.80% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.90% 77.90% +0.00%

Changed lines: Changed C/C++ lines covered: 7/8 (87.50%) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perfect. LGTM.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 15, 2026
Merged via the queue into master with commit 3e95f6b Jul 15, 2026
176 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-set-bytes-string-pool branch July 15, 2026 02:06
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 15, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ring-pool

Fix max_bytes_in_distinct / max_bytes_in_set: count string keys stored in the arena
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants