Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
  • Loading branch information
waynexia committed May 17, 2024
1 parent 0940119 commit 130c788
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Time window is an important attribute of your continuous aggregation query. It defines how the data is aggregated in the flow. GreptimeDB provides two types of time windows: `hop` and `tumble`, or "sliding window" and "fixed window" in other words. You can specify the time window in the `GROUP BY` clause using `hop()` function or `tumble()` function respectively. These two functions are only supported in continuous aggregate queries's `GROUP BY` position.

Here illustrates the `hop()` and `tumble()` functions works:
Here illustrates how the `hop()` and `tumble()` functions work:

![Time Window](/time-window.svg)

Expand Down
80 changes: 79 additions & 1 deletion docs/nightly/en/user-guide/continuous-aggregation/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,82 @@ GreptimeDB provides a continuous aggregation feature that allows you to aggregat
- [Query](./query.md) describes how to write a continuous aggregation query.
- [Expression](./expression.md) is a reference of available expressions in the continuous aggregation query.

![Continuous Aggregation](/flow-ani.svg)
![Continuous Aggregation](/flow-ani.svg)

## Example

Here is a complete example of how a continuous aggregation query looks like.

First, we create a source table `numbers_input` and a sink table `out_num_cnt` with following clauses:

```sql
CREATE TABLE numbers_input (
number INT,
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(number),
TIME INDEX(ts)
);
```

```sql
CREATE TABLE out_num_cnt (
sum_number BIGINT,
start_window TIMESTAMP TIME INDEX,
end_window TIMESTAMP,
update_at TIMESTAMP,
);
```

Then create our flow `test_numbers` to aggregate the sum of `number` column in `numbers_input` table. The aggregation is calculated in 1-second fixed windows.

```sql
CREATE FLOW test_numbers
SINK TO out_num_cnt
AS
SELECT sum(number) FROM numbers_input GROUP BY tumble(ts, '1 second');
```

Now we can insert some data into `numbers_input` table and see the result in `out_num_cnt` table.

```sql
INSERT INTO numbers_input
VALUES
(20,1625097600000),
(22,1625097600500);
```

The output table `out_num_cnt` should have the following data:

```sql
SELECT * FROM out_num_cnt;
```

```sql
sum_number | start_window | end_window | update_at
------------+----------------------------+----------------------------+----------------------------
42 | 2021-07-01 00:00:00.000000 | 2021-07-01 00:00:01.000000 | 2024-05-17 08:32:56.026000
(1 row)
```

Let's try to insert more data into `numbers_input` table.

```sql
public=> INSERT INTO numbers_input
VALUES
(23,1625097601000),
(24,1625097601500);
```

Now the output table `out_num_cnt` should have the following data:

```sql
SELECT * FROM out_num_cnt;
```

```sql
sum_number | start_window | end_window | update_at
------------+----------------------------+----------------------------+----------------------------
42 | 2021-07-01 00:00:00.000000 | 2021-07-01 00:00:01.000000 | 2024-05-17 08:32:56.026000
47 | 2021-07-01 00:00:01.000000 | 2021-07-01 00:00:02.000000 | 2024-05-17 08:33:10.048000
(2 rows)
```

0 comments on commit 130c788

Please sign in to comment.