Repository navigation
Faster ORDER BY ... COLLATE: compare UTF-8 directly in ICU - #126829
Conversation
`Collator::compare` wrapped both strings in `UCharIterator`s (`uiter_setUTF8`) and called `ucol_strcollIter`. That path (`RuleBasedCollator::compare(UCharIterator &, UCharIterator &)`, contrib/icu/icu4c/source/i18n/rulebasedcollator.cpp) reads every code unit through an iterator function pointer and has no `CollationFastLatin` path. `ucol_strcollUTF8` (`internalCompareUTF8` -> `doCompare(const uint8_t *, ...)`) skips the common prefix bytewise, tries `CollationFastLatin::compareUTF8`, and otherwise iterates UTF-8 directly with `(FCD)UTF8CollationIterator`. The order is unchanged. Both entry points decode ill-formed UTF-8 with the same maximal-subpart U+FFFD policy (`utf8IteratorNext` in common/uiter.cpp, `UTF8CollationIterator::handleNextCE32`, and `UTF8NFDIterator` for the identical level all use `U8_NEXT_OR_FFFD`), and with explicit lengths both treat a zero byte as U+0000. ICU's own collationtest.cpp checks the UTF-16, UTF-8 and UCharIterator APIs against each other. `internalCompareUTF8` reads a negative length as NUL-terminated (`uprv_strlen`), whereas `uiter_setUTF8` turned a length below -1 into an empty iterator. `ColumnString` values are not NUL-terminated, so passing `static_cast<int32_t>(length)` for a string longer than `INT32_MAX` bytes would read past the value. All ICU collation lengths are `int32_t`, so such a string cannot be compared correctly at all; throw `TOO_LARGE_STRING_SIZE` instead. Previously it was compared with a wrapped-around length (a 3,072,000,000-byte string sorted before 'b'). Alternatives not taken: keeping the old iterator path for long strings (it only reproduces the wrong order), and per-row sort keys (`ucol_getSortKey`), which would change the sorting design rather than the comparison. Related: ClickHouse#126735 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Checks that `Collator::compare` rejects a length above `INT32_MAX` for either operand with `TOO_LARGE_STRING_SIZE`; SQL cannot reach a value of 2 GiB or more cheaply. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 distinct findings over 4 runs; the last run, on the final head, found nothing).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261019-223300 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261019-194800 |
|
Workflow [PR], commit [a8f1daf] Summary: ✅
AI ReviewSummaryThe PR switches Missing context / blind spots
Final VerdictStatus: ✅ Approve. No source changes requested; pending CI results remain to be checked before merging. |
|
cc @thevar1able @azat, could you review this and add the |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 6 s compile time in total, 7 of them have a recent master baseline. |
abd790a
The collation sort (#2, 5M rows) and the single-column string sort (ClickHouse#3, 20M rows) take 1.2-1.9 s on MasterCI on both arches. They get disabled="1" (text and number unchanged) and are appended as ClickHouse#9 and ClickHouse#10 on a quarter of the rows with max_block_size = 16384, a quarter of the default 65409. `numbers_mt` hands out blocks of exactly max_block_size rows, so each copy has as many blocks as its original (77 and 306): the same number of cursors in every per-stream merge and the same 16-stream final merge, with a quarter of the rows per block. EXPLAIN PIPELINE of each original and its copy is identical (16 streams). Measured on official MasterCI release builds, two servers pinned to 24 cores of one NUMA node, perf.py, 25 runs: copy / original time 0.248 (#2) and 0.252 (ClickHouse#3), projected CI medians 380 / 487 ms amd, 298 / 483 ms arm. On the builds before/after ClickHouse#115869 (sorted array of cursors) #2 is 1.161x faster and its copy 1.159x; on those before/after ClickHouse#126829 (cheaper collation) 2.179x and 2.111x; ClickHouse#3 and its copy show no change on either pair (1.005 / 1.008, 1.004 / 1.001), as the heap guard should. #0 and #1 stay as they are: on this rig the ClickHouse#115869 change did not show on them beyond the A/A resolution, so there was no measured guarantee that copies would keep their sensitivity. About 32 s (amd) and 29 s (arm) of query time less per master performance job; 9 queries stay enabled, so all of them still run. Related: ClickHouse#129258 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Related: #126735
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ORDER BY ... COLLATEis up to 3 times faster: strings are now compared with ICU's UTF-8 collation API instead of a character iterator. Comparing a string of 2 GiB or more under a collation now throwsTOO_LARGE_STRING_SIZEinstead of returning a wrong order.Description
Collator::compare, used by every collated string comparison, wrapped both strings inUCharIterators and calleducol_strcollIter: an indirect call per code unit and no fast path.ucol_strcollUTF8compares the common prefix bytewise, uses ICU's Latin fast path and otherwise iterates UTF-8 directly.The order does not change: both APIs decode each maximal ill-formed UTF-8 subsequence as U+FFFD and keep embedded zero bytes. Sorting 150k generated strings (48% ill-formed) under all 145
system.collationslocales and 10 modifier variants gives the same order on master and with this PR. The new SQL test pins the order of valid and ill-formed strings under 12 collations (reference generated on master).ucol_strcollUTF8reads a negative length as a NUL-terminated string, so a string of 2 GiB or more (ICU lengths areint32_t) is now rejected withTOO_LARGE_STRING_SIZE; a unit test covers this, as SQL cannot build such a string cheaply. Before, it was compared with a wrapped-around length: on master,ORDER BY s COLLATE 'en'puts a 3,072,000,000-byte string before'b'.Release build, 20M rows,
max_threads = 16, medians of 11 rotated rounds (two master servers agreed within 12%):ORDER BY s COLLATE 'en' LIMIT 10, ASCII'ru''zh''en'kway_merge_expensive_comparator.xmlquery 3ORDER BY s LIMIT 10, noCOLLATE(60 runs)Non-Latin text and numeric collation (
'en-u-kn-true': 950 to 820 ms) gain less: they miss the Latin fast path. The existing perf testskway_merge_expensive_comparator.xmlandcollations.xmlcover both.Workflow [PR]
Sync PR [sync-upstream/pr/126829]
Version info
26.11.1.318-master(included in26.11and later)