Repository navigation
fix: keep scan order when the optimizer adds parallelism - #9819
Conversation
a9171db to
b2b0907
Compare
| options.optimizer.enable_round_robin_repartition = false; | ||
| Ok(optimize(plan, &options)?) |
There was a problem hiding this comment.
question: why do we need to re-run optimize here when it was just run above?
There was a problem hiding this comment.
I think it optimizes first to see if the optimized plan is a special plan that wouldn't disturb the order anyways. If the optimized plan is not a speical order-preserving plan then it disables the repartition and re-optimizes.
In other words it has to optimize to determine the value of keeps_order which it uses to decide if it needs to optimize without repartition or not.
That being said, it would be nice if there was a way to avoid this double optimize and it feels like there should be.
westonpace
left a comment
There was a problem hiding this comment.
I'm not sure about this solution. It is probably a step in the right direction, it will be correct more often than the current impl. So I wouldn't stop it.
However, now that we are moving more and more towards classical "plan optimization" instead of "build our own physical plan" I wonder if we should take a different approach.
Also, I think there are two ways to define "scan in order". As a user requested behavior we can interpret this as "the output should be ordered by row address". If we were to do this as a logical plan I think it would be...
- If the user calls
scan_in_orderthen this is equivalent to callingorder_by(["_rowaddr"]). - The user should be prevented from calling both
scan_in_orderandorder_by. - If the user calls
scan_in_orderwe add a sort node to the end of the logical plan - We change the scanner so that it has an ordering (by _rowaddr)
- The optimizer removes the final sort if it determines the plan preserves the ordering
- The optimizer should be deciding if a final sort or removing parallelism is the best approach based on plan statistics or some kind of heuristics.
However, the original use for this argument was something very different. It was an optimization hint to the scanner to tell it to use a different path that would return rows sorted by _rowaddr. We no longer have multiple paths in the scanner. It always returns rows sorted by _rowaddr. This optimization hint is no longer needed. That being said, I suspect removal of this function may be too late now and we should use the first interpretation?
I'm not sure if there is a way to easily move us closer to that final picture that is different from this approach or not.
| options.optimizer.enable_round_robin_repartition = false; | ||
| Ok(optimize(plan, &options)?) |
There was a problem hiding this comment.
I think it optimizes first to see if the optimized plan is a special plan that wouldn't disturb the order anyways. If the optimized plan is not a speical order-preserving plan then it disables the repartition and re-optimizes.
In other words it has to optimize to determine the value of keeps_order which it uses to decide if it needs to optimize without repartition or not.
That being said, it would be nice if there was a way to avoid this double optimize and it feels like there should be.
|
|
||
| Ok(plan) | ||
| options.optimizer.enable_round_robin_repartition = false; | ||
| Ok(optimize(plan, &options)?) |
There was a problem hiding this comment.
Are we guaranteed that the new plan will have keeps_order? What if some other node (other than a round robin repartition) is destroying order? For example, a hash join might destroy order.
There was a problem hiding this comment.
It only removes the round-robin that EnforceDistribution adds. If the scanner's own plan loses order, it stays lost. This PR fixes the order the optimizer breaks, not every way a plan can lose it.
From what I can tell - semantically keeps_order means that the merge from the scan should maintain order which in this case is irrelevant since it is one partition?
Scanner plans are all CollectLeft (which maintains probe side order) and the aggregates are single. EnforceDistribution only adds a hash partition above a node that needs hash-partitioned input and so is not applicable here (please re-validate this assertion)
b2b0907 to
0a1c15e
Compare
b2b0907 to
407c73f
Compare
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The guard fixes optimizer-introduced reordering while retaining parallelism when the full output ordering survives projection. I verified the fallback assumption: current scanner joins use CollectLeft and aggregates use Single, so neither requires hash repartitioning. Focused ordering and ranked-search regressions passed.
`EnforceDistribution` adds round-robin repartitions below filters and computed columns, and the partitions are merged in completion order, so ordered scans of more than 8,192 rows came back shuffled. Turn round-robin off when the order matters but the output declares no ordering that covers it. When the output does, require that ordering at the root so `EnforceDistribution` merges the partitions back in order. MinHash search now declares its rank order.
407c73f to
01ea42b
Compare
EnforceDistributionadds round-robin repartitions below filters andcomputed columns, and the partitions are merged in completion order, so
ordered scans of more than 8,192 rows came back shuffled.
Turn round-robin off when the order matters but the output declares no
ordering that covers it. When the output does, require that ordering at
the root so
EnforceDistributionmerges the partitions back in order.MinHash search now declares its rank order.