Repository navigation
Conversation
countBytesInFilter and countBytesInFilterWithNull reduce a filter 64 bytes at a time with a bitmask and popcount, then count the residue byte by byte. Both wide loops, and the private toBits64 helper they call, were guarded by a bare '#if defined(__SSE2__)'. __SSE2__ is an x86-64 baseline feature, so this was never runtime dispatch: x86 always compiled the wide loop and AArch64 never did, counting the whole filter one byte per iteration. Widen both guards to also admit __aarch64__ && __ARM_NEON, and add an #elif arm defining toBits64 via bytes64MaskToBits64Mask from this module's header, which already has a NEON tier. No feature detection is needed because NEON is unconditional at ClickHouse's AArch64 baseline, verified on both the modern armv8.2-a profile and the NO_ARMV81_OR_HIGHER compat profile. This is not a new ARM SIMD surface either: ColumnsCommon.cpp already calls that helper unconditionally in filterArraysImplGeneric and filterArraysImplInPlace. x86 is unaffected by construction, since __SSE2__ is defined at all four x86 levels and the new #elif is therefore unreachable. Measured: compiling the file before and after at -march=x86-64-v3 and at -march=x86-64 yields a byte-identical preprocessed translation unit, identical disassembly, and a byte-identical object once DWARF line data is excluded. A miscount would silently change result cardinality, so equivalence was tested exhaustively rather than sampled: lengths 0..300 crossed with start offsets 0..7 and 7 byte patterns, the block boundary lengths, and randomised 1 MiB buffers, for both functions, cross-compiled and run under qemu-aarch64-static against the pre-change scalar-only build as oracle. Zero mismatches on both AArch64 profiles under gcc-13 and clang-22, and a digest over the results is identical on AArch64 and x86-64, so the wide path returns the same answers on both. Dropping a single NEON instruction reddens every check, so those are measurements rather than blind comparisons. The new stateless test pins block boundary lengths through max_block_size and uses partial selectivity, because an all-pass filter cannot distinguish a correct block mask from a broken one. It cannot fail on x86, where behaviour is unchanged; its purpose is to exercise the new path in the AArch64 CI jobs. It was checked in both directions against a build with a deliberately corrupted mask. Closes ClickHouse#116073
Internal second-model reviewAn independent model reviewed this change cold. Verdicts below; every disagreement is backed by a ❌
💡 Add a 💡 Add a path-discriminating oracle to the stateless test. Declined: the change is |
Pre-PR validation gate
50 randomized runs of the new test at |
|
Workflow [PR], commit [8088404] Summary: ❌
AI ReviewSummaryThis PR enables the existing 64-byte mask-plus- Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 19/19 (100.00%) · Uncovered code |
|
cc @azat @Avogar, could you review this? The 64-byte bitmask and popcount loops in countBytesInFilter and countBytesInFilterWithNull sat behind a bare |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=22 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
countBytesInFilterWithNull reduces 64 bytes at a time with a bitmask that sets bit i iff filter byte i is non-zero, then counts mask & ~null_mask, i.e. filter != 0 && null == 0. Its byte-at-a-time tail instead counted (*pos & ~*pos2) != 0. Those differ on a NULL row whose filter byte keeps a set bit outside the null byte. An -If condition column is forwarded as a raw UInt8, so such bytes are reachable: countIfOrNull(if(number % 2 = 0, NULL, 1), toUInt8(2)) over 100 rows returned 68 or 100 depending on where the 64-byte boundary fell, against a true answer of 50. Of the 65536 (filter, null) byte pairs the tail disagreed with the wide loop on 58720, always by over-counting, and never on a pair where the wide loop is wrong. The tail now uses the wide loop's predicate, so the result no longer depends on block size. This is the correction 0311320 made to the > 0 comparisons in this file; countBytesInFilter's tail already used != 0. Plain countIf over a Nullable argument is unaffected: AggregateFunctionIfNullUnary folds the null map into the condition and reaches countBytesInFilter instead. The two NOTE blocks were stale in every clause: they described a > 0 comparison neither function has used since 0311320, and claimed != 0 precludes SSE2 when the SSE2 loop that commit introduced implements exactly != 0. Reported by the AI review on ClickHouse#116173.
|
Adopted the second suggested fix in b250f97: the One correction to the finding, which does not change the outcome. The trace is not introduced by Verified in both directions on x86 at equal This is the same correction |
| @@ -0,0 +1,54 @@ | |||
| -- A filter is counted 64 bytes at a time with a byte-at-a-time tail, so a count must not | |||
There was a problem hiding this comment.
@groeneai Remove this test. The functionality is already covered by existing tests.
There was a problem hiding this comment.
Trimmed it to the 5 cases that actually catch the bug, in 8088404 (54 -> 8 lines).
You were right about most of it. The block-size sweep over 0/1 filters asserted nothing this PR affects: countBytesInFilter's tail already compared != 0 before this change, so those rows passed on master. I removed all 26 of them.
The 5 remaining rows are not covered, though, and I did not want to drop them silently. This PR also fixes a correctness bug: countBytesInFilterWithNull's tail counted (filter & ~null) != 0 while its wide loop counts filter != 0 && null == 0, so countIfOrNull with a UInt8 condition outside 0/1 returned a block-size-dependent count. Both per-arch Bugfix validation jobs on the previous commit ran this test against master HEAD and it failed on exactly these 5 rows (amd64 100/68/68/63/65, aarch64 100/100/100/63/129, against 50/50/50/31/64), which is why those jobs report OK.
Removing them entirely also trips the merge gate: new_tests_check.py requires a pr-bugfix PR to carry a test that reproduces on master.
Happy to drop these too if you would rather change the changelog category to a CI/performance one and let the correctness half go untested. Your call.
Only the non-binary UInt8 condition cases fail without the fix. The block-size sweep over 0/1 filters passed on master already, so it asserted nothing this change affects: countBytesInFilter's tail compared != 0 before this PR too, and the AArch64 mask equivalence for 0/1 filters is checked exhaustively out of tree. Both per-arch Bugfix validation jobs on b250f97 confirm which cases discriminate: they ran this test against master HEAD and it failed on exactly these five rows, amd64 reporting 100/68/68/63/65 and aarch64 100/100/100/63/129 against 50/50/50/31/64.
CI finish ledger - 8088404Every failure below has an owner: a fixing PR, or a full-effort fix task whose fixing-PR link will
Neither failure is reachable from this diff. On Session id: cron:our-pr-ci-monitor:20260825-223000 |
|
Fixing PR for the The parked |
|
Fixing PR for the |
|
Nice work on enabling the 64-byte path for AArch64! However, constructing a 64-bit mask on NEON using a On AArch64, an in-register vector accumulator using:
outperforms the bitmask +
(Tested on Apple Silicon / Clang -O3 across 20,000 iterations). This also simplifies Happy to provide a patch or push a commit if you'd like to integrate this into your PR! |
|
Thanks for the detailed numbers. Two things here, and they land differently.
The accumulator has a length ceiling that a 64 KiB block never reaches. On the timings. I have no ARM host, so I cannot reproduce 627 ns against 965 ns. The arbiter this PR named has run since, though: So the bitmask path is already measurable on the CI runner, and your accumulator is a further, separate step. I would rather not carry an unmeasured kernel through a bugfix PR, and I cannot take a commit onto this branch, so I am keeping this one as it is. It adds no SIMD kernel of its own: it reuses |
|
Opened the follow-up PR here as discussed: #119555 Includes the strict boolean pre-normalization for |
|
Quick clarification regarding the accumulator length ceiling: The 16-bit accumulator is not preserved across flushes—it is declared inside the chunk loop and flushed via Each |
|
Point 2 was wrong about your PR, so I retract it. I read the pushed code at The predicate landed right: One overlap is worth flagging for whoever reviews the two together. #119555 also changes the |
|
Superseded by #119555, which merged on 2026-09-13 (merge commit Measured against master
The one line of substance left here is the correction of the stale comment above both loops, since master still says the condition This branch conflicts with master in |
|
Thanks a lot for the thorough and constructive reviews, and especially for catching the subtle non-boolean null-map edge cases early on,! your feedback made the final patch significantly stronger! Really enjoyed collaborating with you on this. |
|
Looks superseded by #119555 |
|
Agreed, it is. I checked master before answering: both halves of this PR are on it now. The AArch64 wide path is #119555's NEON kernel, and the scalar tail uses the same predicate as the wide loop in both functions ( |
Enable the 64-byte filter counting path on AArch64
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed
countIfOrNullover aNullableargument returning a count that depended on the block size when the condition was aUInt8value other than 0 or 1. Also sped up filter row counting on AArch64:countBytesInFilterandcountBytesInFilterWithNullreduce a filter 64 bytes at a time using a bitmask andpopcount, but that loop was gated on__SSE2__, so AArch64 builds counted one byte per iteration. Closes #116073.Description
Both wide loops in
src/Columns/ColumnsCommon.cpp, and the privatetoBits64they call, sat behinda bare
#if defined(__SSE2__): an x86-64 baseline feature, not runtime dispatch, so x86 alwayscompiled the fast path and AArch64 never did. The guards now also admit
defined(__aarch64__) && defined(__ARM_NEON), with an#elifarm definingtoBits64viabytes64MaskToBits64Maskfrom this module's header, which already has a NEON tier. NEON isunconditional at the AArch64 baseline, and this file already calls that helper in
filterArraysImplGenericandfilterArraysImplInPlace. On x86 the guard is inert.One correctness fix rides along, and it does change x86. An
-Ifcondition column is forwarded asa raw
UInt8, so bytes outside 0/1 reach these loops, andcountBytesInFilterWithNull's scalartail counted
(*pos & ~*pos2) != 0while its wide loop countsfilter != 0 && null == 0. Thesediffer on a NULL row whose condition byte keeps a set bit outside the null byte: of the 65536
(filter, null) pairs the tail disagrees on 58720, always over-counting, never where the wide loop
errs.
countIfOrNull(if(number % 2 = 0, NULL, 1), toUInt8(2))over 100 rows returns 68 or 100today depending on where the 64-byte boundary falls, against a true 50. The tail now uses the wide
loop's predicate, removing the block-size dependence everywhere. This is the correction
031132038b1e89eapplied to the> 0comparisons here;countBytesInFilter's tail already used!= 0.AArch64 mask equivalence for 0/1 filters was checked exhaustively under
qemu-aarch64-staticagainst the pre-change build: 0 mismatches, mutation-controlled. The new test covers the non-binary
UInt8condition, pinning block lengths viamax_block_size; all five of its cases fail without thetail fix (100/68/68/63/65 against 50/50/50/31/64).
The ARM
Performance Comparisonat8088404fmeasures it:rewrite_sumIf575 ms to 374 ms onarm_release(1.54x) against 227/227 ms on theamd_releasecontrol. 27 arm queries were flaggedfaster, 0 slower; on amd, 0 either way.
Workflow [PR]
Sync PR [sync-upstream/pr/116173]