Skip to content

fix(substrait): reject incompatible decimal output types - #25087

Draft
Satyr09 wants to merge 2 commits into
apache:mainfrom
Satyr09:daipayan/fix/substrait-decimal-output-type
Draft

fix(substrait): reject incompatible decimal output types#25087
Satyr09 wants to merge 2 commits into
apache:mainfrom
Satyr09:daipayan/fix/substrait-decimal-output-type

Conversation

@Satyr09

@Satyr09 Satyr09 commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

[ This causes a lot of previously passing queries to potentially fail - and as such may not be the best option - keeping this in draft while I work on a better fix ]

The Substrait consumer can accept decimal arithmetic while silently changing its declared result precision and scale. For example, division declared as decimal(21,8) becomes a native DataFusion expression returning decimal(15,6).

This PR rejects conversions that cannot preserve the declared result type. Casting the result alone would not recover fractional digits already lost during calculation or prevent intermediate overflow.

What changes are included in this PR?

  • Validate declared decimal result types when converting addition, subtraction, multiplication, division, and modulus into native binary expressions.
  • Reject incompatible declarations with an error identifying the function and its declared and derived types.
  • Validate decimal precision and scale, including checking protobuf integers before narrowing conversions.
  • Update ten existing TPC-H fixture tests to expect rejection of incompatible declarations.

Plans with matching result types retain their existing behavior. Plans omitting output_type remain accepted for compatibility. Implementing Substrait-specific decimal arithmetic is outside this change’s scope.

What is the testing strategy for this PR?

Added consumer tests covering the issue’s five examples, matching declarations, malformed types, decimal-width differences, missing declarations, and nested mismatches.
An integration test executes native decimal arithmetic through producer/consumer round trips, checks result types and values, and verifies acceptance or rejection after changing the declared output types.
Local validation:

  • Formatting and diff checks passed.
  • All 275 Substrait tests passed; six existing tests remain ignored.
  • Clippy for Substrait targets with --no-deps passed.
  • Broader Clippy checking failed on an existing unnecessary semicolon in datafusion/common/src/rounding.rs.
  • Extended workspace testing encountered failures in unchanged memory-limit tests; the run was stopped, so full workspace validation is incomplete.

Are there any user-facing changes?

Yes. Imported plans whose declared decimal arithmetic result types differ from DataFusion’s native result types now fail during conversion instead of silently producing a different type. Native SQL arithmetic is unchanged.

@github-actions github-actions Bot added the substrait Changes to the substrait crate label Sep 8, 2026
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.06557% with 102 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.75%. Comparing base (8a92281) to head (555b760).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
.../src/logical_plan/consumer/expr/scalar_function.rs 84.02% 8 Missing and 65 partials ⚠️
datafusion/substrait/src/logical_plan/decimal.rs 89.45% 9 Missing and 18 partials ⚠️
.../src/logical_plan/producer/expr/scalar_function.rs 89.47% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #25087      +/-   ##
==========================================
+ Coverage   81.72%   81.75%   +0.02%     
==========================================
  Files        1127     1129       +2     
  Lines      416519   417375     +856     
  Branches   416519   417375     +856     
==========================================
+ Hits       340401   341208     +807     
+ Misses      56115    56025      -90     
- Partials    20003    20142     +139     

☔ 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

substrait Changes to the substrait crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Substrait decimal arithmetic derives return types that differ from the referenced functions

2 participants