-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
*: fast path point select #6937
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
2198b32
*: fast point select path
coocood e855838
add comments
coocood 699b34b
fix golint
coocood e5adb09
change StatsInfo.Count to StatsCount
coocood 9c01155
fix err shadow
coocood 4942f47
Merge commit 'b729a60e019c8fec2cbae007b5b273697cac04b8' into fast-poi…
coocood 0971fff
Merge commit '90c721ad72f44ff5849e131d813509d1dedbe035' into fast-poi…
coocood 171daed
fix build
coocood a183771
revert handle -> Handle in comments
coocood 55757cd
revert unintended change
coocood 01be667
Merge commit '3d04c5ef95198e9e87611939942ed9823629f094' into fast-poi…
coocood 4bb52a0
address comments
coocood b4843e9
Merge branch 'master' into fast-point-select
coocood 5b4f1b3
Merge branch 'master' into fast-point-select
ngaut fd96ed9
Merge branch 'master' into fast-point-select
coocood aaad31e
remove update and fix tests
coocood 050235d
fix CI, add test
coocood 74fc80d
fix vet
coocood f0ff662
Merge branch 'master' into fast-point-select
coocood c8e64d5
fix result field as name
coocood b8598b2
Merge branch 'master' into fast-point-select
ngaut 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
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,200 @@ | ||
// Copyright 2018 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package executor | ||
|
||
import ( | ||
"github.com/juju/errors" | ||
"github.com/pingcap/tidb/expression" | ||
"github.com/pingcap/tidb/kv" | ||
"github.com/pingcap/tidb/model" | ||
"github.com/pingcap/tidb/mysql" | ||
"github.com/pingcap/tidb/plan" | ||
"github.com/pingcap/tidb/sessionctx" | ||
"github.com/pingcap/tidb/table" | ||
"github.com/pingcap/tidb/table/tables" | ||
"github.com/pingcap/tidb/tablecodec" | ||
"github.com/pingcap/tidb/types" | ||
"github.com/pingcap/tidb/util/chunk" | ||
"github.com/pingcap/tidb/util/codec" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
func (b *executorBuilder) buildPointGet(p *plan.PointGetPlan) Executor { | ||
return &PointGetExecutor{ | ||
ctx: b.ctx, | ||
schema: p.Schema(), | ||
tblInfo: p.TblInfo, | ||
idxInfo: p.IndexInfo, | ||
idxVals: p.IndexValues, | ||
handle: p.Handle, | ||
startTS: b.getStartTS(), | ||
} | ||
} | ||
|
||
// PointGetExecutor executes point select query. | ||
type PointGetExecutor struct { | ||
ctx sessionctx.Context | ||
schema *expression.Schema | ||
tps []*types.FieldType | ||
tblInfo *model.TableInfo | ||
handle int64 | ||
idxInfo *model.IndexInfo | ||
idxVals []types.Datum | ||
startTS uint64 | ||
snapshot kv.Snapshot | ||
done bool | ||
} | ||
|
||
// Open implements the Executor interface. | ||
func (e *PointGetExecutor) Open(context.Context) error { | ||
return nil | ||
} | ||
|
||
// Close implements the Executor interface. | ||
func (e *PointGetExecutor) Close() error { | ||
return nil | ||
} | ||
|
||
// Next implements the Executor interface. | ||
func (e *PointGetExecutor) Next(ctx context.Context, chk *chunk.Chunk) error { | ||
chk.Reset() | ||
if e.done { | ||
return nil | ||
} | ||
e.done = true | ||
var err error | ||
e.snapshot, err = e.ctx.GetStore().GetSnapshot(kv.Version{Ver: e.startTS}) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
if e.idxInfo != nil { | ||
idxKey, err1 := e.encodeIndexKey() | ||
if err1 != nil { | ||
return errors.Trace(err1) | ||
} | ||
handleVal, err1 := e.get(idxKey) | ||
if err1 != nil && !kv.ErrNotExist.Equal(err1) { | ||
return errors.Trace(err1) | ||
} | ||
if len(handleVal) == 0 { | ||
return nil | ||
} | ||
e.handle, err1 = tables.DecodeHandle(handleVal) | ||
if err1 != nil { | ||
return errors.Trace(err1) | ||
} | ||
} | ||
key := tablecodec.EncodeRowKeyWithHandle(e.tblInfo.ID, e.handle) | ||
val, err := e.get(key) | ||
if err != nil && !kv.ErrNotExist.Equal(err) { | ||
return errors.Trace(err) | ||
} | ||
if len(val) == 0 { | ||
if e.idxInfo != nil { | ||
return kv.ErrNotExist.Gen("inconsistent extra index %s, handle %d not found in table", | ||
e.idxInfo.Name.O, e.handle) | ||
} | ||
return nil | ||
} | ||
return e.decodeRowValToChunk(val, chk) | ||
} | ||
|
||
func (e *PointGetExecutor) encodeIndexKey() ([]byte, error) { | ||
for i := range e.idxVals { | ||
colInfo := e.tblInfo.Columns[e.idxInfo.Columns[i].Offset] | ||
casted, err := table.CastValue(e.ctx, e.idxVals[i], colInfo) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
e.idxVals[i] = casted | ||
} | ||
encodedIdxVals, err := codec.EncodeKey(e.ctx.GetSessionVars().StmtCtx, nil, e.idxVals...) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
return tablecodec.EncodeIndexSeekKey(e.tblInfo.ID, e.idxInfo.ID, encodedIdxVals), nil | ||
} | ||
|
||
func (e *PointGetExecutor) get(key kv.Key) (val []byte, err error) { | ||
txn := e.ctx.Txn() | ||
if txn != nil && txn.Valid() && !txn.IsReadOnly() { | ||
return txn.Get(key) | ||
} | ||
return e.snapshot.Get(key) | ||
} | ||
|
||
func (e *PointGetExecutor) decodeRowValToChunk(rowVal []byte, chk *chunk.Chunk) error { | ||
colIDs := make(map[int64]int, e.schema.Len()) | ||
for i, col := range e.schema.Columns { | ||
colIDs[col.ID] = i | ||
} | ||
colVals, err := tablecodec.CutRowNew(rowVal, colIDs) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
decoder := codec.NewDecoder(chk, e.ctx.GetSessionVars().Location()) | ||
for id, offset := range colIDs { | ||
if e.tblInfo.PKIsHandle && mysql.HasPriKeyFlag(e.schema.Columns[offset].RetType.Flag) { | ||
chk.AppendInt64(offset, e.handle) | ||
continue | ||
} | ||
if id == model.ExtraHandleID { | ||
chk.AppendInt64(offset, e.handle) | ||
continue | ||
} | ||
colVal := colVals[offset] | ||
if len(colVal) == 0 { | ||
colInfo := getColInfoByID(e.tblInfo, id) | ||
d, err1 := table.GetColOriginDefaultValue(e.ctx, colInfo) | ||
if err1 != nil { | ||
return errors.Trace(err1) | ||
} | ||
chk.AppendDatum(offset, &d) | ||
continue | ||
} | ||
_, err = decoder.DecodeOne(colVals[offset], offset, e.schema.Columns[offset].RetType) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func getColInfoByID(tbl *model.TableInfo, colID int64) *model.ColumnInfo { | ||
for _, col := range tbl.Columns { | ||
if col.ID == colID { | ||
return col | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Schema implements the Executor interface. | ||
func (e *PointGetExecutor) Schema() *expression.Schema { | ||
return e.schema | ||
} | ||
|
||
func (e *PointGetExecutor) retTypes() []*types.FieldType { | ||
if e.tps == nil { | ||
e.tps = make([]*types.FieldType, e.schema.Len()) | ||
for i := range e.schema.Columns { | ||
e.tps[i] = e.schema.Columns[i].RetType | ||
} | ||
} | ||
return e.tps | ||
} | ||
|
||
func (e *PointGetExecutor) newChunk() *chunk.Chunk { | ||
return chunk.NewChunkWithCapacity(e.retTypes(), 1) | ||
} |
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,38 @@ | ||
// Copyright 2018 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package executor_test | ||
|
||
import ( | ||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/util/testkit" | ||
) | ||
|
||
func (s *testSuite) TestPointGet(c *C) { | ||
tk := testkit.NewTestKit(c, s.store) | ||
tk.MustExec("use test") | ||
tk.MustExec("create table point (id int primary key, c int, d varchar(10), unique c_d (c, d))") | ||
tk.MustExec("insert point values (1, 1, 'a')") | ||
tk.MustExec("insert point values (2, 2, 'b')") | ||
tk.MustQuery("select * from point where id = 1 and c = 0").Check(testkit.Rows()) | ||
tk.MustQuery("select * from point where id < 0 and c = 1 and d = 'b'").Check(testkit.Rows()) | ||
result, err := tk.Exec("select id as ident from point where id = 1") | ||
c.Assert(err, IsNil) | ||
fields := result.Fields() | ||
c.Assert(fields[0].ColumnAsName.O, Equals, "ident") | ||
|
||
tk.MustExec("CREATE TABLE tab3(pk INTEGER PRIMARY KEY, col0 INTEGER, col1 FLOAT, col2 TEXT, col3 INTEGER, col4 FLOAT, col5 TEXT);") | ||
tk.MustExec("CREATE UNIQUE INDEX idx_tab3_0 ON tab3 (col4);") | ||
tk.MustExec("INSERT INTO tab3 VALUES(0,854,111.96,'mguub',711,966.36,'snwlo');") | ||
tk.MustQuery("SELECT ALL * FROM tab3 WHERE col4 = 85;").Check(testkit.Rows()) | ||
} |
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 |
---|---|---|
|
@@ -40,8 +40,8 @@ type Plan interface { | |
|
||
context() sessionctx.Context | ||
|
||
// StatsInfo will return the statsInfo for this plan. | ||
StatsInfo() *statsInfo | ||
// statsInfo will return the statsInfo for this plan. | ||
statsInfo() *statsInfo | ||
} | ||
|
||
// taskType is the type of execution task. | ||
|
@@ -91,7 +91,7 @@ func (p *requiredProp) enforceProperty(tsk task, ctx sessionctx.Context) task { | |
} | ||
tsk = finishCopTask(ctx, tsk) | ||
sortReqProp := &requiredProp{taskTp: rootTaskType, cols: p.cols, expectedCnt: math.MaxFloat64} | ||
sort := PhysicalSort{ByItems: make([]*ByItems, 0, len(p.cols))}.init(ctx, tsk.plan().StatsInfo(), sortReqProp) | ||
sort := PhysicalSort{ByItems: make([]*ByItems, 0, len(p.cols))}.init(ctx, tsk.plan().statsInfo(), sortReqProp) | ||
for _, col := range p.cols { | ||
sort.ByItems = append(sort.ByItems, &ByItems{col, p.desc}) | ||
} | ||
|
@@ -229,6 +229,9 @@ type PhysicalPlan interface { | |
// getChildReqProps gets the required property by child index. | ||
getChildReqProps(idx int) *requiredProp | ||
|
||
// StatsCount returns the count of statsInfo for this plan. | ||
StatsCount() float64 | ||
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. I think this function can be removed. 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. This is called by |
||
|
||
// Get all the children. | ||
Children() []PhysicalPlan | ||
|
||
|
@@ -349,8 +352,8 @@ func (p *basePlan) ID() int { | |
return p.id | ||
} | ||
|
||
// StatsInfo implements the Plan interface. | ||
func (p *basePlan) StatsInfo() *statsInfo { | ||
// statsInfo implements the Plan interface. | ||
func (p *basePlan) statsInfo() *statsInfo { | ||
return p.stats | ||
} | ||
|
||
|
Oops, something went wrong.
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.
why make this change?
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.
go lint reports error because it returns an unexported type
statsInfo
.