Skip to content

Commit ca3816b

Browse files
kazantsev-maksimKazantsev Maksim
andauthored
Impl spark bit not function (#18018)
* Impl spark bit not function * Impl spark bit not function * Fix format * Fix format * Fix Clippy warnings * Rename func * Add .slt tests * Fix fmt --------- Co-authored-by: Kazantsev Maksim <mn.kazantsev@gmail.com>
1 parent ca2585e commit ca3816b

3 files changed

Lines changed: 318 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
use arrow::compute::kernels::bitwise;
19+
use arrow::datatypes::{Int16Type, Int32Type, Int64Type, Int8Type};
20+
use arrow::{array::*, datatypes::DataType};
21+
use datafusion_common::{plan_err, Result};
22+
use datafusion_expr::{ColumnarValue, TypeSignature, Volatility};
23+
use datafusion_expr::{ScalarFunctionArgs, ScalarUDFImpl, Signature};
24+
use datafusion_functions::utils::make_scalar_function;
25+
use std::{any::Any, sync::Arc};
26+
27+
#[derive(Debug, PartialEq, Eq, Hash)]
28+
pub struct SparkBitwiseNot {
29+
signature: Signature,
30+
}
31+
32+
impl Default for SparkBitwiseNot {
33+
fn default() -> Self {
34+
Self::new()
35+
}
36+
}
37+
38+
impl SparkBitwiseNot {
39+
pub fn new() -> Self {
40+
Self {
41+
signature: Signature::one_of(
42+
vec![
43+
TypeSignature::Exact(vec![DataType::Int8]),
44+
TypeSignature::Exact(vec![DataType::Int16]),
45+
TypeSignature::Exact(vec![DataType::Int32]),
46+
TypeSignature::Exact(vec![DataType::Int64]),
47+
],
48+
Volatility::Immutable,
49+
),
50+
}
51+
}
52+
}
53+
54+
impl ScalarUDFImpl for SparkBitwiseNot {
55+
fn as_any(&self) -> &dyn Any {
56+
self
57+
}
58+
59+
fn name(&self) -> &str {
60+
"bitwise_not"
61+
}
62+
63+
fn signature(&self) -> &Signature {
64+
&self.signature
65+
}
66+
67+
fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
68+
Ok(arg_types[0].clone())
69+
}
70+
71+
fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> {
72+
if args.args.len() != 1 {
73+
return plan_err!("bitwise_not expects exactly 1 argument");
74+
}
75+
make_scalar_function(spark_bitwise_not, vec![])(&args.args)
76+
}
77+
}
78+
79+
pub fn spark_bitwise_not(args: &[ArrayRef]) -> Result<ArrayRef> {
80+
let array = args[0].as_ref();
81+
match array.data_type() {
82+
DataType::Int8 => {
83+
let result: Int8Array =
84+
bitwise::bitwise_not(array.as_primitive::<Int8Type>())?;
85+
Ok(Arc::new(result))
86+
}
87+
DataType::Int16 => {
88+
let result: Int16Array =
89+
bitwise::bitwise_not(array.as_primitive::<Int16Type>())?;
90+
Ok(Arc::new(result))
91+
}
92+
DataType::Int32 => {
93+
let result: Int32Array =
94+
bitwise::bitwise_not(array.as_primitive::<Int32Type>())?;
95+
Ok(Arc::new(result))
96+
}
97+
DataType::Int64 => {
98+
let result: Int64Array =
99+
bitwise::bitwise_not(array.as_primitive::<Int64Type>())?;
100+
Ok(Arc::new(result))
101+
}
102+
_ => {
103+
plan_err!(
104+
"bitwise_not function does not support data type: {}",
105+
array.data_type()
106+
)
107+
}
108+
}
109+
}

datafusion/spark/src/function/bitwise/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
pub mod bit_count;
1919
pub mod bit_get;
2020
pub mod bit_shift;
21+
pub mod bitwise_not;
2122

2223
use datafusion_expr::ScalarUDF;
2324
use datafusion_functions::make_udf_function;
@@ -28,6 +29,7 @@ make_udf_function!(bit_shift::SparkShiftRight, shiftright);
2829
make_udf_function!(bit_shift::SparkShiftRightUnsigned, shiftrightunsigned);
2930
make_udf_function!(bit_get::SparkBitGet, bit_get);
3031
make_udf_function!(bit_count::SparkBitCount, bit_count);
32+
make_udf_function!(bitwise_not::SparkBitwiseNot, bitwise_not);
3133

3234
pub mod expr_fn {
3335
use datafusion_functions::export_functions;
@@ -38,6 +40,11 @@ pub mod expr_fn {
3840
"Returns the number of bits set in the binary representation of the argument.",
3941
col
4042
));
43+
export_functions!((
44+
bitwise_not,
45+
"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.",
46+
col
47+
));
4148
export_functions!((
4249
shiftleft,
4350
"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)).",
@@ -59,6 +66,7 @@ pub fn functions() -> Vec<Arc<ScalarUDF>> {
5966
vec![
6067
bit_get(),
6168
bit_count(),
69+
bitwise_not(),
6270
shiftleft(),
6371
shiftright(),
6472
shiftrightunsigned(),
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
# This file was originally created by a porting script from:
19+
# https://github.com/lakehq/sail/tree/43b6ed8221de5c4c4adbedbb267ae1351158b43c/crates/sail-spark-connect/tests/gold_data/function
20+
# This file is part of the implementation of the datafusion-spark function library.
21+
# For more information, please see:
22+
# https://github.com/apache/datafusion/issues/15914
23+
24+
## Original Query: SELECT bitwise_not(0);
25+
## PySpark 3.5.5 Result: {'bitwise_not(0)': -1, 'typeof(bitwise_not(0))': 'int', 'typeof(0)': 'int'}
26+
27+
# Basic tests with different integer types
28+
query I
29+
SELECT bitwise_not(0::int);
30+
----
31+
-1
32+
33+
query I
34+
SELECT bitwise_not(1::int);
35+
----
36+
-2
37+
38+
query I
39+
SELECT bitwise_not(7::int);
40+
----
41+
-8
42+
43+
query I
44+
SELECT bitwise_not(15::int);
45+
----
46+
-16
47+
48+
query I
49+
SELECT bitwise_not(255::int);
50+
----
51+
-256
52+
53+
query I
54+
SELECT bitwise_not(1023::int);
55+
----
56+
-1024
57+
58+
# Tests with negative numbers (two's complement)
59+
query I
60+
SELECT bitwise_not(-1::int);
61+
----
62+
0
63+
64+
query I
65+
SELECT bitwise_not(-2::int);
66+
----
67+
1
68+
69+
query I
70+
SELECT bitwise_not(-3::int);
71+
----
72+
2
73+
74+
# Tests with different integer types
75+
query I
76+
SELECT bitwise_not(arrow_cast(0, 'Int8'));
77+
----
78+
-1
79+
80+
query I
81+
SELECT bitwise_not(arrow_cast(15, 'Int8'));
82+
----
83+
-16
84+
85+
query I
86+
SELECT bitwise_not(arrow_cast(-1, 'Int8'));
87+
----
88+
0
89+
90+
query I
91+
SELECT bitwise_not(arrow_cast(0, 'Int16'));
92+
----
93+
-1
94+
95+
query I
96+
SELECT bitwise_not(arrow_cast(255, 'Int16'));
97+
----
98+
-256
99+
100+
query I
101+
SELECT bitwise_not(arrow_cast(-1, 'Int16'));
102+
----
103+
0
104+
105+
query I
106+
SELECT bitwise_not(arrow_cast(0, 'Int32'));
107+
----
108+
-1
109+
110+
query I
111+
SELECT bitwise_not(arrow_cast(255, 'Int32'));
112+
----
113+
-256
114+
115+
query I
116+
SELECT bitwise_not(arrow_cast(-1, 'Int32'));
117+
----
118+
0
119+
120+
query I
121+
SELECT bitwise_not(arrow_cast(0, 'Int64'));
122+
----
123+
-1
124+
125+
query I
126+
SELECT bitwise_not(arrow_cast(255, 'Int64'));
127+
----
128+
-256
129+
130+
query I
131+
SELECT bitwise_not(arrow_cast(-1, 'Int64'));
132+
----
133+
0
134+
135+
# Tests with NULL values
136+
query I
137+
SELECT bitwise_not(arrow_cast(NULL, 'Int32'));
138+
----
139+
NULL
140+
141+
query I
142+
SELECT bitwise_not(arrow_cast(NULL, 'Int8'));
143+
----
144+
NULL
145+
146+
query I
147+
SELECT bitwise_not(arrow_cast(NULL, 'Int64'));
148+
----
149+
NULL
150+
151+
# Tests with edge cases
152+
query I
153+
SELECT bitwise_not(arrow_cast(0, 'Int32')) as zero_not;
154+
----
155+
-1
156+
157+
query I
158+
SELECT bitwise_not(arrow_cast(1, 'Int32')) as one_not;
159+
----
160+
-2
161+
162+
query I
163+
SELECT bitwise_not(arrow_cast(2, 'Int32')) as two_not;
164+
----
165+
-3
166+
167+
query I
168+
SELECT bitwise_not(arrow_cast(3, 'Int32')) as three_not;
169+
----
170+
-4
171+
172+
query I
173+
SELECT bitwise_not(arrow_cast(4, 'Int32')) as four_not;
174+
----
175+
-5
176+
177+
query I
178+
SELECT bitwise_not(arrow_cast(5, 'Int32')) as five_not;
179+
----
180+
-6
181+
182+
# Tests with large numbers
183+
query I
184+
SELECT bitwise_not(arrow_cast(2147483647, 'Int32'));
185+
----
186+
-2147483648
187+
188+
query I
189+
SELECT bitwise_not(arrow_cast(-2147483648, 'Int32'));
190+
----
191+
2147483647
192+
193+
query I
194+
SELECT bitwise_not(arrow_cast(9223372036854775807, 'Int64'));
195+
----
196+
-9223372036854775808
197+
198+
query I
199+
SELECT bitwise_not(arrow_cast(-9223372036854775808, 'Int64'));
200+
----
201+
9223372036854775807

0 commit comments

Comments
 (0)