Skip to content

fix: keep scan order when the optimizer adds parallelism - #9819

Merged
jackye1995 merged 1 commit into
lance-format:mainfrom
vivek-bharathan:vb/scan-order-round-robin
Oct 14, 2026
Merged

jackye1995 merged 1 commit into
lance-format:mainfrom
vivek-bharathan:vb/scan-order-round-robin

Conversation

@vivek-bharathan

@vivek-bharathan vivek-bharathan commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

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.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added the bug Something isn't working label Oct 9, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
@vivek-bharathan
vivek-bharathan force-pushed the vb/scan-order-round-robin branch from a9171db to b2b0907 Compare October 9, 2026 13:59
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
Comment thread rust/lance/src/dataset/scanner.rs Outdated
Comment on lines +3918 to +3919
options.optimizer.enable_round_robin_repartition = false;
Ok(optimize(plan, &options)?)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why do we need to re-run optimize here when it was just run above?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 westonpace left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_order then this is equivalent to calling order_by(["_rowaddr"]).
  • The user should be prevented from calling both scan_in_order and order_by.
  • If the user calls scan_in_order we 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.

Comment thread rust/lance/src/dataset/scanner.rs Outdated
Comment on lines +3918 to +3919
options.optimizer.enable_round_robin_repartition = false;
Ok(optimize(plan, &options)?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread rust/lance/src/dataset/scanner.rs Outdated

Ok(plan)
options.optimizer.enable_round_robin_repartition = false;
Ok(optimize(plan, &options)?)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@vivek-bharathan
vivek-bharathan force-pushed the vb/scan-order-round-robin branch from b2b0907 to 0a1c15e Compare October 9, 2026 16:43
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 9, 2026
@vivek-bharathan
vivek-bharathan force-pushed the vb/scan-order-round-robin branch 2 times, most recently from b2b0907 to 407c73f Compare October 12, 2026 22:36

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ 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.

@lance-gatekeeper lance-gatekeeper Bot added the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 12, 2026
Comment thread rust/lance/src/dataset/scanner.rs Resolved
Comment thread rust/lance/src/dataset/scanner.rs Resolved
Comment thread rust/lance/src/io/exec/minhash.rs Resolved
`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.
@vivek-bharathan
vivek-bharathan force-pushed the vb/scan-order-round-robin branch from 407c73f to 01ea42b Compare October 14, 2026 22:38
@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants