Skip to content

Commit d581f7a

Browse files
authored
fix: support legacy query editor stream from SQL (#61)
1 parent ccc4cea commit d581f7a

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎src/components/QueryEditor.tsx‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,14 @@ export const QueryEditor: ComponentType<Props> = ({ datasource, onChange, onRunQ
223223
// Run queries button. Explore already has Grafana's top-level Run button,
224224
// and Alerting uses its own preview / evaluate controls.
225225
const isDashboard = app === CoreApp.Dashboard || app === CoreApp.PanelEditor;
226-
const rawEditorMode = query.editorMode || datasource.defaultEditorMode || 'builder';
226+
const inferredStream = datasource.extractStreamName(query.queryText || '') || undefined;
227+
const queryStream = query.stream || inferredStream;
228+
// Legacy SQL has no structured Builder state. Opening it in Builder would
229+
// show empty controls; the first edit would then rebuild and truncate SQL.
230+
// Preserve explicit modes, but default unstructured legacy SQL to Code.
231+
const isLegacySqlQuery = !query.stream && Boolean(inferredStream) && Boolean(query.queryText?.trim());
232+
const rawEditorMode =
233+
query.editorMode || (isLegacySqlQuery ? 'code' : datasource.defaultEditorMode || 'builder');
227234
// Auto-run disabled across all surfaces (Explore, Dashboard/Panel-edit,
228235
// Alerting). User explicitly triggers execution via Grafana's Run button
229236
// (Explore), the local Run queries button (Dashboard), or the preview /
@@ -235,8 +242,11 @@ export const QueryEditor: ComponentType<Props> = ({ datasource, onChange, onRunQ
235242
const filters = query.filters || [];
236243
const selectedColumns = query.selectedColumns || [];
237244

245+
// Queries saved before v2 may only contain queryText. The backend can run
246+
// them by deriving the stream from SQL, but the editor needs a selected
247+
// stream before it renders Builder or Code mode.
238248
const [selectedStream, setSelectedStream] = useState<SelectableValue<string>>(
239-
query.stream ? { label: query.stream, value: query.stream } : ({} as SelectableValue<string>)
249+
queryStream ? { label: queryStream, value: queryStream } : ({} as SelectableValue<string>)
240250
);
241251
const [schemaFields, setSchemaFields] = useState<SchemaFields[]>([]);
242252
const [stats, setStats] = useState<StreamStatsResponse>({});
@@ -258,6 +268,19 @@ export const QueryEditor: ComponentType<Props> = ({ datasource, onChange, onRunQ
258268
const [promMetricNames, setPromMetricNames] = useState<string[]>([]);
259269
const [promMetadata, setPromMetadata] = useState<Record<string, { type?: string; help?: string; unit?: string }>>({});
260270

271+
// Grafana can replace a query target without remounting this component.
272+
// Hydrate both local editor state and the persisted query. PromQL execution
273+
// reads query.stream directly, so keeping the inferred stream local would
274+
// make a legacy SQL query fail after switching a metrics dataset to PromQL.
275+
useEffect(() => {
276+
if (queryStream && queryStream !== selectedStream?.value) {
277+
setSelectedStream({ label: queryStream, value: queryStream });
278+
}
279+
if (!query.stream && inferredStream) {
280+
onChange({ ...query, stream: inferredStream });
281+
}
282+
}, [query, queryStream, inferredStream, selectedStream?.value, onChange]);
283+
261284
// Build fieldTypeMap and fieldNames from schema (like Prism's setStreamSchema)
262285
const fieldTypeMap: FieldTypeMap = useMemo(() => buildFieldTypeMap(schemaFields), [schemaFields]);
263286
const fieldNames: string[] = useMemo(() => schemaFields.map((f) => f.name), [schemaFields]);

‎src/datasource.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptio
6262
}
6363

6464
extractStreamName = (sqlQuery: string): string | null => {
65-
const tableRegex = /from\s+([\w\.]+)/i;
65+
const tableRegex = /\bfrom\s+(?:"([^"]+)"|`([^`]+)`|([\w.]+))/i;
6666
const match = sqlQuery.match(tableRegex);
6767
if (match) {
68-
return match[1];
68+
return match[1] || match[2] || match[3];
6969
}
7070

7171
return null;

0 commit comments

Comments
 (0)