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-out-of-range error when checking only_full_group_by #23844

Merged
merged 7 commits into from
Apr 6, 2021

Conversation

xuyifangreeneyes
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #23839

Problem Summary:
TiDB throws index-out-of-range error when checking only_full_group_by for a complex sql.

What is changed and how it works?

What's Changed & How it Works:

In function checkColFuncDepend, if expression.FindFieldName returns -1, i.e., it doesn't find the column name, we just jump out of loop.

However, the problem is not entirely solved. Currently TiDB thinks the sql in #23839 violates only_full_group_by rule but MySQL doesn't think so. As #22301 (comment) says, only-full-group-by checker will be refactored in the future.

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test
  • Integration test

Release note

  • fix index-out-of-range error when checking only_full_group_by

@xuyifangreeneyes xuyifangreeneyes requested a review from a team as a code owner April 3, 2021 08:08
@xuyifangreeneyes xuyifangreeneyes requested review from lzmhhh123 and removed request for a team April 3, 2021 08:08
@ti-chi-bot ti-chi-bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Apr 3, 2021
@ichn-hu ichn-hu mentioned this pull request Apr 3, 2021
Copy link
Contributor

@Reminiscent Reminiscent 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 added the status/LGT1 Indicates that a PR has LGTM 1. label Apr 6, 2021
Copy link
Contributor

@lzmhhh123 lzmhhh123 left a comment

Choose a reason for hiding this comment

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

Should this PR be cherry-picked to 5.0 or 4.0?

@ti-chi-bot
Copy link
Member

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • Reminiscent
  • lzmhhh123

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 writing /lgtm in a comment.
Reviewer can cancel approval by writing /lgtm cancel in a comment.

@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 Apr 6, 2021
@winoros
Copy link
Member

winoros commented Apr 6, 2021

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: 82e5bd4

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Apr 6, 2021
@zimulala
Copy link
Contributor

zimulala commented Apr 6, 2021

Should this PR be cherry-picked to 5.0 or 4.0?

@xuyifangreeneyes

@ti-chi-bot ti-chi-bot merged commit bc06b96 into pingcap:master Apr 6, 2021
@xuyifangreeneyes
Copy link
Contributor Author

/needs-cherry-pick-5.0

@xuyifangreeneyes
Copy link
Contributor Author

/cherrypick release-5.0

@xuyifangreeneyes
Copy link
Contributor Author

/cherry-pick release-5.0

@xuyifangreeneyes
Copy link
Contributor Author

/label needs-cherry-pick-5.0

@xuyifangreeneyes
Copy link
Contributor Author

/run-cherry-picker

@ti-srebot
Copy link
Contributor

cherry pick to release-5.0 in PR #24016

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Apr 14, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@@ -2676,7 +2676,7 @@ func checkColFuncDepend(
Name: colInfo.Name,
}
pIdx, err := expression.FindFieldName(p.OutputNames(), pkName)
if err != nil {
if err != nil || pIdx < 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to add a comment to explain when pIdx will be less than 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Here is a case when pIdx will be less than 0.

CREATE TABLE `BB` (
  `pk` int(11) NOT NULL AUTO_INCREMENT,
  `col_int_not_null` int NOT NULL,
  PRIMARY KEY (`pk`)
);

SELECT OUTR . col2 AS X
FROM
  BB AS OUTR2
INNER JOIN
  (SELECT col_int_not_null AS col1,
          pk AS col2
   FROM BB) AS OUTR ON OUTR2.col_int_not_null = OUTR.col1
GROUP BY OUTR2.col_int_not_null;

When we checkOnlyFullGroupBy for the SELECT statement and enter checkColFuncDepend, pkName.Table is OUTR which is an alias, while pkName.Name is pk which is a original name. Hence expression.FindFieldName will fail and pIdx will be less than 0. Currently, when we meet pIdx < 0, we directly regard primaryFuncDepend as false and jump out. This way is easy to implement but makes only-full-group-by checker not smart enough. Later we will refactor only-full-group-by checker and resolve the inconsistency between the alias table name and the original column name.

Copy link
Contributor

Choose a reason for hiding this comment

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

I mean, you should add comment in the code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The comment has been added into code by #25769

@xuyifangreeneyes
Copy link
Contributor Author

/label needs-cherry-pick-4.0

@xuyifangreeneyes
Copy link
Contributor Author

/run-cherry-picker

@ti-srebot
Copy link
Contributor

cherry pick to release-4.0 in PR #30645

ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Dec 12, 2021
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
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 size/S Denotes a PR that changes 10-29 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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TiDB throw error when explain a complex sql
8 participants