Skip to content

Guard the text index rewrite above a JOIN by column provenance - #116197

Closed
Ergus wants to merge 10 commits into
ClickHouse:masterfrom
Ergus:Fix_115999
Closed

Ergus wants to merge 10 commits into
ClickHouse:masterfrom
Ergus:Fix_115999

Conversation

@Ergus

@Ergus Ergus commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

Closes: #115999
Related: #108808
Related: #113154

When combining an OR with a condition on the other table it is evaluated after the JOIN and silently fell back to splitByNonAlpha. With tokenizer = array the index keeps alpha beta gamma as one token while splitByNonAlpha splits it into three, so matching rows disappeared.

The tokenizer now also reaches predicates evaluated after a JOIN, but only where the value is read straight from the indexed table.

Reading the index directly stays restricted to predicates evaluated on the table itself: it yields a per-granule answer, meaningless once a JOIN has reordered or duplicated rows. Previously it could reach past the JOIN and fail the query with an exception.

When nothing can be evaluated before the JOIN, the index is never consulted for that table (force_data_skipping_indices reports INDEX_NOT_USED) and no tokenizer is available to supply.

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 hasAnyTokens, hasAllTokens and hasPhrase on a column with a non-default text index tokenizer silently returning wrong results when the predicate could not be pushed below a JOIN.


Workflow [PR]
Sync PR [sync-upstream/pr/116197]

@clickhouse-gh

clickhouse-gh Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [5356bba]

Summary: ❌

job_name test_name status info comment
Fast test FAIL
02346_text_index_bug108519_qcc_skip_index FAIL cidb
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL
Code Review DROPPED
Fast test (arm_darwin) DROPPED
Build (amd_debug) DROPPED
Build (amd_asan_ubsan) DROPPED
Build (amd_tsan) DROPPED
Build (amd_msan) DROPPED
Build (amd_binary) DROPPED
Build (arm_debug) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 24, 2026
{
NameSet result;

for (const auto * output : dag.getOutputs())

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.

trackColumnsThroughDAG only follows explicit DAG outputs. FilterStep and many ExpressionSteps preserve untouched scan columns via updateHeader passthrough instead, so a lower pushed-down filter like a.group_id = 1 clears the tracked set here because filter_dag.getOutputs() only contains the boolean filter column.

Once that happens, the loop stops on tracked_columns.empty() before it reaches the higher filter above the JOIN, so the original hasAnyTokens(...) OR b... shape is still broken whenever part of the predicate was already pushed below the join.

Please preserve tracked names that survive as passthrough header columns too, not only aliases that appear in dag.getOutputs().

bool hasIndexForColumn(const String & column_name) const { return header.has(column_name) || column_name == normalized_index_column_name; }
bool hasIndexForColumn(const String & column_name) const
{
String unqualified = stripJoinTableQualifier(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 only normalizes bare __tableN.col names. The post-JOIN text-index carriers for maps/JSON still arrive as mapValues(__table1.m), mapKeys(__table1.m), __table1.json.path, or __table1.map.key_<...>, and none of those forms are normalized before the later traverseFunctionNode / tryMatchNodeToJSONIndex checks.

That means hasAnyTokens(m['k'], ...) and hasAllTokens(json.a::String, ...) above a JOIN still skip tokenizer injection and can silently evaluate with the wrong tokenizer, even though the value is still read straight from the indexed table.

@clickhouse-gh

clickhouse-gh Bot commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

No arm_release build profile data for commit 5356bba - the build was skipped, reused from cache, or predates profile upload.

@Ergus

Ergus commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

Closed in favor of #116339

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect results with hasAnyTokens on JOINed table

1 participant