LucaCappelletti94 commented on code in PR #2246:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2246#discussion_r2878306788
##########
tests/sqlparser_postgres.rs:
##########
@@ -5685,6 +5687,85 @@ fn parse_array_agg() {
pg().verified_stmt(sql4);
}
+#[test]
+fn parse_pg_aggregate_order_by_using_operator() {
+ let sql = "SELECT aggfns(DISTINCT a, a, c ORDER BY c USING ~<~, a) FROM t";
+ let select = pg().verified_only_select(sql);
+ let SelectItem::UnnamedExpr(Expr::Function(Function {
+ args: FunctionArguments::List(FunctionArgumentList { clauses, .. }),
+ ..
+ })) = &select.projection[0]
+ else {
+ unreachable!("expected aggregate function in projection");
+ };
+
+ let Some(FunctionArgumentClause::OrderBy(order_by_exprs)) = clauses
+ .iter()
+ .find(|clause| matches!(clause, FunctionArgumentClause::OrderBy(_)))
+ else {
+ unreachable!("expected ORDER BY clause in aggregate function argument
list");
+ };
+
+ assert_eq!(
+ order_by_exprs[0].using_operator,
+ Some(ObjectName::from(vec!["~<~".into()]))
+ );
+ assert_eq!(order_by_exprs[1].using_operator, None);
+}
+
+#[test]
+fn parse_pg_order_by_using_operator_syntax() {
+ pg().one_statement_parses_to(
+ "SELECT a FROM t ORDER BY a USING OPERATOR(<)",
+ "SELECT a FROM t ORDER BY a USING <",
+ );
+
+ let query =
+ pg().verified_query("SELECT a FROM t ORDER BY a USING
OPERATOR(pg_catalog.<) NULLS LAST");
+ let order_by = query.order_by.expect("expected ORDER BY clause");
+ let OrderByKind::Expressions(exprs) = order_by.kind else {
+ unreachable!("expected ORDER BY expressions");
+ };
+
+ assert_eq!(
+ exprs[0].using_operator,
+ Some(ObjectName::from(vec![
+ Ident::new("pg_catalog"),
+ Ident::new("<"),
+ ]))
+ );
+ assert_eq!(exprs[0].options.asc, None);
+ assert_eq!(exprs[0].options.nulls_first, Some(false));
+}
+
+#[test]
+fn parse_pg_order_by_using_operator_invalid_cases() {
+ let err = pg()
+ .parse_sql_statements("SELECT a FROM t ORDER BY a USING ;")
+ .unwrap_err();
+ assert!(
+ matches!(err, ParserError::ParserError(msg) if msg.contains("an
ordering operator after USING"))
+ );
+
+ let err = pg()
+ .parse_sql_statements("SELECT a FROM t ORDER BY a USING OPERATOR();")
+ .unwrap_err();
+ assert!(matches!(err, ParserError::ParserError(msg) if msg.contains("an
operator name")));
+
+ let err = pg()
+ .parse_sql_statements("SELECT a FROM t ORDER BY a USING < DESC;")
+ .unwrap_err();
+ assert!(
+ matches!(err, ParserError::ParserError(msg) if msg.contains("ASC/DESC
cannot be used together with USING in ORDER BY"))
+ );
+
+ // `USING` in ORDER BY is PostgreSQL-specific and should not parse in
GenericDialect.
+ let generic = TestedDialects::new(vec![Box::new(GenericDialect {})]);
Review Comment:
Replaced the `GenericDialect` negative case with a local wrapper dialect
that behaves like Postgres but returns false for
`supports_order_by_using_operator()`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]