You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "kazantsev-maksim (via GitHub)" <gi...@apache.org> on 2025/10/28 12:00:36 UTC

[PR] Fix: spark bit_count function [datafusion]

kazantsev-maksim opened a new pull request, #18322:
URL: https://github.com/apache/datafusion/pull/18322

   ## Which issue does this PR close?
   
   Closes https://github.com/apache/datafusion/issues/18225
   
   ## Rationale for this change
   
   After adding the bit_count function in Comet, we got different results from Spark. (https://github.com/apache/datafusion-comet/pull/2553)
   
   ## Are these changes tested?
   
   Tested with existing unit tests


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2475313414


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -90,28 +90,28 @@ impl ScalarUDFImpl for SparkBitCount {
 fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     let value_array = value_array[0].as_ref();
     match value_array.data_type() {
-        DataType::Int8 => {
+        DataType::Int8 | DataType::Boolean => {

Review Comment:
   I also think this code will now panic if you pass in a Boolean array



##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -90,28 +90,28 @@ impl ScalarUDFImpl for SparkBitCount {
 fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     let value_array = value_array[0].as_ref();
     match value_array.data_type() {
-        DataType::Int8 => {
+        DataType::Int8 | DataType::Boolean => {

Review Comment:
   This comment seems misplaced as the code adds support for boolean πŸ€” 



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "kazantsev-maksim (via GitHub)" <gi...@apache.org>.
kazantsev-maksim commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2469317431


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -90,28 +90,28 @@ impl ScalarUDFImpl for SparkBitCount {
 fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     let value_array = value_array[0].as_ref();
     match value_array.data_type() {
-        DataType::Int8 => {
+        DataType::Int8 | DataType::Boolean => {

Review Comment:
   Spark supports only signed int types



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "kazantsev-maksim (via GitHub)" <gi...@apache.org>.
kazantsev-maksim commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2478907608


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -90,28 +90,28 @@ impl ScalarUDFImpl for SparkBitCount {
 fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     let value_array = value_array[0].as_ref();
     match value_array.data_type() {
-        DataType::Int8 => {
+        DataType::Int8 | DataType::Boolean => {

Review Comment:
   Added an additional test for BooleanArray.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2476637400


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -147,6 +147,18 @@ fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     }
 }
 
+// Here’s the equivalent Rust implementation of the bitCount function (similar to Apache Spark's bitCount for LongType)
+fn bit_count(i: i64) -> i32 {

Review Comment:
   Could we enhance the comment here with this link + potentially link to original source code this actual function is copied from?



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2479976399


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -147,6 +147,18 @@ fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     }
 }
 
+// Here’s the equivalent Rust implementation of the bitCount function (similar to Apache Spark's bitCount for LongType)
+fn bit_count(i: i64) -> i32 {

Review Comment:
   Where was the actual bitcount implementation code pulled from though?



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2483062173


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -147,12 +155,26 @@ fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     }
 }
 
+// Here’s the equivalent Rust implementation of the bitCount function (similar to Apache Spark's bitCount for LongType)
+// Spark: https://github.com/apache/spark/blob/master/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/bitwiseExpressions.scala#L243
+// Java impl: https://github.com/openjdk/jdk/blob/master/src/java.base/share/classes/java/lang/Long.java#L1584

Review Comment:
   ```suggestion
   // Spark: https://github.com/apache/spark/blob/ac717dd7aec665de578d7c6b0070e8fcdde3cea9/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/bitwiseExpressions.scala#L243
   // Java impl: https://github.com/openjdk/jdk/blob/d226023643f90027a8980d161ec6d423887ae3ce/src/java.base/share/classes/java/lang/Long.java#L1584
   ```
   
   Permalink so we always point to the right code



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "kazantsev-maksim (via GitHub)" <gi...@apache.org>.
kazantsev-maksim commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2478906668


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -90,28 +90,28 @@ impl ScalarUDFImpl for SparkBitCount {
 fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     let value_array = value_array[0].as_ref();
     match value_array.data_type() {
-        DataType::Int8 => {
+        DataType::Int8 | DataType::Boolean => {

Review Comment:
   I wanted to note that Spark only supports signed integer and boolean types as input.



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "kazantsev-maksim (via GitHub)" <gi...@apache.org>.
kazantsev-maksim commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2482100393


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -147,6 +147,18 @@ fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     }
 }
 
+// Here’s the equivalent Rust implementation of the bitCount function (similar to Apache Spark's bitCount for LongType)
+fn bit_count(i: i64) -> i32 {

Review Comment:
   Added additional link to Java implementation of bitCount function



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "kazantsev-maksim (via GitHub)" <gi...@apache.org>.
kazantsev-maksim commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2478884855


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -147,6 +147,18 @@ fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     }
 }
 
+// Here’s the equivalent Rust implementation of the bitCount function (similar to Apache Spark's bitCount for LongType)
+fn bit_count(i: i64) -> i32 {

Review Comment:
   Thanks, added



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "pepijnve (via GitHub)" <gi...@apache.org>.
pepijnve commented on PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#issuecomment-3557011660

   Sorry for reviving this one. We had a need for `bit_count` in our codebase as well, so I was looking at the Spark implementation. This test seems really bizarre:
   
   ```
   #[test]
   fn test_bit_count_int32() {
       // Test bit_count on Int32Array
       let result =
           spark_bit_count(&[Arc::new(Int32Array::from(vec![0i32, 1, 255, 1023, -1]))])
               .unwrap();
   
       let arr = result.as_primitive::<Int32Type>();
   ...
       assert_eq!(arr.value(4), 64); // -1 in two's complement = all 32 bits set
   }
   ```
   
   Counting the bits of a 32-bit value and getting back 64 is not what you would expect at all. Is that really how Spark works? It's a bit underspecced (or at least ambiguous) in the [Spark documentation](https://spark.apache.org/docs/latest/api/sql/index.html#bit_count). 
   
   Just for comparison, here's DuckDB's answer to the same question
   ```
   D select bit_count(cast(-1 as int));
   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚ bit_count(CAST(-1 AS INTEGER)) β”‚
   β”‚              int8              β”‚
   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
   β”‚               32               β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
   ```


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "kazantsev-maksim (via GitHub)" <gi...@apache.org>.
kazantsev-maksim commented on code in PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#discussion_r2469314140


##########
datafusion/spark/src/function/bitwise/bit_count.rs:
##########
@@ -147,6 +147,18 @@ fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
     }
 }
 
+// Here’s the equivalent Rust implementation of the bitCount function (similar to Apache Spark's bitCount for LongType)
+fn bit_count(i: i64) -> i32 {

Review Comment:
   https://github.com/apache/spark/blob/7ac2b5423362e1040f4c5c63b0d4ba6fc999a465/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/bitwiseExpressions.scala#L243



-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "pepijnve (via GitHub)" <gi...@apache.org>.
pepijnve commented on PR #18322:
URL: https://github.com/apache/datafusion/pull/18322#issuecomment-3557103669

   Figured it out in the meantime, and logged a PR to simplify the implementation.


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org


Re: [PR] Fix: spark bit_count function [datafusion]

Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey merged PR #18322:
URL: https://github.com/apache/datafusion/pull/18322


-- 
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: github-unsubscribe@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org