bug: consuming a stream in batches (`WITH (MAX_BATCH_SIZE = n)` / `stream_consume_batch_size_hint`) records the latest table version with the batch's snapshot, so rows after the batch boundary are never delivered once compaction rewrites their blocks
Maintainers usually reply within 1 day
@zhyass is already working on this.
Since Nov 23, 2026.
Assessment
This issue has not been assessed yet.
Description
Search before asking
- I had searched in the issues and found no similar issues.
Version
main 057efa66e6 (nightly docker image v1.2.961-057efa66e6, 2026-11-08); also v1.2.960-nightly-3c9357c866.
What's Wrong?
When a stream is consumed in batches (FROM <stream> WITH (MAX_BATCH_SIZE = n), or the session setting
stream_consume_batch_size_hint), the new offset written at commit pairs the batch boundary snapshot with the
latest table version. Rows inserted after the boundary are still delivered as long as their blocks are never
rewritten (their _origin_version is NULL). But if a compaction (OPTIMIZE TABLE … COMPACT, or the automatic
compaction that runs after small inserts by default), a recluster or a mutation rewrites those blocks before the
next batch, the rows get a non-NULL _origin_version (the table version the rewrite started from), which is below
the recorded (too new) version, and the change query filters them out as already consumed. The consumer never
receives them, the offset moves past them, and SELECT count(*) FROM <stream> reports 0 pending. No error.
Example: with only INSERTs and default compaction settings, consuming with stream_consume_batch_size_hint = 2
lost 26 of 48 rows. APPEND_ONLY = true and false (when the changes after the boundary are insert-only) are both
affected. Without batching, or when no block is rewritten between batches, every row is delivered.
Cause:
StreamTableasksFuseTable::find_stream_batch_snapshotfor the batch boundary. It returns the batch table and
the table version of the boundary snapshot (source_seq = snapshot.prev_table_seq + 1,
stream_batch.rs#L246-L253),
but the caller drops the version:
stream_table.rs#L172-L181
let Some((batch_table, _)) = fuse_table.find_stream_batch_snapshot(…).- The batch table comes from
load_table_by_snapshot, whoseident.seqis "JUST a place holder"
(navigate.rs#L205),
i.e. the latest table version. - At commit,
build_update_stream_reqswritesOPT_KEY_TABLE_VER = inner_fuse.get_table_info().ident.seq(the
placeholder) andOPT_KEY_SNAPSHOT_LOCATION = inner_fuse.snapshot_loc()(the boundary)
(interpreters/common/stream.rs#L59-L65). - The next read computes changes from the boundary snapshot with the append-only filter
not(is_not_null(_origin_version) and (_origin_version < {seq} or …))
(changes.rs#L163-L170),
which drops rewritten rows whose_origin_versionis below the recorded version.
The materialized-view refresh uses the same batch search and keeps the returned version (source_seq = batch_seq,
refresh.rs#L260-L274);
the stream path should do the same and record the boundary snapshot's version.
How to Reproduce?
CREATE TABLE orders (id INT, item STRING);
INSERT INTO orders VALUES (0, 'before the stream');
CREATE STREAM orders_s ON TABLE orders APPEND_ONLY = true; -- same with APPEND_ONLY = false
CREATE STREAM orders_all ON TABLE orders APPEND_ONLY = true; -- control, consumed without batching
CREATE TABLE orders_copy (id INT, item STRING);
CREATE TABLE orders_copy_all (id INT, item STRING);
INSERT INTO orders VALUES (1, 'order 1');
INSERT INTO orders VALUES (2, 'order 2');
INSERT INTO orders VALUES (3, 'order 3');
INSERT INTO orders_copy SELECT id, item FROM orders_s WITH (MAX_BATCH_SIZE = 1); -- 1 row (id 1)
INSERT INTO orders_copy_all SELECT id, item FROM orders_all; -- 3 rows
SELECT name, table_version, snapshot_location FROM system.streams WHERE database = 'default' ORDER BY name;
OPTIMIZE TABLE orders COMPACT;
INSERT INTO orders_copy SELECT id, item FROM orders_s WITH (MAX_BATCH_SIZE = 1); -- 1 row (id 2)
INSERT INTO orders_copy SELECT id, item FROM orders_s WITH (MAX_BATCH_SIZE = 1); -- 0 rows
SELECT count(*) FROM orders_s;
SELECT id FROM orders_copy ORDER BY id;
Result on main 057efa66e6 (the same on v1.2.960-nightly apart from the snapshot names):
orders_all | 50 | 1/10/_ss/h01a1bdb4ddfc763996c4c568019c398b_v4.mpk <- consumed fully: the latest snapshot
orders_s | 50 | 1/10/_ss/h01a1bdb4dcca7ecbad65fcf58ddb3662_v4.mpk <- after the 1-row batch: the boundary snapshot,
but the same (latest) table version
-- after OPTIMIZE: the next batch delivers id 2, then 0 rows forever
SELECT count(*) FROM orders_s: 0
orders_copy: 1, 2 <- id 3 is never delivered
Expected: orders_copy ends with ids 1, 2, 3 (as orders_copy_all does), and the batch offset records the table
version of the boundary snapshot, not the version of the latest one.
With the setting instead of the hint and only INSERTs, no explicit OPTIMIZE: 12 rounds of four single-row
INSERT INTO events … (ids 1–48), each round followed by one INSERT INTO events_copy SELECT id, payload FROM events_s with stream_consume_batch_size_hint = 2, then draining the stream the same way. The default automatic
compaction (auto_compaction_imperfect_blocks_threshold = 25) rewrites the blocks: 22 rows are delivered, ids
23–48 never are, the stream reports 0 pending, and the table has one compacted block.
Single node, default settings, HTTP handler.
Are you willing to submit PR?
- Yes I am willing to submit a PR!
- Dominant language
- Rust
- Stars
- 9.5k
- Forks
- 903
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 204
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from databendlabs/databend
-
bug: segment pruning skips rows when only some blocks have statistics of an added columnPossibly taken @zhyass claimed this today. OpenC-bug
databendlabs/databend#21244 · 1 assignee ·
Maintainers usually reply within 1 day
-
bug: ORDER BY ... NULLS LAST LIMIT returns wrong rows when an inverted index answers the filter and the sort column has NULLsPossibly taken @b41sh claimed this today. OpenC-bug
databendlabs/databend#21243 · 1 assignee ·
Maintainers usually reply within 1 day
-
Feature: Reduce role-graph work for explicit secondary rolesPossibly taken @TCeason claimed this 1 day ago. OpenC-feature
databendlabs/databend#21226 · 1 assignee ·
Maintainers usually reply within 1 day
-
Feature: Key role-dependent query results by authorization contextPossibly taken @TCeason claimed this 1 day ago. OpenC-feature
databendlabs/databend#21225 · 1 assignee ·
Maintainers usually reply within 1 day
-
bug: filters folded to false on block statistics drop matching rows: cast-eliminated equality under NOT, float arithmetic over NaN and infinities, variant IS DISTINCT FROMPossibly taken @forsaken628 claimed this 1 day ago. OpenC-bug
databendlabs/databend#21216 · 1 assignee ·
Maintainers usually reply within 1 day
All issues in databendlabs/databend
Similar issues
-
marketing
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
eliasstravik/herdr-projects#1605 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
jlcodes99/cockpit-tools#3332 ·
Maintainers usually reply within 3 days
-
Cognito: the email MFA code (EMAIL_OTP challenge after a password flow) has 8 digits; AWS delivers 6Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
aws/aws-emulator#692 ·
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 82/100
pamburus/hl#1779 · 1 comment ·
Maintainers usually reply within 1 day