Jefffrey commented on code in PR #19592: URL: https://github.com/apache/datafusion/pull/19592#discussion_r2684619177
########## datafusion/spark/src/function/collection/size.rs: ########## @@ -0,0 +1,165 @@ +// 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 arrow::array::{Array, ArrayRef, AsArray, Int32Array}; +use arrow::compute::kernels::length::length as arrow_length; +use arrow::datatypes::{DataType, Field, FieldRef}; +use datafusion_common::{Result, plan_err}; +use datafusion_expr::{ + ArrayFunctionArgument, ArrayFunctionSignature, ColumnarValue, ReturnFieldArgs, + ScalarFunctionArgs, ScalarUDFImpl, Signature, TypeSignature, Volatility, +}; +use datafusion_functions::utils::make_scalar_function; +use std::any::Any; +use std::sync::Arc; + +/// Spark-compatible `size` function. +/// +/// Returns the number of elements in an array or the number of key-value pairs in a map. +/// Returns -1 for null input (Spark behavior). +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct SparkSize { + signature: Signature, +} + +impl Default for SparkSize { + fn default() -> Self { + Self::new() + } +} + +impl SparkSize { + pub fn new() -> Self { + Self { + signature: Signature::one_of( + vec![ + // Array Type + TypeSignature::ArraySignature(ArrayFunctionSignature::Array { + arguments: vec![ArrayFunctionArgument::Array], + array_coercion: None, + }), + // Map Type + TypeSignature::ArraySignature(ArrayFunctionSignature::MapArray), + ], + Volatility::Immutable, + ), + } + } +} + +impl ScalarUDFImpl for SparkSize { + fn as_any(&self) -> &dyn Any { + self + } + + fn name(&self) -> &str { + "size" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { + Ok(DataType::Int32) + } + + fn return_field_from_args(&self, args: ReturnFieldArgs) -> Result<FieldRef> { + Ok(Arc::new(Field::new( + self.name(), + DataType::Int32, + args.arg_fields[0].is_nullable(), Review Comment: I think we should make this `false` since we're currently supporting the legacy behaviour of returning `-1` for null; if we want the other behaviour (null on null) this would indeed be nullable based on input -- 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]
