Jefffrey commented on code in PR #17013:
URL: https://github.com/apache/datafusion/pull/17013#discussion_r2347125367


##########
datafusion/spark/src/function/bitwise/bit_shift.rs:
##########
@@ -0,0 +1,747 @@
+// 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,
+};
+
+/// Performs a bitwise left shift on each element of the `value` array by the 
corresponding amount in the `shift` array.
+/// The shift amount is normalized to the bit width of the type, matching 
Spark/Java semantics for negative and large shifts.
+///
+/// # Arguments
+/// * `value` - The array of values to shift.
+/// * `shift` - The array of shift amounts (must be Int32).
+///
+/// # Returns
+/// A new array with the shifted values.
+///

Review Comment:
   ```suggestion
   ```
   
   Would prefer to minimize verbosity of documentation where possible 
(applicable for the other similar instances below)



##########
datafusion/spark/src/function/bitwise/bit_shift.rs:
##########
@@ -0,0 +1,747 @@
+// 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,
+};
+
+/// Performs a bitwise left shift on each element of the `value` array by the 
corresponding amount in the `shift` array.
+/// The shift amount is normalized to the bit width of the type, matching 
Spark/Java semantics for negative and large shifts.
+///
+/// # Arguments
+/// * `value` - The array of values to shift.
+/// * `shift` - The array of shift amounts (must be Int32).
+///
+/// # Returns
+/// A new array with the shifted values.
+///
+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)
+}
+
+/// Performs a bitwise right shift on each element of the `value` array by the 
corresponding amount in the `shift` array.
+/// The shift amount is normalized to the bit width of the type, matching 
Spark/Java semantics for negative and large shifts.
+///
+/// # Arguments
+/// * `value` - The array of values to shift.
+/// * `shift` - The array of shift amounts (must be Int32).
+///
+/// # Returns
+/// A new array with the shifted values.
+///
+fn shift_right<T: ArrowPrimitiveType>(
+    value: &PrimitiveArray<T>,
+    shift: &PrimitiveArray<Int32Type>,
+) -> Result<PrimitiveArray<T>>
+where
+    T::Native: ArrowNativeType + std::ops::Shr<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)
+}
+
+/// Trait for performing an unsigned right shift (logical shift right).
+/// This is used to mimic Java's `>>>` operator, which does not exist in Rust.
+/// For unsigned types, this is just the normal right shift.
+/// For signed types, this casts to the unsigned type, shifts, then casts back.
+pub trait UShr<Rhs = Self> {
+    type Output;
+
+    #[must_use]
+    fn ushr(self, rhs: Rhs) -> Self::Output;
+}

Review Comment:
   ```suggestion
   trait UShr<Rhs> {
       fn ushr(self, rhs: Rhs) -> Self;
   }
   ```
   
   Some simplifications
   
   - Since Rhs is always specified, no point having default
   - Output type is always same as Self in implementations so can remove that
   - Don't think must_use attribute is necessary here



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to