Skip to content

feat: include source field context in cast errors - #24920

Open
haohuaijin wants to merge 4 commits into
apache:mainfrom
haohuaijin:fix-cast-error-field-context
Open

feat: include source field context in cast errors#24920
haohuaijin wants to merge 4 commits into
apache:mainfrom
haohuaijin:fix-cast-error-field-context

Conversation

@haohuaijin

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

Runtime cast errors currently identify the invalid value and target type but not the source field. This makes failures difficult to diagnose when a query contains multiple casts or an application processes many fields.

Including the source field in the error provides actionable context while retaining the original Arrow error as the cause.

What changes are included in this PR?

This PR adds context to errors returned by CastExpr::evaluate:

  • Resolves the child expression's output field from the input schema when a cast fails.
  • Reports the source field name and the source and target data types.
  • Falls back to the physical expression when a field name cannot be resolved.
  • Preserves the original cast error as the underlying cause.

For example, the error now looks like:

Failed to cast field 'a' from Utf8View to Int32
caused by
Arrow error: Cast error: Cannot cast string '2619.200945' to value of Int32 type

Are these changes tested?

Yes. The existing invalid cast unit test now verifies the complete error, including the source field, source and target data types, and original Arrow error.

The following checks pass:

cargo fmt --all
cargo test -p datafusion-physical-expr invalid_cast_with_options_error
cargo clippy --all-targets --all-features -- -D warnings

The behavior was also verified end to end with the DataFusion CLI using the reproduction from #24919.

Are there any user-facing changes?

Yes. Runtime cast error messages now include the source field and source and target data types. The original cast error remains available as the cause.

There are no public API changes.

@github-actions github-actions Bot added the physical-expr Changes to the physical-expr crates label Sep 3, 2026
@haohuaijin haohuaijin changed the title fix: include source field context in cast errors feat: include source field context in cast errors Sep 3, 2026
@github-actions github-actions Bot added the sqllogictest SQL Logic Tests (.slt) label Sep 3, 2026
@codecov-commenter

codecov-commenter commented Sep 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.28571% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.63%. Comparing base (4a93ade) to head (86926dc).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
datafusion/physical-expr/src/expressions/cast.rs 94.28% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #24920      +/-   ##
==========================================
- Coverage   81.63%   81.63%   -0.01%     
==========================================
  Files        1123     1123              
  Lines      409963   409990      +27     
  Branches   409963   409990      +27     
==========================================
+ Hits       334684   334704      +20     
- Misses      55602    55608       +6     
- Partials    19677    19678       +1     

☔ 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

physical-expr Changes to the physical-expr crates sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve cast errors with source field context

2 participants