Skip to content

Apache DataFusion SQL Query Engine

fix: replace with empty search string should be a no-op (#22497)

Amogh Ramesh7b7429d94940
diff --git a/datafusion/functions/benches/replace.rs b/datafusion/functions/benches/replace.rs
index b117968bad..7ad198995a 100644
--- a/datafusion/functions/benches/replace.rs
+++ b/datafusion/functions/benches/replace.rs
@@ -162,36 +162,6 @@ fn criterion_benchmark(c: &mut Criterion) {
}
}
- // Empty-`from` path: insert `to` between every char of the input and at
- // both ends.
- if size == 1024 {
- for &str_len in &[32_usize, 128] {
- let args = create_args::<i32>(size, str_len, false, 0, 3, 0.0);
- group.bench_function(
- format!("replace_string_empty_from [size={size}, str_len={str_len}]"),
- |b| {
- b.iter(|| {
- let args_cloned = args.clone();
- black_box(invoke_replace_with_args(args_cloned, size))
- })
- },
- );
-
- let args = create_args::<i32>(size, str_len, true, 0, 3, 0.0);
- group.bench_function(
- format!(
- "replace_string_view_empty_from [size={size}, str_len={str_len}]"
- ),
- |b| {
- b.iter(|| {
- let args_cloned = args.clone();
- black_box(invoke_replace_with_args(args_cloned, size))
- })
- },
- );
- }
- }
-
group.finish();
}
}
diff --git a/datafusion/functions/src/string/replace.rs b/datafusion/functions/src/string/replace.rs
index 769727999e..28f81769f5 100644
--- a/datafusion/functions/src/string/replace.rs
+++ b/datafusion/functions/src/string/replace.rs
@@ -268,14 +268,8 @@ fn apply_replace<B: BulkNullStringArrayBuilder>(
}
if from.is_empty() {
- // Empty `from`: insert `to` before each character and at both ends.
- builder.append_with(|w| {
- w.write_str(to);
- for ch in string.chars() {
- w.write_char(ch);
- w.write_str(to);
- }
- });
+ // PostgreSQL returns the input unchanged when `from` is empty (#22253).
+ builder.append_value(string);
return;
}
@@ -346,6 +340,19 @@ mod tests {
StringArray
);
+ test_function!(
+ ReplaceFunc::new(),
+ vec![
+ ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from("abc")))),
+ ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from("")))),
+ ColumnarValue::Scalar(ScalarValue::LargeUtf8(Some(String::from("x")))),
+ ],
+ Ok(Some("abc")),
+ &str,
+ LargeUtf8,
+ LargeStringArray
+ );
+
Ok(())
}
}
diff --git a/datafusion/sqllogictest/test_files/string/string_literal.slt b/datafusion/sqllogictest/test_files/string/string_literal.slt
index d7547bf145..81aaf48629 100644
--- a/datafusion/sqllogictest/test_files/string/string_literal.slt
+++ b/datafusion/sqllogictest/test_files/string/string_literal.slt
@@ -430,6 +430,26 @@ SELECT replace(arrow_cast('foobar', 'LargeUtf8'), arrow_cast('bar', 'LargeUtf8')
----
foohello
+# PostgreSQL compatibility: empty search string is a no-op (issue #22253)
+query T
+SELECT replace('abc', '', 'x')
+----
+abc
+
+query T
+SELECT replace(arrow_cast('abc', 'Dictionary(Int32, Utf8)'), '', 'x')
+----
+abc
+
+query T
+SELECT replace(arrow_cast('abc', 'Utf8View'), arrow_cast('', 'Utf8View'), arrow_cast('x', 'Utf8View'))
+----
+abc
+
+query T
+SELECT replace(arrow_cast('abc', 'LargeUtf8'), arrow_cast('', 'LargeUtf8'), arrow_cast('x', 'LargeUtf8'))
+----
+abc
query T
SELECT reverse('abcde')
diff --git a/datafusion/sqllogictest/test_files/string/string_query.slt.part b/datafusion/sqllogictest/test_files/string/string_query.slt.part
index 9e5b8f91e7..dac4dd06db 100644
--- a/datafusion/sqllogictest/test_files/string/string_query.slt.part
+++ b/datafusion/sqllogictest/test_files/string/string_query.slt.part
@@ -884,10 +884,10 @@ Xiangpeng bar NULL bar NULL datafusion数据融合
Raphael baraphael NULL datafusionДатbarион NULL datafusionДатаФусион
under_score under_score NULL un iść core NULL un iść core
percent percent NULL pan Tadeusz ma iść w kąt NULL pan Tadeusz ma iść w kąt
-(empty) (empty) NULL bar NULL (empty)
-(empty) (empty) NULL bar NULL (empty)
-% % NULL bar NULL (empty)
-_ _ NULL bar NULL (empty)
+(empty) (empty) NULL (empty) NULL (empty)
+(empty) (empty) NULL (empty) NULL (empty)
+% % NULL (empty) NULL (empty)
+_ _ NULL (empty) NULL (empty)
NULL NULL NULL NULL NULL NULL
NULL NULL NULL NULL NULL NULL