Skip to content

Commit

Permalink
Merge #701
Browse files Browse the repository at this point in the history
701: Stabilize Candlestick/OHLC r=thatzopoulos a=thatzopoulos

This PR renames ohlc.rs to candlestick.rs, removes the now deprecated ohlc code, and completes the following parts of Issue #695:

- [x]  Ensure tests exist for all public API
- [x]  Remove toolkit_experimental tags and update test usages
- [x]  Add arrow operators for accessors if applicable
- [x]  Ensure arrow operators have test coverage
- [x]  If present, ensure combine and rollup are tested
- [x]  Add serialization tests for on disk format
- [x]  Add serialization tests for text output format
- [x]  Add upgrade tests
- [x]  Add continuous aggregate test

Co-authored-by: Thomas Hatzopoulos <thomas@timescale.com>
  • Loading branch information
bors[bot] and thatzopoulos authored Feb 9, 2023
2 parents f80b630 + 1cd824a commit a00de9e
Show file tree
Hide file tree
Showing 8 changed files with 515 additions and 568 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ This changelog should be updated as part of a PR if the work is worth noting (mo
- [#660](https://github.com/timescale/timescaledb-toolkit/issues/660): Heartbeat aggregate rollup should interpolate aggregates
- [#679](https://github.com/timescale/timescaledb-toolkit/issues/679): Heartbeat agg rollup producing invalid aggregates.

#### Stabilized features
- [#701](https://github.com/timescale/timescaledb-toolkit/pull/701): Stabilize candlestick.

#### Other notable changes
- [#685](https://github.com/timescale/timescaledb-toolkit/issues/685): rollup for freq_agg and topn_agg
- [#692](https://github.com/timescale/timescaledb-toolkit/pull/692): Support specifying a range to `duration_in` to specify a time range to get states in for state aggregates
Expand Down
69 changes: 69 additions & 0 deletions docs/test_candlestick_agg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Candlestick Continuous Aggregation Tests

## Setup table
```SQL,non-transactional,ignore-output
SET TIME ZONE 'UTC';
CREATE TABLE stocks_real_time (
time TIMESTAMPTZ NOT NULL,
symbol TEXT NOT NULL,
price DOUBLE PRECISION NULL,
day_volume INT NULL
);
SELECT create_hypertable('stocks_real_time','time');
```

## Setup Continuous Aggs
```SQL,non-transactional,ignore-output
CREATE MATERIALIZED VIEW cs
WITH (timescaledb.continuous) AS
SELECT time_bucket('1 minute'::interval, "time") AS ts,
symbol,
candlestick_agg("time", price, day_volume) AS candlestick
FROM stocks_real_time
GROUP BY ts, symbol;
```

## Insert data into table
```SQL,non-transactional,ignore-output
INSERT INTO stocks_real_time("time","symbol","price","day_volume")
VALUES
('2023-01-11 18:59:59+00','AAPL',140,20),
('2023-01-11 18:23:58+00','AAPL',100,10),
('2023-01-11 17:59:57+00','AAPL',133.445,NULL),
('2023-01-11 17:59:55+00','PFE',47.38,2000),
('2023-01-11 12:15:55+00','PFE',1,23),
('2023-01-11 12:00:52+00','AAPL',29.82,NULL),
('2023-01-11 11:12:12+00','PFE',47.38,14),
('2023-01-11 11:01:50+00','AMZN',95.25,1000),
('2023-01-11 11:01:32+00','AMZN',92,NULL),
('2023-01-11 11:01:30+00','AMZN',75.225,NULL);
```
## Query by-minute continuous aggregate over stock trade data for ohlc prices along with timestamps

```SQL,non-transactional
SELECT ts,
symbol,
open_time(candlestick),
open(candlestick),
high_time(candlestick),
high(candlestick),
low_time(candlestick),
low(candlestick),
close_time(candlestick),
close(candlestick),
volume(candlestick)
FROM cs;
```

```output
ts | symbol | open_time | open | high_time | high | low_time | low | close_time | close | volume
------------------------+--------+------------------------+---------+------------------------+---------+------------------------+---------+------------------------+---------+--------
2023-01-11 12:15:00+00 | PFE | 2023-01-11 12:15:55+00 | 1 | 2023-01-11 12:15:55+00 | 1 | 2023-01-11 12:15:55+00 | 1 | 2023-01-11 12:15:55+00 | 1 | 23
2023-01-11 17:59:00+00 | PFE | 2023-01-11 17:59:55+00 | 47.38 | 2023-01-11 17:59:55+00 | 47.38 | 2023-01-11 17:59:55+00 | 47.38 | 2023-01-11 17:59:55+00 | 47.38 | 2000
2023-01-11 11:01:00+00 | AMZN | 2023-01-11 11:01:30+00 | 75.225 | 2023-01-11 11:01:50+00 | 95.25 | 2023-01-11 11:01:30+00 | 75.225 | 2023-01-11 11:01:50+00 | 95.25 |
2023-01-11 18:59:00+00 | AAPL | 2023-01-11 18:59:59+00 | 140 | 2023-01-11 18:59:59+00 | 140 | 2023-01-11 18:59:59+00 | 140 | 2023-01-11 18:59:59+00 | 140 | 20
2023-01-11 11:12:00+00 | PFE | 2023-01-11 11:12:12+00 | 47.38 | 2023-01-11 11:12:12+00 | 47.38 | 2023-01-11 11:12:12+00 | 47.38 | 2023-01-11 11:12:12+00 | 47.38 | 14
2023-01-11 17:59:00+00 | AAPL | 2023-01-11 17:59:57+00 | 133.445 | 2023-01-11 17:59:57+00 | 133.445 | 2023-01-11 17:59:57+00 | 133.445 | 2023-01-11 17:59:57+00 | 133.445 |
2023-01-11 18:23:00+00 | AAPL | 2023-01-11 18:23:58+00 | 100 | 2023-01-11 18:23:58+00 | 100 | 2023-01-11 18:23:58+00 | 100 | 2023-01-11 18:23:58+00 | 100 | 10
2023-01-11 12:00:00+00 | AAPL | 2023-01-11 12:00:52+00 | 29.82 | 2023-01-11 12:00:52+00 | 29.82 | 2023-01-11 12:00:52+00 | 29.82 | 2023-01-11 12:00:52+00 | 29.82 |
```
9 changes: 8 additions & 1 deletion extension/src/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ accessor! { first_val() }
accessor! { last_val() }
accessor! { first_time() }
accessor! { last_time() }

accessor! { open() }
accessor! { close() }
accessor! { high() }
accessor! { low() }
accessor! { open_time() }
accessor! { high_time() }
accessor! { low_time() }
accessor! { close_time() }
// The rest are more complex, with String or other challenges. Leaving alone for now.

pg_type! {
Expand Down
Loading

0 comments on commit a00de9e

Please sign in to comment.