Skip to content

Commit

Permalink
fix(insert): user can specify insert table in specified database
Browse files Browse the repository at this point in the history
A user reported this issue on Zulip.

We weren't passing `catalog.database` information to `get_schema` inside
of `_build_insert_from_table` which caused inserts to fail if user had
specified a `database` argument, because we were looking in the wrong db
for the existing table to pull the `schema` from.
  • Loading branch information
gforsyth committed Oct 8, 2024
1 parent 11f8921 commit 193e4cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ibis/backends/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def _build_insert_from_table(
# Compare the columns between the target table and the object to be inserted
# If source is a subset of target, use source columns for insert list
# Otherwise, assume auto-generated column names and use positional ordering.
target_cols = self.get_schema(target).keys()
target_cols = self.get_schema(target, catalog=catalog, database=db).keys()

columns = (
source_cols
Expand Down
21 changes: 21 additions & 0 deletions ibis/backends/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,27 @@ def test_overwrite(ddl_con, monkeypatch):
assert t2.count().execute() == expected_count


@contextlib.contextmanager
def create_and_destroy_db(con):
con.create_database(dbname := gen_name("db"))
yield dbname
con.drop_database(dbname)


# TODO: move this to something like `test_ddl.py`
def test_insert_with_database_specified(con_create_database, monkeypatch):
con = con_create_database
monkeypatch.setattr(ibis.options, "default_backend", con)

t = ibis.memtable({"a": [1, 2, 3]})

with create_and_destroy_db(con) as dbname:
con.create_table(table_name := gen_name("table"), obj=t, database=dbname)
con.insert(table_name, obj=t, database=dbname)
assert con.table(table_name, database=dbname).count().to_pandas() == 6
con.drop_table(table_name, database=dbname)


@pytest.mark.notyet(["datafusion"], reason="cannot list or drop catalogs")
def test_create_catalog(con_create_catalog):
catalog = gen_name("test_create_catalog")
Expand Down

0 comments on commit 193e4cc

Please sign in to comment.