Preserve ordering equivalencies on with_reorder
#13770
Open
+296
−3
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.
Which issue does this PR close?
Closes #13769.
Rationale for this change
The current implementation of
with_reorder
discards all existing ordering information when applying a new ordering. This is overly conservative since in many cases, parts of the existing ordering remain valid and could be preserved. Preserving valid orderings is important for query optimization as it allows the optimizer to avoid unnecessary sorting operations.What changes are included in this PR?
with_reorder()
to preserve valid suffixes from existing orderings when they don't conflict with the new orderingexprs_equal()
helper method toEquivalenceGroup
for comparing expressions considering equivalence classesAre these changes tested?
Yes, the PR includes extensive test coverage:
test_with_reorder_constant_filtering
: Tests filtering of constant expressionstest_with_reorder_preserve_suffix
: Verifies preservation of valid ordering suffixestest_with_reorder_equivalent_expressions
: Tests handling of equivalent expressionstest_with_reorder_incompatible_prefix
: Ensures proper handling of incompatible orderingstest_with_reorder_comprehensive
: Tests complex interactions between constants, equivalences, and multiple orderingsAre there any user-facing changes?
This change is an internal optimization that preserves more ordering information. While there are no direct API changes, users may notice improved query performance in scenarios where ordering information is better preserved through operations that modify sort orders.