Skip to content

fix: failed to execute sql with subquery #5542

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 3 commits into from
Mar 13, 2023
Merged

fix: failed to execute sql with subquery #5542

merged 3 commits into from
Mar 13, 2023

Conversation

MichaelScofield
Copy link
Contributor

Which issue does this PR close?

Closes #5529 .

Rationale for this change

fix: failed to run "where x in ((select ...))"

What changes are included in this PR?

rewrite Expr::InList with only one subquery to Expr::InSubquery during logical plan optimization

Are these changes tested?

yes

Are there any user-facing changes?

no

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.

Thank you @MichaelScofield !

This looks great to me.

Can you please add one SQL level test to verify that the plumbing is all hooked up correctly.

I recommend using in https://github.com/apache/arrow-datafusion/tree/main/datafusion/core/tests/sqllogictests

Perhaps in https://github.com/apache/arrow-datafusion/blob/main/datafusion/core/tests/sqllogictests/test_files/subquery.slt ?

@@ -390,6 +390,22 @@ impl<'a, S: SimplifyInfo> ExprRewriter for Simplifier<'a, S> {
lit(negated)
}

// expr IN ((subquery)) -> expr IN (subquery), see ##5529
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@@ -2849,6 +2866,24 @@ mod tests {
simplify(in_list(col("c1"), vec![lit(1), lit(2)], true)),
col("c1").not_eq(lit(2)).and(col("c1").not_eq(lit(1)))
);

let subquery = Arc::new(test_table_scan_with_name("test").unwrap());
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please also add a negative test (with two subqueries)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

of course

} if list.len() == 1
&& matches!(list.first(), Some(Expr::ScalarSubquery { .. })) =>
{
let Expr::ScalarSubquery(subquery) = list.remove(0) else { unreachable!() };
Copy link
Contributor

Choose a reason for hiding this comment

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

Struggling to get why we unreachable here

Copy link
Member

Choose a reason for hiding this comment

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

The condition in L399 ensures that only ScalarSubquery is allowed, making other branches unreachable. Although there is an unstable rust feature available to simplify this, it cannot be used at the moment.

@github-actions github-actions bot added core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) labels Mar 13, 2023
@MichaelScofield
Copy link
Contributor Author

I've added 3 test cases in sqllogictest. The cases are from our original intention to test subquery with "(( ))". PTAL @alamb

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.

Looks great to me -- thank you @MichaelScofield

@alamb alamb merged commit 1a22f9f into apache:main Mar 13, 2023
@ursabot
Copy link

ursabot commented Mar 13, 2023

Benchmark runs are scheduled for baseline = 9261aa3 and contender = 1a22f9f. 1a22f9f is a master commit associated with this PR. Results will be available as each benchmark for each run completes.
Conbench compare runs links:
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ec2-t3-xlarge-us-east-2] ec2-t3-xlarge-us-east-2
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on test-mac-arm] test-mac-arm
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-i9-9960x] ursa-i9-9960x
[Skipped ⚠️ Benchmarking of arrow-datafusion-commits is not supported on ursa-thinkcentre-m75q] ursa-thinkcentre-m75q
Buildkite builds:
Supported benchmarks:
ec2-t3-xlarge-us-east-2: Supported benchmark langs: Python, R. Runs only benchmarks with cloud = True
test-mac-arm: Supported benchmark langs: C++, Python, R
ursa-i9-9960x: Supported benchmark langs: Python, R, JavaScript
ursa-thinkcentre-m75q: Supported benchmark langs: C++, Java

@MichaelScofield MichaelScofield deleted the fix/rewrite-in-subquery branch March 13, 2023 11:34
@MichaelScofield MichaelScofield restored the fix/rewrite-in-subquery branch March 14, 2023 06:44
@andygrove andygrove added the bug Something isn't working label Mar 24, 2023
@MichaelScofield MichaelScofield deleted the fix/rewrite-in-subquery branch July 25, 2023 05:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core Core DataFusion crate optimizer Optimizer rules sqllogictest SQL Logic Tests (.slt)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failed to execute sql with subquery
6 participants