Skip to content

Do not invalidate dictionary if there is only one of them [again] - #103662

Merged
Avogar merged 4 commits into
ClickHouse:masterfrom
bobrik:ivan/no-single-dict-reload-fixed
May 20, 2026
Merged

Avogar merged 4 commits into
ClickHouse:masterfrom
bobrik:ivan/no-single-dict-reload-fixed

Conversation

@bobrik

@bobrik bobrik commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

This is #99285 plus an extra commit that fixes non-adaptive parts.

Changelog category (leave one):

  • Performance Improvement

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

Faster discontinuous queries for LowCardinality columns with a single dictionary.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

@Avogar Avogar self-assigned this Apr 28, 2026
@Avogar Avogar added the can be tested Allows running workflows for external contributors label Apr 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c94b5be]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/check_report_messages.py FAIL IGNORED

AI Review

Summary

This PR improves LowCardinality dictionary reads for discontinuous access by detecting single-dictionary Wide parts more accurately (including non-adaptive parts without a final mark) and by eagerly sharing dictionary state from prefix deserialization. I reviewed the current diff and prior discussion; the previously flagged non-adaptive false-positive case is addressed in the current code, and I did not find remaining correctness, safety, or performance-contract violations that require changes.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Apr 28, 2026
@bobrik

bobrik commented Apr 28, 2026

Copy link
Copy Markdown
Contributor Author

I did validate that the test fails on #99285 but not prior to that:

--- /mnt/data/homes/ivan/projects/clickhouse/tests/queries/0_stateless/04063_low_cardinality_map_wrong_value_nonadaptive.reference      2026-04-28 05:34:57.605915666 +0000
+++ /mnt/data/homes/ivan/projects/clickhouse/tests/queries/0_stateless/04063_low_cardinality_map_wrong_value_nonadaptive.63363.stdout   2026-04-28 05:44:00.295380570 +0000
@@ -1,3 +1,3 @@
 2
 1      value_1
-10000  value_10000
+10000  value_1809

The dictionary size is 8192, so the math checks out for the bug (we look at the first dict instead of the second):

>>> 10000 - 8192
1808

I'm fairly confident in the fix, but I haven't validated it yet as my potato x86_64 system is still doing a full rebuild:

[3207/6216] Building CXX object src/CMakeFiles/dbms.dir/Functions/FunctionsConversion_impl06.cpp.o

@EmeraldShift

Copy link
Copy Markdown
Contributor

I'm curious, where can I read more about when having a single dictionary is better than having many? What's a good rule of thumb?

@bobrik

bobrik commented Apr 28, 2026

Copy link
Copy Markdown
Contributor Author

@EmeraldShift, there's a chain of links: #99285 -> #99236 -> #98968.

@bobrik

bobrik commented Apr 28, 2026

Copy link
Copy Markdown
Contributor Author

The new test succeeded, but the old amplification test timed out on a bunch of sanitizer builds. This was not the case in #99285. I'm not sure what happened there. It doesn't stand out all that much in regular tests:

image

Locally:

04143_low_cardinality_single_dict_read_amplification:                   [ OK ] 7.71 sec.

@bobrik
bobrik force-pushed the ivan/no-single-dict-reload-fixed branch from 47aa23e to f70115b Compare May 2, 2026 03:52
@bobrik
bobrik force-pushed the ivan/no-single-dict-reload-fixed branch from f70115b to a6c4ce0 Compare May 2, 2026 04:40
bobrik added 3 commits May 2, 2026 17:43
This reverts commit 49b0c95.

The fix for the discovered issue with non-adaptive parts is in the next commit.
Previously non-adaptive parts could've fooled the heuristic for single
dictionary detection, now they can't and there is a test to prove it.
…mplification

This makes the test faster, which matters on sanitizer targets that were too slow before.
@bobrik
bobrik force-pushed the ivan/no-single-dict-reload-fixed branch from 04b0b0a to b7aa1c3 Compare May 3, 2026 00:46
@bobrik

bobrik commented May 3, 2026

Copy link
Copy Markdown
Contributor Author

I switched from mark_type.adaptive to a more targeted getIndexGranularity().hasFinalMark()) and amended the test to make it run faster. The only failure was in Finish Workflow and it was due to cidb returning 500:

ERROR: Failed to insert data into CI DB, exception [Failed to write to CI DB, response code [500]] (from: Stress test (amd_debug))

Hopefully it will go away after the rebase.

@bobrik

bobrik commented May 13, 2026

Copy link
Copy Markdown
Contributor Author

@Avogar, could you have a look? Thanks!

@Avogar

Avogar commented May 15, 2026

Copy link
Copy Markdown
Member

Sure, I will take a look next week. Sorry for the delay, just came back from vacation recently

@clickhouse-gh

clickhouse-gh Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.50% +0.00%

Changed lines: 98.25% (56/57) | lost baseline coverage: 5 line(s) · Uncovered code

Full report · Diff report

@Avogar

Avogar commented May 20, 2026

Copy link
Copy Markdown
Member

Let's keep an eye on test 02156_async_insert_query_log during next days. I am still not 100% sure what exact bug caused this test failures.

@Avogar
Avogar added this pull request to the merge queue May 20, 2026
Merged via the queue into ClickHouse:master with commit cd05938 May 20, 2026
163 of 165 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 20, 2026
@bobrik

bobrik commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

Did it fail again? List of failures, the latest one.

#105375 is where it failed. Not sure if that's related to the changes in that PR.

@bobrik

bobrik commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

The branch in #105375 is from 2 days ago, so it doesn't include this PR.

@Avogar

Avogar commented May 21, 2026

Copy link
Copy Markdown
Member

It's suspicious... But let's wait for a few more days

@bobrik

bobrik commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

Let's revert. I'll see if I can figure out what's missing here.

DavidHe-2008 pushed a commit to DavidHe-2008/ClickHouse that referenced this pull request Jun 1, 2026
…reload-fixed

Do not invalidate dictionary if there is only one of them [again]
UnamedRus pushed a commit to UnamedRus/ClickHouse that referenced this pull request Aug 25, 2026
The precomputed distinct-mark count makes the single-dictionary check O(1),
but nothing observed that the fast path was reached. Deleting it left every
test green: the boolean is bit-identical, so the existing SQL tests cannot
tell the memoized answer from the scanned one, and the gtest exercises
MarksInCompressedFile without going through the reader. A later change to
num_columns_in_mark or marks_count bookkeeping could therefore silently
falsify the guard, restore the whole-part scan, and pass CI. This code path
has already been reverted twice, so that is not a hypothetical cost.

Count the fast path with a ProfileEvent and assert a non-zero delta from a
Wide one-column LowCardinality read. Verified by deleting the fast-path
block: the new test fails (1 vs 0) while 04143, 04201, 04144, 04614, 04141,
04142 and the gtest all stay green. That asymmetry is the point, since
04143 can only see the optimization stop firing, not the memoization.

The event is named for the mechanism rather than for LowCardinality because
SerializationObject consumes the same callback for JSON subcolumns.

Also run the two version-only-DictionaryKeys tests ClickHouse#103662 added alongside
04143, and drop a dead array copy in the gtest (create takes a const ref).
robinld pushed a commit to robinld/ClickHouse that referenced this pull request Oct 4, 2026
…subcolumn

`SerializationLowCardinality::deserializeBinaryBulkStatePrefix` asks the
Wide reader's `has_uniform_marks_callback` whether the part may have a single
dictionary. The callback looked the stream up with `streams.find()` and
answered `false` when the reader had not opened it yet. Map bucket and Dynamic
variant streams are opened lazily by the prefix getter, which runs after the
callback, so the answer depended on which column sharing the stream was
deserialized first: `m.keys` was treated as multi-dictionary, `m` as single
dictionary, and `m`'s prefix read the shared dictionary stream to EOF.
Mutations read without seeking to marks, so the data read of `m.keys` then
called `read_dictionary()` on the EOF stream and failed with
`CANNOT_READ_ALL_DATA` on every attempt.

Open the stream in the callback, as the neighbouring callbacks already do.

Regression from ClickHouse#103662 (first released in 26.6).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-performance Pull request with some performance improvements 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