Files
greptimedb/tests/cases/standalone/common/flow/flow_incremental_simple.sql
discord9 b5fdbc4e60 feat(flow): support incremental source reads for simple SQL flows
Window-less single-source SQL flows with a row-local projection/filter
currently rescan all history on every evaluation interval. Add an opt-in
path that reads only the source delta since the last checkpoint, keeping
the existing batching scheduler, sequence-range scan capability and
checkpoint watermark proof.

Eligibility is decided on the plan a round actually executes: the retained
plan is now built without the flow optimizer, because constant folding
would turn a time-dependent expression such as `now()` into a literal and
hide it from the predicate.

The delta path reuses the existing (checkpoint, scan-open snapshot]
sequence range. It fails closed: a row-local delta flow that cannot build
incremental extensions restores its dirty signal instead of running an
unfiltered scan, a source without `preserve_row_sequence` is a hard error
rather than falling back to memtable-only reads, and an append-mode sink
is rejected because a restarted flow replays a full snapshot.

Auto-created sinks derive their time index and primary keys from source
column lineage, so rows of different entities that share a timestamp no
longer collapse into one. The sink remains a keyed materialization: it
does not promise to preserve every original source write.

Everything is gated on `experimental_enable_incremental_read`, so flows
that do not opt in keep their existing behavior and sink layout.

Signed-off-by: discord9 <55937128+discord9@users.noreply.github.com>
2026-10-19 19:49:05 +08:00

99 lines
3.6 KiB
SQL

-- A plain projection/filter flow without a time window reads only source rows
-- written since the last checkpoint when incremental reads are opted in.
--
-- `ADMIN FLUSH_FLOW` returns the number of rows the round wrote to the sink, so
-- the numbers below are the assertion that a round read only the delta: a round
-- that rescanned history would rewrite every already-materialized row
-- (2 + 2 = 4 after the second round instead of 2).
--
-- The source must be append-only and preserve row sequence. The sink is a keyed
-- materialization: two rows sharing the sink key collapse.
CREATE TABLE flow_incremental_simple_input (
host STRING,
val DOUBLE,
ts TIMESTAMP TIME INDEX,
PRIMARY KEY(host)
) WITH (
append_mode = 'true',
preserve_row_sequence = 'true'
);
-- A long interval keeps every round explicit, so the row counts are stable.
CREATE FLOW flow_incremental_simple
SINK TO flow_incremental_simple_sink
EVAL INTERVAL '1h'
WITH (experimental_enable_incremental_read = 'true')
AS
SELECT host, val, ts FROM flow_incremental_simple_input WHERE val >= 0;
-- Both hosts share one timestamp, so the sink keeps two rows and its time index
-- is the source time index rather than the first timestamp column it finds.
INSERT INTO flow_incremental_simple_input VALUES
('host_a', 1.0, '2024-01-01 00:00:00'),
('host_b', 2.0, '2024-01-01 00:00:00');
ADMIN FLUSH_FLOW('flow_incremental_simple');
SELECT host, val, ts FROM flow_incremental_simple_sink ORDER BY host, ts;
SHOW CREATE TABLE flow_incremental_simple_sink;
-- Move the checkpointed source rows into SST files so the next round must use
-- the exact sequence range instead of rescanning them.
ADMIN FLUSH_TABLE('flow_incremental_simple_sink');
ADMIN FLUSH_TABLE('flow_incremental_simple_input');
INSERT INTO flow_incremental_simple_input VALUES
('host_a', 3.0, '2024-01-01 00:01:00'),
('host_c', 4.0, '2024-01-01 00:01:00');
ADMIN FLUSH_TABLE('flow_incremental_simple_input');
ADMIN FLUSH_FLOW('flow_incremental_simple');
SELECT host, val, ts FROM flow_incremental_simple_sink ORDER BY host, ts;
-- A fully filtered round writes nothing; its scan still proves a watermark, so
-- the checkpoint advances and the following insert must be processed.
INSERT INTO flow_incremental_simple_input VALUES
('host_a', -1.0, '2024-01-01 00:02:00');
ADMIN FLUSH_FLOW('flow_incremental_simple');
SELECT host, val, ts FROM flow_incremental_simple_sink ORDER BY host, ts;
INSERT INTO flow_incremental_simple_input VALUES
('host_d', 5.0, '2024-01-01 00:03:00');
ADMIN FLUSH_FLOW('flow_incremental_simple');
SELECT host, val, ts FROM flow_incremental_simple_sink ORDER BY host, ts;
-- Without the opt-in option a window-less flow keeps rescanning history: the same
-- second round rewrites all four rows instead of only the two new ones.
CREATE FLOW flow_incremental_simple_full
SINK TO flow_incremental_simple_full_sink
EVAL INTERVAL '1h'
AS
SELECT host, val, ts FROM flow_incremental_simple_input WHERE val >= 0;
INSERT INTO flow_incremental_simple_input VALUES
('host_e', 6.0, '2024-01-01 00:04:00');
ADMIN FLUSH_FLOW('flow_incremental_simple_full');
SELECT host, val, ts FROM flow_incremental_simple_full_sink ORDER BY host, ts;
INSERT INTO flow_incremental_simple_input VALUES
('host_f', 7.0, '2024-01-01 00:05:00');
ADMIN FLUSH_FLOW('flow_incremental_simple_full');
SELECT host, val, ts FROM flow_incremental_simple_full_sink ORDER BY host, ts;
DROP FLOW flow_incremental_simple;
DROP FLOW flow_incremental_simple_full;
DROP TABLE flow_incremental_simple_input;
DROP TABLE flow_incremental_simple_sink;
DROP TABLE flow_incremental_simple_full_sink;