Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions datafusion/spark/src/function/bitwise/bitwise_not.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// 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::compute::kernels::bitwise;
use arrow::datatypes::{Int16Type, Int32Type, Int64Type, Int8Type};
use arrow::{array::*, datatypes::DataType};
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)]
pub struct SparkBitwiseNot {
signature: Signature,
}

impl Default for SparkBitwiseNot {
fn default() -> Self {
Self::new()
}
}

impl SparkBitwiseNot {
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]),
],
Volatility::Immutable,
),
}
}
}

impl ScalarUDFImpl for SparkBitwiseNot {
fn as_any(&self) -> &dyn Any {
self
}

fn name(&self) -> &str {
"bitwise_not"
}

fn signature(&self) -> &Signature {
&self.signature
}

fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
Ok(arg_types[0].clone())
}

fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
if args.args.len() != 1 {
return plan_err!("bitwise_not expects exactly 1 argument");
}
make_scalar_function(spark_bitwise_not, vec![])(&args.args)
}
}

pub fn spark_bitwise_not(args: &[ArrayRef]) -> Result<ArrayRef> {
let array = args[0].as_ref();
match array.data_type() {
DataType::Int8 => {
let result: Int8Array =
bitwise::bitwise_not(array.as_primitive::<Int8Type>())?;
Ok(Arc::new(result))
}
DataType::Int16 => {
let result: Int16Array =
bitwise::bitwise_not(array.as_primitive::<Int16Type>())?;
Ok(Arc::new(result))
}
DataType::Int32 => {
let result: Int32Array =
bitwise::bitwise_not(array.as_primitive::<Int32Type>())?;
Ok(Arc::new(result))
}
DataType::Int64 => {
let result: Int64Array =
bitwise::bitwise_not(array.as_primitive::<Int64Type>())?;
Ok(Arc::new(result))
}
_ => {
plan_err!(
"bitwise_not function does not support data type: {}",
array.data_type()
)
}
}
}
8 changes: 8 additions & 0 deletions datafusion/spark/src/function/bitwise/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
pub mod bit_count;
pub mod bit_get;
pub mod bit_shift;
pub mod bitwise_not;

use datafusion_expr::ScalarUDF;
use datafusion_functions::make_udf_function;
Expand All @@ -28,6 +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!(bitwise_not::SparkBitwiseNot, bitwise_not);

pub mod expr_fn {
use datafusion_functions::export_functions;
Expand All @@ -38,6 +40,11 @@ pub mod expr_fn {
"Returns the number of bits set in the binary representation of the argument.",
col
));
export_functions!((
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
));
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)).",
Expand All @@ -59,6 +66,7 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
vec![
bit_get(),
bit_count(),
bitwise_not(),
shiftleft(),
shiftright(),
shiftrightunsigned(),
Expand Down
201 changes: 201 additions & 0 deletions datafusion/sqllogictest/test_files/spark/bitwise/bitwise_not.slt
Original file line number Diff line number Diff line change
@@ -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