diff --git a/.changeset/metric-names-source-query-settings.md b/.changeset/metric-names-source-query-settings.md new file mode 100644 index 0000000000..c2a0c7b9f8 --- /dev/null +++ b/.changeset/metric-names-source-query-settings.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/common-utils': patch +'@hyperdx/app': patch +--- + +fix: apply source query settings to metric explorer, metric name, attribute, and Kubernetes facet lookups diff --git a/packages/app/src/components/KubernetesFilters.tsx b/packages/app/src/components/KubernetesFilters.tsx index f8a33fb538..5dff988d6b 100644 --- a/packages/app/src/components/KubernetesFilters.tsx +++ b/packages/app/src/components/KubernetesFilters.tsx @@ -261,6 +261,7 @@ export const KubernetesFilters: React.FC = ({ select: '', timestampValueExpression: metricSource.timestampValueExpression || '', connection: metricSource.connection, + source: metricSource.id, dateRange, }), [metricSource, facetWhere, dateRange], diff --git a/packages/app/src/hooks/__tests__/useAvailableMetricNames.test.ts b/packages/app/src/hooks/__tests__/useAvailableMetricNames.test.ts index e16e06ba97..27243573a9 100644 --- a/packages/app/src/hooks/__tests__/useAvailableMetricNames.test.ts +++ b/packages/app/src/hooks/__tests__/useAvailableMetricNames.test.ts @@ -87,6 +87,23 @@ describe('useAvailableMetricNames', () => { expect(result.current.isLoading).toBe(true); }); + it('names the source so its query settings apply', () => { + mockUseGetKeyValues.mockReturnValue( + fromPartial({ + data: undefined, + isLoading: false, + isPlaceholderData: false, + }), + ); + render(); + expect(mockUseGetKeyValues).toHaveBeenCalledWith( + expect.objectContaining({ + chartConfig: expect.objectContaining({ source: METRIC_SOURCE.id }), + }), + expect.anything(), + ); + }); + it('returns an empty set when the query has no data', () => { mockUseGetKeyValues.mockReturnValue( fromPartial({ diff --git a/packages/app/src/hooks/__tests__/useFetchMetricAttributeValues.test.tsx b/packages/app/src/hooks/__tests__/useFetchMetricAttributeValues.test.tsx new file mode 100644 index 0000000000..9bc32b0823 --- /dev/null +++ b/packages/app/src/hooks/__tests__/useFetchMetricAttributeValues.test.tsx @@ -0,0 +1,87 @@ +import React from 'react'; +import { + MetricsDataType, + SourceKind, + TMetricSource, +} from '@hyperdx/common-utils/dist/types'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { renderHook, waitFor } from '@testing-library/react'; + +import { useFetchMetricAttributeValues } from '@/hooks/useFetchMetricAttributeValues'; + +const query = jest.fn(); + +jest.mock('@/clickhouse', () => ({ + getClickhouseClient: () => ({ query }), +})); +jest.mock('@/queryAttribution', () => ({ + useMetadataQueryAttribution: () => ({}), +})); + +const SOURCE: TMetricSource = { + id: 'metrics', + name: 'Metrics', + kind: SourceKind.Metric, + connection: 'conn-1', + from: { databaseName: 'default', tableName: '' }, + timestampValueExpression: 'TimeUnix', + resourceAttributesExpression: 'ResourceAttributes', + metricTables: { + [MetricsDataType.Gauge]: 'otel_metrics_gauge', + [MetricsDataType.Sum]: '', + [MetricsDataType.Histogram]: '', + [MetricsDataType.ExponentialHistogram]: '', + [MetricsDataType.Summary]: '', + }, + querySettings: [ + { setting: 'force_optimize_skip_unused_shards', value: '0' }, + { setting: 'timeout_overflow_mode', value: 'throw' }, + ], +}; + +const COMMON_ARGS = { + databaseName: 'default', + metricType: MetricsDataType.Gauge, + metricName: 'up', + tableSource: SOURCE, +}; + +function createWrapper() { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }); + return function Wrapper({ children }: { children: React.ReactNode }) { + return ( + {children} + ); + }; +} + +async function settingsSent() { + await waitFor(() => expect(query).toHaveBeenCalled()); + return query.mock.calls[0][0].clickhouse_settings; +} + +beforeEach(() => { + query.mockReset(); + query.mockResolvedValue({ json: () => Promise.resolve({ data: [] }) }); +}); + +describe('useFetchMetricAttributeValues', () => { + it('applies source query settings without overriding its timeout bound', async () => { + renderHook( + () => + useFetchMetricAttributeValues({ + ...COMMON_ARGS, + attributeName: 'service.name', + attributeCategory: 'ResourceAttributes', + }), + { wrapper: createWrapper() }, + ); + + expect(await settingsSent()).toMatchObject({ + force_optimize_skip_unused_shards: '0', + timeout_overflow_mode: 'break', + }); + }); +}); diff --git a/packages/app/src/hooks/__tests__/useFetchMetricMetadata.test.tsx b/packages/app/src/hooks/__tests__/useFetchMetricMetadata.test.tsx new file mode 100644 index 0000000000..49a0ea6c70 --- /dev/null +++ b/packages/app/src/hooks/__tests__/useFetchMetricMetadata.test.tsx @@ -0,0 +1,80 @@ +import React from 'react'; +import { + MetricsDataType, + SourceKind, + TMetricSource, +} from '@hyperdx/common-utils/dist/types'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { renderHook, waitFor } from '@testing-library/react'; + +import { useFetchMetricMetadata } from '@/hooks/useFetchMetricMetadata'; + +const query = jest.fn(); + +jest.mock('@/clickhouse', () => ({ + getClickhouseClient: () => ({ query }), +})); +jest.mock('@/queryAttribution', () => ({ + useMetadataQueryAttribution: () => ({}), +})); + +const SOURCE: TMetricSource = { + id: 'metrics', + name: 'Metrics', + kind: SourceKind.Metric, + connection: 'conn-1', + from: { databaseName: 'default', tableName: '' }, + timestampValueExpression: 'TimeUnix', + resourceAttributesExpression: 'ResourceAttributes', + metricTables: { + [MetricsDataType.Gauge]: 'otel_metrics_gauge', + [MetricsDataType.Sum]: '', + [MetricsDataType.Histogram]: '', + [MetricsDataType.ExponentialHistogram]: '', + [MetricsDataType.Summary]: '', + }, + querySettings: [ + { setting: 'force_optimize_skip_unused_shards', value: '0' }, + { setting: 'timeout_overflow_mode', value: 'throw' }, + ], +}; + +const COMMON_ARGS = { + databaseName: 'default', + metricType: MetricsDataType.Gauge, + metricName: 'up', + tableSource: SOURCE, +}; + +function createWrapper() { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }); + return function Wrapper({ children }: { children: React.ReactNode }) { + return ( + {children} + ); + }; +} + +async function settingsSent() { + await waitFor(() => expect(query).toHaveBeenCalled()); + return query.mock.calls[0][0].clickhouse_settings; +} + +beforeEach(() => { + query.mockReset(); + query.mockResolvedValue({ json: () => Promise.resolve({ data: [] }) }); +}); + +describe('useFetchMetricMetadata', () => { + it('applies source query settings', async () => { + renderHook(() => useFetchMetricMetadata(COMMON_ARGS), { + wrapper: createWrapper(), + }); + + expect(await settingsSent()).toMatchObject({ + force_optimize_skip_unused_shards: '0', + }); + }); +}); diff --git a/packages/app/src/hooks/__tests__/useFetchMetricResourceAttrs.test.tsx b/packages/app/src/hooks/__tests__/useFetchMetricResourceAttrs.test.tsx new file mode 100644 index 0000000000..c9abdca6db --- /dev/null +++ b/packages/app/src/hooks/__tests__/useFetchMetricResourceAttrs.test.tsx @@ -0,0 +1,81 @@ +import React from 'react'; +import { + MetricsDataType, + SourceKind, + TMetricSource, +} from '@hyperdx/common-utils/dist/types'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { renderHook, waitFor } from '@testing-library/react'; + +import { useFetchMetricResourceAttrs } from '@/hooks/useFetchMetricResourceAttrs'; + +const query = jest.fn(); + +jest.mock('@/clickhouse', () => ({ + getClickhouseClient: () => ({ query }), +})); +jest.mock('@/queryAttribution', () => ({ + useMetadataQueryAttribution: () => ({}), +})); + +const SOURCE: TMetricSource = { + id: 'metrics', + name: 'Metrics', + kind: SourceKind.Metric, + connection: 'conn-1', + from: { databaseName: 'default', tableName: '' }, + timestampValueExpression: 'TimeUnix', + resourceAttributesExpression: 'ResourceAttributes', + metricTables: { + [MetricsDataType.Gauge]: 'otel_metrics_gauge', + [MetricsDataType.Sum]: '', + [MetricsDataType.Histogram]: '', + [MetricsDataType.ExponentialHistogram]: '', + [MetricsDataType.Summary]: '', + }, + querySettings: [ + { setting: 'force_optimize_skip_unused_shards', value: '0' }, + { setting: 'timeout_overflow_mode', value: 'throw' }, + ], +}; + +const COMMON_ARGS = { + databaseName: 'default', + metricType: MetricsDataType.Gauge, + metricName: 'up', + tableSource: SOURCE, +}; + +function createWrapper() { + const queryClient = new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }); + return function Wrapper({ children }: { children: React.ReactNode }) { + return ( + {children} + ); + }; +} + +async function settingsSent() { + await waitFor(() => expect(query).toHaveBeenCalled()); + return query.mock.calls[0][0].clickhouse_settings; +} + +beforeEach(() => { + query.mockReset(); + query.mockResolvedValue({ json: () => Promise.resolve({ data: [] }) }); +}); + +describe('useFetchMetricResourceAttrs', () => { + it('applies source query settings', async () => { + renderHook( + () => useFetchMetricResourceAttrs({ ...COMMON_ARGS, isSql: false }), + { wrapper: createWrapper() }, + ); + + expect(await settingsSent()).toMatchObject({ + force_optimize_skip_unused_shards: '0', + }); + }); +}); diff --git a/packages/app/src/hooks/__tests__/useMetricCatalog.test.ts b/packages/app/src/hooks/__tests__/useMetricCatalog.test.ts index 437521a83d..ddb44255a3 100644 --- a/packages/app/src/hooks/__tests__/useMetricCatalog.test.ts +++ b/packages/app/src/hooks/__tests__/useMetricCatalog.test.ts @@ -1,4 +1,30 @@ -import { clampCatalogDateRange } from '@/hooks/useMetricCatalog'; +import React from 'react'; +import { + MetricsDataType, + SourceKind, + TMetricSource, +} from '@hyperdx/common-utils/dist/types'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { renderHook, waitFor } from '@testing-library/react'; + +import { + clampCatalogDateRange, + useMetricCatalog, +} from '@/hooks/useMetricCatalog'; + +const query = jest.fn(); + +jest.mock('@/clickhouse', () => ({ + getClickhouseClient: () => ({ query }), +})); +jest.mock('@/hooks/useMetadata', () => ({ + useMetadataWithSettings: () => ({ + getColumns: () => Promise.resolve([{ name: 'MetricName' }]), + }), +})); +jest.mock('@/queryAttribution', () => ({ + useMetadataQueryAttribution: () => ({}), +})); const NOW = new Date('2026-08-22T12:00:00Z'); const at = (iso: string) => new Date(iso); @@ -54,3 +80,52 @@ describe('clampCatalogDateRange', () => { expect(end).toEqual(at('2026-08-10T00:00:00Z')); }); }); + +describe('useMetricCatalog', () => { + const source: TMetricSource = { + id: 'metrics', + name: 'Metrics', + kind: SourceKind.Metric, + connection: 'conn-1', + from: { databaseName: 'default', tableName: '' }, + timestampValueExpression: 'TimeUnix', + resourceAttributesExpression: 'ResourceAttributes', + metricTables: { + [MetricsDataType.Gauge]: 'otel_metrics_gauge', + [MetricsDataType.Sum]: '', + [MetricsDataType.Histogram]: '', + [MetricsDataType.ExponentialHistogram]: '', + [MetricsDataType.Summary]: '', + }, + querySettings: [ + { setting: 'force_optimize_skip_unused_shards', value: '0' }, + { setting: 'timeout_overflow_mode', value: 'throw' }, + ], + }; + + const wrapper = ({ children }: { children: React.ReactNode }) => + React.createElement( + QueryClientProvider, + { + client: new QueryClient({ + defaultOptions: { queries: { retry: false } }, + }), + }, + children, + ); + + beforeEach(() => { + query.mockReset(); + query.mockResolvedValue({ json: () => Promise.resolve({ data: [] }) }); + }); + + it('applies source query settings without overriding the timeout bound', async () => { + renderHook(() => useMetricCatalog({ source }), { wrapper }); + + await waitFor(() => expect(query).toHaveBeenCalled()); + expect(query.mock.calls[0][0].clickhouse_settings).toMatchObject({ + force_optimize_skip_unused_shards: '0', + timeout_overflow_mode: 'break', + }); + }); +}); diff --git a/packages/app/src/hooks/__tests__/useMetricNames.test.tsx b/packages/app/src/hooks/__tests__/useMetricNames.test.tsx index af63f169f2..deac5d1d5b 100644 --- a/packages/app/src/hooks/__tests__/useMetricNames.test.tsx +++ b/packages/app/src/hooks/__tests__/useMetricNames.test.tsx @@ -184,6 +184,37 @@ describe('useMetricNames', () => { ); }); + it('passes the source query settings to the index read', async () => { + const querySettings = [{ setting: 'max_threads', value: '2' }]; + + renderHook(() => useMetricNames({ ...METRIC_SOURCE, querySettings }), { + wrapper: createWrapper(), + }); + + await waitFor(() => + expect(streamDistinctIndexValues).toHaveBeenCalledWith( + expect.objectContaining({ querySettings }), + ), + ); + }); + + it('passes the source query settings to the exhaustive query', () => { + const querySettings = [ + { setting: 'force_optimize_skip_unused_shards', value: '0' }, + ]; + + renderHook( + () => + useMetricNames({ ...METRIC_SOURCE, querySettings }, undefined, 'up'), + { wrapper: createWrapper() }, + ); + + expect(useGetMetricNames).toHaveBeenCalledWith( + expect.objectContaining({ querySettings }), + expect.anything(), + ); + }); + it('does not enable the exhaustive query while the index read is healthy', async () => { renderHook(() => useMetricNames(METRIC_SOURCE), { wrapper: createWrapper(), diff --git a/packages/app/src/hooks/useAvailableMetricNames.ts b/packages/app/src/hooks/useAvailableMetricNames.ts index dbcb344da3..f4e46a1feb 100644 --- a/packages/app/src/hooks/useAvailableMetricNames.ts +++ b/packages/app/src/hooks/useAvailableMetricNames.ts @@ -56,6 +56,7 @@ export function useAvailableMetricNames({ groupBy: '', timestampValueExpression: metricSource.timestampValueExpression ?? '', connection: metricSource.connection, + source: metricSource.id, dateRange, }; }, [metricSource, gaugeTable, correlationWhere, metricNames, dateRange]); diff --git a/packages/app/src/hooks/useFetchMetricAttributeValues.tsx b/packages/app/src/hooks/useFetchMetricAttributeValues.tsx index e6493f925a..8b46592c2d 100644 --- a/packages/app/src/hooks/useFetchMetricAttributeValues.tsx +++ b/packages/app/src/hooks/useFetchMetricAttributeValues.tsx @@ -3,6 +3,7 @@ import { ResponseJSON, tableExpr, } from '@hyperdx/common-utils/dist/clickhouse'; +import { querySettingsToClickHouseSettings } from '@hyperdx/common-utils/dist/core/utils'; import { SourceKind, TMetricSource } from '@hyperdx/common-utils/dist/types'; import { useQuery } from '@tanstack/react-query'; @@ -97,6 +98,7 @@ export const useFetchMetricAttributeValues = ({ abort_signal: signal, connectionId: tableSource!.connection, clickhouse_settings: { + ...querySettingsToClickHouseSettings(tableSource?.querySettings), max_execution_time: 60, timeout_overflow_mode: 'break', }, diff --git a/packages/app/src/hooks/useFetchMetricMetadata.tsx b/packages/app/src/hooks/useFetchMetricMetadata.tsx index 612df30e50..23c7bd9abe 100644 --- a/packages/app/src/hooks/useFetchMetricMetadata.tsx +++ b/packages/app/src/hooks/useFetchMetricMetadata.tsx @@ -3,6 +3,7 @@ import { ResponseJSON, tableExpr, } from '@hyperdx/common-utils/dist/clickhouse'; +import { querySettingsToClickHouseSettings } from '@hyperdx/common-utils/dist/core/utils'; import { SourceKind, TMetricSource } from '@hyperdx/common-utils/dist/types'; import { useQuery } from '@tanstack/react-query'; @@ -49,7 +50,13 @@ export const useFetchMetricMetadata = ({ const attribution = useMetadataQueryAttribution(); return useQuery({ - queryKey: ['metric-metadata', databaseName, metricType, metricName], + queryKey: [ + 'metric-metadata', + databaseName, + metricType, + metricName, + tableSource?.querySettings, + ], queryFn: async ({ signal }) => { if (!shouldFetch || !metricName) { return null; @@ -72,6 +79,9 @@ export const useFetchMetricMetadata = ({ format: 'JSON', abort_signal: signal, connectionId: tableSource!.connection, + clickhouse_settings: querySettingsToClickHouseSettings( + tableSource?.querySettings, + ), }) .then(res => res.json())) as ResponseJSON; diff --git a/packages/app/src/hooks/useFetchMetricResourceAttrs.tsx b/packages/app/src/hooks/useFetchMetricResourceAttrs.tsx index d08955bfeb..63d815d19a 100644 --- a/packages/app/src/hooks/useFetchMetricResourceAttrs.tsx +++ b/packages/app/src/hooks/useFetchMetricResourceAttrs.tsx @@ -3,6 +3,7 @@ import { ResponseJSON, tableExpr, } from '@hyperdx/common-utils/dist/clickhouse'; +import { querySettingsToClickHouseSettings } from '@hyperdx/common-utils/dist/core/utils'; import { SourceKind, TMetricSource } from '@hyperdx/common-utils/dist/types'; import { useQuery } from '@tanstack/react-query'; @@ -187,6 +188,9 @@ export const useFetchMetricResourceAttrs = ({ format: 'JSON', abort_signal: signal, connectionId: tableSource!.connection, + clickhouse_settings: querySettingsToClickHouseSettings( + tableSource?.querySettings, + ), }) .then(res => res.json())) as ResponseJSON; diff --git a/packages/app/src/hooks/useMetadata.tsx b/packages/app/src/hooks/useMetadata.tsx index 3a206bec0b..494571e6a6 100644 --- a/packages/app/src/hooks/useMetadata.tsx +++ b/packages/app/src/hooks/useMetadata.tsx @@ -27,6 +27,7 @@ import { isLogSource, isTraceSource, MetadataMaterializedViews, + QuerySettings, } from '@hyperdx/common-utils/dist/types'; import { keepPreviousData, @@ -783,6 +784,7 @@ export function useGetMetricNames( dateRange, timestampValueExpression, namePattern, + querySettings, }: { databaseName: string; tableName: string; @@ -790,6 +792,7 @@ export function useGetMetricNames( dateRange: [Date, Date]; timestampValueExpression: string; namePattern?: string; + querySettings?: QuerySettings; }, options?: Partial>, ) { @@ -804,6 +807,7 @@ export function useGetMetricNames( dateRange, timestampValueExpression, namePattern, + querySettings, }, ], queryFn: async ({ signal }) => @@ -814,6 +818,7 @@ export function useGetMetricNames( dateRange, timestampValueExpression, namePattern, + querySettings, signal, }), // An empty table name means the source has no table for this metric kind. diff --git a/packages/app/src/hooks/useMetricCatalog.ts b/packages/app/src/hooks/useMetricCatalog.ts index ccec9e9ff1..b46f52a18e 100644 --- a/packages/app/src/hooks/useMetricCatalog.ts +++ b/packages/app/src/hooks/useMetricCatalog.ts @@ -6,6 +6,7 @@ import { ResponseJSON, tableExpr, } from '@hyperdx/common-utils/dist/clickhouse'; +import { querySettingsToClickHouseSettings } from '@hyperdx/common-utils/dist/core/utils'; import { DateRange, SourceKind, @@ -98,6 +99,7 @@ export function useMetricCatalog({ tables, timestampExpression, dateRange, + source.querySettings, ], queryFn: async ({ signal }) => { // dayjs() rather than `new Date()`: the lint rule bans the latter to stop @@ -149,6 +151,7 @@ export function useMetricCatalog({ abort_signal: signal, connectionId, clickhouse_settings: { + ...querySettingsToClickHouseSettings(source.querySettings), max_execution_time: MAX_EXEC_SECONDS, timeout_overflow_mode: 'break', }, diff --git a/packages/app/src/hooks/useMetricNames.ts b/packages/app/src/hooks/useMetricNames.ts index 611a9b04d4..692f2efa0d 100644 --- a/packages/app/src/hooks/useMetricNames.ts +++ b/packages/app/src/hooks/useMetricNames.ts @@ -36,6 +36,7 @@ const metricNamesQueryArgs = ({ connectionId: metricSource.connection, timestampValueExpression: metricSource.timestampValueExpression ?? '', dateRange: clampCatalogDateRange(dateRange, now), + querySettings: metricSource.querySettings, }; }; @@ -71,6 +72,7 @@ function useMetricNamesForKind( args.timestampValueExpression, args.dateRange[0].getTime(), args.dateRange[1].getTime(), + args.querySettings, ], // Not memoized: read when the query runs, not hashed into its key. streamFactory: ({ signal }) => @@ -81,6 +83,7 @@ function useMetricNamesForKind( connectionId: args.connectionId, dateRange: args.dateRange, timestampValueExpression: args.timestampValueExpression, + querySettings: args.querySettings, signal, }), enabled: isQueryable && !isSearching, diff --git a/packages/common-utils/src/__tests__/metadata.test.ts b/packages/common-utils/src/__tests__/metadata.test.ts index a9c6747277..4170e9f370 100644 --- a/packages/common-utils/src/__tests__/metadata.test.ts +++ b/packages/common-utils/src/__tests__/metadata.test.ts @@ -1382,6 +1382,21 @@ describe('Metadata', () => { ).toBeUndefined(); }); + it('applies source query settings without overriding the pinned ones', async () => { + await metadata.getMetricNames({ + ...baseArgs, + querySettings: [ + { setting: 'force_optimize_skip_unused_shards', value: '0' }, + { setting: 'timeout_overflow_mode', value: 'break' }, + ], + }); + + expect(lastQuery().clickhouse_settings).toMatchObject({ + force_optimize_skip_unused_shards: '0', + timeout_overflow_mode: 'throw', + }); + }); + it('rejects a non-positive limit', async () => { await expect( metadata.getMetricNames({ ...baseArgs, limit: 0 }), diff --git a/packages/common-utils/src/__tests__/streamDistinctIndexValues.test.ts b/packages/common-utils/src/__tests__/streamDistinctIndexValues.test.ts index 92b578a1e6..f0b6193fd6 100644 --- a/packages/common-utils/src/__tests__/streamDistinctIndexValues.test.ts +++ b/packages/common-utils/src/__tests__/streamDistinctIndexValues.test.ts @@ -91,6 +91,22 @@ describe('Metadata.streamDistinctIndexValues', () => { ]); }); + it('applies source query settings', async () => { + const metadata = buildMetadata(); + mockQuery.mockResolvedValue(resultSetOf([])); + + await drain( + metadata.streamDistinctIndexValues({ + ...ARGS, + querySettings: [{ setting: 'max_threads', value: '2' }], + }), + ); + + expect(mockQuery.mock.calls[0][0].clickhouse_settings).toMatchObject({ + max_threads: '2', + }); + }); + it('reads the primary index with DISTINCT and no ORDER BY', async () => { const metadata = buildMetadata(); mockQuery.mockResolvedValue(resultSetOf([])); diff --git a/packages/common-utils/src/core/metadata.ts b/packages/common-utils/src/core/metadata.ts index 999ab85cbd..2393b5eedf 100644 --- a/packages/common-utils/src/core/metadata.ts +++ b/packages/common-utils/src/core/metadata.ts @@ -36,6 +36,7 @@ import type { BuilderChartConfig, BuilderChartConfigWithDateRange, MetadataMaterializedViews, + QuerySettings, TSource, } from '@/types'; import { @@ -62,6 +63,7 @@ import { getTextIndexDictionaryBlockSize, MetadataMVQueryOptions, objectHash, + querySettingsToClickHouseSettings, splitAndTrimWithBracket, TextIndexColumnQueryOptions, TextIndexMapColumnQueryOptions, @@ -1106,6 +1108,7 @@ export class Metadata { dateRange, timestampValueExpression, limit = DEFAULT_MAX_INDEX_VALUES, + querySettings, signal, }: { databaseName: string; @@ -1117,6 +1120,7 @@ export class Metadata { dateRange?: [Date, Date]; timestampValueExpression?: string; limit?: number; + querySettings?: QuerySettings; signal?: AbortSignal; }): AsyncGenerator { const tableMetadata = await this.assertIndexReadable({ @@ -1163,7 +1167,10 @@ export class Metadata { query_params: sql.params, format: 'JSONEachRow', connectionId, - clickhouse_settings: this.getClickHouseSettings(), + clickhouse_settings: { + ...this.getClickHouseSettings(), + ...querySettingsToClickHouseSettings(querySettings), + }, abort_signal: signal, }); @@ -3512,6 +3519,7 @@ export class Metadata { timestampValueExpression, namePattern, limit = DEFAULT_METRIC_NAMES_LIMIT, + querySettings, signal, }: { databaseName: string; @@ -3521,6 +3529,7 @@ export class Metadata { timestampValueExpression: string; namePattern?: string; limit?: number; + querySettings?: QuerySettings; signal?: AbortSignal; }): Promise { if (!Number.isInteger(limit) || limit < 1) { @@ -3582,6 +3591,7 @@ export class Metadata { connectionId, clickhouse_settings: { ...this.getClickHouseSettings(), + ...querySettingsToClickHouseSettings(querySettings), // Bounded by wall clock, not rows: a row cap under `ORDER BY ... LIMIT` // is what made the old result an arbitrary subset, so capping rows here // would reintroduce the bug this method removes. `max_execution_time` diff --git a/packages/common-utils/src/core/utils.ts b/packages/common-utils/src/core/utils.ts index a582af9d01..55410f0308 100644 --- a/packages/common-utils/src/core/utils.ts +++ b/packages/common-utils/src/core/utils.ts @@ -1,4 +1,5 @@ // Port from ChartUtils + source.ts +import type { ClickHouseSettings } from '@clickhouse/client-common'; import { add as fnsAdd, format as fnsFormat } from 'date-fns'; import { formatInTimeZone, zonedTimeToUtc } from 'date-fns-tz'; import { omit } from 'lodash'; @@ -60,6 +61,15 @@ import { validateVariableReferencesInTemplate } from '@/variables'; import { SkipIndexMetadata, TableMetadata } from './metadata'; +/** Source `querySettings` in the shape the ClickHouse client expects. */ +export function querySettingsToClickHouseSettings( + querySettings: QuerySettings | undefined, +): ClickHouseSettings { + return Object.fromEntries( + (querySettings ?? []).map(({ setting, value }) => [setting, value]), + ); +} + /** The default maximum number of buckets setting when determining a bucket duration for 'auto' granularity */ export const DEFAULT_AUTO_GRANULARITY_MAX_BUCKETS = 60;