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 Expr::InSubquery and Expr::ScalarSubquery #2342

Merged
merged 8 commits into from
Apr 26, 2022

Conversation

andygrove
Copy link
Member

Which issue does this PR close?

Part of #2248

Rationale for this change

Add InSubquery and ScalarSubquery to Expr as a step towards supporting SQL queries that use these expressions.

What changes are included in this PR?

  • Add InSubquery and ScalarSubquery to Expr
  • Add functions to create these expressions

Are there any user-facing changes?

Yes, API change

@andygrove andygrove added the api change Changes the API exposed to users of the crate label Apr 26, 2022
@andygrove andygrove self-assigned this Apr 26, 2022
@github-actions github-actions bot added the datafusion Changes in the datafusion crate label Apr 26, 2022
@andygrove andygrove changed the title WIP: Add Expr::InSubquery and Expr::ScalarSubquery Add Expr::InSubquery and Expr::ScalarSubquery Apr 26, 2022
@andygrove andygrove marked this pull request as ready for review April 26, 2022 13:47
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Looking very nice @andygrove

All aboard the subquery 🚂 !

I had a few stylistic / corner case comments but nothing that can't be addressed after merge as a follow on PR

Expr::IsNull(_)
| Expr::IsNotNull(_)
| Expr::Exists { .. }
| Expr::InSubquery { .. } => Ok(false),
Copy link
Contributor

Choose a reason for hiding this comment

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

I think aIN can be nullable if its exprs are nullable:

alamb=# select null IN (select * from a);
 ?column? 
----------
 
(1 row)

So maybe Expr::InSubquery should return expr.nullable(input_schema) too

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks. Fixed.

datafusion/expr/src/expr.rs Outdated Show resolved Hide resolved
datafusion/expr/src/expr.rs Outdated Show resolved Hide resolved
subquery,
negated,
} => Ok(Expr::InSubquery {
expr: Box::new(clone_with_replacement(&**nested_expr, replacement_fn)?),
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

| Expr::Exists(_)
| Expr::Exists { .. }
| Expr::InSubquery { .. }
| Expr::ScalarSubquery(_)
Copy link
Contributor

Choose a reason for hiding this comment

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

at some point it would be cool to be able to evaluate scalar subqueries at plan time, "but not now" 😆

Comment on lines 461 to 471
Expr::InSubquery {
expr,
subquery,
negated,
} => {
if *negated {
write!(f, "{:?} NOT IN ({:?})", expr, subquery)
} else {
write!(f, "{:?} IN ({:?})", expr, subquery)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The same negated: true / negated: false could be applied to Expr::InSubquery as was applied to Expr::Exists

@andygrove andygrove mentioned this pull request Apr 26, 2022
15 tasks
@andygrove andygrove merged commit a1bd9e1 into apache:master Apr 26, 2022
@andygrove andygrove deleted the in-subquery-scalar-subquery branch April 26, 2022 22:53
@alamb
Copy link
Contributor

alamb commented Apr 27, 2022

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api change Changes the API exposed to users of the crate datafusion Changes in the datafusion crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants