fix(doris): enable explain plan support

This commit is contained in:
jearhe authored and GitHub committed 2026-09-10 00:28:40 +08:00
1 parent 1663f3633f
commit e2240d4ed1
8 files changed
+193 -9

No files matched your search

@@ -0,0 +1,36 @@
import { describe, expect, it } from "vitest";
import { flattenExplainPlanNodes, parseExplainResult, supportsExplainPlan } from "@/lib/diagram/explainPlan";
import type { QueryResult } from "@/types/database";
const DORIS_PLAN = `PLAN FRAGMENT 0
OUTPUT EXPRS:
1:<slot 11> : 11: order_id
PARTITION: UNPARTITIONED
VEXCHANGE
0:VSCAN NODE
TABLE: internal.sales.orders
PREAGGREGATION: ON`;
function explainResult(planText: string): QueryResult {
return {
columns: ["Explain String"],
rows: planText.split("\n").map((line) => [line]),
affected_rows: 0,
execution_time_ms: 1,
};
}
describe("Doris explain plan", () => {
it("is enabled by the driver capability manifest", () => {
expect(supportsExplainPlan("doris")).toBe(true);
});
it("keeps the text plan available without sending it through a JSON parser", () => {
const parsed = parseExplainResult("doris", explainResult(DORIS_PLAN));
expect(parsed.databaseType).toBe("doris");
expect(parsed.raw).toBe(DORIS_PLAN);
expect(flattenExplainPlanNodes(parsed.nodes).map((node) => node.title)).toEqual(["PLAN FRAGMENT 0", "OUTPUT EXPRS:", "1:<slot 11> : 11: order_id", "PARTITION: UNPARTITIONED", "VEXCHANGE", "0:VSCAN NODE", "TABLE: internal.sales.orders", "PREAGGREGATION: ON"]);
});
});
+31 -4
View File
@@ -16,16 +16,18 @@ export interface ExplainPlanNode {
children: ExplainPlanNode[];
}
export type ExplainPlanDatabaseType = "mysql" | "postgres" | "dameng" | "questdb" | "doris" | "oracle" | "sqlserver";
export interface ParsedExplainPlan {
databaseType: "mysql" | "postgres" | "dameng" | "questdb" | "oracle" | "sqlserver";
databaseType: ExplainPlanDatabaseType;
raw: unknown;
nodes: ExplainPlanNode[];
}
export type BuildExplainSqlResult = { ok: true; sql: string } | { ok: false; reason: "unsupported" | "empty" | "unsafe" };
const SUPPORTED_EXPLAIN_TYPES = new Set<DatabaseType>(["mysql", "postgres", "dameng", "questdb", "oracle", "sqlserver"]);
export function supportsExplainPlan(databaseType?: DatabaseType): databaseType is "mysql" | "postgres" | "dameng" | "questdb" | "oracle" | "sqlserver" {
const SUPPORTED_EXPLAIN_TYPES = new Set<DatabaseType>(["mysql", "postgres", "dameng", "questdb", "doris", "oracle", "sqlserver"]);
export function supportsExplainPlan(databaseType?: DatabaseType): databaseType is ExplainPlanDatabaseType {
return !!databaseType && supportsDatabaseFeature(databaseType, "sqlExplain") && SUPPORTED_EXPLAIN_TYPES.has(databaseType);
}
@@ -34,11 +36,13 @@ export function buildExplainSql(databaseType: DatabaseType | undefined, sql: str
return api.buildExplainSql({ databaseType, sql, format, analyze }) as Promise<BuildExplainSqlResult>;
}
export function parseExplainResult(databaseType: "mysql" | "postgres" | "dameng" | "questdb" | "sqlserver", result: QueryResult): ParsedExplainPlan {
export function parseExplainResult(databaseType: ExplainPlanDatabaseType, result: QueryResult): ParsedExplainPlan {
if (databaseType === "dameng") {
return parseDamengExplain(result);
} else if (databaseType === "questdb") {
return parseQuestdbExplain(result);
} else if (databaseType === "doris") {
return parseDorisExplain(result);
} else if (databaseType === "sqlserver") {
return parseSqlServerExplain(result);
}
@@ -873,6 +877,29 @@ function parseQuestdbExplain(result: QueryResult): ParsedExplainPlan {
return { databaseType: "questdb", raw: result, nodes };
}
// ── Doris explain parser ────────────────────────────────────────────
function parseDorisExplain(result: QueryResult): ParsedExplainPlan {
const lines = result.rows.flatMap((row) => String(row[0] ?? "").split(/\r?\n/));
const raw = lines.join("\n");
const nodes = lines
.filter((line) => line.trim().length > 0)
.map((line, index): ExplainPlanNode => {
const text = line.trim();
return {
id: `plan_${index}`,
title: text,
nodeType: "Plan",
relation: "",
index: String(index),
cost: undefined,
rows: undefined,
details: [text],
children: [],
};
});
return { databaseType: "doris", raw, nodes };
}
// ── Helpers ───────────────────────────────────────────────────────────
function objectValue(value: unknown): Record<string, unknown> | null {