davidlghellin commented on code in PR #20928:
URL: https://github.com/apache/datafusion/pull/20928#discussion_r3367014704


##########
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()),
+                )));
+            }

Review Comment:
   Right — fixed. Arity is now validated in coerce_types (errors with 
invalid_arg_count_exec_err), and the SLT has a query error SELECT concat_ws(); 
case. 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]

Reply via email to