Skip to content
Open
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: 4 additions & 0 deletions src/dialect/duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ impl Dialect for DuckDbDialect {
true
}

fn supports_aggregate_order_by_with_empty_args(&self) -> bool {
true
}

fn supports_named_fn_args_with_eq_operator(&self) -> bool {
true
}
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ impl Dialect for GenericDialect {
true
}

fn supports_aggregate_order_by_with_empty_args(&self) -> bool {
true
}

fn supports_comment_on(&self) -> bool {
true
}
Expand Down
9 changes: 9 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2038,6 +2038,15 @@ pub trait Dialect: Debug + Any {
fn supports_cast_empty_data_type_to_unspecified(&self) -> bool {
false
}

/// Returns true if the dialect allows an `ORDER BY` clause inside an aggregate function
/// call with no preceding arguments, e.g. `count(ORDER BY a)`.
///
/// [SQLite](https://www.sqlite.org/lang_aggfunc.html#aggorderby),
/// [DuckDB](https://duckdb.org/docs/current/sql/functions/aggregates#order-by-clause-in-aggregate-functions)
fn supports_aggregate_order_by_with_empty_args(&self) -> bool {
false
}
}

/// Operators for which precedence must be defined.
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,8 @@ impl Dialect for SQLiteDialect {
fn supports_parens_around_table_factor(&self) -> bool {
true
}

fn supports_aggregate_order_by_with_empty_args(&self) -> bool {
true
}
}
9 changes: 8 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19345,7 +19345,14 @@ impl<'a> Parser<'a> {
}

let duplicate_treatment = self.parse_duplicate_treatment()?;
let args = self.parse_comma_separated(Parser::parse_function_args)?;
let args = if duplicate_treatment != Some(DuplicateTreatment::Distinct)
&& self.dialect.supports_aggregate_order_by_with_empty_args()
&& self.peek_keywords(&[Keyword::ORDER, Keyword::BY])
{
vec![]
} else {
self.parse_comma_separated(Parser::parse_function_args)?
};

if self.parse_keyword(Keyword::WHERE) {
clauses.push(FunctionArgumentClause::Where(self.parse_expr()?));
Expand Down
22 changes: 22 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20707,3 +20707,25 @@ fn parse_string_literal_alias() {
dialects.verified_stmt("SELECT 1 AS 'x'");
dialects.verified_stmt("SELECT a FROM t AS 'y'");
}

#[test]
fn parse_aggregate_order_by_with_empty_args() {
let dialects = all_dialects_where(|d| d.supports_aggregate_order_by_with_empty_args());
dialects.verified_stmt("SELECT count(ORDER BY a) FROM t");
dialects.verified_stmt("SELECT count(ORDER BY a ASC, b DESC) FROM t");
dialects.verified_stmt("SELECT count(ALL ORDER BY a) FROM t");
assert_eq!(
dialects.parse_sql_statements("SELECT count(DISTINCT ORDER BY a) FROM t"),
Err(ParserError::ParserError(
"Expected: ), found: BY".to_string()
))
);

assert_eq!(
all_dialects_where(|d| !d.supports_aggregate_order_by_with_empty_args())
.parse_sql_statements("SELECT count(ORDER BY a) FROM t"),
Err(ParserError::ParserError(
"Expected: ), found: BY".to_string()
))
);
}
Loading