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

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

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

/// Treats literal prefixes immediately after `.` as unquoted identifiers.
///
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/identifiers.html),
/// [MariaDB](https://mariadb.com/docs/server/reference/sql-structure/sql-language-structure/identifier-names)
fn supports_literal_prefix_in_qualified_name(&self) -> bool {
false
}

/// Determine whether the dialect strips the backslash when escaping LIKE wildcards (%, _).
///
/// [MySQL] has a special case when escaping single quoted strings which leaves these unescaped
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ impl Dialect for MySqlDialect {
true
}

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

fn supports_bitwise_shift_operators(&self) -> bool {
true
}
Expand Down
6 changes: 6 additions & 0 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,12 @@ impl<'a> Tokenizer<'a> {
) -> Result<Option<Token>, TokenizerError> {
match chars.peek() {
Some(&ch) => match ch {
ch if prev_token == Some(&Token::Period)
&& ch.is_ascii_alphabetic()
&& self.dialect.supports_literal_prefix_in_qualified_name() =>
{
self.tokenize_identifier_or_keyword([ch], chars)
}
' ' => self.consume_and_return(chars, Token::Whitespace(Whitespace::Space)),
'\t' => self.consume_and_return(chars, Token::Whitespace(Whitespace::Tab)),
'\n' => self.consume_and_return(chars, Token::Whitespace(Whitespace::Newline)),
Expand Down
62 changes: 62 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21022,3 +21022,65 @@ fn parse_window_spec_keyword_base_window_name() {
"SELECT count(*) OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM t",
);
}

#[test]
fn parse_qualified_column_named_like_a_literal_prefix() {
let dialects = all_dialects_where(|d| d.supports_literal_prefix_in_qualified_name());
for (prefix, value) in [
("x", "ff"),
("X", "ff"),
("b", "01"),
("B", "01"),
("n", "nat"),
("N", "nat"),
("r", "raw"),
("R", "raw"),
("e", "es"),
("E", "es"),
] {
dialects.one_statement_parses_to(
&format!("SELECT t.{prefix}'{value}' FROM t"),
&format!("SELECT t.{prefix} AS '{value}' FROM t"),
);
}
dialects.one_statement_parses_to("SELECT t.u&'uni' FROM t", "SELECT t.u & 'uni' FROM t");
}

#[test]
fn tokenize_qualified_literal_prefix_without_capability() {
all_dialects_where(|d| !d.supports_literal_prefix_in_qualified_name()).tokenizes_to(
"t.x'ff'",
vec![
Token::make_word("t", None),
Token::Period,
Token::HexStringLiteral("ff".to_string()),
],
);
}

#[test]
fn parse_qualified_literal_prefix_requires_adjacent_period() {
let dialects = all_dialects_where(|d| d.supports_literal_prefix_in_qualified_name());
for sql in [
"SELECT t. x'ff' FROM t",
"SELECT t./* comment */x'ff' FROM t",
] {
assert_eq!(
dialects.parse_sql_statements(sql),
Err(ParserError::ParserError(
"Expected: an identifier or a '*' after '.', found: X'ff'".to_string()
)),
"{sql}"
);
}
for (sql, token) in [
("x'ff'", Token::HexStringLiteral("ff".to_string())),
(
"b'01'",
Token::SingleQuotedByteStringLiteral("01".to_string()),
),
("n'nat'", Token::NationalStringLiteral("nat".to_string())),
] {
dialects.tokenizes_to(sql, vec![token]);
}
}
Loading