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
8 changes: 8 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,14 @@ pub trait Dialect: Debug + Any {
explicit || self.is_table_alias(kw, parser)
}

/// Returns true if a single-quoted string can be an alias, as in `SELECT 1 AS 'x'`.
///
/// [MySQL](https://dev.mysql.com/doc/refman/8.4/en/problems-with-alias.html),
/// [SQLite](https://www.sqlite.org/lang_keywords.html)
fn supports_string_literal_alias(&self) -> bool {
true
}

/// Returns true if this dialect supports querying historical table data
/// by specifying which version of the data to query.
fn supports_table_versioning(&self) -> bool {
Expand Down
6 changes: 6 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ impl Dialect for PostgreSqlDialect {
|| RESERVED_EXCLUSIONS_FOR_TABLE_ALIAS.contains(kw)
}

/// Aliases are identifiers, see `output_name` and `alias` in
/// <https://www.postgresql.org/docs/current/sql-select.html>
fn supports_string_literal_alias(&self) -> bool {
false
}

/// See <https://www.postgresql.org/docs/current/sql-createoperator.html>
fn is_custom_operator_part(&self, ch: char) -> bool {
matches!(
Expand Down
5 changes: 3 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13739,8 +13739,9 @@ impl<'a> Parser<'a> {
Token::Word(w) if validator(after_as, &w.keyword, self) => {
Ok(Some(w.into_ident(next_token.span)))
}
// For backwards-compatibility, we accept quoted strings as aliases regardless of the context.
Token::SingleQuotedString(s) => Ok(Some(Ident::with_quote('\'', s))),
Token::SingleQuotedString(s) if self.dialect.supports_string_literal_alias() => {
Ok(Some(Ident::with_quote('\'', s)))
}
Token::DoubleQuotedString(s) => Ok(Some(Ident::with_quote('\"', s))),
_ => {
if after_as {
Expand Down
8 changes: 8 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20699,3 +20699,11 @@ fn parse_cte_materialization() {
}
}
}

#[test]
fn parse_string_literal_alias() {
let dialects = all_dialects_where(|d| d.supports_string_literal_alias());
dialects.one_statement_parses_to("SELECT 1 'x'", "SELECT 1 AS 'x'");
dialects.verified_stmt("SELECT 1 AS 'x'");
dialects.verified_stmt("SELECT a FROM t AS 'y'");
}
30 changes: 30 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10172,3 +10172,33 @@ fn parse_join_using_alias() {
select.to_string()
);
}

#[test]
fn parse_string_literal_alias_rejected() {
for (sql, expected) in [
("SELECT 1 'x'", "Expected: end of statement, found: 'x'"),
(
"SELECT (a) 'x' FROM t",
"Expected: end of statement, found: 'x'",
),
(
"SELECT a AS 'x' FROM t",
"Expected: an identifier after AS, found: 'x'",
),
(
"SELECT a FROM t 'x'",
"Expected: end of statement, found: 'x'",
),
(
"SELECT a FROM t AS 'x'",
"Expected: an identifier after AS, found: 'x'",
),
] {
assert_eq!(
pg().parse_sql_statements(sql).unwrap_err(),
ParserError::ParserError(expected.to_string()),
"{sql}"
);
}
pg().verified_stmt(r#"SELECT a AS "x" FROM t AS "y""#);
}
Loading