-
Notifications
You must be signed in to change notification settings - Fork 698
Add support for PostgreSQL's ORDER BY ... USING <operator> clause #2246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18156,7 +18156,32 @@ impl<'a> Parser<'a> { | |||||
| None | ||||||
| }; | ||||||
|
|
||||||
| let options = self.parse_order_by_options()?; | ||||||
| let using_operator = if !with_operator_class | ||||||
| && self.dialect.supports_order_by_using_operator() | ||||||
| && self.parse_keyword(Keyword::USING) | ||||||
| { | ||||||
| Some(self.parse_order_by_using_operator()?) | ||||||
| } else { | ||||||
| None | ||||||
| }; | ||||||
|
|
||||||
| let options = if using_operator.is_some() { | ||||||
| if self | ||||||
| .peek_one_of_keywords(&[Keyword::ASC, Keyword::DESC]) | ||||||
| .is_some() | ||||||
| { | ||||||
| return parser_err!( | ||||||
| "ASC/DESC cannot be used together with USING in ORDER BY".to_string(), | ||||||
| self.peek_token_ref().span.start | ||||||
| ); | ||||||
| } | ||||||
| OrderByOptions { | ||||||
| asc: None, | ||||||
| nulls_first: self.parse_order_by_nulls_first_last(), | ||||||
|
Comment on lines
+18169
to
+18180
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can skip this validation |
||||||
| } | ||||||
|
Comment on lines
+18159
to
+18181
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be possible to use something like this representation instead? pub enum OrderByExpr {
Asc,
Desc,
Using(...),
UsingOperator(...),
}
pub struct OrderByOptions {
pub expr: Option<OrderByExpr>,
pub nulls_first: Option<bool>,
} |
||||||
| } else { | ||||||
| self.parse_order_by_options()? | ||||||
| }; | ||||||
|
|
||||||
| let with_fill = if self.dialect.supports_with_fill() | ||||||
| && self.parse_keywords(&[Keyword::WITH, Keyword::FILL]) | ||||||
|
|
@@ -18169,23 +18194,61 @@ impl<'a> Parser<'a> { | |||||
| Ok(( | ||||||
| OrderByExpr { | ||||||
| expr, | ||||||
| using_operator, | ||||||
| options, | ||||||
| with_fill, | ||||||
| }, | ||||||
| operator_class, | ||||||
| )) | ||||||
| } | ||||||
|
|
||||||
| fn parse_order_by_options(&mut self) -> Result<OrderByOptions, ParserError> { | ||||||
| let asc = self.parse_asc_desc(); | ||||||
| fn parse_order_by_using_operator(&mut self) -> Result<ObjectName, ParserError> { | ||||||
| let dialect = self.dialect; | ||||||
|
|
||||||
| let nulls_first = if self.parse_keywords(&[Keyword::NULLS, Keyword::FIRST]) { | ||||||
| if self.parse_keyword(Keyword::OPERATOR) { | ||||||
| self.expect_token(&Token::LParen)?; | ||||||
| let operator_name = self.parse_operator_name()?; | ||||||
| let Some(last_part) = operator_name.0.last() else { | ||||||
| return self.expected_ref("an operator name", self.peek_token_ref()); | ||||||
| }; | ||||||
| let operator = last_part.to_string(); | ||||||
| if operator.is_empty() | ||||||
| || !operator | ||||||
| .chars() | ||||||
| .all(|ch| dialect.is_custom_operator_part(ch)) | ||||||
| { | ||||||
| return self.expected_ref("an operator name", self.peek_token_ref()); | ||||||
| } | ||||||
|
Comment on lines
+18214
to
+18221
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the |
||||||
| self.expect_token(&Token::RParen)?; | ||||||
| return Ok(operator_name); | ||||||
| } | ||||||
|
|
||||||
| let token = self.next_token(); | ||||||
| let operator = token.token.to_string(); | ||||||
| if !operator.is_empty() | ||||||
| && operator | ||||||
| .chars() | ||||||
| .all(|ch| dialect.is_custom_operator_part(ch)) | ||||||
| { | ||||||
| Ok(ObjectName::from(vec![Ident::new(operator)])) | ||||||
| } else { | ||||||
| self.expected_ref("an ordering operator after USING", &token) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn parse_order_by_nulls_first_last(&mut self) -> Option<bool> { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| if self.parse_keywords(&[Keyword::NULLS, Keyword::FIRST]) { | ||||||
| Some(true) | ||||||
| } else if self.parse_keywords(&[Keyword::NULLS, Keyword::LAST]) { | ||||||
| Some(false) | ||||||
| } else { | ||||||
| None | ||||||
| }; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn parse_order_by_options(&mut self) -> Result<OrderByOptions, ParserError> { | ||||||
| let asc = self.parse_asc_desc(); | ||||||
| let nulls_first = self.parse_order_by_nulls_first_last(); | ||||||
|
|
||||||
| Ok(OrderByOptions { asc, nulls_first }) | ||||||
| } | ||||||
|
|
@@ -20399,6 +20462,7 @@ mod tests { | |||||
| asc: None, | ||||||
| nulls_first: None, | ||||||
| }, | ||||||
| using_operator: None, | ||||||
| with_fill: None, | ||||||
| }, | ||||||
| operator_class: None, | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we include reference doc to maybe this?
https://www.postgresql.org/docs/current/sql-select.html
so folks know where to find this feature easily
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fyi: re the comment would be nice to include the link