Skip to content

Commit 7b7429d

Browse files
authored
fix: replace with empty search string should be a no-op (apache#22497)
## Which issue does this PR close? - Closes apache#22253 - Closes apache#22357 ## Rationale for this change PostgreSQL returns the input unchanged when `replace` is called with an empty `from`. DataFusion was instead inserting `to` before every character and at both ends, so `replace('abc', '', 'x')` returned `xaxbxcx`. This PR brings the behaviour in line with PostgreSQL. Part of the PG-compatibility cleanup tracked in apache#22247. ## What changes are included in this PR? - `datafusion/functions/src/string/replace.rs`: the empty-`from` branch in `apply_replace` now writes the input verbatim instead of inserting `to`. Added a `LargeUtf8` unit test for the new behaviour. - `datafusion/sqllogictest/test_files/string/string_literal.slt`: four new SLT asserts covering the `Utf8`, `Dictionary`, `Utf8View`, and `LargeUtf8` paths. - `datafusion/sqllogictest/test_files/string/string_query.slt.part`: updated four expected rows that were asserting the old buggy output. ## Are these changes tested? Yes. The unit test in `replace.rs` covers the `LargeUtf8` path, and the four new SLT asserts in `string_literal.slt` cover the remaining Arrow string encodings end-to-end. The full SLT suite passes locally. ## Are there any user-facing changes? Yes. `replace(str, '', x)` now returns `str` unchanged instead of inserting `x` between every character. This matches PostgreSQL. --------- Signed-off-by: Amogh Ramesh <[email protected]>
1 parent f033199 commit 7b7429d

4 files changed

Lines changed: 39 additions & 42 deletions

File tree

‎datafusion/functions/benches/replace.rs‎

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -162,36 +162,6 @@ fn criterion_benchmark(c: &mut Criterion) {
162162
}
163163
}
164164

165-
// Empty-`from` path: insert `to` between every char of the input and at
166-
// both ends.
167-
if size == 1024 {
168-
for &str_len in &[32_usize, 128] {
169-
let args = create_args::<i32>(size, str_len, false, 0, 3, 0.0);
170-
group.bench_function(
171-
format!("replace_string_empty_from [size={size}, str_len={str_len}]"),
172-
|b| {
173-
b.iter(|| {
174-
let args_cloned = args.clone();
175-
black_box(invoke_replace_with_args(args_cloned, size))
176-
})
177-
},
178-
);
179-
180-
let args = create_args::<i32>(size, str_len, true, 0, 3, 0.0);
181-
group.bench_function(
182-
format!(
183-
"replace_string_view_empty_from [size={size}, str_len={str_len}]"
184-
),
185-
|b| {
186-
b.iter(|| {
187-
let args_cloned = args.clone();
188-
black_box(invoke_replace_with_args(args_cloned, size))
189-
})
190-
},
191-
);
192-
}
193-
}
194-
195165
group.finish();
196166
}
197167
}

‎datafusion/functions/src/string/replace.rs‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,8 @@ fn apply_replace<B: BulkNullStringArrayBuilder>(
268268
}
269269

270270
if from.is_empty() {
271-
// Empty `from`: insert `to` before each character and at both ends.
272-
builder.append_with(|w| {
273-
w.write_str(to);
274-
for ch in string.chars() {
275-
w.write_char(ch);
276-
w.write_str(to);
277-
}
278-
});
271+
// PostgreSQL returns the input unchanged when `from` is empty (#22253).
272+
builder.append_value(string);
279273
return;
280274
}
281275

@@ -346,6 +340,19 @@ mod tests {
346340
StringArray
347341
);
348342

343+
test_function!(
344+
ReplaceFunc::new(),
345+
vec![
346+
ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from("abc")))),
347+
ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from("")))),
348+
ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from("x")))),
349+
],
350+
Ok(Some("abc")),
351+
&str,
352+
LargeUtf8,
353+
LargeStringArray
354+
);
355+
349356
Ok(())
350357
}
351358
}

‎datafusion/sqllogictest/test_files/string/string_literal.slt‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,26 @@ SELECT replace(arrow_cast('foobar', 'LargeUtf8'), arrow_cast('bar', 'LargeUtf8')
430430
----
431431
foohello
432432

433+
# PostgreSQL compatibility: empty search string is a no-op (issue #22253)
434+
query T
435+
SELECT replace('abc', '', 'x')
436+
----
437+
abc
438+
439+
query T
440+
SELECT replace(arrow_cast('abc', 'Dictionary(Int32, Utf8)'), '', 'x')
441+
----
442+
abc
443+
444+
query T
445+
SELECT replace(arrow_cast('abc', 'Utf8View'), arrow_cast('', 'Utf8View'), arrow_cast('x', 'Utf8View'))
446+
----
447+
abc
448+
449+
query T
450+
SELECT replace(arrow_cast('abc', 'LargeUtf8'), arrow_cast('', 'LargeUtf8'), arrow_cast('x', 'LargeUtf8'))
451+
----
452+
abc
433453

434454
query T
435455
SELECT reverse('abcde')

‎datafusion/sqllogictest/test_files/string/string_query.slt.part‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -884,10 +884,10 @@ Xiangpeng bar NULL bar NULL datafusion数据融合
884884
Raphael baraphael NULL datafusionДатbarион NULL datafusionДатаФусион
885885
under_score under_score NULL un iść core NULL un iść core
886886
percent percent NULL pan Tadeusz ma iść w kąt NULL pan Tadeusz ma iść w kąt
887-
(empty) (empty) NULL bar NULL (empty)
888-
(empty) (empty) NULL bar NULL (empty)
889-
% % NULL bar NULL (empty)
890-
_ _ NULL bar NULL (empty)
887+
(empty) (empty) NULL (empty) NULL (empty)
888+
(empty) (empty) NULL (empty) NULL (empty)
889+
% % NULL (empty) NULL (empty)
890+
_ _ NULL (empty) NULL (empty)
891891
NULL NULL NULL NULL NULL NULL
892892
NULL NULL NULL NULL NULL NULL
893893

0 commit comments

Comments
 (0)