Skip to content

fix: resolve Distributed table to local table for mergeTreeTextIndex - #2930

Closed
Vansh98789 wants to merge 2 commits into
ClickHouse:mainfrom
Vansh98789:fix/text-index-distributed-table
Closed

Vansh98789 wants to merge 2 commits into
ClickHouse:mainfrom
Vansh98789:fix/text-index-distributed-table

Conversation

@Vansh98789

Copy link
Copy Markdown
Contributor

Summary

Fixes #2880 — text-index metadata queries fail with BAD_ARGUMENTS (There is no index with name ...) when a source's table is Distributed.

Root cause

mergeTreeTextIndex(...) and system.parts only understand MergeTree-family tables — a Distributed table has neither the index nor any local parts. Index discovery already resolves Distributed → local table (via getSkipIndices / getDistributedTableArgs), but index reads in packages/common-utils/src/core/metadata.ts were passing the Distributed table name straight through in four places:

  • getMapKeys — key-index branch
  • getMapKeys — kv-index branch
  • getMapTextIndexKeyValues
  • getTextIndexKeyValues

partsOverlapFilter also queried system.parts using the Distributed table name, which has no local parts, so even fixing the index name alone would have produced an empty (but not erroring) predicate.

Because the error was only console.warn-ed and getMapKeys returned [] on failure, this failed silently: no SpanAttributes/LogAttributes keys in autocomplete or the field picker, and no filter values for columns routed to the text index — for any source backed by a Distributed table (e.g. otel_traces/otel_logs in a standard multi-shard setup).

Fix

  • Added resolveTextIndexTableRef() — resolves a Distributed table to its underlying local table + cluster, reusing the same getDistributedTableArgs / queryTableMetadata pattern getSkipIndices already uses.
  • Added mergeTreeTextIndexExpr() — builds the mergeTreeTextIndex(...) table function expression, wrapping it in cluster(...) when the caller resolved a Distributed table, so all shards are queried.
  • Updated all 4 call sites to resolve the local table/cluster first, and pass the resolved values to both mergeTreeTextIndex(...) and partsOverlapFilter's system.parts lookup.
  • getMapKeys's two text-index catch blocks no longer return [] on failure — they now fall through to the MV / raw-table-scan tiers, so a transient text-index failure degrades gracefully instead of silently hiding all keys for that column (per the issue's proposed fix).

Before / after

Before (against a Distributed source):

FROM mergeTreeTextIndex('observability', 'otel_traces', 'idx_span_attr_items')
WHERE part_name IN (SELECT name FROM system.parts WHERE database = 'observability' AND table = 'otel_traces' ...)

→ Code: 36. DB::Exception: There is no index with name 'idx_span_attr_items'. (BAD_ARGUMENTS)

After:

FROM cluster('my_cluster', mergeTreeTextIndex('observability', 'otel_traces_local', 'idx_span_attr_items'))
WHERE part_name IN (SELECT name FROM system.parts WHERE database = 'observability' AND table = 'otel_traces_local' ...)

Testing

New regression tests in metadata.test.ts:

  • getMapKeys resolves a Distributed table to its local table + cluster() for both mergeTreeTextIndex and system.parts, and never leaks the Distributed table name into query params.
  • getMapKeys falls through to the raw-table scan (not []) when the text-index query throws BAD_ARGUMENTS.
  • getAllKeyValues routes a Distributed-source TraceId lookup through getTextIndexKeyValues using the resolved local table + cluster.

Full metadata.test.ts suite passes (112/112, no regressions).

Changeset

Included — patch on @hyperdx/common-utils.

Related

Closes #2880

Text-index metadata queries (getMapKeys, getMapTextIndexKeyValues,
getTextIndexKeyValues) were passing the Distributed table name straight
to mergeTreeTextIndex(...) and system.parts, which only understand
MergeTree-family tables. This caused BAD_ARGUMENTS ('There is no index
with name ...') for any source pointing at a Distributed table.

Resolves the Distributed table to its underlying local table + cluster
(reusing the same getDistributedTableArgs/queryTableMetadata pattern
getSkipIndices already uses) and wraps the table function in cluster(...).

getMapKeys also now falls through to the MV/raw-table-scan tiers instead
of returning [] when the text-index query fails, so a transient failure
degrades gracefully instead of hiding all keys for that column.

Fixes ClickHouse#2880
@changeset-bot

changeset-bot Bot commented Aug 17, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 4be7ce6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hyperdx/common-utils Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Aug 17, 2026

Copy link
Copy Markdown

@Vansh98789 is attempting to deploy a commit to the HyperDX Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps

greptile-apps Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR resolves Distributed source tables to their local MergeTree tables before querying text-index metadata and adds graceful fallback behavior for map-key discovery.

  • Adds shared helpers for resolving local table/cluster references and constructing cluster-aware mergeTreeTextIndex expressions.
  • Applies resolved references to map-key and key-value text-index queries and their system.parts filters.
  • Adds regression coverage for Distributed sources and failed text-index queries.
  • Adds a patch changeset for @hyperdx/common-utils.

Confidence Score: 5/5

The PR appears safe to merge, with no concrete blocking or independently actionable non-blocking issue established.

The changed metadata paths consistently resolve Distributed tables before building text-index and parts queries, while the new tests cover the principal routing and fallback behavior.

Important Files Changed

Filename Overview
packages/common-utils/src/core/metadata.ts Adds centralized Distributed-to-local table resolution, cluster-aware text-index SQL generation, and map-key fallback behavior without an accepted correctness issue.
packages/common-utils/src/tests/metadata.test.ts Adds regression tests covering Distributed map-key and TraceId text-index routing plus raw-scan fallback after an index-query failure.
.changeset/fix-text-index-distributed-table.md Accurately documents the patch-level Distributed text-index metadata fix and fallback behavior.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Metadata request for source table] --> B{Table engine}
  B -->|MergeTree| C[Use original database and table]
  B -->|Distributed| D[Resolve cluster and local MergeTree table]
  C --> E[Build mergeTreeTextIndex expression]
  D --> F[Wrap mergeTreeTextIndex in cluster]
  E --> G[Apply system.parts overlap filter]
  F --> G
  G --> H{Text-index query succeeds}
  H -->|Yes| I[Return metadata keys or values]
  H -->|No, map-key path| J[Fall through to MV or raw-table scan]
Loading

Reviews (1): Last reviewed commit: "Merge branch 'main' into fix/text-index-..." | Re-trigger Greptile

@wrn14897 wrn14897 closed this Aug 18, 2026
@wrn14897

Copy link
Copy Markdown
Contributor

Please stop spinning up agents to pick up issues unless you really understand what the issue is about.

@Vansh98789

Copy link
Copy Markdown
Contributor Author

Please stop spinning up agents to pick up issues unless you really understand what the issue is about.

@wrn14897, I understand the concern.

I did use AI assistance while working on this PR, but I didn’t blindly pick up the issue. I went through #2880, the existing getSkipIndices implementation, and the generated queries and tests. The fix follows the root cause described in the issue: resolving the Distributed table to the underlying local table + cluster for both mergeTreeTextIndex() and system.parts.

I’m confident in the approach, but if you believe #2930 is technically incorrect, I’m happy to investigate and fix it. I’d just appreciate pointing me to the specific part of the implementation that is wrong so I can understand the concern and address it properly.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Text index metadata path passes the Distributed table name to mergeTreeTextIndex → "There is no index with name ..." (BAD_ARGUMENTS)

2 participants