-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rust, python): fix logical columns of streaming multi-column sort (…
- Loading branch information
Showing
3 changed files
with
126 additions
and
94 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import polars as pl | ||
from polars.testing import assert_series_equal | ||
|
||
|
||
def test_streaming_sort_multiple_columns_logical_types() -> None: | ||
data = { | ||
"foo": [3, 2, 1], | ||
"bar": ["a", "b", "c"], | ||
"baz": [ | ||
datetime(2023, 5, 1, 15, 45), | ||
datetime(2023, 5, 1, 13, 45), | ||
datetime(2023, 5, 1, 14, 45), | ||
], | ||
} | ||
assert pl.DataFrame(data).lazy().sort("foo", "baz").collect(streaming=True).to_dict( | ||
False | ||
) == { | ||
"foo": [1, 2, 3], | ||
"bar": ["c", "b", "a"], | ||
"baz": [ | ||
datetime(2023, 5, 1, 14, 45), | ||
datetime(2023, 5, 1, 13, 45), | ||
datetime(2023, 5, 1, 15, 45), | ||
], | ||
} | ||
|
||
|
||
@pytest.mark.slow() | ||
def test_ooc_sort(monkeypatch: Any) -> None: | ||
monkeypatch.setenv("POLARS_FORCE_OOC", "1") | ||
|
||
s = pl.arange(0, 100_000, eager=True).rename("idx") | ||
|
||
df = s.shuffle().to_frame() | ||
|
||
for descending in [True, False]: | ||
out = ( | ||
df.lazy().sort("idx", descending=descending).collect(streaming=True) | ||
).to_series() | ||
|
||
assert_series_equal(out, s.sort(descending=descending)) | ||
|
||
|
||
@pytest.mark.write_disk() | ||
def test_streaming_sort(monkeypatch: Any, capfd: Any) -> None: | ||
monkeypatch.setenv("POLARS_VERBOSE", "1") | ||
monkeypatch.setenv("POLARS_FORCE_OOC", "1") | ||
# this creates a lot of duplicate partitions and triggers: #7568 | ||
assert ( | ||
pl.Series(np.random.randint(0, 100, 100)) | ||
.to_frame("s") | ||
.lazy() | ||
.sort("s") | ||
.collect(streaming=True)["s"] | ||
.is_sorted() | ||
) | ||
(_, err) = capfd.readouterr() | ||
assert "df -> sort" in err | ||
|
||
|
||
@pytest.mark.write_disk() | ||
def test_out_of_core_sort_9503(monkeypatch: Any) -> None: | ||
monkeypatch.setenv("POLARS_FORCE_OOC", "1") | ||
np.random.seed(0) | ||
|
||
num_rows = 1_00_000 | ||
num_columns = 2 | ||
num_tables = 10 | ||
|
||
# ensure we create many chunks | ||
# this will ensure we create more files | ||
# and that creates contention while dumping | ||
q = pl.concat( | ||
[ | ||
pl.DataFrame( | ||
[ | ||
pl.Series(np.random.randint(0, 10000, size=num_rows)) | ||
for _ in range(num_columns) | ||
] | ||
) | ||
for _ in range(num_tables) | ||
], | ||
rechunk=False, | ||
).lazy() | ||
q = q.sort(q.columns) | ||
df = q.collect(streaming=True) | ||
assert df.shape == (1_000_000, 2) | ||
assert df["column_0"].flags["SORTED_ASC"] | ||
assert df.head(20).to_dict(False) == { | ||
"column_0": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
"column_1": [ | ||
242, | ||
245, | ||
588, | ||
618, | ||
732, | ||
902, | ||
925, | ||
945, | ||
1009, | ||
1161, | ||
1352, | ||
1365, | ||
1451, | ||
1581, | ||
1778, | ||
1836, | ||
1976, | ||
2091, | ||
2120, | ||
2124, | ||
], | ||
} |