davidlghellin commented on code in PR #20928: URL: https://github.com/apache/datafusion/pull/20928#discussion_r3367015362
########## datafusion/spark/src/function/string/concat_ws.rs: ########## @@ -0,0 +1,540 @@ +// 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 `concat_ws` function. +//! +//! Differences with DataFusion core `concat_ws`: +//! - Accepts array arguments and expands their elements +//! - Allows zero value arguments: `concat_ws(',')` → `""` +//! - Null array elements are skipped (same as null scalars) + +use std::sync::Arc; + +use arrow::array::{ + Array, ArrayRef, AsArray, BinaryArray, GenericListArray, LargeBinaryArray, + OffsetSizeTrait, StringBuilder, +}; +use arrow::datatypes::DataType; +use datafusion_common::cast::as_generic_string_array; +use datafusion_common::{Result, ScalarValue, exec_err}; +use datafusion_expr::{ + ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl, Signature, Volatility, +}; + +/// Spark-compatible `concat_ws` expression. +/// +/// In Spark, `concat_ws(sep, a, b, ...)` joins strings with separator. +/// If any argument is an array, its elements are joined with the separator. +/// Null values (both scalar and array elements) are skipped. +/// Null separator produces null result. +#[derive(Debug, PartialEq, Eq, Hash)] +pub struct SparkConcatWs { + signature: Signature, +} + +impl Default for SparkConcatWs { + fn default() -> Self { + Self::new() + } +} + +impl SparkConcatWs { + pub fn new() -> Self { + Self { + signature: Signature::variadic_any(Volatility::Immutable), + } + } +} + +impl ScalarUDFImpl for SparkConcatWs { + fn name(&self) -> &str { + "concat_ws" + } + + fn signature(&self) -> &Signature { + &self.signature + } + + fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> { + // Spark's concat_ws always returns STRING (Utf8) + Ok(DataType::Utf8) + } + + fn invoke_with_args(&self, args: ScalarFunctionArgs) -> Result<ColumnarValue> { + // Zero value args: concat_ws(',') → "" + if args.args.len() <= 1 { + if args.args.is_empty() { + return Ok(ColumnarValue::Scalar(ScalarValue::Utf8( + Some(String::new()), + ))); + } + // Only separator — return "" or NULL depending on separator + return match &args.args[0] { + ColumnarValue::Scalar(s) if s.is_null() => { + Ok(ColumnarValue::Scalar(ScalarValue::Utf8(None))) + } + ColumnarValue::Scalar(_) => Ok(ColumnarValue::Scalar(ScalarValue::Utf8( + Some(String::new()), + ))), + ColumnarValue::Array(arr) => { + // Separator is a column: return "" for non-null, NULL for null + let mut builder = StringBuilder::with_capacity(arr.len(), 0); + for row_idx in 0..arr.len() { + if arr.is_null(row_idx) { + builder.append_null(); + } else { + builder.append_value(""); + } + } + Ok(ColumnarValue::Array(Arc::new(builder.finish()) as ArrayRef)) + } + }; + } + + // Use our implementation for all cases to guarantee consistent Utf8 return type. + // Core's concat_ws may return Utf8View which conflicts with our return_type. + spark_concat_ws_with_arrays(&args.args, args.number_rows) + } + + fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> { Review Comment: Updated — signature is now `user_defined`, so `coerce_types` is called (arity check + list normalization + non-string→Utf8 coercion all live there). Resolving. -- 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]
