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

[GitHub] [arrow-datafusion] Jimexist commented on a diff in pull request #6527: [built-in function] add greatest() and least() SQL functions

Jimexist commented on code in PR #6527:
URL: https://github.com/apache/arrow-datafusion/pull/6527#discussion_r1214417633


##########
datafusion/physical-expr/src/comparison_expressions.rs:
##########
@@ -0,0 +1,162 @@
+// 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.
+
+// Some of these functions reference the Postgres documentation
+// or implementation to ensure compatibility and are subject to
+// the Postgres license.
+
+//! Comparison expressions
+
+use arrow::datatypes::DataType;
+use datafusion_common::scalar::ScalarValue;
+use datafusion_common::{DataFusionError, Result};
+use datafusion_expr::ColumnarValue;
+
+#[derive(Debug, Clone, PartialEq)]
+enum ComparisonOperator {
+    Greatest,
+    Least,
+}
+
+macro_rules! compare_scalar_typed {
+    ($op:expr, $args:expr, $data_type:ident) => {{
+        let value = $args
+            .iter()
+            .filter_map(|scalar| match scalar {
+                ScalarValue::$data_type(v) => v.clone(),
+                _ => panic!("Impossibly got non-scalar values"),
+            })
+            .reduce(|a, b| match $op {
+                ComparisonOperator::Greatest => a.max(b),
+                ComparisonOperator::Least => a.min(b),
+            });
+        ScalarValue::$data_type(value)
+    }};
+}
+
+/// Evaluate a greatest or least function for the case when all arguments are scalars
+fn compare_scalars(
+    data_type: DataType,
+    op: ComparisonOperator,
+    args: &[ScalarValue],
+) -> ScalarValue {
+    match data_type {
+        DataType::Boolean => compare_scalar_typed!(op, args, Boolean),
+        DataType::Int8 => compare_scalar_typed!(op, args, Int8),
+        DataType::Int16 => compare_scalar_typed!(op, args, Int16),
+        DataType::Int32 => compare_scalar_typed!(op, args, Int32),
+        DataType::Int64 => compare_scalar_typed!(op, args, Int64),
+        DataType::UInt8 => compare_scalar_typed!(op, args, UInt8),
+        DataType::UInt16 => compare_scalar_typed!(op, args, UInt16),
+        DataType::UInt32 => compare_scalar_typed!(op, args, UInt32),
+        DataType::UInt64 => compare_scalar_typed!(op, args, UInt64),
+        DataType::Float32 => compare_scalar_typed!(op, args, Float32),
+        DataType::Float64 => compare_scalar_typed!(op, args, Float64),
+        DataType::Utf8 => compare_scalar_typed!(op, args, Utf8),
+        DataType::LargeUtf8 => compare_scalar_typed!(op, args, LargeUtf8),
+        _ => panic!("Unsupported data type for comparison: {:?}", data_type),
+    }
+}
+
+/// Evaluate a greatest or least function
+fn compare(op: ComparisonOperator, args: &[ColumnarValue]) -> Result<ColumnarValue> {
+    if args.is_empty() {
+        return Err(DataFusionError::Internal(format!(
+            "{:?} expressions require at least one argument",
+            op
+        )));
+    } else if args.len() == 1 {
+        return Ok(args[0].clone());
+    }
+
+    let args_types = args
+        .iter()
+        .map(|arg| match arg {
+            ColumnarValue::Array(array) => array.data_type().clone(),
+            ColumnarValue::Scalar(scalar) => scalar.get_datatype(),
+        })
+        .collect::<Vec<_>>();
+
+    if args_types.iter().any(|t| t != &args_types[0]) {
+        return Err(DataFusionError::Internal(format!(
+            "{:?} expressions require all arguments to be of the same type",
+            op
+        )));
+    }
+
+    let all_scalars = args.iter().all(|arg| match arg {
+        ColumnarValue::Array(_) => false,
+        ColumnarValue::Scalar(_) => true,
+    });
+
+    if all_scalars {
+        let args: Vec<_> = args
+            .iter()
+            .map(|arg| match arg {
+                ColumnarValue::Array(_) => {
+                    panic!("Internal error: all arguments should be scalars")
+                }
+                ColumnarValue::Scalar(scalar) => scalar.clone(),
+            })
+            .collect();
+        Ok(ColumnarValue::Scalar(compare_scalars(
+            args_types[0].clone(),
+            op,
+            &args,
+        )))
+    } else {
+        Err(DataFusionError::NotImplemented(format!(
+            "{:?} expressions are not implemented for arrays yet as we need to update arrow kernels",

Review Comment:
   maybe track in this issue:
   - https://github.com/apache/arrow-rs/issues/4347



-- 
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