Jefffrey commented on code in PR #17155: URL: https://github.com/apache/datafusion/pull/17155#discussion_r2306234233
########## datafusion/spark/src/function/bitwise/mod.rs: ########## @@ -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)); Review Comment: Is it necessary to add the detail about two's complement here? ########## 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<String>, +} + +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<DataType> { + 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() + ) Review Comment: I feel this should be encoded in the signature? ########## 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<String>, Review Comment: Remove `aliases` field if it's not used ########## 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<String>, +} + +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<DataType> { + 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<ColumnarValue> { + 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<ArrayRef> { + 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()), Review Comment: Why is this function limited to only signed integer 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-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org