diff --git a/.changeset/sql-query-dashboard-filter-schema.md b/.changeset/sql-query-dashboard-filter-schema.md new file mode 100644 index 0000000000..affca36ff9 --- /dev/null +++ b/.changeset/sql-query-dashboard-filter-schema.md @@ -0,0 +1,9 @@ +--- +'@hyperdx/common-utils': patch +'@hyperdx/api': patch +'@hyperdx/app': patch +--- + +feat: add the SQL query dashboard filter type to the dashboard schema + +A `SQL_QUERY` filter stores a ClickHouse connection and a SQL template whose first column fills the filter's dropdown. It is variable-only. Validation flags templates that use `$__sourceTable` or `$__filters`, or whose macros and variable references can't expand. The filter is not yet exposed through the external API or MCP, and has no editor or values lookup yet. diff --git a/packages/api/src/utils/externalApi.ts b/packages/api/src/utils/externalApi.ts index 187abd7a24..c89015af03 100644 --- a/packages/api/src/utils/externalApi.ts +++ b/packages/api/src/utils/externalApi.ts @@ -5,6 +5,7 @@ import { isFilterBroadcastEnabled, isFilterRequired, isFilterVariableEnabled, + isSqlQueryFilter, } from '@hyperdx/common-utils/dist/filters'; import { AlertErrorType, @@ -241,7 +242,10 @@ export function translateExternalChartToTileConfig( } /** Filter types the external API and MCP don't expose yet. */ -type InternalOnlyDashboardFilter = Extract; +type InternalOnlyDashboardFilter = Extract< + DashboardFilter, + { type: 'ADHOC' | 'SQL_QUERY' } +>; export type ExternalCompatibleDashboardFilter = Exclude< DashboardFilter, InternalOnlyDashboardFilter @@ -253,7 +257,7 @@ export function getExternalCompatibleFilters( ): ExternalCompatibleDashboardFilter[] { return (filters ?? []).filter( (filter): filter is ExternalCompatibleDashboardFilter => - !isAdhocFilter(filter), + !isAdhocFilter(filter) && !isSqlQueryFilter(filter), ); } diff --git a/packages/app/src/DBDashboardImportPage.tsx b/packages/app/src/DBDashboardImportPage.tsx index 71fce453f0..21c5936fd1 100644 --- a/packages/app/src/DBDashboardImportPage.tsx +++ b/packages/app/src/DBDashboardImportPage.tsx @@ -22,6 +22,7 @@ import { isAdhocFilter, isPrometheusLabelFilter, isQueryExpressionFilter, + isSqlQueryFilter, QUERY_EXPRESSION_FILTER_SOURCE_KINDS, type SavedFilterValueIssue, type SavedQueryIssue, @@ -337,10 +338,17 @@ function FileSelection({ details={ {filterQueryWarnings.map(warning => ( - - {warning.filterName}: invalid {warning.language} query:{' '} - {warning.where} - + + + {warning.filterName}: invalid {warning.language} query:{' '} + {warning.where} + + {warning.detail && ( + + {warning.detail} + + )} + ))} } @@ -458,6 +466,8 @@ export function buildMappingFormSchema( connectionMappings: z.array(MappingValueSchema), /** A list of filter source mappings, ordered by input filter index */ filterSourceMappings: z.array(MappingValueSchema).optional(), + /** A list of filter connection mappings, ordered by input filter index. Only applicable for SQL query filters */ + filterConnectionMappings: z.array(MappingValueSchema).optional(), /** * Per-filter applies-to source mappings. Each entry is an array of mapped * source IDs in the same order as the filter's `appliesToSourceIds` names @@ -546,6 +556,17 @@ export function buildMappingFormSchema( return; } + if (isSqlQueryFilter(filter)) { + if (!data.filterConnectionMappings?.[idx]) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + path: ['filterConnectionMappings', idx], + message: 'Select a connection for this filter', + }); + } + return; + } + if (!isSourceBackedTemplateFilter(filter)) return; const mappedSourceId = data.filterSourceMappings?.[idx]; if (!mappedSourceId) { @@ -601,6 +622,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { tileSourceMappings: input.tiles.map(() => ''), connectionMappings: input.tiles.map(() => ''), filterSourceMappings: input.filters?.map(() => '') ?? [], + filterConnectionMappings: input.filters?.map(() => '') ?? [], filterAppliesToSourceMappings: input.filters?.map(() => []) ?? [], filterAdhocSourceMappings: input.filters?.map(() => []) ?? [], onClickSourceMappings: input.tiles.map(() => ''), @@ -631,15 +653,20 @@ export function Mapping({ input }: { input: DashboardTemplate }) { return match?.id || ''; }); + const findConnectionIdByName = (name: string) => + connections.find(conn => conn.name.toLowerCase() === name.toLowerCase()) + ?.id || ''; + const connectionMappings = input.tiles.map(tile => { const config = tile.config as SavedChartConfig; if (!isRawSqlSavedChartConfig(config)) return ''; - const match = connections.find( - conn => conn.name.toLowerCase() === config.connection.toLowerCase(), - ); - return match?.id || ''; + return findConnectionIdByName(config.connection); }); + const filterConnectionMappings = input.filters?.map(filter => + isSqlQueryFilter(filter) ? findConnectionIdByName(filter.connection) : '', + ); + const filterSourceMappings = input.filters?.map(filter => { const sourceName = sourceBackedTemplateFilter(filter)?.source; if (sourceName == null) return ''; @@ -698,6 +725,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { setValue('tileSourceMappings', tileSourceMappings); setValue('connectionMappings', connectionMappings); setValue('filterSourceMappings', filterSourceMappings); + setValue('filterConnectionMappings', filterConnectionMappings); multiMappingSlotsRef.current.clear(); filterAppliesToSourceMappings?.forEach((mapping, idx) => { setValue(`filterAppliesToSourceMappings.${idx}`, mapping); @@ -720,6 +748,10 @@ export function Mapping({ input }: { input: DashboardTemplate }) { name: 'onClickSourceMappings', }); const connectionMappings = useWatch({ control, name: 'connectionMappings' }); + const filterConnectionMappings = useWatch({ + control, + name: 'filterConnectionMappings', + }); const onClickDashboardMappings = useWatch({ control, name: 'onClickDashboardMappings', @@ -728,6 +760,7 @@ export function Mapping({ input }: { input: DashboardTemplate }) { const prevFilterSourceMappingsRef = useRef(filterSourceMappings); const prevOnClickSourceMappingsRef = useRef(onClickSourceMappings); const prevConnectionMappingsRef = useRef(connectionMappings); + const prevFilterConnectionMappingsRef = useRef(filterConnectionMappings); const prevOnClickDashboardMappingsRef = useRef(onClickDashboardMappings); // Propagate source mapping changes to other tiles/filters/onClicks with the @@ -883,26 +916,49 @@ export function Mapping({ input }: { input: DashboardTemplate }) { sources, ]); - // Propagate connection mapping changes to other RawSQL tiles with the same input connection + // Propagate connection mapping changes to other RawSQL tiles and SQL query + // filters with the same input connection useEffect(() => { if (isUpdatingRef.current) return; - if (!connectionMappings || !input.tiles) return; - - const changedIdx = connectionMappings.findIndex( - (mapping, idx) => - mapping !== prevConnectionMappingsRef.current?.[idx] && - getFieldState(`connectionMappings.${idx}`).isDirty, - ); - if (changedIdx === -1) return; + if (!input.tiles) return; - prevConnectionMappingsRef.current = connectionMappings; + let inputConnectionName: string | undefined; + let connectionId = ''; + + const tileIdx = + connectionMappings?.findIndex( + (mapping, idx) => + mapping !== prevConnectionMappingsRef.current?.[idx] && + getFieldState(`connectionMappings.${idx}`).isDirty, + ) ?? -1; + if (connectionMappings && tileIdx !== -1) { + prevConnectionMappingsRef.current = connectionMappings; + const config = input.tiles[tileIdx]?.config as + | SavedChartConfig + | undefined; + if (config && isRawSqlSavedChartConfig(config)) { + inputConnectionName = config.connection; + connectionId = connectionMappings[tileIdx] ?? ''; + } + } - const inputTile = input.tiles[changedIdx]; - const inputTileConfig = inputTile?.config as SavedChartConfig | undefined; - if (!inputTileConfig || !isRawSqlSavedChartConfig(inputTileConfig)) return; + if (inputConnectionName == null && filterConnectionMappings) { + const filterIdx = filterConnectionMappings.findIndex( + (mapping, idx) => + mapping !== prevFilterConnectionMappingsRef.current?.[idx] && + getFieldState(`filterConnectionMappings.${idx}`).isDirty, + ); + if (filterIdx !== -1) { + prevFilterConnectionMappingsRef.current = filterConnectionMappings; + const filter = input.filters?.[filterIdx]; + if (filter && isSqlQueryFilter(filter)) { + inputConnectionName = filter.connection; + connectionId = filterConnectionMappings[filterIdx] ?? ''; + } + } + } - const connectionId = connectionMappings[changedIdx] ?? ''; - const inputTileConnection = inputTileConfig.connection; + if (inputConnectionName == null) return; const keysForTilesWithMatchingConnection = input.tiles .map((tile, index) => ({ @@ -912,18 +968,38 @@ export function Mapping({ input }: { input: DashboardTemplate }) { .filter( tile => isRawSqlSavedChartConfig(tile.config) && - tile.config.connection === inputTileConnection, + tile.config.connection === inputConnectionName, ) .map(({ index }) => `connectionMappings.${index}` as const); + const keysForFiltersWithMatchingConnection = + input.filters + ?.map((filter, index) => ({ filter, index })) + .filter( + ({ filter }) => + isSqlQueryFilter(filter) && + filter.connection === inputConnectionName, + ) + .map(({ index }) => `filterConnectionMappings.${index}` as const) ?? []; + isUpdatingRef.current = true; - for (const key of keysForTilesWithMatchingConnection) { + for (const key of [ + ...keysForTilesWithMatchingConnection, + ...keysForFiltersWithMatchingConnection, + ]) { if (!getFieldState(key).isDirty) { setValue(key, connectionId, { shouldValidate: true }); } } isUpdatingRef.current = false; - }, [connectionMappings, input.tiles, getFieldState, setValue]); + }, [ + connectionMappings, + filterConnectionMappings, + input.tiles, + input.filters, + getFieldState, + setValue, + ]); // Propagate dashboard mapping changes to other tiles whose onClick targets // the same input dashboard. Dashboard-type onClicks in a template store the @@ -1054,6 +1130,12 @@ export function Mapping({ input }: { input: DashboardTemplate }) { appliesToSourceIds, }; } + if (isSqlQueryFilter(filter)) { + const connection = findConnection( + data.filterConnectionMappings?.[idx], + ); + return { ...filter, connection: connection!.id }; + } const source = findSource(data.filterSourceMappings?.[idx]); return { ...filter, @@ -1256,6 +1338,24 @@ export function Mapping({ input }: { input: DashboardTemplate }) { )} + {isSqlQueryFilter(filter) && ( + + {filter.name} (Filter) + Data connection + {filter.connection} + + ({ + value: conn.id, + label: conn.name, + }))} + placeholder="Select a connection" + /> + + + )} {isAdhocFilter(filter) && ( {filter.name} (Filter) diff --git a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx index fee5937fd7..664064e767 100644 --- a/packages/app/src/__tests__/DBDashboardImportPage.test.tsx +++ b/packages/app/src/__tests__/DBDashboardImportPage.test.tsx @@ -265,6 +265,24 @@ const unresolvedConnectionTemplate = DashboardTemplateSchema.parse({ ], }); +const sqlQueryFilterTemplate = (connection: string) => + DashboardTemplateSchema.parse({ + version: '0.1.0', + name: 'SQL Query Filter', + tiles: [builderTile('tile-number', 'number', 'Metrics', 0)], + filters: [ + { + id: 'filter-env', + type: 'SQL_QUERY', + name: 'Environment', + connection, + sqlTemplate: 'SELECT DISTINCT env FROM envs', + isBroadcastEnabled: false, + isVariableEnabled: true, + }, + ], + }); + // A markdown tile renders static content, so its source is not worth blocking // the import over even when the template names one that no longer exists. const markdownStaleSourceTemplate = DashboardTemplateSchema.parse({ @@ -389,6 +407,44 @@ describe('Dashboard import - PromQL label filter source', () => { }); }); +describe('Dashboard import - SQL query filter connection', () => { + it('auto-maps the connection by name and imports it as an id', async () => { + renderWithMantine(); + + const finish = await screen.findByRole('button', { + name: /finish import/i, + }); + await act(async () => { + fireEvent.click(finish); + }); + + await waitFor(() => expect(mockMutateAsync).toHaveBeenCalledTimes(1)); + const payload = mockMutateAsync.mock.calls[0][0]; + expect(DashboardWithoutIdSchema.safeParse(payload).success).toBe(true); + expect(payload.filters[0]).toMatchObject({ + type: 'SQL_QUERY', + connection: 'conn-default', + }); + }); + + it('blocks the import and flags the filter row when its connection is unmapped', async () => { + renderWithMantine(); + + await act(async () => { + fireEvent.click( + await screen.findByRole('button', { name: /finish import/i }), + ); + }); + + await waitFor(() => + expect( + screen.getByText('Select a connection for this filter'), + ).toBeInTheDocument(), + ); + expect(mockMutateAsync).not.toHaveBeenCalled(); + }); +}); + describe('Dashboard import - queried filter source is required', () => { it('blocks the import and flags the filter row when its source is unmapped', async () => { renderWithMantine(); diff --git a/packages/app/src/components/DashboardFiltersModal/DashboardFiltersList.tsx b/packages/app/src/components/DashboardFiltersModal/DashboardFiltersList.tsx index 22e99756ed..e2d1a64a11 100644 --- a/packages/app/src/components/DashboardFiltersModal/DashboardFiltersList.tsx +++ b/packages/app/src/components/DashboardFiltersModal/DashboardFiltersList.tsx @@ -23,6 +23,7 @@ import { IconPencil, IconRefresh, IconSearch, + IconSql, IconTrash, } from '@tabler/icons-react'; @@ -91,6 +92,12 @@ function getValuesAttribute( tooltip: 'Sources the condition keys and values come from', label: getSourceNames(filter.sources, sources), }; + case 'SQL_QUERY': + return { + icon: , + tooltip: 'The dropdown values come from a SQL query', + label: 'SQL query', + }; default: filter satisfies never; // exhaustive check return { icon: , label: '' }; diff --git a/packages/app/src/components/DashboardFiltersModal/__tests__/filterFormState.test.ts b/packages/app/src/components/DashboardFiltersModal/__tests__/filterFormState.test.ts index d0e3131fbe..699f02bba5 100644 --- a/packages/app/src/components/DashboardFiltersModal/__tests__/filterFormState.test.ts +++ b/packages/app/src/components/DashboardFiltersModal/__tests__/filterFormState.test.ts @@ -326,7 +326,7 @@ describe('toSavedFilter', () => { /** The minimum a form needs to save, per filter type the form supports. */ const byType: Record< - Exclude, + Exclude, Partial > = { QUERY_EXPRESSION: { diff --git a/packages/common-utils/src/__tests__/filters.test.ts b/packages/common-utils/src/__tests__/filters.test.ts index 0f26d14929..c6cd27dca1 100644 --- a/packages/common-utils/src/__tests__/filters.test.ts +++ b/packages/common-utils/src/__tests__/filters.test.ts @@ -44,6 +44,7 @@ import type { Filter, PromqlConditionFilter, QueryExpressionDashboardFilter, + SqlQueryDashboardFilter, StaticListDashboardFilter, } from '@/types'; import { @@ -740,6 +741,74 @@ describe('filters', () => { ).toEqual([]); }); + describe('SQL query filters', () => { + const sqlFilter = (sqlTemplate: string): SqlQueryDashboardFilter => ({ + id: 'sql', + type: 'SQL_QUERY', + name: 'Environment', + connection: 'conn-1', + sqlTemplate, + isBroadcastEnabled: false, + isVariableEnabled: true, + }); + const cloud = filter({ + id: 'cloud', + name: 'cloud', + isVariableEnabled: true, + }); + + it('accepts time macros and references to other variables', () => { + expect( + validateDashboardFilterQueries([ + cloud, + sqlFilter( + 'SELECT env FROM envs WHERE $__timeFilter(ts) AND $__filter(cloud, $cloud) AND region IN ($cloud)', + ), + ]), + ).toEqual([]); + }); + + // Running it is the only reliable check of ClickHouse syntax. + it('does not parse the SQL itself', () => { + expect( + validateDashboardFilterQueries([sqlFilter('SELEC env FRM')]), + ).toEqual([]); + }); + + it.each([ + ['$__sourceTable', 'SELECT env FROM $__sourceTable'], + ['$__filters', 'SELECT env FROM envs WHERE $__filters'], + ])( + 'flags %s, which needs a source the filter does not have', + (macro, sql) => { + expect(validateDashboardFilterQueries([sqlFilter(sql)])).toEqual([ + { + filterId: 'sql', + filterName: 'Environment', + language: 'sql', + where: sql, + detail: `SQL query filters have no source, so ${macro} cannot be used`, + }, + ]); + }, + ); + + it.each([ + ['an undeclared variable macro', 'WHERE $__filter(env, $missing)'], + ['an unterminated macro', 'WHERE $__timeFilter(ts'], + ['an unknown variable format', 'WHERE env IN (${cloud:bogus})'], + ])('flags %s', (_label, sql) => { + const issues = validateDashboardFilterQueries([cloud, sqlFilter(sql)]); + + expect(issues).toHaveLength(1); + expect(issues[0]).toMatchObject({ + filterId: 'sql', + where: sql, + detail: expect.any(String), + }); + }); + }); + it('accepts a valid lucene where clause', () => { expect( validateDashboardFilterQueries([ diff --git a/packages/common-utils/src/__tests__/types.test.ts b/packages/common-utils/src/__tests__/types.test.ts index 1640578a2e..4f5b3b2906 100644 --- a/packages/common-utils/src/__tests__/types.test.ts +++ b/packages/common-utils/src/__tests__/types.test.ts @@ -678,6 +678,48 @@ describe('DashboardFilterSchema static-list variant', () => { }); }); +describe('DashboardFilterSchema SQL query variant', () => { + const sqlFilter = { + id: 'f1', + type: 'SQL_QUERY' as const, + name: 'Environment', + connection: 'conn-1', + sqlTemplate: 'SELECT DISTINCT env FROM envs', + isBroadcastEnabled: false, + isVariableEnabled: true, + variableName: 'env', + }; + + it('parses a SQL query filter', () => { + expect(DashboardFilterSchema.parse(sqlFilter)).toEqual(sqlFilter); + }); + + it.each(['connection', 'sqlTemplate'] as const)( + 'requires a non-empty %s', + field => { + for (const value of [undefined, '']) { + expect( + DashboardFilterSchema.safeParse({ ...sqlFilter, [field]: value }) + .success, + ).toBe(false); + } + }, + ); + + it.each([ + ['broadcast enabled', { isBroadcastEnabled: true }], + ['variables disabled', { isVariableEnabled: false }], + ])('rejects a SQL query filter with %s', (_label, overrides) => { + const result = DashboardFilterSchema.safeParse({ + ...sqlFilter, + ...overrides, + }); + + expect(result.success).toBe(false); + expect(result.error?.issues[0].path).toEqual([Object.keys(overrides)[0]]); + }); +}); + describe('DashboardFilterSchema query-expression variant', () => { const queryFilter = { id: 'f1', diff --git a/packages/common-utils/src/__tests__/utils.test.ts b/packages/common-utils/src/__tests__/utils.test.ts index 327585152b..0f6687e09b 100644 --- a/packages/common-utils/src/__tests__/utils.test.ts +++ b/packages/common-utils/src/__tests__/utils.test.ts @@ -1606,6 +1606,39 @@ describe('utils', () => { ).toEqual(staticFilter); }); + it('should export a SQL query filter with its connection id remapped to a name', () => { + const sqlFilter = { + id: 'filter-sql', + type: 'SQL_QUERY' as const, + name: 'Environment', + connection: 'conn1', + sqlTemplate: 'SELECT DISTINCT env FROM envs', + isBroadcastEnabled: false as const, + isVariableEnabled: true as const, + }; + const dashboard: z.infer = { + id: 'dashboard1', + name: 'SQL Filter Dashboard', + tags: [], + tiles: [], + filters: [sqlFilter], + }; + const connections: Connection[] = [ + { + id: 'conn1', + name: 'Production DB', + host: 'http://localhost:8123', + username: 'default', + }, + ]; + + const template = convertToDashboardTemplate(dashboard, [], connections); + + expect(template.filters).toEqual([ + { ...sqlFilter, connection: 'Production DB' }, + ]); + }); + it('should convert a dashboard without filters to a dashboard template', () => { const dashboard: z.infer = { id: 'dashboard1', diff --git a/packages/common-utils/src/core/utils.ts b/packages/common-utils/src/core/utils.ts index 7c93078458..b3e4546e3c 100644 --- a/packages/common-utils/src/core/utils.ts +++ b/packages/common-utils/src/core/utils.ts @@ -9,6 +9,7 @@ export { default as objectHash } from 'object-hash'; import { isAdhocFilter, isQueryExpressionFilter, + isSqlQueryFilter, isStaticListFilter, } from '@/filters'; import { @@ -944,12 +945,19 @@ export function convertToDashboardTemplate( const convertToFilterTemplate = ( input: DashboardFilter, sources: TSource[], + connections: Connection[], ): DashboardFilter => { const filter = DashboardFilterSchema.parse(structuredClone(input)); // A static filter references nothing in the workspace if (isStaticListFilter(filter)) return filter; + if (isSqlQueryFilter(filter)) { + filter.connection = + connections.find(conn => conn.id === filter.connection)?.name ?? ''; + return filter; + } + const getSourceName = (id: string) => sources.find(source => source.id === id)?.name ?? ''; @@ -975,7 +983,9 @@ export function convertToDashboardTemplate( if (input.filters) { output.filters = []; for (const filter of input.filters ?? []) { - output.filters.push(convertToFilterTemplate(filter, sources)); + output.filters.push( + convertToFilterTemplate(filter, sources, connections), + ); } } diff --git a/packages/common-utils/src/filters.ts b/packages/common-utils/src/filters.ts index 3412e16d8c..260b8735ac 100644 --- a/packages/common-utils/src/filters.ts +++ b/packages/common-utils/src/filters.ts @@ -1,6 +1,7 @@ import * as SQLParser from 'node-sql-parser'; import { escapeSqlString, replaceJsonExpressions } from '@/core/utils'; +import { getSourceDependentMacrosUsed, replaceMacros } from '@/macros'; import { parse } from '@/queryParser'; import { AdhocDashboardFilter, @@ -770,7 +771,7 @@ export type DashboardFilterQueryIssue = { filterName: string; /** Query language of the filter's `where` clause */ language: 'lucene' | 'sql'; - /** The raw `where` clause that failed to parse */ + /** The raw `where` clause, or a SQL query filter's template, that failed to validate */ where: string; /** Why it failed, when the clause itself parses but its variables don't resolve. */ detail?: string; @@ -803,6 +804,23 @@ export function validateDashboardFilterQueries( ).map(declaration => ({ ...declaration, values: [] })); for (const filter of filters) { + if (isSqlQueryFilter(filter)) { + const detail = validateSqlQueryFilterTemplate( + filter.sqlTemplate, + variables, + ); + if (detail) { + issues.push({ + filterId: filter.id, + filterName: filter.name, + language: 'sql', + where: filter.sqlTemplate, + detail, + }); + } + continue; + } + // Only ClickHouse-queried filters carry a values query to validate. // A PromQL ad hoc filter may store a `where`, but nothing reads it. const isSqlAdhoc = isAdhocFilter(filter) && filter.sourceType === 'sql'; @@ -832,6 +850,28 @@ export function validateDashboardFilterQueries( return issues; } +/** + * Why a SQL query filter's template can't expand, or undefined when it can. + * The SQL itself is not parsed: ClickHouse syntax is only checked when it runs. + */ +export function validateSqlQueryFilterTemplate( + sqlTemplate: string, + variables: ChartVariable[], +): string | undefined { + try { + const sourceMacros = getSourceDependentMacrosUsed(sqlTemplate); + if (sourceMacros.length > 0) { + return `SQL query filters have no source, so ${sourceMacros + .map(macro => `$__${macro}`) + .join(' and ')} cannot be used`; + } + replaceMacros({ sqlTemplate, variables }); + return undefined; + } catch (e) { + return e instanceof Error ? e.message : String(e); + } +} + /** Derive the default `variableName` for a filter from its display name. */ export function deriveVariableName(filterName: string): string { const sanitized = filterName @@ -938,6 +978,13 @@ export function isAdhocFilter( return filter.type === 'ADHOC'; } +/** Type guard for SQL_QUERY type filters. */ +export function isSqlQueryFilter( + filter: T, +): filter is Extract { + return filter.type === 'SQL_QUERY'; +} + /** * The sources an ADHOC filter broadcasts its conditions to: its * `appliesToSourceIds`, or its own `sources` when that is unset or empty. diff --git a/packages/common-utils/src/types.ts b/packages/common-utils/src/types.ts index d21d64df04..75b4dda70c 100644 --- a/packages/common-utils/src/types.ts +++ b/packages/common-utils/src/types.ts @@ -2301,6 +2301,7 @@ export const DashboardFilterType = z.enum([ 'STATIC_LIST', 'PROMETHEUS_LABEL', 'ADHOC', + 'SQL_QUERY', ]); /** Allowed variable names for dashboard filters. Alphanumeric + underscore, must start with a letter. */ @@ -2409,6 +2410,21 @@ export const PromqlLabelDashboardFilterSchema = isVariableEnabled: z.literal(true), }); +/** + * A filter whose dropdown lists the first column returned by a free-form + * ClickHouse query. The query can use the time macros and other filters' + * variables, but not `$__sourceTable` or `$__filters`, as it has no source. + */ +export const SqlQueryDashboardFilterSchema = dashboardFilterBaseSchema.extend({ + type: z.literal(DashboardFilterType.enum.SQL_QUERY), + /** ID of the ClickHouse connection the query runs on. */ + connection: z.string().min(1), + sqlTemplate: z.string().min(1), + // Variable-only: there is no column to broadcast + isBroadcastEnabled: z.literal(false), + isVariableEnabled: z.literal(true), +}); + /** * A filter whose conditions (`key operator value`) are picked freely from the * keys of its sources: columns and map keys for SQL sources, labels for PromQL @@ -2445,6 +2461,7 @@ export const DashboardFilterSchema = z.discriminatedUnion('type', [ StaticListDashboardFilterSchema, PromqlLabelDashboardFilterSchema, AdhocDashboardFilterSchema, + SqlQueryDashboardFilterSchema, ]); export type QueryExpressionDashboardFilter = z.infer< @@ -2457,6 +2474,9 @@ export type PromqlLabelDashboardFilter = z.infer< typeof PromqlLabelDashboardFilterSchema >; export type AdhocDashboardFilter = z.infer; +export type SqlQueryDashboardFilter = z.infer< + typeof SqlQueryDashboardFilterSchema +>; export type DashboardFilter = z.infer; export enum PresetDashboard {