Skip to content

Reset the Npy row counter between streamed inputs - #129109

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-npy-reset-row-counter-streamed-inputs
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-npy-reset-row-counter-streamed-inputs

Conversation

@groeneai

@groeneai groeneai commented Nov 3, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed lost rows when several Npy inputs are parsed by the same format, as with the entries of one asynchronous insert batch: every input after the first one was cut by the row count of the inputs before it, and the lost rows were not reported.

Description

Related: #128943 (the same defect class in MsgPack, whose description noted this one).

On 26.10.2.103, two asynchronous Npy inserts that land in one batch insert only the first one's rows; system.asynchronous_insert_log reports both Ok. For example, entries of 2, 3 and 1 rows give 3 rows instead of 6. 26.3 is correct: this is a regression from #101179 (26.4).

StreamingFormatExecutor reuses one input format for all entries of a batch (and, by the same code, for Kafka, NATS and RabbitMQ messages) and calls resetParser between them, also after a parse error. Since #101179, NpyRowInputFormat::readRow stops at read_rows >= shape[0], but read_rows was never reset, so each later input read only shape[0] - read_rows rows. After a failed input the next valid one was cut as well.

NpyRowInputFormat::resetParser now resets read_rows and counted_rows after the base reset. File, URL and S3 reads build one format per file and are unchanged.

The new test queues entries of 2, 3 and 1 rows, and a truncated entry followed by a valid one, into one batch each. On master it gives [1,2,3] and []; with the fix it passes 50/50 with randomized settings.

SQLite, MySQLDump and One drop or reject later inputs of a batch in the same way; not addressed here.


Workflow [PR]
Sync PR [sync-upstream/pr/129109]

StreamingFormatExecutor reuses one input format for all entries of an
asynchronous insert batch (and for Kafka, NATS and RabbitMQ messages) and
calls resetParser() between them, also after a parse error.
IRowInputFormat::resetParser() zeroes total_rows, so readPrefix() parses
the next input's header again, but NpyRowInputFormat had no override, so
read_rows kept the count of the previous inputs.

Since ClickHouse#101179 (first in 26.4) readRow() stops at read_rows >= shape[0]
instead of at eof (needed for zero-size elements), so every later input
read only shape[0] - read_rows rows, often none, and the entries were
still logged Ok. After a failed input the next valid one was cut too.

Reset read_rows and counted_rows in a resetParser() override. 26.3 is
correct because it stopped at eof.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings, 1 round) plus my own cold review (1 nit, dropped).

# Sev Finding Verdict Evidence / action
1 💡 No test covers the counted_rows reset noted, not blocking countRows runs only with need_only_count, which file/URL/object-storage reads set on a format built per file; StreamingFormatExecutor never sets it, so the reset is unobservable through reuse today

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-self-slot-58:20261103-215400

@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 05497_npy_async_insert_batch fails 10/10 on the unfixed head and on the master debug build, every run with [1,2,3] instead of [1,2,3,4,5,6] and [] instead of [1,2].
b Root cause explained? Yes. StreamingFormatExecutor reuses one format per batch and calls resetParser between entries; NpyRowInputFormat had no override, so read_rows (the row stop since #101179) carried over and each later entry read only shape[0] - read_rows rows.
c Fix matches root cause? Yes. resetParser override resets the per-input counters read_rows and counted_rows, the same hook other formats use for per-input state. No tags, bounds or test changes.
d Test intent preserved / new tests added? New stateless test: entries of 2, 3 and 1 rows in one batch, and a truncated entry followed by a valid one. No existing test changed.
e Both directions demonstrated? Yes, same commit, Build-ID-verified servers: unfixed 10/10 FAIL, fixed 50/50 OK with randomized settings.
f Fix is general across code paths? counted_rows reset too; the header is re-parsed per input; the schema reader is never reused; async insert disables parallel parsing. Kafka, NATS and RabbitMQ go through the same StreamingFormatExecutor reuse. SQLite, MySQLDump and One have the same class of defect in other classes, not addressed here (one bug per PR).
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked 1-D and 2-D (Array(Int64)) arrays, a zero-row entry, both entry orders, longer and shorter next entries, and a truncated entry before and between valid ones: all correct with the fix. The counter is per row, independent of the element type.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or storage change; the behaviour is that of 26.3 again.
i Invariants and contracts preserved? After resetParser the format parses the next input as a fresh object would; the base reset still runs first, and the error path is covered because resetParser also runs in every catch block of StreamingFormatExecutor.

Session id: cron:clickhouse-impl-self-slot-57:20261103-213300

@groeneai

groeneai commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator Author

cc @Avogar @al13n321, could you review this and add the can be tested label so CI runs? NpyRowInputFormat had no resetParser override, so read_rows carried over between the entries of one async insert batch and every entry after the first lost rows (the same class as the MsgPack fix in #128943).

@clickhouse-gh clickhouse-gh Bot added the can be tested Allows running workflows for external contributors label Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ed01bbe]

Summary: ✅


AI Review

Summary

This PR fixes silently dropped rows when a NpyRowInputFormat is reused across streamed inputs by resetting read_rows and counted_rows. The regression test covers differently sized asynchronous insert entries and recovery after a truncated entry, and passed in Fast test CI. No actionable findings.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). labels Nov 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ed01bbe77 with master ebff85edc (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 10 s compile time in total, 7 of them have a recent master baseline.

Job report

@groeneai

groeneai commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

Replaced by #129209, which fixes this class for every input format in StreamingFormatExecutor: a format is reused only if it resets all its state, so Npy is now created anew for each input, with no per-format code (the Npy batch case is in the extended 02187_async_inserts_all_formats). Closing.

@groeneai groeneai closed this Nov 4, 2026
alexey-milovidov pushed a commit to bobrik/ClickHouse that referenced this pull request Nov 13, 2026
…self

StreamingFormatExecutor (async insert flushes, Kafka, Kafka2, NATS,
RabbitMQ, FileLog) kept one input format object for many independent
inputs and called resetParser() between them, trusting every format to
bring itself back to the state of a fresh object. Nothing enforced that:
32 of the 82 input format names kept some per-input field (One `done`,
Npy `read_rows`, MySQLDump `table_name` and column mapping, SQLite
flags and database, JSON `parse_as_json_each_row`, DWARF section
extractors, TSV `first_row`, CustomSeparated `columns` and the detected
header, RowBinaryWithNamesAndTypes `read_data_types`, Template
`row_num`), so a later input of an async insert batch or a later
message lost rows, was rejected, or was parsed with the previous
input's state. Per-format resetParser fixes (ClickHouse#128943, ClickHouse#129109, ClickHouse#129140)
leave the default unsafe for the next format and the next field.

Invert the default: a format is reused only when its name is registered
with FormatFactory::markInputFormatSupportsResetParser; any other format
is released after each input and created anew for the next one. The
executor now takes a format creator and an AddingDefaultsTransform
creator (the transform holds a reference to the format). The streaming
engines compute their single-threaded parser resources once and share
them between created formats.

Marked: the 50 names whose reset is complete and 26 hot names completed
here with one statement each (TSV first_row, CustomSeparated columns,
WithNamesAndTypes is_header_detected, RowBinary read_data_types,
Template row_num). Reuse matters for them: a fresh CapnProto, Template,
Protobuf, Regexp or AvroConfluent object reads or compiles schemas.
Not marked, so re-created with no per-format code: One, Npy, MySQLDump,
SQLite, JSON, DWARF. DWARF's incomplete resetParser is deleted.

02187_async_inserts_all_formats now also inserts every format as one
batch of two different inputs, plus targeted cases for the leaks that
a same-shaped pair does not show, and a FileLog case.

Closes ClickHouse#122351

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-formats Input/output formats (CSV/JSON/Parquet/ORC/Arrow/Protobuf/etc.). pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant