Repository navigation
Infer schema for input() table function from fixed-schema formats - #104533
alexey-milovidov merged 4 commits into
Conversation
When the surrounding INSERT query specifies a format that has a fixed
schema (LineAsString, RawBLOB, JSONAsString, JSONAsObject, ...), the
input() table function now derives its structure from that format
without requiring the user to repeat it as input('line String').
Previously this query failed with CANNOT_EXTRACT_TABLE_STRUCTURE:
INSERT INTO logs SELECT line FROM input() FORMAT LineAsString
even though LineAsString unambiguously declares a single
String column named line. The structure_hint mechanism set by the
analyzer from the destination table only worked when the SELECT could
be matched to the destination's columns, which fails as soon as
constant projections are added or destination column names differ.
This change consults FormatFactory's external schema reader for the
configured insert format inside getActualTableStructure. The format's
fixed schema is authoritative for fixed-schema formats and overrides
any guess derived from the destination table.
Closes ClickHouse#104532
|
Pre-PR validation gate (per a) Deterministic repro? Yes.
With the fix it runs to completion and the rows arrive in the destination table. b) Root cause explained? The c) Fix matches root cause? The fix queries d) Test intent preserved? / New tests added? The new test e) Demonstrated in both directions? Yes — same query reproduces the exception against the pre-fix binary and succeeds against the post-fix binary. Verified via TCP and HTTP transports. f) Fix is general, not a narrow patch? The change automatically covers every format with an Session: |
|
cc @alexey-milovidov @Avogar @vitlibar — could you take a look? The fix wires |
|
Workflow [PR], commit [513f02a] Summary: ❌
AI ReviewSummaryThis PR updates Final Verdict
|
Address review feedback on PR ClickHouse#104533: gate the new external schema reader fallback on `structure_hint.empty()` so the analyzer's hint (derived from destination-table columns in `INSERT ... SELECT FROM input()`) always wins. This is a defensive change — for the four formats this PR cares about (`LineAsString`, `RawBLOB`, `JSONAsString`, `JSONAsObject`) the analyzer never sets `structure_hint` because `FormatFactory::checkIfFormatSupportsSubsetOfColumns` returns false, so the hint path is unreachable in practice. But preserving the ordering keeps the change strictly additive: it can only improve queries that were already failing with `CANNOT_EXTRACT_TABLE_STRUCTURE`, never override a working analyzer hint. Refs ClickHouse#104533 (comment)
…xed_schema_format` The negative case for `CSV` previously relied on `grep` matching the expected error string in a pipeline. Under `set -e` without `pipefail` the pipeline's status is the status of `head`, so an unexpected successful `INSERT` would only have been caught by the reference diff. Capture the client's exit status explicitly: if the `INSERT` did not fail, the test now aborts immediately with a clear diagnostic. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
The remaining CI failures on this PR are unrelated to the change (which only touches
@groeneai, investigate the I have also pushed |
|
Thanks @alexey-milovidov — confirming the attributions and acknowledging the push. 1. 2. 3. I do not have access to Push acknowledgement: I see Session: cron:clickhouse-ci-task-worker:20260517-180000 |
LLVM Coverage Report
Changed lines: 100.00% (15/15) · Uncovered code |
Description
When the surrounding
INSERTquery specifies a format that has a fixed schema (LineAsString,RawBLOB,JSONAsString,JSONAsObject, ...), theinputtable function now derives its structure from that format. Previously the user had to repeat the structure on the table function itself, e.g.input('line String').The reported regression:
failed with
CANNOT_EXTRACT_TABLE_STRUCTUREeven thoughLineAsStringunambiguously declares a singleStringcolumn namedline. The existingstructure_hintmechanism is set by the analyzer from the destination table's columns, but it doesn't fire when theSELECThas constant projections in front of theinputreference, when destination column names don't line up with the format's column name, or when there is no insertion table at all.The fix consults
FormatFactory::getExternalSchemaReaderfrom insideTableFunctionInput::getActualTableStructure. Formats that register an external schema reader (LineAsString,RawBLOB,JSONAsString,JSONAsObject,Protobuf,CapnProto, etc.) declare a schema that is independent of the data, so it is the authoritative source forinputwhen no explicit structure was given. Formats without an external schema reader (CSV,JSONEachRow, ...) fall through to the existingstructure_hint/ "please provide structure manually" path and behave as before.For
Protobuf/CapnProto(which needformat_schema) the format's own readSchema error now surfaces directly to the user, e.g.The format Protobuf requires a schema. The corresponding setting should be set., which is more actionable than the previousprovide structure manuallymessage.Reproducible locally with the new stateless test
04215_input_fixed_schema_format.sh, which coversLineAsString,RawBLOB,JSONAsString, the original issue's exact shape, the negative case forCSV, and that the existinginput('my_col String')path still works unchanged.Closes #104532
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
The
inputtable function now infers its structure from the surroundingINSERTquery'sFORMATclause when that format has a fixed schema (LineAsString,RawBLOB,JSONAsString, etc.), so users no longer have to repeat the structure asinput('line String')for these formats. #104532.Documentation entry for user-facing changes
Version info
26.5.1.781