Skip to content

Infer schema for input() table function from fixed-schema formats - #104533

Merged
alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-104532-input-fixed-schema
May 18, 2026
Merged

alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-104532-input-fixed-schema

Conversation

@groeneai

@groeneai groeneai commented May 10, 2026 •

Copy link
Copy Markdown
Collaborator

Description

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. Previously the user had to repeat the structure on the table function itself, e.g. input('line String').

The reported regression:

INSERT INTO logs SELECT '@system@' AS system, '$machine' AS machine,
                       '@repo@' AS repo, '@branch@' AS branch, line
FROM input()
FORMAT LineAsString

failed with CANNOT_EXTRACT_TABLE_STRUCTURE even though LineAsString unambiguously declares a single String column named line. The existing structure_hint mechanism is set by the analyzer from the destination table's columns, but it doesn't fire when the SELECT has constant projections in front of the input reference, 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::getExternalSchemaReader from inside TableFunctionInput::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 for input when no explicit structure was given. Formats without an external schema reader (CSV, JSONEachRow, ...) fall through to the existing structure_hint / "please provide structure manually" path and behave as before.

For Protobuf/CapnProto (which need format_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 previous provide structure manually message.

Reproducible locally with the new stateless test 04215_input_fixed_schema_format.sh, which covers LineAsString, RawBLOB, JSONAsString, the original issue's exact shape, the negative case for CSV, and that the existing input('my_col String') path still works unchanged.

Closes #104532

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):

The input table function now infers its structure from the surrounding INSERT query's FORMAT clause when that format has a fixed schema (LineAsString, RawBLOB, JSONAsString, etc.), so users no longer have to repeat the structure as input('line String') for these formats. #104532.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.781

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
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate (per fix-ci-failure skill):

a) Deterministic repro? Yes. INSERT INTO logs SELECT line FROM input() FORMAT LineAsString against the unmodified binary produces:

Code: 636. DB::Exception: Table function 'input' was used without structure argument but structure could not be determined automatically. Please, provide structure manually. (CANNOT_EXTRACT_TABLE_STRUCTURE)

With the fix it runs to completion and the rows arrive in the destination table.

b) Root cause explained? The input table function only had two paths for resolving its schema when called without an explicit structure: (1) the analyzer's structure_hint mechanism, populated from the destination table when SELECT projections can be matched to destination columns by name, and (2) an outright error. Fixed-schema formats — LineAsString, RawBLOB, JSONAsString, JSONAsObject, ... — declare their schema unambiguously via IExternalSchemaReader, but TableFunctionInput::getActualTableStructure never consulted the format. So as soon as the SELECT contained constant projections in front of the input reference (the issue's exact shape), or the destination column name diverged from the format's, the analyzer hint stayed empty and the user hit the generic "provide structure manually" error.

c) Fix matches root cause? The fix queries FormatFactory::getExternalSchemaReader(context->getInsertFormat()) from inside getActualTableStructure. This is the same mechanism readSchemaFromFormatImpl uses for fixed-schema formats — by definition, an external schema reader's output does not depend on the data, so it is the authoritative source for input whenever no explicit structure was given. Formats without an external schema reader (CSV, JSONEachRow, ...) fall through to the unchanged structure_hint / error path.

d) Test intent preserved? / New tests added? The new test 04215_input_fixed_schema_format.sh covers LineAsString (the issue's exact shape and a single-column variant), RawBLOB, JSONAsString, the negative CSV case (asserting the actionable error is preserved), and the legacy input('my_col String') path (asserting it still works). The existing 00952_input_function.sh and 02915_input_table_function_in_subquery.sh paths were re-checked manually and are unchanged.

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 IExternalSchemaReader. For Protobuf / CapnProto, which require format_schema, the format's own error (The format Protobuf requires a schema. The corresponding setting should be set.) now surfaces directly instead of the previous generic "provide structure manually" — a small bonus improvement.

Session: cron:clickhouse-ci-task-worker:20260510-151500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov @Avogar @vitlibar — could you take a look? The fix wires TableFunctionInput::getActualTableStructure up to FormatFactory::getExternalSchemaReader so that fixed-schema formats (LineAsString, RawBLOB, JSONAsString, ...) supply the structure when the user calls input() without arguments. Closes #104532.

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label May 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [513f02a]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) FAIL
01710_projection_additional_filters FAIL cidb
02345_implicit_transaction NOT_FAILED cidb
02435_rollback_cancelled_queries NOT_FAILED cidb

AI Review

Summary

This PR updates input() schema inference for INSERT ... SELECT ... FROM input() so that when no explicit structure is provided and the FORMAT has an external schema reader (for example LineAsString, RawBLOB, JSONAsString), input() derives its structure from the format instead of failing with CANNOT_EXTRACT_TABLE_STRUCTURE. I reviewed the current diff and prior discussion threads against the current code and found no unresolved correctness, safety, compatibility, or evidence gaps.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 10, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this May 10, 2026
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]>
@alexey-milovidov

Copy link
Copy Markdown
Member

The remaining CI failures on this PR are unrelated to the change (which only touches src/TableFunctions/TableFunctionInput.cpp and adds 04215_input_fixed_schema_format.sh):

  • Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) → 01710_projection_additional_filters — failing widely on master (15–26 PRs/day in the last two weeks). Tracked at Test 01710_projection_additional_filters is flaky #104219.
  • Unit tests (asan_ubsan, function_prop_fuzzer) → FunctionsStress.stress / AllTests — roundDown determinism fuzz failure, also widely failing on master. Tracked at FunctionsStress.stress #104877.
  • CH Inc sync → Build (arm_tidy) — fails at src/Processors/QueryPlan/ReadFromMergeTree.cpp:4627: error: nested redundant #if; consider removing it in the clickhouse-private build. The cited line does not exist in the public ReadFromMergeTree.cpp (4509 lines), so this is a private-master issue that cannot be addressed from this PR.

@groeneai, investigate the Build (arm_tidy) failure: https://github.com/ClickHouse/clickhouse-private/actions/runs/25950584577/job/76287678534 and provide a fix in a separate PR. If the fix is already in progress, link it here.

I have also pushed 513f02a8006 to address the latest review thread on 04215_input_fixed_schema_format.sh: the negative case for CSV now captures the client exit status explicitly so an unexpectedly successful INSERT fails the test loudly rather than relying on the reference diff catching an empty line.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks @alexey-milovidov — confirming the attributions and acknowledging the push.

1. Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel) → 01710_projection_additional_filters
Confirmed unrelated. Tracked under #104219 ("Flaky test 01710_projection_additional_filters"), with the active fix PR #104296 by @azat ("Fix applying additional_table_filters with parallel replicas and analyzer", MERGEABLE/BLOCKED). The regression originated from PR #102921 (merged 2026-05-04). I'm monitoring #104296 for merge via CIDB.

2. Unit tests (asan_ubsan, function_prop_fuzzer) → FunctionsStress.stress / AllTests
Confirmed unrelated. Same function_prop_fuzzer non-determinism family as PR #105101 — roundDown on this PR maps to the shapes umbrella'd at #104877 (FunctionsStress.stress, OPEN, labels testing/fuzz) and the auto-rolled #105040 (AllTests). Sibling shapes have been fixed piecewise (#104804, #104858, #104989, #104992, #105147); the roundDown shape is not yet addressed but belongs in the same umbrella.

3. CH Inc sync → Build (arm_tidy) → src/Processors/QueryPlan/ReadFromMergeTree.cpp:4627
Confirmed not actionable from this PR. The public src/Processors/QueryPlan/ReadFromMergeTree.cpp is 4530 lines and contains only two non-nested #if USE_USEARCH blocks (lines 2084 and 2184) — neither is flagged. The failing lines (4627 and the :4707 companion that fires on PR #105101) lie in private-only code in ClickHouse/clickhouse-private#58151, beyond the end of the public file. I've already noted this exact nested redundant #if pattern on PR #105101 at #105101 (comment) — same private lines, same diagnostic.

I do not have access to ClickHouse/clickhouse-private, so I cannot file a fix PR in that repo or read the surrounding code to identify which #if to remove. The fix itself is mechanical (readability-redundant-preprocessor: drop the inner #if whose condition is implied by the enclosing one), but it needs a contributor with private-repo access. If you can paste the offending block or the diff between public and private around lines 4627 / 4707, I'm happy to point at the exact lines to drop — but I can't propose the change directly.

Push acknowledgement: I see 513f02a8006 ("Address review: assert INSERT failure explicitly in 04215_input_fixed_schema_format") — captures the client exit status explicitly so a silently-successful INSERT aborts the test loudly instead of relying on the reference diff. That matches the bot's set -o pipefail request from 2026-05-16T02:26:13Z and is strictly stronger than the original grep | head pipeline. CI is re-running against this commit now; I'm monitoring.

Session: cron:clickhouse-ci-task-worker:20260517-180000

@clickhouse-gh

clickhouse-gh Bot commented May 17, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.20% +0.10%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 100.00% (15/15) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov merged commit f9e38cb into ClickHouse:master May 18, 2026
164 of 167 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 18, 2026
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 pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Schema inference from fixed-schema formats is not applied for table function input

3 participants