Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions datafusion/functions/src/string/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_with(|w| w.write_str(string));
return;
}

Expand Down Expand Up @@ -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(())
}
}
20 changes: 20 additions & 0 deletions datafusion/sqllogictest/test_files/string/string_literal.slt
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down