Repository navigation
fix: left-fold n-ary arithmetic in the Substrait consumer - #215
Open
Adam-Alani wants to merge 1 commit into
Open
Adam-Alani wants to merge 1 commit into
Adam-Alani wants to merge 1 commit into
Conversation
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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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.
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) + dbecame(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.
poweris a binary UDF, so an n-arypowercall 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_functionleft-foldsadd,subtract,multiply,divide,mod, andmoduluswith more than two arguments, instead of callingarg_list_to_binary_op_tree.powerwith more than two arguments is folded into nested binarypowercalls before the generic UDF path. It uses thepowerUDF and falls back topow.andandorkeep the balanced tree.Only
datafusion/substrait/src/logical_plan/consumer/expr/scalar_function.rschanges.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-argumentorstays a balanced tree.nary_power_is_left_folded: a 4-argumentpowerbecomes nested binarypowercalls, left to right.cargo test -p datafusion-substrait --lib logical_plan::consumer::expr::scalar_functionpasses: 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.
powerwith more than two arguments now plans instead of failing. No public API change, so noapi changelabel.