Repository navigation
Conversation
An .npy header states its row count in shape[0], and the data section that
follows is exactly prod(shape) * itemsize bytes. ClickHouse enforced that
contract in one place and took it on faith in three, so a file that cannot
deliver the rows it declares was rejected when read with a knowable size and
believed everywhere else.
Measured on 208-byte fixtures holding 10 <i8 rows, shape over-declared:
clickhouse local -q "SELECT count() FROM file('huge.npy', Npy)"
before: 1000000000, rc=0 after: Code: 117 INCORRECT_DATA
SELECT count() FROM file('huge.npy.gz', Npy)
before: 1000000000, rc=0 after: Code: 33 CANNOT_READ_ALL_DATA
SELECT * FROM file('huge.npy.gz', Npy)
before: 10 rows, rc=0 after: Code: 33 CANNOT_READ_ALL_DATA
optimize_count_from_files defaults to true, so the wrong count was the default
answer. Master was also self-contradictory one byte apart: losing part of a
declared row gave CANNOT_READ_ALL_DATA, losing all of it gave silence.
Root cause. readPrefix() is the only place that reconciles shape[0] with the
data, and it cannot do so when the size is hidden behind a decompressor, where
it silently skipped the check and discarded the fact that it had. Nothing else
re-derived the number from the data: countRows() returned shape[0] verbatim,
NpySchemaReader::readNumberOrRows() pre-filled the row-count cache from the
unvalidated header, and readRow() treated eof() as a legal end of input.
The fix records whether the declared count was confirmed (the size was knowable
and reconciled, or the declared payload is zero bytes, which no data section can
contradict), offers the O(1) count only where it was, deletes the schema reader's
row-count override, and lets the strict value reads already in place reject a
declared row whose bytes are missing. This is the treatment ClickHouse#118942 gives Iceberg
metadata that over-reports: withhold an unvouched count and fail closed to a real
scan.
count() stays O(1) for an honest uncompressed file: readPrefix confirms, countRows
answers from the header, no data is read, and the count cache is still filled after
a real read by addNumRowsToCache. Only unconfirmed input pays a read.
Deleting readNumberOrRows() rather than throwing from it keeps DESCRIBE and
SELECT * byte-identical to master: INCORRECT_DATA is not retryable during schema
inference, so a throw would have surfaced as CANNOT_EXTRACT_TABLE_STRUCTURE on a
file whose dtype is perfectly knowable. Clamping the count to the rows that fit
was rejected too: for knowable sizes master already rejects this input (ClickHouse#99585,
test 04056), so clamping would replace one wrong answer with another.
Removing readRow()'s eof() early return does not regress the zero-size cases it
was narrowed for in 31ea671 and 3090066: |S0 shape (3,) reaches
readAndInsertString with size == 0 and readStrict(to, 0) reads 0 of 0 bytes
without throwing, while shape (3, 0) ends the dimension loop with no elements, so
readValue is never called. Tests 8-10 of 04056 and 04512 cover both.
02908_Npy_files_caching.reference changes on the two lines that read
number_of_rows out of system.schema_inference_cache after a bare DESCRIBE, which
is no longer pre-filled from an unvalidated header. Every count value that test
asserts is unchanged.
Schema inference itself is untouched: readSchema() is not modified and DESCRIBE
returns array Int64 for every file above, before and after.
Co-Authored-By: Claude Opus 5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 2 gate passes, 1 finding then 0 on
Severity: ❌ blocker / Also noticed and deliberately not raised: the other Session id: cron:clickhouse-review-slot-8:20260920-152000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20260920-125905 |
|
Workflow [PR], commit [70cbee6] Summary: ✅
AI ReviewSummaryThis PR makes PR MetadataThe Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit 70cbee6.
Changed lines: Changed C/C++ lines covered: 12/12 (100.00%) · Uncovered code |
Build profile diff (arm_release)Commit See the job log for details. |
| " | tail -n 1 | ||
|
|
||
| # 6-10: compressed input has no knowable size, so the rows themselves must vouch for the count. | ||
| ${CLICKHOUSE_LOCAL} --query "SELECT count() FROM (SELECT * FROM file('$DATA_DIR/huge.npy.gz', 'Npy'))" 2>&1 | verdict |
There was a problem hiding this comment.
The compressed / unseekable path is still only validated lazily when readRow reaches the first missing row. That leaves an acceptance hole for early-stopping consumers: for example,
SELECT *
FROM file('huge.npy.gz', 'Npy')
SETTINGS max_block_size = 1
LIMIT 1can return one row successfully, because the pipeline never asks for row 11 and therefore never discovers that the file cannot deliver the declared shape[0]. Please add an early-stop repro like this and tighten the implementation so malformed compressed Npy files are rejected before they can leak a prefix of valid rows.
There was a problem hiding this comment.
Confirmed, and reachable more easily than the snippet suggests: max_block_size = 1 is not needed,
because the block size is clamped to the LIMIT, so plain
SELECT * FROM file('huge.npy.gz', 'Npy') LIMIT 1 returns one row at default settings. (As written
the snippet is a syntax error, SETTINGS has to follow LIMIT.)
Early-stop coverage added in ab2f2b3, on a gzipped file declaring 1000000000 rows over 10 real rows:
| query | this branch | master |
|---|---|---|
LIMIT 1 |
1 row | 1 row |
LIMIT 11 |
CANNOT_READ_ALL_DATA |
10 rows, rc=0 |
So an early stop is not exempt from validation: it is rejected as soon as it asks for a row the file
cannot deliver. What it is not rejected for is asking only for rows that are present, and I kept that
rather than tightening it.
For unseekable input there is nothing to compare shape[0] against until the stream has been
consumed, and the format is given a ReadBuffer it cannot rewind, so rejecting the prefix eagerly
means reading the whole payload before the first row: on an honest 200 MB .npy.gz that turns a
0.24 s LIMIT 1 into the 1.02 s of a full read, scaling with file size. The rows returned are the
file's own first rows, not a count taken from the header. Where the size is knowable, readPrefix
still rejects the same file up front with Code: 117 before any row is emitted, which is the
asymmetry this PR is built on: validate where validation is free, and make the count come from the
data everywhere else.
That last part is a semantics call, not a measurement. If the preference is to fail fast on a header
already known to be unsatisfiable even when the consumer stops early, I will implement it as an
explicit validate-by-consuming step for unseekable input.
A consumer that stops before the declared row count is reached is served from
the rows that are present. For unseekable input readPrefix has no size to
reconcile shape[0] against, so the contradiction can only surface once a row
past the data is requested, and a query that never asks for one never sees it.
Measured on a gzipped file declaring 1000000000 rows over 10 real rows:
SELECT * FROM file('huge.npy.gz', Npy) LIMIT 1 -> 1 row
SELECT * FROM file('huge.npy.gz', Npy) LIMIT 11 -> Code: 33 CANNOT_READ_ALL_DATA
Pristine master answers LIMIT 11 with 10 rows and rc=0, so both lines carry
signal. The rows an early stop does return are the file's own first rows, not
a fabricated count, and any consumer that reaches past them is rejected.
Rejecting the prefix eagerly instead would mean consuming the whole stream
before the first row, since the format is handed a ReadBuffer it cannot rewind:
on an honest 200 MB payload that turns a 0.24 s LIMIT 1 into the 1.02 s of a
full read, scaling with file size. Where the size is knowable nothing changes,
because readPrefix still rejects the same file up front (Code: 117).
max_block_size is pinned because tests/clickhouse-test randomizes it in
[8000, 100000], and at max_block_size equal to the LIMIT the source is asked
for a second block, which reads a row the file does not hold.
Co-Authored-By: Claude Opus 5 <[email protected]>
The assertion that a confirmed header answers count() without touching the payload read `sum(value) < 1000000` over a single system.events row. sum() over no rows is 0, so the oracle also passed once the event it names stopped existing, which is the state a rename or a dropped counter leaves behind. Measured against a deliberately absent event name: the old form still answers 1, the new form answers 0. Against the real event both answer 1, so the expected output does not change. Co-Authored-By: Claude Opus 5 <[email protected]>
CI finish ledger - f243b43Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Measured on this head: 176 contexts, 155 SUCCESS / 18 SKIPPED / 3 FAILURE. CIDB records zero Session id: cron:our-pr-ci-monitor:20260921-050309 |
| SELECT number_of_rows FROM system.schema_inference_cache WHERE format = 'Npy'; | ||
| " | ||
|
|
||
| # An early stop is served from the rows that are there: where the size is not knowable, the |
There was a problem hiding this comment.
This early-stop exception seems intentional, but the PR body and changelog entry still promise the stronger behavior that malformed compressed reads are now simply "rejected". With this test in place, LIMIT 1 on the same malformed .npy.gz is explicitly expected to succeed and return the available prefix. Please narrow the user-facing wording to full reads / count() paths, or mention the early-stop exception explicitly, so the release note matches the behavior this test locks in.
There was a problem hiding this comment.
Confirmed, and the release note was the only place that overclaimed.
It said a compressed read is "now rejected" without qualification, while this test pins the
opposite for an early stop: reference line 21 is 1 for the LIMIT 1 query, against
CANNOT_READ_ALL_DATA on line 22 for LIMIT 11 over the same huge.npy.gz.
The changelog entry now narrows the rejection to a full read and states the exception in the same
sentence: behind a decompressor the shortfall shows only once a row past the data is read, so a
query stopping earlier (LIMIT 1) still returns the rows present. ### Description already carried
this implicitly ("reject a declared row whose bytes are missing"); it now says it outright, so a
reader does not have to derive the boundary from the mechanism.
No code change, and the early-stop semantics are unchanged: that call was made with measurements in
#121201 (comment), and the offer there
still stands. If the preference is to fail fast on a header already known to be unsatisfiable even
when the consumer stops early, I will implement it as an explicit validate-by-consuming step for
unseekable input.
I re-checked the changelog category instead of leaving it alone, since the entry changed. On
v26.9.2.8-stable the same file raises INCORRECT_DATA from SELECT * while count() answers the
declared shape[0], reachable through file(..., 'Npy') at the default
optimize_count_from_files = 1, so it stays a bug fix in a released version and the category is
unchanged.
Related: #99585
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed the
Npyinput format reporting a row count the file does not hold.SELECT count()over an.npyfile whose header over-declaresshape[0]answered with the declared number, and a full read through a compressed wrapper silently returned fewer rows than declared; both are now rejected. Behind a decompressor the shortfall shows only once a row past the data is read, so a query stopping earlier (LIMIT 1) still returns the rows present.Description
An
.npyheader states its row count inshape[0], followed by exactlyprod(shape) * itemsizebytes of data. A truncated or crafted file was rejected only when the size was knowable.optimize_count_from_filesdefaults totrue, so the wrong count was the default.Measured with
clickhouse localon 208-byte fixtures holding 10<i8rows withshape(1000000000,)(the compressed rows reproduce through a server too):count()onhuge.npy1000000000, rc=0INCORRECT_DATAcount()onhuge.npy.gz1000000000, rc=0CANNOT_READ_ALL_DATASELECT *onhuge.npy.gzCANNOT_READ_ALL_DATAMaster was self-contradictory one byte apart: losing part of a declared row gave
CANNOT_READ_ALL_DATA, losing all of it gave silence.Root cause:
readPrefix()alone reconcilesshape[0]with the data and discarded the fact it skipped the check; nothing else re-derived the count.The fix records whether the declared count was confirmed, offers the O(1) count only where it was, deletes
NpySchemaReader::readNumberOrRows()so the row-count cache is never pre-filled from an unvalidated header, and lets the existing strict value reads reject a declared row whose bytes are missing, which a read stopping earlier never reaches. #118942 treats over-reporting Iceberg metadata the same way: withhold an unvouched count, fail closed to a real scan.count()stays O(1) for an honest uncompressed file and the cache is still filled after a real read, so only unconfirmed input pays one.DESCRIBEreturnsarray Int64for every file above andreadSchema()is untouched.02908_Npy_files_caching.referencechanges only on its twonumber_of_rowslines, which a bareDESCRIBEno longer pre-fills; every count value it asserts is unchanged.Workflow [PR]
Sync PR [sync-upstream/pr/121201]