SparkApplicationMaster commented on code in PR #16940: URL: https://github.com/apache/datafusion/pull/16940#discussion_r2296444829
########## datafusion/spark/src/function/map/map_function.rs: ########## @@ -0,0 +1,161 @@ +// 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. + +use std::any::Any; +use std::sync::Arc; + +use arrow::array::{ArrayRef, MapArray, StructArray}; +use arrow::buffer::OffsetBuffer; +use arrow::compute::interleave; +use arrow::datatypes::{DataType, Field, Fields}; +use datafusion_common::{exec_err, Result}; +use datafusion_expr::{ + ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, +}; + +trait KeyValue<T> +where + T: 'static, +{ + fn keys(&self) -> impl Iterator<Item = &T>; + fn values(&self) -> impl Iterator<Item = &T>; +} + +impl<T> KeyValue<T> for &[T] +where + T: 'static, +{ + fn keys(&self) -> impl Iterator<Item = &T> { + self.iter().step_by(2) + } + + fn values(&self) -> impl Iterator<Item = &T> { + self.iter().skip(1).step_by(2) + } +} + +fn to_map_array(args: &[ArrayRef]) -> Result<ArrayRef> { + if !args.len().is_multiple_of(2) { + return exec_err!("map requires an even number of arguments"); + } + let num_entries = args.len() / 2; + let num_rows = args.first().map(|a| a.len()).unwrap_or(0); + if args.iter().any(|a| a.len() != num_rows) { + return exec_err!("map requires all arrays to have the same length"); + } + let key_type = args + .first() + .map(|a| a.data_type()) + .unwrap_or(&DataType::Null); + let value_type = args + .get(1) + .map(|a| a.data_type()) + .unwrap_or(&DataType::Null); + let keys = args.keys().map(|a| a.as_ref()).collect::<Vec<_>>(); + let values = args.values().map(|a| a.as_ref()).collect::<Vec<_>>(); + if keys.iter().any(|a| a.data_type() != key_type) { + return exec_err!("map requires all key types to be the same"); Review Comment: please consider checking this implementation: https://github.com/lakehq/sail/blob/0a82099c61cf29e9cdfa33666955e55b37a2256a/crates/sail-plan/src/function/scalar/map.rs#L8 https://github.com/lakehq/sail/blob/0a82099c61cf29e9cdfa33666955e55b37a2256a/crates/sail-plan/src/extension/function/map/map_function.rs#L15 it supports: - type coersion for keys and values - empty map creation - keys deduplication - map creation from values, arrays and entries and passes spark-tests -- 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-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org