Skip to content
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

Add support for ORDER BY x USING <op> (PostgreSQL) #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

coderdan
Copy link

Adds support for the USING keyword when using ORDER BY in PostgreSQL queries. Only handles a handful of binary operators at the moment but I realised that Postgres operators can be any string of up to 63 valid characters! SqlParser doesn't support that yet, either.

@coderdan coderdan mentioned this pull request Nov 26, 2023
@coderdan coderdan marked this pull request as ready for review November 26, 2023 21:08
pub asc: Option<bool>,
/// Optional `NULLS FIRST` or `NULLS LAST`
pub nulls_first: Option<bool>,
/// Optional `USING` operator for ordering (PostgreSQL only)
pub using: Option<BinaryOperator>,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be OK in our fork, but I'd probably put more thought into this before raising upstream since we'd have to make a breaking change to support ops other than binary ops.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't understand what you mean.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the AST is part of the API, changing Option<BinaryOperator> to something else will be a breaking change.

@@ -6013,6 +6013,22 @@ impl<'a> Parser<'a> {
Ok(expr)
}

pub fn parse_operator(&mut self) -> Option<BinaryOperator> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is for ORDER BY ... USING ... maybe make the name more specific? Something like parse_using_operator?

Also feels like a smell thatUSING needs its own operator parsing function, though.

Copy link

@CDThomas CDThomas Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to dig more into how PG handles this itself vs what sqlparser does now for operators to have more concrete suggestions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this function is intended to be a replacement for operator parsing which is a dogs breakfast and buried in the expression parsing stuff. Adding yet another way to parse operators is a bad idea IMHO. Calling this function parse_using_operator sets the expectation for future developers that this is just for that purpose and exacerbates the problem.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm taking the approach of trying to leave the code in a better place than I found it, rather than just throw stuff on the pile.

What's the argument for parsing using operators on their own? Any binary operator is supported AFAICT.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the argument for parsing using operators on their own? Any binary operator is supported AFAICT.

Ah, that's not what I was suggesting. I was confused that this is only used for ORDER BY ... USING .... I don't think that this leaves the code in a better place to be honest because it adds a special case to be aware of that's partially implemented.

Might be fine just to unblock us in our fork so that things parse. Probably worth properly designing before raising upstream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants