Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/components/QueryEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,14 @@ export const QueryEditor: ComponentType<Props> = ({ 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 /
Expand All @@ -235,8 +242,11 @@ export const QueryEditor: ComponentType<Props> = ({ 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 [selectedStream, setSelectedStream] = useState<SelectableValue<string>>(
query.stream ? { label: query.stream, value: query.stream } : ({} as SelectableValue<string>)
queryStream ? { label: queryStream, value: queryStream } : ({} as SelectableValue<string>)
);
const [schemaFields, setSchemaFields] = useState<SchemaFields[]>([]);
const [stats, setStats] = useState<StreamStatsResponse>({});
Expand All @@ -258,6 +268,19 @@ export const QueryEditor: ComponentType<Props> = ({ datasource, onChange, onRunQ
const [promMetricNames, setPromMetricNames] = useState<string[]>([]);
const [promMetadata, setPromMetadata] = useState<Record<string, { type?: string; help?: string; unit?: string }>>({});

// Grafana can replace a query target without remounting this component.
// 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 });
}
Comment thread
praveen5959 marked this conversation as resolved.
Resolved
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]);
const fieldNames: string[] = useMemo(() => schemaFields.map((f) => f.name), [schemaFields]);
Expand Down
4 changes: 2 additions & 2 deletions src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptio
}

extractStreamName = (sqlQuery: string): string | null => {
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;
Expand Down
Loading