-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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: consider prefix index column length in skyline pruning #27527
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0303d7d
consider index column prefix length in skyline pruning
xuyifangreeneyes 9abf4c9
update
xuyifangreeneyes 9cf4863
resolve conflict
xuyifangreeneyes 35b2437
Merge branch 'master' into fix-issue-27313
xuyifangreeneyes 50e87b9
fmt; address comment; fix ut
xuyifangreeneyes 5d523ca
add comment
xuyifangreeneyes 0c4afcf
fix ut
xuyifangreeneyes 88ec94e
address comment
xuyifangreeneyes 401703c
Merge branch 'master' into fix-issue-27313
xuyifangreeneyes a1ecb3f
add main_test
xuyifangreeneyes 2a5be8f
Merge branch 'fix-issue-27313' of https://github.com/xuyifangreeneyes…
xuyifangreeneyes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -181,3 +181,105 @@ func (path *AccessPath) OnlyPointRange(sctx sessionctx.Context) bool { | |||||
} | ||||||
return noIntervalRange && !haveNullVal | ||||||
} | ||||||
|
||||||
// Col2Len maps expression.Column.UniqueID to column length | ||||||
type Col2Len map[int64]int | ||||||
|
||||||
// ExtractCol2Len collects index/table columns with lengths from expressions. If idxCols and idxColLens are not nil, it collects index columns with lengths(maybe prefix lengths). | ||||||
// Otherwise it collects table columns with full lengths. | ||||||
func ExtractCol2Len(exprs []expression.Expression, idxCols []*expression.Column, idxColLens []int) Col2Len { | ||||||
col2len := make(Col2Len, len(idxCols)) | ||||||
for _, expr := range exprs { | ||||||
extractCol2LenFromExpr(expr, idxCols, idxColLens, col2len) | ||||||
} | ||||||
return col2len | ||||||
} | ||||||
|
||||||
func extractCol2LenFromExpr(expr expression.Expression, idxCols []*expression.Column, idxColLens []int, col2Len Col2Len) { | ||||||
switch v := expr.(type) { | ||||||
case *expression.Column: | ||||||
if idxCols == nil { | ||||||
col2Len[v.UniqueID] = types.UnspecifiedLength | ||||||
} else { | ||||||
for i, col := range idxCols { | ||||||
if v.UniqueID == col.UniqueID { | ||||||
col2Len[v.UniqueID] = idxColLens[i] | ||||||
break | ||||||
} | ||||||
} | ||||||
} | ||||||
case *expression.ScalarFunction: | ||||||
for _, arg := range v.GetArgs() { | ||||||
extractCol2LenFromExpr(arg, idxCols, idxColLens, col2Len) | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
func compareLength(l, r int) int { | ||||||
if l == types.UnspecifiedLength && r == types.UnspecifiedLength { | ||||||
return 0 | ||||||
} | ||||||
if l == types.UnspecifiedLength { | ||||||
return 1 | ||||||
} | ||||||
if r == types.UnspecifiedLength { | ||||||
return -1 | ||||||
} | ||||||
if l > r { | ||||||
return 1 | ||||||
} | ||||||
if l == r { | ||||||
return 0 | ||||||
} | ||||||
return -1 | ||||||
} | ||||||
|
||||||
// c1 dominates c2 when each column of c2 exists in c1 and c2's column length is no longer than c1's column length. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we should follow the pattern
Suggested change
|
||||||
func (c1 Col2Len) dominate(c2 Col2Len) bool { | ||||||
if len(c2) > len(c1) { | ||||||
return false | ||||||
} | ||||||
for colID, len2 := range c2 { | ||||||
len1, ok := c1[colID] | ||||||
if !ok || compareLength(len2, len1) == 1 { | ||||||
return false | ||||||
} | ||||||
} | ||||||
return true | ||||||
} | ||||||
|
||||||
// CompareCol2Len will compare the two Col2Len maps. The last return value is used to indicate whether they are comparable. | ||||||
// When the second return value is true, the first return value: | ||||||
// (1) -1 means that c1 is worse than c2; | ||||||
// (2) 0 means that c1 equals to c2; | ||||||
// (3) 1 means that c1 is better than c2; | ||||||
func CompareCol2Len(c1, c2 Col2Len) (int, bool) { | ||||||
l1, l2 := len(c1), len(c2) | ||||||
if l1 > l2 { | ||||||
if c1.dominate(c2) { | ||||||
return 1, true | ||||||
} | ||||||
return 0, false | ||||||
} | ||||||
if l1 < l2 { | ||||||
if c2.dominate(c1) { | ||||||
return -1, true | ||||||
} | ||||||
return 0, false | ||||||
} | ||||||
// If c1 and c2 have the same columns but have different lengths on some column, we regard c1 and c2 incomparable. | ||||||
for colID, colLen2 := range c2 { | ||||||
colLen1, ok := c1[colID] | ||||||
if !ok || colLen1 != colLen2 { | ||||||
return 0, false | ||||||
} | ||||||
} | ||||||
return 0, true | ||||||
} | ||||||
|
||||||
func (path *AccessPath) GetCol2LenFromAccessConds() Col2Len { | ||||||
if path.IsTablePath() { | ||||||
return ExtractCol2Len(path.AccessConds, nil, nil) | ||||||
} | ||||||
return ExtractCol2Len(path.AccessConds, path.IdxCols, path.IdxColLens) | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
winoros marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
func TestCompareCol2Len(t *testing.T) { | ||
tests := []struct { | ||
c1 Col2Len | ||
c2 Col2Len | ||
res int | ||
comparable bool | ||
}{ | ||
{ | ||
c1: Col2Len{1:-1, 2:-1, 3:-1}, | ||
c2: Col2Len{1:-1, 2:10}, | ||
res: 1, | ||
comparable: true, | ||
}, | ||
{ | ||
c1: Col2Len{1:5}, | ||
c2: Col2Len{1:10, 2:-1}, | ||
res: -1, | ||
comparable: true, | ||
}, | ||
{ | ||
c1: Col2Len{1:-1, 2:-1}, | ||
c2: Col2Len{1:-1, 2:5, 3:-1}, | ||
res: 0, | ||
comparable: false, | ||
}, | ||
{ | ||
c1: Col2Len{1:-1, 2:10}, | ||
c2: Col2Len{1:-1, 2:5, 3:-1}, | ||
res: 0, | ||
comparable: false, | ||
}, | ||
{ | ||
c1: Col2Len{1:-1, 2:10}, | ||
c2: Col2Len{1:-1, 2:10}, | ||
res: 0, | ||
comparable: true, | ||
}, | ||
{ | ||
c1: Col2Len{1:-1, 2:-1}, | ||
c2: Col2Len{1:-1, 2:10}, | ||
res: 0, | ||
comparable: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
res, comparable := CompareCol2Len(tt.c1, tt.c2) | ||
require.Equal(t, tt.res, res) | ||
require.Equal(t, tt.comparable, comparable) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add some comments to explain the return value.