Skip to content

Text index postprocessor resubmit - #108606

Merged
Ergus merged 11 commits into
ClickHouse:masterfrom
Ergus:text_index_postprocessor_resubmit
Jun 30, 2026
Merged

Ergus merged 11 commits into
ClickHouse:masterfrom
Ergus:text_index_postprocessor_resubmit

Conversation

@Ergus

@Ergus Ergus commented Jun 26, 2026 •

Copy link
Copy Markdown
Member

Related: #98939
Related: #108432
Related: #103172
Related: #108456

Parameters:

postprocessor : The postprocessor is an arbitrary expression that transforms each token after tokenization.

Example use:

CREATE TABLE users (
    str String,
    index idx() type text(tokenizer = 'spliByNonApha', postprocessor = lower(str))
ENGINE = MergeTree ORDER BY tuple();

SELECT * FROM users WHERE hasAnyTokens(name, 'crash');

Changelog category (leave one):

  • New Feature

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Added a postprocessor to the text index which transforms the tokens after tokenization.

Version info

  • Merged into: 26.7.1.283 (included in 26.7 and later)
  • Backported to: 26.6.2.33

Ergus added 7 commits June 26, 2026 11:33
1. Add hasPhrase to needApplyPostprocessor
2. Process haystack by postprocessing and rejoining.
3. Add postprocess code to has_positions branch in condition
4. Use a counter to set positions when addDocumentsFromArray.
@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e674dce]

Summary: ✅


AI Review

Summary

This PR adds a postprocessor argument for text indexes and rewires indexed token functions so query-time normalization matches the indexed token stream. The current patch still has two correctness blockers: one read path for hasPhrase can disagree with exact positional index reads after token-length-changing postprocessors, and the fallback/default-expression DAG does not support wrapped string types that the text index itself accepts.

Missing context / blind spots
  • ⚠️ The direct S3 CI report for e674dcebbef724b7b05ba7fb8b377833aa72d320 exposed no test rows, and there is no built clickhouse binary in this checkout, so I could not locally run the new stateless tests.
Findings

❌ Blockers

  • [src/Processors/QueryPlan/Optimizations/optimizeDirectReadFromTextIndex.cpp:647] hasPhrase with positions promises the same postprocessed phrase semantics whether a part is read from the text index, read through the default expression for an unmaterialized part, or evaluated with query_plan_direct_read_from_text_index = 0. The fallback expression rejoins postprocessed tokens and calls hasPhrase with the original tokenizer again, so a length-shortening postprocessor can make the fallback tokenize to a different phrase than the index stores. For example, tokenizer = ngrams(3), postprocessor = substring(message, 1, 1), row abc, and needle abc store/match token a in the positional index, while the fallback evaluates hasPhrase('a', 'a', 'ngrams(3)') and returns 0.
    Suggested fix: evaluate phrase matching over the postprocessed token array directly, or disable the exact/fallback normalization for tokenizer/postprocessor combinations where re-tokenizing transformed tokens is not equivalent.
  • [src/Storages/MergeTree/MergeTreeIndexTextPostprocessor.cpp:128] The fallback/default-expression path must accept the same input types as index construction. getOriginalActionsDAG feeds the original column, or each array element, directly into tokens, but tokens only accepts String/FixedString; it rejects Nullable(String), LowCardinality(String), and nullable array elements. A supported postprocessed text index on Nullable(String) or Array(Nullable(String)) can therefore fail while building the optimizer DAG for hasToken, hasAnyTokens, hasAllTokens, or hasPhrase.
    Suggested fix: unwrap supported wrappers before tokens and skip nulls consistently with index construction, e.g. guard nullable array elements and tokenize assumeNotNull(x).
Tests
  • ⚠️ Add a focused hasPhrase regression with positions = 1, an ngrams tokenizer, and a postprocessor that changes token length, checking direct read on/off and a partially materialized index.
  • ⚠️ Add a nullable or low-cardinality postprocessor regression that forces the fallback/default-expression path, such as query_plan_direct_read_from_text_index = 0 and a part written before ALTER ADD INDEX.
Final Verdict

Status: ❌ Block

Minimum required actions: fix the hasPhrase fallback semantics, fix wrapped-string handling in getOriginalActionsDAG, and add the focused regressions above.

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jun 26, 2026
@Ergus

Ergus commented Jun 26, 2026

Copy link
Copy Markdown
Member Author

@ahmadov Please check if the hasPhrase behavior in the test is the expected one

MutableColumnConstPtr separator_column = separator_type->createColumnConst(0, Field(String(" ")));
const ActionsDAG::Node & separator = actions_dag.addColumn(std::move(separator_column), separator_type, "' '");
FunctionOverloadResolverPtr concat = FunctionFactory::instance().get("arrayStringConcat", context);
new_children[0] = &actions_dag.addFunction(concat, {new_children[0], &separator}, "");

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.

The fallback expression here is not equivalent to the positional index once the hasPhrase postprocessor changes token lengths. The indexed path stores and matches the postprocessed token sequence directly, but unmaterialized parts, query_plan_direct_read_from_text_index = 0, and the text-index reader's high-cardinality fallback all run this rewritten expression, which joins those tokens and calls hasPhrase with the original tokenizer again. For example, with tokenizer = ngrams(3), postprocessor = substring(message, 1, 1), and positions = 1, a row abc stores token a and the exact index matches hasPhrase(message, 'abc'); the fallback rewrites both sides to a, then hasPhrase('a', 'a', 'ngrams(3)') has no phrase tokens and returns 0. That makes results depend on materialization/direct-read/fallback choice. Please either evaluate hasPhrase over the postprocessed token array directly, or disable this exact/fallback normalization when re-tokenizing transformed tokens is not proven equivalent, and add a direct-read on/off regression for this case.

Comment on lines +128 to +141
ASTPtr tokens_ast = make_intrusive<ASTIdentifier>(col_name);
if (isArray(col_type))
{
tokens_ast = makeASTFunction("arrayFlatten",
makeASTFunction("arrayMap",
makeASTLambda({postprocessor_element_arg},
makeASTFunction("tokens",
make_intrusive<ASTIdentifier>(postprocessor_element_arg),
make_intrusive<ASTLiteral>(Field(tokenizer_description)))),
std::move(tokens_ast)));
}
else
{
tokens_ast = makeASTFunction("tokens", std::move(tokens_ast), make_intrusive<ASTLiteral>(Field(tokenizer_description)));

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.

The fallback/default-expression path has to accept the same column types that the index builder accepts. Here it feeds the original column, or each array element, directly into tokens, but tokens only type-checks String/FixedString; it does not accept Nullable(String) or LowCardinality(String). As a result, a postprocessed text index on a supported type such as Nullable(String) or Array(Nullable(String)) fails while building this DAG for hasToken/hasAnyTokens/hasAllTokens/hasPhrase (this is built before exact direct-read replacement too, and it is also the expression needed for unmaterialized parts). Please unwrap/skip nullable values before calling tokens (for arrays, e.g. guard null elements and tokenize assumeNotNull(x)) and add a direct-read-off or partially materialized regression for a nullable postprocessed index.

@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.40% 85.40% +0.00%
Functions 92.60% 92.70% +0.10%
Branches 77.60% 77.60% +0.00%

Changed lines: Changed C/C++ lines covered: 514/530 (96.98%) · Uncovered code

Full report · Diff report

@Ergus
Ergus added this pull request to the merge queue Jun 30, 2026
@Ergus

Ergus commented Jun 30, 2026

Copy link
Copy Markdown
Member Author

The private tests failing are unrelated and known issues

@fm4v fm4v self-assigned this Jun 30, 2026
Merged via the queue into ClickHouse:master with commit 33af86e Jun 30, 2026
174 checks passed
@Ergus
Ergus deleted the text_index_postprocessor_resubmit branch June 30, 2026 10:59
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 30, 2026
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jun 30, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi @Ergus @rschu1ze — while reviewing this PR I found the following:

Could you review the test PR and add the can be tested label if it looks good?
Happy to discuss — close anything that's wrong or already addressed.

robot-clickhouse added a commit that referenced this pull request Jul 6, 2026
Cherry pick #108606 to 26.6: Text index postprocessor resubmit
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 6, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 6, 2026
Backport #108606 to 26.6: Text index postprocessor resubmit
pull Bot pushed a commit to sonvt1710/ClickHouse that referenced this pull request Aug 1, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…sor_resubmit

Text index postprocessor resubmit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-feature Pull request with new product feature pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.6-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants