andygrove commented on code in PR #331:
URL: https://github.com/apache/datafusion-spark/pull/331#discussion_r4603381112


##########
crates/spark/src/function/array/array_extrema.rs:
##########
@@ -0,0 +1,780 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+//! Spark-compatible `array_max` and `array_min`.
+
+use super::array_position::contains_map;
+use super::float_normalize::normalize_array;
+use arrow::array::{
+    Array, ArrayRef, ArrowPrimitiveType, AsArray, GenericListArray, 
OffsetSizeTrait,
+    PrimitiveArray, UInt64Array, make_comparator, new_null_array,
+};
+use arrow::compute::{SortOptions, take};
+use arrow::datatypes::{
+    DataType, Date32Type, Date64Type, Decimal128Type, Decimal256Type, Field, 
Float16Type,
+    Float32Type, Float64Type, Int8Type, Int16Type, Int32Type, Int64Type, 
TimeUnit,
+    TimestampMicrosecondType, TimestampMillisecondType, 
TimestampNanosecondType,
+    TimestampSecondType, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
+};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, ScalarValue, exec_err, plan_err};
+use datafusion_expr::{
+    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    Volatility,
+};
+use datafusion_macros::user_doc;
+use std::cmp::Ordering;
+use std::sync::Arc;
+
+/// Spark-compatible `array_max` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#array_max>
+#[user_doc(
+    doc_section(label = "Array Functions"),
+    description = "Returns the maximum element of the array, using Spark's 
ordering. NULL \

Review Comment:
   Done in 1728af8. Both descriptions now say how they differ from DataFusion's 
built-ins, which I checked by running them directly. The built-ins order `-0.0` 
before `0.0` instead of returning the first tied element, sort a nested NULL 
after other values, reject a NULL argument at planning, and fail at execution 
on map elements. Each difference already had an slt test, and those tests now 
note the built-in's result. NaN is the greatest value in both, so it isn't a 
difference.



##########
crates/spark/src/function/array/array_extrema.rs:
##########
@@ -0,0 +1,780 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+//! Spark-compatible `array_max` and `array_min`.
+
+use super::array_position::contains_map;
+use super::float_normalize::normalize_array;
+use arrow::array::{
+    Array, ArrayRef, ArrowPrimitiveType, AsArray, GenericListArray, 
OffsetSizeTrait,
+    PrimitiveArray, UInt64Array, make_comparator, new_null_array,
+};
+use arrow::compute::{SortOptions, take};
+use arrow::datatypes::{
+    DataType, Date32Type, Date64Type, Decimal128Type, Decimal256Type, Field, 
Float16Type,
+    Float32Type, Float64Type, Int8Type, Int16Type, Int32Type, Int64Type, 
TimeUnit,
+    TimestampMicrosecondType, TimestampMillisecondType, 
TimestampNanosecondType,
+    TimestampSecondType, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
+};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, ScalarValue, exec_err, plan_err};
+use datafusion_expr::{
+    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    Volatility,
+};
+use datafusion_macros::user_doc;
+use std::cmp::Ordering;
+use std::sync::Arc;
+
+/// Spark-compatible `array_max` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#array_max>
+#[user_doc(
+    doc_section(label = "Array Functions"),
+    description = "Returns the maximum element of the array, using Spark's 
ordering. NULL \
+        elements are skipped, and an empty array or an array of only NULL 
elements returns \
+        NULL. For floating point values, also inside nested arrays and 
structs, NaN is \
+        greater than every other value, including infinity, and -0.0 is equal 
to 0.0. On a \
+        tie the first of the tied elements is returned as it is, so the sign 
of a zero and the \
+        bits of a NaN are kept. Strings and binary values compare bytewise. 
Arrays compare \
+        element by element and then by length, and structs compare field by 
field; NULLs \
+        nested inside them sort before other values. Map elements, at any 
depth, are an \
+        error, as in Spark.",
+    syntax_example = "array_max(array)",
+    sql_example = r#"```sql
+> SELECT array_max(array(1, 20, NULL, 3));
+20
+```"#,
+    argument(name = "array", description = "Array expression. NULL returns 
NULL.")
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkArrayMax {
+    signature: Signature,
+}
+
+impl Default for SparkArrayMax {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkArrayMax {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::user_defined(Volatility::Immutable),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkArrayMax {
+    fn name(&self) -> &str {
+        "array_max"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        element_type(self.name(), arg_types)
+    }
+
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        coerce_array(self.name(), arg_types)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        invoke(self.name(), args, Ordering::Greater)
+    }
+
+    fn documentation(&self) -> Option<&Documentation> {
+        self.doc()
+    }
+}
+
+/// Spark-compatible `array_min` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#array_min>
+#[user_doc(
+    doc_section(label = "Array Functions"),
+    description = "Returns the minimum element of the array, using Spark's 
ordering. NULL \
+        elements are skipped, and an empty array or an array of only NULL 
elements returns \
+        NULL. For floating point values, also inside nested arrays and 
structs, NaN is \
+        greater than every other value, including infinity, and -0.0 is equal 
to 0.0. On a \
+        tie the first of the tied elements is returned as it is, so the sign 
of a zero and the \
+        bits of a NaN are kept. Strings and binary values compare bytewise. 
Arrays compare \
+        element by element and then by length, and structs compare field by 
field; NULLs \
+        nested inside them sort before other values. Map elements, at any 
depth, are an \
+        error, as in Spark.",
+    syntax_example = "array_min(array)",
+    sql_example = r#"```sql
+> SELECT array_min(array(1, 20, NULL, 3));
+1
+```"#,
+    argument(name = "array", description = "Array expression. NULL returns 
NULL.")
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkArrayMin {
+    signature: Signature,
+}
+
+impl Default for SparkArrayMin {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkArrayMin {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::user_defined(Volatility::Immutable),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkArrayMin {
+    fn name(&self) -> &str {
+        "array_min"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        element_type(self.name(), arg_types)
+    }
+
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        coerce_array(self.name(), arg_types)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        invoke(self.name(), args, Ordering::Less)
+    }
+
+    fn documentation(&self) -> Option<&Documentation> {
+        self.doc()
+    }
+}
+
+/// The element type of the array argument. A NULL-typed argument, which Spark 
accepts,
+/// returns the NULL type.
+fn element_type(name: &str, arg_types: &[DataType]) -> Result<DataType> {
+    let [array] = take_function_args(name, arg_types)?;
+    match array {
+        DataType::Null => Ok(DataType::Null),
+        DataType::List(field)
+        | DataType::LargeList(field)
+        | DataType::FixedSizeList(field, _) => Ok(field.data_type().clone()),
+        other => unexpected_input_type(name, other),
+    }
+}
+
+/// Accepts the argument types that Spark's `ArrayMax` and `ArrayMin` accept: 
an array of an
+/// orderable type, or NULL. Fixed-size lists are cast to lists.
+fn coerce_array(name: &str, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+    let [array] = take_function_args(name, arg_types)?;
+    let coerced = match array {
+        DataType::Null | DataType::List(_) | DataType::LargeList(_) => 
array.clone(),
+        DataType::FixedSizeList(field, _) => 
DataType::List(Arc::new(Field::new(
+            field.name(),
+            field.data_type().clone(),
+            true,
+        ))),
+        other => return unexpected_input_type(name, other),
+    };
+    let element = element_type(name, std::slice::from_ref(&coerced))?;
+    if contains_map(&element) {
+        return plan_err!(
+            "[DATATYPE_MISMATCH.INVALID_ORDERING_TYPE] The `{name}` does not 
support \
+             ordering on type {element}"
+        );
+    }

Review Comment:
   Done. This now uses `is_orderable` from #345, which rejects 
`Interval(MonthDayNano)` at any depth. 1728af8 adds error cases for a flat 
interval and one inside a struct, and Spark 4.2.0 raises the same errors.



##########
crates/spark/src/function/array/array_extrema.rs:
##########
@@ -0,0 +1,780 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+//! Spark-compatible `array_max` and `array_min`.
+
+use super::array_position::contains_map;
+use super::float_normalize::normalize_array;
+use arrow::array::{
+    Array, ArrayRef, ArrowPrimitiveType, AsArray, GenericListArray, 
OffsetSizeTrait,
+    PrimitiveArray, UInt64Array, make_comparator, new_null_array,
+};
+use arrow::compute::{SortOptions, take};
+use arrow::datatypes::{
+    DataType, Date32Type, Date64Type, Decimal128Type, Decimal256Type, Field, 
Float16Type,
+    Float32Type, Float64Type, Int8Type, Int16Type, Int32Type, Int64Type, 
TimeUnit,
+    TimestampMicrosecondType, TimestampMillisecondType, 
TimestampNanosecondType,
+    TimestampSecondType, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
+};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, ScalarValue, exec_err, plan_err};
+use datafusion_expr::{
+    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    Volatility,
+};
+use datafusion_macros::user_doc;
+use std::cmp::Ordering;
+use std::sync::Arc;
+
+/// Spark-compatible `array_max` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#array_max>
+#[user_doc(
+    doc_section(label = "Array Functions"),
+    description = "Returns the maximum element of the array, using Spark's 
ordering. NULL \
+        elements are skipped, and an empty array or an array of only NULL 
elements returns \
+        NULL. For floating point values, also inside nested arrays and 
structs, NaN is \
+        greater than every other value, including infinity, and -0.0 is equal 
to 0.0. On a \
+        tie the first of the tied elements is returned as it is, so the sign 
of a zero and the \
+        bits of a NaN are kept. Strings and binary values compare bytewise. 
Arrays compare \
+        element by element and then by length, and structs compare field by 
field; NULLs \
+        nested inside them sort before other values. Map elements, at any 
depth, are an \
+        error, as in Spark.",
+    syntax_example = "array_max(array)",
+    sql_example = r#"```sql
+> SELECT array_max(array(1, 20, NULL, 3));
+20
+```"#,
+    argument(name = "array", description = "Array expression. NULL returns 
NULL.")
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkArrayMax {
+    signature: Signature,
+}
+
+impl Default for SparkArrayMax {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkArrayMax {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::user_defined(Volatility::Immutable),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkArrayMax {
+    fn name(&self) -> &str {
+        "array_max"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        element_type(self.name(), arg_types)
+    }
+
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        coerce_array(self.name(), arg_types)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        invoke(self.name(), args, Ordering::Greater)
+    }
+
+    fn documentation(&self) -> Option<&Documentation> {
+        self.doc()
+    }
+}
+
+/// Spark-compatible `array_min` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#array_min>
+#[user_doc(
+    doc_section(label = "Array Functions"),
+    description = "Returns the minimum element of the array, using Spark's 
ordering. NULL \
+        elements are skipped, and an empty array or an array of only NULL 
elements returns \
+        NULL. For floating point values, also inside nested arrays and 
structs, NaN is \
+        greater than every other value, including infinity, and -0.0 is equal 
to 0.0. On a \
+        tie the first of the tied elements is returned as it is, so the sign 
of a zero and the \
+        bits of a NaN are kept. Strings and binary values compare bytewise. 
Arrays compare \
+        element by element and then by length, and structs compare field by 
field; NULLs \
+        nested inside them sort before other values. Map elements, at any 
depth, are an \
+        error, as in Spark.",
+    syntax_example = "array_min(array)",
+    sql_example = r#"```sql
+> SELECT array_min(array(1, 20, NULL, 3));
+1
+```"#,
+    argument(name = "array", description = "Array expression. NULL returns 
NULL.")
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkArrayMin {
+    signature: Signature,
+}
+
+impl Default for SparkArrayMin {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkArrayMin {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::user_defined(Volatility::Immutable),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkArrayMin {
+    fn name(&self) -> &str {
+        "array_min"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        element_type(self.name(), arg_types)
+    }
+
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        coerce_array(self.name(), arg_types)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        invoke(self.name(), args, Ordering::Less)
+    }
+
+    fn documentation(&self) -> Option<&Documentation> {
+        self.doc()
+    }
+}
+
+/// The element type of the array argument. A NULL-typed argument, which Spark 
accepts,
+/// returns the NULL type.
+fn element_type(name: &str, arg_types: &[DataType]) -> Result<DataType> {
+    let [array] = take_function_args(name, arg_types)?;
+    match array {
+        DataType::Null => Ok(DataType::Null),
+        DataType::List(field)
+        | DataType::LargeList(field)
+        | DataType::FixedSizeList(field, _) => Ok(field.data_type().clone()),
+        other => unexpected_input_type(name, other),
+    }
+}
+
+/// Accepts the argument types that Spark's `ArrayMax` and `ArrayMin` accept: 
an array of an
+/// orderable type, or NULL. Fixed-size lists are cast to lists.
+fn coerce_array(name: &str, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+    let [array] = take_function_args(name, arg_types)?;
+    let coerced = match array {
+        DataType::Null | DataType::List(_) | DataType::LargeList(_) => 
array.clone(),
+        DataType::FixedSizeList(field, _) => 
DataType::List(Arc::new(Field::new(
+            field.name(),
+            field.data_type().clone(),
+            true,
+        ))),
+        other => return unexpected_input_type(name, other),
+    };
+    let element = element_type(name, std::slice::from_ref(&coerced))?;
+    if contains_map(&element) {
+        return plan_err!(
+            "[DATATYPE_MISMATCH.INVALID_ORDERING_TYPE] The `{name}` does not 
support \
+             ordering on type {element}"
+        );
+    }
+    Ok(vec![coerced])
+}
+
+fn unexpected_input_type<T>(name: &str, data_type: &DataType) -> Result<T> {
+    plan_err!(
+        "[DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE] The first parameter of 
`{name}` \
+         requires the \"ARRAY\" type, however it has the type {data_type}"
+    )

Review Comment:
   Done in 1728af8 with `spark_sql_type` from #345. `array_max(1)` now gives 
`... however it has the type "BIGINT". SQLSTATE: 42K09` and maps give `... on 
type "MAP<BIGINT, BIGINT>". SQLSTATE: 42K09`. The message says "it" rather than 
Spark's quoted expression, because the function only sees the argument's type.



##########
crates/spark/src/function/array/array_extrema.rs:
##########
@@ -0,0 +1,780 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for 
additional information regarding copyright ownership
+// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) 
under one or more contributor license agreements
+
+//! Spark-compatible `array_max` and `array_min`.
+
+use super::array_position::contains_map;
+use super::float_normalize::normalize_array;
+use arrow::array::{
+    Array, ArrayRef, ArrowPrimitiveType, AsArray, GenericListArray, 
OffsetSizeTrait,
+    PrimitiveArray, UInt64Array, make_comparator, new_null_array,
+};
+use arrow::compute::{SortOptions, take};
+use arrow::datatypes::{
+    DataType, Date32Type, Date64Type, Decimal128Type, Decimal256Type, Field, 
Float16Type,
+    Float32Type, Float64Type, Int8Type, Int16Type, Int32Type, Int64Type, 
TimeUnit,
+    TimestampMicrosecondType, TimestampMillisecondType, 
TimestampNanosecondType,
+    TimestampSecondType, UInt8Type, UInt16Type, UInt32Type, UInt64Type,
+};
+use datafusion_common::utils::take_function_args;
+use datafusion_common::{Result, ScalarValue, exec_err, plan_err};
+use datafusion_expr::{
+    ColumnarValue, Documentation, ScalarFunctionArgs, ScalarUDFImpl, Signature,
+    Volatility,
+};
+use datafusion_macros::user_doc;
+use std::cmp::Ordering;
+use std::sync::Arc;
+
+/// Spark-compatible `array_max` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#array_max>
+#[user_doc(
+    doc_section(label = "Array Functions"),
+    description = "Returns the maximum element of the array, using Spark's 
ordering. NULL \
+        elements are skipped, and an empty array or an array of only NULL 
elements returns \
+        NULL. For floating point values, also inside nested arrays and 
structs, NaN is \
+        greater than every other value, including infinity, and -0.0 is equal 
to 0.0. On a \
+        tie the first of the tied elements is returned as it is, so the sign 
of a zero and the \
+        bits of a NaN are kept. Strings and binary values compare bytewise. 
Arrays compare \
+        element by element and then by length, and structs compare field by 
field; NULLs \
+        nested inside them sort before other values. Map elements, at any 
depth, are an \
+        error, as in Spark.",
+    syntax_example = "array_max(array)",
+    sql_example = r#"```sql
+> SELECT array_max(array(1, 20, NULL, 3));
+20
+```"#,
+    argument(name = "array", description = "Array expression. NULL returns 
NULL.")
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkArrayMax {
+    signature: Signature,
+}
+
+impl Default for SparkArrayMax {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkArrayMax {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::user_defined(Volatility::Immutable),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkArrayMax {
+    fn name(&self) -> &str {
+        "array_max"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        element_type(self.name(), arg_types)
+    }
+
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        coerce_array(self.name(), arg_types)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        invoke(self.name(), args, Ordering::Greater)
+    }
+
+    fn documentation(&self) -> Option<&Documentation> {
+        self.doc()
+    }
+}
+
+/// Spark-compatible `array_min` expression.
+///
+/// <https://spark.apache.org/docs/latest/api/sql/index.html#array_min>
+#[user_doc(
+    doc_section(label = "Array Functions"),
+    description = "Returns the minimum element of the array, using Spark's 
ordering. NULL \
+        elements are skipped, and an empty array or an array of only NULL 
elements returns \
+        NULL. For floating point values, also inside nested arrays and 
structs, NaN is \
+        greater than every other value, including infinity, and -0.0 is equal 
to 0.0. On a \
+        tie the first of the tied elements is returned as it is, so the sign 
of a zero and the \
+        bits of a NaN are kept. Strings and binary values compare bytewise. 
Arrays compare \
+        element by element and then by length, and structs compare field by 
field; NULLs \
+        nested inside them sort before other values. Map elements, at any 
depth, are an \
+        error, as in Spark.",
+    syntax_example = "array_min(array)",
+    sql_example = r#"```sql
+> SELECT array_min(array(1, 20, NULL, 3));
+1
+```"#,
+    argument(name = "array", description = "Array expression. NULL returns 
NULL.")
+)]
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkArrayMin {
+    signature: Signature,
+}
+
+impl Default for SparkArrayMin {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkArrayMin {
+    pub fn new() -> Self {
+        Self {
+            signature: Signature::user_defined(Volatility::Immutable),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkArrayMin {
+    fn name(&self) -> &str {
+        "array_min"
+    }
+
+    fn signature(&self) -> &Signature {
+        &self.signature
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        element_type(self.name(), arg_types)
+    }
+
+    fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+        coerce_array(self.name(), arg_types)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        invoke(self.name(), args, Ordering::Less)
+    }
+
+    fn documentation(&self) -> Option<&Documentation> {
+        self.doc()
+    }
+}
+
+/// The element type of the array argument. A NULL-typed argument, which Spark 
accepts,
+/// returns the NULL type.
+fn element_type(name: &str, arg_types: &[DataType]) -> Result<DataType> {
+    let [array] = take_function_args(name, arg_types)?;
+    match array {
+        DataType::Null => Ok(DataType::Null),
+        DataType::List(field)
+        | DataType::LargeList(field)
+        | DataType::FixedSizeList(field, _) => Ok(field.data_type().clone()),
+        other => unexpected_input_type(name, other),
+    }
+}
+
+/// Accepts the argument types that Spark's `ArrayMax` and `ArrayMin` accept: 
an array of an
+/// orderable type, or NULL. Fixed-size lists are cast to lists.
+fn coerce_array(name: &str, arg_types: &[DataType]) -> Result<Vec<DataType>> {
+    let [array] = take_function_args(name, arg_types)?;
+    let coerced = match array {
+        DataType::Null | DataType::List(_) | DataType::LargeList(_) => 
array.clone(),
+        DataType::FixedSizeList(field, _) => 
DataType::List(Arc::new(Field::new(
+            field.name(),
+            field.data_type().clone(),
+            true,
+        ))),
+        other => return unexpected_input_type(name, other),
+    };
+    let element = element_type(name, std::slice::from_ref(&coerced))?;
+    if contains_map(&element) {
+        return plan_err!(
+            "[DATATYPE_MISMATCH.INVALID_ORDERING_TYPE] The `{name}` does not 
support \
+             ordering on type {element}"
+        );
+    }
+    Ok(vec![coerced])
+}
+
+fn unexpected_input_type<T>(name: &str, data_type: &DataType) -> Result<T> {
+    plan_err!(
+        "[DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE] The first parameter of 
`{name}` \
+         requires the \"ARRAY\" type, however it has the type {data_type}"
+    )
+}
+
+fn invoke(
+    name: &str,
+    args: ScalarFunctionArgs,
+    target: Ordering,
+) -> Result<ColumnarValue> {
+    let [arg] = take_function_args(name, &args.args)?;
+    let element_type = args.return_field.data_type();
+    let array = match arg {
+        ColumnarValue::Array(array) => Arc::clone(array),
+        ColumnarValue::Scalar(scalar) => scalar.to_array()?,
+    };
+
+    let result = match array.data_type() {
+        DataType::Null => new_null_array(element_type, array.len()),
+        // All the elements are NULL, so every result is NULL.
+        _ if element_type.is_null() => new_null_array(element_type, 
array.len()),
+        DataType::List(_) => extrema(array.as_list::<i32>(), target)?,
+        DataType::LargeList(_) => extrema(array.as_list::<i64>(), target)?,
+        other => return exec_err!("{name} does not support type {other}"),
+    };
+
+    if matches!(arg, ColumnarValue::Scalar(_)) {
+        Ok(ColumnarValue::Scalar(ScalarValue::try_from_array(
+            &result, 0,
+        )?))
+    } else {
+        Ok(ColumnarValue::Array(result))
+    }
+}
+
+/// Returns, for each list, its first non-NULL element that no other element 
orders
+/// `target` of, or NULL when the list is NULL, empty, or holds only NULL 
elements.
+fn extrema<O: OffsetSizeTrait>(
+    list: &GenericListArray<O>,
+    target: Ordering,
+) -> Result<ArrayRef> {
+    let offsets = list.offsets();
+    // Only the values that this (possibly sliced) list refers to are compared.
+    let base = offsets[0].as_usize();
+    let len = offsets[offsets.len() - 1].as_usize() - base;
+    let values = list.values().slice(base, len);
+
+    // Primitive elements take a typed path: no copy of the values, no 
type-erased comparator
+    // and no `take`.
+    macro_rules! primitive {
+        ($t:ty, $cmp:expr) => {
+            return Ok(Arc::new(primitive_extrema::<$t, O>(
+                list,
+                values.as_primitive::<$t>(),
+                base,
+                target,
+                $cmp,
+            )))
+        };
+    }
+    macro_rules! ordered {
+        ($t:ty) => {
+            primitive!($t, |a, b| a.cmp(&b))
+        };
+    }
+    match values.data_type() {
+        DataType::Int8 => ordered!(Int8Type),
+        DataType::Int16 => ordered!(Int16Type),
+        DataType::Int32 => ordered!(Int32Type),
+        DataType::Int64 => ordered!(Int64Type),
+        DataType::UInt8 => ordered!(UInt8Type),
+        DataType::UInt16 => ordered!(UInt16Type),
+        DataType::UInt32 => ordered!(UInt32Type),
+        DataType::UInt64 => ordered!(UInt64Type),
+        DataType::Decimal128(_, _) => ordered!(Decimal128Type),
+        DataType::Decimal256(_, _) => ordered!(Decimal256Type),
+        DataType::Date32 => ordered!(Date32Type),
+        DataType::Date64 => ordered!(Date64Type),
+        DataType::Timestamp(TimeUnit::Second, _) => 
ordered!(TimestampSecondType),
+        DataType::Timestamp(TimeUnit::Millisecond, _) => {
+            ordered!(TimestampMillisecondType)
+        }
+        DataType::Timestamp(TimeUnit::Microsecond, _) => {
+            ordered!(TimestampMicrosecondType)
+        }
+        DataType::Timestamp(TimeUnit::Nanosecond, _) => {
+            ordered!(TimestampNanosecondType)
+        }
+        DataType::Float16 => primitive!(Float16Type, spark_float_cmp),
+        DataType::Float32 => primitive!(Float32Type, spark_float_cmp),
+        DataType::Float64 => primitive!(Float64Type, spark_float_cmp),

Review Comment:
   Dropped the unsigned, `Float16`, `Date64` and `Decimal256` arms in 1728af8. 
Those types now go to `generic_extrema`.



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