xiaoxuandev opened a new pull request, #54631:
URL: https://github.com/apache/spark/pull/54631

   ### 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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to