Skip to content

Handle empty projection in Postgres SELECT statements #1613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 22, 2024
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: 3 additions & 1 deletion src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}")?;
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,8 @@ impl Dialect for GenericDialect {
fn supports_struct_literal(&self) -> bool {
true
}

fn supports_empty_projections(&self) -> bool {
true
}
}
10 changes: 10 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>> {
Expand Down
7 changes: 6 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9604,7 +9604,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
Expand Down
6 changes: 6 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12446,3 +12446,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");
}
Loading