Skip to content

Fix max_bytes_in_distinct and max_bytes_in_set ignoring the bytes of string keys - #110215

Closed
nihalzp wants to merge 6 commits into
ClickHouse:masterfrom
nihalzp:fix-set-byte-accounting-string-pool
Closed

nihalzp wants to merge 6 commits into
ClickHouse:masterfrom
nihalzp:fix-set-byte-accounting-string-pool

Conversation

@nihalzp

@nihalzp nihalzp commented Jul 13, 2026

Copy link
Copy Markdown
Member

SizeLimits::check for DISTINCT and IN sets was called with getTotalByteCount, which returned only the hash table buffer size and ignored the string_pool arena where the actual bytes of String / FixedString keys are stored. For string keys the limit therefore compared ~16 bytes per key against the budget while the real memory grew unbounded.

-- 10 KB string keys under a 1 MB byte budget: DISTINCT is expected to stop around 1 MB.
-- Before the fix the limit never tripped (the hash table buffer for 2000 keys is only
-- ~130 KB) and all 19.07 MiB came out; after the fix it stops at 976 KiB (100 keys).
SELECT count(), formatReadableSize(sum(length(s)))
FROM (SELECT DISTINCT randomPrintableASCII(10000) AS s FROM numbers(2000))
SETTINGS max_threads = 1, max_block_size = 100,
         distinct_overflow_mode = 'break', max_bytes_in_distinct = 1000000;

-- The same undercount in throw mode: SET_SIZE_LIMIT_EXCEEDED is expected once the keys
-- exceed 1 MB, but before the fix the query succeeded and returned 2000.
SELECT count()
FROM (SELECT DISTINCT randomPrintableASCII(10000) AS s FROM numbers(2000))
SETTINGS max_block_size = 100, distinct_overflow_mode = 'throw', max_bytes_in_distinct = 1000000;

-- Same for IN sets (default set_overflow_mode = 'throw'): SET_SIZE_LIMIT_EXCEEDED is
-- expected while building the ~19 MB set, but before the fix the set was built in full
-- and the query succeeded.
SELECT count() FROM numbers(10)
WHERE toString(number) IN (SELECT randomPrintableASCII(10000) FROM numbers(2000))
SETTINGS max_block_size = 100, max_bytes_in_set = 1000000;

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 not counting the bytes of String and FixedString keys. The keys themselves are stored in the set's string pool, but only the hash table buffer (about 16 bytes per key) was checked against the limit, so for string keys the limits were exceeded by an unbounded factor before tripping. Queries running close to these limits on string keys now stop or throw where they previously did not.

@clickhouse-gh

clickhouse-gh Bot commented Jul 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f34341a]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_asan_ubsan, flaky check) FAIL
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_tsan, flaky check) FAIL
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_msan, flaky check) FAIL
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_debug, flaky check) FAIL
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
00753_system_columns_and_system_tables_long FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_asan_ubsan, distributed plan, parallel) FAIL
00753_system_columns_and_system_tables_long FAIL cidb
Stateless tests (amd_debug, parallel) FAIL
00753_system_columns_and_system_tables_long FAIL cidb
Stateless tests (amd_tsan, parallel) FAIL
00753_system_columns_and_system_tables_long FAIL cidb
Stateless tests (amd_msan, WasmEdge, parallel, 2/2) FAIL
00753_system_columns_and_system_tables_long FAIL cidb
Stateless tests (arm_binary, parallel) FAIL
00753_system_columns_and_system_tables_long FAIL cidb
Stateless tests (arm_asan_ubsan, azure, parallel) FAIL
02805_distributed_queries_timeouts FAIL cidb

AI Review

Summary

This PR fixes SetVariants::getTotalByteCount so DISTINCT and IN byte limits include string_pool allocations for single String and FixedString keys, and it adds focused stateless coverage for the affected paths. After reviewing the current head diff and the prior PR discussion, I did not find any remaining blockers or majors. Residual risk is limited to this review being code-inspection-only on my side; I did not rerun the stateless queries locally, but the PR CI for commit f34341a1daeac43163f56f8712e96181bcf4f7ac is green.

Final Verdict

✅ No blocking findings.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 13, 2026
@azat azat self-assigned this Jul 13, 2026
-- which depends on internal allocation sizes.
CREATE TABLE check_system_tables Engine=Set() AS SELECT * FROM numbers(50);
SELECT total_bytes, total_rows FROM system.tables WHERE name = 'check_system_tables' AND database = currentDatabase();
SELECT total_bytes BETWEEN 2048 AND 15000, total_rows FROM system.tables WHERE name = 'check_system_tables' AND database = currentDatabase();

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.

Hm, why it is not deterministic?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is deterministic. This pattern is copied from the JOIN test below:

CREATE TABLE check_system_tables Engine=Join(ANY, LEFT, number) AS SELECT * FROM numbers(50);
SELECT total_bytes BETWEEN 4000 AND 15000, total_rows FROM system.tables WHERE name = 'check_system_tables' AND database = currentDatabase();
INSERT INTO check_system_tables SELECT number+50 FROM numbers(50);
SELECT total_bytes BETWEEN 4000 AND 15000, total_rows FROM system.tables WHERE name = 'check_system_tables' AND database = currentDatabase();

This is written this way to emphasis the fact that total_bytes is more than 2048 (which is exactly the bytes used by the HashTable). If you prefer the version without BETWEEN 2048 AND 15000, I can simplify it.

@azat azat Jul 13, 2026 •

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.

Yes, let's just match the "total_bytes" here (w/o BETWEEN). If it is deterministic it looks better.

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.

The BETWEEN that already exists in the test was introduced in 05784b6 (9e0645c), it does not contains something sensible in the commit, but I'm pretty sure it should be deterministic as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay, I will change both.

@clickhouse-gh

clickhouse-gh Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.80% 85.90% +0.10%
Functions 92.70% 92.70% +0.00%
Branches 78.00% 78.00% +0.00%

Changed lines: Changed C/C++ lines covered: 6/7 (85.71%) · Uncovered code

Full report · Diff report

@nihalzp

nihalzp commented Jul 15, 2026

Copy link
Copy Markdown
Member Author

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants