Handle empty projection in Postgres SELECT statements (#1613)

This commit is contained in:
Toby Hede 2024-12-23 02:19:43 +10:00 committed by GitHub
parent 0647a4aa82
commit 27822e254b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 39 additions and 2 deletions

View file

@ -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}")?;

View file

@ -127,4 +127,8 @@ impl Dialect for GenericDialect {
fn supports_struct_literal(&self) -> bool {
true
}
fn supports_empty_projections(&self) -> bool {
true
}
}

View file

@ -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.

View file

@ -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>> {

View file

@ -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

View file

@ -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");
}