Skip to content

Commit 8268d4d

Browse files
authored
*: move irrelevant code out of package "distsql" (#5893)
1 parent e112181 commit 8268d4d

File tree

7 files changed

+219
-214
lines changed

7 files changed

+219
-214
lines changed

distsql/distsql.go

Lines changed: 8 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919
"github.com/juju/errors"
2020
"github.com/pingcap/tidb/kv"
2121
"github.com/pingcap/tidb/metrics"
22-
"github.com/pingcap/tidb/model"
23-
"github.com/pingcap/tidb/mysql"
2422
"github.com/pingcap/tidb/sessionctx"
2523
"github.com/pingcap/tidb/terror"
2624
"github.com/pingcap/tidb/types"
@@ -68,11 +66,10 @@ type PartialResult interface {
6866
}
6967

7068
type selectResult struct {
71-
label string
72-
aggregate bool
73-
resp kv.Response
69+
label string
70+
resp kv.Response
7471

75-
results chan newResultWithErr
72+
results chan resultWithErr
7673
closed chan struct{}
7774

7875
rowLen int
@@ -86,7 +83,7 @@ type selectResult struct {
8683
partialCount int64 // number of partial results.
8784
}
8885

89-
type newResultWithErr struct {
86+
type resultWithErr struct {
9087
result []byte
9188
err error
9289
}
@@ -107,15 +104,15 @@ func (r *selectResult) fetch(ctx context.Context) {
107104
for {
108105
resultSubset, err := r.resp.Next(ctx)
109106
if err != nil {
110-
r.results <- newResultWithErr{err: errors.Trace(err)}
107+
r.results <- resultWithErr{err: errors.Trace(err)}
111108
return
112109
}
113110
if resultSubset == nil {
114111
return
115112
}
116113

117114
select {
118-
case r.results <- newResultWithErr{result: resultSubset}:
115+
case r.results <- resultWithErr{result: resultSubset}:
119116
case <-r.closed:
120117
// If selectResult called Close() already, make fetch goroutine exit.
121118
return
@@ -321,7 +318,7 @@ func Select(ctx context.Context, sctx sessionctx.Context, kvReq *kv.Request, fie
321318
return &selectResult{
322319
label: "dag",
323320
resp: resp,
324-
results: make(chan newResultWithErr, kvReq.Concurrency),
321+
results: make(chan resultWithErr, kvReq.Concurrency),
325322
closed: make(chan struct{}),
326323
rowLen: len(fieldTypes),
327324
fieldTypes: fieldTypes,
@@ -338,7 +335,7 @@ func Analyze(ctx context.Context, client kv.Client, kvReq *kv.Request) (SelectRe
338335
result := &selectResult{
339336
label: "analyze",
340337
resp: resp,
341-
results: make(chan newResultWithErr, kvReq.Concurrency),
338+
results: make(chan resultWithErr, kvReq.Concurrency),
342339
closed: make(chan struct{}),
343340
}
344341
return result, nil
@@ -348,83 +345,3 @@ func Analyze(ctx context.Context, client kv.Client, kvReq *kv.Request) (SelectRe
348345
const (
349346
codeInvalidResp = 1
350347
)
351-
352-
// FieldTypeFromPBColumn creates a types.FieldType from tipb.ColumnInfo.
353-
func FieldTypeFromPBColumn(col *tipb.ColumnInfo) *types.FieldType {
354-
return &types.FieldType{
355-
Tp: byte(col.GetTp()),
356-
Flag: uint(col.Flag),
357-
Flen: int(col.GetColumnLen()),
358-
Decimal: int(col.GetDecimal()),
359-
Elems: col.Elems,
360-
Collate: mysql.Collations[uint8(col.GetCollation())],
361-
}
362-
}
363-
364-
func columnToProto(c *model.ColumnInfo) *tipb.ColumnInfo {
365-
pc := &tipb.ColumnInfo{
366-
ColumnId: c.ID,
367-
Collation: collationToProto(c.FieldType.Collate),
368-
ColumnLen: int32(c.FieldType.Flen),
369-
Decimal: int32(c.FieldType.Decimal),
370-
Flag: int32(c.Flag),
371-
Elems: c.Elems,
372-
}
373-
pc.Tp = int32(c.FieldType.Tp)
374-
return pc
375-
}
376-
377-
// TODO: update it when more collate is supported.
378-
func collationToProto(c string) int32 {
379-
v := mysql.CollationNames[c]
380-
if v == mysql.BinaryCollationID {
381-
return int32(mysql.BinaryCollationID)
382-
}
383-
// We only support binary and utf8_bin collation.
384-
// Setting other collations to utf8_bin for old data compatibility.
385-
// For the data created when we didn't enforce utf8_bin collation in create table.
386-
return int32(mysql.DefaultCollationID)
387-
}
388-
389-
// ColumnsToProto converts a slice of model.ColumnInfo to a slice of tipb.ColumnInfo.
390-
func ColumnsToProto(columns []*model.ColumnInfo, pkIsHandle bool) []*tipb.ColumnInfo {
391-
cols := make([]*tipb.ColumnInfo, 0, len(columns))
392-
for _, c := range columns {
393-
col := columnToProto(c)
394-
// TODO: Here `PkHandle`'s meaning is changed, we will change it to `IsHandle` when tikv's old select logic
395-
// is abandoned.
396-
if (pkIsHandle && mysql.HasPriKeyFlag(c.Flag)) || c.ID == model.ExtraHandleID {
397-
col.PkHandle = true
398-
} else {
399-
col.PkHandle = false
400-
}
401-
cols = append(cols, col)
402-
}
403-
return cols
404-
}
405-
406-
// IndexToProto converts a model.IndexInfo to a tipb.IndexInfo.
407-
func IndexToProto(t *model.TableInfo, idx *model.IndexInfo) *tipb.IndexInfo {
408-
pi := &tipb.IndexInfo{
409-
TableId: t.ID,
410-
IndexId: idx.ID,
411-
Unique: idx.Unique,
412-
}
413-
cols := make([]*tipb.ColumnInfo, 0, len(idx.Columns)+1)
414-
for _, c := range idx.Columns {
415-
cols = append(cols, columnToProto(t.Columns[c.Offset]))
416-
}
417-
if t.PKIsHandle {
418-
// Coprocessor needs to know PKHandle column info, so we need to append it.
419-
for _, col := range t.Columns {
420-
if mysql.HasPriKeyFlag(col.Flag) {
421-
colPB := columnToProto(col)
422-
colPB.PkHandle = true
423-
cols = append(cols, colPB)
424-
break
425-
}
426-
}
427-
}
428-
pi.Columns = cols
429-
return pi
430-
}

distsql/distsql_test.go

Lines changed: 2 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2016 PingCAP, Inc.
1+
// Copyright 2018 PingCAP, Inc.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -14,124 +14,11 @@
1414
package distsql
1515

1616
import (
17-
"errors"
18-
"testing"
19-
20-
. "github.com/pingcap/check"
21-
"github.com/pingcap/tidb/model"
22-
"github.com/pingcap/tidb/mysql"
23-
"github.com/pingcap/tidb/types"
24-
"github.com/pingcap/tidb/util/testleak"
17+
"github.com/juju/errors"
2518
"github.com/pingcap/tipb/go-tipb"
2619
"golang.org/x/net/context"
2720
)
2821

29-
func TestT(t *testing.T) {
30-
CustomVerboseFlag = true
31-
TestingT(t)
32-
}
33-
34-
var _ = Suite(&testDistsqlSuite{})
35-
36-
type testDistsqlSuite struct{}
37-
38-
func (s *testDistsqlSuite) TestColumnToProto(c *C) {
39-
defer testleak.AfterTest(c)()
40-
// Make sure the Flag is set in tipb.ColumnInfo
41-
tp := types.NewFieldType(mysql.TypeLong)
42-
tp.Flag = 10
43-
tp.Collate = "utf8_bin"
44-
col := &model.ColumnInfo{
45-
FieldType: *tp,
46-
}
47-
pc := columnToProto(col)
48-
c.Assert(pc.GetFlag(), Equals, int32(10))
49-
ntp := FieldTypeFromPBColumn(pc)
50-
c.Assert(ntp, DeepEquals, tp)
51-
52-
cols := []*model.ColumnInfo{col, col}
53-
pcs := ColumnsToProto(cols, false)
54-
for _, v := range pcs {
55-
c.Assert(v.GetFlag(), Equals, int32(10))
56-
}
57-
pcs = ColumnsToProto(cols, true)
58-
for _, v := range pcs {
59-
c.Assert(v.GetFlag(), Equals, int32(10))
60-
}
61-
62-
// Make sure we only convert to supported collate.
63-
tp = types.NewFieldType(mysql.TypeVarchar)
64-
tp.Flag = 10
65-
tp.Collate = "latin1_swedish_ci"
66-
col = &model.ColumnInfo{
67-
FieldType: *tp,
68-
}
69-
pc = columnToProto(col)
70-
c.Assert(pc.Collation, Equals, int32(mysql.DefaultCollationID))
71-
}
72-
73-
func (s *testDistsqlSuite) TestIndexToProto(c *C) {
74-
defer testleak.AfterTest(c)()
75-
cols := []*model.ColumnInfo{
76-
{
77-
ID: 1,
78-
Name: model.NewCIStr("col1"),
79-
Offset: 1,
80-
},
81-
{
82-
ID: 2,
83-
Name: model.NewCIStr("col2"),
84-
Offset: 2,
85-
},
86-
}
87-
cols[0].Flag |= mysql.PriKeyFlag
88-
89-
idxCols := []*model.IndexColumn{
90-
{
91-
Name: model.NewCIStr("col1"),
92-
Offset: 1,
93-
Length: 1,
94-
},
95-
{
96-
Name: model.NewCIStr("col1"),
97-
Offset: 1,
98-
Length: 1,
99-
},
100-
}
101-
102-
idxInfos := []*model.IndexInfo{
103-
{
104-
ID: 1,
105-
Name: model.NewCIStr("idx1"),
106-
Table: model.NewCIStr("test"),
107-
Columns: idxCols,
108-
Unique: true,
109-
Primary: true,
110-
},
111-
{
112-
ID: 2,
113-
Name: model.NewCIStr("idx2"),
114-
Table: model.NewCIStr("test"),
115-
Columns: idxCols,
116-
Unique: true,
117-
Primary: true,
118-
},
119-
}
120-
121-
tbInfo := model.TableInfo{
122-
ID: 1,
123-
Name: model.NewCIStr("test"),
124-
Columns: cols,
125-
Indices: idxInfos,
126-
PKIsHandle: true,
127-
}
128-
129-
pIdx := IndexToProto(&tbInfo, idxInfos[0])
130-
c.Assert(pIdx.TableId, Equals, int64(1))
131-
c.Assert(pIdx.IndexId, Equals, int64(1))
132-
c.Assert(pIdx.Unique, Equals, true)
133-
}
134-
13522
type mockResponse struct {
13623
count int
13724
}

executor/builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ func (b *executorBuilder) buildAnalyzeColumnsPushdown(task plan.AnalyzeColumnsTa
10251025
BucketSize: maxBucketSize,
10261026
SampleSize: maxRegionSampleSize,
10271027
SketchSize: maxSketchSize,
1028-
ColumnsInfo: distsql.ColumnsToProto(cols, task.TableInfo.PKIsHandle),
1028+
ColumnsInfo: plan.ColumnsToProto(cols, task.TableInfo.PKIsHandle),
10291029
CmsketchDepth: &depth,
10301030
CmsketchWidth: &width,
10311031
}

plan/plan_to_pb.go

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ package plan
1515

1616
import (
1717
"github.com/juju/errors"
18-
"github.com/pingcap/tidb/distsql"
1918
"github.com/pingcap/tidb/expression"
2019
"github.com/pingcap/tidb/expression/aggregation"
2120
"github.com/pingcap/tidb/model"
21+
"github.com/pingcap/tidb/mysql"
2222
"github.com/pingcap/tidb/sessionctx"
2323
"github.com/pingcap/tidb/table"
2424
"github.com/pingcap/tidb/tablecodec"
@@ -93,7 +93,7 @@ func (p *PhysicalTableScan) ToPB(ctx sessionctx.Context) (*tipb.Executor, error)
9393
columns := p.Columns
9494
tsExec := &tipb.TableScan{
9595
TableId: p.Table.ID,
96-
Columns: distsql.ColumnsToProto(columns, p.Table.PKIsHandle),
96+
Columns: ColumnsToProto(columns, p.Table.PKIsHandle),
9797
Desc: p.Desc,
9898
}
9999
err := setPBColumnsDefaultValue(ctx, tsExec.Columns, p.Columns)
@@ -132,7 +132,7 @@ func (p *PhysicalIndexScan) ToPB(ctx sessionctx.Context) (*tipb.Executor, error)
132132
idxExec := &tipb.IndexScan{
133133
TableId: p.Table.ID,
134134
IndexId: p.Index.ID,
135-
Columns: distsql.ColumnsToProto(columns, p.Table.PKIsHandle),
135+
Columns: ColumnsToProto(columns, p.Table.PKIsHandle),
136136
Desc: p.Desc,
137137
}
138138
unique := checkCoverIndex(p.Index, p.Ranges)
@@ -162,3 +162,71 @@ func setPBColumnsDefaultValue(ctx sessionctx.Context, pbColumns []*tipb.ColumnIn
162162
}
163163
return nil
164164
}
165+
166+
// ColumnsToProto converts a slice of model.ColumnInfo to a slice of tipb.ColumnInfo.
167+
func ColumnsToProto(columns []*model.ColumnInfo, pkIsHandle bool) []*tipb.ColumnInfo {
168+
cols := make([]*tipb.ColumnInfo, 0, len(columns))
169+
for _, c := range columns {
170+
col := columnToProto(c)
171+
// TODO: Here `PkHandle`'s meaning is changed, we will change it to `IsHandle` when tikv's old select logic
172+
// is abandoned.
173+
if (pkIsHandle && mysql.HasPriKeyFlag(c.Flag)) || c.ID == model.ExtraHandleID {
174+
col.PkHandle = true
175+
} else {
176+
col.PkHandle = false
177+
}
178+
cols = append(cols, col)
179+
}
180+
return cols
181+
}
182+
183+
// IndexToProto converts a model.IndexInfo to a tipb.IndexInfo.
184+
func IndexToProto(t *model.TableInfo, idx *model.IndexInfo) *tipb.IndexInfo {
185+
pi := &tipb.IndexInfo{
186+
TableId: t.ID,
187+
IndexId: idx.ID,
188+
Unique: idx.Unique,
189+
}
190+
cols := make([]*tipb.ColumnInfo, 0, len(idx.Columns)+1)
191+
for _, c := range idx.Columns {
192+
cols = append(cols, columnToProto(t.Columns[c.Offset]))
193+
}
194+
if t.PKIsHandle {
195+
// Coprocessor needs to know PKHandle column info, so we need to append it.
196+
for _, col := range t.Columns {
197+
if mysql.HasPriKeyFlag(col.Flag) {
198+
colPB := columnToProto(col)
199+
colPB.PkHandle = true
200+
cols = append(cols, colPB)
201+
break
202+
}
203+
}
204+
}
205+
pi.Columns = cols
206+
return pi
207+
}
208+
209+
func columnToProto(c *model.ColumnInfo) *tipb.ColumnInfo {
210+
pc := &tipb.ColumnInfo{
211+
ColumnId: c.ID,
212+
Collation: collationToProto(c.FieldType.Collate),
213+
ColumnLen: int32(c.FieldType.Flen),
214+
Decimal: int32(c.FieldType.Decimal),
215+
Flag: int32(c.Flag),
216+
Elems: c.Elems,
217+
}
218+
pc.Tp = int32(c.FieldType.Tp)
219+
return pc
220+
}
221+
222+
// TODO: update it when more collate is supported.
223+
func collationToProto(c string) int32 {
224+
v := mysql.CollationNames[c]
225+
if v == mysql.BinaryCollationID {
226+
return int32(mysql.BinaryCollationID)
227+
}
228+
// We only support binary and utf8_bin collation.
229+
// Setting other collations to utf8_bin for old data compatibility.
230+
// For the data created when we didn't enforce utf8_bin collation in create table.
231+
return int32(mysql.DefaultCollationID)
232+
}

0 commit comments

Comments
 (0)