Skip to content

Commit 70fe102

Browse files
committed
fix(sqlite): create a single table from Python object
1 parent 484776f commit 70fe102

File tree

2 files changed

+28
-11
lines changed

2 files changed

+28
-11
lines changed

ibis/backends/sqlite/__init__.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,11 @@ def create_table(
485485
if schema is not None:
486486
schema = ibis.schema(schema)
487487

488+
in_memory = False
488489
if obj is not None:
489490
if not isinstance(obj, ir.Expr):
490491
obj = ibis.memtable(obj)
492+
in_memory = True
491493

492494
self._run_pre_execute_hooks(obj)
493495

@@ -503,43 +505,49 @@ def create_table(
503505
else:
504506
database = "temp"
505507

508+
quoted = self.compiler.quoted
509+
dialect = self.dialect
510+
506511
if overwrite:
507512
created_table = sg.table(
508513
util.gen_name(f"{self.name}_table"),
509514
catalog=database,
510-
quoted=self.compiler.quoted,
515+
quoted=quoted,
511516
)
512-
table = sg.table(name, catalog=database, quoted=self.compiler.quoted)
517+
table = sg.table(name, catalog=database, quoted=quoted)
513518
else:
514-
created_table = table = sg.table(
515-
name, catalog=database, quoted=self.compiler.quoted
516-
)
519+
created_table = table = sg.table(name, catalog=database, quoted=quoted)
517520

518521
create_stmt = self._generate_create_table(
519522
created_table, schema=(schema or obj.schema())
520-
).sql(self.name)
523+
).sql(dialect)
521524

522525
with self.begin() as cur:
523526
cur.execute(create_stmt)
524527

525528
if insert_query is not None:
526529
cur.execute(
527-
sge.Insert(this=created_table, expression=insert_query).sql(
528-
self.name
529-
)
530+
sge.Insert(this=created_table, expression=insert_query).sql(dialect)
530531
)
531532

533+
if in_memory:
534+
cur.execute(
535+
sge.Drop(kind="TABLE", this=obj.get_name(), exists=True).sql(
536+
dialect
537+
)
538+
)
539+
532540
if overwrite:
533541
cur.execute(
534-
sge.Drop(kind="TABLE", this=table, exists=True).sql(self.name)
542+
sge.Drop(kind="TABLE", this=table, exists=True).sql(dialect)
535543
)
536544
# SQLite's ALTER TABLE statement doesn't support using a
537545
# fully-qualified table reference after RENAME TO. Since we
538546
# never rename between databases, we only need the table name
539547
# here.
540548
quoted_name = _quote(name)
541549
cur.execute(
542-
f"ALTER TABLE {created_table.sql(self.name)} RENAME TO {quoted_name}"
550+
f"ALTER TABLE {created_table.sql(dialect)} RENAME TO {quoted_name}"
543551
)
544552

545553
if schema is None:

ibis/backends/sqlite/tests/test_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sqlite3
55
from pathlib import Path
66

7+
import pandas as pd
78
import pytest
89
from pytest import param
910

@@ -101,3 +102,11 @@ def test_list_temp_tables_by_default(con):
101102
con.create_table(name, schema={"a": "int"}, temp=True)
102103
assert name in con.list_tables(database="temp")
103104
assert name in con.list_tables()
105+
106+
107+
@pytest.mark.parametrize("temp", [False, True])
108+
def test_create_table_from_in_memory_data(temp):
109+
con = ibis.sqlite.connect()
110+
con.create_table("foo", pd.DataFrame({"id": [1, 2, 3]}), temp=temp)
111+
112+
assert con.list_tables() == ["foo"]

0 commit comments

Comments
 (0)