Repository navigation
fix: stable sort in PolarsBuilderVisitor.visit_order_by - #21
Merged
Merged
Conversation
Polars LazyFrame.sort defaults to maintain_order=False, so rows with equal sort keys come out in thread-scheduling order. Combined with the randomized ages in tests/mock_user.py, order_by(age).take(3) picked a different third row on different runs (e.g. [1,4,2] vs [1,4,3]), making tests/test_polars.py::PolarsTests::test_project flaky in CI. Generalize the op stack entries to (func, params, kwargs) and pass maintain_order=True for sorts so ties keep input order, matching the in-memory executor.
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.
What was broken
CI (
build (ubuntu-latest, 3.12)on PR #20, also reproducible onmain) fails intests/test_polars.py::PolarsTests::test_project:This failure is pre-existing and unrelated to PR #20 — it fails identically on
main(a8cdb82).Root cause
Nondeterministic tie order in the Polars sort, via three compounding facts:
User.get()assignsage=random.choice([16,17,18]). With the test'srandom.seed(100), ages are{1:16, 2:17, 3:17, 4:16, 5:18, 6:17, 7:18, 8:17, 9:17}, sowhere(age >= 16)keeps all 9 rows andorder_by(age)has a 5-way tie at age 17.take(3)then picks[1, 4, <first 17-year-old in sort output>].visit_order_byappliedLazyFrame.sort(field), whose defaults aremaintain_order=False, multithreaded=True— equal elements may come out in any order.[1, 4, 2],[1, 4, 3],[1, 4, 2]. It passed onmainback on Oct 5 purely by scheduling luck;polarsis unpinned (>= 0.12.0), so any version/threading change reshuffles the ties.Change
fquery/polars_builder.py: generalize the deferred-op stack entries from(func, params)to(func, params, kwargs)and passmaintain_order=Truefor sorts, so ties keep input order — matching the in-memory executor's behavior.Verification
tests/test_polars.py: 6/6 consecutive runs pass (previously failed intermittently).59 passed(baseline onmain: 58 passed + the 1 polars failure).black+flake8clean on the touched file.