From e71453176f64cab2a17a303fecd1db58d1c3b3fe Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Sat, 7 Sep 2024 11:51:24 -0400 Subject: [PATCH] refactor(polars): handle memtables like every other backend --- ibis/backends/polars/__init__.py | 5 +++++ ibis/backends/polars/compiler.py | 8 ++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/ibis/backends/polars/__init__.py b/ibis/backends/polars/__init__.py index 6fcb9ec58917..3de313cf3730 100644 --- a/ibis/backends/polars/__init__.py +++ b/ibis/backends/polars/__init__.py @@ -75,6 +75,10 @@ def table(self, name: str) -> ir.Table: schema = sch.infer(self._tables[name]) return ops.DatabaseTable(name, schema, self).to_expr() + def _register_in_memory_table(self, op: ops.InMemoryTable) -> None: + if (name := op.name) not in self._tables: + self._add_table(name, op.data.to_polars(op.schema).lazy()) + @deprecated( as_of="9.1", instead="use the explicit `read_*` method for the filetype you are trying to read, e.g., read_parquet, read_csv, etc.", @@ -466,6 +470,7 @@ def _to_dataframe( streaming: bool = False, **kwargs: Any, ) -> pl.DataFrame: + self._run_pre_execute_hooks(expr) table_expr = expr.as_table() lf = self.compile(table_expr, params=params, **kwargs) if limit == "default": diff --git a/ibis/backends/polars/compiler.py b/ibis/backends/polars/compiler.py index d32bd22dca89..907ced252582 100644 --- a/ibis/backends/polars/compiler.py +++ b/ibis/backends/polars/compiler.py @@ -9,11 +9,14 @@ from math import isnan import polars as pl +import sqlglot as sg import ibis.common.exceptions as com import ibis.expr.datatypes as dt import ibis.expr.operations as ops from ibis.backends.pandas.rewrites import PandasAsofJoin, PandasJoin, PandasRename +from ibis.backends.sql.compilers.base import STAR +from ibis.backends.sql.dialects import Polars from ibis.expr.operations.udf import InputType from ibis.formats.polars import PolarsType from ibis.util import gen_name @@ -64,8 +67,9 @@ def dummy_table(op, **kw): @translate.register(ops.InMemoryTable) -def in_memory_table(op, **_): - return op.data.to_polars(op.schema).lazy() +def in_memory_table(op, *, ctx, **_): + sql = sg.select(STAR).from_(sg.to_identifier(op.name, quoted=True)).sql(Polars) + return ctx.execute(sql, eager=False) def _make_duration(value, dtype):