You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datafusion.apache.org by gi...@apache.org on 2026/10/18 10:17:38 UTC
(datafusion-sqlparser-rs) branch gh-readonly-queue/main/pr-2668-4ad878405805b0a0c0df347acd91845c4745cff5 created (now 66e0e8985)
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a change to branch gh-readonly-queue/main/pr-2668-4ad878405805b0a0c0df347acd91845c4745cff5
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
at 66e0e8985 PostgreSQL: Reject string literals as aliases (#2668)
This branch includes the following new commits:
new 66e0e8985 PostgreSQL: Reject string literals as aliases (#2668)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org
(datafusion-sqlparser-rs) 01/01: PostgreSQL: Reject string literals as aliases (#2668)
Posted by gi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-2668-4ad878405805b0a0c0df347acd91845c4745cff5
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
commit 66e0e89852c34bf0ddb0a6eb767d76e6aa19f0c4
Author: Luca Cappelletti <ca...@gmail.com>
AuthorDate: Sun Oct 18 10:17:11 2026 +0000
PostgreSQL: Reject string literals as aliases (#2668)
---
src/dialect/mod.rs | 8 ++++++++
src/dialect/postgresql.rs | 6 ++++++
src/parser/mod.rs | 5 +++--
tests/sqlparser_common.rs | 8 ++++++++
tests/sqlparser_postgres.rs | 30 ++++++++++++++++++++++++++++++
5 files changed, 55 insertions(+), 2 deletions(-)
diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs
index 5997d7d30..512af328e 100644
--- a/src/dialect/mod.rs
+++ b/src/dialect/mod.rs
@@ -1525,6 +1525,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 {
diff --git a/src/dialect/postgresql.rs b/src/dialect/postgresql.rs
index 65bfd46a7..e49417cc4 100644
--- a/src/dialect/postgresql.rs
+++ b/src/dialect/postgresql.rs
@@ -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!(
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index 39a3a754f..db54296fc 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -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 {
diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs
index 1959bcd34..61fcb8131 100644
--- a/tests/sqlparser_common.rs
+++ b/tests/sqlparser_common.rs
@@ -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'");
+}
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs
index 11a6b3871..1e0028867 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -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""#);
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datafusion.apache.org
For additional commands, e-mail: commits-help@datafusion.apache.org