Conversation
check_support recursed through a CastExpr without inspecting the cast target type, so an expression casting through a type that interval arithmetic cannot order (for example Utf8) was reported as analyzable. Interval::cast_to then converted both endpoints individually and Interval::try_new rejected the result, failing a valid query with an internal error about reversed bounds. Require is_datatype_supported for the cast target before recursing. Closes apache#25532.
haohuaijin
approved these changes
Sep 21, 2026
haohuaijin
left a comment
Contributor
There was a problem hiding this comment.
Thanks @shoemoney , LGTM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue does this PR close?
Rationale for this change
A view that exposes integer ids as strings, queried with an integer predicate, fails with an internal error even though every value is valid:
The cause is in
check_supportindatafusion/physical-expr/src/intervals/utils.rs. On main theCastExprarm (lines 55 and 56) is:It recurses into the operand and never looks at
cast.cast_type().is_datatype_supported(lines 93 to 110) allows the integer, float and temporal types, and deliberately does not includeUtf8, but that allowlist is only ever consulted for columns and literals, never for a cast target.So
check_supportreturnstruefor the expression above.filter.rscallsanalyze(...)?inside that branch, with no fallback, andInterval::cast_toindatafusion/expr-common/src/interval_arithmetic.rsconverts the two endpoints independently before handing them toInterval::try_new. The numeric interval[2, 10]derived from Parquet statistics becomes["2", "10"], and under string ordering"2" > "10", sotry_newrejects it and the query fails.One nuance worth stating, because it explains why an obvious test does not reproduce it. The simple shape
CAST(id AS VARCHAR) = '5'is already rejected, because the literal'5'isUtf8and the existing literal check catches it. Reproducing the bug needs the unsupported type to appear only in the middle of a cast chain whose outermost type is supported, which is exactly the view scenario in the issue: the view supplies the inner cast to string, and the user predicate supplies the outer cast back to integer. Nothing in the expression tree is then string typed at a point the current checks look at.What changes are included in this PR?
The
CastExprarm now requires the cast target type to be one interval arithmetic can order before it recurses:This is strictly analysis disabling.
check_supportis a gate that decides whether to attempt interval analysis at all, and the change can only ever turn atrueinto afalse. It cannot alter any interval that is computed today, so no currently valid numeric or temporal interval can regress. The only effect is that an expression casting through a type outsideis_datatype_supportedfalls back to not being analyzed, which is the behavior these expressions should have had all along.This also follows the direction of #21520, which last touched this file and extended the same allowlist for temporal types.
What is the testing strategy for this PR?
Two unit tests in a new
testsmodule indatafusion/physical-expr/src/intervals/utils.rs, covering both the direct unsupported target and the round trip through an unsupported type, plus a positive case asserting supported cast targets are still analyzed. Plus an end to end case indatafusion/sqllogictest/test_files/cast.sltthat reproduces the issue over Parquet, so the statistics derived interval is real rather than synthetic.Unit tests, against unmodified source:
sqllogictest, against unmodified source:
That is the exact error from the issue.
With the fix,
cargo test -p datafusion-physical-expr:The baseline on
mainfor the same crate was 1136 passing in theintervalsfilter, and 1138 after adding the two new tests, so nothing existing changed status.cargo test --test sqllogictests -- cast.sltpasses, andcargo fmt --all -- --checkandcargo clippy -p datafusion-physical-expr --all-targets --all-features -- -D warningsare both clean.Are there any user-facing changes?
Yes, in the sense that a query that previously failed with an internal error now returns the correct rows. There are no API changes.