Back to Materialize

date_bin_hopping function

doc/user/content/sql/functions/date-bin-hopping.md

1232.1 KB
Original Source

date_bin_hopping returns every "binned" value:

  • Greater than or equal to the source timestamp minus width
  • Less than or equal to the source timestamp
  • Where the bin's stride is equal to hop

date_bin_hopping provides a primitive operation to express what are referred to as "hopping windows" in other systems.

Signatures

{{% include-syntax file="examples/sql_functions/date_bin_hopping" example="syntax" %}}

ParameterTypeDescription
hopintervalDefine bins of this width.
widthintervalProduce an "oldest" bin at the equivalent to date_bin(hop, source + hop - width).
sourcetimestamp, timestamp with time zoneDetermine this value's bins; produce a "newest" bin at the equivalent to date_bin(hop, source).
originMust be the same as sourceAlign bins to this value. If not provided, defaults to the Unix epoch.

Return value

date_bin_hopping returns the same type as source.

Details

  • origin and source cannot be more than 2^63 nanoseconds apart.
  • hop and width cannot contain any years or months, but e.g. can exceed 30 days.
  • hop and width only support values between 1 and 9,223,372,036 seconds.

Examples

mzsql
SELECT * FROM date_bin_hopping('45s', '1m', TIMESTAMP '2001-01-01 00:01:20');
nofmt
  date_bin_hopping
---------------------
 2001-01-01 00:00:30
 2001-01-01 00:01:15
mzsql
SELECT date_bin_hopping AS timeframe_start, sum(v)
  FROM ( VALUES
    (TIMESTAMP '2021-01-01 01:05', 41),
    (TIMESTAMP '2021-01-01 01:07', 21),
    (TIMESTAMP '2021-01-01 01:09', 51),
    (TIMESTAMP '2021-01-01 01:11', 31),
    (TIMESTAMP '2021-01-01 01:13', 11),
    (TIMESTAMP '2021-01-01 01:17', 61)
  ) t (ts, v),
  date_bin_hopping(INTERVAL '5m', INTERVAL '10m', t.ts)
GROUP BY timeframe_start
ORDER BY 1;
nofmt
   timeframe_start   | sum
---------------------+-----
 2021-01-01 01:00:00 | 113
 2021-01-01 01:05:00 | 155
 2021-01-01 01:10:00 | 103
 2021-01-01 01:15:00 |  61