Skip to content

Add evaluateConstantExpressionAsColumn to avoid Field materialization - #110944

Closed
yakov-olkhovskiy wants to merge 2 commits into
masterfrom
cursor/field-eval-const-to-column
Closed

yakov-olkhovskiy wants to merge 2 commits into
masterfrom
cursor/field-eval-const-to-column

Conversation

@yakov-olkhovskiy

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

Copy link
Copy Markdown
Member

Part of a larger effort to eliminate DB::Field

This PR is one step in an ongoing, multi-phase effort to remove DB::Field from the ClickHouse server, replacing it with type-consistent, copy-free representations (a size-1 IColumn + DataTypePtr for owned values, and a non-owning column-reference for transient reads). The motivation: Field's type tags do not map to ClickHouse SQL types (its NearestFieldType mapping collapses UInt8 -> UInt64, Float32 -> Float64, DateTime64 -> Decimal, ...), which is a recurring source of type bugs, and Field forces copies of data that already lives in memory as columns. Each PR in the effort is small, self-contained, behavior-preserving, and independently mergeable; this is one of the first.

This change

evaluateConstantExpression already builds a size-1 ColumnConst internally, then discards it by returning (*result_column)[0] as a Field. That both allocates a Field and collapses the exact SQL type through Field's NearestFieldType mapping.

This adds evaluateConstantExpressionAsColumn and tryEvaluateConstantExpressionAsColumn, which return the value as {ColumnPtr /* size 1 */, DataTypePtr} — preserving the exact type and avoiding the Field materialization. The existing Field-returning evaluateConstantExpression/tryEvaluateConstantExpression become thin shims over the new functions, so behavior is unchanged for callers that have not been migrated.

As a first use, the plain-String literal arguments of prometheusQuery/prometheusQueryRange (StoragePrometheusQuery) and timeSeriesSelector (StorageTimeSeriesSelector) now read their value with IColumn::getDataAt instead of materializing a Field and calling safeGet<String>.

There is no user-facing behavior change.

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

Internal refactor: add a column-returning evaluateConstantExpressionAsColumn to avoid materializing a Field for constant expressions. No user-facing change.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

🤖 Generated with Claude Code

`evaluateConstantExpression` already builds a size-1 `ColumnConst` internally,
then discards it by returning `(*result_column)[0]` as a `Field` — which both
allocates and collapses the exact SQL type through `Field`'s `NearestFieldType`
mapping (`UInt8`->`UInt64`, `Float32`->`Float64`, `DateTime64`->`Decimal`, ...).

Add `evaluateConstantExpressionAsColumn` (and `tryEvaluateConstantExpressionAsColumn`)
returning the value as `{ColumnPtr /*size 1*/, DataTypePtr}`, preserving the exact
type and avoiding the `Field`. The existing `Field`-returning functions become thin
shims over it, so behavior is unchanged for callers not yet migrated.

Migrate the plain `String` literal arguments of `prometheusQuery`/`prometheusQueryRange`
(`StoragePrometheusQuery`) and `timeSeriesSelector` (`StorageTimeSeriesSelector`) to the
new API, reading the value with `IColumn::getDataAt` instead of materializing a `Field`
and calling `safeGet<String>`.

This is the first step of reducing `DB::Field` usage; no behavior change.

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

clickhouse-gh Bot commented Jul 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c527ddb]

Summary: ✅


AI Review

Summary

This PR adds evaluateConstantExpressionAsColumn / tryEvaluateConstantExpressionAsColumn so constant-expression callers can keep the exact SQL type and avoid materializing a Field, then switches the string literal handling in prometheusQuery / prometheusQueryRange and timeSeriesSelector to the new API. I re-read the current diff, the touched files, the prior bot thread, and the Praktika report; the earlier wrapper-type regression (Nullable(String) / LowCardinality(String) arguments) is fixed in the current code, and I did not find remaining correctness, compatibility, or performance issues that warrant another review comment.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jul 18, 2026
Comment thread src/Storages/StoragePrometheusQuery.cpp Resolved
Addresses automated review on #110944: the exact-type column API narrowed the
accepted argument types. The previous `Field`-based code read the value via
`operator[]`, which flattens wrappers, so a non-NULL `Nullable(String)` /
`LowCardinality(String)` constant passed the `Field::Types::String` check.

Unwrap via `removeLowCardinalityAndNullable` before the String kind check to keep
those accepted, and add an explicit `isNullAt` check so a NULL value is still
rejected as before.

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

clickhouse-gh Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.10% 86.10% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 78.20% 78.20% +0.00%

Changed lines: Changed C/C++ lines covered: 42/52 (80.77%) · Uncovered code

Full report · Diff report

@yakov-olkhovskiy

Copy link
Copy Markdown
Member Author

Superseded by #110945, which folds in this change (evaluateConstantExpressionAsColumn) together with the column-native coercion (convertColumnToType) and the actual Field removal from the numbers/primes/generateSeries table functions, so the whole constant-expression flow is migrated in one reviewable PR.

@yakov-olkhovskiy
yakov-olkhovskiy deleted the cursor/field-eval-const-to-column branch July 20, 2026 06:42
scanhex12 pushed a commit to scanhex12/ClickHouse that referenced this pull request Jul 29, 2026
Addresses automated review on ClickHouse#110944: the exact-type column API narrowed the
accepted argument types. The previous `Field`-based code read the value via
`operator[]`, which flattens wrappers, so a non-NULL `Nullable(String)` /
`LowCardinality(String)` constant passed the `Field::Types::String` check.

Unwrap via `removeLowCardinalityAndNullable` before the String kind check to keep
those accepted, and add an explicit `isNullAt` check so a NULL value is still
rejected as before.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant