Skip to content

Commit

Permalink
refactor(polars): handle memtables like every other backend
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Sep 7, 2024
1 parent 415e298 commit 5a56730
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 12 additions & 0 deletions ibis/backends/polars/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import weakref
from collections.abc import Iterable, Mapping
from functools import lru_cache
from pathlib import Path
Expand Down Expand Up @@ -75,6 +76,16 @@ def table(self, name: str) -> ir.Table:
schema = sch.infer(self._tables[name])
return ops.DatabaseTable(name, schema, self).to_expr()

def _table_exists(self, name: str) -> bool:
return name in self._tables

def _register_in_memory_table(self, op: ops.InMemoryTable) -> None:
df = op.data.to_polars(op.schema).lazy()
self._add_table(op.name, df)

def _register_memtable_finalizer(self, op: ops.InMemoryTable) -> None:
weakref.finalize(op, self.drop_table, op.name)

@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 +477,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 5a56730

Please sign in to comment.