rich7420 commented on code in PR #5854:
URL: https://github.com/apache/datafusion-comet/pull/5854#discussion_r4016897902


##########
native/spark-expr/src/map_funcs/map_builders.rs:
##########
@@ -0,0 +1,651 @@
+// 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.
+
+//! Spark-compatible `map_from_arrays`, `map_from_entries` and `str_to_map`.
+//!
+//! The `datafusion-spark` kernels build the `MapArray` and already follow 
Spark's
+//! `spark.sql.mapKeyDedupPolicy`, which Comet forwards as
+//! `datafusion.spark.map_key_dedup_policy`. These wrappers add the checks 
Spark's
+//! `ArrayBasedMapBuilder` performs before inserting an entry, and restate the 
upstream errors
+//! as the Spark error classes `SparkErrorConverter` turns back into 
`QueryExecutionErrors`:
+//!
+//! - a `NULL` key element raises `[NULL_MAP_KEY]`, ahead of any duplicate-key 
check, because
+//!   Spark rejects the `NULL` before it reaches the dedup map;
+//! - a key array and value array of different lengths raise 
`[MAP_KEY_VALUE_DIFF_SIZES]`;
+//! - a duplicate key under `EXCEPTION` raises `[DUPLICATED_MAP_KEY]` naming 
the key.
+//!
+//! `str_to_map` builds its keys by splitting a string, so it needs only the 
duplicate-key
+//! restatement.
+
+use crate::SparkError;
+use arrow::array::{Array, ArrayRef, AsArray, StructArray};
+use arrow::buffer::NullBuffer;
+use arrow::datatypes::{DataType, FieldRef};
+use datafusion::common::{exec_err, DataFusionError, Result};
+use datafusion::logical_expr::{
+    ColumnarValue, ReturnFieldArgs, ScalarFunctionArgs, ScalarUDFImpl, 
Signature,
+};
+use datafusion_spark::function::map::map_from_arrays::MapFromArrays as 
DataFusionMapFromArrays;
+use datafusion_spark::function::map::map_from_entries::MapFromEntries as 
DataFusionMapFromEntries;
+use datafusion_spark::function::map::str_to_map::SparkStrToMap as 
DataFusionStrToMap;
+use std::sync::Arc;
+
+/// Spark-compatible `map_from_arrays(keys, values)`.
+#[derive(Debug, PartialEq, Eq, Hash)]
+pub struct SparkMapFromArrays {
+    inner: DataFusionMapFromArrays,
+}
+
+impl Default for SparkMapFromArrays {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+
+impl SparkMapFromArrays {
+    pub fn new() -> Self {
+        Self {
+            inner: DataFusionMapFromArrays::new(),
+        }
+    }
+}
+
+impl ScalarUDFImpl for SparkMapFromArrays {
+    fn name(&self) -> &str {
+        self.inner.name()
+    }
+
+    fn signature(&self) -> &Signature {
+        self.inner.signature()
+    }
+
+    fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {
+        self.inner.return_type(arg_types)
+    }
+
+    fn return_field_from_args(&self, args: ReturnFieldArgs) -> 
Result<FieldRef> {
+        self.inner.return_field_from_args(args)
+    }
+
+    fn invoke_with_args(&self, args: ScalarFunctionArgs) -> 
Result<ColumnarValue> {
+        let args = expand_scalars(args)?;
+        match args.args.as_slice() {
+            [ColumnarValue::Array(keys), ColumnarValue::Array(values)] => {
+                validate_map_from_arrays(keys, values)?
+            }
+            other => return exec_err!("map_from_arrays expects 2 arguments, 
got {}", other.len()),
+        }
+        self.inner
+            .invoke_with_args(args)
+            .map_err(|error| as_spark_error(error, DuplicateKeyFormat::Bare))

Review Comment:
   Verified 



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