Skip to content

Apache DataFusion SQL Query Engine

feat(spark): implement Spark math function bit_get/bit_count (#16942)

Chen Chongchenc5cb6f4ad751
diff --git a/datafusion/spark/src/function/bitwise/bit_count.rs b/datafusion/spark/src/function/bitwise/bit_count.rs
new file mode 100644
index 0000000000..5605e80c37
--- /dev/null
+++ b/datafusion/spark/src/function/bitwise/bit_count.rs
@@ -0,0 +1,319 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::any::Any;
+use std::sync::Arc;
+
+use arrow::array::{ArrayRef, AsArray, Int32Array};
+use arrow::datatypes::{
+ DataType, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type, UInt32Type,
+ UInt64Type, UInt8Type,
+};
+use datafusion_common::{plan_err, Result};
+use datafusion_expr::{
+ ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature,
+ Volatility,
+};
+use datafusion_functions::utils::make_scalar_function;
+
+#[derive(Debug)]
+pub struct SparkBitCount {
+ signature: Signature,
+}
+
+impl Default for SparkBitCount {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl SparkBitCount {
+ pub fn new() -> Self {
+ Self {
+ signature: Signature::one_of(
+ vec![
+ TypeSignature::Exact(vec![DataType::Int8]),
+ TypeSignature::Exact(vec![DataType::Int16]),
+ TypeSignature::Exact(vec![DataType::Int32]),
+ TypeSignature::Exact(vec![DataType::Int64]),
+ TypeSignature::Exact(vec![DataType::UInt8]),
+ TypeSignature::Exact(vec![DataType::UInt16]),
+ TypeSignature::Exact(vec![DataType::UInt32]),
+ TypeSignature::Exact(vec![DataType::UInt64]),
+ ],
+ Volatility::Immutable,
+ ),
+ }
+ }
+}
+
+impl ScalarUDFImpl for SparkBitCount {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
+ fn name(&self) -> &str {
+ "bit_count"
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+ Ok(DataType::Int32) // Spark returns int (Int32)
+ }
+
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
+ if args.args.len() != 1 {
+ return plan_err!("bit_count expects exactly 1 argument");
+ }
+
+ make_scalar_function(spark_bit_count, vec![])(&args.args)
+ }
+}
+
+fn spark_bit_count(value_array: &[ArrayRef]) -> Result<ArrayRef> {
+ let value_array = value_array[0].as_ref();
+ match value_array.data_type() {
+ DataType::Int8 => {
+ let result: Int32Array = value_array
+ .as_primitive::<Int8Type>()
+ .unary(|v| v.count_ones() as i32);
+ Ok(Arc::new(result))
+ }
+ DataType::Int16 => {
+ let result: Int32Array = value_array
+ .as_primitive::<Int16Type>()
+ .unary(|v| v.count_ones() as i32);
+ Ok(Arc::new(result))
+ }
+ DataType::Int32 => {
+ let result: Int32Array = value_array
+ .as_primitive::<Int32Type>()
+ .unary(|v| v.count_ones() as i32);
+ Ok(Arc::new(result))
+ }
+ DataType::Int64 => {
+ let result: Int32Array = value_array
+ .as_primitive::<Int64Type>()
+ .unary(|v| v.count_ones() as i32);
+ Ok(Arc::new(result))
+ }
+ DataType::UInt8 => {
+ let result: Int32Array = value_array
+ .as_primitive::<UInt8Type>()
+ .unary(|v| v.count_ones() as i32);
+ Ok(Arc::new(result))
+ }
+ DataType::UInt16 => {
+ let result: Int32Array = value_array
+ .as_primitive::<UInt16Type>()
+ .unary(|v| v.count_ones() as i32);
+ Ok(Arc::new(result))
+ }
+ DataType::UInt32 => {
+ let result: Int32Array = value_array
+ .as_primitive::<UInt32Type>()
+ .unary(|v| v.count_ones() as i32);
+ Ok(Arc::new(result))
+ }
+ DataType::UInt64 => {
+ let result: Int32Array = value_array
+ .as_primitive::<UInt64Type>()
+ .unary(|v| v.count_ones() as i32);
+ Ok(Arc::new(result))
+ }
+ _ => {
+ plan_err!(
+ "bit_count function does not support data type: {:?}",
+ value_array.data_type()
+ )
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use arrow::array::{
+ Array, Int16Array, Int32Array, Int64Array, Int8Array, UInt16Array, UInt32Array,
+ UInt64Array, UInt8Array,
+ };
+ use arrow::datatypes::Int32Type;
+
+ #[test]
+ fn test_bit_count_basic() {
+ // Test bit_count(0) - no bits set
+ let result = spark_bit_count(&[Arc::new(Int32Array::from(vec![0]))]).unwrap();
+
+ assert_eq!(result.as_primitive::<Int32Type>().value(0), 0);
+
+ // Test bit_count(1) - 1 bit set
+ let result = spark_bit_count(&[Arc::new(Int32Array::from(vec![1]))]).unwrap();
+
+ assert_eq!(result.as_primitive::<Int32Type>().value(0), 1);
+
+ // Test bit_count(7) - 7 = 111 in binary, 3 bits set
+ let result = spark_bit_count(&[Arc::new(Int32Array::from(vec![7]))]).unwrap();
+
+ assert_eq!(result.as_primitive::<Int32Type>().value(0), 3);
+
+ // Test bit_count(15) - 15 = 1111 in binary, 4 bits set
+ let result = spark_bit_count(&[Arc::new(Int32Array::from(vec![15]))]).unwrap();
+
+ assert_eq!(result.as_primitive::<Int32Type>().value(0), 4);
+ }
+
+ #[test]
+ fn test_bit_count_int8() {
+ // Test bit_count on Int8Array
+ let result =
+ spark_bit_count(&[Arc::new(Int8Array::from(vec![0i8, 1, 3, 7, 15, -1]))])
+ .unwrap();
+
+ let arr = result.as_primitive::<Int32Type>();
+ assert_eq!(arr.value(0), 0);
+ assert_eq!(arr.value(1), 1);
+ assert_eq!(arr.value(2), 2);
+ assert_eq!(arr.value(3), 3);
+ assert_eq!(arr.value(4), 4);
+ assert_eq!(arr.value(5), 8);
+ }
+
+ #[test]
+ fn test_bit_count_int16() {
+ // Test bit_count on Int16Array
+ let result =
+ spark_bit_count(&[Arc::new(Int16Array::from(vec![0i16, 1, 255, 1023, -1]))])
+ .unwrap();
+
+ let arr = result.as_primitive::<Int32Type>();
+ assert_eq!(arr.value(0), 0);
+ assert_eq!(arr.value(1), 1);
+ assert_eq!(arr.value(2), 8);
+ assert_eq!(arr.value(3), 10);
+ assert_eq!(arr.value(4), 16);
+ }
+
+ #[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(0), 0); // 0b00000000000000000000000000000000 = 0
+ assert_eq!(arr.value(1), 1); // 0b00000000000000000000000000000001 = 1
+ assert_eq!(arr.value(2), 8); // 0b00000000000000000000000011111111 = 8
+ assert_eq!(arr.value(3), 10); // 0b00000000000000000000001111111111 = 10
+ assert_eq!(arr.value(4), 32); // -1 in two's complement = all 32 bits set
+ }
+
+ #[test]
+ fn test_bit_count_int64() {
+ // Test bit_count on Int64Array
+ let result =
+ spark_bit_count(&[Arc::new(Int64Array::from(vec![0i64, 1, 255, 1023, -1]))])
+ .unwrap();
+
+ let arr = result.as_primitive::<Int32Type>();
+ assert_eq!(arr.value(0), 0); // 0b0000000000000000000000000000000000000000000000000000000000000000 = 0
+ assert_eq!(arr.value(1), 1); // 0b0000000000000000000000000000000000000000000000000000000000000001 = 1
+ assert_eq!(arr.value(2), 8); // 0b0000000000000000000000000000000000000000000000000000000011111111 = 8
+ assert_eq!(arr.value(3), 10); // 0b0000000000000000000000000000000000000000000000000000001111111111 = 10
+ assert_eq!(arr.value(4), 64); // -1 in two's complement = all 64 bits set
+ }
+
+ #[test]
+ fn test_bit_count_uint8() {
+ // Test bit_count on UInt8Array
+ let result =
+ spark_bit_count(&[Arc::new(UInt8Array::from(vec![0u8, 1, 255]))]).unwrap();
+
+ let arr = result.as_primitive::<Int32Type>();
+ assert_eq!(arr.value(0), 0); // 0b00000000 = 0
+ assert_eq!(arr.value(1), 1); // 0b00000001 = 1
+ assert_eq!(arr.value(2), 8); // 0b11111111 = 8
+ }
+
+ #[test]
+ fn test_bit_count_uint16() {
+ // Test bit_count on UInt16Array
+ let result =
+ spark_bit_count(&[Arc::new(UInt16Array::from(vec![0u16, 1, 255, 65535]))])
+ .unwrap();
+
+ let arr = result.as_primitive::<Int32Type>();
+ assert_eq!(arr.value(0), 0); // 0b0000000000000000 = 0
+ assert_eq!(arr.value(1), 1); // 0b0000000000000001 = 1
+ assert_eq!(arr.value(2), 8); // 0b0000000011111111 = 8
+ assert_eq!(arr.value(3), 16); // 0b1111111111111111 = 16
+ }
+
+ #[test]
+ fn test_bit_count_uint32() {
+ // Test bit_count on UInt32Array
+ let result = spark_bit_count(&[Arc::new(UInt32Array::from(vec![
+ 0u32, 1, 255, 4294967295,
+ ]))])
+ .unwrap();
+
+ let arr = result.as_primitive::<Int32Type>();
+ assert_eq!(arr.value(0), 0); // 0b00000000000000000000000000000000 = 0
+ assert_eq!(arr.value(1), 1); // 0b00000000000000000000000000000001 = 1
+ assert_eq!(arr.value(2), 8); // 0b00000000000000000000000011111111 = 8
+ assert_eq!(arr.value(3), 32); // 0b11111111111111111111111111111111 = 32
+ }
+
+ #[test]
+ fn test_bit_count_uint64() {
+ // Test bit_count on UInt64Array
+ let result = spark_bit_count(&[Arc::new(UInt64Array::from(vec![
+ 0u64,
+ 1,
+ 255,
+ 256,
+ u64::MAX,
+ ]))])
+ .unwrap();
+
+ let arr = result.as_primitive::<Int32Type>();
+ // 0b0 = 0
+ assert_eq!(arr.value(0), 0);
+ // 0b1 = 1
+ assert_eq!(arr.value(1), 1);
+ // 0b11111111 = 8
+ assert_eq!(arr.value(2), 8);
+ // 0b100000000 = 1
+ assert_eq!(arr.value(3), 1);
+ // u64::MAX = all 64 bits set
+ assert_eq!(arr.value(4), 64);
+ }
+
+ #[test]
+ fn test_bit_count_nulls() {
+ // Test bit_count with nulls
+ let arr = Int32Array::from(vec![Some(3), None, Some(7)]);
+ let result = spark_bit_count(&[Arc::new(arr)]).unwrap();
+ let arr = result.as_primitive::<Int32Type>();
+ assert_eq!(arr.value(0), 2); // 0b11
+ assert!(arr.is_null(1));
+ assert_eq!(arr.value(2), 3); // 0b111
+ }
+}
diff --git a/datafusion/spark/src/function/bitwise/bit_get.rs b/datafusion/spark/src/function/bitwise/bit_get.rs
new file mode 100644
index 0000000000..cd68c538f5
--- /dev/null
+++ b/datafusion/spark/src/function/bitwise/bit_get.rs
@@ -0,0 +1,287 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::any::Any;
+use std::mem::size_of;
+use std::sync::Arc;
+
+use arrow::array::{Array, ArrayRef, ArrowPrimitiveType, AsArray, PrimitiveArray};
+use arrow::compute::try_binary;
+use arrow::datatypes::DataType::{
+ Int16, Int32, Int64, Int8, UInt16, UInt32, UInt64, UInt8,
+};
+use arrow::datatypes::{
+ ArrowNativeType, DataType, Int16Type, Int32Type, Int64Type, Int8Type, UInt16Type,
+ UInt32Type, UInt64Type, UInt8Type,
+};
+use datafusion_common::{exec_err, Result};
+use datafusion_expr::{
+ ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility,
+};
+use datafusion_functions::utils::make_scalar_function;
+
+use crate::function::error_utils::{
+ invalid_arg_count_exec_err, unsupported_data_type_exec_err,
+};
+
+#[derive(Debug)]
+pub struct SparkBitGet {
+ signature: Signature,
+ aliases: Vec<String>,
+}
+
+impl Default for SparkBitGet {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl SparkBitGet {
+ pub fn new() -> Self {
+ Self {
+ signature: Signature::user_defined(Volatility::Immutable),
+ aliases: vec!["getbit".to_string()],
+ }
+ }
+}
+
+impl ScalarUDFImpl for SparkBitGet {
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
+ fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+ if arg_types.len() != 2 {
+ return Err(invalid_arg_count_exec_err(
+ "bit_get",
+ (2, 2),
+ arg_types.len(),
+ ));
+ }
+ if !arg_types[0].is_integer() && !arg_types[0].is_null() {
+ return Err(unsupported_data_type_exec_err(
+ "bit_get",
+ "Integer Type",
+ &arg_types[0],
+ ));
+ }
+ if !arg_types[1].is_integer() && !arg_types[1].is_null() {
+ return Err(unsupported_data_type_exec_err(
+ "bit_get",
+ "Integer Type",
+ &arg_types[1],
+ ));
+ }
+ if arg_types[0].is_null() {
+ return Ok(vec![Int8, Int32]);
+ }
+ Ok(vec![arg_types[0].clone(), Int32])
+ }
+
+ fn name(&self) -> &str {
+ "bit_get"
+ }
+
+ fn aliases(&self) -> &[String] {
+ &self.aliases
+ }
+
+ fn signature(&self) -> &Signature {
+ &self.signature
+ }
+
+ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
+ Ok(Int8)
+ }
+
+ fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
+ make_scalar_function(spark_bit_get, vec![])(&args.args)
+ }
+}
+
+fn spark_bit_get_inner<T: ArrowPrimitiveType>(
+ value: &PrimitiveArray<T>,
+ pos: &PrimitiveArray<Int32Type>,
+) -> Result<PrimitiveArray<Int8Type>> {
+ let bit_length = (size_of::<T::Native>() * 8) as i32;
+
+ let result: PrimitiveArray<Int8Type> = try_binary(value, pos, |value, pos| {
+ if pos < 0 || pos >= bit_length {
+ return Err(arrow::error::ArrowError::ComputeError(format!(
+ "bit_get: position {pos} is out of bounds. Expected pos < {bit_length} and pos >= 0"
+ )));
+ }
+ Ok(((value.to_i64().unwrap() >> pos) & 1) as i8)
+ })?;
+ Ok(result)
+}
+
+pub fn spark_bit_get(args: &[ArrayRef]) -> Result<ArrayRef> {
+ if args.len() != 2 {
+ return exec_err!("`bit_get` expects exactly two arguments");
+ }
+
+ if args[1].data_type() != &Int32 {
+ return exec_err!("`bit_get` expects Int32 as the second argument");
+ }
+
+ let pos_arg = args[1].as_primitive::<Int32Type>();
+
+ let ret = match &args[0].data_type() {
+ Int64 => {
+ let value_arg = args[0].as_primitive::<Int64Type>();
+ spark_bit_get_inner(value_arg, pos_arg)
+ }
+ Int32 => {
+ let value_arg = args[0].as_primitive::<Int32Type>();
+ spark_bit_get_inner(value_arg, pos_arg)
+ }
+ Int16 => {
+ let value_arg = args[0].as_primitive::<Int16Type>();
+ spark_bit_get_inner(value_arg, pos_arg)
+ }
+ Int8 => {
+ let value_arg = args[0].as_primitive::<Int8Type>();
+ spark_bit_get_inner(value_arg, pos_arg)
+ }
+ UInt64 => {
+ let value_arg = args[0].as_primitive::<UInt64Type>();
+ spark_bit_get_inner(value_arg, pos_arg)
+ }
+ UInt32 => {
+ let value_arg = args[0].as_primitive::<UInt32Type>();
+ spark_bit_get_inner(value_arg, pos_arg)
+ }
+ UInt16 => {
+ let value_arg = args[0].as_primitive::<UInt16Type>();
+ spark_bit_get_inner(value_arg, pos_arg)
+ }
+ UInt8 => {
+ let value_arg = args[0].as_primitive::<UInt8Type>();
+ spark_bit_get_inner(value_arg, pos_arg)
+ }
+ _ => {
+ exec_err!(
+ "`bit_get` expects Int64, Int32, Int16, or Int8 as the first argument"
+ )
+ }
+ }?;
+ Ok(Arc::new(ret))
+}
+
+#[cfg(test)]
+mod tests {
+ use arrow::array::{Int32Array, Int64Array};
+
+ use super::*;
+
+ #[test]
+ fn test_bit_get_basic() {
+ // Test bit_get(11, 0) - 11 = 1011 in binary, bit 0 = 1
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![11])),
+ Arc::new(Int32Array::from(vec![0])),
+ ])
+ .unwrap();
+
+ assert_eq!(result.as_primitive::<Int8Type>().value(0), 1);
+
+ // Test bit_get(11, 2) - 11 = 1011 in binary, bit 2 = 0
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![11])),
+ Arc::new(Int32Array::from(vec![2])),
+ ])
+ .unwrap();
+
+ assert_eq!(result.as_primitive::<Int8Type>().value(0), 0);
+
+ // Test bit_get(11, 3) - 11 = 1011 in binary, bit 3 = 1
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![11])),
+ Arc::new(Int32Array::from(vec![3])),
+ ])
+ .unwrap();
+
+ assert_eq!(result.as_primitive::<Int8Type>().value(0), 1);
+ }
+
+ #[test]
+ fn test_bit_get_edge_cases() {
+ // Test with 0
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![0])),
+ Arc::new(Int32Array::from(vec![0])),
+ ])
+ .unwrap();
+
+ assert_eq!(result.as_primitive::<Int8Type>().value(0), 0);
+
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![11])),
+ Arc::new(Int32Array::from(vec![-1])),
+ ]);
+ assert_eq!(result.unwrap_err().message(), "Compute error: bit_get: position -1 is out of bounds. Expected pos < 64 and pos >= 0");
+
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![11])),
+ Arc::new(Int32Array::from(vec![64])),
+ ]);
+
+ assert_eq!(result.unwrap_err().message(), "Compute error: bit_get: position 64 is out of bounds. Expected pos < 64 and pos >= 0");
+ }
+
+ #[test]
+ fn test_bit_get_null_inputs() {
+ // Test with NULL value
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![None])),
+ Arc::new(Int32Array::from(vec![0])),
+ ])
+ .unwrap();
+
+ assert_eq!(result.as_primitive::<Int8Type>().value(0), 0);
+
+ // Test with NULL position
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![11])),
+ Arc::new(Int32Array::from(vec![None])),
+ ])
+ .unwrap();
+
+ assert_eq!(result.as_primitive::<Int8Type>().value(0), 0);
+ }
+
+ #[test]
+ fn test_bit_get_large_numbers() {
+ // Test with larger number
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![255])), // 11111111 in binary
+ Arc::new(Int32Array::from(vec![7])), // bit 7 = 1
+ ])
+ .unwrap();
+
+ assert_eq!(result.as_primitive::<Int8Type>().value(0), 1);
+
+ let result = spark_bit_get(&[
+ Arc::new(Int64Array::from(vec![255])), // 11111111 in binary
+ Arc::new(Int32Array::from(vec![8])), // bit 8 = 0
+ ])
+ .unwrap();
+
+ assert_eq!(result.as_primitive::<Int8Type>().value(0), 0);
+ }
+}
diff --git a/datafusion/spark/src/function/bitwise/mod.rs b/datafusion/spark/src/function/bitwise/mod.rs
index a87df9a2c8..f8131176ff 100644
--- a/datafusion/spark/src/function/bitwise/mod.rs
+++ b/datafusion/spark/src/function/bitwise/mod.rs
@@ -15,11 +15,27 @@
// specific language governing permissions and limitations
// under the License.
+pub mod bit_count;
+pub mod bit_get;
+
use datafusion_expr::ScalarUDF;
+use datafusion_functions::make_udf_function;
use std::sync::Arc;
-pub mod expr_fn {}
+make_udf_function!(bit_get::SparkBitGet, bit_get);
+make_udf_function!(bit_count::SparkBitCount, bit_count);
+
+pub mod expr_fn {
+ use datafusion_functions::export_functions;
+
+ export_functions!((bit_get, "Returns the value of the bit (0 or 1) at the specified position.", col pos));
+ export_functions!((
+ bit_count,
+ "Returns the number of bits set in the binary representation of the argument.",
+ col
+ ));
+}
pub fn functions() -> Vec<Arc<ScalarUDF>> {
- vec![]
+ vec![bit_get(), bit_count()]
}
diff --git a/datafusion/sqllogictest/test_files/spark/bitwise/bit_count.slt b/datafusion/sqllogictest/test_files/spark/bitwise/bit_count.slt
index d513888db9..2a75c7648d 100644
--- a/datafusion/sqllogictest/test_files/spark/bitwise/bit_count.slt
+++ b/datafusion/sqllogictest/test_files/spark/bitwise/bit_count.slt
@@ -23,5 +23,205 @@
## Original Query: SELECT bit_count(0);
## PySpark 3.5.5 Result: {'bit_count(0)': 0, 'typeof(bit_count(0))': 'int', 'typeof(0)': 'int'}
-#query
-#SELECT bit_count(0::int);
+
+# Basic tests with different integer types
+query I
+SELECT bit_count(0::int);
+----
+0
+
+query I
+SELECT bit_count(1::int);
+----
+1
+
+query I
+SELECT bit_count(7::int);
+----
+3
+
+query I
+SELECT bit_count(15::int);
+----
+4
+
+query I
+SELECT bit_count(255::int);
+----
+8
+
+query I
+SELECT bit_count(1023::int);
+----
+10
+
+# Tests with negative numbers (two's complement)
+query I
+SELECT bit_count(-1::int);
+----
+32
+
+query I
+SELECT bit_count(-2::int);
+----
+31
+
+query I
+SELECT bit_count(-3::int);
+----
+31
+
+# Tests with different integer types
+query I
+SELECT bit_count(arrow_cast(0, 'Int8'));
+----
+0
+
+query I
+SELECT bit_count(arrow_cast(15, 'Int8'));
+----
+4
+
+query I
+SELECT bit_count(arrow_cast(-1, 'Int8'));
+----
+8
+
+query I
+SELECT bit_count(arrow_cast(0, 'Int16'));
+----
+0
+
+query I
+SELECT bit_count(arrow_cast(255, 'Int16'));
+----
+8
+
+query I
+SELECT bit_count(arrow_cast(-1, 'Int16'));
+----
+16
+
+query I
+SELECT bit_count(arrow_cast(0, 'Int64'));
+----
+0
+
+query I
+SELECT bit_count(arrow_cast(255, 'Int64'));
+----
+8
+
+query I
+SELECT bit_count(arrow_cast(-1, 'Int64'));
+----
+64
+
+# Tests with unsigned integer types
+query I
+SELECT bit_count(arrow_cast(0, 'UInt8'));
+----
+0
+
+query I
+SELECT bit_count(arrow_cast(255, 'UInt8'));
+----
+8
+
+query I
+SELECT bit_count(arrow_cast(0, 'UInt16'));
+----
+0
+
+query I
+SELECT bit_count(arrow_cast(65535, 'UInt16'));
+----
+16
+
+query I
+SELECT bit_count(arrow_cast(0, 'UInt32'));
+----
+0
+
+query I
+SELECT bit_count(arrow_cast(4294967295, 'UInt32'));
+----
+32
+
+query I
+SELECT bit_count(arrow_cast(0, 'UInt64'));
+----
+0
+
+query I
+SELECT bit_count(arrow_cast(18446744073709551615, 'UInt64'));
+----
+64
+
+# Tests with NULL values
+query I
+SELECT bit_count(arrow_cast(NULL, 'Int32'));
+----
+NULL
+
+query I
+SELECT bit_count(arrow_cast(NULL, 'Int8'));
+----
+NULL
+
+query I
+SELECT bit_count(arrow_cast(NULL, 'UInt64'));
+----
+NULL
+
+# Tests with edge cases
+query I
+SELECT bit_count(arrow_cast(0, 'Int32')) as zero_count;
+----
+0
+
+query I
+SELECT bit_count(arrow_cast(1, 'Int32')) as one_count;
+----
+1
+
+query I
+SELECT bit_count(arrow_cast(2, 'Int32')) as two_count;
+----
+1
+
+query I
+SELECT bit_count(arrow_cast(3, 'Int32')) as three_count;
+----
+2
+
+query I
+SELECT bit_count(arrow_cast(4, 'Int32')) as four_count;
+----
+1
+
+query I
+SELECT bit_count(arrow_cast(5, 'Int32')) as five_count;
+----
+2
+
+# Tests with large numbers
+query I
+SELECT bit_count(arrow_cast(2147483647, 'Int32'));
+----
+31
+
+query I
+SELECT bit_count(arrow_cast(-2147483648, 'Int32'));
+----
+1
+
+query I
+SELECT bit_count(arrow_cast(9223372036854775807, 'Int64'));
+----
+63
+
+query I
+SELECT bit_count(arrow_cast(-9223372036854775808, 'Int64'));
+----
+1
diff --git a/datafusion/sqllogictest/test_files/spark/bitwise/bit_get.slt b/datafusion/sqllogictest/test_files/spark/bitwise/bit_get.slt
index 8bf8851831..6a2b244d58 100644
--- a/datafusion/sqllogictest/test_files/spark/bitwise/bit_get.slt
+++ b/datafusion/sqllogictest/test_files/spark/bitwise/bit_get.slt
@@ -23,10 +23,53 @@
## Original Query: SELECT bit_get(11, 0);
## PySpark 3.5.5 Result: {'bit_get(11, 0)': 1, 'typeof(bit_get(11, 0))': 'tinyint', 'typeof(11)': 'int', 'typeof(0)': 'int'}
-#query
-#SELECT bit_get(11::int, 0::int);
+query I
+SELECT bit_get(11, 0);
+----
+1
## Original Query: SELECT bit_get(11, 2);
## PySpark 3.5.5 Result: {'bit_get(11, 2)': 0, 'typeof(bit_get(11, 2))': 'tinyint', 'typeof(11)': 'int', 'typeof(2)': 'int'}
-#query
-#SELECT bit_get(11::int, 2::int);
+query I
+SELECT bit_get(11, 2);
+----
+0
+
+## Test additional cases
+query I
+SELECT bit_get(11, 3);
+----
+1
+
+query I
+SELECT bit_get(255, 7);
+----
+1
+
+query I
+SELECT bit_get(255, 8);
+----
+0
+
+query I
+SELECT bit_get(0, 0);
+----
+0
+
+## Test edge cases
+statement error DataFusion error: Arrow error: Compute error: bit_get: position -1 is out of bounds. Expected pos < 64 and pos >= 0
+SELECT bit_get(11, -1);
+
+statement error DataFusion error: Arrow error: Compute error: bit_get: position 64 is out of bounds. Expected pos < 64 and pos >= 0
+SELECT bit_get(11, 64);
+
+## Test null inputs
+query I
+SELECT bit_get(NULL, 0);
+----
+NULL
+
+query I
+SELECT bit_get(11, NULL);
+----
+NULL
diff --git a/datafusion/sqllogictest/test_files/spark/bitwise/getbit.slt b/datafusion/sqllogictest/test_files/spark/bitwise/getbit.slt
index 438f5cb427..ca1b2724a8 100644
--- a/datafusion/sqllogictest/test_files/spark/bitwise/getbit.slt
+++ b/datafusion/sqllogictest/test_files/spark/bitwise/getbit.slt
@@ -23,10 +23,54 @@
## Original Query: SELECT getbit(11, 0);
## PySpark 3.5.5 Result: {'getbit(11, 0)': 1, 'typeof(getbit(11, 0))': 'tinyint', 'typeof(11)': 'int', 'typeof(0)': 'int'}
-#query
-#SELECT getbit(11::int, 0::int);
+query I
+SELECT getbit(11, 0);
+----
+1
## Original Query: SELECT getbit(11, 2);
## PySpark 3.5.5 Result: {'getbit(11, 2)': 0, 'typeof(getbit(11, 2))': 'tinyint', 'typeof(11)': 'int', 'typeof(2)': 'int'}
-#query
-#SELECT getbit(11::int, 2::int);
+query I
+SELECT getbit(11, 2);
+----
+0
+
+## Test additional cases
+query I
+SELECT getbit(11, 3);
+----
+1
+
+query I
+SELECT getbit(255, 7);
+----
+1
+
+query I
+SELECT getbit(255, 8);
+----
+0
+
+query I
+SELECT getbit(0, 0);
+----
+0
+
+## Test edge cases
+statement error DataFusion error: Arrow error: Compute error: bit_get: position -1 is out of bounds. Expected pos < 64 and pos >= 0
+SELECT getbit(11, -1);
+
+statement error DataFusion error: Arrow error: Compute error: bit_get: position 64 is out of bounds. Expected pos < 64 and pos >= 0
+SELECT getbit(11, 64);
+
+## Test null inputs
+query I
+SELECT getbit(NULL, 0);
+----
+NULL
+
+query I
+SELECT getbit(11, NULL);
+----
+NULL
+