-
Notifications
You must be signed in to change notification settings - Fork 264
/
compiler_context.go
346 lines (292 loc) · 8.75 KB
/
compiler_context.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright 2022 Matrix Origin
//
// 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 memoryengine
import (
"context"
"strconv"
"github.com/matrixorigin/matrixone/pkg/catalog"
"github.com/matrixorigin/matrixone/pkg/common/moerr"
"github.com/matrixorigin/matrixone/pkg/defines"
planpb "github.com/matrixorigin/matrixone/pkg/pb/plan"
pb "github.com/matrixorigin/matrixone/pkg/pb/statsinfo"
"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
"github.com/matrixorigin/matrixone/pkg/sql/plan"
"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
"github.com/matrixorigin/matrixone/pkg/testutil"
"github.com/matrixorigin/matrixone/pkg/txn/client"
"github.com/matrixorigin/matrixone/pkg/vm/engine"
"github.com/matrixorigin/matrixone/pkg/vm/process"
)
type CompilerContext struct {
ctx context.Context
defaultDB string
engine *Engine
txnOp client.TxnOperator
}
func (c *CompilerContext) GetViews() []string {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) SetViews(views []string) {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) GetSnapshot() *plan.Snapshot {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) SetSnapshot(snapshot *plan.Snapshot) {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) ReplacePlan(execPlan *planpb.Execute) (*planpb.Plan, tree.Statement, error) {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) CheckSubscriptionValid(subName, accName string, pubName string) error {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) ResolveSubscriptionTableById(tableId uint64, pubmeta *plan.SubscriptionMeta) (*plan.ObjectRef, *plan.TableDef) {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) IsPublishing(dbName string) (bool, error) {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) ResolveSnapshotWithSnapshotName(snapshotName string) (*plan.Snapshot, error) {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) CheckTimeStampValid(ts int64) (bool, error) {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) SetQueryingSubscription(meta *plan.SubscriptionMeta) {
//TODO implement me
panic("implement me")
}
func (c *CompilerContext) GetQueryingSubscription() *plan.SubscriptionMeta {
return nil
}
func (e *Engine) NewCompilerContext(
ctx context.Context,
defaultDB string,
txnOp client.TxnOperator,
) *CompilerContext {
return &CompilerContext{
ctx: ctx,
defaultDB: defaultDB,
engine: e,
txnOp: txnOp,
}
}
var _ plan.CompilerContext = new(CompilerContext)
func (c *CompilerContext) ResolveUdf(name string, ast []*plan.Expr) (*function.Udf, error) {
return nil, nil
}
func (c *CompilerContext) ResolveAccountIds(accountNames []string) ([]uint32, error) {
return []uint32{catalog.System_Account}, nil
}
func (*CompilerContext) Stats(obj *plan.ObjectRef, snapshot plan.Snapshot) (*pb.StatsInfo, error) {
return nil, nil
}
func (*CompilerContext) GetStatsCache() *plan.StatsCache {
return nil
}
func (c *CompilerContext) GetSubscriptionMeta(dbName string, snapshot plan.Snapshot) (*plan.SubscriptionMeta, error) {
return nil, nil
}
func (c *CompilerContext) GetProcess() *process.Process {
proc := testutil.NewProcess()
proc.Ctx = context.Background()
return proc
}
func (c *CompilerContext) GetQueryResultMeta(uuid string) ([]*plan.ColDef, string, error) {
return nil, "", nil
}
func (c *CompilerContext) DatabaseExists(name string, snapshot plan.Snapshot) bool {
ctx := c.GetContext()
txnOpt := c.txnOp
if plan.IsSnapshotValid(&snapshot) && snapshot.TS.Less(c.txnOp.Txn().SnapshotTS) {
txnOpt = c.txnOp.CloneSnapshotOp(*snapshot.TS)
if snapshot.Tenant != nil {
ctx = context.WithValue(ctx, defines.TenantIDKey{}, snapshot.Tenant.TenantID)
}
}
_, err := c.engine.Database(
ctx,
name,
txnOpt,
)
return err == nil
}
func (c *CompilerContext) GetDatabaseId(dbName string, snapshot plan.Snapshot) (uint64, error) {
ctx := c.GetContext()
txnOpt := c.txnOp
if plan.IsSnapshotValid(&snapshot) && snapshot.TS.Less(c.txnOp.Txn().SnapshotTS) {
txnOpt = c.txnOp.CloneSnapshotOp(*snapshot.TS)
if snapshot.Tenant != nil {
ctx = context.WithValue(ctx, defines.TenantIDKey{}, snapshot.Tenant.TenantID)
}
}
database, err := c.engine.Database(ctx, dbName, txnOpt)
if err != nil {
return 0, err
}
databaseId, err := strconv.ParseUint(database.GetDatabaseId(ctx), 10, 64)
if err != nil {
return 0, moerr.NewInternalError(ctx, "The databaseid of '%s' is not a valid number", dbName)
}
return databaseId, nil
}
func (c *CompilerContext) DefaultDatabase() string {
return c.defaultDB
}
func (c *CompilerContext) GetPrimaryKeyDef(dbName string, tableName string, snapshot plan.Snapshot) (defs []*plan.ColDef) {
attrs, err := c.getTableAttrs(dbName, tableName, snapshot)
if err != nil {
panic(err)
}
for i, attr := range attrs {
if !attr.Primary {
continue
}
defs = append(defs, engineAttrToPlanColDef(i, attr))
}
return
}
func (*CompilerContext) GetRootSql() string {
return ""
}
func (*CompilerContext) GetUserName() string {
return "root"
}
func (c *CompilerContext) GetAccountId() (uint32, error) {
return defines.GetAccountId(c.ctx)
}
func (c *CompilerContext) GetContext() context.Context {
return c.ctx
}
func (c *CompilerContext) ResolveById(tableId uint64, snapshot plan.Snapshot) (objRef *plan.ObjectRef, tableDef *plan.TableDef) {
dbName, tableName, _ := c.engine.GetNameById(c.ctx, c.txnOp, tableId)
if dbName == "" || tableName == "" {
return nil, nil
}
return c.Resolve(dbName, tableName, snapshot)
}
func (c *CompilerContext) Resolve(schemaName string, tableName string, snapshot plan.Snapshot) (objRef *plan.ObjectRef, tableDef *plan.TableDef) {
if schemaName == "" {
schemaName = c.defaultDB
}
objRef = &plan.ObjectRef{
SchemaName: schemaName,
ObjName: tableName,
}
tableDef = &plan.TableDef{
Name: tableName,
DbName: schemaName,
}
attrs, err := c.getTableAttrs(schemaName, tableName, snapshot)
if err != nil {
return nil, nil
}
for i, attr := range attrs {
// return hidden columns for update or detete statement
//if attr.IsHidden {
// switch e.stmt.(type) {
// case *tree.Update, *tree.Delete:
// default:
// continue
// }
//}
if attr.Primary {
tableDef.Pkey = &plan.PrimaryKeyDef{
Cols: []uint64{uint64(i)},
PkeyColId: uint64(i),
PkeyColName: attr.Name,
Names: []string{attr.Name},
}
}
tableDef.Cols = append(tableDef.Cols, engineAttrToPlanColDef(i, attr))
}
//TODO properties
//TODO view
return
}
func (*CompilerContext) ResolveVariable(varName string, isSystemVar bool, isGlobalVar bool) (interface{}, error) {
return nil, nil
}
func (c *CompilerContext) getTableAttrs(dbName string, tableName string, snapshot plan.Snapshot) (attrs []*engine.Attribute, err error) {
ctx := c.GetContext()
txnOpt := c.txnOp
if plan.IsSnapshotValid(&snapshot) && snapshot.TS.Less(c.txnOp.Txn().SnapshotTS) {
txnOpt = c.txnOp.CloneSnapshotOp(*snapshot.TS)
if snapshot.Tenant != nil {
ctx = context.WithValue(ctx, defines.TenantIDKey{}, snapshot.Tenant.TenantID)
}
}
db, err := c.engine.Database(
ctx,
dbName,
txnOpt,
)
if err != nil {
return nil, err
}
table, err := db.Relation(
ctx,
tableName,
nil,
)
if err != nil {
return nil, err
}
defs, err := table.TableDefs(ctx)
if err != nil {
return nil, err
}
for _, def := range defs {
attr, ok := def.(*engine.AttributeDef)
if !ok {
continue
}
attrs = append(attrs, &attr.Attr)
}
return
}
func (c *CompilerContext) SetBuildingAlterView(yesOrNo bool, dbName, viewName string) {}
func (c *CompilerContext) GetBuildingAlterView() (bool, string, string) {
return false, "", ""
}
func engineAttrToPlanColDef(idx int, attr *engine.Attribute) *plan.ColDef {
return &plan.ColDef{
ColId: uint64(attr.ID),
Name: attr.Name,
Typ: plan.Type{
Id: int32(attr.Type.Oid),
NotNullable: attr.Default != nil && !(attr.Default.NullAbility),
Width: attr.Type.Width,
Scale: attr.Type.Scale,
Enumvalues: attr.EnumVlaues,
},
Default: attr.Default,
Primary: attr.Primary,
Pkidx: int32(idx),
Comment: attr.Comment,
ClusterBy: attr.ClusterBy,
Seqnum: uint32(attr.Seqnum),
}
}