From c2ccd0d7e641e646fc948e2da100a7fecc590e9c Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Fri, 8 Nov 2024 16:29:57 +0100 Subject: [PATCH 1/2] Support LIKE with ESCAPE `\` Other escape characters are currently not supported. --- datafusion/physical-expr/src/planner.rs | 5 ++-- .../test_files/string/string_literal.slt | 28 ++++++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/datafusion/physical-expr/src/planner.rs b/datafusion/physical-expr/src/planner.rs index bffc2c46fc1e..c07986211d41 100644 --- a/datafusion/physical-expr/src/planner.rs +++ b/datafusion/physical-expr/src/planner.rs @@ -200,8 +200,9 @@ pub fn create_physical_expr( escape_char, case_insensitive, }) => { - if escape_char.is_some() { - return exec_err!("LIKE does not support escape_char"); + // `\` is the implicit escape, see https://github.com/apache/datafusion/issues/13291 + if escape_char.unwrap_or('\\') != '\\' { + return exec_err!("LIKE does not support escape_char other than the backslash (\\)"); } let physical_expr = create_physical_expr(expr, input_dfschema, execution_props)?; diff --git a/datafusion/sqllogictest/test_files/string/string_literal.slt b/datafusion/sqllogictest/test_files/string/string_literal.slt index 57261470f6eb..a34ad6be66e3 100644 --- a/datafusion/sqllogictest/test_files/string/string_literal.slt +++ b/datafusion/sqllogictest/test_files/string/string_literal.slt @@ -861,16 +861,16 @@ SELECT ---- false false true false true false -# \ as an explicit escape character is currently not supported -query error DataFusion error: Execution error: LIKE does not support escape_char +query BBBB SELECT 'a' LIKE '\%' ESCAPE '\', '\a' LIKE '\%' ESCAPE '\', '%' LIKE '\%' ESCAPE '\', '\%' LIKE '\%' ESCAPE '\' +---- +false false true false -# \ as an explicit escape character is currently not supported -query error DataFusion error: Execution error: LIKE does not support escape_char +query BBBBBB SELECT 'a' LIKE '\_' ESCAPE '\', '\a' LIKE '\_' ESCAPE '\', @@ -878,6 +878,26 @@ SELECT '\_' LIKE '\_' ESCAPE '\', 'abc' LIKE 'a_c' ESCAPE '\', 'abc' LIKE 'a\_c' ESCAPE '\' +---- +false false true false true false + +# Only \ is currently supported as an explicit escape character +query error DataFusion error: Execution error: LIKE does not support escape_char other than the backslash \(\\\) +SELECT + 'a' LIKE '$%' ESCAPE '$', + '\a' LIKE '$%' ESCAPE '$', + '%' LIKE '$%' ESCAPE '$', + '\%' LIKE '$%' ESCAPE '$' + +# Only \ is currently supported as an explicit escape character +query error DataFusion error: Execution error: LIKE does not support escape_char other than the backslash \(\\\) +SELECT + 'a' LIKE '$_' ESCAPE '$', + '\a' LIKE '$_' ESCAPE '$', + '_' LIKE '$_' ESCAPE '$', + '\_' LIKE '$_' ESCAPE '$', + 'abc' LIKE 'a_c' ESCAPE '$', + 'abc' LIKE 'a$_c' ESCAPE '$' # a LIKE pattern containing escape can never match an empty string query BBBBB From 3c2ffe4b0372bed730a7e9bebdadda84934f9166 Mon Sep 17 00:00:00 2001 From: Piotr Findeisen Date: Sat, 23 Nov 2024 17:57:47 +0100 Subject: [PATCH 2/2] fmt --- datafusion/physical-expr/src/planner.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/datafusion/physical-expr/src/planner.rs b/datafusion/physical-expr/src/planner.rs index c07986211d41..add6c18b329c 100644 --- a/datafusion/physical-expr/src/planner.rs +++ b/datafusion/physical-expr/src/planner.rs @@ -202,7 +202,9 @@ pub fn create_physical_expr( }) => { // `\` is the implicit escape, see https://github.com/apache/datafusion/issues/13291 if escape_char.unwrap_or('\\') != '\\' { - return exec_err!("LIKE does not support escape_char other than the backslash (\\)"); + return exec_err!( + "LIKE does not support escape_char other than the backslash (\\)" + ); } let physical_expr = create_physical_expr(expr, input_dfschema, execution_props)?;