Skip to content

Infer numbers starting from zero as strings in TSV - #44522

Merged
Avogar merged 2 commits into
ClickHouse:masterfrom
Avogar:zero-numbers
Dec 27, 2022
Merged

Avogar merged 2 commits into
ClickHouse:masterfrom
Avogar:zero-numbers

Conversation

@Avogar

@Avogar Avogar commented Dec 22, 2022

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Infer numbers starting from zero as strings in TSV as we cannot parse them as integer

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-not-for-changelog This PR should not be mentioned in the changelog label Dec 22, 2022
@nickitat

Copy link
Copy Markdown
Member

Even if number = 0?

@nickitat nickitat self-assigned this Dec 23, 2022
@Avogar

Avogar commented Dec 23, 2022

Copy link
Copy Markdown
Member Author

Even if number = 0?

Oups, forgot to add a check that field size is not 1. Thanks!

@Avogar

Avogar commented Dec 23, 2022

Copy link
Copy Markdown
Member Author

AST fuzzer (debug) — Received signal 11 - #44549

@Avogar
Avogar merged commit ac162a2 into ClickHouse:master Dec 27, 2022
asya-ch pushed a commit to melvynator/ClickHouse that referenced this pull request Sep 15, 2026
In the `Escaped` and `Raw` escaping-rule family (`TSV`, `TSVRaw`, `TSKV`, and
`CustomSeparated` at its default escaping rule) an integer column could not be
read for any zero-padded or `+`-signed spelling, while every other text format
reads the same bytes. Issue ClickHouse#5999, closed in 2019, still reproduced verbatim.

The most visible form is inference contradicting its own reader: `DESCRIBE` on
a file containing `-007` returns `Nullable(Int64)`, and `SELECT` on that same
file then throws `CANNOT_PARSE_INPUT_ASSERTION_FAILED`. Unsigned spellings were
shielded from the error only because inference demoted them to `String`, which
in turn made TSV header auto-detection consume the first data row as a column
name, so `007\n123\n456` counted 2 rows instead of 3.

`readIntTextUnsafe` consumed an optional `-`, then short-circuited on the first
`0` and returned that as the whole value, leaving the remaining digits in the
buffer; and `+` was neither `-` nor a digit, so the digit loop read nothing and
reported success with zero. Either way the caller found unread bytes where a
delimiter belonged. Its own doc comment listed both gaps.

The reader now skips a run of leading zeros and accepts a redundant `+`.
Because a `+` is newly consumed as a sign, the reader also requires a decimal
digit after one: without that, a field that is a lone `+` would read as the
value zero, a wrong result this change would itself have introduced. Before it,
the `+` was left unconsumed and the format layer refused the remainder, so end
to end this moves `CANNOT_PARSE_INPUT_ASSERTION_FAILED` to
`CANNOT_PARSE_NUMBER` for that one spelling.

The `-` branch is deliberately left exactly as it was, including its existing
end-of-input check. An earlier revision of this change required a digit after
either sign, which also rejected a field that is a lone `-`. That is documented
behaviour, not an accident: the `TabSeparated` format documentation states that
when reading it is allowed to parse "a string consisting of just a minus sign as
a zero" for signed types, and the generated page carries the same sentence. So
`-` still reads as zero, `-\t1` still reads as the two columns zero and one, and
`TSV` legitimately differs from `CSV` here, which never made that promise. All
thirty-one `-`-first observations across the format-level and reader-level
probes are byte-identical to master.

The reader has one call site, `SerializationNumber::deserializeText`, but that
is where many entry points converge, so the scope of the two halves differs.
`SerializationNumber` overrides only the JSON and CSV entry points, routing
those to the tolerant `readIntText` -- which is why they always worked and why
CSV is used here as the oracle for every expected value. It overrides neither
`deserializeTextQuoted` nor `deserializeTextRaw` nor `deserializeWholeText`, so
`SimpleTextSerialization` sends all of them to the same `deserializeText`. The
acceptance half is an improvement wherever it is reached. The lone-`+`
rejection, being a rejection, reaches all of them: beyond the four named formats
that is `Values` and `MySQLDump` through `deserializeTextQuoted`, and sixteen
external `deserializeWholeText` consumers including query parameters,
dictionary `null_value`, `Map` keys and bloom-filter keys.

With the reader repaired, the schema-inference guard that forced a zero-padded
integer to `String` had no premise left; its comment said it existed only
because of this reader. That guard is @Avogar's ClickHouse#44522, whose stated premise was
"Infer numbers starting from zero as strings in TSV as we cannot parse them as
integer" -- exactly what this change falsifies. Its regression test
`02514_tsv_zero_started_number` is re-baselined rather than deleted: `0123` now
reads as `Nullable(Int64)` `123`, which is what CSV has always answered, so the
test keeps asserting the same thing against the corrected expectation. Removing
the guard also closes the row-loss symptom in the same change.
`FormatSettings::allow_number_leading_zeros` and its single setter in hive
partitioning go with it: the flag was a private opt-in to what everybody now
gets, and hive behaviour is unchanged.

On cost. An earlier reader-level benchmark over unpadded integers found the
change free, but it structurally excluded the path this change lengthens, so it
was redone as a sweep over the density of single-`0` fields, each delta measured
against a same-binary control arm. That sweep found a real regression at every
density, including at zero density where neither a zero nor a sign exists --
which is what located the cause. It is neither parsing feature: constructing
`CANNOT_PARSE_NUMBER` inline for the digit-after-sign check grew the reader's
stack frame from 8 to 72 bytes and its body from 85 to 143 instructions on every
call. Throwing from an out-of-line `[[noreturn]]` helper, as this function's
neighbour `throwReadAfterEOF` already does, restores the 8-byte frame and leaves
+2.3% at 0% density rising to +6.8% at 50%, against a control-arm spread of 0.6%
-- about half to three quarters of the regression recovered. Marking the branch
`[[unlikely]]` on its own recovers nothing: measured, it emits the identical
72-byte frame. This deliberately does not route the caller to `readIntText`,
which is the slower variant the doc comment contrasts against, so the
specialized loop is kept.

Also corrected: `ReadHelpers.h`'s comment on why the date/time readers avoid
this function said it was because leading zeros were not supported. They now
are; the surviving reason is that `readIntTextUnsafe` cannot check for overflow,
which those call sites request explicitly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants