diff --git a/datafusion/sql/src/expr/mod.rs b/datafusion/sql/src/expr/mod.rs index c00dcb82ff3a9..c2e4822f76b99 100644 --- a/datafusion/sql/src/expr/mod.rs +++ b/datafusion/sql/src/expr/mod.rs @@ -1008,10 +1008,6 @@ impl SqlToRel<'_, S> { planner_context: &mut PlannerContext, ) -> Result { let pattern = self.sql_expr_to_logical_expr(pattern, schema, planner_context)?; - let pattern_type = pattern.get_type(schema)?; - if pattern_type != DataType::Utf8 && pattern_type != DataType::Null { - return plan_err!("Invalid pattern in SIMILAR TO expression"); - } let escape_char = match escape_char.map(|v| v.value) { Some(Value::SingleQuotedString(char)) if char.len() == 1 => { Some(char.chars().next().unwrap()) diff --git a/datafusion/sqllogictest/test_files/type_coercion.slt b/datafusion/sqllogictest/test_files/type_coercion.slt index 7ec0f5f1dba30..6a56fc2407a94 100644 --- a/datafusion/sqllogictest/test_files/type_coercion.slt +++ b/datafusion/sqllogictest/test_files/type_coercion.slt @@ -304,7 +304,9 @@ query error does not support zero arguments SELECT * FROM (SELECT 1) WHERE TRY_CAST(STARTS_WITH() AS INT) = 1; ################################################################### -## SIMILAR TO type coercion (https://github.com/apache/datafusion/issues/22886) +## SIMILAR TO type coercion +## https://github.com/apache/datafusion/issues/22886 +## https://github.com/apache/datafusion/issues/23732 ################################################################### # NULL pattern is coerced to a typed NULL and evaluates to NULL instead of panicking @@ -349,6 +351,44 @@ SELECT arrow_cast(t.s, 'Dictionary(Int32, Utf8)') SIMILAR TO p.pat FROM t CROSS ---- true +# non-scalar string-like patterns are coerced by the analyzer +query B +SELECT t.s SIMILAR TO arrow_cast(p.pat, 'Utf8View') FROM t CROSS JOIN p; +---- +true + +query B +SELECT t.s SIMILAR TO arrow_cast(p.pat, 'LargeUtf8') FROM t CROSS JOIN p; +---- +true + +query B +SELECT t.s NOT SIMILAR TO arrow_cast(p.pat, 'Utf8View') FROM t CROSS JOIN p; +---- +false + +query B +SELECT t.s SIMILAR TO arrow_cast(p.pat, 'Dictionary(Int32, Utf8)') FROM t CROSS JOIN p; +---- +true + +# NULL patterns (literal or Null-typed non-scalar) evaluate to NULL +query B +SELECT t.s SIMILAR TO NULL FROM t; +---- +NULL + +statement ok +CREATE TABLE pn AS SELECT NULL AS pat; + +query B +SELECT t.s SIMILAR TO pn.pat FROM t CROSS JOIN pn; +---- +NULL + +statement ok +DROP TABLE pn; + statement ok DROP TABLE t; @@ -359,6 +399,6 @@ DROP TABLE p; query error There isn't a common type to coerce Int64 and Utf8 in SIMILAR TO expression SELECT 1 SIMILAR TO 'a'; -# a non-string pattern is rejected even earlier, during SQL planning -query error Invalid pattern in SIMILAR TO expression +# a non-string pattern is rejected by the analyzer +query error There isn't a common type to coerce Utf8 and Int64 in SIMILAR TO expression SELECT 'a' SIMILAR TO 1;