You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "alamb (via GitHub)" <gi...@apache.org> on 2023/04/23 11:58:31 UTC

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #6098: Use arrow kernels for bitwise operations

alamb commented on code in PR #6098:
URL: https://github.com/apache/arrow-datafusion/pull/6098#discussion_r1174566528


##########
datafusion/physical-expr/src/expressions/binary.rs:
##########
@@ -5117,31 +5117,31 @@ mod tests {
         let left = Arc::new(Int32Array::from(vec![Some(12), None, Some(11)])) as ArrayRef;
         let right =
             Arc::new(Int32Array::from(vec![Some(1), Some(3), Some(7)])) as ArrayRef;
-        let mut result = bitwise_and(left.clone(), right.clone())?;
+        let mut result = bitwise_and_dyn(left.clone(), right.clone())?;

Review Comment:
   renamed these kernels to match the naming scheme in arrow-rs (where `_dyn` is used to refer to kernels that take `ArrayRef`



##########
datafusion/physical-expr/src/expressions/binary/kernels.rs:
##########
@@ -56,41 +60,64 @@ macro_rules! binary_bitwise_array_scalar {
     }};
 }
 
-pub(crate) fn bitwise_and(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
-    match &left.data_type() {
-        DataType::Int8 => {
-            binary_bitwise_array_op!(left, right, |a: i8, b: i8| a & b, Int8Array)
-        }
-        DataType::Int16 => {
-            binary_bitwise_array_op!(left, right, |a: i16, b: i16| a & b, Int16Array)
-        }
-        DataType::Int32 => {
-            binary_bitwise_array_op!(left, right, |a: i32, b: i32| a & b, Int32Array)
-        }
-        DataType::Int64 => {
-            binary_bitwise_array_op!(left, right, |a: i64, b: i64| a & b, Int64Array)
-        }
-        DataType::UInt8 => {
-            binary_bitwise_array_op!(left, right, |a: u8, b: u8| a & b, UInt8Array)
-        }
-        DataType::UInt16 => {
-            binary_bitwise_array_op!(left, right, |a: u16, b: u16| a & b, UInt16Array)
-        }
-        DataType::UInt32 => {
-            binary_bitwise_array_op!(left, right, |a: u32, b: u32| a & b, UInt32Array)
-        }
-        DataType::UInt64 => {
-            binary_bitwise_array_op!(left, right, |a: u64, b: u64| a & b, UInt64Array)
+/// Downcasts $LEFT and $RIGHT to $ARRAY_TYPE and then calls $KERNEL($LEFT, $RIGHT)
+macro_rules! call_bitwise_kernel {
+    ($LEFT:expr, $RIGHT:expr, $KERNEL:expr, $ARRAY_TYPE:ident) => {{
+        let left = $LEFT.as_any().downcast_ref::<$ARRAY_TYPE>().unwrap();
+        let right = $RIGHT.as_any().downcast_ref::<$ARRAY_TYPE>().unwrap();
+        let result: $ARRAY_TYPE = $KERNEL(left, right)?;
+        Ok(Arc::new(result))
+    }};
+}
+
+/// Creates a $FUNC(left: ArrayRef, right: ArrayRef) that
+/// downcasts left / right to the appropriate integral type and calls the kernel
+macro_rules! create_dyn_kernel {
+    ($FUNC:ident, $KERNEL:ident) => {
+        pub(crate) fn $FUNC(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
+            match &left.data_type() {
+                DataType::Int8 => {
+                    call_bitwise_kernel!(left, right, $KERNEL, Int8Array)
+                }
+                DataType::Int16 => {
+                    call_bitwise_kernel!(left, right, $KERNEL, Int16Array)
+                }
+                DataType::Int32 => {
+                    call_bitwise_kernel!(left, right, $KERNEL, Int32Array)
+                }
+                DataType::Int64 => {
+                    call_bitwise_kernel!(left, right, $KERNEL, Int64Array)
+                }
+                DataType::UInt8 => {
+                    call_bitwise_kernel!(left, right, $KERNEL, UInt8Array)
+                }
+                DataType::UInt16 => {
+                    call_bitwise_kernel!(left, right, $KERNEL, UInt16Array)
+                }
+                DataType::UInt32 => {
+                    call_bitwise_kernel!(left, right, $KERNEL, UInt32Array)
+                }
+                DataType::UInt64 => {
+                    call_bitwise_kernel!(left, right, $KERNEL, UInt64Array)
+                }
+                other => Err(DataFusionError::Internal(format!(
+                    "Data type {:?} not supported for binary operation '{}' on dyn arrays",
+                    other,
+                    stringify!($KERNEL),
+                ))),
+            }
         }
-        other => Err(DataFusionError::Internal(format!(
-            "Data type {:?} not supported for binary operation '{}' on dyn arrays",
-            other,
-            Operator::BitwiseAnd
-        ))),
-    }
+    };
 }
 
-pub(crate) fn bitwise_shift_right(left: ArrayRef, right: ArrayRef) -> Result<ArrayRef> {
+create_dyn_kernel!(bitwise_or_dyn, bitwise_or);
+create_dyn_kernel!(bitwise_xor_dyn, bitwise_xor);
+create_dyn_kernel!(bitwise_and_dyn, bitwise_and);
+
+pub(crate) fn bitwise_shift_right_dyn(

Review Comment:
   Sadly there are no shift_right or shift_left in arrows



-- 
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-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org