Skip to content

Commit fffbe64

Browse files
Add support for PostgreSQL's ORDER BY ... USING <operator> clause
1 parent bd7f70e commit fffbe64

File tree

10 files changed

+311
-99
lines changed

10 files changed

+311
-99
lines changed

src/ast/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ 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-
PipeOperator, PivotValueSource, ProjectionSelect, Query, RenameSelectItem,
101-
RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch, Select,
102-
SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
100+
OrderByUsingOperator, PipeOperator, PivotValueSource, ProjectionSelect, Query,
101+
RenameSelectItem, RepetitionQuantifier, ReplaceSelectElement, ReplaceSelectItem, RowsPerMatch,
102+
Select, SelectFlavor, SelectInto, SelectItem, SelectItemQualifiedWildcardKind, SelectModifiers,
103103
SetExpr, SetOperator, SetQuantifier, Setting, SymbolDefinition, Table, TableAlias,
104104
TableAliasColumnDef, TableFactor, TableFunctionArgs, TableIndexHintForClause,
105105
TableIndexHintType, TableIndexHints, TableIndexType, TableSample, TableSampleBucket,

src/ast/query.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2860,16 +2860,41 @@ impl fmt::Display for OrderBy {
28602860
pub struct OrderByExpr {
28612861
/// The expression to order by.
28622862
pub expr: Expr,
2863+
/// Optional PostgreSQL `USING <operator>` clause.
2864+
pub using_operator: Option<OrderByUsingOperator>,
28632865
/// Ordering options such as `ASC`/`DESC` and `NULLS` behavior.
28642866
pub options: OrderByOptions,
28652867
/// Optional `WITH FILL` clause (ClickHouse extension) which specifies how to fill gaps.
28662868
pub with_fill: Option<WithFill>,
28672869
}
28682870

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+
28692893
impl From<Ident> for OrderByExpr {
28702894
fn from(ident: Ident) -> Self {
28712895
OrderByExpr {
28722896
expr: Expr::Identifier(ident),
2897+
using_operator: None,
28732898
options: OrderByOptions::default(),
28742899
with_fill: None,
28752900
}
@@ -2878,7 +2903,11 @@ impl From<Ident> for OrderByExpr {
28782903

28792904
impl fmt::Display for OrderByExpr {
28802905
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2881-
write!(f, "{}{}", self.expr, self.options)?;
2906+
write!(f, "{}", self.expr)?;
2907+
if let Some(using_operator) = &self.using_operator {
2908+
write!(f, " USING {using_operator}")?;
2909+
}
2910+
write!(f, "{}", self.options)?;
28822911
if let Some(ref with_fill) = self.with_fill {
28832912
write!(f, " {with_fill}")?
28842913
}

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2087,6 +2087,7 @@ impl Spanned for OrderByExpr {
20872087
fn span(&self) -> Span {
20882088
let OrderByExpr {
20892089
expr,
2090+
using_operator: _,
20902091
options: _,
20912092
with_fill,
20922093
} = self;

0 commit comments

Comments
 (0)