Skip to content

Resolve an omitted Buffer structure only in the register method - #114203

Closed
groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:buffer-resolve-omitted-structure-in-register-method
Closed

groeneai wants to merge 2 commits into
ClickHouse:masterfrom
groeneai:buffer-resolve-omitted-structure-in-register-method

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Related: #113372
Related: #113220

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

Description

Follow-up to #113372, asked for in #113372 (comment).

#113372 moved the inference of an omitted Buffer structure into registerStorageBuffer, where there
is a user to check SHOW COLUMNS against, but moved only the branch it needed and left the old
fallback in the constructor for the callers that arrive with no user. This drops that copy: the
register branch now covers the stored-metadata callers too, and the constructor is a plain
setColumns(columns_).

The fallback could not simply be deleted. DatabaseOnDisk::createTableFromAST passes empty columns for
any engine with supports_schema_inference, and metadata written before the inferred column list
started being persisted still reloads that way. That branch takes no access check, since those callers
have no user, and resolves with args.getContext(), the same context the constructor used.

No behaviour change, with one exception: Buffer('', '') without a column list now reports
EMPTY_LIST_OF_COLUMNS_PASSED rather than UNKNOWN_TABLE: StorageID is empty. Both reject it. Such a
table cannot exist in stored metadata, since that CREATE has always been rejected.

Two tests: 04843_buffer_empty_destination_omitted_columns pins the error above, and the loading
branch, which had no coverage, gets an integration test. That one edits on-disk metadata, which per
#111922 must not happen in 0_stateless/. 04729_buffer_infer_columns_access and
03631_buffer_access_check pass unchanged.

Distributed has the same surviving fallback but is left out: that ask is on @ PedroTadim's thread in
#113220, and getStructureOfRemoteTable over a cluster is a much larger change.

Requested in ClickHouse#113372 (comment)
as a follow-up to ClickHouse#113372.

ClickHouse#113372 moved the inference of an omitted Buffer structure into registerStorageBuffer,
which is the only scope holding a user to check SHOW_COLUMNS against: StorageBuffer keeps
only a long-lived context_->getBufferContext() and structurally cannot do a user-scoped
check in its constructor. It moved just the branch it needed, though, and left the old
fallback in the constructor for the callers that arrive with no user, guarding the new site
with !(isLoadingFromExistingMetadata(mode) || attach_short_syntax) so the two branches
partition the callers instead of overlapping. The result was the same "read the
destination's columns" logic in two places, with two contexts and two access postures.

The constructor copy could not simply be deleted. DatabaseOnDisk::createTableFromAST passes
empty columns for any engine with supports_schema_inference, and metadata written before
the inferred column list started being persisted still reloads that way, so the register
method needed a branch covering it. That branch takes no access check, since those callers
have no user at all, and resolves with args.getContext(), which is the same context the
constructor used: getLocalContext() is a different object on both stored-metadata paths
(a copy of global under TablesLoader, the user's query context under short ATTACH), so the
context has to be selected per branch rather than uniformly, or the metadata read would
silently move to a different context on paths the request never mentioned.

Buffer('', '') with no column list now reports EMPTY_LIST_OF_COLUMNS_PASSED from
StorageInMemoryMetadata::setColumns instead of UNKNOWN_TABLE "StorageID is empty" from the
constructor calling getTable on an empty StorageID. Both reject the statement.

The loading branch had no test. The new integration test strips the column list from the
persisted definition to produce the older shape, so it cannot be a stateless test (ClickHouse#111922),
and it fails against a build with the constructor fallback deleted and the register branch
left un-widened.

Distributed has the identical surviving fallback and is left for a separate change:
getStructureOfRemoteTable over a cluster, with its macro expansion and table-function
handling, is materially larger than Buffer's local metadata read.
Buffer('', '') with an omitted column list has nothing to infer a structure from. Before
the constructor's fallback was removed it reached DatabaseCatalog::getTable with an empty
StorageID and reported UNKNOWN_TABLE "Cannot find table: StorageID is empty"; now the
constructor always calls setColumns, which rejects the empty structure with
EMPTY_LIST_OF_COLUMNS_PASSED. Both reject the statement, and nothing in the tree asserted
either error, so a later change could move it again unnoticed.

The test fails on master with the old UNKNOWN_TABLE and passes here. Its second half
creates the same destination-less engine with an explicit structure and reads a row back,
so a pass cannot come from the statement being rejected before it reaches the engine.

It needs no tags: it only touches tables inside the per-test database, so copies of it run
concurrently with each other.
@groeneai

Copy link
Copy Markdown
Collaborator Author

One thing noted during review and left as is: the docstring of the new integration test says the
column-less metadata shape comes from "before 22.x". The precise window is 22.1 and 22.2 -- Buffer
gained schema inference in 22.1 (8112a7123362d82, which also added the constructor fallback this
PR removes), and the inferred column list started being persisted in 22.3 (d45b6ae03c8c183).
Before 22.1 the shape could not occur, since omitting the column list was not accepted at all.

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @novikd @pufit, could you review this? It removes the duplicated omitted-structure resolve from the StorageBuffer constructor by widening the register-method branch to cover the callers that arrive from stored metadata, selecting the context per branch so every entry point keeps the one it used before, and leaving the SHOW COLUMNS check on exactly the user-facing paths that had it.

@PedroTadim

Copy link
Copy Markdown
Member

I didn't ask you to do this

@PedroTadim PedroTadim closed this Aug 10, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

azat asked for this on the merged PR, inline: #113372 (comment) ("let's move resolving from the ctor into register method as well"). I acked on that thread before starting and posted the link back there when it was open.

No objection to the close. Do you want to take it, or should I reopen?

@groeneai groeneai added the groeneai-origin-request PR origin: a maintainer pinged or directed groeneai label Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

groeneai-origin-request PR origin: a maintainer pinged or directed groeneai

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants