Apache DataFusion SQL Query Engine
fix: accept LargeUtf8 and Utf8View patterns in SIMILAR TO planning (#23735)
kid5f7feda66b1f
diff --git a/datafusion/sql/src/expr/mod.rs b/datafusion/sql/src/expr/mod.rsindex c00dcb82ff..c2e4822f76 100644--- a/datafusion/sql/src/expr/mod.rs+++ b/datafusion/sql/src/expr/mod.rs@@ -1008,10 +1008,6 @@ impl<S: ContextProvider> SqlToRel<'_, S> {planner_context: &mut PlannerContext,) -> Result<Expr> {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.sltindex 7ec0f5f1db..6a56fc2407 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 argumentsSELECT * 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 okDROP 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 expressionSELECT 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 expressionSELECT 'a' SIMILAR TO 1;