Uh oh!
There was an error while loading. Please reload this page.
[SPARK-58186][SQL] Add bitmap_contains function - #58117
Open
jiangxt2 wants to merge 4 commits into
Open
Conversation
This change adds the `bitmap_contains(bitmap, bit_position)` scalar function to the existing flat BinaryType bitmap function family. It checks whether the bit at the given position is set in a bitmap, so that prebuilt bitmaps can be used directly in WHERE, CASE WHEN and join conditions as a membership predicate. The function returns false for a negative position, a position outside the 32768-bit fixed range, or a position beyond the actual byte array length. Type-valid SQL NULL inputs follow Spark nullable expression semantics and produce NULL. The second argument accepts any Spark numeric value and is explicitly cast to BIGINT with standard Spark cast semantics, including ANSI mode behavior, before the lookup. Implementation: - Add the BitmapContains expression as a RuntimeReplaceable that invokes the bitmapContains helper in BitmapExpressionUtils, and register the `bitmap_contains` function in FunctionRegistry. - Add the Scala `bitmap_contains(bitmap: Column, bitPosition: Column)` API and the PySpark classic / Spark Connect wrappers with full docs. - Add unit, SQL, PySpark and Connect plan generation tests, and regenerate the SQL expression schema and Connect golden files. Tests: Targeted BitmapExpressionUtilsSuite, BitmapExpressionsQuerySuite, PySpark functions tests and doctests, Connect plan generation, and golden-file validation pass. Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com>
Clarify that bitmap_contains checks a bucket-local bit position and document the required bitmap_bucket_number and bitmap_bit_position mapping for original values. Update the join test to exercise the complete bucketed lookup path. 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>
jiangxt2
marked this pull request as ready for review
August 20, 2026 07:10
jiangxt2
commented
Aug 20, 2026
ContributorAuthor
Additional ecosystem context: Apache Doris and StarRocks expose |
Preserve bitmap_contains while incorporating the upstream bitmap_xor_agg changes. Co-Authored-By: Chang-Tong <zdcheerful@hotmail.com> Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com>
uros-b
approved these changes
Aug 20, 2026
uros-b
commented
Aug 20, 2026
Member
Thank you @jiangxt2! |
uros-b
commented
Aug 20, 2026
Member
cc @HyukjinKwon who reviewed #57336 |
Merge current master changes while preserving bitmap_contains and bitmap scalar set operation implementations and tests. Co-Authored-By: cwq222 <15503804976@163.com> Signed-off-by: jiangxt2 <jiangxt2@vip.qq.com>
jiangxt2
commented
Aug 24, 2026
ContributorAuthor
Hi @cloud-fan, if you have time, would you mind taking a look at this PR? It adds bitmap_contains to Spark’s bitmap function family. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR adds
bitmap_contains(bitmap, bit_position), a scalar predicate for Spark's existing flatBinaryTypebitmap function family. It is available through SQL, the Scala DataFrame API, PySpark classic, and PySpark Connect.The function:
BIGINTusing Spark's existing cast semantics;NULLfor null bitmap or position inputs;falsefor negative positions, positions at or above 32768, and positions beyond the actual bitmap byte length.For an original value, callers use
bitmap_bucket_number(value)to select the bitmap bucket andbitmap_bit_position(value)as the position passed tobitmap_contains.This is a clean resubmission of #57336 against the current Spark codebase. It retains the function-argument documentation requested during the earlier review and expands the coverage for bucket semantics and bucket-aware joins. It keeps Spark's existing flat bitmap representation and does not introduce another bitmap type, serialization format, or external dependency.
This PR is independent of #58066, which proposes scalar bitmap set operations. The APIs are complementary: if both changes are merged,
bitmap_containscan also test a bitmap returned by the scalar set operations.Why are the changes needed?
Spark can construct flat bitmaps, aggregate them across rows, and count their set bits, but it has no built-in predicate for testing whether a specific position is present.
Without this function, users must convert the bitmap to another collection representation or use a UDF. That loses the consistent SQL, Scala, PySpark classic, and Spark Connect API surface provided by a native Catalyst expression.
bitmap_containscompletes the existing bitmap workflow and allows precomputed bitmaps to be used directly in filters,CASE WHENexpressions, and bucket-aware membership joins.Does this PR introduce any user-facing change?
Yes. Released Spark versions do not provide this function, and this PR does not change the behavior of any existing bitmap function.
It adds the following function in SQL, the Scala DataFrame API, PySpark classic, and PySpark Connect:
The position is local to one bitmap bucket. A query using original values must match the bucket and pass
bitmap_bit_position(value)tobitmap_contains.For example:
This returns
1and32769.A null bitmap or position produces SQL
NULL. A negative position, a position at or above 32768, or a position beyond the actual bitmap byte length producesfalse.How was this patch tested?
The patch adds coverage for:
Longboundaries;CASE WHENpredicates;The SQL expression schema and Connect query-test golden files were regenerated.
Targeted validation completed for the production implementation before the final bucket-aware join test rewrite:
BitmapExpressionUtilsSuite: 9 tests passed.BitmapExpressionsQuerySuite: 19 tests passed.PlanGenerationTestSuite: 745 tests passed, with 2 ignored.ProtoToParsedPlanTestSuite: 750 tests passed.The current head,
c52d16aa09ce406f38084d4adf7a54d86ef80e94, includes the final bucket-aware join test. Its GitHub CI rollup passed: 30 checks succeeded, and one Kubernetes integration check was skipped.Current cumulative diff static validation:
git diff --check: passed.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Codex and Claude AI