Skip to content

Commit 8464286

Browse files
u70b3kosiew
authored andcommitted
fix: accept LargeUtf8 and Utf8View patterns in SIMILAR TO planning (apache#23735)
## Which issue does this PR close? - Closes apache#23732 ## Rationale for this change Follow-up to apache#23704, which made the `TypeCoercion` analyzer coerce `SIMILAR TO` operands to a common string type. The SQL planner still rejects `LargeUtf8` and `Utf8View` **patterns** before the analyzer ever runs: ```sql SELECT s FROM test, patterns WHERE s SIMILAR TO arrow_cast(pat, 'Utf8View'); -- error: Invalid pattern in SIMILAR TO expression ``` Now that the analyzer coerces both operands, this planner restriction is unnecessary (and inconsistent with `LIKE`, whose planner path has no such check). ## What changes are included in this PR? - `datafusion/sql/src/expr/mod.rs`: the pattern-type check in `sql_similarto_to_expr` now accepts `Utf8` / `LargeUtf8` / `Utf8View` / `Null` instead of only `Utf8` / `Null`. - `datafusion/sqllogictest/test_files/type_coercion.slt`: regression tests with non-literal `Utf8View` and `LargeUtf8` patterns (plus a `NOT SIMILAR TO` case), next to the existing apache#22886 regression tests. The planner relaxation and tests were originally part of apache#22887; this PR lands the remaining piece of it. ## Are these changes tested? Yes — new sqllogictest cases in `type_coercion.slt`. The existing planner rejection of non-string patterns (`SELECT 'a' SIMILAR TO 1`) still errors as before. ## Are there any user-facing changes? Yes: `SIMILAR TO` queries with non-literal `LargeUtf8` / `Utf8View` patterns that previously failed during SQL planning with `Invalid pattern in SIMILAR TO expression` now plan and execute successfully. No breaking API changes.
1 parent c8c1521 commit 8464286

2 files changed

Lines changed: 43 additions & 7 deletions

File tree

‎datafusion/sql/src/expr/mod.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,10 +1008,6 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
10081008
planner_context: &mut PlannerContext,
10091009
) -> Result<Expr> {
10101010
let pattern = self.sql_expr_to_logical_expr(pattern, schema, planner_context)?;
1011-
let pattern_type = pattern.get_type(schema)?;
1012-
if pattern_type != DataType::Utf8 && pattern_type != DataType::Null {
1013-
return plan_err!("Invalid pattern in SIMILAR TO expression");
1014-
}
10151011
let escape_char = match escape_char.map(|v| v.value) {
10161012
Some(Value::SingleQuotedString(char)) if char.len() == 1 => {
10171013
Some(char.chars().next().unwrap())

‎datafusion/sqllogictest/test_files/type_coercion.slt‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,9 @@ query error does not support zero arguments
304304
SELECT * FROM (SELECT 1) WHERE TRY_CAST(STARTS_WITH() AS INT) = 1;
305305

306306
###################################################################
307-
## SIMILAR TO type coercion (https://github.com/apache/datafusion/issues/22886)
307+
## SIMILAR TO type coercion
308+
## https://github.com/apache/datafusion/issues/22886
309+
## https://github.com/apache/datafusion/issues/23732
308310
###################################################################
309311

310312
# 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
349351
----
350352
true
351353

354+
# non-scalar string-like patterns are coerced by the analyzer
355+
query B
356+
SELECT t.s SIMILAR TO arrow_cast(p.pat, 'Utf8View') FROM t CROSS JOIN p;
357+
----
358+
true
359+
360+
query B
361+
SELECT t.s SIMILAR TO arrow_cast(p.pat, 'LargeUtf8') FROM t CROSS JOIN p;
362+
----
363+
true
364+
365+
query B
366+
SELECT t.s NOT SIMILAR TO arrow_cast(p.pat, 'Utf8View') FROM t CROSS JOIN p;
367+
----
368+
false
369+
370+
query B
371+
SELECT t.s SIMILAR TO arrow_cast(p.pat, 'Dictionary(Int32, Utf8)') FROM t CROSS JOIN p;
372+
----
373+
true
374+
375+
# NULL patterns (literal or Null-typed non-scalar) evaluate to NULL
376+
query B
377+
SELECT t.s SIMILAR TO NULL FROM t;
378+
----
379+
NULL
380+
381+
statement ok
382+
CREATE TABLE pn AS SELECT NULL AS pat;
383+
384+
query B
385+
SELECT t.s SIMILAR TO pn.pat FROM t CROSS JOIN pn;
386+
----
387+
NULL
388+
389+
statement ok
390+
DROP TABLE pn;
391+
352392
statement ok
353393
DROP TABLE t;
354394

@@ -359,6 +399,6 @@ DROP TABLE p;
359399
query error There isn't a common type to coerce Int64 and Utf8 in SIMILAR TO expression
360400
SELECT 1 SIMILAR TO 'a';
361401

362-
# a non-string pattern is rejected even earlier, during SQL planning
363-
query error Invalid pattern in SIMILAR TO expression
402+
# a non-string pattern is rejected by the analyzer
403+
query error There isn't a common type to coerce Utf8 and Int64 in SIMILAR TO expression
364404
SELECT 'a' SIMILAR TO 1;

0 commit comments

Comments
 (0)