Fix ordering for UNION ALL over heterogeneous constants#23528
Open
vadimpiven wants to merge 1 commit into
Open
Fix ordering for UNION ALL over heterogeneous constants#23528vadimpiven wants to merge 1 commit into
vadimpiven wants to merge 1 commit into
Conversation
`add_sort_above` and `add_sort_above_with_distribution` pruned every expression that equivalence properties reported as constant, via `is_expr_constant(..).is_none()`. That treats `AcrossPartitions::Heterogeneous` the same as `AcrossPartitions::Uniform`, which is wrong. A `Heterogeneous` constant is constant *within* each partition but varies *across* partitions (e.g. `sample` in `SELECT 1 AS sample, c FROM t UNION ALL SELECT 2 AS sample, c FROM t`). Both helpers build a partition-preserving `SortExec` whose output is merged across partitions downstream, where such a column is still the leading ordering discriminator. Dropping it silently discards the global ordering, producing non-deterministic output order for `ORDER BY sample, c` once the plan is rooted at a sink / output requirement. Only `AcrossPartitions::Uniform` is globally constant and safe to drop. Introduce `is_uniform_constant` and use it at both prune sites. Adds a regression test that builds a union with a heterogeneous-constant column and asserts the key survives `add_sort_above`.
Contributor
Author
|
@alamb Hi! May I ask you to have a look at this tiny bugfix? |
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.
Which issue does this PR close?
Rationale for this change
add_sort_aboveandadd_sort_above_with_distributionin physical-optimizer prune every expression thatEquivalenceProperties::is_expr_constantreports as constant.This treats
AcrossPartitions::Heterogeneousthe same asAcrossPartitions::Uniform, which is incorrect.Check an example:
The union reports sample as a
Heterogeneousconstant and so it gets pruned, which breaksORDER BY.What changes are included in this PR?
Helper that activates pruning only for
AcrossPartitions::Uniform, use it at both prune sites (add_sort_aboveandadd_sort_above_with_distribution) soHeterogeneousconstants are retained.Are these changes tested?
Yes, a new unit-test added.
Are there any user-facing changes?
No API changes, just an internal logic bug-fix.