Skip to content

Conversation

@waynexia
Copy link
Member

@waynexia waynexia commented Dec 5, 2025

Which issue does this PR close?

Rationale for this change

Support Set Comparison Subquery (or Quantified Comparison Subquery in some systems), it looks like

... WHERE t1.a > ANY(SELECT ... FROM t2) ...

or

... WHERE t1.a < ALL(SELECT ... FROM t2) ...

What changes are included in this PR?

  • New Expr branch Expr::SetComparison and corresponding SetComparison struct
  • New optimizer rule RewriteSetComparison to translate this kind of subquery using existing components
  • Corresponding substrait and proto changes

RewriteSetComparison is a very naive implementation. We can optimize it in various ways in the follow-up PRs, like using min/max for non-equal comparison operators etc.

Are these changes tested?

Yes, by unit test and slt

Are there any user-facing changes?

To SQL user, this is a new feature. To API user, ExprPlanner::plan_any is removed (not needed)

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
@github-actions github-actions bot added sql SQL Planner logical-expr Logical plan and expressions optimizer Optimizer rules core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) substrait Changes to the substrait crate catalog Related to the catalog crate execution Related to the execution crate proto Related to proto crate labels Dec 5, 2025
@github-actions github-actions bot removed the execution Related to the execution crate label Dec 6, 2025
Copy link
Contributor

@kosiew kosiew left a comment

Choose a reason for hiding this comment

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

@waynexia

Thanks for your contribution.

Comment on lines +59 to +60
async fn set_comparison_all_empty() -> Result<()> {
let ctx = SessionContext::new();
Copy link
Contributor

Choose a reason for hiding this comment

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

how about adding

async fn set_comparison_type_mismatch() -> Result<()> {
    // SELECT v FROM t WHERE v > ANY (SELECT s FROM strings)
    // INT > STRING should error with clear message

...

too?

)?;

let df = ctx
.sql("select v from t where v < all(select v from e)")
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 tests for:

  • Multiple operators (=, !=, >=, <=)
  • NULL semantics (e.g., 5 != ALL (1, NULL)
    would improve test coverage

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you @kosiew, I added above cases in 07e23bd

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

catalog Related to the catalog crate core Core DataFusion crate logical-expr Logical plan and expressions optimizer Optimizer rules proto Related to proto crate sql SQL Planner sqllogictest SQL Logic Tests (.slt) substrait Changes to the substrait crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: support non-equal quantified subquery

2 participants