Skip to content
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
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ impl Dialect for GenericDialect {
true
}

fn supports_table_partitions(&self) -> bool {
true
}

fn supports_select_format(&self) -> bool {
true
}
Expand Down
13 changes: 13 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,19 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if this dialect supports the `PARTITION` clause on a table factor,
/// restricting a query to an explicit list of partitions.
///
/// Example:
/// ```sql
/// SELECT * FROM employees PARTITION (p0, p1)
/// ```
///
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/partitioning-selection.html)
fn supports_table_partitions(&self) -> bool {
false
}

/// Returns true if this dialect supports the `FORMAT` clause in `SELECT` statements.
///
/// Example:
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ impl Dialect for MySqlDialect {
true
}

/// See: <https://dev.mysql.com/doc/refman/8.4/en/partitioning-selection.html>
fn supports_table_partitions(&self) -> bool {
true
}

fn supports_comment_optimizer_hint(&self) -> bool {
true
}
Expand Down
2 changes: 1 addition & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16753,7 +16753,7 @@ impl<'a> Parser<'a> {
_ => None,
};

let partitions: Vec<Ident> = if dialect_of!(self is MySqlDialect | GenericDialect)
let partitions: Vec<Ident> = if self.dialect.supports_table_partitions()
&& self.parse_keyword(Keyword::PARTITION)
{
self.parse_parenthesized_identifiers()?
Expand Down
41 changes: 41 additions & 0 deletions tests/sqlparser_custom_dialect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,47 @@ fn test_map_syntax_not_support_default() -> Result<(), ParserError> {
Ok(())
}

#[test]
fn custom_dialect_supports_table_partitions() -> Result<(), ParserError> {
#[derive(Debug)]
struct MyDialect {}

impl Dialect for MyDialect {
fn is_identifier_start(&self, ch: char) -> bool {
is_identifier_start(ch)
}

fn is_identifier_part(&self, ch: char) -> bool {
is_identifier_part(ch)
}

fn supports_table_partitions(&self) -> bool {
true
}
}

let sql = "SELECT * FROM employees PARTITION (p0, p1)";
let ast = Parser::parse_sql(&MyDialect {}, sql)?;
assert_eq!(sql, &format!("{}", ast[0]));

// A dialect that does not opt in still rejects the clause.
#[derive(Debug)]
struct WithoutPartitions {}

impl Dialect for WithoutPartitions {
fn is_identifier_start(&self, ch: char) -> bool {
is_identifier_start(ch)
}

fn is_identifier_part(&self, ch: char) -> bool {
is_identifier_part(ch)
}
}

assert!(Parser::parse_sql(&WithoutPartitions {}, sql).is_err());
Ok(())
}

fn is_identifier_start(ch: char) -> bool {
ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch == '_'
}
Expand Down