Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(memtable): ensure that constructing an empty memtable from a dataframe works #10945

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions ibis/backends/tests/test_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
np = pytest.importorskip("numpy")
pd = pytest.importorskip("pandas")
tm = pytest.importorskip("pandas.testing")
pa = pytest.importorskip("pyarrow")

NULL_BACKEND_TYPES = {
"bigquery": "NULL",
Expand Down Expand Up @@ -2516,3 +2517,17 @@ def test_table_describe_with_multiple_decimal_columns(con):
expr = t.describe()
result = con.to_pyarrow(expr)
assert len(result) == 2


@pytest.mark.parametrize(
"input",
[[], pa.table([[]], pa.schema({"x": pa.int64()}))],
ids=["list", "pyarrow-table"],
)
@pytest.mark.notyet(["druid"], raises=PyDruidProgrammingError)
@pytest.mark.notyet(
["flink"], raises=ValueError, reason="flink doesn't support empty tables"
)
def test_empty_memtable(con, input):
t = ibis.memtable(input, schema={"x": "int64"})
assert not len(con.to_pyarrow(t))
13 changes: 11 additions & 2 deletions ibis/expr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,15 +429,24 @@ def _memtable(
schema: SchemaLike | None = None,
name: str | None = None,
) -> Table:
import ibis

if hasattr(data, "__arrow_c_stream__"):
# Support objects exposing arrow's PyCapsule interface
import pyarrow as pa

data = pa.table(data)
data = pa.table(
data,
schema=ibis.schema(schema).to_pyarrow() if schema is not None else None,
)
else:
import pandas as pd

data = pd.DataFrame(data, columns=columns)
data = pd.DataFrame(
data,
columns=columns
or (ibis.schema(schema).names if schema is not None else None),
)
return _memtable(data, columns=columns, schema=schema, name=name)


Expand Down