Jefffrey commented on code in PR #17155: URL: https://github.com/apache/datafusion/pull/17155#discussion_r2306246262
########## 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)) + }}; +} Review Comment: It is possible to just use the arrow compute kernel here? https://docs.rs/arrow/latest/arrow/compute/kernels/bitwise/fn.bitwise_not.html -- 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