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: fix a bug when pushing streamAgg down (#41056) #41312

Merged

Conversation

ti-chi-bot
Copy link
Member

This is an automated cherry-pick of #41056

What problem does this PR solve?

Issue Number: close #40857

Problem Summary:
minimal reproduce process:

CREATE TABLE t (
  c1 mediumint(9) DEFAULT '-4747160',
  c2 year(4) NOT NULL DEFAULT '2075',
  c3 double DEFAULT '1.1559030660251948',
  c4 enum('wbv4','eli','d8ym','m3gsx','lz7td','o','d1k7l','y1x','xcxq','bj','n7') DEFAULT 'xcxq',
  c5 int(11) DEFAULT '255080866',
  c6 tinyint(1) DEFAULT '1',
  PRIMARY KEY (c2),
  KEY `c4d86d54-091c-4307-957b-b164c9652b7f` (c6,c4)
);

insert into t values (-4747160, 2075, 722.5719203870632, 'xcxq', 1576824797, 1);

select   /*+ stream_agg() */  bit_or( t.c5 ) as r0 from t 
where t.c3 in ( select c6 from t where not( t.c6 <> 1 ) and not( t.c3 in ( 9263.749352636818 ) ) ) 
group by t.c1;

The following plan will introduce this panic:

mysql> explain select   /*+ stream_agg() */  bit_or( t.c5 ) as r0 from t  where t.c3 in ( select c6 from t where not( t.c6 <> 1 ) and not( t.c3 in ( 9263.749352636818 ) ) )  group by t.c1;
+--------------------------------------------+----------+-----------+-------------------------------------------------------------+----------------------------------------------------------+
| id                                         | estRows  | task      | access object                                               | operator info                                            |
+--------------------------------------------+----------+-----------+-------------------------------------------------------------+----------------------------------------------------------+
| StreamAgg_17                               | 6.66     | root      |                                                             | group by:test.t.c1, funcs:bit_or(test.t.c5)->Column#13   |
| └─Sort_52                                  | 6.66     | root      |                                                             | test.t.c1                                                |
|   └─HashJoin_21                            | 6.66     | root      |                                                             | inner join, equal:[eq(Column#14, test.t.c3)]             |
|     ├─Projection_22(Build)                 | 5.33     | root      |                                                             | cast(test.t.c6, double BINARY)->Column#14                |
|     │ └─StreamAgg_43                       | 5.33     | root      |                                                             | group by:test.t.c6, funcs:firstrow(test.t.c6)->test.t.c6 |
|     │   └─IndexLookUp_44                   | 5.33     | root      |                                                             |                                                          |
|     │     ├─IndexRangeScan_40(Build)       | 10.00    | cop[tikv] | table:t, index:c4d86d54-091c-4307-957b-b164c9652b7f(c6, c4) | range:[1,1], keep order:true, stats:pseudo               |
|     │     └─StreamAgg_29(Probe)            | 5.33     | cop[tikv] |                                                             | group by:test.t.c6,                                      |
|     │       └─Selection_42                 | 6.66     | cop[tikv] |                                                             | ne(test.t.c3, 9263.749352636818)                         |
|     │         └─TableRowIDScan_41          | 10.00    | cop[tikv] | table:t                                                     | keep order:false, stats:pseudo                           |
|     └─TableReader_51(Probe)                | 10000.00 | root      |                                                             | data:TableFullScan_50                                    |
|       └─TableFullScan_50                   | 10000.00 | cop[tikv] | table:t                                                     | keep order:false, stats:pseudo                           |
+--------------------------------------------+----------+-----------+-------------------------------------------------------------+----------------------------------------------------------+
12 rows in set (0.00 sec)

What happend:
In indexLookUp, the handles(row id) fetched from index is originally ordered by index, when keepOrder is true, we will save the order of handles in indexOrder, and then tableWorker will use handles to rescan table to obtain rows. If keepOrder is true, the tableWorker will restore the rows to the original order through indexOrder. When restoring, we need to extract the handle from the row. In the above plan, the agg is pushed down, and its schma only contains c6 and does not contain the handle , but the executor thinks that the handle is in the third column of the row, resulting in "index out of range [2]".

What is changed and how it works?

The reason for the above error is that streamAgg was pushed down to double read when keepOrder was true. I looked through the historical PRs and found that @winoros solved this problem in PR#12443: if the original order of index needs to be maintained during double read (Call this situation doubleReadWithOrderReserved), we will prevent the optimizer from pushing streamagg down to double read. PR#12443 uses extraHandleCol to determine whether the current situation is doubleReadWithOrderReserved. At that time, if extraHandleCol != nil, then it must be the case of doubleReadWithOrderReserved.

But then PR#18054 added !ds.tableInfo.IsCommonHandle, which caused extraHandleCol may be nil in the case of doubleReadWithOrderReserved, so we can’t use extraHandleCol, we can use (cop.indexPlan != nil && cop.tablePlan != nil && cop.keepOrder) to represent this situation .

PR#18054:
- if cop.tablePlan != nil {
+ if cop.tablePlan != nil && !ds.tableInfo.IsCommonHandle {
  	col, isNew := cop.tablePlan.(*PhysicalTableScan).appendExtraHandleCol(ds)
  	cop.extraHandleCol = col
  	// ...
  }

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

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
Copy link
Member Author

ti-chi-bot commented Feb 12, 2023

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • fixdb

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

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

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added do-not-merge/cherry-pick-not-approved release-note-none Denotes a PR that doesn't merit a release note. ok-to-test Indicates a PR is ready to be tested. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. type/cherry-pick-for-release-6.5 This PR is cherry-picked to release-6.5 from a source PR. labels Feb 12, 2023
@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Feb 12, 2023
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Mar 1, 2023
@ti-chi-bot ti-chi-bot added the cherry-pick-approved Cherry pick PR approved by release team. label Jul 24, 2023
@ti-chi-bot ti-chi-bot bot deleted a comment from ti-chi-bot Jul 24, 2023
@ti-chi-bot ti-chi-bot bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Aug 4, 2023
@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Aug 4, 2023
@codecov
Copy link

codecov bot commented Aug 4, 2023

Codecov Report

❗ No coverage uploaded for pull request base (release-6.5@461d290). Click here to learn what that means.
The diff coverage is n/a.

Additional details and impacted files
@@               Coverage Diff                @@
##             release-6.5     #41312   +/-   ##
================================================
  Coverage               ?   73.6617%           
================================================
  Files                  ?       1083           
  Lines                  ?     347183           
  Branches               ?          0           
================================================
  Hits                   ?     255741           
  Misses                 ?      75020           
  Partials               ?      16422           

Copy link
Member

@bb7133 bb7133 left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-chi-bot
Copy link

ti-chi-bot bot commented Aug 7, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: bb7133, fixdb, winoros

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 Aug 7, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Aug 7, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-08-04 15:23:39.662314698 +0000 UTC m=+458103.604663227: ☑️ agreed by winoros.
  • 2023-08-07 02:48:34.652036065 +0000 UTC m=+671998.594384591: ☑️ agreed by bb7133.

@ti-chi-bot ti-chi-bot bot merged commit ac0e678 into pingcap:release-6.5 Aug 7, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved cherry-pick-approved Cherry pick PR approved by release team. lgtm 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/S Denotes a PR that changes 10-29 lines, ignoring generated files. status/LGT1 Indicates that a PR has LGTM 1. type/cherry-pick-for-release-6.5 This PR is cherry-picked to release-6.5 from a source PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants