Skip to content

Add support for SHOW CHARSET #1974

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

Merged
merged 3 commits into from
Jul 29, 2025
Merged
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
33 changes: 33 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3704,6 +3704,12 @@ pub enum Statement {
history: bool,
show_options: ShowStatementOptions,
},
// ```sql
// SHOW {CHARACTER SET | CHARSET}
// ```
// [MySQL]:
// <https://dev.mysql.com/doc/refman/8.4/en/show.html#:~:text=SHOW%20%7BCHARACTER%20SET%20%7C%20CHARSET%7D%20%5Blike_or_where%5D>
ShowCharset(ShowCharset),
/// ```sql
/// SHOW OBJECTS LIKE 'line%' IN mydb.public
/// ```
Expand Down Expand Up @@ -5674,6 +5680,7 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::ShowCharset(show_stm) => show_stm.fmt(f),
Statement::StartTransaction {
modes,
begin: syntax_begin,
Expand Down Expand Up @@ -9792,6 +9799,32 @@ impl fmt::Display for ShowStatementIn {
}
}

/// A Show Charset statement
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct ShowCharset {
/// The statement can be written as `SHOW CHARSET` or `SHOW CHARACTER SET`
/// true means CHARSET was used and false means CHARACTER SET was used
pub is_shorthand: bool,
pub filter: Option<ShowStatementFilter>,
}

impl fmt::Display for ShowCharset {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "SHOW")?;
if self.is_shorthand {
write!(f, " CHARSET")?;
} else {
write!(f, " CHARACTER SET")?;
}
if self.filter.is_some() {
write!(f, " {}", self.filter.as_ref().unwrap())?;
}
Ok(())
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ impl Spanned for Statement {
Statement::ShowColumns { .. } => Span::empty(),
Statement::ShowTables { .. } => Span::empty(),
Statement::ShowCollation { .. } => Span::empty(),
Statement::ShowCharset { .. } => Span::empty(),
Statement::Use(u) => u.span(),
Statement::StartTransaction { .. } => Span::empty(),
Statement::Comment { .. } => Span::empty(),
Expand Down
12 changes: 12 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12505,13 +12505,25 @@ impl<'a> Parser<'a> {
self.parse_show_databases(terse)
} else if self.parse_keyword(Keyword::SCHEMAS) {
self.parse_show_schemas(terse)
} else if self.parse_keywords(&[Keyword::CHARACTER, Keyword::SET]) {
self.parse_show_charset(false)
} else if self.parse_keyword(Keyword::CHARSET) {
self.parse_show_charset(true)
} else {
Ok(Statement::ShowVariable {
variable: self.parse_identifiers()?,
})
}
}

fn parse_show_charset(&mut self, is_shorthand: bool) -> Result<Statement, ParserError> {
// parse one of keywords
Ok(Statement::ShowCharset(ShowCharset {
is_shorthand,
filter: self.parse_show_statement_filter()?,
}))
}

fn parse_show_databases(&mut self, terse: bool) -> Result<Statement, ParserError> {
let history = self.parse_keyword(Keyword::HISTORY);
let show_options = self.parse_show_stmt_options()?;
Expand Down
15 changes: 15 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4143,3 +4143,18 @@ fn parse_json_member_of() {
_ => panic!("Unexpected statement {stmt}"),
}
}

#[test]
fn parse_show_charset() {
let res = mysql().verified_stmt("SHOW CHARACTER SET");
assert_eq!(
res,
Statement::ShowCharset(ShowCharset {
is_shorthand: false,
filter: None
})
);
mysql().verified_stmt("SHOW CHARACTER SET LIKE 'utf8mb4%'");
mysql().verified_stmt("SHOW CHARSET WHERE charset = 'utf8mb4%'");
mysql().verified_stmt("SHOW CHARSET LIKE 'utf8mb4%'");
}
Loading