Repository navigation
Compute bitmap intersection cardinality without materializing the result - #117478
PedroTadim merged 2 commits into
Conversation
bitmapAndCardinality built the full intersection bitmap, counted it and threw it away, and bitmapHasAny used the same construct to answer a boolean question. CRoaring provides roaring_bitmap_and_cardinality and roaring_bitmap_intersect, which reach the answer without allocating or populating a result, and the latter can stop at the first shared element. Use them for 32-bit element types. bitmapOrCardinality, bitmapXorCardinality and bitmapAndnotCardinality all derive their answer from rb_and_cardinality by inclusion-exclusion, so they benefit too. rb_and_cardinality also promoted a small set to a Roaring bitmap whenever the larger operand came first, then ran a full intersection. It now iterates the small side against contains(), matching what rb_intersect already did and the operand swap that FunctionsBitmap.h already performs for bitmapAnd. UInt64 and Int64 bitmaps are backed by Roaring64Map, which exposes neither routine, so those instantiations keep the previous implementation.
|
@alexey-milovidov |
|
Workflow [PR], commit [f16bd91] Summary: ❌
AI ReviewSummaryThis PR replaces materialized bitmap intersections with direct CRoaring PR Metadata
💡 The current changelog entry reads like all bitmap element widths benefit, but
Missing context / blind spots
Tests
Final VerdictStatus: Minimum required action: attach before/after results from the new benchmark against LLVM Coverage ReportMeasured on commit f16bd91.
Changed lines: Changed C/C++ lines covered: 13/13 (100.00%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units23 translation units recompiled, 238 s compile time in total, 23 of them have a recent master baseline. |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbenchFlagged queries (3 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
@cv4g |
|
@anandheritage can you show the performance improvement you got? |
Both operands of every measured call were scalar subqueries, so every argument was constant. bitmapAndCardinality and bitmapHasAny use the default implementation for constants, which evaluates the call once on a single row and broadcasts the result as a constant, so the loop measured the sum over a precomputed value rather than rb_and_cardinality or rb_intersect. The CI performance comparison accordingly reported every query within noise, between -2.7% and +3.3%. Wrap one operand of each call in materialize(). That leaves the arguments not all constant, so defaultImplementationForConstantArguments declines and executeImpl runs over the real row count. The other operand stays constant and is not expanded, because the function already reads both arguments through is_column_const. materialize() is cheap here: it converts a constant ColumnAggregateFunction to a full column via replicate, which copies the aggregate state pointer per row and does not copy the bitmap. Build the operands once into a Memory table instead of in scalar subqueries, so that construction is no longer part of the measured time. It previously dominated it: the dense query spent about 65 ms of its 67 ms building two 3-million-element bitmaps. Iteration counts are raised to keep each run in the 100 ms range now that the loop does the work, well below the 2 second per-run limit, and max_threads is pinned to 1 to match bitmap_contains.xml and bitmap_array_element.xml. The operand shapes are unchanged: dense bitset containers, sparse array containers, bitmapOrCardinality reaching rb_and_cardinality by inclusion-exclusion, the Large x Small order, and the two bitmapHasAny cases with and without an available early exit.
|
@anandheritage please sign the CLA |
@PedroTadim here are the numbers from the perf comparison on
These are all nine of the above-threshold speedups in the run. The one strong slowdown reported is an |
@PedroTadim Let me know if any more details are required or we are good to merge ? |
3c9f065
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improved performance of
bitmapAndCardinality,bitmapOrCardinality,bitmapXorCardinality,bitmapAndnotCardinalityandbitmapHasAnyby computing the result directly instead of first materializing the intersection bitmap.Problem
rb_and_cardinalityanswered "how large is the intersection" by constructing the entire intersection bitmap, reading its cardinality and discarding it:rb_intersectused the same construct to answer a boolean question:operator&allocates and populates a fresh container for every overlapping container pair. In the boolean case it also removes any possibility of an early exit, because the whole intersection is built before the first element is examined.CRoaring already provides
roaring_bitmap_and_cardinalityandroaring_bitmap_intersectfor exactly these two questions. Both walk the container pairs and accumulate or short-circuit without allocating or writing a result structure.Change
rb_and_cardinalitynow callsand_cardinality().rb_intersectnow callsintersect(), which stops at the first shared element.rb_and_cardinalitypreviously promoted a small set to a full Roaring bitmap whenever the larger operand came first, then ran a complete intersection over it. It now iterates the small side againstcontains(). This mirrors whatrb_intersectalready did for the same operand shape, and matches the operand swap thatFunctionsBitmap.halready performs forbitmapAndfor the same reason:// bitmapAnd(RoaringBitMap, SmallSet) is slower than bitmapAnd(SmallSet, RoaringBitMap), so we can exchange the position of two arguments for the speedFive SQL functions are affected:
bitmapAndCardinalityandbitmapHasAnydirectly, andbitmapOrCardinality,bitmapXorCardinalityandbitmapAndnotCardinalitytransitively, since all three derive their result fromrb_and_cardinalityby inclusion-exclusion.Scope: 32-bit element types
RoaringBitmapWithSmallSetselectsroaring::Roaring64Mapwhensizeof(T) >= 8.Roaring64Mapexposes neitherand_cardinalitynorintersect(still true on CRoaring master), soUInt64andInt64bitmaps keep the previous implementation behindif constexpr (sizeof(T) < 8). Adding those routines toRoaring64Mapwould be a separate upstream change.Both routines are read-only, so results and the serialization format are unchanged.
Testing
tests/queries/0_stateless/05055_bitmap_cardinality_intersect_paths.sqlcovers every combination of small-set and Roaring representation, including theLarge x Smalloperand order, which previously had no coverage for these functions.The assertions are differential: each cardinality function is checked against
bitmapAndcombined withbitmapCardinality, which reaches the same answer throughrb_andfollowed bysize()and shares no code with the paths changed here. Every assertion prints1when the two agree. Covered:Small x Small,Small x Large,Large x Small(with and without overlap),Large x Large(overlapping and disjoint)UInt64including values above the 32-bit range, plusInt32andInt16, so theRoaring64Mapfallback and the signed dispatch are both exercisedtests/performance/bitmap_cardinality.xmlmeasures the changed paths with operand construction hoisted into scalar subqueries, so the measured loop contains only the operation under test. It covers dense (bitset container) and sparse (array container) operands,bitmapOrCardinalityreachingrb_and_cardinalityindirectly, theLarge x Smalloperand order, and twobitmapHasAnyshapes: one where the operands share their lowest element so the predicate can exit immediately, and one where both operands occupy the same container keys but share no element, which is the worst case with no early exit available.Workflow [PR]
Sync PR [sync-upstream/pr/117478]