Skip to content

Refactor convertToFullIfNeeded to avoid issues with recursive LowCardinality stripping - #107773

Merged
Avogar merged 7 commits into
masterfrom
refactor-convertToFullIfWrapped
Jul 10, 2026
Merged

Avogar merged 7 commits into
masterfrom
refactor-convertToFullIfWrapped

Conversation

@Avogar

@Avogar Avogar commented Jun 17, 2026 •

Copy link
Copy Markdown
Member

convertToFullIfNeeded conflated two unrelated operations: stripping internal representation wrappers (Const/Sparse/Replicated) and stripping LowCardinality. The former is always safe — it normalizes column representation without changing the semantic type. The latter changes the type, requiring symmetric updates on the DataType side. Mixing both in one recursive method caused a trail of bugs with Variant/Dynamic columns (column/type mismatches when inner LowCardinality was stripped from columns but preserved in types) and led to workarounds in concat, format, concatWithSeparator, and KeyCondition.

The new convertToFullIfWrapped only strips Const/Replicated/Sparse — recursively through subcolumns — and never touches LowCardinality. Callers that also need LowCardinality removed chain ->convertToFullColumnIfLowCardinality() for top-level removal, or use recursiveRemoveLowCardinality for recursive removal.

This eliminates the need for the no-op overrides in ColumnVariant and ColumnDynamic (which existed solely to prevent recursive LC stripping), removes the manual four-call-chain workarounds in concat/format/concatWithSeparator and KeyCondition, and structurally prevents the class of bugs where convertToFullIfNeeded would bypass ColumnVariant's override when invoked on a Const-wrapped Variant.

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

Refactor convertToFullIfNeeded to avoid issues with recursive LowCardinality stripping.
Closes #107373
Closes #107598

Avogar and others added 2 commits June 17, 2026 18:27
…LowCardinality stripping

`convertToFullIfNeeded` conflated two unrelated operations: stripping internal
representation wrappers (Const/Sparse/Replicated) and stripping `LowCardinality`.
The former is always safe — it normalizes column representation without changing the
semantic type. The latter changes the type, requiring symmetric updates on the
`DataType` side. Mixing both in one recursive method caused a trail of bugs with
`Variant`/`Dynamic` columns (column/type mismatches when inner `LowCardinality` was
stripped from columns but preserved in types) and led to workarounds in `concat`,
`format`, `concatWithSeparator`, and `KeyCondition`.

The new `convertToFullIfWrapped` only strips Const/Replicated/Sparse — recursively
through subcolumns — and never touches `LowCardinality`. Callers that also need
`LowCardinality` removed chain `->convertToFullColumnIfLowCardinality()` for
top-level removal, or use `recursiveRemoveLowCardinality` for recursive removal.

This eliminates the need for the no-op overrides in `ColumnVariant` and
`ColumnDynamic` (which existed solely to prevent recursive LC stripping), removes
the manual four-call-chain workarounds in `concat`/`format`/`concatWithSeparator`
and `KeyCondition`, and structurally prevents the class of bugs where
`convertToFullIfNeeded` would bypass `ColumnVariant`'s override when invoked on
a `Const`-wrapped `Variant`.

Related: #107373

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jun 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ac168bc]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_msan, 6/8) FAIL
test_storage_rabbitmq/test.py::test_rabbitmq_mv_combo FAIL cidb, issue ISSUE EXISTS
Stress test (arm_release) FAIL
Logical error: 'Arguments of 'plus' have incorrect data types: 'acc' of type 'Int64', 'x' of type 'Int64'' (STID: 2735-474b) FAIL cidb, issue ISSUE CREATED

AI Review

Summary

This PR splits convertToFullIfNeeded into convertToFullIfWrapped plus explicit LowCardinality removal, updates the affected callers, and adds focused regression coverage for the Variant / Dynamic / Set / KeyCondition cases. The runtime change looks coherent and the Praktika report is clean, but several rewritten test comments still describe the old failure as if the new helper were stripping nested LowCardinality, which is the opposite of the API this PR introduces.

PR Metadata

Changelog category is reasonable for the user-visible LOGICAL_ERROR fixes. The Changelog entry is required for Bug Fix, but the current text is still implementation-centric and too vague for CHANGELOG.md; a user-readable replacement would be: Fixed exceptions in concat, format, IN, and index or partition pruning when Variant or Dynamic values contained nested LowCardinality types.

Findings

💡 Nits

  • [tests/queries/0_stateless/03987_concat_variant_low_cardinality.sql:2, tests/queries/0_stateless/03988_concat_with_separator_format_variant_low_cardinality.sql:2, tests/queries/0_stateless/03989_set_low_cardinality_in_tuple.sql:3, tests/queries/0_stateless/03990_set_dynamic_low_cardinality_join_runtime_filter.sql:3, tests/queries/0_stateless/03991_set_variant_low_cardinality_in_global_in.sql:3, tests/queries/0_stateless/04105_key_condition_nested_lc_variant.sql:7] These comments now say convertToFullIfWrapped recursively strips LowCardinality, but the whole point of the refactor is that it does not. Leaving them this way documents the opposite contract and will mislead the next person who debugs one of these regressions.
    Suggested fix: keep the root-cause prose tied to the old convertToFullIfNeeded behavior, or say that the new helper only unwraps Const / Replicated / Sparse while the explicit LowCardinality removal happens separately.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jun 17, 2026
Avogar added 2 commits June 25, 2026 17:33
…:ClickHouse/ClickHouse into refactor-convertToFullIfWrapped
@@ -1,5 +1,5 @@
-- Concat with Variant type containing LowCardinality caused a LOGICAL_ERROR
-- because convertToFullIfNeeded recursively stripped LowCardinality from inside the Variant column
-- because convertToFullIfWrapped recursively stripped LowCardinality from inside the Variant column

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This comment now attributes the old behavior to the new helper: convertToFullIfWrapped explicitly does not strip LowCardinality. The same mechanical rename appears in 03988_concat_with_separator_format_variant_low_cardinality.sql, 03989_set_low_cardinality_in_tuple.sql, 03990_set_dynamic_low_cardinality_join_runtime_filter.sql, 03991_set_variant_low_cardinality_in_global_in.sql, and 04105_key_condition_nested_lc_variant.sql.

Please keep the root-cause prose tied to the old convertToFullIfNeeded behavior, e.g. say the old helper recursively stripped nested LowCardinality while the new wrapper-only helper avoids that mismatch. Otherwise these regression tests document the opposite of the API contract introduced by the PR.

@clickhouse-gh

clickhouse-gh Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 80.00% 85.80% +5.80%
Functions 91.80% 92.70% +0.90%
Branches 71.90% 77.90% +6.00%

Changed lines: Changed C/C++ lines covered: 147/149 (98.66%) · Uncovered code

Full report · Diff report

@Avogar
Avogar added this pull request to the merge queue Jul 10, 2026
Merged via the queue into master with commit eb3d620 Jul 10, 2026
173 of 176 checks passed
@Avogar
Avogar deleted the refactor-convertToFullIfWrapped branch July 10, 2026 13:23
alexey-milovidov added a commit that referenced this pull request Jul 10, 2026
Pick up the merged fix #107773 (Avogar's `convertToFullIfNeeded` refactor,
commit eb3d620) which resolves the AST-fuzzer flake #107598 (`Bad cast
from type DB::ColumnString to DB::ColumnLowCardinality`, STID 4054-600e) that
was the sole functional red on this PR's CI. No code change to the PR itself.
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 10, 2026
alexey-milovidov added a commit that referenced this pull request Aug 29, 2026
Two differences against 26.3 had to be reconciled:
- 26.3 has no `should_materialize` filter, so the fix applies to its
  materialize-every-column loop;
- 26.3 has no `convertToFullIfWrapped`: that is master's post-rename name, from
  #107773, which also split `LowCardinality` stripping out of it. The predecessor
  `convertToFullIfNeeded` is present here and performs the same recursion into
  subcolumns, which is the actual fix - the previous explicit
  `convertToFullColumnIfReplicated`/`Const`/`Sparse` chain only converted the top
  level, so a sparse element inside a Tuple stayed sparse.

The outer `recursiveRemoveLowCardinality` is kept as it was on this branch.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
alexey-milovidov added a commit that referenced this pull request Aug 29, 2026
Same two differences as the 26.3 cherry-pick: 26.6 has no `should_materialize`
filter, so the fix applies to its materialize-every-column loop, and it has no
`convertToFullIfWrapped` - that is master's name after #107773. The predecessor
`convertToFullIfNeeded` is present here and does the same recursion into
subcolumns, which is the fix: the explicit
`convertToFullColumnIfReplicated`/`Const`/`Sparse` chain only converted the top
level, leaving a sparse element inside a Tuple sparse.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ToFullIfWrapped

Refactor `convertToFullIfNeeded` to avoid issues with recursive `LowCardinality` stripping
vdimir added a commit that referenced this pull request Nov 18, 2026
The change is taken as in the original (1a44640, #130895), with the adaptations below.

Adapted:
- `src/Formats/NativeReader.h`, `src/Formats/NativeReader.cpp`: the new `allowed_kinds_` parameter and the
  `allowed_kinds(allowed_kinds_)` initializer go after `block_missing_values_`, without the master-only
  `type_cache_` (`NativeReaderTypeCache`, #129831). The branch keeps its own
  `format_settings_ = std::nullopt` default.
- `src/Processors/Formats/Impl/NativeFormat.cpp` (`makeReader`): the `/*type_cache_=*/ nullptr,` argument is
  dropped. The file merged without a conflict, but would not compile as merged.
- `src/Server/TCPHandler.cpp`: the original only adds the `ISerialization::KindSet{ISerialization::Kind::DEFAULT}`
  argument; `getNativeWireFormatSettings` (#123649) is on its context lines and is not on the branch.
  `skipDataPacket` passes no format settings here, so an explicit `std::nullopt` stands in that position.
  `initBlockInput` keeps `getFormatSettings(state.query_context)`, with an explicit `nullptr` for
  `block_missing_values_` and without `type_cache_`. Both comments of the original are kept.
- `src/Interpreters/AsynchronousInsertQueue.cpp`: `IColumn::convertToFullIfWrapped` (#107773) is not on the
  branch, and `convertToFullIfNeeded` also strips `LowCardinality`. The check uses a file-local
  `hasWrappedColumn` instead: a `ColumnBLOB`, `ColumnConst`, `ColumnReplicated` or `ColumnSparse` at any
  nesting depth, `LowCardinality` left alone, nothing converted. The exception and its message are as in the
  original. The only producers of `pushQueryWithBlock` on the branch are the two calls in
  `TCPHandler::processAsyncInsertQuery`: a header without columns, and the squashed blocks that
  `initBlockInput` reads, which now accepts only the default kind.

Left out:
- `tests/queries/0_stateless/04616_replicated_serialization_oob_index_native_protocol.python` and `.reference`:
  the scenarios the branch does not have: invalid index type size, index row count mismatch, sparse over
  replicated (#114186) and huge element count (#129932). The rewrite of the two existing scenarios and the new
  `replicatedTupleElementScenario` and `replicatedOnInsertScenario` are kept, with their reference lines.
- `tests/queries/0_stateless/05473_sparse_truncated_values_native_protocol.python`, `.reference`, `.sh`: the
  original only edits this test, and it comes from #128363, which is not on the branch (modify/delete
  conflict).
- `docs/resources/develop-contribute/native-protocol/format.mdx`: the branch has no English specification of
  the `Native` format, so the hunks have nowhere to go (modify/delete conflict).

`tests/queries/0_stateless/helpers/fake_native_server.py` is as in the original here. It does not work with
the client of this branch, which sends a `Ping` before each query; the next commit adapts it.

Everything else merged without conflicts and has the same changed lines as the original.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Logical error: Bad cast from type A to B (STID: 4054-600e)

3 participants