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) #41311

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

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
@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.1 This PR is cherry-picked to release-6.1 from a source PR. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Feb 12, 2023
@fixdb
Copy link
Contributor

fixdb commented Feb 12, 2023

@Dousir9 There is merge conflict, can you resolve the conflict? thanks.

@Dousir9
Copy link
Contributor

Dousir9 commented Feb 13, 2023

@Dousir9 There is merge conflict, can you resolve the conflict? thanks.

Sure.

@Dousir9
Copy link
Contributor

Dousir9 commented Feb 13, 2023

/cherry-pick-invite

@ti-chi-bot
Copy link
Member Author

@Dousir9 you're not a member of org pingcap

@hawkingrei
Copy link
Member

/cherry-pick-invite

@ti-chi-bot
Copy link
Member Author

@hawkingrei Please accept the invitation then you can push to the cherry-pick pull requests.
Comment with "/cherry-pick-invite" if the invitation is expired.
https://github.com/ti-chi-bot/tidb/invitations

Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
@ti-chi-bot ti-chi-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Feb 13, 2023
Signed-off-by: Weizhen Wang <wangweizhen@pingcap.com>
@Dousir9
Copy link
Contributor

Dousir9 commented Feb 15, 2023

/retest

@Dousir9
Copy link
Contributor

Dousir9 commented Feb 15, 2023

Thanks for @hawkingrei .

@Dousir9
Copy link
Contributor

Dousir9 commented Feb 15, 2023

/cc fixdb

Copy link
Contributor

@fixdb fixdb left a comment

Choose a reason for hiding this comment

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

+1

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Feb 15, 2023
@ti-chi-bot ti-chi-bot added the cherry-pick-approved Cherry pick PR approved by release team. label Jun 29, 2023
@ti-chi-bot ti-chi-bot bot deleted a comment from ti-chi-bot Jun 29, 2023
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

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Jun 29, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Jun 30, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: fixdb, hawkingrei, 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:
  • OWNERS [YangKeao,hawkingrei]

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

ti-chi-bot bot commented Jun 30, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-06-29 07:19:41.55343566 +0000 UTC m=+864946.954686110: ☑️ agreed by YangKeao.
  • 2023-06-30 03:04:17.569635461 +0000 UTC m=+54740.944213314: ☑️ agreed by hawkingrei.

@ti-chi-bot ti-chi-bot bot merged commit 96f648d into pingcap:release-6.1 Jun 30, 2023
@ti-chi-bot ti-chi-bot removed the cherry-pick-approved Cherry pick PR approved by release team. label Jul 12, 2023
@ti-chi-bot ti-chi-bot added the cherry-pick-approved Cherry pick PR approved by release team. label Jul 12, 2023
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/M Denotes a PR that changes 30-99 lines, ignoring generated files. status/LGT1 Indicates that a PR has LGTM 1. type/cherry-pick-for-release-6.1 This PR is cherry-picked to release-6.1 from a source PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants