Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve ordering equivalencies on with_reorder #13769

Closed
gokselk opened this issue Dec 13, 2024 · 0 comments · Fixed by #13770
Closed

Preserve ordering equivalencies on with_reorder #13769

gokselk opened this issue Dec 13, 2024 · 0 comments · Fixed by #13770
Labels
enhancement New feature or request

Comments

@gokselk
Copy link
Contributor

gokselk commented Dec 13, 2024

Is your feature request related to a problem or challenge?

When applying new orderings via with_reorder(), the current implementation discards all existing ordering information and replaces it with the new ordering. This is suboptimal because in many cases, parts of the existing ordering remain valid and could be preserved.

For example, if we have data ordered by [a ASC, b DESC] and apply a new ordering [a ASC], we currently lose the information that the data is also ordered by b DESC, even though this ordering is still valid and could be valuable for query optimization.

This overly conservative approach can lead to unnecessary sorting operations in query execution, impacting query performance.

Describe the solution you'd like

Enhance with_reorder() to preserve valid ordering information by:

  1. Filtering out constant expressions from ordering considerations (they don't affect physical ordering)
  2. Preserving valid suffixes from existing orderings when they're compatible with the new ordering
  3. Considering expression equivalences when comparing orderings

For example:

  • When applying [a ASC] to existing [a ASC, b DESC], preserve b DESC
  • When columns a and b are equivalent and applying [b ASC] to existing [a ASC, c DESC], preserve c DESC
  • When column a is constant, applying [a ASC, b ASC] should only consider [b ASC]

Describe alternatives you've considered

Only preserve exact matching prefixes without considering equivalences

Additional context

This optimization is particularly important for complex queries with multiple ordering requirements and transformations, where maintaining ordering information can significantly reduce the number of required sort operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant