feat(query): add MySQL standard explain table view
This commit is contained in:
1 parent
830e3c2183
commit
54632f4c16
16 files changed
+480
-28
No files matched your search
@@ -0,0 +1,254 @@
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { ParsedExplainPlan } from "@/lib/diagram/explainPlan";
|
||||
import type { QueryResult } from "@/types/database";
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
buildExplainSql: vi.fn(),
|
||||
parseExplainResult: vi.fn(),
|
||||
parseDamengExplainText: vi.fn(),
|
||||
parseOracleExplainText: vi.fn(),
|
||||
executeQuery: vi.fn(),
|
||||
cancelQuery: vi.fn(),
|
||||
closeClientConnectionSession: vi.fn(),
|
||||
saveOpenTabsState: vi.fn(),
|
||||
getConfig: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/diagram/explainPlan", () => ({
|
||||
buildExplainSql: mocks.buildExplainSql,
|
||||
parseExplainResult: mocks.parseExplainResult,
|
||||
parseDamengExplainText: mocks.parseDamengExplainText,
|
||||
parseOracleExplainText: mocks.parseOracleExplainText,
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/backend/api", () => ({
|
||||
executeQuery: mocks.executeQuery,
|
||||
cancelQuery: mocks.cancelQuery,
|
||||
closeClientConnectionSession: mocks.closeClientConnectionSession,
|
||||
saveOpenTabsState: mocks.saveOpenTabsState,
|
||||
}));
|
||||
|
||||
vi.mock("@/stores/connectionStore", () => ({
|
||||
useConnectionStore: () => ({
|
||||
getConfig: mocks.getConfig,
|
||||
recordConnectionLostError: vi.fn(),
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/stores/settingsStore", () => ({
|
||||
useSettingsStore: () => ({
|
||||
editorSettings: { pageSize: 100, openTabsRestoreMode: "all", confirmUnsavedSqlClose: false },
|
||||
}),
|
||||
}));
|
||||
|
||||
function installLocalStorage() {
|
||||
const data = new Map<string, string>();
|
||||
vi.stubGlobal("localStorage", {
|
||||
getItem: vi.fn((key: string) => data.get(key) ?? null),
|
||||
setItem: vi.fn((key: string, value: string) => data.set(key, value)),
|
||||
removeItem: vi.fn((key: string) => data.delete(key)),
|
||||
});
|
||||
}
|
||||
|
||||
function deferred<T>() {
|
||||
let resolve!: (value: T) => void;
|
||||
let reject!: (reason?: unknown) => void;
|
||||
const promise = new Promise<T>((promiseResolve, promiseReject) => {
|
||||
resolve = promiseResolve;
|
||||
reject = promiseReject;
|
||||
});
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
||||
const standardSql = "EXPLAIN SELECT * FROM users WHERE status = 'active'";
|
||||
const jsonSql = "EXPLAIN FORMAT=JSON SELECT * FROM users WHERE status = 'active'";
|
||||
const sourceSql = "SELECT * FROM users WHERE status = 'active'";
|
||||
|
||||
const tableResult: QueryResult = {
|
||||
columns: ["id", "select_type", "table", "type", "rows", "Extra"],
|
||||
rows: [[1, "SIMPLE", "users", "ref", 12, "Using where"]],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 4,
|
||||
};
|
||||
|
||||
const jsonResult: QueryResult = {
|
||||
columns: ["EXPLAIN"],
|
||||
rows: [['{"query_block":{"select_id":1}}']],
|
||||
affected_rows: 0,
|
||||
execution_time_ms: 5,
|
||||
};
|
||||
|
||||
const visualPlan: ParsedExplainPlan = {
|
||||
databaseType: "mysql",
|
||||
raw: { query_block: { select_id: 1 } },
|
||||
nodes: [],
|
||||
};
|
||||
|
||||
function configureSuccessfulBuilds() {
|
||||
mocks.buildExplainSql.mockImplementation(async (_databaseType: string, _sql: string, format: "standard" | "json") => ({
|
||||
ok: true,
|
||||
sql: format === "standard" ? standardSql : jsonSql,
|
||||
}));
|
||||
}
|
||||
|
||||
describe("queryStore MySQL dual explain", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.unstubAllGlobals();
|
||||
installLocalStorage();
|
||||
setActivePinia(createPinia());
|
||||
|
||||
mocks.getConfig.mockReturnValue({
|
||||
id: "mysql-1",
|
||||
name: "MySQL",
|
||||
db_type: "mysql",
|
||||
query_timeout_secs: 45,
|
||||
});
|
||||
mocks.cancelQuery.mockResolvedValue(true);
|
||||
mocks.closeClientConnectionSession.mockResolvedValue(undefined);
|
||||
mocks.saveOpenTabsState.mockResolvedValue(undefined);
|
||||
mocks.parseExplainResult.mockReturnValue(visualPlan);
|
||||
configureSuccessfulBuilds();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("executes standard EXPLAIN before JSON EXPLAIN using one execution and client session", async () => {
|
||||
const standardExecution = deferred<QueryResult>();
|
||||
mocks.executeQuery.mockImplementationOnce(() => standardExecution.promise).mockResolvedValueOnce(jsonResult);
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query", "query", "analytics");
|
||||
const explain = store.explainTabSql(tabId, sourceSql, "mysql");
|
||||
|
||||
await vi.waitFor(() => expect(mocks.executeQuery).toHaveBeenCalledTimes(1));
|
||||
expect(mocks.buildExplainSql).toHaveBeenNthCalledWith(1, "mysql", sourceSql, "standard");
|
||||
expect(mocks.buildExplainSql).toHaveBeenNthCalledWith(2, "mysql", sourceSql, "json");
|
||||
expect(mocks.executeQuery).toHaveBeenNthCalledWith(1, "mysql-1", "app", standardSql, "analytics", expect.any(String), expect.objectContaining({ timeoutSecs: 45 }));
|
||||
|
||||
standardExecution.resolve(tableResult);
|
||||
await vi.waitFor(() => expect(mocks.executeQuery).toHaveBeenCalledTimes(2));
|
||||
await explain;
|
||||
|
||||
const standardCall = mocks.executeQuery.mock.calls[0]!;
|
||||
const jsonCall = mocks.executeQuery.mock.calls[1]!;
|
||||
const executionId = standardCall[4] as string;
|
||||
const standardOptions = standardCall[5] as { clientSessionId: string; timeoutSecs: number };
|
||||
const jsonOptions = jsonCall[5] as { clientSessionId: string; timeoutSecs: number };
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
|
||||
expect(jsonCall[0]).toBe("mysql-1");
|
||||
expect(jsonCall[1]).toBe("app");
|
||||
expect(jsonCall[2]).toBe(jsonSql);
|
||||
expect(jsonCall[3]).toBe("analytics");
|
||||
expect(jsonCall[4]).toBe(executionId);
|
||||
expect(jsonOptions.clientSessionId).toBe(standardOptions.clientSessionId);
|
||||
expect(standardOptions.clientSessionId).toBe(`${tabId}:explain:${executionId}`);
|
||||
expect(jsonOptions.timeoutSecs).toBe(45);
|
||||
expect(mocks.parseExplainResult).toHaveBeenCalledWith("mysql", jsonResult);
|
||||
expect(tab.explainTableResult).toEqual(tableResult);
|
||||
expect(tab.explainPlan).toEqual(visualPlan);
|
||||
expect(tab.explainTableSql).toBe(standardSql);
|
||||
expect(tab.explainSql).toBe(jsonSql);
|
||||
expect(tab.isExplaining).toBe(false);
|
||||
expect(tab.explainExecutionId).toBeUndefined();
|
||||
await vi.waitFor(() => expect(mocks.closeClientConnectionSession).toHaveBeenCalledWith("mysql-1", "app", standardOptions.clientSessionId));
|
||||
});
|
||||
|
||||
it("keeps the JSON visual plan when the standard table EXPLAIN fails", async () => {
|
||||
mocks.executeQuery.mockRejectedValueOnce(new Error("standard EXPLAIN failed")).mockResolvedValueOnce(jsonResult);
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
|
||||
await store.explainTabSql(tabId, sourceSql, "mysql");
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
expect(mocks.executeQuery).toHaveBeenNthCalledWith(1, "mysql-1", "app", standardSql, undefined, expect.any(String), expect.any(Object));
|
||||
expect(mocks.executeQuery).toHaveBeenNthCalledWith(2, "mysql-1", "app", jsonSql, undefined, expect.any(String), expect.any(Object));
|
||||
expect(tab.explainTableResult).toBeUndefined();
|
||||
expect(tab.explainTableError).toBe("standard EXPLAIN failed");
|
||||
expect(tab.explainPlan).toEqual(visualPlan);
|
||||
expect(tab.explainError).toBeUndefined();
|
||||
expect(tab.isExplaining).toBe(false);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["is acknowledged", () => mocks.cancelQuery.mockResolvedValue(true), true],
|
||||
["returns false", () => mocks.cancelQuery.mockResolvedValue(false), false],
|
||||
["rejects", () => mocks.cancelQuery.mockRejectedValue(new Error("cancel request failed")), false],
|
||||
])("does not start JSON EXPLAIN when cancellation %s during standard EXPLAIN", async (_label, configureCancel, expectedResult) => {
|
||||
const standardExecution = deferred<QueryResult>();
|
||||
mocks.executeQuery.mockImplementationOnce(() => standardExecution.promise);
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
const explain = store.explainTabSql(tabId, sourceSql, "mysql");
|
||||
|
||||
await vi.waitFor(() => expect(mocks.executeQuery).toHaveBeenCalledTimes(1));
|
||||
const executionId = store.tabs.find((item) => item.id === tabId)?.explainExecutionId;
|
||||
expect(executionId).toEqual(expect.any(String));
|
||||
|
||||
configureCancel();
|
||||
await expect(store.cancelTabExplain(tabId)).resolves.toBe(expectedResult);
|
||||
expect(mocks.cancelQuery).toHaveBeenCalledWith(executionId);
|
||||
|
||||
standardExecution.resolve(tableResult);
|
||||
await explain;
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
expect(mocks.executeQuery).toHaveBeenCalledTimes(1);
|
||||
expect(tab.explainTableResult).toBeUndefined();
|
||||
expect(tab.explainPlan).toBeUndefined();
|
||||
expect(tab.isExplaining).toBe(false);
|
||||
expect(tab.explainExecutionId).toBeUndefined();
|
||||
});
|
||||
|
||||
it.each([
|
||||
["returns false", () => mocks.cancelQuery.mockResolvedValue(false)],
|
||||
["rejects", () => mocks.cancelQuery.mockRejectedValue(new Error("cancel request failed"))],
|
||||
])("does not start either EXPLAIN when cancellation %s while SQL formats are building", async (_label, configureCancel) => {
|
||||
const standardBuild = deferred<{ ok: true; sql: string }>();
|
||||
const jsonBuild = deferred<{ ok: true; sql: string }>();
|
||||
mocks.buildExplainSql.mockImplementation((_databaseType: string, _sql: string, format: "standard" | "json") => (format === "standard" ? standardBuild.promise : jsonBuild.promise));
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
const explain = store.explainTabSql(tabId, sourceSql, "mysql");
|
||||
|
||||
await vi.waitFor(() => expect(mocks.buildExplainSql).toHaveBeenCalledTimes(2));
|
||||
configureCancel();
|
||||
await expect(store.cancelTabExplain(tabId)).resolves.toBe(false);
|
||||
|
||||
standardBuild.resolve({ ok: true, sql: standardSql });
|
||||
jsonBuild.resolve({ ok: true, sql: jsonSql });
|
||||
await explain;
|
||||
|
||||
expect(mocks.executeQuery).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("clears loading state when one of the format builders rejects", async () => {
|
||||
mocks.buildExplainSql.mockRejectedValueOnce(new Error("format builder unavailable"));
|
||||
|
||||
const { useQueryStore } = await import("@/stores/queryStore");
|
||||
const store = useQueryStore();
|
||||
const tabId = store.createTab("mysql-1", "app", "Query");
|
||||
|
||||
await expect(store.explainTabSql(tabId, sourceSql, "mysql")).resolves.toEqual({ ok: true, sql: "" });
|
||||
|
||||
const tab = store.tabs.find((item) => item.id === tabId)!;
|
||||
expect(mocks.executeQuery).not.toHaveBeenCalled();
|
||||
expect(tab).toMatchObject({
|
||||
isExplaining: false,
|
||||
explainExecutionId: undefined,
|
||||
explainError: "format builder unavailable",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -5,7 +5,7 @@ import { useI18n } from "vue-i18n";
|
||||
import type { ConnectionConfig, DatabaseType, ObjectBrowserViewport, QueryResult, QueryTab, TableInfoTab, TableStructureEditorTarget } from "@/types/database";
|
||||
import { orderPinnedFirst } from "@/lib/app/pinnedItems";
|
||||
import { canCancelQueryExecution } from "@/lib/sql/queryExecutionState";
|
||||
import { buildExplainSql, parseExplainResult, parseDamengExplainText, parseOracleExplainText } from "@/lib/diagram/explainPlan";
|
||||
import { buildExplainSql, parseExplainResult, parseDamengExplainText, parseOracleExplainText, type BuildExplainSqlResult } from "@/lib/diagram/explainPlan";
|
||||
import { allEditableColumnsWriteable, allPrimaryKeysPresent, analyzeEditableQueryEditability, sourceColumnsForResult, type EditableQueryInfo, type EditableQuerySource } from "@/lib/sql/sqlAnalysis";
|
||||
import { buildQueryWithHiddenPrimaryKeys, hiddenResultColumnIndexes, type HiddenPrimaryKeyProjection } from "@/lib/sql/editableQueryHiddenKeys";
|
||||
import { ACTIVE_TAB_STORAGE_KEY, OPEN_TABS_STORAGE_KEY, restoreOpenTabsPayload, restoreOpenTabsState, serializeOpenTabs } from "@/lib/app/openTabsPersistence";
|
||||
@@ -382,7 +382,7 @@ export const useQueryStore = defineStore("query", () => {
|
||||
const catalog = tab.mode === "data" ? tab.tableMeta?.catalog : undefined;
|
||||
const connection = catalog ? useConnectionStore().getConfig(tab.connectionId) : undefined;
|
||||
const executionDatabase = dataTabExecutionDatabase(connection, tab.database, catalog);
|
||||
const clientSessionIds = [tabClientSessionId(tab), ...BACKGROUND_CLIENT_SESSION_SUFFIXES.map((suffix) => tabClientSessionId(tab, suffix))];
|
||||
const clientSessionIds = [...new Set([tabClientSessionId(tab), ...BACKGROUND_CLIENT_SESSION_SUFFIXES.map((suffix) => tabClientSessionId(tab, suffix)), tab.explainClientSessionId].filter((sessionId): sessionId is string => !!sessionId))];
|
||||
for (const clientSessionId of clientSessionIds) {
|
||||
await closeClientSessionId(tab.connectionId, executionDatabase, clientSessionId, { tabId: tab.id });
|
||||
}
|
||||
@@ -1962,11 +1962,15 @@ export const useQueryStore = defineStore("query", () => {
|
||||
|
||||
function clearExplain(tab: QueryTab) {
|
||||
tab.explainPlan = undefined;
|
||||
tab.explainTableResult = undefined;
|
||||
tab.explainError = undefined;
|
||||
tab.explainTableError = undefined;
|
||||
tab.explainSql = undefined;
|
||||
tab.explainTableSql = undefined;
|
||||
tab.lastExplainedSql = undefined;
|
||||
tab.isExplaining = false;
|
||||
tab.explainExecutionId = undefined;
|
||||
tab.explainClientSessionId = undefined;
|
||||
}
|
||||
|
||||
function toErrorResult(e: any): NonNullable<QueryTab["result"]> {
|
||||
@@ -3003,7 +3007,12 @@ export const useQueryStore = defineStore("query", () => {
|
||||
|
||||
tab.isExplaining = true;
|
||||
tab.explainExecutionId = executionId;
|
||||
tab.explainPlan = undefined;
|
||||
tab.explainTableResult = undefined;
|
||||
tab.explainError = undefined;
|
||||
tab.explainTableError = undefined;
|
||||
tab.explainSql = undefined;
|
||||
tab.explainTableSql = undefined;
|
||||
tab.lastExplainedSql = sql;
|
||||
|
||||
// DM and Oracle agents expose native text plans. DM also supports autotrace.
|
||||
@@ -3064,10 +3073,99 @@ export const useQueryStore = defineStore("query", () => {
|
||||
return { ok: true as const, sql: explainSql };
|
||||
}
|
||||
|
||||
if (databaseType === "mysql") {
|
||||
let tableBuilt: BuildExplainSqlResult;
|
||||
let jsonBuilt: BuildExplainSqlResult;
|
||||
try {
|
||||
[tableBuilt, jsonBuilt] = await Promise.all([buildExplainSql(databaseType, sql, "standard"), buildExplainSql(databaseType, sql, "json")]);
|
||||
} catch (e: any) {
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current?.explainExecutionId === executionId) {
|
||||
current.isExplaining = false;
|
||||
current.explainExecutionId = undefined;
|
||||
current.explainError = String(e?.message || e);
|
||||
}
|
||||
return { ok: true as const, sql: "" };
|
||||
}
|
||||
if (tabs.value.find((t) => t.id === id)?.explainExecutionId !== executionId) {
|
||||
return { ok: true as const, sql: jsonBuilt.ok ? jsonBuilt.sql : "" };
|
||||
}
|
||||
if (!tableBuilt.ok || !jsonBuilt.ok) {
|
||||
const failed = !tableBuilt.ok ? tableBuilt : jsonBuilt;
|
||||
const reason = !tableBuilt.ok ? tableBuilt.reason : !jsonBuilt.ok ? jsonBuilt.reason : "unsupported";
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current?.explainExecutionId === executionId) {
|
||||
current.isExplaining = false;
|
||||
current.explainExecutionId = undefined;
|
||||
current.explainError = reason;
|
||||
}
|
||||
return failed;
|
||||
}
|
||||
|
||||
tab.explainTableSql = tableBuilt.sql;
|
||||
tab.explainSql = jsonBuilt.sql;
|
||||
// Keep the two EXPLAIN statements on the same one-connection MySQL session.
|
||||
const clientSessionId = `${tabClientSessionId(tab, "explain")}:${executionId}`;
|
||||
tab.explainClientSessionId = clientSessionId;
|
||||
try {
|
||||
try {
|
||||
const tableResult = await api.executeQuery(tab.connectionId, tab.database, tableBuilt.sql, tab.schema, executionId, {
|
||||
clientSessionId,
|
||||
timeoutSecs: queryTimeoutSecs,
|
||||
});
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current?.explainExecutionId === executionId) {
|
||||
current.explainTableResult = markQueryResultRowsRaw(tableResult);
|
||||
current.explainTableError = undefined;
|
||||
}
|
||||
} catch (e: any) {
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current?.explainExecutionId === executionId) {
|
||||
current.explainTableResult = undefined;
|
||||
current.explainTableError = String(e?.message || e);
|
||||
}
|
||||
}
|
||||
|
||||
// A canceled or superseded standard request must not start the JSON request.
|
||||
if (tabs.value.find((t) => t.id === id)?.explainExecutionId !== executionId) {
|
||||
return { ok: true as const, sql: jsonBuilt.sql };
|
||||
}
|
||||
|
||||
try {
|
||||
const jsonResult = await api.executeQuery(tab.connectionId, tab.database, jsonBuilt.sql, tab.schema, executionId, {
|
||||
clientSessionId,
|
||||
timeoutSecs: queryTimeoutSecs,
|
||||
});
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current?.explainExecutionId === executionId) {
|
||||
current.explainPlan = parseExplainResult("mysql", jsonResult);
|
||||
current.explainError = undefined;
|
||||
}
|
||||
} catch (e: any) {
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current?.explainExecutionId === executionId) {
|
||||
current.explainPlan = undefined;
|
||||
current.explainError = String(e?.message || e);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current?.explainExecutionId === executionId) {
|
||||
current.isExplaining = false;
|
||||
current.explainExecutionId = undefined;
|
||||
}
|
||||
if (current?.explainClientSessionId === clientSessionId) current.explainClientSessionId = undefined;
|
||||
void closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id, explainExecutionId: executionId });
|
||||
}
|
||||
return { ok: true as const, sql: jsonBuilt.sql };
|
||||
}
|
||||
|
||||
const built = await buildExplainSql(databaseType, sql);
|
||||
if (!built.ok) {
|
||||
tab.explainPlan = undefined;
|
||||
tab.explainError = built.reason;
|
||||
tab.isExplaining = false;
|
||||
tab.explainExecutionId = undefined;
|
||||
return built;
|
||||
}
|
||||
|
||||
@@ -3142,19 +3240,12 @@ export const useQueryStore = defineStore("query", () => {
|
||||
if (!tab?.isExplaining || !tab.explainExecutionId) return false;
|
||||
|
||||
const executionId = tab.explainExecutionId;
|
||||
// Invalidate locally before the remote cancellation call so no later stage can start.
|
||||
tab.isExplaining = false;
|
||||
tab.explainExecutionId = undefined;
|
||||
try {
|
||||
const canceled = await api.cancelQuery(executionId);
|
||||
if (!canceled) {
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current && current.explainExecutionId === executionId) current.isExplaining = false;
|
||||
}
|
||||
return canceled;
|
||||
} catch (e: any) {
|
||||
const current = tabs.value.find((t) => t.id === id);
|
||||
if (current && current.explainExecutionId === executionId) {
|
||||
current.isExplaining = false;
|
||||
current.explainError = String(e?.message || e);
|
||||
}
|
||||
return await api.cancelQuery(executionId);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user