-
Notifications
You must be signed in to change notification settings - Fork 707
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 3 commits
7c2e3b6
ca32b8f
e8056c7
2d3c4f1
1ad989f
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(), | ||||||
|
||||||
| } | ||||||
|
||||||
| } 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()); | ||||||
| } | ||||||
|
||||||
| 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> { | ||||||
|
||||||
| fn parse_order_by_nulls_first_last(&mut self) -> Option<bool> { | |
| fn parse_null_ordering_modifier(&mut self) -> Option<bool> { |
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