Skip to content
Closed
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
32 changes: 32 additions & 0 deletions src/cst/Explain.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
import { BaseNode, Keyword } from "./Base";
import { ListExpr, ParenExpr } from "./Expr";
import { BooleanLiteral, NumberLiteral, StringLiteral } from "./Literal";
import { Statement } from "./Statement";

export type AllExplainNodes = ExplainStmt | ExplainOption;

// EXPLAIN
export interface ExplainStmt extends BaseNode {
type: "explain_stmt";
explainKw: Keyword<"EXPLAIN" | "DESCRIBE" | "DESC">;
analyzeKw?: Keyword<"ANALYZE">;
verboseKw?: Keyword<"VERBOSE">; // PostgreSQL
options?: ParenExpr<ListExpr<ExplainOption>>; // PostgreSQL
queryPlanKw?: [Keyword<"QUERY">, Keyword<"PLAN">];
statement: Statement;
}

// PostgreSQL
export interface ExplainOption extends BaseNode {
type: "explain_option";
name: Keyword<
| "ANALYZE"
| "VERBOSE"
| "COSTS"
| "SETTINGS"
| "GENERIC_PLAN"
| "BUFFERS"
| "SERIALIZE"
| "WAL"
| "TIMING"
| "SUMMARY"
| "MEMORY"
| "FORMAT"
>;
value?:
| BooleanLiteral
| NumberLiteral
| StringLiteral
| Keyword<
"ON" | "OFF" | "NONE" | "TEXT" | "BINARY" | "XML" | "JSON" | "YAML"
>;
}
2 changes: 2 additions & 0 deletions src/cst/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { AllDataTypeNodes } from "./DataType";
import { AllDclNodes } from "./Dcl";
import { AllDeleteNodes } from "./Delete";
import { AllDomainNodes } from "./Domain";
import { AllExplainNodes } from "./Explain";
import { AllExprNodes } from "./Expr";
import { AllExtensionNodes } from "./Extension";
import { AllFrameNodes } from "./WindowFrame";
Expand Down Expand Up @@ -107,6 +108,7 @@ export type Node =
| AllDclNodes
| AllDeleteNodes
| AllDomainNodes
| AllExplainNodes
| AllExprNodes
| AllExtensionNodes
| AllFrameNodes
Expand Down
62 changes: 61 additions & 1 deletion src/parser.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -4886,11 +4886,24 @@ analyze_stmt
* ------------------------------------------------------------------------------------ *
*/
explain_stmt
= kw:(explain_kw __) anKw:(ANALYZE __)? qpKw:(QUERY __ PLAN __)? stmt:statement {
= kw:(explain_kw __) options:(explain_options __) stmt:statement {
return loc({
type: "explain_stmt",
explainKw: read(kw),
options: read(options),
statement: stmt,
});
}
/ kw:(explain_kw __)
anKw:(ANALYZE __)?
verboseKw:(explain_verbose __)?
qpKw:(QUERY __ PLAN __)?
stmt:statement {
return loc({
type: "explain_stmt",
explainKw: read(kw),
analyzeKw: read(anKw),
verboseKw: read(verboseKw),
queryPlanKw: read(qpKw),
statement: stmt,
});
Expand All @@ -4901,6 +4914,41 @@ explain_kw
/ kw:DESCRIBE &mysql { return kw; }
/ kw:DESC &mysql { return kw; }

explain_verbose
= &postgres kw:VERBOSE { return kw; }

explain_options
= &postgres options:paren$list$explain_option { return options; }

explain_option
= name:explain_option_name value:(__ explain_option_value)? {
return loc({
type: "explain_option",
name,
value: read(value),
});
}

explain_option_name
= ANALYZE
/ VERBOSE
/ COSTS
/ SETTINGS
/ GENERIC_PLAN
/ BUFFERS
/ SERIALIZE
/ WAL
/ TIMING
/ SUMMARY
/ MEMORY
/ FORMAT

explain_option_value
= boolean_literal
/ number_literal
/ string_literal
/ ON / OFF / NONE / TEXT / BINARY / XML / JSON / YAML

/**
* ------------------------------------------------------------------------------------ *
* *
Expand Down Expand Up @@ -8612,6 +8660,7 @@ paren$list$data_type = .
paren$list$entity_name = .
paren$list$equals_expr = .
paren$list$exclusion_param = .
paren$list$explain_option = .
paren$list$expr = .
paren$list$expr_or_default = .
paren$list$expr_or_explicit_alias = .
Expand Down Expand Up @@ -8685,6 +8734,7 @@ list$delete_clause_table = .
list$entity_name = .
list$equals_expr = .
list$exclusion_param = .
list$explain_option = .
list$expr = .
list$expr_or_default = .
list$expr_or_explicit_alias = .
Expand Down Expand Up @@ -9798,6 +9848,7 @@ BOOLEAN = kw:"BOOLEAN"i !ident_part { return loc(createK
BREADTH = kw:"BREADTH"i !ident_part { return loc(createKeyword(kw)); }
BREAK = kw:"BREAK"i !ident_part { return loc(createKeyword(kw)); }
BTREE = kw:"BTREE"i !ident_part { return loc(createKeyword(kw)); }
BUFFERS = kw:"BUFFERS"i !ident_part { return loc(createKeyword(kw)); }
BY = kw:"BY"i !ident_part { return loc(createKeyword(kw)); }
BYPASSRLS = kw:"BYPASSRLS"i !ident_part { return loc(createKeyword(kw)); }
BYTEINT = kw:"BYTEINT"i !ident_part { return loc(createKeyword(kw)); }
Expand Down Expand Up @@ -9845,6 +9896,7 @@ CONTINUE = kw:"CONTINUE"i !ident_part { return loc(createK
CONVERSION = kw:"CONVERSION"i !ident_part { return loc(createKeyword(kw)); }
COPY = kw:"COPY"i !ident_part { return loc(createKeyword(kw)); }
COST = kw:"COST"i !ident_part { return loc(createKeyword(kw)); }
COSTS = kw:"COSTS"i !ident_part { return loc(createKeyword(kw)); }
COUNT = kw:"COUNT"i !ident_part { return loc(createKeyword(kw)); }
CREATE = kw:"CREATE"i !ident_part { return loc(createKeyword(kw)); }
CREATEDB = kw:"CREATEDB"i !ident_part { return loc(createKeyword(kw)); }
Expand Down Expand Up @@ -9974,6 +10026,7 @@ FULLTEXT = kw:"FULLTEXT"i !ident_part { return loc(createK
FUNCTION = kw:"FUNCTION"i !ident_part { return loc(createKeyword(kw)); }
FUNCTIONS = kw:"FUNCTIONS"i !ident_part { return loc(createKeyword(kw)); }
GENERATED = kw:"GENERATED"i !ident_part { return loc(createKeyword(kw)); }
GENERIC_PLAN = kw:"GENERIC_PLAN"i !ident_part { return loc(createKeyword(kw)); }
GEOGRAPHY = kw:"GEOGRAPHY"i !ident_part { return loc(createKeyword(kw)); }
GLOB = kw:"GLOB"i !ident_part { return loc(createKeyword(kw)); }
GLOBAL = kw:"GLOBAL"i !ident_part { return loc(createKeyword(kw)); }
Expand Down Expand Up @@ -10259,12 +10312,14 @@ SELECT = kw:"SELECT"i !ident_part { return loc(createK
SEQUENCE = kw:"SEQUENCE"i !ident_part { return loc(createKeyword(kw)); }
SEQUENCES = kw:"SEQUENCES"i !ident_part { return loc(createKeyword(kw)); }
SERIALIZABLE = kw:"SERIALIZABLE"i !ident_part { return loc(createKeyword(kw)); }
SERIALIZE = kw:"SERIALIZE"i !ident_part { return loc(createKeyword(kw)); }
SERVER = kw:"SERVER"i !ident_part { return loc(createKeyword(kw)); }
SESSION = kw:"SESSION"i !ident_part { return loc(createKeyword(kw)); }
SESSION_USER = kw:"SESSION_USER"i !ident_part { return loc(createKeyword(kw)); }
SET = kw:"SET"i !ident_part { return loc(createKeyword(kw)); }
SETOF = kw:"SETOF"i !ident_part { return loc(createKeyword(kw)); }
SETS = kw:"SETS"i !ident_part { return loc(createKeyword(kw)); }
SETTINGS = kw:"SETTINGS"i !ident_part { return loc(createKeyword(kw)); }
SHARE = kw:"SHARE"i !ident_part { return loc(createKeyword(kw)); }
SHARED = kw:"SHARED"i !ident_part { return loc(createKeyword(kw)); }
SHOW = kw:"SHOW"i !ident_part { return loc(createKeyword(kw)); }
Expand Down Expand Up @@ -10303,6 +10358,7 @@ STRING = kw:"STRING"i !ident_part { return loc(createK
STRUCT = kw:"STRUCT"i !ident_part { return loc(createKeyword(kw)); }
SUBSCRIPTION = kw:"SUBSCRIPTION"i !ident_part { return loc(createKeyword(kw)); }
SUM = kw:"SUM"i !ident_part { return loc(createKeyword(kw)); }
SUMMARY = kw:"SUMMARY"i !ident_part { return loc(createKeyword(kw)); }
SUNDAY = kw:"SUNDAY"i !ident_part { return loc(createKeyword(kw)); }
SUPERUSER = kw:"SUPERUSER"i !ident_part { return loc(createKeyword(kw)); }
SUPPORT = kw:"SUPPORT"i !ident_part { return loc(createKeyword(kw)); }
Expand Down Expand Up @@ -10330,6 +10386,7 @@ TIMESTAMP = kw:"TIMESTAMP"i !ident_part { return loc(createK
TIMEZONE = kw:"TIMEZONE"i !ident_part { return loc(createKeyword(kw)); }
TIMEZONE_HOUR = kw:"TIMEZONE_HOUR"i !ident_part { return loc(createKeyword(kw)); }
TIMEZONE_MINUTE = kw:"TIMEZONE_MINUTE"i !ident_part { return loc(createKeyword(kw)); }
TIMING = kw:"TIMING"i !ident_part { return loc(createKeyword(kw)); }
TINYBLOB = kw:"TINYBLOB"i !ident_part { return loc(createKeyword(kw)); }
TINYINT = kw:"TINYINT"i !ident_part { return loc(createKeyword(kw)); }
TINYTEXT = kw:"TINYTEXT"i !ident_part { return loc(createKeyword(kw)); }
Expand Down Expand Up @@ -10378,6 +10435,7 @@ VIRTUAL = kw:"VIRTUAL"i !ident_part { return loc(createK
VISIBLE = kw:"VISIBLE"i !ident_part { return loc(createKeyword(kw)); }
VOLATILE = kw:"VOLATILE"i !ident_part { return loc(createKeyword(kw)); }
WAIT = kw:"WAIT"i !ident_part { return loc(createKeyword(kw)); }
WAL = kw:"WAL"i !ident_part { return loc(createKeyword(kw)); }
WARNING = kw:"WARNING"i !ident_part { return loc(createKeyword(kw)); }
WEDNESDAY = kw:"WEDNESDAY"i !ident_part { return loc(createKeyword(kw)); }
WEEK = kw:"WEEK"i !ident_part { return loc(createKeyword(kw)); }
Expand All @@ -10390,7 +10448,9 @@ WITHOUT = kw:"WITHOUT"i !ident_part { return loc(createK
WORK = kw:"WORK"i !ident_part { return loc(createKeyword(kw)); }
WRAPPER = kw:"WRAPPER"i !ident_part { return loc(createKeyword(kw)); }
WRITE = kw:"WRITE"i !ident_part { return loc(createKeyword(kw)); }
XML = kw:"XML"i !ident_part { return loc(createKeyword(kw)); }
XOR = kw:"XOR"i !ident_part { return loc(createKeyword(kw)); }
YAML = kw:"YAML"i !ident_part { return loc(createKeyword(kw)); }
YEAR = kw:"YEAR"i !ident_part { return loc(createKeyword(kw)); }
YEAR_MONTH = kw:"YEAR_MONTH"i !ident_part { return loc(createKeyword(kw)); }
ZEROFILL = kw:"ZEROFILL"i !ident_part { return loc(createKeyword(kw)); }
Expand Down
14 changes: 11 additions & 3 deletions src/showNode/explain.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { show } from "../show";
import { ExplainStmt } from "../cst/Node";
import { AllExplainNodes } from "../cst/Node";
import { FullTransformMap } from "../cstTransformer";

export const explainMap: FullTransformMap<string, ExplainStmt> = {
export const explainMap: FullTransformMap<string, AllExplainNodes> = {
explain_stmt: (node) =>
show([node.explainKw, node.analyzeKw, node.queryPlanKw, node.statement]),
show([
node.explainKw,
node.analyzeKw,
node.verboseKw,
node.options,
node.queryPlanKw,
node.statement,
]),
explain_option: (node) => show([node.name, node.value]),
};
130 changes: 129 additions & 1 deletion test/explain.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dialect, test, testWc } from "./test_utils";
import { dialect, parseStmt, test, testWc } from "./test_utils";

describe("explain", () => {
dialect(["mysql", "mariadb", "sqlite", "postgresql", "plpgsql"], () => {
Expand All @@ -25,6 +25,134 @@ describe("explain", () => {
});
});

dialect(["postgresql", "plpgsql"], () => {
it("supports EXPLAIN VERBOSE", () => {
testWc("EXPLAIN VERBOSE SELECT 1");
testWc("EXPLAIN ANALYZE VERBOSE SELECT 1");
});

describe("explain options", () => {
it("supports single option", () => {
testWc("EXPLAIN (ANALYZE) SELECT 1");
testWc("EXPLAIN (VERBOSE) SELECT 1");
});

it("supports multiple options", () => {
testWc("EXPLAIN ( ANALYZE, BUFFERS, COSTS false ) SELECT 1");
});

it("supports all option names", () => {
testWc(
"EXPLAIN (ANALYZE, VERBOSE, COSTS, SETTINGS, GENERIC_PLAN, BUFFERS, SERIALIZE, " +
"WAL, TIMING, SUMMARY, MEMORY, FORMAT TEXT) SELECT 1"
);
});

it("supports boolean values", () => {
testWc("EXPLAIN (ANALYZE true, TIMING false) SELECT 1");
testWc("EXPLAIN (ANALYZE ON, TIMING OFF) SELECT 1");
testWc("EXPLAIN (ANALYZE 1, TIMING 0) SELECT 1");
});

it("supports FORMAT", () => {
testWc("EXPLAIN (FORMAT TEXT) SELECT 1");
testWc("EXPLAIN (FORMAT XML) SELECT 1");
testWc("EXPLAIN (FORMAT JSON) SELECT 1");
testWc("EXPLAIN (FORMAT YAML) SELECT 1");
testWc("EXPLAIN (FORMAT 'json') SELECT 1");
});

it("supports SERIALIZE", () => {
testWc("EXPLAIN (ANALYZE, SERIALIZE NONE) SELECT 1");
testWc("EXPLAIN (ANALYZE, SERIALIZE TEXT) SELECT 1");
testWc("EXPLAIN (ANALYZE, SERIALIZE BINARY) SELECT 1");
});

it("supports options with non-SELECT statements", () => {
testWc("EXPLAIN (ANALYZE, BUFFERS) UPDATE tbl SET x = 1");
testWc("EXPLAIN (ANALYZE, BUFFERS) CREATE TABLE foo AS SELECT 1");
});

it("still supports EXPLAIN of parenthesized SELECT", () => {
testWc("EXPLAIN (SELECT 1)");
});

it("parses options", () => {
expect(parseStmt("EXPLAIN (ANALYZE, FORMAT JSON) SELECT 1")).toMatchInlineSnapshot(`
{
"explainKw": {
"name": "EXPLAIN",
"text": "EXPLAIN",
"type": "keyword",
},
"options": {
"expr": {
"items": [
{
"name": {
"name": "ANALYZE",
"text": "ANALYZE",
"type": "keyword",
},
"type": "explain_option",
"value": undefined,
},
{
"name": {
"name": "FORMAT",
"text": "FORMAT",
"type": "keyword",
},
"type": "explain_option",
"value": {
"name": "JSON",
"text": "JSON",
"type": "keyword",
},
},
],
"type": "list_expr",
},
"type": "paren_expr",
},
"statement": {
"clauses": [
{
"columns": {
"items": [
{
"text": "1",
"type": "number_literal",
"value": 1,
},
],
"type": "list_expr",
},
"modifiers": [],
"selectKw": {
"name": "SELECT",
"text": "SELECT",
"type": "keyword",
},
"type": "select_clause",
},
],
"type": "select_stmt",
},
"type": "explain_stmt",
}
`);
});
});
});

dialect(["mysql", "mariadb", "sqlite"], () => {
it("does not support EXPLAIN options", () => {
expect(() => test("EXPLAIN (ANALYZE) SELECT 1")).toThrow();
expect(() => test("EXPLAIN VERBOSE SELECT 1")).toThrow();
});
});

dialect(["mysql", "mariadb"], () => {
it("supports DESCRIBE/DESC instead of EXPLAIN", () => {
testWc("DESCRIBE SELECT 1");
Expand Down
Loading