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

feat(frontend): support InSubquery. #2187

Merged
merged 6 commits into from
Apr 29, 2022
Merged

feat(frontend): support InSubquery. #2187

merged 6 commits into from
Apr 29, 2022

Conversation

likg227
Copy link
Contributor

@likg227 likg227 commented Apr 28, 2022

What's changed and what's your intention?

PLEASE DO NOT LEAVE THIS EMPTY !!!

Please explain IN DETAIL what the changes are in this PR and why they are needed:

  • Summarize your change (mandatory)
    In this pr, I supported (NOT) InSubquery for WHERE clause and add on for LogicalApply.
    I bind InSubquery as normal InList and plan it as Left-Semi-Apply using equal condition as its predicate.

  • Describe any limitations of the current code (optional)
    Please note that InSubquery that goes into others cannot be handled by substitute_subqueries yet.

Checklist

  • I have written necessary docs and comments
  • I have added necessary unit tests and integration tests

Refer to a related PR or issue link (optional)

close #2027

@codecov
Copy link

codecov bot commented Apr 28, 2022

Codecov Report

Merging #2187 (e034886) into main (1332556) will increase coverage by 0.02%.
The diff coverage is 93.18%.

@@            Coverage Diff             @@
##             main    #2187      +/-   ##
==========================================
+ Coverage   71.02%   71.05%   +0.02%     
==========================================
  Files         657      657              
  Lines       83592    83633      +41     
==========================================
+ Hits        59373    59423      +50     
+ Misses      24219    24210       -9     
Flag Coverage Δ
rust 71.05% <93.18%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/frontend/src/planner/select.rs 92.81% <85.00%> (+3.37%) ⬆️
src/frontend/src/binder/expr/mod.rs 86.12% <100.00%> (-1.05%) ⬇️
src/frontend/src/binder/expr/subquery.rs 100.00% <100.00%> (ø)
src/frontend/src/expr/subquery.rs 46.42% <100.00%> (-1.85%) ⬇️
.../frontend/src/optimizer/plan_node/logical_apply.rs 83.87% <100.00%> (+6.31%) ⬆️
...src/optimizer/rule/pull_up_correlated_predicate.rs 98.71% <100.00%> (-0.02%) ⬇️
.../src/executor/managed_state/aggregation/extreme.rs 90.29% <0.00%> (+0.26%) ⬆️
... and 4 more

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@likg227 likg227 requested a review from fuyufjh April 28, 2022 03:36
- sql: |
create table t1(x int, y int);
create table t2(x int, y int);
select x from t1 where y in (select y from t2 where t1.x = t2.x);
Copy link
Contributor

@fuyufjh fuyufjh Apr 28, 2022

Choose a reason for hiding this comment

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

Can you add more test cases? e.g.

  • not in
  • with other conditions in the subquery
  • with more than 1 correlated variable in the subquery
  • with non-equi correlated condition in the subquery
  • ...

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 have added tests for not in in subquery_expr.yaml, because we cann't unnest LogicalAntiApply now. I will add other types of tests.

Copy link
Contributor

@fuyufjh fuyufjh Apr 28, 2022

Choose a reason for hiding this comment

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

It's okay. You may add a test expecting error output if that's not supported yet.

Copy link
Member

Choose a reason for hiding this comment

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

+1 for an error test

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 just tried it and it will panic, so it doesn't display any error. I just asked @skyzh about this, and he said that planner test can't handle panic.

Copy link
Contributor

Choose a reason for hiding this comment

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

create_apply_or_join may not be enough. This issue is mostly specific to ExprImpl::Subquery, ExprImpl::CorrelatedInputRef, and LogicalApply: they are supposed to be present in the expr tree or plan tree in early phases, and completely replaced later. Another way to report errors is to add a dedicate phase than scans the whole plan for any undesired residuals - if we do not want to make existing to_prost / to_batch / to_stream to return Result.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another way to report errors is to add a dedicate phase than scans the whole plan for any undesired residuals

Do you mean do this check right after unnesting? If so, then we should add optimizer_error.

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant impl PlanVisitor just to check the existence of one of the three, and call it in PlanRoot::gen_batch_query_plan / PlanRoot::gen_stream_plan immediately after gen_optimized_logical_plan.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In conclusion, we will add optimizer error or something else in future prs to report error instead of panicing.

Copy link
Contributor

@st1page st1page Apr 29, 2022

Choose a reason for hiding this comment

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

impl PlanVisitor just to check the existence of one of the three

+1

src/frontend/src/planner/select.rs Outdated Show resolved Hide resolved
src/frontend/src/planner/select.rs Outdated Show resolved Hide resolved
src/frontend/src/planner/select.rs Outdated Show resolved Hide resolved
@likg227 likg227 requested a review from xiangjinwu April 29, 2022 08:26
Copy link
Contributor

@xiangjinwu xiangjinwu left a comment

Choose a reason for hiding this comment

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

LGTM!

Just a small note for future considerations:

  • NOT (a NOT IN subquery) may cause a problem ...
  • It may be simpler to use same logic of negated as NOT EXISTS.

@likg227
Copy link
Contributor Author

likg227 commented Apr 29, 2022

LGTM!

Just a small note for future considerations:

  • NOT (a NOT IN subquery) may cause a problem ...
  • It may be simpler to use same logic of negated as NOT EXISTS.

NOT EXISTS will be parsed to NOT + EXISTS. They are not an entity.

@likg227 likg227 merged commit 00efabd into main Apr 29, 2022
@likg227 likg227 deleted the lkg/in_subquery branch April 29, 2022 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

frontend: support IN subquery
6 participants