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

planner, CTE: Fix default inline CTE which contains agg or window function and refactor inline CTE strategy | tidb-test=pr/2239 #48188

Merged
merged 8 commits into from
Nov 8, 2023

Conversation

elsa0520
Copy link
Contributor

@elsa0520 elsa0520 commented Nov 1, 2023

What problem does this PR solve?

Issue Number: close #47711
close #47603

Problem Summary:

What is changed and how it works?

  1. Forbidden CTE inline when it contains or indirectly contains agg or window and is referenced by recursive part of CTE.
  2. The fake recursive CTE can be identified and judged as non-recursive CTE. (mark as recursive but query has no recursive part)
  3. Refactor the CTE inline strategy

Refactor
Unified inline strategy logic in CTE used.

  1. preprocessor and buildCTE() : collect CTE info such as consumerCount, forceInlineByHintOrVar, containAggOrWindow
  2. tryBuildCTE() : Combine the declaration of CTE and the use of CTE to jointly determine whether a CTE can be inlined.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added needs-cherry-pick-release-6.5 Should cherry pick this PR to release-6.5 branch. do-not-merge/needs-tests-checked needs-cherry-pick-release-7.1 Should cherry pick this PR to release-7.1 branch. release-note-none Denotes a PR that doesn't merit a release note. needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 1, 2023
Copy link

tiprow bot commented Nov 1, 2023

Hi @elsa0520. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 1, 2023

/test all

Copy link

tiprow bot commented Nov 1, 2023

@elsa0520: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/test all

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 1, 2023

/cherry-pick release-7.5

@ti-chi-bot
Copy link
Member

@elsa0520: once the present PR merges, I will cherry-pick it on top of release-7.5 in the new PR and assign it to you.

In response to this:

/cherry-pick release-7.5

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the ti-community-infra/tichi repository.

Copy link

codecov bot commented Nov 1, 2023

Codecov Report

Merging #48188 (02f3f47) into master (2b18f64) will increase coverage by 1.2360%.
Report is 92 commits behind head on master.
The diff coverage is 91.4285%.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #48188        +/-   ##
================================================
+ Coverage   71.5890%   72.8250%   +1.2360%     
================================================
  Files          1400       1425        +25     
  Lines        405903     413333      +7430     
================================================
+ Hits         290582     301010     +10428     
+ Misses        95511      93396      -2115     
+ Partials      19810      18927       -883     
Flag Coverage Δ
integration 43.2572% <74.2857%> (?)
unit 71.3875% <91.4285%> (-0.2016%) ⬇️

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

Components Coverage Δ
dumpling 53.9874% <ø> (-0.0629%) ⬇️
parser ∅ <ø> (∅)
br 48.7908% <ø> (-4.1012%) ⬇️

Copy link
Member

@time-and-fate time-and-fate left a comment

Choose a reason for hiding this comment

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

  1. We need some test cases.
  2. Another bad case:
explain
with a as (select count(*) as id from dual),
b as (select 2 as bb from a),
c as (
  with recursive tmp as (select 1 as res from dual union all select res+1 from tmp,b where res+1 < bb) 
  select * from tmp
)
select * from c;

For the new bad case, I think it's caused by the maintenance of buildingRecursivePartForCTE, and the current implementation doesn't process the setting and resetting properly.

Comment on lines +492 to 493
// isInline will determine whether it can be inlined when **CTE is used**
isInline bool
Copy link
Member

Choose a reason for hiding this comment

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

I think we can remove this field now.
Because it's no longer a property of a cte, it's checked every time we use the cte.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This property should be record in cte because the tryToBuildSequence use it later ~

Copy link
Member

Choose a reason for hiding this comment

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

But it seems the isInline now is not correct anymore according to the rationale of this PR. 🤔
If we want to know if it's expected by the user to inline this CTE there, we should use the new forceInlineByHintOrVar. If we want to know if it really can be inlined, we should go through the new checking logic introduced in this PR.

Copy link
Member

Choose a reason for hiding this comment

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

In tryToBuildSequence, it looks like isInline actually means "whether this CTE is really inlined in the most recent buildWith call for this CTE".

Copy link
Member

Choose a reason for hiding this comment

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

If my understanding is correct, I think we at least need some comments to clarify this.

pkg/planner/core/logical_plan_builder.go Show resolved Hide resolved
@time-and-fate
Copy link
Member

Also, there are a lot of failures in the CI, please take a look.

1. with in setoprselectlist should be record in ctestack
2. the fake recursive cte also can be inlined. such as mark as recursive but has no recursive part in query
@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 7, 2023
@ti-chi-bot ti-chi-bot bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 7, 2023
@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 7, 2023

  1. We need some test cases.
  2. Another bad case:
explain
with a as (select count(*) as id from dual),
b as (select 2 as bb from a),
c as (
  with recursive tmp as (select 1 as res from dual union all select res+1 from tmp,b where res+1 < bb) 
  select * from tmp
)
select * from c;

For the new bad case, I think it's caused by the maintenance of buildingRecursivePartForCTE, and the current implementation doesn't process the setting and resetting properly.

Cover this case and add more tests ~

@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 8, 2023

/test unit-test

Copy link

ti-chi-bot bot commented Nov 8, 2023

@elsa0520: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test build
  • /test check-dev
  • /test check-dev2
  • /test mysql-test
  • /test unit-test

The following commands are available to trigger optional jobs:

  • /test canary-notify-when-compatibility-sections-changed
  • /test pingcap/tidb/canary_ghpr_unit_test
  • /test pull-br-integration-test
  • /test pull-common-test
  • /test pull-e2e-test
  • /test pull-integration-common-test
  • /test pull-integration-copr-test
  • /test pull-integration-ddl-test
  • /test pull-integration-jdbc-test
  • /test pull-integration-mysql-test
  • /test pull-integration-nodejs-test
  • /test pull-mysql-client-test
  • /test pull-sqllogic-test
  • /test pull-tiflash-test

Use /test all to run the following jobs that were automatically triggered:

  • pingcap/tidb/ghpr_build
  • pingcap/tidb/ghpr_check
  • pingcap/tidb/ghpr_check2
  • pingcap/tidb/ghpr_mysql_test
  • pingcap/tidb/ghpr_unit_test

In response to this:

/test unit-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 8, 2023

/test check_dev_2

Copy link

ti-chi-bot bot commented Nov 8, 2023

@elsa0520: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test build
  • /test check-dev
  • /test check-dev2
  • /test mysql-test
  • /test unit-test

The following commands are available to trigger optional jobs:

  • /test canary-notify-when-compatibility-sections-changed
  • /test pingcap/tidb/canary_ghpr_unit_test
  • /test pull-br-integration-test
  • /test pull-common-test
  • /test pull-e2e-test
  • /test pull-integration-common-test
  • /test pull-integration-copr-test
  • /test pull-integration-ddl-test
  • /test pull-integration-jdbc-test
  • /test pull-integration-mysql-test
  • /test pull-integration-nodejs-test
  • /test pull-mysql-client-test
  • /test pull-sqllogic-test
  • /test pull-tiflash-test

Use /test all to run the following jobs that were automatically triggered:

  • pingcap/tidb/ghpr_build
  • pingcap/tidb/ghpr_check
  • pingcap/tidb/ghpr_check2
  • pingcap/tidb/ghpr_mysql_test
  • pingcap/tidb/ghpr_unit_test

In response to this:

/test check_dev_2

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 8, 2023

/test mysql-test

Copy link

tiprow bot commented Nov 8, 2023

@elsa0520: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test tiprow_fast_test

Use /test all to run all jobs.

In response to this:

/test mysql-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 8, 2023

/test mysql-test

Copy link

tiprow bot commented Nov 8, 2023

@elsa0520: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test tiprow_fast_test

Use /test all to run all jobs.

In response to this:

/test mysql-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 8, 2023

/test mysql-test

Copy link

tiprow bot commented Nov 8, 2023

@elsa0520: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test tiprow_fast_test

Use /test all to run all jobs.

In response to this:

/test mysql-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 8, 2023

/test mysql-test

Copy link

tiprow bot commented Nov 8, 2023

@elsa0520: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test tiprow_fast_test

Use /test all to run all jobs.

In response to this:

/test mysql-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@YangKeao
Copy link
Member

YangKeao commented Nov 8, 2023

Will also close #47603

Copy link
Member

@YangKeao YangKeao left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link

ti-chi-bot bot commented Nov 8, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: qw4990, YangKeao

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Nov 8, 2023
Copy link

ti-chi-bot bot commented Nov 8, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-11-08 03:41:31.112034241 +0000 UTC m=+3615688.699144388: ☑️ agreed by qw4990.
  • 2023-11-08 07:27:08.673658156 +0000 UTC m=+3629226.260768303: ☑️ agreed by YangKeao.

@YangKeao
Copy link
Member

YangKeao commented Nov 8, 2023

/retest

@elsa0520
Copy link
Contributor Author

elsa0520 commented Nov 8, 2023

/test mysql-test

Copy link

tiprow bot commented Nov 8, 2023

@elsa0520: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test tiprow_fast_test

Use /test all to run all jobs.

In response to this:

/test mysql-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ti-chi-bot ti-chi-bot bot merged commit c131521 into pingcap:master Nov 8, 2023
11 of 16 checks passed
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.1: #48436.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Nov 8, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-7.5: #48437.

@ti-chi-bot
Copy link
Member

In response to a cherrypick label: new pull request created to branch release-6.5: #48438.

ti-chi-bot pushed a commit to ti-chi-bot/tidb that referenced this pull request Nov 8, 2023
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm needs-cherry-pick-release-6.5 Should cherry pick this PR to release-6.5 branch. needs-cherry-pick-release-7.1 Should cherry pick this PR to release-7.1 branch. needs-cherry-pick-release-7.5 Should cherry pick this PR to release-7.5 branch. ok-to-test Indicates a PR is ready to be tested. release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
5 participants