Skip to content

Commit

Permalink
refactor(polars): handle memtables like every other backend (#10056)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored Sep 9, 2024
1 parent 43e5f12 commit 2b0dbb9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ibis/backends/polars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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":
Expand Down
8 changes: 6 additions & 2 deletions ibis/backends/polars/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 2b0dbb9

Please sign in to comment.