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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
Perform a vectorized search for tables (#8450)
Shahmir Varqha5b9e87ad0f0d
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.pyindex d2c7ff8952..38dac0d40d 100644--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py@@ -329,11 +329,9 @@ class NarwhalsTableManager(if not expressions:return NarwhalsTableManager(self.data.filter(nw.lit(False)))- or_expr = expressions[0]- for expr in expressions[1:]:- or_expr = or_expr | expr-- filtered = self.data.filter(or_expr)+ filtered = self.data.filter(+ nw.any_horizontal(expressions, ignore_nulls=False)+ )return NarwhalsTableManager(filtered)def get_stats(self, column: str) -> ColumnStats:diff --git a/marimo/_plugins/ui/_impl/tables/polars_table.py b/marimo/_plugins/ui/_impl/tables/polars_table.pyindex 6249a10622..25b7667e1f 100644--- a/marimo/_plugins/ui/_impl/tables/polars_table.py+++ b/marimo/_plugins/ui/_impl/tables/polars_table.py@@ -275,11 +275,9 @@ class PolarsTableManagerFactory(TableManagerFactory):if not expressions:return self- or_expr = expressions[0]- for expr in expressions[1:]:- or_expr = or_expr | expr-- filtered = self._original_data.filter(or_expr)+ filtered = self._original_data.filter(+ pl.any_horizontal(expressions)+ )return PolarsTableManager(filtered)# We override the default implementation to use polars'sdiff --git a/tests/_plugins/ui/_impl/tables/test_narwhals.py b/tests/_plugins/ui/_impl/tables/test_narwhals.pyindex 6facf27cb7..5af0ce8d71 100644--- a/tests/_plugins/ui/_impl/tables/test_narwhals.py+++ b/tests/_plugins/ui/_impl/tables/test_narwhals.py@@ -1390,6 +1390,29 @@ def test_search_with_regex(df: Any) -> None:assert result.get_num_rows() == 2[email protected](not HAS_DEPS, reason="optional dependencies not installed")+ "df",+ create_dataframes(+ {f"col_{i}": ["a", "b", "c"] for i in range(1, 500)}+ | {"col_0": ["target", "b", "c"]},+ exclude=["ibis", "duckdb"],+ ),+)+def test_search_wide_dataframe(df: Any) -> None:+ """Regression test: search must not crash on wide tables (GitHub #8449)."""+ manager = NarwhalsTableManager.from_dataframe(df)++ result = manager.search("target")+ assert result.get_num_rows() == 1++ result = manager.search("b")+ assert result.get_num_rows() == 1++ result = manager.search("nonexistent")+ assert result.get_num_rows() == 0++@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")@pytest.mark.parametrize("df",diff --git a/tests/_plugins/ui/_impl/tables/test_polars_table.py b/tests/_plugins/ui/_impl/tables/test_polars_table.pyindex 677c42c61f..01a5dd010f 100644--- a/tests/_plugins/ui/_impl/tables/test_polars_table.py+++ b/tests/_plugins/ui/_impl/tables/test_polars_table.py@@ -852,6 +852,25 @@ class TestPolarsTableManagerFactory(unittest.TestCase):result = manager.search("^[ab]")assert result.get_num_rows() == 2+ def test_search_wide_dataframe(self) -> None:+ """Regression test: search must not crash on wide tables (GitHub #8449)."""+ import polars as pl++ num_cols = 5000+ data = {f"col_{i}": ["a", "b", "c"] for i in range(num_cols)}+ data["col_0"] = ["target", "b", "c"]+ df = pl.DataFrame(data)+ manager = self.factory.create()(df)++ result = manager.search("target")+ assert result.get_num_rows() == 1++ result = manager.search("b")+ assert result.get_num_rows() == 1++ result = manager.search("nonexistent")+ assert result.get_num_rows() == 0+def test_sort_values_with_nulls(self) -> None:import polars as pl