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

plan: fix a bug when using correlated column as index #7357

Merged
merged 15 commits into from
Aug 24, 2018

Conversation

winoros
Copy link
Member

@winoros winoros commented Aug 10, 2018

What problem does this PR solve?

The for loop was wrongly breaked. Hence there may be case that we misjudged.

In the for loop, we check that whether this index column have corresponding equal condition which has correlated column.
So we should break once we found it. If one condition is not matched, we should continue to next condition rather than breaking the loop.

What is changed and how it works?

Break the for loop at the correct position.

Check List

Tests

  • Unit test

Will be added later.

Related changes

  • Need to be included in the release note

Fix the case that index may not use correlated column correctly.

@winoros winoros added type/bugfix This PR fixes a bug. sig/planner SIG: Planner release-note Denotes a PR that will be considered when it comes time to generate release notes. labels Aug 10, 2018
@@ -484,14 +484,15 @@ func (path *accessPath) splitCorColAccessCondFromFilters() (access, remained []e
for i := path.eqCondCount; i < len(path.idxCols); i++ {
matched := false
for j, filter := range path.tableFilters {
if !isColEqCorColOrConstant(filter, path.idxCols[i]) {
break
if used[j] || !isColEqCorColOrConstant(filter, path.idxCols[i]) {
Copy link
Member

Choose a reason for hiding this comment

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

It's better to add a UT for function splitCorColAccessCondFromFilters though we already have integration tests, with UT we can test some corner cases that are not easy to be covered.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this function is not big enough and not special enough to add test that tests this method seperately.
This is involved in how should we add test in plan package.

Copy link
Member

Choose a reason for hiding this comment

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

If we have UT for this function, even without the integration test you added in this PR, we can avoid this bug in the very beginning.

You can see the advantage of unit test through Wikipedia: https://en.wikipedia.org/wiki/Unit_testing#Advantages

Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

need UT

defer testleak.AfterTest(c)()
totalSchema := expression.NewSchema()
totalSchema.Append(&expression.Column{
ColName: model.NewCIStr("a"),
Copy link
Member

Choose a reason for hiding this comment

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

I think a better way is to:

s/a/col1/
s/b/col2/
s/c/col3/
s/d/col4/
s/e/col5/

It helps us understanding the test case.

}
}
if !found {
break
Copy link
Member

Choose a reason for hiding this comment

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

This check can be removed. we can make this function to find all the columns in the unique id list.

Copy link
Member Author

Choose a reason for hiding this comment

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

But we still need this check. In such case, we need to return nil or a full slice?

Copy link
Member

Choose a reason for hiding this comment

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

The function caller can check whether there is any column can not be found in cols by:

len(ids) == len(retCols)

Copy link
Member

Choose a reason for hiding this comment

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

Another consideration is, this function is only used in the test currently, all the column identified by ids can be found in cols. We can extend this function in the future when we meet more complicated scenarios. For now, I think we'd better to keep it simple.

zz-jason
zz-jason previously approved these changes Aug 15, 2018
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason zz-jason dismissed their stale review August 15, 2018 09:07

need one more reviewer

@zz-jason
Copy link
Member

@XuHuaiyu @lamxTyler PTAL

@zz-jason zz-jason added the status/LGT1 Indicates that a PR has LGTM 1. label Aug 15, 2018
@@ -0,0 +1,197 @@
// Copyright 2016 PingCAP, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

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

2018

ctx sessionctx.Context
}

func (s *testUnitTestSuit) SetUpSuite(col3 *C) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why name it col3?

return expr, nil
}

func (s *testUnitTestSuit) TestIndexPathSplitCorColCond(col3 *C) {
Copy link
Contributor

Choose a reason for hiding this comment

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

ditto.

if _, ok := colIDs[x.UniqueID]; ok {
return &expression.CorrelatedColumn{Column: *x}, nil
}
default:
Copy link
Contributor

Choose a reason for hiding this comment

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

redundant default? line#66 is enough.

@winoros
Copy link
Member Author

winoros commented Aug 21, 2018

/run-unit-test

@zz-jason
Copy link
Member

/run-all-tests

zz-jason
zz-jason previously approved these changes Aug 24, 2018
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

@zz-jason
Copy link
Member

@crazycs520 @XuHuaiyu PTAL

@@ -375,3 +375,18 @@ func IndexInfo2Cols(cols []*Column, index *model.IndexInfo) ([]*Column, []int) {
}
return retCols, lengths
}

// FindColumnsByUniqueIDs will find columns by checking the unique id.
// Note: This id list must be a subset of the column slice.
Copy link
Contributor

Choose a reason for hiding this comment

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

What does This id list mean?

Should we change it to `ids`?

return tp
}

func (s *testUnitTestSuit) SubstituteCol2CorCol(expr expression.Expression, colIDs map[int]struct{}) (expression.Expression, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We do no need to return error?

idxColIDs: []int{1, 3},
idxColLens: []int{types.UnspecifiedLength, 2},
access: "[eq(col1, col2) eq(col3, col4)]",
remained: "[eq(col3, col4) eq(col5, 1)]",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why eq(col3 col4) be access and remained at the same time?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because it's a prefix index.

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

LGMT

@XuHuaiyu XuHuaiyu added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Aug 24, 2018
@winoros winoros merged commit d9f7ffe into pingcap:master Aug 24, 2018
@winoros winoros deleted the corcol-bug branch August 24, 2018 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note Denotes a PR that will be considered when it comes time to generate release notes. sig/planner SIG: Planner 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.

5 participants