Skip to content

Add support for parenthesized subquery as IN predicate #1793

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

Merged
merged 1 commit into from
Apr 15, 2025

Conversation

adamchainz
Copy link
Contributor

Fixes #1792.

This is my attempt to hack in support with my minimal Rust skills. I stopped when I realized that my change of InSubquery to use SetExpr for its subquery broke many tests.

@adamchainz adamchainz force-pushed the postgresql_in_set_expression branch from 58372cf to a2d59d6 Compare April 4, 2025 22:02
@adamchainz adamchainz changed the title Add support for PostgreSQL 'IN <SetExpression>' Add support for 'IN <SetExpression>' Apr 4, 2025
let in_op = if self.parse_keyword(Keyword::SELECT) || self.parse_keyword(Keyword::WITH) {
self.prev_token();
Expr::InSubquery {
let in_op = match self.try_parse(|p| p.parse_query_body(p.dialect.prec_unknown())) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The previous approach peeked for a SELECT or WITH, but we cannot rely on that because there may be more than one parenthesis, as in IN ((SELECT ...) UNION ()).

The new approach tries to parse a subquery first, then an in list second.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this makes sense!

}

#[test]
fn parse_in_union() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I only added one test - happy to add more if deemed necessary.

Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

Thanks @adamchainz! This looks good to me overall, left one comment

let in_op = if self.parse_keyword(Keyword::SELECT) || self.parse_keyword(Keyword::WITH) {
self.prev_token();
Expr::InSubquery {
let in_op = match self.try_parse(|p| p.parse_query_body(p.dialect.prec_unknown())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah this makes sense!

@iffyio iffyio marked this pull request as draft April 10, 2025 05:19
@iffyio
Copy link
Contributor

iffyio commented Apr 10, 2025

Marking as draft in the meantime as this is no longer pending review, @adamchainz please feel free to undraft and ping when ready!

@adamchainz adamchainz force-pushed the postgresql_in_set_expression branch from a2d59d6 to feac964 Compare April 15, 2025 09:38
@adamchainz adamchainz marked this pull request as ready for review April 15, 2025 09:38
@iffyio iffyio changed the title Add support for 'IN <SetExpression>' Add support for parenthesized subquery as IN predicate Apr 15, 2025
Copy link
Contributor

@iffyio iffyio left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks @adamchainz!
cc @alamb

@iffyio iffyio merged commit 3ad13af into apache:main Apr 15, 2025
9 checks passed
@adamchainz adamchainz deleted the postgresql_in_set_expression branch April 15, 2025 21:31
@adamchainz
Copy link
Contributor Author

Yay, thank you!

@alamb
Copy link
Contributor

alamb commented Apr 15, 2025

The code train keeps on moving in this repo. very impressive

@adamchainz
Copy link
Contributor Author

image

choo choo!

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

Successfully merging this pull request may close these issues.

PostgreSQL dialect fails to parse IN with union
3 participants