Skip to content

Commit

Permalink
fix: registration of in-memory tables (letsql#232)
Browse files Browse the repository at this point in the history
closes letsql#231
  • Loading branch information
mesejo authored Aug 25, 2024
1 parent 74b6567 commit e4454d0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 12 additions & 1 deletion python/letsql/backends/let/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,21 @@ def register(
table_name: str | None = None,
**kwargs: Any,
) -> ir.Table:
# FIXME: make sure all paths set the correct backend, table_or_expr pairs
table_or_expr = None
if isinstance(source, ir.Expr) and hasattr(source, "to_pyarrow_batches"):
table_or_expr = source.op()
backend = source._find_backend(use_default=False)

backends, has_unbound = source._find_backends()
backend = None
if not backends:
if not has_unbound:
source = super().execute(source)
table_or_expr = None
elif len(backends) > 1:
raise ValueError("Multiple backends found for this expression")
else:
backend = backends[0]

if isinstance(backend, Backend):
if backend is self and table_or_expr in self._sources:
Expand Down
8 changes: 8 additions & 0 deletions python/letsql/tests/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import pyarrow.dataset as ds
import pytest

import letsql


@pytest.fixture
def gzip_csv(data_dir, tmp_path):
Expand Down Expand Up @@ -84,3 +86,9 @@ def test_register_dataset(con):
dataset = ds.InMemoryDataset(tab)
con.register(dataset, "my_table")
assert con.table("my_table").x.sum().execute() == 6


def test_register_memtable(con):
data = pd.DataFrame({"a": [1, 2, 3, 4, 5], "b": [2, 3, 4, 5, 6]})
t = letsql.memtable(data).pipe(con.register, "data")
assert t.a.sum().execute() == 15

0 comments on commit e4454d0

Please sign in to comment.