Skip to content

Commit

Permalink
feat(postgres): add support for timestamp bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist authored and gforsyth committed Oct 20, 2023
1 parent ca0f7bc commit 4d34afc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
12 changes: 12 additions & 0 deletions ibis/backends/postgres/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ def _timestamp_truncate(t, op):
return sa.func.date_trunc(precision, sa_arg)


def _timestamp_bucket(t, op):
arg = t.translate(op.arg)
interval = t.translate(op.interval)

origin = sa.literal_column("timestamp '1970-01-01 00:00:00'")

if op.offset is not None:
origin = origin + t.translate(op.offset)
return sa.func.date_bin(interval, arg, origin)


def _typeof(t, op):
sa_arg = t.translate(op.arg)
typ = sa.cast(sa.func.pg_typeof(sa_arg), sa.TEXT)
Expand Down Expand Up @@ -638,6 +649,7 @@ def _array_filter(t, op):
ops.DateFromYMD: fixed_arity(sa.func.make_date, 3),
ops.DateTruncate: _timestamp_truncate,
ops.TimestampTruncate: _timestamp_truncate,
ops.TimestampBucket: _timestamp_bucket,
ops.IntervalFromInteger: (
lambda t, op: t.translate(op.arg)
* sa.text(f"INTERVAL '1 {op.dtype.resolution}'")
Expand Down
2 changes: 0 additions & 2 deletions ibis/backends/tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,6 @@ def test_delta(con, start, end, unit, expected):
"oracle",
"pandas",
"polars",
"postgres",
"pyspark",
"snowflake",
"sqlite",
Expand Down Expand Up @@ -2571,7 +2570,6 @@ def test_timestamp_bucket(backend, kws, pd_freq):
"oracle",
"pandas",
"polars",
"postgres",
"pyspark",
"snowflake",
"sqlite",
Expand Down

0 comments on commit 4d34afc

Please # to comment.