Skip to content

Commit 7f82e5e

Browse files
mesejocpcloud
authored andcommitted
fix(duckdb): create a single table from Python data object
closes #11709
1 parent 4ff2df7 commit 7f82e5e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

ibis/backends/duckdb/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ def create_table(
180180
catalog = "temp"
181181
database = "main"
182182

183+
in_memory = False
183184
if obj is not None:
184185
if not isinstance(obj, ir.Expr):
185186
table = ibis.memtable(obj)
187+
in_memory = True
186188
else:
187189
table = obj
188190

@@ -233,6 +235,13 @@ def create_table(
233235
).sql(dialect)
234236
cur.execute(insert_stmt).fetchall()
235237

238+
if in_memory:
239+
cur.execute(
240+
sge.Drop(kind="VIEW", this=table.get_name(), exists=True).sql(
241+
dialect
242+
)
243+
)
244+
236245
if overwrite:
237246
cur.execute(
238247
sge.Drop(kind="TABLE", this=final_table, exists=True).sql(dialect)

ibis/backends/duckdb/tests/test_client.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,18 @@ def test_create_temp_table_in_nondefault_schema():
468468
con.con.execute(f"USE {database}")
469469
con.create_table("foo", {"id": [1, 2, 3]}, temp=True)
470470

471+
assert not con.list_tables(database=database) and not con.list_tables()
472+
assert con.list_tables(database="main") == ["foo"]
473+
474+
475+
@pytest.mark.parametrize("temp", [False, True])
476+
def test_create_table_from_data(temp):
477+
con = ibis.duckdb.connect()
478+
con.create_table("foo", pd.DataFrame({"id": [1, 2, 3]}), temp=temp)
479+
480+
assert con.list_tables() == ["foo"]
481+
assert con.con.execute("SHOW TABLES").fetchall() == [("foo",)]
482+
471483

472484
def test_create_table_with_quoted_columns():
473485
con = ibis.duckdb.connect()
@@ -476,6 +488,7 @@ def test_create_table_with_quoted_columns():
476488
{"group": ["G1"], "value": ["E1"], "id": [1], "date": [datetime(2025, 5, 13)]}
477489
)
478490
con.create_table(name, df, temp=True)
491+
assert con.list_tables() == [name]
479492

480493

481494
def test_create_table_with_out_of_order_columns(con):

0 commit comments

Comments
 (0)