Skip to content

Commit

Permalink
fix(duckdb): disallow creating null-typed columns with duckdb
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud committed Aug 9, 2024
1 parent 57f4816 commit 8f42f56
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
12 changes: 11 additions & 1 deletion ibis/backends/duckdb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,16 @@ def create_table(
else:
query = None

if schema is None:
schema = table.schema()

if schema.nulls:
raise exc.UnsupportedBackendType(
"DuckDB does not support creating tables with NULL typed columns. "
"Ensure that every column has non-NULL type. "
f"NULL columns: {schema.nulls}"
)

column_defs = [
sge.ColumnDef(
this=sg.to_identifier(colname, quoted=self.compiler.quoted),
Expand All @@ -179,7 +189,7 @@ def create_table(
else [sge.ColumnConstraint(kind=sge.NotNullColumnConstraint())]
),
)
for colname, typ in (schema or table.schema()).items()
for colname, typ in schema.items()
]

if overwrite:
Expand Down
20 changes: 20 additions & 0 deletions ibis/backends/duckdb/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pytest import param

import ibis
import ibis.common.exceptions as com
import ibis.expr.datatypes as dt
from ibis.conftest import LINUX, SANDBOXED, not_windows
from ibis.util import gen_name
Expand Down Expand Up @@ -403,3 +404,22 @@ def test_read_csv_with_types(tmp_path, input, all_varchar):
path.write_bytes(data)
t = con.read_csv(path, all_varchar=all_varchar, **input)
assert t.schema()["geom"].is_geospatial()


def test_create_table_with_nulls(con):
t = ibis.memtable({"a": [None]})
schema = t.schema()

assert schema == ibis.schema({"a": "null"})
assert schema.nulls == ("a",)

name = gen_name("duckdb_all_nulls")

with pytest.raises(com.UnsupportedBackendType, match="NULL typed columns"):
con.create_table(name, obj=t)

with pytest.raises(com.UnsupportedBackendType, match="NULL typed columns"):
con.create_table(name, obj=t, schema=schema)

with pytest.raises(com.UnsupportedBackendType, match="NULL typed columns"):
con.create_table(name, schema=schema)

0 comments on commit 8f42f56

Please sign in to comment.