Skip to content

Commit 23f40cd

Browse files
authored
MySQL: Support EXPLAIN ANALYZE format variants (#1945)
1 parent 40bbcc9 commit 23f40cd

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/ast/mod.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4126,7 +4126,7 @@ pub enum Statement {
41264126
/// A SQL query that specifies what to explain
41274127
statement: Box<Statement>,
41284128
/// Optional output format of explain
4129-
format: Option<AnalyzeFormat>,
4129+
format: Option<AnalyzeFormatKind>,
41304130
/// Postgres style utility options, `(analyze, verbose true)`
41314131
options: Option<Vec<UtilityOption>>,
41324132
},
@@ -4494,7 +4494,7 @@ impl fmt::Display for Statement {
44944494
}
44954495

44964496
if let Some(format) = format {
4497-
write!(f, "FORMAT {format} ")?;
4497+
write!(f, "{format} ")?;
44984498
}
44994499

45004500
if let Some(options) = options {
@@ -7641,13 +7641,34 @@ impl fmt::Display for DuplicateTreatment {
76417641
}
76427642
}
76437643

7644+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7645+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7646+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7647+
pub enum AnalyzeFormatKind {
7648+
/// e.g. `EXPLAIN ANALYZE FORMAT JSON SELECT * FROM tbl`
7649+
Keyword(AnalyzeFormat),
7650+
/// e.g. `EXPLAIN ANALYZE FORMAT=JSON SELECT * FROM tbl`
7651+
Assignment(AnalyzeFormat),
7652+
}
7653+
7654+
impl fmt::Display for AnalyzeFormatKind {
7655+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
7656+
match self {
7657+
AnalyzeFormatKind::Keyword(format) => write!(f, "FORMAT {format}"),
7658+
AnalyzeFormatKind::Assignment(format) => write!(f, "FORMAT={format}"),
7659+
}
7660+
}
7661+
}
7662+
76447663
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
76457664
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
76467665
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
76477666
pub enum AnalyzeFormat {
76487667
TEXT,
76497668
GRAPHVIZ,
76507669
JSON,
7670+
TRADITIONAL,
7671+
TREE,
76517672
}
76527673

76537674
impl fmt::Display for AnalyzeFormat {
@@ -7656,6 +7677,8 @@ impl fmt::Display for AnalyzeFormat {
76567677
AnalyzeFormat::TEXT => "TEXT",
76577678
AnalyzeFormat::GRAPHVIZ => "GRAPHVIZ",
76587679
AnalyzeFormat::JSON => "JSON",
7680+
AnalyzeFormat::TRADITIONAL => "TRADITIONAL",
7681+
AnalyzeFormat::TREE => "TREE",
76597682
})
76607683
}
76617684
}

src/parser/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5674,6 +5674,14 @@ impl<'a> Parser<'a> {
56745674
}
56755675
}
56765676

5677+
fn parse_analyze_format_kind(&mut self) -> Result<AnalyzeFormatKind, ParserError> {
5678+
if self.consume_token(&Token::Eq) {
5679+
Ok(AnalyzeFormatKind::Assignment(self.parse_analyze_format()?))
5680+
} else {
5681+
Ok(AnalyzeFormatKind::Keyword(self.parse_analyze_format()?))
5682+
}
5683+
}
5684+
56775685
pub fn parse_analyze_format(&mut self) -> Result<AnalyzeFormat, ParserError> {
56785686
let next_token = self.next_token();
56795687
match &next_token.token {
@@ -11074,7 +11082,7 @@ impl<'a> Parser<'a> {
1107411082
analyze = self.parse_keyword(Keyword::ANALYZE);
1107511083
verbose = self.parse_keyword(Keyword::VERBOSE);
1107611084
if self.parse_keyword(Keyword::FORMAT) {
11077-
format = Some(self.parse_analyze_format()?);
11085+
format = Some(self.parse_analyze_format_kind()?);
1107811086
}
1107911087
}
1108011088

tests/sqlparser_common.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5161,7 +5161,7 @@ fn run_explain_analyze(
51615161
query: &str,
51625162
expected_verbose: bool,
51635163
expected_analyze: bool,
5164-
expected_format: Option<AnalyzeFormat>,
5164+
expected_format: Option<AnalyzeFormatKind>,
51655165
expected_options: Option<Vec<UtilityOption>>,
51665166
) {
51675167
match dialect.verified_stmt(query) {
@@ -5272,7 +5272,7 @@ fn parse_explain_analyze_with_simple_select() {
52725272
"EXPLAIN ANALYZE FORMAT GRAPHVIZ SELECT sqrt(id) FROM foo",
52735273
false,
52745274
true,
5275-
Some(AnalyzeFormat::GRAPHVIZ),
5275+
Some(AnalyzeFormatKind::Keyword(AnalyzeFormat::GRAPHVIZ)),
52765276
None,
52775277
);
52785278

@@ -5281,7 +5281,16 @@ fn parse_explain_analyze_with_simple_select() {
52815281
"EXPLAIN ANALYZE VERBOSE FORMAT JSON SELECT sqrt(id) FROM foo",
52825282
true,
52835283
true,
5284-
Some(AnalyzeFormat::JSON),
5284+
Some(AnalyzeFormatKind::Keyword(AnalyzeFormat::JSON)),
5285+
None,
5286+
);
5287+
5288+
run_explain_analyze(
5289+
all_dialects(),
5290+
"EXPLAIN ANALYZE VERBOSE FORMAT=JSON SELECT sqrt(id) FROM foo",
5291+
true,
5292+
true,
5293+
Some(AnalyzeFormatKind::Assignment(AnalyzeFormat::JSON)),
52855294
None,
52865295
);
52875296

@@ -5290,7 +5299,7 @@ fn parse_explain_analyze_with_simple_select() {
52905299
"EXPLAIN VERBOSE FORMAT TEXT SELECT sqrt(id) FROM foo",
52915300
true,
52925301
false,
5293-
Some(AnalyzeFormat::TEXT),
5302+
Some(AnalyzeFormatKind::Keyword(AnalyzeFormat::TEXT)),
52945303
None,
52955304
);
52965305
}

0 commit comments

Comments
 (0)