Skip to content

Commit b42cb8b

Browse files
authored
Support LIKE with ESCAPE \ (#13312)
* Support LIKE with ESCAPE `\` Other escape characters are currently not supported. * fmt
1 parent b122bab commit b42cb8b

File tree

2 files changed

+29
-6
lines changed

2 files changed

+29
-6
lines changed

datafusion/physical-expr/src/planner.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,11 @@ pub fn create_physical_expr(
200200
escape_char,
201201
case_insensitive,
202202
}) => {
203-
if escape_char.is_some() {
204-
return exec_err!("LIKE does not support escape_char");
203+
// `\` is the implicit escape, see https://github.com/apache/datafusion/issues/13291
204+
if escape_char.unwrap_or('\\') != '\\' {
205+
return exec_err!(
206+
"LIKE does not support escape_char other than the backslash (\\)"
207+
);
205208
}
206209
let physical_expr =
207210
create_physical_expr(expr, input_dfschema, execution_props)?;

datafusion/sqllogictest/test_files/string/string_literal.slt

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -861,23 +861,43 @@ SELECT
861861
----
862862
false false true false true false
863863

864-
# \ as an explicit escape character is currently not supported
865-
query error DataFusion error: Execution error: LIKE does not support escape_char
864+
query BBBB
866865
SELECT
867866
'a' LIKE '\%' ESCAPE '\',
868867
'\a' LIKE '\%' ESCAPE '\',
869868
'%' LIKE '\%' ESCAPE '\',
870869
'\%' LIKE '\%' ESCAPE '\'
870+
----
871+
false false true false
871872

872-
# \ as an explicit escape character is currently not supported
873-
query error DataFusion error: Execution error: LIKE does not support escape_char
873+
query BBBBBB
874874
SELECT
875875
'a' LIKE '\_' ESCAPE '\',
876876
'\a' LIKE '\_' ESCAPE '\',
877877
'_' LIKE '\_' ESCAPE '\',
878878
'\_' LIKE '\_' ESCAPE '\',
879879
'abc' LIKE 'a_c' ESCAPE '\',
880880
'abc' LIKE 'a\_c' ESCAPE '\'
881+
----
882+
false false true false true false
883+
884+
# Only \ is currently supported as an explicit escape character
885+
query error DataFusion error: Execution error: LIKE does not support escape_char other than the backslash \(\\\)
886+
SELECT
887+
'a' LIKE '$%' ESCAPE '$',
888+
'\a' LIKE '$%' ESCAPE '$',
889+
'%' LIKE '$%' ESCAPE '$',
890+
'\%' LIKE '$%' ESCAPE '$'
891+
892+
# Only \ is currently supported as an explicit escape character
893+
query error DataFusion error: Execution error: LIKE does not support escape_char other than the backslash \(\\\)
894+
SELECT
895+
'a' LIKE '$_' ESCAPE '$',
896+
'\a' LIKE '$_' ESCAPE '$',
897+
'_' LIKE '$_' ESCAPE '$',
898+
'\_' LIKE '$_' ESCAPE '$',
899+
'abc' LIKE 'a_c' ESCAPE '$',
900+
'abc' LIKE 'a$_c' ESCAPE '$'
881901

882902
# a LIKE pattern containing escape can never match an empty string
883903
query BBBBB

0 commit comments

Comments
 (0)