Skip to content

Commit f5709e7

Browse files
authored
feat(spark): implement StringView for SparkConcat (apache#19984)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - This PR is part of the [Utf8View support](apache#10918) epic. It adds `Utf8View` support in the Spark-compat layer. ## Rationale for this change In our internal project we're only suppporting `Utf8View` _(because of design constraints)_ and the current implementation of `SparkConcat` only supports `Utf8`. The `SparkConcat` function should accept `Utf8View` and mixed string types in line with the main DataFusion concat. This PR adds that support and follows the same patterns as [DataFusion’s concat](https://github.com/apache/datafusion/blob/main/datafusion/functions/src/string/concat.rs). Prevents errors like : > The type of Utf8 AND Utf8View of like physical should be same. > This issue was likely caused by a bug in DataFusion's code. Please help us to resolve this by filing a bug report in our issue tracker: https://github.com/apache/datafusion/issues from a query like:- ```sql select i_item_sk, item_info from (select i_item_sk, CONCAT('Item: ', i_item_desc) as item_info from item) sub where item_info LIKE 'Item: Electronic%' order by 1; ``` ## What changes are included in this PR? - Extend the type signature to accept `Utf8View` in addition to `Utf8` and `LargeUtf8` via `TypeSignature::Variadic(vec![Utf8View, Utf8, LargeUtf8])` matching DataFusion’s concat. - In `return_field_from_args`, compute the result type with precedence Utf8View &gt; LargeUtf8 &gt; Utf8. In spark_concat, handle Utf8View and LargeUtf8 in scalar paths (zero-argument and all-NULL). ## Are these changes tested? Yes. - Unit tests: `cargo test --package datafusion-spark function::string::concat::tests`, including `test_concat_utf8view`. - Sqllogictest: `spark/string/concat.slt` includes a “**Utf8View: no extra CAST in plan**” case that uses EXPLAIN and a temporary table to ensure no extra CASTs when using arrow_cast(..., 'Utf8View') with table columns. ## Are there any user-facing changes? - **API:** SparkConcat’s signature is extended to include Utf8View in the variadic list. No breaking changes. _used gpt to rephrase some of these points_
1 parent c560bee commit f5709e7

2 files changed

Lines changed: 57 additions & 5 deletions

File tree

datafusion/spark/src/function/string/concat.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,21 @@ impl ScalarUDFImpl for SparkConcat {
8989
)
9090
}
9191
fn return_field_from_args(&self, args: ReturnFieldArgs<'_>) -> Result<FieldRef> {
92+
use DataType::*;
93+
9294
// Spark semantics: concat returns NULL if ANY input is NULL
9395
let nullable = args.arg_fields.iter().any(|f| f.is_nullable());
9496

95-
Ok(Arc::new(Field::new("concat", DataType::Utf8, nullable)))
97+
// Determine return type: Utf8View > LargeUtf8 > Utf8
98+
let mut dt = &Utf8;
99+
for field in args.arg_fields {
100+
let data_type = field.data_type();
101+
if data_type == &Utf8View || (data_type == &LargeUtf8 && dt != &Utf8View) {
102+
dt = data_type;
103+
}
104+
}
105+
106+
Ok(Arc::new(Field::new("concat", dt.clone(), nullable)))
96107
}
97108
}
98109

@@ -110,17 +121,33 @@ fn spark_concat(args: ScalarFunctionArgs) -> Result<ColumnarValue> {
110121

111122
// Handle zero-argument case: return empty string
112123
if arg_values.is_empty() {
113-
return Ok(ColumnarValue::Scalar(ScalarValue::Utf8(
114-
Some(String::new()),
115-
)));
124+
let return_type = return_field.data_type();
125+
return match return_type {
126+
DataType::Utf8View => Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(Some(
127+
String::new(),
128+
)))),
129+
DataType::LargeUtf8 => Ok(ColumnarValue::Scalar(ScalarValue::LargeUtf8(
130+
Some(String::new()),
131+
))),
132+
_ => Ok(ColumnarValue::Scalar(ScalarValue::Utf8(
133+
Some(String::new()),
134+
))),
135+
};
116136
}
117137

118138
// Step 1: Check for NULL mask in incoming args
119139
let null_mask = compute_null_mask(&arg_values, number_rows)?;
120140

121141
// If all scalars and any is NULL, return NULL immediately
122142
if matches!(null_mask, NullMaskResolution::ReturnNull) {
123-
return Ok(ColumnarValue::Scalar(ScalarValue::Utf8(None)));
143+
let return_type = return_field.data_type();
144+
return match return_type {
145+
DataType::Utf8View => Ok(ColumnarValue::Scalar(ScalarValue::Utf8View(None))),
146+
DataType::LargeUtf8 => {
147+
Ok(ColumnarValue::Scalar(ScalarValue::LargeUtf8(None)))
148+
}
149+
_ => Ok(ColumnarValue::Scalar(ScalarValue::Utf8(None))),
150+
};
124151
}
125152

126153
// Step 2: Delegate to DataFusion's concat
@@ -181,6 +208,7 @@ mod tests {
181208
);
182209
Ok(())
183210
}
211+
184212
#[test]
185213
fn test_spark_concat_return_field_non_nullable() -> Result<()> {
186214
let func = SparkConcat::new();

datafusion/sqllogictest/test_files/spark/string/concat.slt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ SELECT concat('Spark', 'SQL');
2020
----
2121
SparkSQL
2222

23+
# Test two Utf8View inputs: value and return type
24+
query TT
25+
SELECT concat(arrow_cast('Spark', 'Utf8View'), arrow_cast('SQL', 'Utf8View')), arrow_typeof(concat(arrow_cast('Spark', 'Utf8View'), arrow_cast('SQL', 'Utf8View')));
26+
----
27+
SparkSQL Utf8View
28+
2329
query T
2430
SELECT concat('Spark', 'SQL', NULL);
2531
----
@@ -46,3 +52,21 @@ SELECT concat(a, b, c) from (select 'a' a, 'b' b, 'c' c union all select null a,
4652
----
4753
abc
4854
NULL
55+
56+
# Test mixed types: Utf8View + Utf8
57+
query TT
58+
SELECT concat(arrow_cast('hello', 'Utf8View'), ' world'), arrow_typeof(concat(arrow_cast('hello', 'Utf8View'), ' world'));
59+
----
60+
hello world Utf8View
61+
62+
# Test Utf8 + LargeUtf8 => return type LargeUtf8
63+
query TT
64+
SELECT concat('a', arrow_cast('b', 'LargeUtf8')), arrow_typeof(concat('a', arrow_cast('b', 'LargeUtf8')));
65+
----
66+
ab LargeUtf8
67+
68+
# Test all three types mixed together
69+
query TT
70+
SELECT concat('a', arrow_cast('b', 'LargeUtf8'), arrow_cast('c', 'Utf8View')), arrow_typeof(concat('a', arrow_cast('b', 'LargeUtf8'), arrow_cast('c', 'Utf8View')));
71+
----
72+
abc Utf8View

0 commit comments

Comments
 (0)