From 310047e10ae38239adca1e3235f37a4cacc5fa69 Mon Sep 17 00:00:00 2001 From: George Goldenberg <228832143+squaredg@users.noreply.github.com> Date: Thu, 1 Oct 2026 18:08:20 -0700 Subject: [PATCH] fix(common-utils): fall back to the scan when the Map key text index query fails getMapKeys returned [] from both text-index catch blocks, so a failed mergeTreeTextIndex query ended key discovery before the rollup and bounded scan paths ran. ClickHouse refuses mergeTreeTextIndex with ACCESS_DENIED for any user under a row policy on the table, so those users saw no Map keys. Log and fall through instead, matching getMapTextIndexKeyValues and getTextIndexKeyValues. The scan respects the row policy. Fixes #3265 --- .changeset/map-keys-text-index-fallthrough.md | 8 ++ .../src/__tests__/metadata.test.ts | 103 +++++++++++++++++- packages/common-utils/src/core/metadata.ts | 10 +- 3 files changed, 115 insertions(+), 6 deletions(-) create mode 100644 .changeset/map-keys-text-index-fallthrough.md diff --git a/.changeset/map-keys-text-index-fallthrough.md b/.changeset/map-keys-text-index-fallthrough.md new file mode 100644 index 0000000000..c6c7c6619e --- /dev/null +++ b/.changeset/map-keys-text-index-fallthrough.md @@ -0,0 +1,8 @@ +--- +'@hyperdx/common-utils': patch +'@hyperdx/app': patch +--- + +fix: fall back to the table scan when the Map key text index query fails, so key +autocomplete works on tables with row policies (where ClickHouse refuses +`mergeTreeTextIndex` with `ACCESS_DENIED`) instead of showing no keys. diff --git a/packages/common-utils/src/__tests__/metadata.test.ts b/packages/common-utils/src/__tests__/metadata.test.ts index 354d1e4c6b..ce8c1a4425 100644 --- a/packages/common-utils/src/__tests__/metadata.test.ts +++ b/packages/common-utils/src/__tests__/metadata.test.ts @@ -1,4 +1,4 @@ -import { ColumnMeta } from '@/clickhouse'; +import { ClickHouseQueryError, ColumnMeta } from '@/clickhouse'; import { ClickhouseClient } from '@/clickhouse/node'; import { GET_ALL_KEY_VALUES_CHUNK_SIZE, @@ -10,7 +10,7 @@ import { import * as renderChartConfigModule from '@/core/renderChartConfig'; import { timeFilterExpr } from '@/core/renderChartConfig'; import { isBuilderChartConfig } from '@/guards'; -import { TextIndexInfoLookup } from '@/queryParser'; +import { KvIndexInfo, TextIndexInfo, TextIndexInfoLookup } from '@/queryParser'; import { BuilderChartConfigWithDateRange, SourceKind, TSource } from '@/types'; // Mock ClickhouseClient @@ -2025,6 +2025,105 @@ describe('Metadata', () => { warn.mockRestore(); }); + describe('text index query failures', () => { + const dateRange: [Date, Date] = [ + new Date('2026-05-11T16:00:00Z'), + new Date('2026-05-11T17:00:00Z'), + ]; + const getKeys = (md: Metadata) => + md.getMapKeys({ + databaseName: 'otel', + tableName: 'otel_logs', + column: 'LogAttributes', + connectionId: 'conn-1', + dateRange, + timestampValueExpression: 'Timestamp', + }); + + // ClickHouse refuses mergeTreeTextIndex for any user under a row policy + // on the table (ClickHouse#119032); the scan respects the policy. + const rowPolicyDenied = () => + Promise.reject( + new ClickHouseQueryError( + 'Cannot read from `mergeTreeTextIndex` because a row policy is applied on table otel.otel_logs. (ACCESS_DENIED)', + '', + ), + ); + + const buildWithTextIndex = (textIndexInfo: TextIndexInfo) => { + const md = new Metadata(mockClickhouseClient, new MetadataCache()); + jest.spyOn(md, 'getServerVersion').mockResolvedValue([26, 8, 0, 0]); + jest + .spyOn(md, 'getMapColumnTextIndexes') + .mockResolvedValue(new Map([['LogAttributes', textIndexInfo]])); + return md; + }; + + const mockQueries = (onTextIndex: () => Promise) => + (mockClickhouseClient.query as jest.Mock).mockImplementation( + ({ query }: any) => { + if (query.includes('mergeTreeTextIndex(')) return onTextIndex(); + if (query.includes('sampledKeys')) { + return Promise.resolve({ + json: () => Promise.resolve({ data: [{ key: 'http.method' }] }), + }); + } + return Promise.resolve({ + json: () => Promise.resolve({ data: [lowCardinalityMapColumn] }), + }); + }, + ); + + const queriesSent = () => + (mockClickhouseClient.query as jest.Mock).mock.calls.map( + ([opts]) => opts.query as string, + ); + + let warn: jest.SpyInstance; + beforeEach(() => { + warn = jest.spyOn(console, 'warn').mockImplementation(() => {}); + }); + afterEach(() => warn.mockRestore()); + + it('falls back to the scan when the key text index query is denied', async () => { + const md = buildWithTextIndex({ + key: { indexName: 'idx_log_attr_key', mapColumn: 'LogAttributes' }, + }); + mockQueries(rowPolicyDenied); + + await expect(getKeys(md)).resolves.toEqual(['http.method']); + expect(queriesSent().some(q => q.includes('sampledKeys'))).toBe(true); + }); + + it('falls back to the scan when the kv text index query is denied', async () => { + const md = buildWithTextIndex({ + kv: { + columnName: 'LogAttributeItems', + indexName: 'idx_log_attr_items', + separator: '=', + } as KvIndexInfo, + }); + mockQueries(rowPolicyDenied); + + await expect(getKeys(md)).resolves.toEqual(['http.method']); + expect(queriesSent().some(q => q.includes('sampledKeys'))).toBe(true); + }); + + it('returns text index keys without scanning when the index query succeeds', async () => { + const md = buildWithTextIndex({ + key: { indexName: 'idx_log_attr_key', mapColumn: 'LogAttributes' }, + }); + mockQueries(() => + Promise.resolve({ + json: () => Promise.resolve({ data: [{ key: 'from.index' }] }), + }), + ); + + await expect(getKeys(md)).resolves.toEqual(['from.index']); + expect(queriesSent().some(q => q.includes('sampledKeys'))).toBe(false); + }); + }); + it('scans the last 24h when the caller supplies no dateRange', async () => { const md = buildMetadata(); (mockClickhouseClient.query as jest.Mock) diff --git a/packages/common-utils/src/core/metadata.ts b/packages/common-utils/src/core/metadata.ts index 06ef78269d..a602a5ce5f 100644 --- a/packages/common-utils/src/core/metadata.ts +++ b/packages/common-utils/src/core/metadata.ts @@ -1028,11 +1028,13 @@ export class Metadata { return keys; } } catch (e) { + // Fall through rather than return []: ClickHouse refuses + // mergeTreeTextIndex under any row policy on the table (ACCESS_DENIED), + // and the scan below still works and respects the policy. #3265 console.warn( - 'getMapKeys rollup query failed for key text index query', + 'getMapKeys key text index query failed; falling back to the next strategy', e, ); - return []; } } else if ( textIndexInfo?.kv?.indexName && @@ -1068,11 +1070,11 @@ export class Metadata { return keys; } } catch (e) { + // See the key text index catch above. console.warn( - 'getMapKeys rollup query failed for kv text index query', + 'getMapKeys kv text index query failed; falling back to the next strategy', e, ); - return []; } }