You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "adriangb (via GitHub)" <gi...@apache.org> on 2026/06/10 17:27:16 UTC
[PR] fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic [datafusion]
adriangb opened a new pull request, #22887:
URL: https://github.com/apache/datafusion/pull/22887
## Which issue does this PR close?
- Closes #22886.
## Rationale for this change
`SIMILAR TO` panics with `failed to downcast array` whenever its operands end up as arrays of different physical types — e.g. a `NULL` pattern (which even panics at plan time via constant folding) or a `Utf8View` input matched against a non-literal `Utf8` pattern. The root cause is twofold:
1. `Expr::SimilarTo` was listed in the "nothing to coerce" arm of the `TypeCoercion` analyzer, so its operands were never coerced to a common string type (unlike `LIKE` and the regex binary operators).
2. `regex_match_dyn` picks the downcast type from the left array and blind-`.expect()` downcasts the right array to the same type, panicking on any mismatch.
## What changes are included in this PR?
- `TypeCoercionRewriter` now coerces `Expr::SimilarTo` operands to a common string type using `regex_coercion`, mirroring the existing `Expr::Like` handling (including the `Dictionary(_, Utf8)` left-hand-side exception that preserves the dictionary fast path).
- The `regexp_is_match_flag!` / `regexp_is_match_flag_scalar!` kernels in `physical-expr` now return an internal error instead of panicking if a downcast fails (defense in depth).
- The SQL planner's `SIMILAR TO` pattern type check now also accepts `LargeUtf8` and `Utf8View` patterns (previously only `Utf8` and `NULL`); the coercion added above makes these work.
## Are these changes tested?
Yes:
- New sqllogictest cases in `test_files/strings.slt` covering `NULL` patterns / inputs (now return `NULL` instead of panicking), and `Utf8View` / `LargeUtf8` / `Utf8` inputs matched against non-literal patterns of a different string type (previously panicked).
- New `similar_to_for_type_coercion` unit test in `type_coercion.rs` following the existing `like_for_type_coercion` pattern, including the error case for non-string operands.
## Are there any user-facing changes?
- Queries that previously panicked (`'a' SIMILAR TO NULL`, mixed string types with non-literal patterns) now return `NULL` / correct results.
- Non-string `SIMILAR TO` operands now produce a planning error (`There isn't a common type to coerce ... in SIMILAR TO expression`) instead of a runtime panic.
- `LargeUtf8` and `Utf8View` patterns are now accepted by the SQL planner.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #22887:
URL: https://github.com/apache/datafusion/pull/22887#discussion_r3396781746
##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -613,6 +613,33 @@ impl TreeNodeRewriter for TypeCoercionRewriter<'_> {
case_insensitive,
))))
}
+ Expr::SimilarTo(Like {
+ negated,
+ expr,
+ pattern,
+ escape_char,
+ case_insensitive,
+ }) => {
+ let left_type = expr.get_type(self.schema)?;
+ let right_type = pattern.get_type(self.schema)?;
+ let coerced_type = regex_coercion(&left_type, &right_type).ok_or_else(|| {
+ plan_datafusion_err!(
+ "There isn't a common type to coerce {left_type} and {right_type} in SIMILAR TO expression"
+ )
+ })?;
+ let expr = match left_type {
Review Comment:
Keeping the dictionary input unchanged can still send a dictionary array into the array-array regex path, which does not handle dictionary arrays.
For example, `dictionary_string SIMILAR TO NULL` or `dictionary_string SIMILAR TO pattern_column` can still fail.
Should `SIMILAR TO` cast dictionary strings to the coerced string type here, or should we add dictionary support to the array-array regex path?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic [datafusion]
Posted by "Jefffrey (via GitHub)" <gi...@apache.org>.
Jefffrey commented on code in PR #22887:
URL: https://github.com/apache/datafusion/pull/22887#discussion_r3426745459
##########
datafusion/sql/src/expr/mod.rs:
##########
@@ -927,7 +927,10 @@ impl<S: ContextProvider> SqlToRel<'_, S> {
) -> 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 {
+ if !matches!(
Review Comment:
can use [`DataType::is_string`](https://docs.rs/arrow/latest/arrow/datatypes/enum.DataType.html#method.is_string) here, though I wonder if we should be allowing dictionaries here too?
##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -613,6 +613,33 @@ impl TreeNodeRewriter for TypeCoercionRewriter<'_> {
case_insensitive,
))))
}
+ Expr::SimilarTo(Like {
+ negated,
+ expr,
+ pattern,
+ escape_char,
+ case_insensitive,
+ }) => {
+ let left_type = expr.get_type(self.schema)?;
+ let right_type = pattern.get_type(self.schema)?;
+ let coerced_type = regex_coercion(&left_type, &right_type).ok_or_else(|| {
+ plan_datafusion_err!(
+ "There isn't a common type to coerce {left_type} and {right_type} in SIMILAR TO expression"
+ )
+ })?;
+ let expr = match left_type {
+ DataType::Dictionary(_, inner) if *inner == DataType::Utf8 => expr,
Review Comment:
it seems a bit odd here to limit it only to `Utf8` and not other string types
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic [datafusion]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on PR #22887:
URL: https://github.com/apache/datafusion/pull/22887#issuecomment-5028821862
Closing in favor of https://github.com/apache/datafusion/pull/23704 and followups. Thanks for reviewing @Jefffrey
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic [datafusion]
Posted by "kumarUjjawal (via GitHub)" <gi...@apache.org>.
kumarUjjawal commented on code in PR #22887:
URL: https://github.com/apache/datafusion/pull/22887#discussion_r3397172088
##########
datafusion/physical-expr/src/expressions/binary/kernels.rs:
##########
@@ -273,12 +268,37 @@ macro_rules! regexp_is_match_flag {
}};
}
+/// Unpack a dictionary array to its value type, leaving other arrays as-is
+fn flatten_dictionary(array: &ArrayRef) -> Result<ArrayRef> {
+ match array.data_type() {
+ DataType::Dictionary(_, value_type) => Ok(cast(array, value_type)?),
+ _ => Ok(Arc::clone(array)),
+ }
+}
+
pub(crate) fn regex_match_dyn(
left: &ArrayRef,
right: &ArrayRef,
not_match: bool,
flag: bool,
) -> Result<ArrayRef> {
+ // Type coercion deliberately preserves `Dictionary(_, Utf8)` inputs so
+ // that literal patterns can use the dictionary fast path in
+ // `regex_match_dyn_scalar`. Here every row has its own pattern, so the
+ // dictionary encoding offers no shortcut: flatten the inputs, and if the
+ // dictionary's value type differs from the coerced pattern type (e.g.
+ // `Dictionary(Int32, Utf8)` matched against `Utf8View` patterns), cast to
+ // a common string type.
+ if matches!(left.data_type(), DataType::Dictionary(_, _))
+ || matches!(right.data_type(), DataType::Dictionary(_, _))
+ {
+ let left = flatten_dictionary(left)?;
+ let mut right = flatten_dictionary(right)?;
+ if right.data_type() != left.data_type() {
+ right = cast(&right, left.data_type())?;
Review Comment:
if the left side is `Dictionary(Int32, Utf8)` and the pattern is `LargeUtf8`, type coercion should keep the common type as `LargeUtf8`. But we are flatenning the dictionary to `Utf8`, then casts the `LargeUtf8` pattern back to `Utf8`. That can fail for valid large strings.
We can cast the dictionary side to the already-coerced pattern type, or compute the common string type again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic [datafusion]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on code in PR #22887:
URL: https://github.com/apache/datafusion/pull/22887#discussion_r3396994211
##########
datafusion/optimizer/src/analyzer/type_coercion.rs:
##########
@@ -613,6 +613,33 @@ impl TreeNodeRewriter for TypeCoercionRewriter<'_> {
case_insensitive,
))))
}
+ Expr::SimilarTo(Like {
+ negated,
+ expr,
+ pattern,
+ escape_char,
+ case_insensitive,
+ }) => {
+ let left_type = expr.get_type(self.schema)?;
+ let right_type = pattern.get_type(self.schema)?;
+ let coerced_type = regex_coercion(&left_type, &right_type).ok_or_else(|| {
+ plan_datafusion_err!(
+ "There isn't a common type to coerce {left_type} and {right_type} in SIMILAR TO expression"
+ )
+ })?;
+ let expr = match left_type {
Review Comment:
good point! i've gone with your second suggestion (add dictionary support to the array-array regex path) in 8ca1f7e6f6d2235347030bf0ac96f555746ba395
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic [datafusion]
Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway commented on PR #22887:
URL: https://github.com/apache/datafusion/pull/22887#issuecomment-5074715567
@adriangb
> Closing in favor of https://github.com/apache/datafusion/pull/23704 and followups.
I went ahead and closed this out; feel free to revert if that was incorrect.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic [datafusion]
Posted by "neilconway (via GitHub)" <gi...@apache.org>.
neilconway closed pull request #22887: fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic
URL: https://github.com/apache/datafusion/pull/22887
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: coerce SIMILAR TO operands to a common string type to avoid 'failed to downcast array' panic [datafusion]
Posted by "adriangb (via GitHub)" <gi...@apache.org>.
adriangb commented on PR #22887:
URL: https://github.com/apache/datafusion/pull/22887#issuecomment-4681492339
@kumarUjjawal wonder if you could review this?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org