CREATE TABLE bytes_log (
    byte INT,
    ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    -- event time
    TIME INDEX(ts)
);

Affected Rows: 0

CREATE TABLE approx_rate (
    rate DOUBLE,
    time_window TIMESTAMP,
    update_at TIMESTAMP,
    TIME INDEX(time_window)
);

Affected Rows: 0

CREATE FLOW find_approx_rate SINK TO approx_rate AS
SELECT
    (max(byte) - min(byte)) / 30.0 as rate,
    date_bin(INTERVAL '30 second', ts) as time_window
from
    bytes_log
GROUP BY
    time_window;

Affected Rows: 0

INSERT INTO
    bytes_log (byte)
VALUES
    (NULL),
    (300);

Affected Rows: 2

-- SQLNESS REPLACE (ADMIN\sFLUSH_FLOW\('\w+'\)\s+\|\n\+-+\+\n\|\s+)[0-9]+\s+\| $1 FLOW_FLUSHED  |
ADMIN FLUSH_FLOW('find_approx_rate');

+--------------------------------------+
| ADMIN FLUSH_FLOW('find_approx_rate') |
+--------------------------------------+
|  FLOW_FLUSHED  |
+--------------------------------------+

-- since ts is default to now(), omit it when querying
SELECT
    rate
FROM
    approx_rate;

+------+
| rate |
+------+
| 0.0  |
+------+

DROP FLOW find_approx_rate;

Affected Rows: 0

DROP TABLE bytes_log;

Affected Rows: 0

DROP TABLE approx_rate;

Affected Rows: 0

