Skip to content

Commit

Permalink
fix update tests, remove cagg test for time_weight, formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thatzopoulos committed Mar 1, 2023
1 parent 3b4e5b8 commit 4257d72
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This changelog should be updated as part of a PR if the work is worth noting (mo
- [#715](https://github.com/timescale/timescaledb-toolkit/pull/715): Fix out-of-bounds indexing error in `state_agg` rollup

#### Stabilized features
- [#XXX](https://github.com/timescale/timescaledb-toolkit/pull/XXX): Stabilize integral, and interpolated_integral for time-weighted-average.
- [#724](https://github.com/timescale/timescaledb-toolkit/pull/724): Stabilize integral, and interpolated_integral for time-weighted-average.

#### Other notable changes

Expand Down
14 changes: 1 addition & 13 deletions docs/test_caggs.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ AS SELECT
hyperloglog(64, value1) as hll,
counter_agg(time, value1) as counter,
stats_agg(value1, value2) as stats,
timevector(time, value2) as tvec,
time_weight('Linear',time,value1) as twa
timevector(time, value2) as tvec
FROM test
GROUP BY time_bucket('7 day'::interval, time);
```
Expand Down Expand Up @@ -115,14 +114,3 @@ ORDER BY week;
2020-07-20 00:00:00+00 | 168
2020-07-27 00:00:00+00 | 59
```

```SQL
SELECT average(rollup(twa)) as time_weighted_average
FROM weekly_aggs;
```

```output
time_weighted_average
-----------------------
67.0032679739
```
4 changes: 2 additions & 2 deletions extension/src/stabilization_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ crate::functions_stabilized_at! {
crate::types_stabilized_at! {
STABLE_TYPES
"1.15.0" => {
accessorintegral,
accessorintegral,
}
"1.14.0" => {
candlestick,
Expand Down Expand Up @@ -593,7 +593,7 @@ crate::types_stabilized_at! {
crate::operators_stabilized_at! {
STABLE_OPERATORS
"1.15.0" => {
"->"(timeweightsummary,accessorintegral),
"->"(timeweightsummary,accessorintegral),
}
"1.14.0" => {
"->"(candlestick,accessorclose),
Expand Down
81 changes: 42 additions & 39 deletions tests/update/time-weighted-average.md
Original file line number Diff line number Diff line change
@@ -1,53 +1,56 @@

# Time Weighted Average Test

## Test aggregating data into a TimeWeightSummary and calculating the average
# Time Weighted Average Tests

## Test integral and interpolated integral

```sql,creation,min-toolkit-version=1.15.0
CREATE TABLE freezer_temps (
freezer_id int,
ts timestamptz,
temperature float);
INSERT INTO freezer_temps VALUES
( 1, '2020-01-01 00:00:00+00', 4.0),
( 1, '2020-01-01 00:05:00+00', 5.5),
( 1, '2020-01-01 00:10:00+00', 3.0),
( 1, '2020-01-01 00:15:00+00', 4.0),
( 1, '2020-01-01 00:20:00+00', 3.5),
( 1, '2020-01-01 00:25:00+00', 8.0),
( 1, '2020-01-01 00:30:00+00', 9.0),
( 1, '2020-01-01 00:31:00+00', 10.5), -- door opened!
( 1, '2020-01-01 00:31:30+00', 11.0),
( 1, '2020-01-01 00:32:00+00', 15.0),
( 1, '2020-01-01 00:32:30+00', 20.0), -- door closed
( 1, '2020-01-01 00:33:00+00', 18.5),
( 1, '2020-01-01 00:33:30+00', 17.0),
( 1, '2020-01-01 00:34:00+00', 15.5),
( 1, '2020-01-01 00:34:30+00', 14.0),
( 1, '2020-01-01 00:35:00+00', 12.5),
( 1, '2020-01-01 00:35:30+00', 11.0),
( 1, '2020-01-01 00:36:00+00', 10.0), -- temperature stabilized
( 1, '2020-01-01 00:40:00+00', 7.0),
( 1, '2020-01-01 00:45:00+00', 5.0);
CREATE TABLE time_weight_test(time timestamptz, value double precision, bucket timestamptz);
INSERT INTO time_weight_test VALUES
('2020-1-1 8:00'::timestamptz, 10.0, '2020-1-1'::timestamptz),
('2020-1-1 12:00'::timestamptz, 40.0, '2020-1-1'::timestamptz),
('2020-1-1 16:00'::timestamptz, 20.0, '2020-1-1'::timestamptz),
('2020-1-2 2:00'::timestamptz, 15.0, '2020-1-2'::timestamptz),
('2020-1-2 12:00'::timestamptz, 50.0, '2020-1-2'::timestamptz),
('2020-1-2 20:00'::timestamptz, 25.0, '2020-1-2'::timestamptz),
('2020-1-3 10:00'::timestamptz, 30.0, '2020-1-3'::timestamptz),
('2020-1-3 12:00'::timestamptz, 0.0, '2020-1-3'::timestamptz),
('2020-1-3 16:00'::timestamptz, 35.0, '2020-1-3'::timestamptz);
CREATE MATERIALIZED VIEW twa AS (
SELECT time_bucket('10 mins'::interval, ts) as bucket,
freezer_id,
time_weight('Linear', ts, temperature)
FROM freezer_temps
GROUP BY bucket, freezer_id
SELECT bucket, time_weight('LOCF', time, value) as agg
FROM time_weight_test
GROUP BY bucket
);
```

```sql,validation,min-toolkit-version=1.15.0
SELECT average(rollup(time_weight)) as time_weighted_average
FROM twa;
SELECT
interpolated_integral(
agg,
bucket,
'1 day'::interval,
LAG(agg) OVER (ORDER BY bucket),
LEAD(agg) OVER (ORDER BY bucket),
'hours')
FROM twa
ORDER BY bucket;
```

```output
time_weighted_average
interpolated_integral
-----------------------
6.636111111111111
360
690
310
```

```sql,validation,min-toolkit-version=1.15.0
SELECT integral(agg, 'hrs') FROM twa;
```

```output
integral
----------
200
60
550
```

0 comments on commit 4257d72

Please sign in to comment.