Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

perf(table): collect only the requested Polars page (#11406)

Shahmir Varqha37ab6c2e1ec2
diff --git a/marimo/_plugins/ui/_impl/tables/polars_table.py b/marimo/_plugins/ui/_impl/tables/polars_table.py
index c9b1f020da..989542500a 100644
--- a/marimo/_plugins/ui/_impl/tables/polars_table.py
+++ b/marimo/_plugins/ui/_impl/tables/polars_table.py
@@ -54,6 +54,16 @@ class PolarsTableManagerFactory(TableManagerFactory):
return native
raise ValueError(f"Unsupported native type: {type(native)}")
+ def take(self, count: int, offset: int) -> PolarsTableManager:
+ if count < 0:
+ raise ValueError("Count must be a positive integer")
+ if offset < 0:
+ raise ValueError("Offset must be a non-negative integer")
+
+ return PolarsTableManager(
+ self._original_data.slice(offset, count)
+ )
+
@cached_property
def schema(self) -> dict[str, pl.DataType]:
if isinstance(self._original_data, pl.LazyFrame):
diff --git a/tests/_plugins/ui/_impl/tables/test_polars_table.py b/tests/_plugins/ui/_impl/tables/test_polars_table.py
index 5e771dcfb2..e650298de6 100644
--- a/tests/_plugins/ui/_impl/tables/test_polars_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_polars_table.py
@@ -977,7 +977,7 @@ class TestPolarsTableManagerFactory(unittest.TestCase):
result = manager.take(count=5, offset=10)
assert result.get_num_rows() == 5
- assert result.data["A"].to_list() == [10, 11, 12, 13, 14]
+ assert result.data.collect()["A"].to_list() == [10, 11, 12, 13, 14]
assert len(recorded_warnings) == 0
@@ -1093,3 +1093,48 @@ class TestPolarsTableManagerFactory(unittest.TestCase):
data = self.get_complex_data()
json_str = data.to_json_str(strict_json=True)
snapshot("polars.download.json", json_str)
+
+
[email protected]("lazy", [False, True])
+ ("count", "offset"), [(0, 0), (0, 2), (2, 0), (2, 2), (2, 10)]
+)
+def test_take_filtered_sorted_page(
+ lazy: bool, count: int, offset: int, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ import polars as pl
+
+ source = pl.DataFrame(
+ {"value": [6, 1, 5, 2, 4, 3], "label": list("abcdef")}
+ ).with_row_index("_marimo_row_id")
+ original = source.clone()
+ frame = source.lazy() if lazy else source
+ manager = PolarsTableManagerFactory.create()(frame)
+ filtered = manager.with_new_data(
+ manager.data.filter(nw.col("value") > 1)
+ ).sort_values([SortArgs(by="value", descending=True)])
+ expected = (
+ source.filter(pl.col("value") > 1)
+ .sort("value", descending=True)
+ .slice(offset, count)
+ .to_dicts()
+ )
+ collected_shapes: list[tuple[int, int]] = []
+ collect = pl.LazyFrame.collect
+
+ def record_collect(
+ self: pl.LazyFrame, *args: Any, **kwargs: Any
+ ) -> pl.DataFrame:
+ result = collect(self, *args, **kwargs)
+ collected_shapes.append(result.shape)
+ return result
+
+ monkeypatch.setattr(pl.LazyFrame, "collect", record_collect)
+ page = filtered.take(count, offset)
+ assert collected_shapes == []
+ assert isinstance(nw.to_native(page.data), pl.LazyFrame) == lazy
+ assert json.loads(page.to_json_str()) == expected
+ if lazy:
+ assert collected_shapes == [(len(expected), 3)]
+ assert source.equals(original)