From 8f538269cf7f494988a0909bc5c8fafbf694f2d6 Mon Sep 17 00:00:00 2001 From: Praveen K B Date: Wed, 11 Nov 2026 15:18:48 +0530 Subject: [PATCH 1/2] fix: support legacy query editor stream from SQL --- src/components/QueryEditor.tsx | 14 +++++++++++++- src/datasource.ts | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/components/QueryEditor.tsx b/src/components/QueryEditor.tsx index db9341b..cb0429d 100644 --- a/src/components/QueryEditor.tsx +++ b/src/components/QueryEditor.tsx @@ -235,8 +235,12 @@ export const QueryEditor: ComponentType = ({ datasource, onChange, onRunQ const filters = query.filters || []; const selectedColumns = query.selectedColumns || []; + // Queries saved before v2 may only contain queryText. The backend can run + // them by deriving the stream from SQL, but the editor needs a selected + // stream before it renders Builder or Code mode. + const queryStream = query.stream || datasource.extractStreamName(query.queryText || '') || undefined; const [selectedStream, setSelectedStream] = useState>( - query.stream ? { label: query.stream, value: query.stream } : ({} as SelectableValue) + queryStream ? { label: queryStream, value: queryStream } : ({} as SelectableValue) ); const [schemaFields, setSchemaFields] = useState([]); const [stats, setStats] = useState({}); @@ -258,6 +262,14 @@ export const QueryEditor: ComponentType = ({ datasource, onChange, onRunQ const [promMetricNames, setPromMetricNames] = useState([]); const [promMetadata, setPromMetadata] = useState>({}); + // Grafana can replace a query target without remounting this component. + // Hydrate the editor when that replacement contains legacy SQL. + useEffect(() => { + if (queryStream && queryStream !== selectedStream?.value) { + setSelectedStream({ label: queryStream, value: queryStream }); + } + }, [queryStream, selectedStream?.value]); + // Build fieldTypeMap and fieldNames from schema (like Prism's setStreamSchema) const fieldTypeMap: FieldTypeMap = useMemo(() => buildFieldTypeMap(schemaFields), [schemaFields]); const fieldNames: string[] = useMemo(() => schemaFields.map((f) => f.name), [schemaFields]); diff --git a/src/datasource.ts b/src/datasource.ts index e71d450..28eb082 100644 --- a/src/datasource.ts +++ b/src/datasource.ts @@ -62,10 +62,10 @@ export class DataSource extends DataSourceWithBackend { - const tableRegex = /from\s+([\w\.]+)/i; + const tableRegex = /\bfrom\s+(?:"([^"]+)"|`([^`]+)`|([\w.]+))/i; const match = sqlQuery.match(tableRegex); if (match) { - return match[1]; + return match[1] || match[2] || match[3]; } return null; From a79f88a59936ae52cbf8ed2e0515f718c1bbbb5b Mon Sep 17 00:00:00 2001 From: Praveen K B Date: Wed, 11 Nov 2026 15:26:44 +0530 Subject: [PATCH 2/2] fix: Fixed review comments --- src/components/QueryEditor.tsx | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/components/QueryEditor.tsx b/src/components/QueryEditor.tsx index cb0429d..c2d9e07 100644 --- a/src/components/QueryEditor.tsx +++ b/src/components/QueryEditor.tsx @@ -223,7 +223,14 @@ export const QueryEditor: ComponentType = ({ datasource, onChange, onRunQ // Run queries button. Explore already has Grafana's top-level Run button, // and Alerting uses its own preview / evaluate controls. const isDashboard = app === CoreApp.Dashboard || app === CoreApp.PanelEditor; - const rawEditorMode = query.editorMode || datasource.defaultEditorMode || 'builder'; + const inferredStream = datasource.extractStreamName(query.queryText || '') || undefined; + const queryStream = query.stream || inferredStream; + // Legacy SQL has no structured Builder state. Opening it in Builder would + // show empty controls; the first edit would then rebuild and truncate SQL. + // Preserve explicit modes, but default unstructured legacy SQL to Code. + const isLegacySqlQuery = !query.stream && Boolean(inferredStream) && Boolean(query.queryText?.trim()); + const rawEditorMode = + query.editorMode || (isLegacySqlQuery ? 'code' : datasource.defaultEditorMode || 'builder'); // Auto-run disabled across all surfaces (Explore, Dashboard/Panel-edit, // Alerting). User explicitly triggers execution via Grafana's Run button // (Explore), the local Run queries button (Dashboard), or the preview / @@ -238,7 +245,6 @@ export const QueryEditor: ComponentType = ({ datasource, onChange, onRunQ // Queries saved before v2 may only contain queryText. The backend can run // them by deriving the stream from SQL, but the editor needs a selected // stream before it renders Builder or Code mode. - const queryStream = query.stream || datasource.extractStreamName(query.queryText || '') || undefined; const [selectedStream, setSelectedStream] = useState>( queryStream ? { label: queryStream, value: queryStream } : ({} as SelectableValue) ); @@ -263,12 +269,17 @@ export const QueryEditor: ComponentType = ({ datasource, onChange, onRunQ const [promMetadata, setPromMetadata] = useState>({}); // Grafana can replace a query target without remounting this component. - // Hydrate the editor when that replacement contains legacy SQL. + // Hydrate both local editor state and the persisted query. PromQL execution + // reads query.stream directly, so keeping the inferred stream local would + // make a legacy SQL query fail after switching a metrics dataset to PromQL. useEffect(() => { if (queryStream && queryStream !== selectedStream?.value) { setSelectedStream({ label: queryStream, value: queryStream }); } - }, [queryStream, selectedStream?.value]); + if (!query.stream && inferredStream) { + onChange({ ...query, stream: inferredStream }); + } + }, [query, queryStream, inferredStream, selectedStream?.value, onChange]); // Build fieldTypeMap and fieldNames from schema (like Prism's setStreamSchema) const fieldTypeMap: FieldTypeMap = useMemo(() => buildFieldTypeMap(schemaFields), [schemaFields]);