This is an automated email from the ASF dual-hosted git repository.
alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 27822e25 Handle empty projection in Postgres SELECT statements (#1613)
27822e25 is described below
commit 27822e254b65525fb934c3f04d15389acf06a47e
Author: Toby Hede <[email protected]>
AuthorDate: Mon Dec 23 02:19:43 2024 +1000
Handle empty projection in Postgres SELECT statements (#1613)
---
src/ast/query.rs | 4 +++-
src/dialect/generic.rs | 4 ++++
src/dialect/mod.rs | 10 ++++++++++
src/dialect/postgresql.rs | 10 ++++++++++
src/parser/mod.rs | 7 ++++++-
tests/sqlparser_common.rs | 6 ++++++
6 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/src/ast/query.rs b/src/ast/query.rs
index 948febd2..69b7ea1c 100644
--- a/src/ast/query.rs
+++ b/src/ast/query.rs
@@ -350,7 +350,9 @@ impl fmt::Display for Select {
}
}
- write!(f, " {}", display_comma_separated(&self.projection))?;
+ if !self.projection.is_empty() {
+ write!(f, " {}", display_comma_separated(&self.projection))?;
+ }
if let Some(ref into) = self.into {
write!(f, " {into}")?;
diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs
index 61e5070f..f852152a 100644
--- a/src/dialect/generic.rs
+++ b/src/dialect/generic.rs
@@ -127,4 +127,8 @@ impl Dialect for GenericDialect {
fn supports_struct_literal(&self) -> bool {
true
}
+
+ fn supports_empty_projections(&self) -> bool {
+ true
+ }
}
diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs
index c32b763a..7a71d662 100644
--- a/src/dialect/mod.rs
+++ b/src/dialect/mod.rs
@@ -410,6 +410,16 @@ pub trait Dialect: Debug + Any {
false
}
+ /// Return true if the dialect supports empty projections in SELECT
statements
+ ///
+ /// Example
+ /// ```sql
+ /// SELECT from table_name
+ /// ```
+ fn supports_empty_projections(&self) -> bool {
+ false
+ }
+
/// Dialect-specific infix parser override
///
/// This method is called to parse the next infix expression.
diff --git a/src/dialect/postgresql.rs b/src/dialect/postgresql.rs
index dcdcc88c..80c14c8d 100644
--- a/src/dialect/postgresql.rs
+++ b/src/dialect/postgresql.rs
@@ -231,6 +231,16 @@ impl Dialect for PostgreSqlDialect {
fn supports_named_fn_args_with_expr_name(&self) -> bool {
true
}
+
+ /// Return true if the dialect supports empty projections in SELECT
statements
+ ///
+ /// Example
+ /// ```sql
+ /// SELECT from table_name
+ /// ```
+ fn supports_empty_projections(&self) -> bool {
+ true
+ }
}
pub fn parse_create(parser: &mut Parser) -> Option<Result<Statement,
ParserError>> {
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index af4b7b45..cc0a57e4 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -9692,7 +9692,12 @@ impl<'a> Parser<'a> {
top = Some(self.parse_top()?);
}
- let projection = self.parse_projection()?;
+ let projection =
+ if self.dialect.supports_empty_projections() &&
self.peek_keyword(Keyword::FROM) {
+ vec![]
+ } else {
+ self.parse_projection()?
+ };
let into = if self.parse_keyword(Keyword::INTO) {
let temporary = self
diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs
index c294eab0..b8ea7586 100644
--- a/tests/sqlparser_common.rs
+++ b/tests/sqlparser_common.rs
@@ -12576,3 +12576,9 @@ fn overflow() {
let statement = statements.pop().unwrap();
assert_eq!(statement.to_string(), sql);
}
+
+#[test]
+fn parse_select_without_projection() {
+ let dialects = all_dialects_where(|d| d.supports_empty_projections());
+ dialects.verified_stmt("SELECT FROM users");
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]