Jefffrey commented on code in PR #17013: URL: https://github.com/apache/datafusion/pull/17013#discussion_r2308807125
########## datafusion/spark/src/function/bitwise/bit_shift.rs: ########## @@ -0,0 +1,678 @@ +// 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 std::any::Any; +use std::sync::Arc; + +use arrow::array::{ArrayRef, ArrowPrimitiveType, AsArray, PrimitiveArray}; +use arrow::compute; +use arrow::datatypes::{ + ArrowNativeType, DataType, Int32Type, Int64Type, UInt32Type, UInt64Type, +}; +use datafusion_common::{plan_err, Result}; +use datafusion_expr::{ + ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, +}; +use datafusion_functions::utils::make_scalar_function; + +use crate::function::error_utils::{ + invalid_arg_count_exec_err, unsupported_data_type_exec_err, +}; + +fn shift_left<T: ArrowPrimitiveType>( + value: &PrimitiveArray<T>, + shift: &PrimitiveArray<Int32Type>, +) -> Result<PrimitiveArray<T>> +where + T::Native: ArrowNativeType + std::ops::Shl<i32, Output = T::Native>, +{ + let bit_num = (T::Native::get_byte_width() * 8) as i32; + let result = compute::binary::<_, Int32Type, _, _>( + value, + shift, + |value: T::Native, shift: i32| { + let shift = ((shift % bit_num) + bit_num) % bit_num; + value << shift + }, + )?; + Ok(result) +} Review Comment: Right, so the existing kernels don't match the behaviour of how shift is done in Spark; can we provide some kind of documentation here explaining that, as well as explaining the logic behind the shift mechanism here? ```rust let shift = ((shift % bit_num) + bit_num) % bit_num; value << shift ``` To me it's not clearly obvious why these modulos and such are done -- 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