[SPARK-58186][SQL] Add bitmap_contains function#57336
Conversation
Add bitmap_contains(bitmap, bit_position) that returns true if the bit at the given position is set in the bitmap, false otherwise. This completes the Spark flat-bitmap expression family by providing the membership-test predicate needed for segment filtering, retention analysis, and crowd-selection queries — the primary use case of bitmap indexes in analytical databases. Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com>
Use explicit checkInputDataTypes() instead of ImplicitCastInputTypes to reject non-Binary first arguments and non-Numeric second arguments, matching BitmapCount's strict semantics. This prevents implicit STRING -> BINARY coercion for the bitmap parameter. Add boundary test coverage for zero-length bitmap and the early-return guard for positions far beyond the bitmap range. Add query-level tests for NULL bit_position propagation and the non-numeric second-argument error path. Co-Authored-By: Zhang Dong <zdcheerful@hotmail.com> Co-Authored-By: ArtificialIdoit <bill.sea@hotmail.com> Co-Authored-By: cwq222 <15503804976@163.com> Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com>
- Wrap long lines to stay within 100-char limit - Fix show() column width in bitmap_contains doctest - Regenerate explain golden file after rebase Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com> Co-Authored-By: Zhang Dong <zdcheerful@hotmail.com> Co-Authored-By: ArtificialIdoit <bill.sea@hotmail.com> Co-Authored-By: cwq222 <15503804976@163.com>
eea6228 to
e6a5637
Compare
ContextWe run a telecom data platform where event tables reach tens-of-billions of
-- ETL: once
CREATE TABLE seg_bitmaps AS
SELECT group_id, bitmap_bucket_number(user_id) AS bucket,
bitmap_construct_agg(bitmap_bit_position(user_id)) AS bm
FROM user_segments GROUP BY group_id, bitmap_bucket_number(user_id);
-- Query: bit check instead of shuffle join
SELECT e.* FROM events e
JOIN seg_bitmaps s
ON bitmap_bucket_number(e.user_id) = s.bucket AND s.group_id = 100
WHERE bitmap_contains(s.bm, bitmap_bit_position(e.user_id));Bloom Filter vs bitmap_containsBoth are join pre-filtering techniques, but they differ fundamentally:
The key difference: bloom filter degrades as the creation side grows — Currently Cross-engine: ClickHouse, Doris, and StarRocks all provide this function. Feedback welcome. |
| usage = """ | ||
| _FUNC_(bitmap, bit_position) - Returns true if the bit at the given position is set in the | ||
| bitmap, false otherwise. | ||
| """, |
There was a problem hiding this comment.
Thanks for the review! Added the arguments block to bitmap_contains, following the same format as in #57183.
…tion Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com> Co-Authored-By: Zhang Dong <zdcheerful@hotmail.com> Co-Authored-By: ArtificialIdoit <bill.sea@hotmail.com> Co-Authored-By: cwq222 <15503804976@163.com>
What changes were proposed in this pull request?
bitmap_contains(bitmap, bit_position)scalar function: returns trueif the bit at the given position is set in the bitmap, false otherwise.
range return false rather than raising an error, following the principle that
invalid lookups in a query filter should not fail the entire query.
Why are the changes needed?
Spark's flat-bitmap expression family has construction (
bitmap_construct_agg),aggregation (
bitmap_or_agg,bitmap_and_agg), and counting (bitmap_count),but lacks a membership-test predicate for filtering rows.
bitmap_containsenables bitmap columns to be used inWHEREfilters,CASE WHENclassification, andJOINconditions -- the primary use case ofbitmap indexes in analytical databases. This function is also available in
other bitmap-using engines such as ClickHouse
bitmapContains, Dorisbitmap_contains, and StarRocksbitmap_contains.Example:
Does this PR introduce any user-facing change?
Yes. A new
bitmap_containsfunction is available in SQL, the Scala DataFrame API,and PySpark (classic and Connect).
How was this patch tested?
BitmapExpressionUtilsSuite: 9 unit tests covering bit-set, bit-unset,boundary positions, out-of-range, short bitmap, zero-length bitmap, and
early-return guard for extreme position values.
BitmapExpressionsQuerySuite: 10 SQL-level tests covering basic usage,WHEREfilter,CASE WHEN,NULLpropagation, type errors for botharguments, and combination with
bitmap_and_agg.PlanGenerationTestSuite: Spark Connect plan generation test withgenerated golden files.
sql-expression-schema.md: Regenerated golden file.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code with Claude Opus 4.8