feat: rebalance associative bitwise/Add/Multiply chains to avoid protobuf recursion limit#4588
Open
schenksj wants to merge 1 commit into
Open
feat: rebalance associative bitwise/Add/Multiply chains to avoid protobuf recursion limit#4588schenksj wants to merge 1 commit into
schenksj wants to merge 1 commit into
Conversation
) Follow-up to apache#4531 (deep And/Or chains). Protobuf's recursion limit (100) applies to any deeply nested BinaryExpr, so a long left-deep chain of other associative operators overflows the same way when the serialized plan is re-parsed. Extend the rebalancing (flattenAssociative + a balanced O(log n)-depth tree) to: - BitwiseAnd / BitwiseOr / BitwiseXor: always integral and exactly associative, so they reuse the existing createBalancedBinaryExpr directly. - Add / Multiply: gated via isAssociativeAndRebalanceable to integral types in LEGACY (wrapping, modular) eval mode -- the only exactly-associative case. Float isn't associative (Spark's ReorderAssociativeOperator excludes it too); ANSI/TRY make integer overflow position (which the grouping changes) observable; decimal precision grows per op. Those keep the existing left-deep serialization. Add and Multiply emit a MathExpr (eval_mode + return_type) rather than a BinaryExpr, so a new createBalancedMathExpr builds the balanced tree with the chain's uniform type and eval mode at every inner node. Tests mirror apache#4531: project 200-deep chains and assert Comet runs them natively with results matching Spark (which also verifies the associativity guarantee). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.
Closes #4577.
Follow-up to #4531 (deep And/Or chains). Protobuf's recursion limit (100) applies to
any deeply nested
BinaryExpr, so a long left-deep chain of other associative operatorsoverflows the same way when the serialized plan is re-parsed.
What
Extend the rebalancing (
flattenAssociative+ a balanced O(log n)-depth tree) to:they reuse the existing
createBalancedBinaryExprdirectly.isAssociativeAndRebalanceableto integral types inLEGACY (wrapping, modular) eval mode, the only exactly-associative case. Float isn't
associative (Spark's
ReorderAssociativeOperatorexcludes it too); ANSI/TRY makeinteger-overflow position observable and grouping changes it; decimal precision grows
per op. Those keep the existing left-deep serialization.
Add/Multiplyemit aMathExpr(eval_mode + return_type) rather than aBinaryExpr, so a newcreateBalancedMathExprbuilds the balanced tree with the chain's uniform type andeval mode at every inner node.
Tests
Mirror #4531: project 200-deep chains and assert Comet runs them natively with results
matching Spark (which also verifies the associativity guarantee).