Skip to content

Commit

Permalink
Merge #640
Browse files Browse the repository at this point in the history
640: Support rollup and integer states state/timeline aggs r=Smittyvb a=Smittyvb

- Adds `rollup` for `state_agg` and `timeline_agg`.
- Supports integer states for `state_agg` and `timeline_agg`. An aggregate can have integer states or string states, but not both. For a few functions this would mean having two functions with the same argument types but differing return types, so in that case I made a separate function for integer-valued aggregates (e.g. `state_int_timeline`).

Ideally this should have been two separate PRs, but my changes to `rollup` and the rest of the `state_agg` code have become intertwined.

(this should be merged after #636)

Co-authored-by: Smitty <smitty@timescale.com>
Co-authored-by: Smittyvb <smitty@timescale.com>
  • Loading branch information
bors[bot] and syvb authored Dec 6, 2022
2 parents 1484bd2 + 35b1fe3 commit 6347638
Show file tree
Hide file tree
Showing 4 changed files with 1,298 additions and 123 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ This changelog should be updated as part of a PR if the work is worth noting (mo

- [#636](https://github.com/timescale/timescaledb-toolkit/pull/636): New `timeline_agg` aggregate, which is similar to `state_agg` but tracks the entire state timeline instead of just the duration in each state.

- [#640](https://github.com/timescale/timescaledb-toolkit/pull/640): Support `rollup` for `state_agg` and `timeline_agg`.
- [#640](https://github.com/timescale/timescaledb-toolkit/pull/640): Support integer states for `state_agg` and `timeline_agg`.

- [#638](https://github.com/timescale/timescaledb-toolkit/pull/638): Introducing Time Vector Templates.

#### Bug fixes
Expand Down
165 changes: 165 additions & 0 deletions docs/state_agg.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ CREATE TABLE states_test_3(ts TIMESTAMPTZ, state TEXT);
INSERT INTO states_test_3 VALUES
('2019-12-31 00:00:11+00', 'UNUSED'),
('2019-12-31 00:01:00+00', 'START');
CREATE TABLE states_test_4(ts TIMESTAMPTZ, state BIGINT);
INSERT INTO states_test_4 VALUES
('2020-01-01 00:00:00+00', 4),
('2020-01-01 00:00:11+00', 51351),
('2020-01-01 00:01:00+00', 2),
('2020-01-01 00:01:03+00', 51351),
('2020-01-01 00:02:00+00', -9);
CREATE TABLE states_test_5(ts TIMESTAMPTZ, state BIGINT);
INSERT INTO states_test_5 VALUES
('2020-01-01 00:00:00+00', 4),
('2020-01-01 00:00:11+00', 51351),
('2020-01-01 00:01:00+00', 2),
('2020-01-01 00:02:03+00', 51351),
('2020-01-01 00:02:05+00', -9);
```

## Functions
Expand All @@ -39,6 +53,14 @@ SELECT toolkit_experimental.duration_in('ERROR', toolkit_experimental.state_agg(
----------
00:00:03
```
```SQL
SELECT toolkit_experimental.duration_in(2, toolkit_experimental.state_agg(ts, state)) FROM states_test_4;
```
```output
interval
----------
00:00:03
```

Extract as number of seconds:

Expand Down Expand Up @@ -70,6 +92,19 @@ SELECT state, duration FROM toolkit_experimental.into_values(
START | 11000000
STOP | 0
```
```SQL
SELECT state, duration FROM toolkit_experimental.into_int_values(
(SELECT toolkit_experimental.state_agg(ts, state) FROM states_test_4))
ORDER BY state, duration;
```
```output
state | duration
-------+-----------
-9 | 0
2 | 3000000
4 | 11000000
51351 | 106000000
```

### state_timeline

Expand All @@ -88,6 +123,22 @@ ERROR | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00
STOP | 2020-01-01 00:02:00+00 | 2020-01-01 00:02:00+00
```

```SQL
SELECT state, start_time, end_time FROM toolkit_experimental.state_int_timeline(
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_4))
ORDER BY start_time;
```
```output
state | start_time | end_time
------+------------------------+-----------------------
4 | 2020-01-01 00:00:00+00 | 2020-01-01 00:00:11+00
51351 | 2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
2 | 2020-01-01 00:01:00+00 | 2020-01-01 00:01:03+00
51351 | 2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
-9 | 2020-01-01 00:02:00+00 | 2020-01-01 00:02:00+00
```


```SQL
SELECT state, start_time, end_time FROM toolkit_experimental.state_timeline(
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_2))
Expand Down Expand Up @@ -118,6 +169,21 @@ start_time | end_time
2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
```

```SQL
SELECT start_time, end_time
FROM toolkit_experimental.state_periods(
51351,
(SELECT toolkit_experimental.timeline_agg(ts, state) FROM states_test_4)
)
ORDER BY start_time;
```
```output
start_time | end_time
-----------------------+-----------------------
2020-01-01 00:00:11+00 | 2020-01-01 00:01:00+00
2020-01-01 00:01:03+00 | 2020-01-01 00:02:00+00
```

```SQL
SELECT start_time, end_time
FROM toolkit_experimental.state_periods(
Expand Down Expand Up @@ -280,3 +346,102 @@ start_time | end_time
2019-12-31 00:00:00+00 | 2020-01-01 00:00:00+00
2020-01-01 00:02:00+00 | 2020-01-05 00:00:00+00
```

## rolllup

```SQL
WITH buckets AS (SELECT
date_trunc('minute', ts) as dt,
toolkit_experimental.state_agg(ts, state) AS sa
FROM states_test
GROUP BY date_trunc('minute', ts))
SELECT toolkit_experimental.duration_in(
'START',
toolkit_experimental.rollup(buckets.sa)
)
FROM buckets;
```
```output
interval
----------
00:00:11
```

```SQL
WITH buckets AS (SELECT
date_trunc('minute', ts) as dt,
toolkit_experimental.state_agg(ts, state) AS sa
FROM states_test
GROUP BY date_trunc('minute', ts))
SELECT toolkit_experimental.duration_in(
'OK',
toolkit_experimental.rollup(buckets.sa)
)
FROM buckets;
```
```output
interval
----------
00:01:46
```

```SQL
WITH buckets AS (SELECT
date_trunc('minute', ts) as dt,
toolkit_experimental.timeline_agg(ts, state) AS sa
FROM states_test
GROUP BY date_trunc('minute', ts))
SELECT toolkit_experimental.state_timeline(
toolkit_experimental.rollup(buckets.sa)
)
FROM buckets;
```
```output
state_timeline
-----------------------------------------------------------
(START,"2020-01-01 00:00:00+00","2020-01-01 00:00:11+00")
(OK,"2020-01-01 00:00:11+00","2020-01-01 00:01:00+00")
(ERROR,"2020-01-01 00:01:00+00","2020-01-01 00:01:03+00")
(OK,"2020-01-01 00:01:03+00","2020-01-01 00:02:00+00")
(STOP,"2020-01-01 00:02:00+00","2020-01-01 00:02:00+00")
```

```SQL
WITH buckets AS (SELECT
date_trunc('minute', ts) as dt,
toolkit_experimental.timeline_agg(ts, state) AS sa
FROM states_test
GROUP BY date_trunc('minute', ts)
HAVING date_trunc('minute', ts) != '2020-01-01 00:01:00+00'::timestamptz)
SELECT toolkit_experimental.state_timeline(
toolkit_experimental.rollup(buckets.sa)
)
FROM buckets;
```
```output
state_timeline
-----------------------------------------------------------
(START,"2020-01-01 00:00:00+00","2020-01-01 00:00:11+00")
(OK,"2020-01-01 00:00:11+00","2020-01-01 00:02:00+00")
(STOP,"2020-01-01 00:02:00+00","2020-01-01 00:02:00+00")
```

```SQL
WITH buckets AS (SELECT
date_trunc('minute', ts) as dt,
toolkit_experimental.timeline_agg(ts, state) AS sa
FROM states_test_5
GROUP BY date_trunc('minute', ts)
HAVING date_trunc('minute', ts) != '2020-01-01 00:01:00+00'::timestamptz)
SELECT toolkit_experimental.state_int_timeline(
toolkit_experimental.rollup(buckets.sa)
)
FROM buckets;
```
```output
state_timeline
-----------------------------------------------------------
(4,"2020-01-01 00:00:00+00","2020-01-01 00:00:11+00")
(51351,"2020-01-01 00:00:11+00","2020-01-01 00:02:05+00")
(-9,"2020-01-01 00:02:05+00","2020-01-01 00:02:05+00")
```
Loading

0 comments on commit 6347638

Please sign in to comment.