Skip to content

fix: left-fold n-ary arithmetic in the Substrait consumer - #215

Open
Adam-Alani wants to merge 1 commit into
branch-55from
adam.alani/left-fold-nary-arithmetic
Open

Adam-Alani wants to merge 1 commit into
branch-55from
adam.alani/left-fold-nary-arithmetic

Conversation

@Adam-Alani

@Adam-Alani Adam-Alani commented Nov 16, 2026 •

Copy link
Copy Markdown

SQL evaluates add, subtract, multiply, divide, mod, and power from left to right. Rebuilding an n-ary call as a balanced tree changes float results. and/or stay balanced.

Which issue does this PR close?

No GitHub issue. Tracked in Jira: QSEM-973.

Rationale for this change

Substrait plans from the DD producer can carry a left spine of arithmetic as one n-ary call, for example add(a, b, c, d). The consumer rebuilt that call as a balanced tree, so ((a + b) + c) + d became (a + b) + (c + d). For add, subtract, multiply, divide, and mod, floats give different results for the two shapes, and the balanced shape does not match SQL's left-to-right order.

Power had a separate gap. power is a binary UDF, so an n-ary power call was passed to it with all of its arguments, which its two-argument signature does not accept.

What changes are included in this PR?

  • from_scalar_function left-folds add, subtract, multiply, divide, mod, and modulus with more than two arguments, instead of calling arg_list_to_binary_op_tree.
  • power with more than two arguments is folded into nested binary power calls before the generic UDF path. It uses the power UDF and falls back to pow.
  • and and or keep the balanced tree.

Only datafusion/substrait/src/logical_plan/consumer/expr/scalar_function.rs changes.

Are these changes tested?

New unit tests in scalar_function.rs:

  • nary_arithmetic_is_left_folded: for each left-folded operator, a 4-argument call produces the left-deep chain (((1 op 2) op 3) op 4).
  • nary_or_stays_balanced: a 4-argument or stays a balanced tree.
  • nary_power_is_left_folded: a 4-argument power becomes nested binary power calls, left to right.

cargo test -p datafusion-substrait --lib logical_plan::consumer::expr::scalar_function passes: 10 tests, including the existing balanced-tree and large-argument tests. The full crate test suite has not been run.

Are there any user-facing changes?

Only for Substrait plans with more than two arguments to an arithmetic function. Those now evaluate left to right, so float results match SQL order. Integer results do not change. power with more than two arguments now plans instead of failing. No public API change, so no api change label.

SQL evaluates add, subtract, multiply, divide, mod, and power from
left to right. Rebuilding an n-ary call as a balanced tree changes
float results. and/or stay balanced.
@codecov-commenter

codecov-commenter commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.55224% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.23%. Comparing base (e7af2a2) to head (a885bfc).

Files with missing lines Patch % Lines
.../src/logical_plan/consumer/expr/scalar_function.rs 89.55% 8 Missing and 6 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           branch-55     #215    +/-   ##
===========================================
  Coverage      81.22%   81.23%            
===========================================
  Files           1111     1111            
  Lines         389078   389211   +133     
  Branches      389078   389211   +133     
===========================================
+ Hits          316046   316170   +124     
- Misses         54358    54361     +3     
- Partials       18674    18680     +6     

☔ 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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants