[SPARK-55559][SQL] Fix BitCount to respect input integer type bit width#54631
Open
xiaoxuandev wants to merge 1 commit intoapache:masterfrom
Open
[SPARK-55559][SQL] Fix BitCount to respect input integer type bit width#54631xiaoxuandev wants to merge 1 commit intoapache:masterfrom
xiaoxuandev wants to merge 1 commit intoapache:masterfrom
Conversation
### What changes were proposed in this pull request? Fix BitwiseCount (bit_count) to correctly count set bits for negative values of Byte, Short, and Int types by using java.lang.Byte.toUnsignedInt() / Short.toUnsignedInt() and Integer.bitCount() instead of Long.bitCount() with hardcoded bitmasks. Both the interpreted (nullSafeEval) and codegen (doGenCode) paths are fixed. ### Why are the changes needed? bit_count(CAST(-1 AS TINYINT)) incorrectly returned 64 instead of 8 because java.lang.Long.bitCount() sign-extends smaller integer types to 64-bit before counting. For example, the byte value 0xFF was sign-extended to 0xFFFFFFFFFFFFFFFF, resulting in 64 bits instead of 8. ### Does this PR introduce _any_ user-facing change? Yes. bit_count now returns correct results for negative Byte, Short, and Int values. ### How was this patch tested? Added new test case in BitwiseExpressionsSuite covering negative values for all integer types, including boundary values and consistency checks between interpreted and codegen paths. ### Was this patch authored or co-authored using generative AI tooling? No.
Member
|
FYI, #52574 |
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 free
to 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?
Fix BitwiseCount (bit_count) to correctly count set bits for negative values of Byte, Short, and Int types by using java.lang.Byte.toUnsignedInt() / Short.toUnsignedInt() and Integer.bitCount() instead of Long.bitCount() with hardcoded bitmasks. Both the interpreted (nullSafeEval) and codegen (doGenCode) paths are fixed.
Why are the changes needed?
bit_count(CAST(-1 AS TINYINT)) incorrectly returned 64 instead of 8 because java.lang.Long.bitCount() sign-extends smaller integer types to 64-bit before counting. For example, the byte value 0xFF was sign-extended to 0xFFFFFFFFFFFFFFFF, resulting in 64 bits instead of 8.
Does this PR introduce any user-facing change?
Yes. bit_count now returns correct results for negative Byte, Short, and Int values.
How was this patch tested?
Added new test case in BitwiseExpressionsSuite covering negative values for all integer types, including boundary values and consistency checks between interpreted and codegen paths.
Was this patch authored or co-authored using generative AI tooling?
No.