Skip to content

Commit

Permalink
fix(duckdb): support casting to unsigned integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and gforsyth committed Mar 10, 2023
1 parent e687033 commit 066c158
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 8 deletions.
20 changes: 16 additions & 4 deletions ibis/backends/duckdb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@ class DuckDBSQLExprTranslator(AlchemyExprTranslator):
_dialect_name = "duckdb"


@compiles(sat.UInt64, "duckdb")
@compiles(sat.UInt32, "duckdb")
@compiles(sat.UInt16, "duckdb")
@compiles(sat.UInt8, "duckdb")
def compile_uint8(element, compiler, **kw):
return "UTINYINT"


@compiles(sat.UInt16, "duckdb")
def compile_uint16(element, compiler, **kw):
return "USMALLINT"


@compiles(sat.UInt32, "duckdb")
def compile_uint32(element, compiler, **kw):
return "UINTEGER"


@compiles(sat.UInt64, "duckdb")
def compile_uint(element, compiler, **kw):
return element.__class__.__name__.upper()
return "UBIGINT"


@compiles(sat.ArrayType, "duckdb")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
CAST(t0.a AS USMALLINT) AS "Cast(a, uint16)"
FROM t AS t0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
CAST(t0.a AS UINTEGER) AS "Cast(a, uint32)"
FROM t AS t0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
CAST(t0.a AS UBIGINT) AS "Cast(a, uint64)"
FROM t AS t0
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT
CAST(t0.a AS UTINYINT) AS "Cast(a, uint8)"
FROM t AS t0
17 changes: 17 additions & 0 deletions ibis/backends/duckdb/tests/test_datatypes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import pytest
import sqlglot as sg
from packaging.version import parse as vparse
from pytest import param

import ibis.expr.datatypes as dt
Expand Down Expand Up @@ -78,3 +80,18 @@
def test_parser(typ, expected):
ty = parse(typ)
assert ty == expected


@pytest.mark.parametrize("uint_type", ["uint8", "uint16", "uint32", "uint64"])
@pytest.mark.xfail(
vparse(sg.__version__) < vparse("11.3.4"),
raises=sg.ParseError,
reason="sqlglot version doesn't support duckdb unsigned integer types",
)
def test_cast_uints(uint_type, snapshot):
import ibis

t = ibis.table(dict(a="int8"), name="t")
snapshot.assert_match(
str(ibis.to_sql(t.a.cast(uint_type), dialect="duckdb")), "out.sql"
)
6 changes: 3 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion requirements.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 066c158

Please sign in to comment.