Skip to content

Do not refuse to attach a table because of an implicit min-max index that cannot be built - #125861

Merged
Algunenano merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-implicit-minmax-index-unbuildable-attach
Oct 20, 2026
Merged

Algunenano merged 4 commits into
ClickHouse:masterfrom
groeneai:fix-implicit-minmax-index-unbuildable-attach

Conversation

@groeneai

@groeneai groeneai commented Oct 14, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #118527
Related: #121140
Related: #123926

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

A MergeTree table with add_minmax_index_for_string_columns (or the numeric / temporal variant) and an ALIAS column that calls dictGet, joinGet or an executable user-defined function can now be loaded when that dictionary, Join table (or its database) or function is missing, as an executable function is at every startup. A table written before 25.12 whose definition or Keeper metadata lists its implicit indices loads too, also with such an index over an ALIAS expression, which failed with Index with name ... already exists.

Description

On 26.3.42.3 and 26.8.18.2, a MergeTree table with add_minmax_index_for_string_columns and an ALIAS column that calls dictGet cannot be attached once that dictionary is gone, so with async_load_databases = 0 the server does not start. Found by Stress test (arm_release) (report): the fuzzer cloned a table of 05325_analyzer_lazy_alias_columns with that setting before the test dropped the dictionary.

addImplicitIndicesForColumn built the index outside the try that skips one failing validation; this PR moves it inside and adds UNKNOWN_TABLE, UNKNOWN_DATABASE and UNKNOWN_FUNCTION. An executable function is registered after tables load, so its index is skipped at startup.

Before 25.12, implicit indices were stored in the definition (after any ALTER) and in Keeper, and loading analysed them as regular ones. A stored index identical to the generated one is now skipped unanalysed, locally and in Keeper, and generated as for a new table. It also loads a 25.8 table with one over a ALIAS id * 2 (26.3/26.8/26.9 fail: Index with name auto_minmax_index_a already exists) and drops one over joinGet, which CREATE never builds. Another reserved-name index (GRANULARITY 8) stays regular: re-marked implicit, it was dropped by an ALTER and regenerated with granularity 1 (Cannot read all marks). A manual ATTACH of such a definition still analyses it in InterpreterCreateQuery. My open #123938 conflicts only textually.

Validation: 05397 fails before and passes 30/30 randomized runs after; the integration cases (a table created and altered on 25.10, a reserved-name index on a replica, executable functions across a restart) fail before, as does a mutant of each change.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/125861]

Version info

  • Merged into: 26.11.1.298-master (included in 26.11 and later)

groeneai and others added 3 commits October 14, 2026 17:58
…that cannot be built

`StorageInMemoryMetadata::addImplicitIndicesForColumn` already skips an
implicit index that fails `MergeTreeIndexFactory::validate` with
BAD_ARGUMENTS / INCORRECT_QUERY, but it built the index outside that try.
Building it analyses the column's ALIAS expression, so an ALIAS that calls
`dictGet` on a dictionary that no longer exists (dropped with
`check_table_dependencies = 0`, or removed from the configuration) threw
BAD_ARGUMENTS from ATTACH. The table could not be loaded and, with
`async_load_databases = 0`, the server did not start. Found by the Stress
test: the AST fuzzer cloned 05325's `t_alias_dict` with
`add_minmax_index_for_string_columns = 1` before the test dropped the
dictionary.

Move the construction into the existing try and keep the error filter. An
implicit index over `dictGet` is never kept when the dictionary exists
either (`dictGet` is non-deterministic and validation rejects it), so ATTACH
now yields the same indices CREATE did.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…able or function

Building the implicit index analyses the ALIAS expression. Besides a
missing dictionary (BAD_ARGUMENTS), `joinGet` on a `Join` table dropped
with `check_table_dependencies = 0` throws UNKNOWN_TABLE and an
executable user-defined function that is not registered throws
UNKNOWN_FUNCTION, and both made the table unattachable. Add these two
codes to the filter of `addImplicitIndicesForColumn`.

Executable functions are registered only after the table metadata is
loaded at startup, so this also covers a deterministic executable
function that is still configured: the table used to fail to load at
every restart, now it loads without that implicit index.

Build, validate and push the index inside the `try`, which removes the
staging `IndexDescription` and its extra deep copy.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…f a dropped database

`joinGet` keeps the database qualifier of its `Join` table, so after `DROP DATABASE ... SETTINGS
check_table_dependencies = 0` the analysis of the ALIAS fails with UNKNOWN_DATABASE before the table is
looked up, which the filter did not accept: the dependent table stayed unattachable. Add UNKNOWN_DATABASE.

Tests: a cross-database `joinGet` arm in 05397, and a `test_executable_user_defined_function` case that
pins UNKNOWN_FUNCTION: executable functions are registered only after the tables are loaded, so a
deterministic executable function in an ALIAS with a string implicit index made the table fail to load
at every restart (with `async_load_databases = 0` the server did not start).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex) plus my own cold review; three rounds (heads 4b398a741d0b6, 75112e5409149, 760499d47d22c). The last round found nothing.

# Sev Finding Verdict Evidence / action
1 ⚠️ Other "referenced object is gone" codes (joinGet on a dropped Join table: UNKNOWN_TABLE; a removed executable function: UNKNOWN_FUNCTION) still make the table unattachable (StorageInMemoryMetadata.cpp, catch filter) AGREE fixed after round 1
2 ⚠️ The a/b + escape_index_filenames = 0 CREATE arm is not in the test DISAGREE same moved statement, same catch, same code as the ATTACH arm the test pins (fails on master and on the mutant); the CREATE change is measured both ways and stated in the description
3 💡 Default construction + assignment adds a deep copy of the index (no move operations) AGREE fixed after round 1
4 💡 Mention #123938, which edits the same block AGREE added to the description
5 ❌ A Join table gone with its database (joinGet('db2.j', ...) after DROP DATABASE db2) raises UNKNOWN_DATABASE from the same DatabaseCatalog::getTable lookup, still rethrown AGREE fixed after round 2 (filter + cross-database arm in the test)
6 ⚠️ No test exercises the UNKNOWN_FUNCTION code AGREE fixed after round 2 (integration case: deterministic executable function in an ALIAS, restart)
7 💡 A test comment describes an index-less part that the test never creates AGREE fixed after round 2

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. After the dictionary is dropped, ATTACH of a table with add_minmax_index_for_string_columns = 1 and ALIAS dictGet(...) fails with Code 36 every run on master, 26.3.42.3 and 26.8.18.2 (joinGet: Code 60 / 81; executable function, restart with async_load_databases = 0: Code 46).
b Root cause explained? addImplicitIndicesForColumn built the implicit index outside its best-effort try; building it analyses the alias expression, which throws when the dictionary, Join table (or its database) or function it calls is missing (executable functions are registered only after tables load).
c Fix matches root cause? Yes. The construction moves into the existing try, and the filter also accepts UNKNOWN_TABLE, UNKNOWN_DATABASE and UNKNOWN_FUNCTION, the codes measured for those cases. No test is muted; 05325 is unchanged.
d Test intent preserved / new tests added? New 05397_implicit_minmax_index_alias_unresolvable_attach (String / UInt64 / Date dictGet, joinGet in the same and in a dropped database) and a new test_executable_user_defined_function restart case; 25 existing implicit-index and ALIAS tests pass unchanged.
e Both directions demonstrated? Yes, on Build-ID-verified binaries: 05397 fails on master and on three mutants (construction outside the try, no UNKNOWN_TABLE, no UNKNOWN_DATABASE) and passes 50/50 randomized plus 10/10 Replicated; the integration case fails on master and without UNKNOWN_FUNCTION, passes 3/3.
f Fix is general across code paths? Every implicit-index path (CREATE, ATTACH/startup, ALTER, MODIFY/RESET SETTING, projections, replica metadata update) goes through this one function; explicit indices stay strict.
g Fix generalizes across inputs (params/datatypes/wrappers)? String, numeric and temporal implicit indices, DDL and config dictionaries, Join tables in the same and a dropped database, executable functions, and a Replicated database are covered.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or metadata change; implicit indices are recomputed on every load and never persisted. A statement that failed only while building an implicit index now succeeds without it.
i Invariants and contracts preserved? An implicit index that cannot be built or validated is not generated; other error codes (including memory limits) and non-ClickHouse exceptions still propagate, and the index is pushed only once fully built and validated.

Session id: cron:clickhouse-impl-slot-5:20261014-192700

@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f8cbc53]

Summary: ✅


AI Review

Summary

Reviewed f8cbc530ec28efd4376acff3c1e34e1ed5e890e8. The change lets MergeTree tables load when an implicit min-max index cannot resolve an ALIAS dependency, including implicit definitions persisted before 25.12. The earlier finding about local and Keeper metadata loading is addressed and its thread is resolved. No remaining actionable findings.

Missing context / blind spots
  • ⚠️ The new stateless regression passed in Fast test. Targeted integration CI was still pending; upgrade and executable-function restart validation relies on the author's recorded runs. Completed integration results would independently confirm those paths.
Final Verdict

Status: ✅ Approve.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @Algunenano @shankar-iyer, could you review this and add can be tested (I cannot set labels right now)? addImplicitIndicesForColumn built an implicit min-max index outside its best-effort try, so an ALIAS calling dictGet, joinGet or an executable function whose dictionary, Join table or function is missing made the table unattachable and, with async_load_databases = 0, stopped the server from starting; the construction now runs inside the try, which also skips UNKNOWN_TABLE, UNKNOWN_DATABASE and UNKNOWN_FUNCTION.

@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Oct 14, 2026
@Algunenano Algunenano self-assigned this Oct 14, 2026
@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label Oct 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f8cbc530e with master 324e8a3a1 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 19ee68634; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

9 translation units recompiled, 32 s compile time in total, 9 of them have a recent master baseline.

Job report

…instead of reading them

Versions before 25.12 serialized implicit min-max indices into the table definition
(after any ALTER) and into Keeper metadata. The loader analyzed such a stored index as a
regular one and then re-marked every index with the reserved name as implicit, so:

- once the dictionary, `Join` table or function used by an ALIAS was gone (or, for an
  executable function, not yet registered at startup), the table could not be loaded,
  and a replica stayed read-only because `parseAndNormalize` analyzed the stored
  definition too;
- for an ALIAS expression column (`b ALIAS key * 2`), `addImplicitIndicesForColumn` did not
  recognize the stored index (its `column_names` holds the columns of the expression) and
  generated it a second time, so loading failed with
  `Index with name auto_minmax_index_b already exists` even with every object present
  (measured on 25.8, 26.3, 26.8 and 26.9 with a table written and altered by 25.8);
- a stored implicit index over a non-deterministic `joinGet` / `dictGet` ALIAS was kept,
  although such an index is never generated;
- an index with the reserved name but another shape (e.g. `GRANULARITY 8` from a
  full-definition `ATTACH`) was re-marked implicit, so the next ALTER dropped it from the
  definition and the next load generated it with granularity 1, after which reading it
  failed with `Cannot read all marks`.

Now a stored index identical to the one the generator builds is skipped without analysis,
in `registerStorageMergeTree` (it is generated below exactly as for a table created now)
and in `parseAndNormalize` (stripped from Keeper metadata); any other index is a regular
index on both sides.

The upgrade test creates and alters the table on 25.10; the executable function case
attaches the stored form of the definition.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@Algunenano Algunenano 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.

@groeneai In the future, unless they are about the same code, please prefer opening 2 PRs. Having one PR fixing several different bugs makes it harder to review and backport if necessary.

Also, please avoid adding backwards compatibility code for versions that aren't supported anymore unless they are critical and reported by real scenarios. Why do we need to workaround a tiny corner case from versions prior to 25.10 when the oldest supported release is 26.3 and nobody has complained about it?

This PR could have been 2 much simpler ones 😉

@Algunenano
Algunenano added this pull request to the merge queue Oct 20, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks, noted for future PRs: one bug per PR unless the fixes touch the same code, and no compatibility code for unsupported versions unless the case is critical and reported.

Nobody reported the legacy case. It came from an automated review comment on this PR (a table created and ALTERed on an old release keeps a serialized auto_minmax_index_* definition, which then fails to load once the referenced object is gone). I reproduced it and fixed it here instead of declining it; it should have stayed out of this PR.

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 comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. 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.

3 participants