Skip to content

MySQL: EXPLAIN ANALYZE format type #1945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 23 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4126,7 +4126,7 @@ pub enum Statement {
/// A SQL query that specifies what to explain
statement: Box<Statement>,
/// Optional output format of explain
format: Option<AnalyzeFormat>,
format: Option<AnalyzeFormatKind>,
/// Postgres style utility options, `(analyze, verbose true)`
options: Option<Vec<UtilityOption>>,
},
Expand Down Expand Up @@ -4494,7 +4494,7 @@ impl fmt::Display for Statement {
}

if let Some(format) = format {
write!(f, "FORMAT {format} ")?;
write!(f, "{format} ")?;
}

if let Some(options) = options {
Expand Down Expand Up @@ -7641,13 +7641,32 @@ impl fmt::Display for DuplicateTreatment {
}
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum AnalyzeFormatKind {
Keyword(AnalyzeFormat),
Assignment(AnalyzeFormat),
}

impl fmt::Display for AnalyzeFormatKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
AnalyzeFormatKind::Keyword(format) => write!(f, "FORMAT {format}"),
AnalyzeFormatKind::Assignment(format) => write!(f, "FORMAT={format}"),
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum AnalyzeFormat {
TEXT,
GRAPHVIZ,
JSON,
TRADITIONAL,
TREE,
}

impl fmt::Display for AnalyzeFormat {
Expand All @@ -7656,6 +7675,8 @@ impl fmt::Display for AnalyzeFormat {
AnalyzeFormat::TEXT => "TEXT",
AnalyzeFormat::GRAPHVIZ => "GRAPHVIZ",
AnalyzeFormat::JSON => "JSON",
AnalyzeFormat::TRADITIONAL => "TRADITIONAL",
AnalyzeFormat::TREE => "TREE",
})
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5666,6 +5666,14 @@ impl<'a> Parser<'a> {
}
}

fn parse_analyze_format_kind(&mut self) -> Result<AnalyzeFormatKind, ParserError> {
if self.consume_token(&Token::Eq) {
Ok(AnalyzeFormatKind::Assignment(self.parse_analyze_format()?))
} else {
Ok(AnalyzeFormatKind::Keyword(self.parse_analyze_format()?))
}
}

pub fn parse_analyze_format(&mut self) -> Result<AnalyzeFormat, ParserError> {
let next_token = self.next_token();
match &next_token.token {
Expand Down Expand Up @@ -11064,7 +11072,7 @@ impl<'a> Parser<'a> {
analyze = self.parse_keyword(Keyword::ANALYZE);
verbose = self.parse_keyword(Keyword::VERBOSE);
if self.parse_keyword(Keyword::FORMAT) {
format = Some(self.parse_analyze_format()?);
format = Some(self.parse_analyze_format_kind()?);
}
}

Expand Down
17 changes: 13 additions & 4 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5161,7 +5161,7 @@ fn run_explain_analyze(
query: &str,
expected_verbose: bool,
expected_analyze: bool,
expected_format: Option<AnalyzeFormat>,
expected_format: Option<AnalyzeFormatKind>,
expected_options: Option<Vec<UtilityOption>>,
) {
match dialect.verified_stmt(query) {
Expand Down Expand Up @@ -5272,7 +5272,7 @@ fn parse_explain_analyze_with_simple_select() {
"EXPLAIN ANALYZE FORMAT GRAPHVIZ SELECT sqrt(id) FROM foo",
false,
true,
Some(AnalyzeFormat::GRAPHVIZ),
Some(AnalyzeFormatKind::Keyword(AnalyzeFormat::GRAPHVIZ)),
None,
);

Expand All @@ -5281,7 +5281,16 @@ fn parse_explain_analyze_with_simple_select() {
"EXPLAIN ANALYZE VERBOSE FORMAT JSON SELECT sqrt(id) FROM foo",
true,
true,
Some(AnalyzeFormat::JSON),
Some(AnalyzeFormatKind::Keyword(AnalyzeFormat::JSON)),
None,
);

run_explain_analyze(
all_dialects(),
"EXPLAIN ANALYZE VERBOSE FORMAT=JSON SELECT sqrt(id) FROM foo",
true,
true,
Some(AnalyzeFormatKind::Assignment(AnalyzeFormat::JSON)),
None,
);

Expand All @@ -5290,7 +5299,7 @@ fn parse_explain_analyze_with_simple_select() {
"EXPLAIN VERBOSE FORMAT TEXT SELECT sqrt(id) FROM foo",
true,
false,
Some(AnalyzeFormat::TEXT),
Some(AnalyzeFormatKind::Keyword(AnalyzeFormat::TEXT)),
None,
);
}
Expand Down