Skip to content

Commit 330de88

Browse files
committed
MySQL: EXPLAIN ANALYZE format type
1 parent 6506814 commit 330de88

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

src/ast/mod.rs

Lines changed: 23 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,32 @@ 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+
Keyword(AnalyzeFormat),
7649+
Assignment(AnalyzeFormat)
7650+
}
7651+
7652+
impl fmt::Display for AnalyzeFormatKind {
7653+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
7654+
match self {
7655+
AnalyzeFormatKind::Keyword(format) => write!(f, "FORMAT {format}"),
7656+
AnalyzeFormatKind::Assignment(format) => write!(f, "FORMAT={format}"),
7657+
}
7658+
}
7659+
}
7660+
76447661
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
76457662
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
76467663
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
76477664
pub enum AnalyzeFormat {
76487665
TEXT,
76497666
GRAPHVIZ,
76507667
JSON,
7668+
TRADITIONAL,
7669+
TREE
76517670
}
76527671

76537672
impl fmt::Display for AnalyzeFormat {
@@ -7656,6 +7675,8 @@ impl fmt::Display for AnalyzeFormat {
76567675
AnalyzeFormat::TEXT => "TEXT",
76577676
AnalyzeFormat::GRAPHVIZ => "GRAPHVIZ",
76587677
AnalyzeFormat::JSON => "JSON",
7678+
AnalyzeFormat::TRADITIONAL => "TRADITIONAL",
7679+
AnalyzeFormat::TREE => "TREE",
76597680
})
76607681
}
76617682
}

src/parser/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5666,6 +5666,14 @@ impl<'a> Parser<'a> {
56665666
}
56675667
}
56685668

5669+
fn parse_analyze_format_kind(&mut self) -> Result<AnalyzeFormatKind, ParserError> {
5670+
if self.consume_token(&Token::Eq) {
5671+
Ok(AnalyzeFormatKind::Assignment(self.parse_analyze_format()?))
5672+
} else {
5673+
Ok(AnalyzeFormatKind::Keyword(self.parse_analyze_format()?))
5674+
}
5675+
}
5676+
56695677
pub fn parse_analyze_format(&mut self) -> Result<AnalyzeFormat, ParserError> {
56705678
let next_token = self.next_token();
56715679
match &next_token.token {
@@ -11064,7 +11072,7 @@ impl<'a> Parser<'a> {
1106411072
analyze = self.parse_keyword(Keyword::ANALYZE);
1106511073
verbose = self.parse_keyword(Keyword::VERBOSE);
1106611074
if self.parse_keyword(Keyword::FORMAT) {
11067-
format = Some(self.parse_analyze_format()?);
11075+
format = Some(self.parse_analyze_format_kind()?);
1106811076
}
1106911077
}
1107011078

tests/sqlparser_common.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5285,6 +5285,15 @@ fn parse_explain_analyze_with_simple_select() {
52855285
None,
52865286
);
52875287

5288+
run_explain_analyze(
5289+
all_dialects(),
5290+
"EXPLAIN ANALYZE VERBOSE FORMAT=JSON SELECT sqrt(id) FROM foo",
5291+
true,
5292+
true,
5293+
Some(AnalyzeFormat::JSON),
5294+
None,
5295+
);
5296+
52885297
run_explain_analyze(
52895298
all_dialects(),
52905299
"EXPLAIN VERBOSE FORMAT TEXT SELECT sqrt(id) FROM foo",

0 commit comments

Comments
 (0)