Skip to content

Commit a209834

Browse files
committed
fix: delegate SIMILAR TO validation to analyzer
1 parent 21f48ec commit a209834

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,15 +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-
// The analyzer coerces both operands to a common string type, so any
1013-
// string-typed (or untyped NULL) pattern is accepted here.
1014-
if !matches!(
1015-
pattern_type,
1016-
DataType::Utf8 | DataType::LargeUtf8 | DataType::Utf8View | DataType::Null
1017-
) {
1018-
return plan_err!("Invalid pattern in SIMILAR TO expression");
1019-
}
10201011
let escape_char = match escape_char.map(|v| v.value) {
10211012
Some(Value::SingleQuotedString(char)) if char.len() == 1 => {
10221013
Some(char.chars().next().unwrap())

‎datafusion/sqllogictest/test_files/type_coercion.slt‎

Lines changed: 12 additions & 7 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,8 +351,7 @@ SELECT arrow_cast(t.s, 'Dictionary(Int32, Utf8)') SIMILAR TO p.pat FROM t CROSS
349351
----
350352
true
351353

352-
# non-scalar Utf8View / LargeUtf8 patterns are accepted by the SQL planner and
353-
# coerced by the analyzer (https://github.com/apache/datafusion/issues/23732)
354+
# non-scalar string-like patterns are coerced by the analyzer
354355
query B
355356
SELECT t.s SIMILAR TO arrow_cast(p.pat, 'Utf8View') FROM t CROSS JOIN p;
356357
----
@@ -366,8 +367,12 @@ SELECT t.s NOT SIMILAR TO arrow_cast(p.pat, 'Utf8View') FROM t CROSS JOIN p;
366367
----
367368
false
368369

369-
# NULL patterns (literal or Null-typed non-scalar) are accepted by the planner
370-
# and evaluate to NULL
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
371376
query B
372377
SELECT t.s SIMILAR TO NULL FROM t;
373378
----
@@ -394,6 +399,6 @@ DROP TABLE p;
394399
query error There isn't a common type to coerce Int64 and Utf8 in SIMILAR TO expression
395400
SELECT 1 SIMILAR TO 'a';
396401

397-
# a non-string pattern is rejected even earlier, during SQL planning
398-
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
399404
SELECT 'a' SIMILAR TO 1;

0 commit comments

Comments
 (0)