Skip to content

Support ALIAS columns in text index preprocessor and postprocessor - #110014

Merged
Ergus merged 31 commits into
ClickHouse:masterfrom
Ergus:text_index_alias_columns
Aug 6, 2026
Merged

Ergus merged 31 commits into
ClickHouse:masterfrom
Ergus:text_index_alias_columns

Conversation

@Ergus

@Ergus Ergus commented Jul 10, 2026

Copy link
Copy Markdown
Member

Closes: #95944
Related: #103626

The text index preprocessor and postprocessor arguments only accepted physical
columns. Fixing that

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

Allow the preprocessor and postprocessor expressions of a text index to reference ALIAS columns.

@Ergus
Ergus requested a review from ahmadov July 10, 2026 15:30
@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b38e421]

Summary: ✅


AI Review

Summary

This PR fixes the main ALIAS handling problems for text-index preprocessor / postprocessor expressions, including lambda shadowing, chained aliases, and the ILIKE case-folding optimization on expanded ALIAS expressions. The remaining blocker is that the expander still only recognizes exact identifier names, so transforms that reference a subcolumn of a composite ALIAS (for example a.v where a is ALIAS raw) are still left unexpanded and continue to fail.

Findings

⚠️ Majors

  • [src/Storages/ReplaceAliasByExpressionVisitor.cpp:59] The new contract is “text-index transforms may reference ALIAS columns”, but ReplaceAliasByExpressionMatcher still only calls columns.hasAlias(column.name()) on the full identifier string. Compound identifiers such as a.v therefore never match an ALIAS root a, so preprocessor = lower(a.v) / postprocessor = lower(a.v) on tuple/map/json-style ALIAS columns are still analyzed against physical source columns only and remain rejected. Suggested fix: when visiting a compound identifier, detect an ALIAS root in name_parts.front(), expand that root recursively, then rebuild the remaining subcolumn access on top of the expanded AST and add focused regression tests for both preprocessor and postprocessor.
Final Verdict

⚠️ Needs changes before merge: support for composite ALIAS references in text-index transforms is still incomplete.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.60% 86.50% -0.10%
Functions 91.90% 91.90% +0.00%
Branches 78.80% 78.80% +0.00%

Changed lines: Changed C/C++ lines covered: 64/64 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 10, 2026
@Ergus

Ergus commented Jul 10, 2026

Copy link
Copy Markdown
Member Author

@ahmadov I faced the issue myself and didn't know that there was an open issue related and you implement a fix.

Comment thread src/Storages/IndicesDescription.cpp Resolved
Comment thread src/Storages/IndicesDescription.cpp Resolved
/// upper(col), or upperUTF8(col), where col is the index column itself.
/// upper(col), or upperUTF8(col), where col is the index column itself. The argument is
/// compared by column name so that an ALIAS index column (expanded to its expression) is
/// still recognized, matching the physical-column case.

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.

matching the physical-column case --> matching the non-ALIAS column handling

@rschu1ze rschu1ze self-assigned this Jul 13, 2026

@ahmadov ahmadov 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 👍

DROP TABLE tab;

SELECT '-- Preprocessor referencing an ALIAS column';
-- https://github.com/ClickHouse/ClickHouse/issues/95944

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.

I suppose the general practice is moving such tests into its own file with the issue number in the name e.g. 02346_text_index_bug95944.sql. What do you think about moving to a new test file?

I don't have a strong opinion on that, it's up to tou.

@Ergus Ergus Jul 22, 2026 •

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.

I tend to agree, I put them here because if I run all preprocessor and postprocessor tests I won't forget to run also these... maybe calling them like 02346_text_index_preprocessor_bug95944.sql...

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.

yes, that could be one option 👍

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.

Yes, let's move all new tests into 02346_text_index_bug95944.sql. You will still be able to run clickhouse-test 02346_text_index and it will execute this test.

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.

Well, thinking again - the preprocessor tests also kind of fit. Still consider moving if CI is red and another iteration is needed.

@clickhouse-gh

clickhouse-gh Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 62/62 (100.00%) · Uncovered code

Full report · Diff report

if (data.private_aliases.contains(column_name))
return;

if (data.columns.hasAlias(column_name))

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 still only expands exact identifier names. For a transform that references a subcolumn of an ALIAS-typed tuple/map/etc., the AST identifier is compound (a.v), so column.name() is a.v and columns.hasAlias(column_name) is false. The visitor leaves the transform untouched, and validation later analyzes it against the physical source columns only, so definitions like

CREATE TABLE t
(
    raw Tuple(v String),
    a Tuple(v String) ALIAS raw,
    INDEX idx(raw.v) TYPE text(tokenizer = 'splitByNonAlpha', preprocessor = lower(a.v))
) ENGINE = MergeTree ORDER BY tuple();

still fail even though they are logically the same as lower(raw.v). The same gap exists for postprocessor. Can we expand compound identifiers whose root name is an ALIAS (or explicitly reject/document that surface) instead of only matching whole-name aliases?

@clickhouse-gh

clickhouse-gh Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing b38e421ed with master 52788b977 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e23087462; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 689.04 MiB 686.05 MiB -3.00 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes

11 object files changed (+83.23 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/ReplaceAliasByExpressionVisitor.cpp.o 6.76 KiB 66.36 KiB +59.59 KiB (+881.34%)

737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

@Ergus
Ergus added this pull request to the merge queue Aug 6, 2026
Merged via the queue into ClickHouse:master with commit a5c0c5d Aug 6, 2026
179 checks passed
@Ergus
Ergus deleted the text_index_alias_columns branch August 6, 2026 13:48
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 6, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Ergus @rschu1ze @ahmadov — a couple of suggestions to make this PR easier for reviewers and the changelog reader.

Description (currently incomplete):

  • Why: The body states only that the arguments accepted physical columns and that this is fixed. It does not mention the two side effects on the shared ReplaceAliasByExpressionMatcher: recursive ALIAS-chain expansion and lambda-parameter masking, both of which apply to every skip index expression, nor the new BAD_ARGUMENTS rejection when an ALIAS body would be captured by an enclosing lambda parameter (a new user-visible DDL error), nor why getNameWithoutAliases had to switch to value-based constant rendering.

  • Suggested addition:

    Besides the text index arguments, this changes ReplaceAliasByExpressionMatcher, which is shared by IndexDescription::initExpressionInfo and therefore affects the expression of every skip index type:

    • ALIAS chains (a ALIAS b, b ALIAS s) are now expanded recursively. Previously only one level was substituted, so INDEX idx(a) TYPE set(100) failed with UNKNOWN_IDENTIFIER.
    • A lambda parameter now masks a same-named ALIAS column, so INDEX idx(arrayMap(x -> upper(x), arr)) no longer substitutes an ALIAS column named x into the lambda body.

    Only the text index preprocessor/postprocessor path passes reject_lambda_capture = true; there an ALIAS whose expanded body would be captured by an enclosing lambda parameter is now rejected with BAD_ARGUMENTS instead of being silently captured.

    getNameWithoutAliases in optimizeDirectReadFromTextIndex renders constant nodes by Field value because an ALIAS body containing a literal (e.g. ifNull(p, 'default')) produces different constant result_names in the query DAG and in the preprocessor DAG, which would otherwise defeat the hasSubexpression match.

Ignore this if the current title/body is intentional.

alexey-milovidov added a commit that referenced this pull request Sep 13, 2026
`02346_text_index_functions_select_expression` fails at query 54 on
this branch:

    Code: 47. DB::Exception: Missing columns: 'alias' while processing:
    'lower(alias)', required columns: 'alias', available columns:
    '__text_index_token': When validating secondary index `idx_alias`.
    (UNKNOWN_IDENTIFIER)

Using an `ALIAS` column inside a text index `preprocessor` /
`postprocessor` needs `expandTextIndexTransformAliases` in
`IndexDescription::getIndexFromAST`, which rewrites the argument's
`alias` into `ifNull(str, 'default')` before
`MergeTreeIndexTextPostprocessor` looks for the index column name.
That comes from #110014, which is not in this branch: here the
postprocessor stays `lower(alias)`, nothing matches the single source
column `__text_index_token`, and the `CREATE TABLE` is rejected.

#110014 is not folded in: besides that helper it also rewrites
`ReplaceAliasByExpressionVisitor`, which every skip index goes
through, and that is too wide a change to carry inside a bug-fix
backport of an unrelated pull request. If the `ALIAS` support is
wanted on 26.6 it should be backported on its own merit - it is
labelled `pr-bugfix`.

Everything the backported pull request is actually about - injecting
the tokenizer into supported functions outside `WHERE` / `PREWHERE` -
is covered by the rest of the test, which passes.
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.

Support ALIAS columns in preprocessor argument for Full-Text Index

5 participants