|
1 | 1 | import * as SQLParser from 'node-sql-parser'; |
2 | 2 |
|
3 | 3 | import { escapeSqlString, replaceJsonExpressions } from '@/core/utils'; |
| 4 | +import { getSourceDependentMacrosUsed, replaceMacros } from '@/macros'; |
4 | 5 | import { parse } from '@/queryParser'; |
5 | 6 | import { |
6 | 7 | AdhocDashboardFilter, |
@@ -770,7 +771,7 @@ export type DashboardFilterQueryIssue = { |
770 | 771 | filterName: string; |
771 | 772 | /** Query language of the filter's `where` clause */ |
772 | 773 | language: 'lucene' | 'sql'; |
773 | | - /** The raw `where` clause that failed to parse */ |
| 774 | + /** The raw `where` clause, or a SQL query filter's template, that failed to validate */ |
774 | 775 | where: string; |
775 | 776 | /** Why it failed, when the clause itself parses but its variables don't resolve. */ |
776 | 777 | detail?: string; |
@@ -803,6 +804,23 @@ export function validateDashboardFilterQueries( |
803 | 804 | ).map(declaration => ({ ...declaration, values: [] })); |
804 | 805 |
|
805 | 806 | for (const filter of filters) { |
| 807 | + if (isSqlQueryFilter(filter)) { |
| 808 | + const detail = validateSqlQueryFilterTemplate( |
| 809 | + filter.sqlTemplate, |
| 810 | + variables, |
| 811 | + ); |
| 812 | + if (detail) { |
| 813 | + issues.push({ |
| 814 | + filterId: filter.id, |
| 815 | + filterName: filter.name, |
| 816 | + language: 'sql', |
| 817 | + where: filter.sqlTemplate, |
| 818 | + detail, |
| 819 | + }); |
| 820 | + } |
| 821 | + continue; |
| 822 | + } |
| 823 | + |
806 | 824 | // Only ClickHouse-queried filters carry a values query to validate. |
807 | 825 | // A PromQL ad hoc filter may store a `where`, but nothing reads it. |
808 | 826 | const isSqlAdhoc = isAdhocFilter(filter) && filter.sourceType === 'sql'; |
@@ -832,6 +850,28 @@ export function validateDashboardFilterQueries( |
832 | 850 | return issues; |
833 | 851 | } |
834 | 852 |
|
| 853 | +/** |
| 854 | + * Why a SQL query filter's template can't expand, or undefined when it can. |
| 855 | + * The SQL itself is not parsed: ClickHouse syntax is only checked when it runs. |
| 856 | + */ |
| 857 | +function validateSqlQueryFilterTemplate( |
| 858 | + sqlTemplate: string, |
| 859 | + variables: ChartVariable[], |
| 860 | +): string | undefined { |
| 861 | + try { |
| 862 | + const sourceMacros = getSourceDependentMacrosUsed(sqlTemplate); |
| 863 | + if (sourceMacros.length > 0) { |
| 864 | + return `SQL query filters have no source, so ${sourceMacros |
| 865 | + .map(macro => `$__${macro}`) |
| 866 | + .join(' and ')} cannot be used`; |
| 867 | + } |
| 868 | + replaceMacros({ sqlTemplate, variables }); |
| 869 | + return undefined; |
| 870 | + } catch (e) { |
| 871 | + return e instanceof Error ? e.message : String(e); |
| 872 | + } |
| 873 | +} |
| 874 | + |
835 | 875 | /** Derive the default `variableName` for a filter from its display name. */ |
836 | 876 | export function deriveVariableName(filterName: string): string { |
837 | 877 | const sanitized = filterName |
@@ -938,6 +978,13 @@ export function isAdhocFilter<T extends { type: DashboardFilterKind }>( |
938 | 978 | return filter.type === 'ADHOC'; |
939 | 979 | } |
940 | 980 |
|
| 981 | +/** Type guard for SQL_QUERY type filters. */ |
| 982 | +export function isSqlQueryFilter<T extends { type: DashboardFilterKind }>( |
| 983 | + filter: T, |
| 984 | +): filter is Extract<T, { type: 'SQL_QUERY' }> { |
| 985 | + return filter.type === 'SQL_QUERY'; |
| 986 | +} |
| 987 | + |
941 | 988 | /** |
942 | 989 | * The sources an ADHOC filter broadcasts its conditions to: its |
943 | 990 | * `appliesToSourceIds`, or its own `sources` when that is unset or empty. |
|
0 commit comments