jayzhan211 commented on code in PR #11361: URL: https://github.com/apache/datafusion/pull/11361#discussion_r1674058650
########## datafusion/functions/src/core/map.rs: ########## @@ -0,0 +1,325 @@ +// 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::collections::VecDeque; +use std::sync::Arc; + +use arrow::array::{new_null_array, Array, ArrayData, ArrayRef, MapArray, StructArray}; +use arrow::compute::concat; +use arrow::datatypes::{DataType, Field, SchemaBuilder}; +use arrow_buffer::{Buffer, ToByteSlice}; +use datafusion_common::Result; +use datafusion_common::{exec_err, internal_err, ScalarValue}; +use datafusion_expr::{ColumnarValue, ScalarUDFImpl, Signature, Volatility}; + +fn make_map(args: &[ColumnarValue]) -> Result<ColumnarValue> { + if args.is_empty() { + return exec_err!("map requires at least one pair of arguments, got 0 instead"); + } + + if args.len() % 2 != 0 { + return exec_err!( + "map requires an even number of arguments, got {} instead", + args.len() + ); + } + + let mut value_type = args[1].data_type(); + + let (key, value): (Vec<_>, Vec<_>) = args.chunks_exact(2) + .enumerate() + .map(|(i, chunk)| { + if chunk[0].data_type().is_null() { + return exec_err!("map key cannot be null"); + } + if !chunk[1].data_type().is_null() { Review Comment: It seems that you are trying to replace null with the non-null type here. It is possible to coerce_types earlier before `invoke()` You can use `Signature::user_defined(Volatility::Immutable)` with your defined `coerce_types` to coerce null to non-null, and you can check if all the value type is the same here as well. This function is called before `return_types` and `invoke`, so we don't deal with null type after. Note that although the null type is converted to other type like Int32, but the array value is still `null`, it is something like Int32(Null). ```rust fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { let mut dt = DataType::Int32; for (i, t) in arg_types.iter().enumerate() { if i % 2 == 1 && !t.is_null() { dt = t.clone(); } } let mut dts = vec![]; for (i, t) in arg_types.iter().enumerate() { if i % 2 == 1 && t.is_null() { dts.push(dt.clone()) } else { dts.push(t.clone()) } } Ok(dts) } ``` -- 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