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 index join on unmatched collation suffix columns paniced #24828

Merged
merged 9 commits into from
Jun 15, 2021
Merged

planner: fix index join on unmatched collation suffix columns paniced #24828

merged 9 commits into from
Jun 15, 2021

Conversation

lysu
Copy link
Contributor

@lysu lysu commented May 21, 2021

What problem does this PR solve?

Issue Number: close #24706

Problem Summary:

index join on a, b, but b's collation is not matched, b's curIdxOff2KeyOff not be set to -1?

What is changed and how it works?

What's Changed, How it Works:

index join on a, b, but b's collation is not matched, we should only build range (a)

Related changes

  • Need to cherry-pick to the release branch 5.0

Check List

Tests

  • Unit test

Side effects

  • n/a

Release note

  • Fix panic caused by index join on unmatch collation columns

@lysu lysu added type/bugfix This PR fixes a bug. sig/planner SIG: Planner labels May 21, 2021
@lysu lysu requested review from a team as code owners May 21, 2021 08:55
@lysu lysu requested review from wshwsh12 and eurekaka and removed request for a team May 21, 2021 08:55
@ti-chi-bot ti-chi-bot added the size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. label May 21, 2021
@lysu lysu requested review from wjhuang2016 and winoros and removed request for wshwsh12 and eurekaka May 21, 2021 08:55
@lysu
Copy link
Contributor Author

lysu commented May 21, 2021

@wjhuang2016 PTAL, I'm not very sure about this change - -

@lysu lysu changed the title planner: fix index join on unmatched collation suffix columns planner: fix index join on unmatched collation suffix columns paniced May 21, 2021
@lysu lysu marked this pull request as draft May 21, 2021 09:07
@ti-chi-bot ti-chi-bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 21, 2021
@ichn-hu ichn-hu mentioned this pull request May 21, 2021
@lysu lysu marked this pull request as ready for review May 24, 2021 03:32
@ti-chi-bot ti-chi-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 24, 2021
@@ -1272,6 +1272,7 @@ func (ijHelper *indexJoinBuildHelper) removeUselessEqAndInFunc(idxCols []*expres
ijHelper.curPossibleUsedKeys = append(ijHelper.curPossibleUsedKeys, idxCols[idxColPos])
continue
}
ijHelper.curIdxOff2KeyOff[idxColPos] = -1
Copy link
Member

Choose a reason for hiding this comment

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

Why we did this change? Or should we strengthen the case to STRING type only?

Copy link
Contributor Author

@lysu lysu May 25, 2021

Choose a reason for hiding this comment

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

if we didn't change this

for table t1 (a int not null, b varcharbinary not null,index (a, b)) and t2(a int not null, b varchar not null, index(a, b))

the query: 't1 join t2 on t1. a = tb2.a and t2.a = t2.b'

will cause ranges only contain column a's value(caused by https://github.com/pingcap/tidb/pull/21201...not use the column if collation isn't matched), but keyOff2IdxOff include both a and b's offset in

for keyOff, idxOff := range keyOff2IdxOff {

so the query will panic

@winoros is any other advise to fix this~? I'm not familiar with those part logic 😂

@ti-chi-bot ti-chi-bot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels May 25, 2021
@lysu
Copy link
Contributor Author

lysu commented Jun 9, 2021

@winoros PTAL if free thx...

@winoros
Copy link
Member

winoros commented Jun 10, 2021

The logic around this function is:

  • We want to build an index's range using the join conditions.
  • There are two type expression: col_1=col_2, col_1=const, col1 in (const_list).
  • So for a join, the expression used to construct ranges may be like idx_col1=col_1 and idx_col2 in (const_list) and idx_col3 = col3.
  • The curIdxOff2KeyOff records the map relation between idx_col1 with col1 and idx_col3 with col3, so it might be the case [0, -1, 2].
  • The curNotUsedIndexCols records the expression idx_col2 in (const_list). So it would be [idx_col2] for this case.

The removeUselessEqAndInFunc is to maintain the curIdxOff2KeyOff. It will check the index column whose position is occupied by -1 in curIdxOff2KeyOff can be found in curNotUsedIndexCols. Otherwise there's neither idx_col=another_col nor idx_col=const or idx_col in (const_list) expression so we cannot build range for index from its position. So we are going to reset the value to -1 after it.

For this the issue to be fixed in the pr. The collation check for the column is a little later. We'd better add the check earlier at https://github.com/pingcap/tidb/blob/master/planner/core/exhaust_physical_plans.go#L1219.

@winoros
Copy link
Member

winoros commented Jun 10, 2021

Also, i think we'd better check the collation after we checked the column type is a STRING type. /cc @wjhuang2016

@ti-chi-bot ti-chi-bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jun 11, 2021
@lysu lysu requested a review from winoros June 11, 2021 05:01
@lysu
Copy link
Contributor Author

lysu commented Jun 15, 2021

@winoros @wjhuang2016 PTAL if free thx~

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jun 15, 2021
Co-authored-by: wjHuang <huangwenjun1997@gmail.com>
@lysu lysu requested a review from wjhuang2016 June 15, 2021 07:26
@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • winoros
  • wjhuang2016

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 status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jun 15, 2021
@lysu
Copy link
Contributor Author

lysu commented Jun 15, 2021

/merge

@ti-chi-bot
Copy link
Member

@lysu: /merge is only allowed for the committers, you can assign this pull request to the committer in list by filling /assign @committer in the comment to help merge this pull request.

In response to this:

/merge

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.

@lysu lysu added the sig/execution SIG execution label Jun 15, 2021
@lysu
Copy link
Contributor Author

lysu commented Jun 15, 2021

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: 3883bd6

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Jun 15, 2021
@zhouqiang-cl
Copy link
Contributor

/run-check_dev_2

@lysu
Copy link
Contributor Author

lysu commented Jun 15, 2021

/run-integration-common-test

@ti-chi-bot ti-chi-bot merged commit aa9e5c6 into pingcap:master Jun 15, 2021
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Jun 15, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-5.1 in PR #25436

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression needs-cherry-pick-release-5.1 sig/execution SIG execution sig/planner SIG: Planner size/M Denotes a PR that changes 30-99 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.

open new collation + index join + not null column,occur error: index out of range
6 participants