Skip to content

Commit 8e4b909

Browse files
yoavcloudayman-sigma
authored andcommitted
MySQL: Support EXPLAIN ANALYZE format variants (apache#1945)
1 parent 89dde29 commit 8e4b909

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
@@ -4155,7 +4155,7 @@ pub enum Statement {
41554155
/// A SQL query that specifies what to explain
41564156
statement: Box<Statement>,
41574157
/// Optional output format of explain
4158-
format: Option<AnalyzeFormat>,
4158+
format: Option<AnalyzeFormatKind>,
41594159
/// Postgres style utility options, `(analyze, verbose true)`
41604160
options: Option<Vec<UtilityOption>>,
41614161
},
@@ -4523,7 +4523,7 @@ impl fmt::Display for Statement {
45234523
}
45244524

45254525
if let Some(format) = format {
4526-
write!(f, "FORMAT {format} ")?;
4526+
write!(f, "{format} ")?;
45274527
}
45284528

45294529
if let Some(options) = options {
@@ -7670,13 +7670,34 @@ impl fmt::Display for DuplicateTreatment {
76707670
}
76717671
}
76727672

7673+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
7674+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7675+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
7676+
pub enum AnalyzeFormatKind {
7677+
/// e.g. `EXPLAIN ANALYZE FORMAT JSON SELECT * FROM tbl`
7678+
Keyword(AnalyzeFormat),
7679+
/// e.g. `EXPLAIN ANALYZE FORMAT=JSON SELECT * FROM tbl`
7680+
Assignment(AnalyzeFormat),
7681+
}
7682+
7683+
impl fmt::Display for AnalyzeFormatKind {
7684+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
7685+
match self {
7686+
AnalyzeFormatKind::Keyword(format) => write!(f, "FORMAT {format}"),
7687+
AnalyzeFormatKind::Assignment(format) => write!(f, "FORMAT={format}"),
7688+
}
7689+
}
7690+
}
7691+
76737692
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
76747693
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
76757694
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
76767695
pub enum AnalyzeFormat {
76777696
TEXT,
76787697
GRAPHVIZ,
76797698
JSON,
7699+
TRADITIONAL,
7700+
TREE,
76807701
}
76817702

76827703
impl fmt::Display for AnalyzeFormat {
@@ -7685,6 +7706,8 @@ impl fmt::Display for AnalyzeFormat {
76857706
AnalyzeFormat::TEXT => "TEXT",
76867707
AnalyzeFormat::GRAPHVIZ => "GRAPHVIZ",
76877708
AnalyzeFormat::JSON => "JSON",
7709+
AnalyzeFormat::TRADITIONAL => "TRADITIONAL",
7710+
AnalyzeFormat::TREE => "TREE",
76887711
})
76897712
}
76907713
}

src/parser/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5705,6 +5705,14 @@ impl<'a> Parser<'a> {
57055705
}
57065706
}
57075707

5708+
fn parse_analyze_format_kind(&mut self) -> Result<AnalyzeFormatKind, ParserError> {
5709+
if self.consume_token(&Token::Eq) {
5710+
Ok(AnalyzeFormatKind::Assignment(self.parse_analyze_format()?))
5711+
} else {
5712+
Ok(AnalyzeFormatKind::Keyword(self.parse_analyze_format()?))
5713+
}
5714+
}
5715+
57085716
pub fn parse_analyze_format(&mut self) -> Result<AnalyzeFormat, ParserError> {
57095717
let next_token = self.next_token();
57105718
match &next_token.token {
@@ -11105,7 +11113,7 @@ impl<'a> Parser<'a> {
1110511113
analyze = self.parse_keyword(Keyword::ANALYZE);
1110611114
verbose = self.parse_keyword(Keyword::VERBOSE);
1110711115
if self.parse_keyword(Keyword::FORMAT) {
11108-
format = Some(self.parse_analyze_format()?);
11116+
format = Some(self.parse_analyze_format_kind()?);
1110911117
}
1111011118
}
1111111119

tests/sqlparser_common.rs

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

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

@@ -5291,7 +5300,7 @@ fn parse_explain_analyze_with_simple_select() {
52915300
"EXPLAIN VERBOSE FORMAT TEXT SELECT sqrt(id) FROM foo",
52925301
true,
52935302
false,
5294-
Some(AnalyzeFormat::TEXT),
5303+
Some(AnalyzeFormatKind::Keyword(AnalyzeFormat::TEXT)),
52955304
None,
52965305
);
52975306
}

0 commit comments

Comments
 (0)