From 12324e1229d19132571388c7d4988937c6969f41 Mon Sep 17 00:00:00 2001 From: Kazantsev Maksim Date: Tue, 12 Aug 2025 22:08:25 +0400 Subject: [PATCH 1/8] Impl spark bit not function --- .../spark/src/function/bitwise/bit_not.rs | 229 ++++++++++++++++++ datafusion/spark/src/function/bitwise/mod.rs | 5 +- 2 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 datafusion/spark/src/function/bitwise/bit_not.rs diff --git a/datafusion/spark/src/function/bitwise/bit_not.rs b/datafusion/spark/src/function/bitwise/bit_not.rs new file mode 100644 index 000000000000..ca5815f933b6 --- /dev/null +++ b/datafusion/spark/src/function/bitwise/bit_not.rs @@ -0,0 +1,229 @@ +// 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 arrow::array::{ArrayRef, AsArray, Int16Array, Int32Array, Int64Array, Int8Array}; +use arrow::datatypes::{DataType, Int16Type, Int32Type, Int64Type, Int8Type}; +use datafusion_common::{exec_err, plan_err, Result}; +use datafusion_expr::{ + ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, +}; +use datafusion_functions::utils::make_scalar_function; +use std::any::Any; +use std::sync::Arc; + +#[derive(Debug)] +pub struct SparkBitNot { + signature: Signature, + aliases: Vec, +} + +impl Default for SparkBitNot { + fn default() -> Self { + Self::new() + } +} + +impl SparkBitNot { + pub fn new() -> Self { + Self { + signature: Signature::user_defined(Volatility::Immutable), + aliases: vec![], + } + } +} + +impl ScalarUDFImpl for SparkBitNot { + fn as_any(&self) -> &dyn Any { + self + } + + fn name(&self) -> &str { + "bit_not" + } + + fn aliases(&self) -> &[String] { + &self.aliases + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, arg_types: &[DataType]) -> Result { + Ok(match arg_types[0] { + DataType::Int8 => DataType::Int8, + DataType::Int16 => DataType::Int16, + DataType::Int32 => DataType::Int32, + DataType::Int64 => DataType::Int64, + DataType::Null => DataType::Null, + _ => { + return exec_err!( + "{} function can only accept integral arrays", + self.name() + ) + } + }) + } + + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result { + if args.args.len() != 1 { + return plan_err!("bit_not expects exactly 1 argument"); + } + make_scalar_function(spark_bit_not, vec![])(&args.args) + } +} + +macro_rules! bit_not_op { + ($OPERAND:expr, $PT:ident, $AT:ident) => {{ + let result: $AT = $OPERAND + .as_primitive::<$PT>() + .iter() + .map(|x| x.map(|y| !y)) + .collect(); + Ok(Arc::new(result)) + }}; +} + +pub fn spark_bit_not(value_array: &[ArrayRef]) -> Result { + let value_array = value_array[0].as_ref(); + match value_array.data_type() { + DataType::Int8 => bit_not_op!(value_array, Int8Type, Int8Array), + DataType::Int16 => bit_not_op!(value_array, Int16Type, Int16Array), + DataType::Int32 => bit_not_op!(value_array, Int32Type, Int32Array), + DataType::Int64 => bit_not_op!(value_array, Int64Type, Int64Array), + _ => exec_err!("bit_not can't be evaluated because the expression's type is {:?}, not signed int", value_array.data_type()), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_spark_bit_not_int32() -> Result<()> { + let int32_array = Int32Array::from(vec![ + Some(1), + Some(2), + None, + Some(12345), + Some(89), + Some(-3456), + ]); + let expected = &Int32Array::from(vec![ + Some(-2), + Some(-3), + None, + Some(-12346), + Some(-90), + Some(3455), + ]); + + let result = spark_bit_not(&[Arc::new(int32_array)])?; + let result = result.as_primitive::(); + + assert_eq!(result, expected); + + Ok(()) + } + + #[test] + fn test_spark_bit_not_int8() -> Result<()> { + let int8_array = Int8Array::from(vec![ + Some(0), + Some(127), + Some(-128), + None, + Some(42), + Some(-73), + Some(1), + ]); + let expected = &Int8Array::from(vec![ + Some(-1), + Some(-128), + Some(127), + None, + Some(-43), + Some(72), + Some(-2), + ]); + + let result = spark_bit_not(&[Arc::new(int8_array)])?; + let result = result.as_primitive::(); + + assert_eq!(result, expected); + + Ok(()) + } + + #[test] + fn test_spark_bit_not_int16() -> Result<()> { + let int16_array = Int16Array::from(vec![ + Some(0), + Some(32767), + Some(-32768), + None, + Some(12345), + Some(-24680), + Some(0x5555), + ]); + let expected = &Int16Array::from(vec![ + Some(-1), + Some(-32768), + Some(32767), + None, + Some(-12346), + Some(24679), + Some(-0x5556), + ]); + + let result = spark_bit_not(&[Arc::new(int16_array)])?; + let result = result.as_primitive::(); + + assert_eq!(result, expected); + + Ok(()) + } + + #[test] + fn test_spark_bit_not_int64() -> Result<()> { + let int64_array = Int64Array::from(vec![ + Some(0), + Some(i64::MAX), + Some(i64::MIN), + None, + Some(1234567890123456), + Some(-9876543210987654), + Some(0x5555555555555555), + ]); + let expected = &Int64Array::from(vec![ + Some(-1), + Some(i64::MIN), + Some(i64::MAX), + None, + Some(-1234567890123457), + Some(9876543210987653), + Some(-0x5555555555555556), + ]); + + let result = spark_bit_not(&[Arc::new(int64_array)])?; + let result = result.as_primitive::(); + + assert_eq!(result, expected); + + Ok(()) + } +} diff --git a/datafusion/spark/src/function/bitwise/mod.rs b/datafusion/spark/src/function/bitwise/mod.rs index f8131176ff31..7d52e9485bdc 100644 --- a/datafusion/spark/src/function/bitwise/mod.rs +++ b/datafusion/spark/src/function/bitwise/mod.rs @@ -17,6 +17,7 @@ pub mod bit_count; pub mod bit_get; +pub mod bit_not; use datafusion_expr::ScalarUDF; use datafusion_functions::make_udf_function; @@ -24,6 +25,7 @@ use std::sync::Arc; make_udf_function!(bit_get::SparkBitGet, bit_get); make_udf_function!(bit_count::SparkBitCount, bit_count); +make_udf_function!(bit_not::SparkBitNot, bit_not); pub mod expr_fn { use datafusion_functions::export_functions; @@ -34,8 +36,9 @@ pub mod expr_fn { "Returns the number of bits set in the binary representation of the argument.", col )); + export_functions!((bit_not, "Returns the result of a bitwise negation operation on the argument, where each bit in the binary representation is flipped, following two's complement arithmetic for signed integers.", col)); } pub fn functions() -> Vec> { - vec![bit_get(), bit_count()] + vec![bit_get(), bit_count(), bit_not()] } From df815d38cf88e91baa6ace36fb77c80defff37f0 Mon Sep 17 00:00:00 2001 From: Kazantsev Maksim Date: Sat, 11 Oct 2025 19:47:53 +0400 Subject: [PATCH 2/8] Impl spark bit not function --- .../spark/src/function/bitwise/bit_not.rs | 214 ++++-------------- 1 file changed, 45 insertions(+), 169 deletions(-) diff --git a/datafusion/spark/src/function/bitwise/bit_not.rs b/datafusion/spark/src/function/bitwise/bit_not.rs index ca5815f933b6..dfc5ab6aa33a 100644 --- a/datafusion/spark/src/function/bitwise/bit_not.rs +++ b/datafusion/spark/src/function/bitwise/bit_not.rs @@ -15,20 +15,18 @@ // specific language governing permissions and limitations // under the License. -use arrow::array::{ArrayRef, AsArray, Int16Array, Int32Array, Int64Array, Int8Array}; -use arrow::datatypes::{DataType, Int16Type, Int32Type, Int64Type, Int8Type}; -use datafusion_common::{exec_err, plan_err, Result}; -use datafusion_expr::{ - ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, -}; -use datafusion_functions::utils::make_scalar_function; -use std::any::Any; -use std::sync::Arc; - -#[derive(Debug)] +use arrow::compute::kernels::bitwise; +use arrow::datatypes::{Int16Type, Int32Type, Int64Type, Int8Type}; +use arrow::{array::*, datatypes::DataType}; +use datafusion::common::{plan_err, Result}; +use datafusion::functions::utils::make_scalar_function; +use datafusion::logical_expr::{ColumnarValue, TypeSignature, Volatility}; +use datafusion::logical_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature}; +use std::{any::Any, sync::Arc}; + +#[derive(Debug, PartialEq, Eq, Hash)] pub struct SparkBitNot { signature: Signature, - aliases: Vec, } impl Default for SparkBitNot { @@ -40,8 +38,15 @@ impl Default for SparkBitNot { impl SparkBitNot { pub fn new() -> Self { Self { - signature: Signature::user_defined(Volatility::Immutable), - aliases: vec![], + 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]), + ], + Volatility::Immutable, + ), } } } @@ -55,28 +60,12 @@ impl ScalarUDFImpl for SparkBitNot { "bit_not" } - fn aliases(&self) -> &[String] { - &self.aliases - } - fn signature(&self) -> &Signature { &self.signature } fn return_type(&self, arg_types: &[DataType]) -> Result { - Ok(match arg_types[0] { - DataType::Int8 => DataType::Int8, - DataType::Int16 => DataType::Int16, - DataType::Int32 => DataType::Int32, - DataType::Int64 => DataType::Int64, - DataType::Null => DataType::Null, - _ => { - return exec_err!( - "{} function can only accept integral arrays", - self.name() - ) - } - }) + Ok(arg_types[0].clone()) } fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result { @@ -87,143 +76,30 @@ impl ScalarUDFImpl for SparkBitNot { } } -macro_rules! bit_not_op { - ($OPERAND:expr, $PT:ident, $AT:ident) => {{ - let result: $AT = $OPERAND - .as_primitive::<$PT>() - .iter() - .map(|x| x.map(|y| !y)) - .collect(); - Ok(Arc::new(result)) - }}; -} - -pub fn spark_bit_not(value_array: &[ArrayRef]) -> Result { - let value_array = value_array[0].as_ref(); - match value_array.data_type() { - DataType::Int8 => bit_not_op!(value_array, Int8Type, Int8Array), - DataType::Int16 => bit_not_op!(value_array, Int16Type, Int16Array), - DataType::Int32 => bit_not_op!(value_array, Int32Type, Int32Array), - DataType::Int64 => bit_not_op!(value_array, Int64Type, Int64Array), - _ => exec_err!("bit_not can't be evaluated because the expression's type is {:?}, not signed int", value_array.data_type()), - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_spark_bit_not_int32() -> Result<()> { - let int32_array = Int32Array::from(vec![ - Some(1), - Some(2), - None, - Some(12345), - Some(89), - Some(-3456), - ]); - let expected = &Int32Array::from(vec![ - Some(-2), - Some(-3), - None, - Some(-12346), - Some(-90), - Some(3455), - ]); - - let result = spark_bit_not(&[Arc::new(int32_array)])?; - let result = result.as_primitive::(); - - assert_eq!(result, expected); - - Ok(()) - } - - #[test] - fn test_spark_bit_not_int8() -> Result<()> { - let int8_array = Int8Array::from(vec![ - Some(0), - Some(127), - Some(-128), - None, - Some(42), - Some(-73), - Some(1), - ]); - let expected = &Int8Array::from(vec![ - Some(-1), - Some(-128), - Some(127), - None, - Some(-43), - Some(72), - Some(-2), - ]); - - let result = spark_bit_not(&[Arc::new(int8_array)])?; - let result = result.as_primitive::(); - - assert_eq!(result, expected); - - Ok(()) - } - - #[test] - fn test_spark_bit_not_int16() -> Result<()> { - let int16_array = Int16Array::from(vec![ - Some(0), - Some(32767), - Some(-32768), - None, - Some(12345), - Some(-24680), - Some(0x5555), - ]); - let expected = &Int16Array::from(vec![ - Some(-1), - Some(-32768), - Some(32767), - None, - Some(-12346), - Some(24679), - Some(-0x5556), - ]); - - let result = spark_bit_not(&[Arc::new(int16_array)])?; - let result = result.as_primitive::(); - - assert_eq!(result, expected); - - Ok(()) - } - - #[test] - fn test_spark_bit_not_int64() -> Result<()> { - let int64_array = Int64Array::from(vec![ - Some(0), - Some(i64::MAX), - Some(i64::MIN), - None, - Some(1234567890123456), - Some(-9876543210987654), - Some(0x5555555555555555), - ]); - let expected = &Int64Array::from(vec![ - Some(-1), - Some(i64::MIN), - Some(i64::MAX), - None, - Some(-1234567890123457), - Some(9876543210987653), - Some(-0x5555555555555556), - ]); - - let result = spark_bit_not(&[Arc::new(int64_array)])?; - let result = result.as_primitive::(); - - assert_eq!(result, expected); - - Ok(()) +pub fn spark_bit_not(args: &[ArrayRef]) -> Result { + let array = args[0].as_ref(); + match array.data_type() { + DataType::Int8 => { + let result: Int8Array = bitwise::bitwise_not(&array.as_primitive::())?; + Ok(Arc::new(result)) + } + DataType::Int16 => { + let result: Int16Array = bitwise::bitwise_not(&array.as_primitive::())?; + Ok(Arc::new(result)) + } + DataType::Int32 => { + let result: Int32Array = bitwise::bitwise_not(&array.as_primitive::())?; + Ok(Arc::new(result)) + } + DataType::Int64 => { + let result: Int64Array = bitwise::bitwise_not(&array.as_primitive::())?; + Ok(Arc::new(result)) + } + _ => { + plan_err!( + "bit_not function does not support data type: {}", + array.data_type() + ) + } } } From bd578c9de2bcea9ff969dfb649eccab99c0c7ea4 Mon Sep 17 00:00:00 2001 From: Kazantsev Maksim Date: Sat, 11 Oct 2025 19:53:53 +0400 Subject: [PATCH 3/8] Fix format --- datafusion/spark/src/function/bitwise/mod.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/datafusion/spark/src/function/bitwise/mod.rs b/datafusion/spark/src/function/bitwise/mod.rs index 9e06c77b6a3b..3989cd642036 100644 --- a/datafusion/spark/src/function/bitwise/mod.rs +++ b/datafusion/spark/src/function/bitwise/mod.rs @@ -40,7 +40,11 @@ pub mod expr_fn { "Returns the number of bits set in the binary representation of the argument.", col )); - export_functions!((bit_not, "Returns the result of a bitwise negation operation on the argument, where each bit in the binary representation is flipped, following two's complement arithmetic for signed integers.", col)); + export_functions!(( + bit_not, + "Returns the result of a bitwise negation operation on the argument, where each bit in the binary representation is flipped, following two's complement arithmetic for signed integers.", + col + )); export_functions!(( shiftleft, "Shifts the bits of the first argument left by the number of positions specified by the second argument. If the shift amount is negative or greater than or equal to the bit width, it is normalized to the bit width (i.e., pmod(shift, bit_width)).", From c1c569e7049bce079cec549bc2fc8da81c67bdda Mon Sep 17 00:00:00 2001 From: Kazantsev Maksim Date: Sat, 11 Oct 2025 20:58:50 +0400 Subject: [PATCH 4/8] Fix format --- .../spark/src/function/bitwise/bit_not.rs | 20 +++++++++++-------- datafusion/spark/src/function/bitwise/mod.rs | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/datafusion/spark/src/function/bitwise/bit_not.rs b/datafusion/spark/src/function/bitwise/bit_not.rs index dfc5ab6aa33a..dc344d985662 100644 --- a/datafusion/spark/src/function/bitwise/bit_not.rs +++ b/datafusion/spark/src/function/bitwise/bit_not.rs @@ -18,10 +18,10 @@ use arrow::compute::kernels::bitwise; use arrow::datatypes::{Int16Type, Int32Type, Int64Type, Int8Type}; use arrow::{array::*, datatypes::DataType}; -use datafusion::common::{plan_err, Result}; -use datafusion::functions::utils::make_scalar_function; -use datafusion::logical_expr::{ColumnarValue, TypeSignature, Volatility}; -use datafusion::logical_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature}; +use datafusion_common::{plan_err, Result}; +use datafusion_expr::{ColumnarValue, TypeSignature, Volatility}; +use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature}; +use datafusion_functions::utils::make_scalar_function; use std::{any::Any, sync::Arc}; #[derive(Debug, PartialEq, Eq, Hash)] @@ -80,19 +80,23 @@ pub fn spark_bit_not(args: &[ArrayRef]) -> Result { let array = args[0].as_ref(); match array.data_type() { DataType::Int8 => { - let result: Int8Array = bitwise::bitwise_not(&array.as_primitive::())?; + let result: Int8Array = + bitwise::bitwise_not(&array.as_primitive::())?; Ok(Arc::new(result)) } DataType::Int16 => { - let result: Int16Array = bitwise::bitwise_not(&array.as_primitive::())?; + let result: Int16Array = + bitwise::bitwise_not(&array.as_primitive::())?; Ok(Arc::new(result)) } DataType::Int32 => { - let result: Int32Array = bitwise::bitwise_not(&array.as_primitive::())?; + let result: Int32Array = + bitwise::bitwise_not(&array.as_primitive::())?; Ok(Arc::new(result)) } DataType::Int64 => { - let result: Int64Array = bitwise::bitwise_not(&array.as_primitive::())?; + let result: Int64Array = + bitwise::bitwise_not(&array.as_primitive::())?; Ok(Arc::new(result)) } _ => { diff --git a/datafusion/spark/src/function/bitwise/mod.rs b/datafusion/spark/src/function/bitwise/mod.rs index 3989cd642036..98e9f6d82a90 100644 --- a/datafusion/spark/src/function/bitwise/mod.rs +++ b/datafusion/spark/src/function/bitwise/mod.rs @@ -17,8 +17,8 @@ pub mod bit_count; pub mod bit_get; -pub mod bit_shift; pub mod bit_not; +pub mod bit_shift; use datafusion_expr::ScalarUDF; use datafusion_functions::make_udf_function; From 3da6a7adf1b2c940088945b167289dd21054a635 Mon Sep 17 00:00:00 2001 From: Kazantsev Maksim Date: Sun, 12 Oct 2025 14:55:34 +0400 Subject: [PATCH 5/8] Fix Clippy warnings --- datafusion/spark/src/function/bitwise/bit_not.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/datafusion/spark/src/function/bitwise/bit_not.rs b/datafusion/spark/src/function/bitwise/bit_not.rs index dc344d985662..b088a47aca61 100644 --- a/datafusion/spark/src/function/bitwise/bit_not.rs +++ b/datafusion/spark/src/function/bitwise/bit_not.rs @@ -81,22 +81,22 @@ pub fn spark_bit_not(args: &[ArrayRef]) -> Result { match array.data_type() { DataType::Int8 => { let result: Int8Array = - bitwise::bitwise_not(&array.as_primitive::())?; + bitwise::bitwise_not(array.as_primitive::())?; Ok(Arc::new(result)) } DataType::Int16 => { let result: Int16Array = - bitwise::bitwise_not(&array.as_primitive::())?; + bitwise::bitwise_not(array.as_primitive::())?; Ok(Arc::new(result)) } DataType::Int32 => { let result: Int32Array = - bitwise::bitwise_not(&array.as_primitive::())?; + bitwise::bitwise_not(array.as_primitive::())?; Ok(Arc::new(result)) } DataType::Int64 => { let result: Int64Array = - bitwise::bitwise_not(&array.as_primitive::())?; + bitwise::bitwise_not(array.as_primitive::())?; Ok(Arc::new(result)) } _ => { From d126de514ec6340eb669b18a06ec81b693afaa5b Mon Sep 17 00:00:00 2001 From: Kazantsev Maksim Date: Sun, 12 Oct 2025 20:20:02 +0400 Subject: [PATCH 6/8] Rename func --- .../bitwise/{bit_not.rs => bitwise_not.rs} | 18 +++++++++--------- datafusion/spark/src/function/bitwise/mod.rs | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) rename datafusion/spark/src/function/bitwise/{bit_not.rs => bitwise_not.rs} (87%) diff --git a/datafusion/spark/src/function/bitwise/bit_not.rs b/datafusion/spark/src/function/bitwise/bitwise_not.rs similarity index 87% rename from datafusion/spark/src/function/bitwise/bit_not.rs rename to datafusion/spark/src/function/bitwise/bitwise_not.rs index b088a47aca61..2f3fe227833b 100644 --- a/datafusion/spark/src/function/bitwise/bit_not.rs +++ b/datafusion/spark/src/function/bitwise/bitwise_not.rs @@ -25,17 +25,17 @@ use datafusion_functions::utils::make_scalar_function; use std::{any::Any, sync::Arc}; #[derive(Debug, PartialEq, Eq, Hash)] -pub struct SparkBitNot { +pub struct SparkBitwiseNot { signature: Signature, } -impl Default for SparkBitNot { +impl Default for SparkBitwiseNot { fn default() -> Self { Self::new() } } -impl SparkBitNot { +impl SparkBitwiseNot { pub fn new() -> Self { Self { signature: Signature::one_of( @@ -51,13 +51,13 @@ impl SparkBitNot { } } -impl ScalarUDFImpl for SparkBitNot { +impl ScalarUDFImpl for SparkBitwiseNot { fn as_any(&self) -> &dyn Any { self } fn name(&self) -> &str { - "bit_not" + "bitwise_not" } fn signature(&self) -> &Signature { @@ -70,13 +70,13 @@ impl ScalarUDFImpl for SparkBitNot { fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result { if args.args.len() != 1 { - return plan_err!("bit_not expects exactly 1 argument"); + return plan_err!("bitwise_not expects exactly 1 argument"); } - make_scalar_function(spark_bit_not, vec![])(&args.args) + make_scalar_function(spark_bitwise_not, vec![])(&args.args) } } -pub fn spark_bit_not(args: &[ArrayRef]) -> Result { +pub fn spark_bitwise_not(args: &[ArrayRef]) -> Result { let array = args[0].as_ref(); match array.data_type() { DataType::Int8 => { @@ -101,7 +101,7 @@ pub fn spark_bit_not(args: &[ArrayRef]) -> Result { } _ => { plan_err!( - "bit_not function does not support data type: {}", + "bitwise_not function does not support data type: {}", array.data_type() ) } diff --git a/datafusion/spark/src/function/bitwise/mod.rs b/datafusion/spark/src/function/bitwise/mod.rs index 98e9f6d82a90..d719e6a0d06f 100644 --- a/datafusion/spark/src/function/bitwise/mod.rs +++ b/datafusion/spark/src/function/bitwise/mod.rs @@ -17,7 +17,7 @@ pub mod bit_count; pub mod bit_get; -pub mod bit_not; +pub mod bitwise_not; pub mod bit_shift; use datafusion_expr::ScalarUDF; @@ -29,7 +29,7 @@ make_udf_function!(bit_shift::SparkShiftRight, shiftright); make_udf_function!(bit_shift::SparkShiftRightUnsigned, shiftrightunsigned); make_udf_function!(bit_get::SparkBitGet, bit_get); make_udf_function!(bit_count::SparkBitCount, bit_count); -make_udf_function!(bit_not::SparkBitNot, bit_not); +make_udf_function!(bitwise_not::SparkBitwiseNot, bitwise_not); pub mod expr_fn { use datafusion_functions::export_functions; @@ -41,7 +41,7 @@ pub mod expr_fn { col )); export_functions!(( - bit_not, + bitwise_not, "Returns the result of a bitwise negation operation on the argument, where each bit in the binary representation is flipped, following two's complement arithmetic for signed integers.", col )); @@ -66,7 +66,7 @@ pub fn functions() -> Vec> { vec![ bit_get(), bit_count(), - bit_not(), + bitwise_not(), shiftleft(), shiftright(), shiftrightunsigned(), From 082a502f3508b345035b7959c7c29e95edf0f7d0 Mon Sep 17 00:00:00 2001 From: Kazantsev Maksim Date: Mon, 13 Oct 2025 20:29:51 +0400 Subject: [PATCH 7/8] Add .slt tests --- .../test_files/spark/bitwise/bitwise_not.slt | 201 ++++++++++++++++++ 1 file changed, 201 insertions(+) create mode 100644 datafusion/sqllogictest/test_files/spark/bitwise/bitwise_not.slt diff --git a/datafusion/sqllogictest/test_files/spark/bitwise/bitwise_not.slt b/datafusion/sqllogictest/test_files/spark/bitwise/bitwise_not.slt new file mode 100644 index 000000000000..5f51cd68ef94 --- /dev/null +++ b/datafusion/sqllogictest/test_files/spark/bitwise/bitwise_not.slt @@ -0,0 +1,201 @@ +# 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. + +# This file was originally created by a porting script from: +# https://github.com/lakehq/sail/tree/43b6ed8221de5c4c4adbedbb267ae1351158b43c/crates/sail-spark-connect/tests/gold_data/function +# This file is part of the implementation of the datafusion-spark function library. +# For more information, please see: +# https://github.com/apache/datafusion/issues/15914 + +## Original Query: SELECT bitwise_not(0); +## PySpark 3.5.5 Result: {'bitwise_not(0)': -1, 'typeof(bitwise_not(0))': 'int', 'typeof(0)': 'int'} + +# Basic tests with different integer types +query I +SELECT bitwise_not(0::int); +---- +-1 + +query I +SELECT bitwise_not(1::int); +---- +-2 + +query I +SELECT bitwise_not(7::int); +---- +-8 + +query I +SELECT bitwise_not(15::int); +---- +-16 + +query I +SELECT bitwise_not(255::int); +---- +-256 + +query I +SELECT bitwise_not(1023::int); +---- +-1024 + +# Tests with negative numbers (two's complement) +query I +SELECT bitwise_not(-1::int); +---- +0 + +query I +SELECT bitwise_not(-2::int); +---- +1 + +query I +SELECT bitwise_not(-3::int); +---- +2 + +# Tests with different integer types +query I +SELECT bitwise_not(arrow_cast(0, 'Int8')); +---- +-1 + +query I +SELECT bitwise_not(arrow_cast(15, 'Int8')); +---- +-16 + +query I +SELECT bitwise_not(arrow_cast(-1, 'Int8')); +---- +0 + +query I +SELECT bitwise_not(arrow_cast(0, 'Int16')); +---- +-1 + +query I +SELECT bitwise_not(arrow_cast(255, 'Int16')); +---- +-256 + +query I +SELECT bitwise_not(arrow_cast(-1, 'Int16')); +---- +0 + +query I +SELECT bitwise_not(arrow_cast(0, 'Int32')); +---- +-1 + +query I +SELECT bitwise_not(arrow_cast(255, 'Int32')); +---- +-256 + +query I +SELECT bitwise_not(arrow_cast(-1, 'Int32')); +---- +0 + +query I +SELECT bitwise_not(arrow_cast(0, 'Int64')); +---- +-1 + +query I +SELECT bitwise_not(arrow_cast(255, 'Int64')); +---- +-256 + +query I +SELECT bitwise_not(arrow_cast(-1, 'Int64')); +---- +0 + +# Tests with NULL values +query I +SELECT bitwise_not(arrow_cast(NULL, 'Int32')); +---- +NULL + +query I +SELECT bitwise_not(arrow_cast(NULL, 'Int8')); +---- +NULL + +query I +SELECT bitwise_not(arrow_cast(NULL, 'Int64')); +---- +NULL + +# Tests with edge cases +query I +SELECT bitwise_not(arrow_cast(0, 'Int32')) as zero_not; +---- +-1 + +query I +SELECT bitwise_not(arrow_cast(1, 'Int32')) as one_not; +---- +-2 + +query I +SELECT bitwise_not(arrow_cast(2, 'Int32')) as two_not; +---- +-3 + +query I +SELECT bitwise_not(arrow_cast(3, 'Int32')) as three_not; +---- +-4 + +query I +SELECT bitwise_not(arrow_cast(4, 'Int32')) as four_not; +---- +-5 + +query I +SELECT bitwise_not(arrow_cast(5, 'Int32')) as five_not; +---- +-6 + +# Tests with large numbers +query I +SELECT bitwise_not(arrow_cast(2147483647, 'Int32')); +---- +-2147483648 + +query I +SELECT bitwise_not(arrow_cast(-2147483648, 'Int32')); +---- +2147483647 + +query I +SELECT bitwise_not(arrow_cast(9223372036854775807, 'Int64')); +---- +-9223372036854775808 + +query I +SELECT bitwise_not(arrow_cast(-9223372036854775808, 'Int64')); +---- +9223372036854775807 From 286a6a04d0b8c475bf645b818b2c2455263cba69 Mon Sep 17 00:00:00 2001 From: Kazantsev Maksim Date: Tue, 14 Oct 2025 18:07:32 +0400 Subject: [PATCH 8/8] Fix fmt --- datafusion/spark/src/function/bitwise/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/datafusion/spark/src/function/bitwise/mod.rs b/datafusion/spark/src/function/bitwise/mod.rs index d719e6a0d06f..d729a3ddd09a 100644 --- a/datafusion/spark/src/function/bitwise/mod.rs +++ b/datafusion/spark/src/function/bitwise/mod.rs @@ -17,8 +17,8 @@ pub mod bit_count; pub mod bit_get; -pub mod bitwise_not; pub mod bit_shift; +pub mod bitwise_not; use datafusion_expr::ScalarUDF; use datafusion_functions::make_udf_function;