-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinfoschema.go
180 lines (155 loc) · 4.81 KB
/
infoschema.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package parplan
import (
"fmt"
"github.com/pingcap/parser/mysql"
"github.com/squareup/pranadb/tidb"
"github.com/pingcap/parser/model"
"github.com/squareup/pranadb/common"
"github.com/squareup/pranadb/tidb/infoschema"
)
// Implementation of TiDB InfoSchema so we can plug our schema into the TiDB planner
// Derived from the tIDB MockInfoSchema
// We only implement the parts we actually need
type pranaInfoSchema struct {
schemaMap map[string]*schemaTables
}
type schemaTables struct {
dbInfo *model.DBInfo
tables map[string]*model.TableInfo
}
type iSSchemaInfo struct {
SchemaName string
TablesInfos map[string]*common.TableInfo
}
func schemaToInfoSchema(schema *common.Schema) infoschema.InfoSchema {
tableInfos := schema.GetAllTableInfos()
schemaInfo := iSSchemaInfo{
SchemaName: schema.Name,
TablesInfos: tableInfos,
}
result := &pranaInfoSchema{}
result.schemaMap = make(map[string]*schemaTables)
var tabInfos []*model.TableInfo
tablesMap := make(map[string]*model.TableInfo)
for _, tableInfo := range schemaInfo.TablesInfos {
if tableInfo.Internal {
continue
}
var columns []*model.ColumnInfo
for columnIndex, columnType := range tableInfo.ColumnTypes {
if tableInfo.ColsVisible != nil && !tableInfo.ColsVisible[columnIndex] {
continue
}
colType := common.ConvertPranaTypeToTiDBType(columnType)
col := &model.ColumnInfo{
State: model.StatePublic,
Offset: columnIndex,
Name: model.NewCIStr(tableInfo.ColumnNames[columnIndex]),
FieldType: *colType,
ID: int64(columnIndex + 1),
}
for _, pkIndex := range tableInfo.PrimaryKeyCols {
if columnIndex == pkIndex {
col.Flag |= mysql.PriKeyFlag
break
}
}
columns = append(columns, col)
}
tableName := model.NewCIStr(tableInfo.Name)
var indexes []*model.IndexInfo
// The TiDB planner doesn't seem to support PK with more than one column, so in this case we create a fake index
// which has all the PK cols in it, so the planner can generate an index scan with it, so we get fast lookups
// and table scans
if len(tableInfo.PrimaryKeyCols) > 1 {
var indexCols []*model.IndexColumn
for _, columnIndex := range tableInfo.PrimaryKeyCols {
col := &model.IndexColumn{
Name: model.NewCIStr(tableInfo.ColumnNames[columnIndex]),
Offset: columnIndex,
Length: -1,
}
indexCols = append(indexCols, col)
}
pkIndex := &model.IndexInfo{
ID: int64(tableInfo.ID), // we can just use the table id as we're not going to create a real index
Name: model.NewCIStr(fmt.Sprintf("%s_pk", tableInfo.Name)),
Table: tableName,
Columns: indexCols,
State: model.StatePublic,
Comment: "",
Tp: model.IndexTypeBtree,
Unique: true,
Primary: true,
Invisible: false,
Global: false,
}
indexes = append(indexes, pkIndex)
}
if tableInfo.IndexInfos != nil {
for _, indexInfo := range tableInfo.IndexInfos {
var indexCols []*model.IndexColumn
for _, columnIndex := range indexInfo.IndexCols {
col := &model.IndexColumn{
Name: model.NewCIStr(tableInfo.ColumnNames[columnIndex]),
Offset: columnIndex,
Length: -1,
}
indexCols = append(indexCols, col)
}
index := &model.IndexInfo{
ID: int64(indexInfo.ID),
Name: model.NewCIStr(fmt.Sprintf("%s_u%s", tableInfo.Name, indexInfo.Name)),
Table: tableName,
Columns: indexCols,
State: model.StatePublic,
Comment: "",
Tp: model.IndexTypeBtree,
Unique: false,
Primary: false,
Invisible: false,
Global: false,
}
indexes = append(indexes, index)
}
}
tab := &model.TableInfo{
ID: int64(tableInfo.ID),
Columns: columns,
Indices: indexes,
Name: tableName,
PKIsHandle: len(tableInfo.PrimaryKeyCols) == 1,
State: model.StatePublic,
}
tablesMap[tableInfo.Name] = tab
tabInfos = append(tabInfos, tab)
}
dbInfo := &model.DBInfo{ID: 0, Name: model.NewCIStr(schemaInfo.SchemaName), Tables: tabInfos}
tableNames := &schemaTables{
dbInfo: dbInfo,
tables: tablesMap,
}
result.schemaMap[schemaInfo.SchemaName] = tableNames
return result
}
func (pis *pranaInfoSchema) SchemaByName(schema model.CIStr) (val *model.DBInfo, ok bool) {
tableNames, ok := pis.schemaMap[schema.L]
if !ok {
return
}
return tableNames.dbInfo, true
}
func (pis *pranaInfoSchema) TableByName(schema, table model.CIStr) (t *model.TableInfo, err error) {
if tbNames, ok := pis.schemaMap[schema.L]; ok {
if t, ok = tbNames.tables[table.L]; ok {
return
}
}
return nil, tidb.ErrTableNotExists.GenWithStackByArgs(schema, table)
}
func (pis *pranaInfoSchema) TableByID(id int64) (*model.TableInfo, bool) {
panic("should not be called")
}
func (pis *pranaInfoSchema) SchemaMetaVersion() int64 {
return 0
}