Skip to content

convertColumnToType: tag-faithful Bool; migrate values() off Field - #112308

Merged
yakov-olkhovskiy merged 7 commits into
masterfrom
valueref-convertcolumn-tag-faithful
Aug 1, 2026
Merged

yakov-olkhovskiy merged 7 commits into
masterfrom
valueref-convertcolumn-tag-faithful

Conversation

@yakov-olkhovskiy

@yakov-olkhovskiy yakov-olkhovskiy commented Jul 28, 2026 •

Copy link
Copy Markdown
Member

Two related steps in removing DB::Field from constant-expression handling:

  1. Make convertColumnToType tag-faithful for Bool. Its Field fallback reconstructs a value
    with IColumn::get, which does not round-trip the Bool tag (a DataTypeBool column is a plain
    ColumnUInt8, so get yields UInt64). convertFieldToType keys on that tag, so Bool -> String
    returned '1'/'0' instead of 'true'/'false' (and likewise for nested Array(Bool) /
    Tuple(Bool) / Map(..., Bool)). The fallback now re-tags Bool values (recursing through
    Array/Tuple/Map, unwrapping Nullable/LowCardinality) before delegating. Scope: faithful for
    Bool under the structural carriers, not for Bool nested under Variant/Dynamic/JSON
    (ColumnVariant::get erases the active alternative - the legacy Field path has the same
    limitation). The convertColumnToTypeOrThrow error path reproduces convertFieldToType's diagnostic.

  2. Migrate the values table function off Field. TableFunctionValues now evaluates its
    arguments with evaluateConstantExpressionAsColumn and coerces them with convertColumnToTypeOrThrow
    (per element for the tuple path), inserting the converted columns directly instead of materializing a
    Field per value.

One user-visible behavior change (see the new stateless test): a non-literal Bool-typed constant
(wrapped via CAST/Nullable/LowCardinality) inserted into a textual values column now renders as
'true'/'false' instead of '1'/'0'. Example: SELECT * FROM values('x String', CAST(true, 'Nullable(Bool)')) returns true (was 1). Direct Bool literals were already 'true' and are
unchanged; a computed comparison such as 1 > 0 is UInt8 (not Bool) and is also unchanged. The old
'1'/'0' was an inconsistency (direct literals and CAST(... AS String) already produce
'true'/'false'), so this is a deliberate fix, not preserved.

Related: #110945

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

Fixed the values table function rendering a non-literal Bool constant (e.g. CAST(true, 'Nullable(Bool)')) inserted into a String/textual column as 1/0 instead of true/false.

Version info

  • Merged into: 26.8.1.570 (included in 26.8 and later)

`convertColumnToTypeOrNull`'s `Field` fallback does `IColumn::get(0, field)` to reconstruct a value and
then delegates to `convertFieldToType`. `get` reconstructs the `Field` using the storage column's
`NearestFieldType`, which does not round-trip the `Bool` tag: a `DataTypeBool` column is a plain
`ColumnUInt8`, so `get` yields a `UInt64` `Field`. `convertFieldToType` keys on that tag, so
`Bool -> String` produced '1'/'0' instead of 'true'/'false' (and likewise for nested `Array(Bool)` /
`Tuple(Bool)` / `Map(..., Bool)`), diverging from `convertFieldToType` even though the helper is
documented and tested as its column twin.

Re-tag `Bool` values in the reconstructed field (recursing through `Array`/`Tuple`/`Map`, unwrapping
`Nullable`/`LowCardinality`) before delegating, so the conversion is faithful for tag-sensitive targets
too. `Bool` is the only built-in type whose column `get` loses its `Field` tag - IPv4/IPv6/UUID/Decimal
have dedicated columns and `Field` types - confirmed by extending the differential test with a matrix of
`-> String` conversions (Bool/nested-Bool now pass; Enum/Date/Decimal/IPv4 were already faithful).

No behavior change for current callers (all convert to numeric targets, which are value-preserving
regardless of the tag). This unblocks migrating textual-conversion callers of the legacy `Field`-returning
`evaluateConstantExpression` to the column API.

Part of removing DB::Field.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [91da3b7]

Summary: ❌

job_name test_name status info comment
Bugfix validation (unit tests) ERROR
Bugfix validation (unit tests) ERROR

AI Review

Summary

This PR makes convertColumnToType preserve Bool tags for tag-sensitive textual conversions under scalar and structural carriers, migrates values to the column API, and adds focused regression coverage for the intended values output change. I did not find remaining correctness, compatibility, or test-coverage issues in the current diff; the earlier review points about Variant scope, exceptional-path diagnostics, and the user-visible wrapped-Bool behavior change are all addressed in the current code and tests.

Final Verdict

✅ No new findings.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 28, 2026
yakov-olkhovskiy and others added 2 commits July 28, 2026 23:23
A review noted `ColumnVariant::get` erases a `Bool` alternative to the nested column's `UInt64` field,
and for an ambiguous variant (e.g. `Variant(Bool, UInt8)`) the reconstructed `Field` no longer records
which alternative was active - so `retagBoolInField` cannot recover it structurally the way it does for
`Array`/`Tuple`/`Map`. Rather than add a `ColumnVariant`-aware path (which would also pull in
`Dynamic`/`JSON`) for a case no caller needs, document the limitation honestly in the header and the
helper comment: faithful for `Bool` under the structural carriers, not for `Bool` nested under
`Variant`. Note that the legacy `Field` path (`convertFieldToType` on `(*column)[0]`) has the exact
same limitation, so migrating a caller to this helper does not change that behavior.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
`TableFunctionValues` evaluated each argument with the legacy `Field`-returning
`evaluateConstantExpression` and coerced it with `convertFieldToTypeOrThrow`, materializing a `Field`
per value. Switch it to `evaluateConstantExpressionAsColumn` + `convertColumnToTypeOrThrow`, inserting
the converted size-1 column directly - no `Field`. The tuple (multi-column) path converts each tuple
element independently (same per-element semantics and error behavior as before), and structure
inference (`getTypesFromArgument`) now only reads the type. This is the first textual-conversion caller
moved to the now-tag-faithful column coercion; `values('x String', true)` returns 'true' via the column
path (previously via the `Field`-API literal shim).

Behavior is unchanged: verified against the existing `values` tests and a new stateless test covering
`Bool` -> textual as a single value, inside a tuple row, and inside an array column.

Part of removing DB::Field.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@yakov-olkhovskiy yakov-olkhovskiy changed the title Make convertColumnToType tag-faithful for Bool (and nested Bool) convertColumnToType: tag-faithful Bool; migrate values() off Field Jul 28, 2026
A review noted that on a not-representable value (e.g. `values('x UInt8', 256)`)
`convertColumnToTypeOrThrow` threw a generic "Value in column of type UInt64 cannot be represented as
UInt8", dropping the offending literal that the previous `convertFieldToTypeOrThrow` reported ("Cannot
convert value '256' from ...") - which also made it hard to tell which element of a multi-column row
failed.

On the exceptional (not-representable) path, reconstruct the `Field` and delegate to
`convertFieldToTypeOrThrow` to reproduce that diagnostic exactly, with a defensive generic throw as an
unreachable guard. The `Field` is materialized only on this error path; the happy path in
`convertColumnToTypeOrNull` stays `Field`-free. This rides the fallback's existing `convertFieldToType`
delegation and is removed together with it when that fallback becomes column-native (throwing
conversions such as a bad string->number parse are unaffected - they already propagate their own error).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Migrating `values` to the column API changes one user-visible behavior: a non-literal `Bool`-typed
constant (wrapped in `CAST` / `Nullable` / `LowCardinality`) converted to a textual column now renders
as 'true'/'false' instead of '1'/'0'. Previously such non-literal constants went through
`evaluateConstantExpression` + `convertFieldToType`, which materialized `(*result_column)[0]` and
collapsed the `Bool` tag to `UInt64`, so `String`-formatting produced '1'/'0'. The column-native path
keeps the `Bool` type, which is consistent with direct `Bool` literals and with `CAST(... AS String)`.

This is a deliberate fix, not a regression - the old '1'/'0' was an inconsistency, so it is not
preserved. Pin the corrected behavior with an end-to-end regression, and note that a computed
comparison (`1 > 0`) is `UInt8` rather than `Bool` and is therefore unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Jul 29, 2026
yakov-olkhovskiy and others added 2 commits July 29, 2026 17:00
The merge with master brought several `04652_*` stateless tests, colliding with the number this test
was originally assigned. Renumber it to the next free number to keep prefixes unique. Content unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@yariks5s yariks5s self-assigned this Jul 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.40% 86.40% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.50% 78.50% +0.00%

Changed lines: Changed C/C++ lines covered: 79/80 (98.75%) · Uncovered code

Full report · Diff report

@yariks5s yariks5s 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, but seems like we do not completely remove Field here, we do it in convertColumnToType.cpp:153

for (auto & entry : field.safeGet<Map>())
{
auto & key_and_value = entry.safeGet<Tuple>();
if (key_and_value.size() == 2)

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.

Nit: looks like for Map this check is redundant and we can do assert here instead. But up to you, I don't see this as a problem

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

it doesn't matter - this function is temporary to keep everything consistent during transition - will be removed at the end

@yakov-olkhovskiy
yakov-olkhovskiy added this pull request to the merge queue Aug 1, 2026
Merged via the queue into master with commit 09da4d1 Aug 1, 2026
696 of 701 checks passed
@yakov-olkhovskiy
yakov-olkhovskiy deleted the valueref-convertcolumn-tag-faithful branch August 1, 2026 03:40
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 1, 2026
yakov-olkhovskiy added a commit that referenced this pull request Aug 3, 2026
Migrate the constant `IN` / set-building path off `DB::Field`.
`getSetElementsForConstantValue` now takes the right-hand side as a
`ColumnPtr` instead of a `Field`, and `createBlockFromCollection` iterates the
collection's member columns (`Array` elements as rows, `Tuple` elements as
subcolumns) instead of `Array`/`Tuple` of `Field`. Per-element conversion goes
through `convertColumnToType` (strict) via a new `convertColumnToTypeCheckEnum`,
the column twin of `convertFieldToTypeCheckEnum`; the not-representable vs
genuine-NULL distinction uses `convertColumnToType`'s null channel (`{}` vs a
size-1 NULL column) instead of inspecting `Field::isNull`. The dispatch
(`transform_null_in`, `Nullable(Tuple)` dual interpretation, tuple-vs-set
disambiguation) is preserved, with `Field` value inspections replaced by
column-native ones (`isNullAt`, homogeneous element-type checks).

Callers:
- analyzer (`CollectSets`, `resolveFunction`) pass `ConstantNode::getColumn`
  instead of `ConstantNode::getValue` (no `Field` materialization);
- legacy `ActionsVisitor` evaluates set elements with
  `evaluateConstantExpressionAsColumn` and assembles the collection column
  directly (`buildCollectionColumnAndTypeFromASTFunction`).

Because the set is now built with `convertColumnToType` (tag-faithful for
`Bool` since #112308), a `Bool` set element converted to a textual left-hand
type is now the canonical `'true'`/`'false'` rather than the numeric collapse
`'1'`/`'0'` produced by the old `Field` path (`IColumn::get` erased the `Bool`
tag to `UInt64` before `convertFieldToType`). This is an incidental fix, kept
rather than pinned, on both the analyzer and legacy paths; regression test
`04692_bool_tag_faithful_in_set` covers it. This matches the `values` table
function and `CAST` behavior.

Co-Authored-By: Claude Opus 4.8 <[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.

3 participants