Skip to content

fix: avoid query failures from inferred numeric cast bounds - #25528

Open
haohuaijin wants to merge 2 commits into
apache:mainfrom
haohuaijin:fix/25524-inferred-cast-bounds
Open

haohuaijin wants to merge 2 commits into
apache:mainfrom
haohuaijin:fix/25524-inferred-cast-bounds

Conversation

@haohuaijin

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Closes #25524.
Related to #25407 and #22028.

Rationale for this change

A valid query can fail when an inferred numeric interval extends beyond the target CAST type, even though every runtime value fits. For example, an expression that always evaluates to zero can have an estimated upper bound of 1000 and fail when cast to TINYINT during planning.

What changes are included in this PR?

Use safe Arrow casts for numeric interval endpoints in Interval::cast_to, independently of the runtime cast error policy. An endpoint that cannot be represented becomes unbounded, while a valid opposite endpoint is retained. Non-numeric conversions continue to honor the supplied cast options, and errors returned by the cast kernel are still propagated.

What is the testing strategy for this PR?

  • Add regression cases to the existing cast.slt for positive and negative inferred overflow, ordinary CAST runtime errors, and TRY_CAST runtime NULL results.
  • Add unit coverage for numeric narrowing, signed/unsigned conversions, decimal precision and scale changes, fractional values, existing unbounded endpoints, and non-numeric error handling.
  • Check sampled value containment across 14 numeric types with both safe option settings.
  • Validated expr-common unit tests, physical-expr and pruning unit tests, and six relevant SLT files covering casts, filter ordering, and filter pushdown.

Are there any user-facing changes?

Valid queries no longer fail solely because inferred numeric cast endpoints are out of range. Runtime CAST and TRY_CAST behavior is unchanged.

@github-actions github-actions Bot added logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt) labels Sep 20, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.79532% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.38%. Comparing base (ccfe704) to head (de67ec4).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/expr-common/src/interval_arithmetic.rs 84.79% 1 Missing and 25 partials ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #25528    +/-   ##
========================================
  Coverage   82.38%   82.38%            
========================================
  Files        1138     1138            
  Lines      433733   434499   +766     
  Branches   433733   434499   +766     
========================================
+ Hits       357331   357965   +634     
- Misses      54848    54877    +29     
- Partials    21554    21657   +103     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logical-expr Logical plan and expressions sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Narrowing casts of inferred interval endpoints can reject valid queries

2 participants