https://github.com/ekzhu/trino-interval-merge-group-agg
https://github.com/ekzhu/trino-interval-merge-group-agg
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/ekzhu/trino-interval-merge-group-agg
- Owner: ekzhu
- Created: 2022-04-21T01:01:17.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-22T21:41:47.000Z (almost 3 years ago)
- Last Synced: 2025-01-23T06:32:34.021Z (9 months ago)
- Language: Java
- Size: 48.8 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# `merge_group`
User-Defined Aggregate plugin for merging overlapping intervals in Trino.
Usage Examples:
Merge integer intervals without `PARTITION BY`:
```sql
WITH input AS (
VALUES
(cast(1 AS bigint), cast(4 AS bigint)),
(2, 3),
(3, 5),
(2, 4),
(4, 6),
(8, 19)
)
SELECT min(x) AS x, max(y) AS y
FROM (
SELECT x, y, merge_group(x, y) OVER (ORDER BY x) AS group_id
FROM input AS i(x, y)
) AS t(x, y, group_id)
GROUP BY group_id;-- Results:
-- x, y
-- 1, 6
-- 8, 19
```Merge date intervals with `PARTITION BY`:
```sql
WITH input AS (
VALUES
('a', date '2012-01-01', date '2012-01-02'),
('a', date '2012-01-02', date '2012-01-03'),
('b', date '2012-01-03', date '2012-01-05'),
('c', date '2012-01-02', date '2012-01-04'),
('b', date '2012-01-04', date '2012-01-06'),
('a', date '2012-01-05', date '2012-01-07')
)
SELECT pid, min(x) as x, min(y) as y
FROM (
SELECT pid, x, y, merge_group(x, y) OVER (PARTITION BY pid ORDER BY x) AS group_id
FROM input AS (pid, x, y)
) AS t(pid, x, y, group_id)
GROUP BY pid, group_id;-- Results:
-- pid, x, y
-- 'a', date '2012-01-01', date '2012-01-03'
-- 'a', date '2012-01-05', date '2012-01-07'
-- 'b', date '2012-01-03', date '2012-01-06'
-- 'c', date '2012-01-02', date '2012-01-04'
```