Skip to content

Commit 0093a3e

Browse files
Refactor ORDER BY ... USING operator handling to use ObjectName instead of OrderByUsingOperator
1 parent fffbe64 commit 0093a3e

File tree

4 files changed

+25
-46
lines changed

4 files changed

+25
-46
lines changed

src/ast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub use self::query::{
9797
JsonTableNestedColumn, LateralView, LimitClause, LockClause, LockType, MatchRecognizePattern,
9898
MatchRecognizeSymbol, Measure, NamedWindowDefinition, NamedWindowExpr, NonBlock, Offset,
9999
OffsetRows, OpenJsonTableColumn, OrderBy, OrderByExpr, OrderByKind, OrderByOptions,
100-
OrderByUsingOperator, PipeOperator, PivotValueSource, ProjectionSelect, Query,
100+
PipeOperator, PivotValueSource, ProjectionSelect, Query,
101101
RenameSelectItem, RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch,
102102
Select, SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
103103
SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table, TableAlias,

src/ast/query.rs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2861,35 +2861,13 @@ pub struct OrderByExpr {
28612861
/// The expression to order by.
28622862
pub expr: Expr,
28632863
/// Optional PostgreSQL `USING <operator>` clause.
2864-
pub using_operator: Option<OrderByUsingOperator>,
2864+
pub using_operator: Option<ObjectName>,
28652865
/// Ordering options such as `ASC`/`DESC` and `NULLS` behavior.
28662866
pub options: OrderByOptions,
28672867
/// Optional `WITH FILL` clause (ClickHouse extension) which specifies how to fill gaps.
28682868
pub with_fill: Option<WithFill>,
28692869
}
28702870

2871-
/// An operator used in PostgreSQL's `ORDER BY ... USING <operator>` clause.
2872-
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2873-
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2874-
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2875-
pub enum OrderByUsingOperator {
2876-
/// A symbolic operator such as `<`, `>`, or `~<~`.
2877-
Symbol(String),
2878-
/// PostgreSQL `OPERATOR(...)` syntax, e.g. `OPERATOR(pg_catalog.<)`.
2879-
Qualified(Vec<String>),
2880-
}
2881-
2882-
impl fmt::Display for OrderByUsingOperator {
2883-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2884-
match self {
2885-
OrderByUsingOperator::Symbol(op) => write!(f, "{op}"),
2886-
OrderByUsingOperator::Qualified(path) => {
2887-
write!(f, "OPERATOR({})", display_separated(path, "."))
2888-
}
2889-
}
2890-
}
2891-
}
2892-
28932871
impl From<Ident> for OrderByExpr {
28942872
fn from(ident: Ident) -> Self {
28952873
OrderByExpr {
@@ -2905,7 +2883,11 @@ impl fmt::Display for OrderByExpr {
29052883
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29062884
write!(f, "{}", self.expr)?;
29072885
if let Some(using_operator) = &self.using_operator {
2908-
write!(f, " USING {using_operator}")?;
2886+
if using_operator.0.len() > 1 {
2887+
write!(f, " USING OPERATOR({using_operator})")?;
2888+
} else {
2889+
write!(f, " USING {using_operator}")?;
2890+
}
29092891
}
29102892
write!(f, "{}", self.options)?;
29112893
if let Some(ref with_fill) = self.with_fill {

src/parser/mod.rs

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18118,33 +18118,25 @@ impl<'a> Parser<'a> {
1811818118
))
1811918119
}
1812018120

18121-
fn parse_order_by_using_operator(&mut self) -> Result<OrderByUsingOperator, ParserError> {
18121+
fn parse_order_by_using_operator(&mut self) -> Result<ObjectName, ParserError> {
1812218122
if self.parse_keyword(Keyword::OPERATOR) {
1812318123
self.expect_token(&Token::LParen)?;
18124-
let mut operator_path = vec![];
18125-
let last_token = loop {
18126-
let token = self.next_token();
18127-
if token.token == Token::RParen {
18128-
return self.expected_ref("an operator name", &token);
18129-
}
18130-
operator_path.push(token.token.to_string());
18131-
if !self.consume_token(&Token::Period) {
18132-
break token;
18133-
}
18124+
let operator_name = self.parse_operator_name()?;
18125+
let Some(last_part) = operator_name.0.last() else {
18126+
return self.expected_ref("an operator name", self.peek_token_ref());
1813418127
};
18135-
if let Some(operator) = operator_path.last() {
18136-
if !Self::is_valid_order_by_using_operator_symbol(operator) {
18137-
return self.expected_ref("an operator name", &last_token);
18138-
}
18128+
let operator = last_part.to_string();
18129+
if !Self::is_valid_order_by_using_operator_symbol(&operator) {
18130+
return self.expected_ref("an operator name", self.peek_token_ref());
1813918131
}
1814018132
self.expect_token(&Token::RParen)?;
18141-
return Ok(OrderByUsingOperator::Qualified(operator_path));
18133+
return Ok(operator_name);
1814218134
}
1814318135

1814418136
let token = self.next_token();
1814518137
let operator = token.token.to_string();
1814618138
if Self::is_valid_order_by_using_operator_symbol(&operator) {
18147-
Ok(OrderByUsingOperator::Symbol(operator))
18139+
Ok(ObjectName::from(vec![Ident::new(operator)]))
1814818140
} else {
1814918141
self.expected_ref("an ordering operator after USING", &token)
1815018142
}

tests/sqlparser_postgres.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5708,13 +5708,18 @@ fn parse_pg_aggregate_order_by_using_operator() {
57085708

57095709
assert_eq!(
57105710
order_by_exprs[0].using_operator,
5711-
Some(OrderByUsingOperator::Symbol("~<~".to_string()))
5711+
Some(ObjectName::from(vec!["~<~".into()]))
57125712
);
57135713
assert_eq!(order_by_exprs[1].using_operator, None);
57145714
}
57155715

57165716
#[test]
57175717
fn parse_pg_order_by_using_operator_syntax() {
5718+
pg().one_statement_parses_to(
5719+
"SELECT a FROM t ORDER BY a USING OPERATOR(<)",
5720+
"SELECT a FROM t ORDER BY a USING <",
5721+
);
5722+
57185723
let query =
57195724
pg().verified_query("SELECT a FROM t ORDER BY a USING OPERATOR(pg_catalog.<) NULLS LAST");
57205725
let order_by = query.order_by.expect("expected ORDER BY clause");
@@ -5724,9 +5729,9 @@ fn parse_pg_order_by_using_operator_syntax() {
57245729

57255730
assert_eq!(
57265731
exprs[0].using_operator,
5727-
Some(OrderByUsingOperator::Qualified(vec![
5728-
"pg_catalog".to_string(),
5729-
"<".to_string(),
5732+
Some(ObjectName::from(vec![
5733+
Ident::new("pg_catalog"),
5734+
Ident::new("<"),
57305735
]))
57315736
);
57325737
assert_eq!(exprs[0].options.asc, None);

0 commit comments

Comments
 (0)