matthewmturner commented on a change in pull request #1074:
URL: https://github.com/apache/arrow-rs/pull/1074#discussion_r774611115
##########
File path: arrow/src/compute/kernels/comparison.rs
##########
@@ -898,6 +899,224 @@ pub fn gt_eq_utf8_scalar<OffsetSize:
StringOffsetSizeTrait>(
compare_op_scalar!(left, right, |a, b| a >= b)
}
+macro_rules! dyn_compare_scalar {
+ ($LEFT: expr, $RIGHT: expr, $OP: ident) => {{
+ let right: i128 = $RIGHT.try_into().map_err(|_| {
+ ArrowError::ComputeError(String::from("Can not convert scalar to
i128"))
+ })?;
+ match ($LEFT.data_type(), $RIGHT.get_arrow_type()) {
+ // (DataType::Boolean, DataType::Boolean) => {
+ // typed_cmp_scalar!($LEFT, $RIGHT, BooleanArray, $OP_BOOL)
+ // }
+ (DataType::Int8, DataType::Int8) => {
+ let right: i8 = right.try_into().map_err(|_| {
+ ArrowError::ComputeError(String::from(
+ "Can not convert scalar to i128",
+ ))
+ })?;
+ let left =
+ $LEFT.as_any().downcast_ref::<Int8Array>().ok_or_else(|| {
+ ArrowError::CastError(String::from("Left array cannot
be cast"))
+ })?;
+ $OP::<Int8Type>(left, right)
+ }
+ (DataType::Int16, DataType::Int16) => {
+ let right: i16 = right.try_into().map_err(|_| {
+ ArrowError::ComputeError(String::from(
+ "Can not convert scalar to i128",
+ ))
+ })?;
+ let left =
+ $LEFT.as_any().downcast_ref::<Int16Array>().ok_or_else(|| {
+ ArrowError::CastError(String::from("Left array cannot
be cast"))
+ })?;
+ $OP::<Int16Type>(left, right)
+ }
+ (DataType::Int32, DataType::Int32) => {
+ let right: i32 = right.try_into().map_err(|_| {
+ ArrowError::ComputeError(String::from(
+ "Can not convert scalar to i128",
+ ))
+ })?;
+ let left =
+ $LEFT.as_any().downcast_ref::<Int32Array>().ok_or_else(|| {
+ ArrowError::CastError(String::from("Left array cannot
be cast"))
+ })?;
+ $OP::<Int32Type>(left, right)
+ }
+ (DataType::Int64, DataType::Int64) => {
+ let right: i64 = right.try_into().map_err(|_| {
+ ArrowError::ComputeError(String::from(
+ "Can not convert scalar to i128",
+ ))
+ })?;
+ let left =
+ $LEFT.as_any().downcast_ref::<Int64Array>().ok_or_else(|| {
+ ArrowError::CastError(String::from("Left array cannot
be cast"))
+ })?;
+ $OP::<Int64Type>(left, right)
+ }
+ // (DataType::UInt8, DataType::UInt8) => {
+ // dyn_cmp_scalar!($LEFT, right, UInt8Array, $OP, UInt8Type)
+ // }
+ // (DataType::UInt16, DataType::UInt16) => {
+ // dyn_cmp_scalar!($LEFT, right, UInt16Array, $OP, UInt16Type)
+ // }
+ // (DataType::UInt32, DataType::UInt32) => {
+ // dyn_cmp_scalar!($LEFT, right, UInt32Array, $OP, UInt32Type)
+ // }
+ // (DataType::UInt64, DataType::UInt64) => {
+ // dyn_cmp_scalar!($LEFT, right, UInt64Array, $OP, UInt64Type)
+ // }
+ // (DataType::Float32, DataType::Float32) => {
+ // dyn_cmp_scalar!($LEFT, right, Float32Array, $OP,
Float32Type)
+ // }
+ // (DataType::Float64, DataType::Float64) => {
+ // dyn_cmp_scalar!($LEFT, right, Float64Array, $OP,
Float64Type)
+ // }
+ // (DataType::Utf8, DataType::Utf8) => {
+ // let right: i32 = right.try_into().map_err(|_| {
+ // ArrowError::ComputeError(String::from(
+ // "Can not convert scalar to i128",
+ // ))
+ // })?;
+ // let left =
+ // $LEFT
+ // .as_any()
+ // .downcast_ref::<StringArray>()
+ // .ok_or_else(|| {
+ // ArrowError::CastError(String::from(
+ // "Left array cannot be cast",
+ // ))
+ // })?;
+ // $OP::<Int32Type>(left, right)
+ // }
+ // (DataType::LargeUtf8, DataType::LargeUtf8) => {
+ // dyn_cmp_scalar!($LEFT, $RIGHT, LargeStringArray, $OP, i64)
+ // }
+ (t1, t2) if t1 == t2 => Err(ArrowError::NotYetImplemented(format!(
+ "Comparing arrays of type {} is not yet implemented",
+ t1
+ ))),
+ (t1, t2) => Err(ArrowError::CastError(format!(
+ "Cannot compare an array with a scalar of different type ({}
and {})",
+ t1, t2
+ ))),
+ }
+ }};
+}
+
+pub trait IntoArrowNumericType {
+ // type Arrow: ArrowNumericType<Native = Self>;
+ fn get_arrow_type(&self) -> &DataType;
+}
+
+impl IntoArrowNumericType for i8 {
+ // type Arrow = Int8Type;
+ fn get_arrow_type(&self) -> &DataType {
+ &DataType::Int8
+ }
+}
+
+impl IntoArrowNumericType for i16 {
+ // type Arrow = Int8Type;
+ fn get_arrow_type(&self) -> &DataType {
+ &DataType::Int16
+ }
+}
+
+impl IntoArrowNumericType for i32 {
+ // type Arrow = Int8Type;
+ fn get_arrow_type(&self) -> &DataType {
+ &DataType::Int32
+ }
+}
+
+/// Perform `left == right` operation on an array and a numeric scalar
+/// value. Supports PrimitiveArrays, and DictionaryArrays that have primitive
values
+pub fn eq_dyn_scalar<T>(left: &dyn Array, right: T) -> Result<BooleanArray>
+where
+ T: IntoArrowNumericType + TryInto<i128> + Copy + std::fmt::Debug,
Review comment:
Yes agree, I was starting to think the same. Will try it out.
--
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]