Skip to content

Commit

Permalink
feat(duckdb): add support for specific timestamp scales
Browse files Browse the repository at this point in the history
  • Loading branch information
cpcloud authored and gforsyth committed Oct 23, 2023
1 parent a5e3062 commit 3518b78
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
20 changes: 20 additions & 0 deletions ibis/backends/base/sqlglot/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,26 @@ class DuckDBType(SqlglotType):
default_decimal_scale = 3
default_interval_precision = "us"

@classmethod
def _from_sqlglot_TIMESTAMP(cls) -> dt.Timestamp:
return dt.Timestamp(scale=6, nullable=cls.default_nullable)

@classmethod
def _from_sqlglot_TIMESTAMPTZ(cls) -> dt.Timestamp:
return dt.Timestamp(scale=6, timezone="UTC", nullable=cls.default_nullable)

@classmethod
def _from_sqlglot_TIMESTAMP_S(cls) -> dt.Timestamp:
return dt.Timestamp(scale=0, nullable=cls.default_nullable)

@classmethod
def _from_sqlglot_TIMESTAMP_MS(cls) -> dt.Timestamp:
return dt.Timestamp(scale=3, nullable=cls.default_nullable)

@classmethod
def _from_sqlglot_TIMESTAMP_NS(cls) -> dt.Timestamp:
return dt.Timestamp(scale=9, nullable=cls.default_nullable)


class TrinoType(SqlglotType):
dialect = "trino"
Expand Down
19 changes: 19 additions & 0 deletions ibis/backends/duckdb/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import duckdb
import pytest
import sqlalchemy as sa
from pytest import param

import ibis
import ibis.expr.datatypes as dt
from ibis.conftest import LINUX, SANDBOXED
from ibis.util import gen_name


@pytest.fixture(scope="session")
Expand Down Expand Up @@ -113,3 +116,19 @@ def test_attach_detach(tmpdir):

with pytest.raises(sa.exc.ProgrammingError):
con2.detach(name)


@pytest.mark.parametrize(
"scale",
[
None,
param(0, id="seconds"),
param(3, id="millis"),
param(6, id="micros"),
param(9, id="nanos"),
],
)
def test_create_table_with_timestamp_scales(con, scale):
schema = ibis.schema(dict(ts=dt.Timestamp(scale=scale)))
t = con.create_table(gen_name("duckdb_timestamp_scale"), schema=schema, temp=True)
assert t.schema() == schema
7 changes: 5 additions & 2 deletions ibis/backends/duckdb/tests/test_datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
("SMALLINT", dt.int16),
("TIME", dt.time),
("TIME WITH TIME ZONE", dt.time),
("TIMESTAMP", dt.timestamp),
("TIMESTAMP WITH TIME ZONE", dt.Timestamp("UTC")),
("TIMESTAMP", dt.Timestamp(scale=6)),
("TIMESTAMP WITH TIME ZONE", dt.Timestamp(scale=6, timezone="UTC")),
("TINYINT", dt.int8),
("UBIGINT", dt.uint64),
("UINTEGER", dt.uint32),
Expand All @@ -53,6 +53,9 @@
("INTEGER[][]", dt.Array(dt.Array(dt.int32))),
("JSON", dt.json),
("HUGEINT", dt.Decimal(38, 0)),
("TIMESTAMP_S", dt.Timestamp(scale=0)),
("TIMESTAMP_MS", dt.Timestamp(scale=3)),
("TIMESTAMP_NS", dt.Timestamp(scale=9)),
]
],
)
Expand Down

0 comments on commit 3518b78

Please sign in to comment.