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,executor: fix 'select ...(join on partition table) for update' panic #21148

Merged
merged 20 commits into from
Jun 16, 2021

Conversation

tiancaiamao
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #20028

Problem Summary:

'select ... for update' need the table ID to construct the lock key.
When this SQL works on a partitioned table, the table ID should be partition ID.

So how to get that partition ID?
In http://github.com/pingcap/tidb/pull/14921 I try to avoid column pruning of the partition columns,
so we can get the partition column data, and then use it to calculate the partition to get the ID.

That fix works on a single table, but not the joining of tables.

What is changed and how it works?

What's Changed:

In the logical plan phase, if buildSelectLock find itself works on partitioned tables,
change the schema add an extra partition ID column to the DataSource.

Some tiny changes in the column pruning step to get the correct schema.

In the TableReader executor, fill the extra partition ID column in the chunk row.

In the SelectLockExec, use the partition ID from the chunk row, to construct the correct lock key.

How it Works:

Let the table reader return an extra column with partition ID, so the
SelectLockExec can use that partition ID to construct the lock key.

Related changes

  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test

Release note

  • Fix panic when 'select ... for update' works on a join operation and the join uses partition table

…' panic

Let the table reader return an extra column with partition ID, so the
SelectLockExec can use that partition ID to construct the lock key.
@tiancaiamao tiancaiamao requested review from a team as code owners November 19, 2020 10:23
@tiancaiamao tiancaiamao requested review from SunRunAway and removed request for a team November 19, 2020 10:23
@github-actions github-actions bot added the sig/execution SIG execution label Nov 19, 2020
@tiancaiamao
Copy link
Contributor Author

PTAL @SunRunAway @eurekaka

executor/table_reader.go Outdated Show resolved Hide resolved
planner/core/planbuilder.go Show resolved Hide resolved
@tiancaiamao
Copy link
Contributor Author

Adjusted according to the advice from @eurekaka

  • add ExtraPidCol in buildDataSource and change LogicalLock to a logicalSchemaProducer
  • add ExtraPidCol into LogicalLock's schema, and let column pruning handle the rest of the thing

Some extra information is still necessary for SelectLock to know the mapping from table ID to the partition column index.

@tiancaiamao
Copy link
Contributor Author

/run-check_dev_2

@tiancaiamao
Copy link
Contributor Author

I have reverted the code to the previous implementation.
Add extra partition ID columns in table reader and depends on column pruning to prune them does not always work.

For example, in the physical optimizing phase, if there is only one KV entry in the range, DataSource will be converted to PointGet, but the schema is not correct, the extra PID column is not pruned.
And another case is Delete, delete may not handle the projection of the used columns, so column pruning will not prune the extra partition ID column for Delete, and this lead to a panic in the binlog encoding (the schema mismatch with the chunk data)

The current implementation only adds the partition ID column when there is a SelectLock on the partitioned table, so the change is minimal and no other parts are affected.

PTAL @eurekaka

@SunRunAway SunRunAway requested review from XuHuaiyu and removed request for SunRunAway November 26, 2020 06:33
Copy link
Contributor

@eurekaka eurekaka 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-srebot
Copy link
Contributor

@eurekaka, Thanks for your review. The bot only counts LGTMs from Reviewers and higher roles, but you're still welcome to leave your comments. See the corresponding SIG page for more information. Related SIG: execution(slack).

executor/executor.go Outdated Show resolved Hide resolved
planner/core/rule_column_pruning.go Outdated Show resolved Hide resolved
for i := 0; i < len(v.ExtraPIDInfo.Columns); i++ {
col := v.ExtraPIDInfo.Columns[i]
tblID := v.ExtraPIDInfo.TblIDs[i]
offset := schema.ColumnIndex(col)
Copy link
Contributor

@XuHuaiyu XuHuaiyu Nov 27, 2020

Choose a reason for hiding this comment

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

Check the column.ID == model.ExtraPidColID is better

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check the column to be ExtraPidColID can get the column index in the schema, but can't build the mapping of

table ID => partition column index in the schema

Here we take each partition column (v.ExtraPIDInfo.Columns[i]) and find its index in the schema (offset), to build the
mapping tblID(v.ExtraPIDInfo.TblIDs[i]) => offset

executor/partition_table_test.go Outdated Show resolved Hide resolved
@XuHuaiyu XuHuaiyu added the sig/planner SIG: Planner label Nov 27, 2020
@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 98872f4

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jun 16, 2021
@ti-chi-bot ti-chi-bot merged commit 0490590 into pingcap:master Jun 16, 2021
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Jun 16, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-4.0 in PR #25501

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Jun 16, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-5.1 in PR #25502

@ti-srebot
Copy link
Contributor

cherry pick to release-5.0 in PR #25845

@tiancaiamao
Copy link
Contributor Author

This PR is terrible... it's a nightmare...
Just see how many bugs it has introduced!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-cherry-pick-release-5.0 needs-cherry-pick-release-5.1 sig/execution SIG execution sig/planner SIG: Planner size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Index out of range when querying on partitioned tables within a transaction
9 participants