github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/rpc/base_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package rpc 16 17 import ( 18 "context" 19 "fmt" 20 "runtime" 21 "testing" 22 "time" 23 24 "github.com/matrixorigin/matrixone/pkg/catalog" 25 "github.com/matrixorigin/matrixone/pkg/common/moerr" 26 "github.com/matrixorigin/matrixone/pkg/common/mpool" 27 "github.com/matrixorigin/matrixone/pkg/container/batch" 28 "github.com/matrixorigin/matrixone/pkg/container/types" 29 "github.com/matrixorigin/matrixone/pkg/container/vector" 30 "github.com/matrixorigin/matrixone/pkg/pb/api" 31 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 32 "github.com/matrixorigin/matrixone/pkg/pb/timestamp" 33 "github.com/matrixorigin/matrixone/pkg/pb/txn" 34 "github.com/matrixorigin/matrixone/pkg/vm/engine" 35 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/blockio" 36 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/common" 37 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db" 38 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options" 39 "github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils" 40 ) 41 42 const ModuleName = "TAEHANDLE" 43 44 type mockHandle struct { 45 *Handle 46 m *mpool.MPool 47 } 48 49 type CmdType int32 50 51 const ( 52 CmdPreCommitWrite CmdType = iota 53 CmdPrepare 54 CmdCommitting 55 CmdCommit 56 CmdRollback 57 ) 58 59 type txnCommand struct { 60 typ CmdType 61 cmd any 62 } 63 64 func (h *mockHandle) HandleClose(ctx context.Context) error { 65 err := h.Handle.HandleClose(ctx) 66 blockio.Stop() 67 blockio.ResetPipeline() 68 return err 69 } 70 71 func (h *mockHandle) HandleCommit(ctx context.Context, meta *txn.TxnMeta) (timestamp.Timestamp, error) { 72 //2PC 73 if len(meta.TNShards) > 1 && meta.CommitTS.IsEmpty() { 74 meta.CommitTS = meta.PreparedTS.Next() 75 } 76 return h.Handle.HandleCommit(ctx, *meta) 77 } 78 79 func (h *mockHandle) HandleCommitting(ctx context.Context, meta *txn.TxnMeta) error { 80 //meta.CommitTS = h.eng.GetTAE(context.TODO()).TxnMgr.TsAlloc.Alloc().ToTimestamp() 81 if meta.PreparedTS.IsEmpty() { 82 return moerr.NewInternalError(ctx, "PreparedTS is empty") 83 } 84 meta.CommitTS = meta.PreparedTS.Next() 85 return h.Handle.HandleCommitting(ctx, *meta) 86 } 87 88 func (h *mockHandle) HandlePrepare(ctx context.Context, meta *txn.TxnMeta) error { 89 pts, err := h.Handle.HandlePrepare(ctx, *meta) 90 if err != nil { 91 return err 92 } 93 meta.PreparedTS = pts 94 return nil 95 } 96 97 func (h *mockHandle) HandleRollback(ctx context.Context, meta *txn.TxnMeta) error { 98 return h.Handle.HandleRollback(ctx, *meta) 99 } 100 101 func (h *mockHandle) HandlePreCommit( 102 ctx context.Context, 103 meta *txn.TxnMeta, 104 req *api.PrecommitWriteCmd, 105 resp *api.SyncLogTailResp) error { 106 107 return h.Handle.HandlePreCommitWrite(ctx, *meta, req, resp) 108 } 109 110 func (h *mockHandle) handleCmds( 111 ctx context.Context, 112 txn *txn.TxnMeta, 113 cmds []txnCommand) (err error) { 114 for _, e := range cmds { 115 switch e.typ { 116 case CmdPreCommitWrite: 117 cmd, ok := e.cmd.(api.PrecommitWriteCmd) 118 if !ok { 119 return moerr.NewInfo(ctx, "cmd is not PreCommitWriteCmd") 120 } 121 if err = h.Handle.HandlePreCommitWrite(ctx, *txn, 122 &cmd, new(api.SyncLogTailResp)); err != nil { 123 return 124 } 125 case CmdPrepare: 126 if err = h.HandlePrepare(ctx, txn); err != nil { 127 return 128 } 129 case CmdCommitting: 130 if err = h.HandleCommitting(ctx, txn); err != nil { 131 return 132 } 133 case CmdCommit: 134 if _, err = h.HandleCommit(ctx, txn); err != nil { 135 return 136 } 137 case CmdRollback: 138 if err = h.HandleRollback(ctx, txn); err != nil { 139 return 140 } 141 default: 142 panic(moerr.NewInfo(ctx, "Invalid CmdType")) 143 } 144 } 145 return 146 } 147 148 func initDB(ctx context.Context, t *testing.T, opts *options.Options) *db.DB { 149 dir := testutils.InitTestEnv(ModuleName, t) 150 db, _ := db.Open(ctx, dir, opts) 151 return db 152 } 153 154 func mockTAEHandle(ctx context.Context, t *testing.T, opts *options.Options) *mockHandle { 155 blockio.Start() 156 tae := initDB(ctx, t, opts) 157 mh := &mockHandle{ 158 m: mpool.MustNewZero(), 159 } 160 161 mh.Handle = &Handle{ 162 db: tae, 163 } 164 mh.Handle.txnCtxs = common.NewMap[string, *txnContext](runtime.GOMAXPROCS(0)) 165 return mh 166 } 167 168 func mock1PCTxn(db *db.DB) *txn.TxnMeta { 169 txnMeta := &txn.TxnMeta{} 170 txnMeta.ID = db.TxnMgr.IdAlloc.Alloc() 171 txnMeta.SnapshotTS = db.TxnMgr.Now().ToTimestamp() 172 return txnMeta 173 } 174 175 func mockTNShard(id uint64) metadata.TNShard { 176 return metadata.TNShard{ 177 TNShardRecord: metadata.TNShardRecord{ 178 ShardID: id, 179 LogShardID: id, 180 }, 181 ReplicaID: id, 182 Address: fmt.Sprintf("dn-%d", id), 183 } 184 } 185 186 func mock2PCTxn(db *db.DB) *txn.TxnMeta { 187 txnMeta := &txn.TxnMeta{} 188 txnMeta.ID = db.TxnMgr.IdAlloc.Alloc() 189 txnMeta.SnapshotTS = db.TxnMgr.Now().ToTimestamp() 190 txnMeta.TNShards = append(txnMeta.TNShards, mockTNShard(1)) 191 txnMeta.TNShards = append(txnMeta.TNShards, mockTNShard(2)) 192 return txnMeta 193 } 194 195 const ( 196 INSERT = iota 197 DELETE 198 ) 199 200 type AccessInfo struct { 201 accountId uint32 202 userId uint32 203 roleId uint32 204 } 205 206 // Entry represents a delete/insert 207 type Entry struct { 208 typ int 209 tableId uint64 210 databaseId uint64 211 tableName string 212 databaseName string 213 //object name for s3 file 214 fileName string 215 // update or delete tuples 216 bat *batch.Batch 217 } 218 219 type column struct { 220 databaseId uint64 221 accountId uint32 222 tableId uint64 223 // column name 224 name string 225 tableName string 226 databaseName string 227 typ []byte 228 typLen int32 229 num int32 230 comment string 231 notNull int8 232 hasDef int8 233 defaultExpr []byte 234 constraintType string 235 isHidden int8 236 isAutoIncrement int8 237 hasUpdate int8 238 updateExpr []byte 239 clusterBy int8 240 enumValues string 241 } 242 243 func genColumns(accountId uint32, tableName, databaseName string, 244 tableId, databaseId uint64, defs []engine.TableDef) ([]column, error) { 245 { // XXX Why not store PrimaryIndexDef and 246 // then use PrimaryIndexDef for all primary key constraints. 247 mp := make(map[string]int) 248 for i, def := range defs { 249 if attr, ok := def.(*engine.AttributeDef); ok { 250 mp[attr.Attr.Name] = i 251 } 252 } 253 for _, def := range defs { 254 if constraintDef, ok := def.(*engine.ConstraintDef); ok { 255 pkeyDef := constraintDef.GetPrimaryKeyDef() 256 if pkeyDef != nil { 257 pkeyColName := pkeyDef.Pkey.PkeyColName 258 attr, _ := defs[mp[pkeyColName]].(*engine.AttributeDef) 259 attr.Attr.Primary = true 260 } 261 } 262 } 263 } 264 num := 0 265 cols := make([]column, 0, len(defs)) 266 for _, def := range defs { 267 attrDef, ok := def.(*engine.AttributeDef) 268 if !ok { 269 continue 270 } 271 typ, err := types.Encode(&attrDef.Attr.Type) 272 if err != nil { 273 return nil, err 274 } 275 col := column{ 276 typ: typ, 277 typLen: int32(len(typ)), 278 accountId: accountId, 279 tableId: tableId, 280 databaseId: databaseId, 281 name: attrDef.Attr.Name, 282 tableName: tableName, 283 databaseName: databaseName, 284 num: int32(num), 285 comment: attrDef.Attr.Comment, 286 enumValues: attrDef.Attr.EnumVlaues, 287 } 288 col.hasDef = 0 289 if attrDef.Attr.Default != nil { 290 defaultExpr, err := types.Encode(attrDef.Attr.Default) 291 if err != nil { 292 return nil, err 293 } 294 if len(defaultExpr) > 0 { 295 col.hasDef = 1 296 col.defaultExpr = defaultExpr 297 } 298 } 299 if attrDef.Attr.OnUpdate != nil { 300 expr, err := types.Encode(attrDef.Attr.OnUpdate) 301 if err != nil { 302 return nil, err 303 } 304 if len(expr) > 0 { 305 col.hasUpdate = 1 306 col.updateExpr = expr 307 } 308 } 309 if attrDef.Attr.IsHidden { 310 col.isHidden = 1 311 } 312 if attrDef.Attr.AutoIncrement { 313 col.isAutoIncrement = 1 314 } 315 if attrDef.Attr.Primary { 316 col.constraintType = catalog.SystemColPKConstraint 317 } else { 318 col.constraintType = catalog.SystemColNoConstraint 319 } 320 cols = append(cols, col) 321 num++ 322 } 323 return cols, nil 324 } 325 326 func makePBEntry( 327 typ int, 328 dbId, 329 tableId uint64, 330 dbName, 331 tbName, 332 file string, 333 bat *batch.Batch) (pe *api.Entry, err error) { 334 e := Entry{ 335 typ: typ, 336 databaseName: dbName, 337 databaseId: dbId, 338 tableName: tbName, 339 tableId: tableId, 340 fileName: file, 341 bat: bat, 342 } 343 return toPBEntry(e) 344 } 345 346 func getTableComment(defs []engine.TableDef) string { 347 for _, def := range defs { 348 if cdef, ok := def.(*engine.CommentDef); ok { 349 return cdef.Comment 350 } 351 } 352 return "" 353 } 354 355 func genCreateDatabaseTuple( 356 sql string, 357 accountId, 358 userId, 359 roleId uint32, 360 name string, 361 id uint64, 362 typ string, 363 m *mpool.MPool, 364 ) (*batch.Batch, error) { 365 bat := batch.NewWithSize(len(catalog.MoDatabaseSchema)) 366 bat.Attrs = append(bat.Attrs, catalog.MoDatabaseSchema...) 367 { 368 idx := catalog.MO_DATABASE_DAT_ID_IDX 369 bat.Vecs[idx] = vector.NewVec(catalog.MoDatabaseTypes[idx]) // dat_id 370 if err := vector.AppendFixed(bat.Vecs[idx], uint64(id), false, m); err != nil { 371 return nil, err 372 } 373 idx = catalog.MO_DATABASE_DAT_NAME_IDX 374 bat.Vecs[idx] = vector.NewVec(catalog.MoDatabaseTypes[idx]) // datname 375 if err := vector.AppendBytes(bat.Vecs[idx], []byte(name), false, m); err != nil { 376 return nil, err 377 } 378 idx = catalog.MO_DATABASE_DAT_CATALOG_NAME_IDX 379 bat.Vecs[idx] = vector.NewVec(catalog.MoDatabaseTypes[idx]) // dat_catalog_name 380 if err := vector.AppendBytes(bat.Vecs[idx], []byte(catalog.MO_CATALOG), false, m); err != nil { 381 return nil, err 382 } 383 idx = catalog.MO_DATABASE_CREATESQL_IDX 384 bat.Vecs[idx] = vector.NewVec(catalog.MoDatabaseTypes[idx]) // dat_createsql 385 if err := vector.AppendBytes(bat.Vecs[idx], []byte(sql), false, m); err != nil { // TODO 386 return nil, err 387 } 388 idx = catalog.MO_DATABASE_OWNER_IDX 389 bat.Vecs[idx] = vector.NewVec(catalog.MoDatabaseTypes[idx]) // owner 390 if err := vector.AppendFixed(bat.Vecs[idx], roleId, false, m); err != nil { 391 return nil, err 392 } 393 idx = catalog.MO_DATABASE_CREATOR_IDX 394 bat.Vecs[idx] = vector.NewVec(catalog.MoDatabaseTypes[idx]) // creator 395 if err := vector.AppendFixed(bat.Vecs[idx], userId, false, m); err != nil { 396 return nil, err 397 } 398 idx = catalog.MO_DATABASE_CREATED_TIME_IDX 399 bat.Vecs[idx] = vector.NewVec(catalog.MoDatabaseTypes[idx]) // created_time 400 if err := vector.AppendFixed(bat.Vecs[idx], types.Timestamp(time.Now().Unix()), false, m); err != nil { 401 return nil, err 402 } 403 idx = catalog.MO_DATABASE_ACCOUNT_ID_IDX 404 bat.Vecs[idx] = vector.NewVec(catalog.MoDatabaseTypes[idx]) // account_id 405 if err := vector.AppendFixed(bat.Vecs[idx], accountId, false, m); err != nil { 406 return nil, err 407 } 408 idx = catalog.MO_DATABASE_DAT_TYPE_IDX 409 bat.Vecs[idx] = vector.NewVec(catalog.MoDatabaseTypes[idx]) // dat_type 410 if err := vector.AppendBytes(bat.Vecs[idx], []byte(typ), false, m); err != nil { 411 return nil, err 412 } 413 } 414 return bat, nil 415 } 416 417 func genCreateColumnTuple( 418 col column, 419 m *mpool.MPool, 420 ) (*batch.Batch, error) { 421 bat := batch.NewWithSize(len(catalog.MoColumnsSchema)) 422 bat.Attrs = append(bat.Attrs, catalog.MoColumnsSchema...) 423 bat.SetRowCount(1) 424 { 425 idx := catalog.MO_COLUMNS_ATT_UNIQ_NAME_IDX 426 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_uniq_name 427 if err := vector.AppendBytes(bat.Vecs[idx], []byte(""), false, m); err != nil { 428 return nil, err 429 } 430 idx = catalog.MO_COLUMNS_ACCOUNT_ID_IDX 431 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // account_id 432 if err := vector.AppendFixed(bat.Vecs[idx], uint32(0), false, m); err != nil { 433 return nil, err 434 } 435 idx = catalog.MO_COLUMNS_ATT_DATABASE_ID_IDX 436 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_database_id 437 if err := vector.AppendFixed(bat.Vecs[idx], col.databaseId, false, m); err != nil { 438 return nil, err 439 } 440 idx = catalog.MO_COLUMNS_ATT_DATABASE_IDX 441 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_database 442 if err := vector.AppendBytes(bat.Vecs[idx], []byte(col.databaseName), false, m); err != nil { 443 return nil, err 444 } 445 446 idx = catalog.MO_COLUMNS_ATT_RELNAME_ID_IDX 447 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_relname_id 448 if err := vector.AppendFixed(bat.Vecs[idx], col.tableId, false, m); err != nil { 449 return nil, err 450 } 451 452 idx = catalog.MO_COLUMNS_ATT_RELNAME_IDX 453 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_relname_id 454 if err := vector.AppendBytes(bat.Vecs[idx], []byte(col.tableName), false, m); err != nil { 455 return nil, err 456 } 457 //idx = catalog.MO_COLUMNS_ATTNUM_IDX 458 idx = catalog.MO_COLUMNS_ATTNAME_IDX 459 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // attname 460 if err := vector.AppendBytes(bat.Vecs[idx], []byte(col.name), false, m); err != nil { 461 return nil, err 462 } 463 idx = catalog.MO_COLUMNS_ATTTYP_IDX 464 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_relname 465 if err := vector.AppendBytes(bat.Vecs[idx], col.typ, false, m); err != nil { 466 return nil, err 467 } 468 idx = catalog.MO_COLUMNS_ATTNUM_IDX 469 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // attnum 470 if err := vector.AppendFixed(bat.Vecs[idx], col.num, false, m); err != nil { 471 return nil, err 472 } 473 idx = catalog.MO_COLUMNS_ATT_LENGTH_IDX 474 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_length 475 if err := vector.AppendFixed(bat.Vecs[idx], col.typLen, false, m); err != nil { 476 return nil, err 477 } 478 idx = catalog.MO_COLUMNS_ATTNOTNULL_IDX 479 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // attnotnul 480 if err := vector.AppendFixed(bat.Vecs[idx], col.notNull, false, m); err != nil { 481 return nil, err 482 } 483 idx = catalog.MO_COLUMNS_ATTHASDEF_IDX 484 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // atthasdef 485 if err := vector.AppendFixed(bat.Vecs[idx], col.hasDef, false, m); err != nil { 486 return nil, err 487 } 488 idx = catalog.MO_COLUMNS_ATT_DEFAULT_IDX 489 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_default 490 if err := vector.AppendBytes(bat.Vecs[idx], col.defaultExpr, false, m); err != nil { 491 return nil, err 492 } 493 idx = catalog.MO_COLUMNS_ATTISDROPPED_IDX 494 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // attisdropped 495 if err := vector.AppendFixed(bat.Vecs[idx], int8(0), false, m); err != nil { 496 return nil, err 497 } 498 idx = catalog.MO_COLUMNS_ATT_CONSTRAINT_TYPE_IDX 499 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_constraint_type 500 if err := vector.AppendBytes(bat.Vecs[idx], []byte(col.constraintType), false, m); err != nil { 501 return nil, err 502 } 503 idx = catalog.MO_COLUMNS_ATT_IS_UNSIGNED_IDX 504 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_is_unsigned 505 if err := vector.AppendFixed(bat.Vecs[idx], int8(0), false, m); err != nil { 506 return nil, err 507 } 508 idx = catalog.MO_COLUMNS_ATT_IS_AUTO_INCREMENT_IDX 509 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_is_auto_increment 510 if err := vector.AppendFixed(bat.Vecs[idx], col.isAutoIncrement, false, m); err != nil { 511 return nil, err 512 } 513 idx = catalog.MO_COLUMNS_ATT_COMMENT_IDX 514 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_comment 515 if err := vector.AppendBytes(bat.Vecs[idx], []byte(col.comment), false, m); err != nil { 516 return nil, err 517 } 518 idx = catalog.MO_COLUMNS_ATT_IS_HIDDEN_IDX 519 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_is_hidden 520 if err := vector.AppendFixed(bat.Vecs[idx], col.isHidden, false, m); err != nil { 521 return nil, err 522 } 523 524 idx = catalog.MO_COLUMNS_ATT_HAS_UPDATE_IDX 525 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_has_update 526 if err := vector.AppendFixed(bat.Vecs[idx], col.hasUpdate, false, m); err != nil { 527 return nil, err 528 } 529 idx = catalog.MO_COLUMNS_ATT_UPDATE_IDX 530 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_update 531 if err := vector.AppendBytes(bat.Vecs[idx], col.updateExpr, false, m); err != nil { 532 return nil, err 533 } 534 idx = catalog.MO_COLUMNS_ATT_IS_CLUSTERBY 535 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_is_clusterby 536 if err := vector.AppendFixed(bat.Vecs[idx], col.clusterBy, false, m); err != nil { 537 return nil, err 538 } 539 idx = catalog.MO_COLUMNS_ATT_SEQNUM_IDX 540 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_is_clusterby 541 if err := vector.AppendFixed(bat.Vecs[idx], uint16(0) /*just create*/, false, m); err != nil { 542 return nil, err 543 } 544 idx = catalog.MO_COLUMNS_ATT_ENUM_IDX 545 bat.Vecs[idx] = vector.NewVec(catalog.MoColumnsTypes[idx]) // att_enum 546 if err := vector.AppendBytes(bat.Vecs[idx], []byte(col.enumValues), false, m); err != nil { 547 return nil, err 548 } 549 550 } 551 return bat, nil 552 } 553 554 func makeCreateDatabaseEntries( 555 sql string, 556 ac AccessInfo, 557 name string, 558 id uint64, 559 m *mpool.MPool, 560 ) ([]*api.Entry, error) { 561 562 createDbBat, err := genCreateDatabaseTuple( 563 sql, 564 ac.accountId, 565 ac.userId, 566 ac.roleId, 567 name, 568 id, 569 "", 570 m, 571 ) 572 if err != nil { 573 return nil, err 574 } 575 createDbEntry, err := makePBEntry( 576 INSERT, 577 catalog.MO_CATALOG_ID, 578 catalog.MO_DATABASE_ID, 579 catalog.MO_CATALOG, 580 catalog.MO_DATABASE, 581 "", 582 createDbBat, 583 ) 584 if err != nil { 585 return nil, err 586 } 587 var entries []*api.Entry 588 entries = append(entries, createDbEntry) 589 return entries, nil 590 591 } 592 593 func makeCreateTableEntries( 594 sql string, 595 ac AccessInfo, 596 name string, 597 tableId uint64, 598 dbId uint64, 599 dbName string, 600 constraint []byte, 601 m *mpool.MPool, 602 defs []engine.TableDef, 603 ) ([]*api.Entry, error) { 604 comment := getTableComment(defs) 605 cols, err := genColumns(ac.accountId, name, dbName, tableId, dbId, defs) 606 if err != nil { 607 return nil, err 608 } 609 //var entries []*api.Entry 610 entries := make([]*api.Entry, 0) 611 { 612 bat, err := genCreateTableTuple( 613 sql, ac.accountId, ac.userId, ac.roleId, 614 name, tableId, dbId, dbName, comment, constraint, m) 615 if err != nil { 616 return nil, err 617 } 618 createTbEntry, err := makePBEntry(INSERT, 619 catalog.MO_CATALOG_ID, catalog.MO_TABLES_ID, 620 catalog.MO_CATALOG, catalog.MO_TABLES, "", bat) 621 if err != nil { 622 return nil, err 623 } 624 entries = append(entries, createTbEntry) 625 } 626 for _, col := range cols { 627 bat, err := genCreateColumnTuple(col, m) 628 if err != nil { 629 return nil, err 630 } 631 createColumnEntry, err := makePBEntry( 632 INSERT, catalog.MO_CATALOG_ID, 633 catalog.MO_COLUMNS_ID, catalog.MO_CATALOG, 634 catalog.MO_COLUMNS, "", bat) 635 if err != nil { 636 return nil, err 637 } 638 entries = append(entries, createColumnEntry) 639 } 640 return entries, nil 641 642 } 643 644 func genCreateTableTuple( 645 sql string, 646 accountId, 647 userId, 648 roleId uint32, 649 name string, 650 tableId uint64, 651 databaseId uint64, 652 databaseName string, 653 comment string, 654 constraint []byte, 655 m *mpool.MPool) (*batch.Batch, error) { 656 bat := batch.NewWithSize(len(catalog.MoTablesSchema)) 657 bat.Attrs = append(bat.Attrs, catalog.MoTablesSchema...) 658 bat.SetRowCount(1) 659 { 660 idx := catalog.MO_TABLES_REL_ID_IDX 661 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // rel_id 662 if err := vector.AppendFixed(bat.Vecs[idx], tableId, false, m); err != nil { 663 return nil, err 664 } 665 idx = catalog.MO_TABLES_REL_NAME_IDX 666 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // relname 667 if err := vector.AppendBytes(bat.Vecs[idx], []byte(name), false, m); err != nil { 668 return nil, err 669 } 670 idx = catalog.MO_TABLES_RELDATABASE_IDX 671 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // reldatabase 672 if err := vector.AppendBytes(bat.Vecs[idx], []byte(databaseName), false, m); err != nil { 673 return nil, err 674 } 675 idx = catalog.MO_TABLES_RELDATABASE_ID_IDX 676 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // reldatabase_id 677 if err := vector.AppendFixed(bat.Vecs[idx], databaseId, false, m); err != nil { 678 return nil, err 679 } 680 idx = catalog.MO_TABLES_RELPERSISTENCE_IDX 681 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // relpersistence 682 if err := vector.AppendBytes(bat.Vecs[idx], []byte(""), false, m); err != nil { 683 return nil, err 684 } 685 idx = catalog.MO_TABLES_RELKIND_IDX 686 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // relkind 687 if err := vector.AppendBytes(bat.Vecs[idx], []byte(""), false, m); err != nil { 688 return nil, err 689 } 690 idx = catalog.MO_TABLES_REL_COMMENT_IDX 691 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // rel_comment 692 if err := vector.AppendBytes(bat.Vecs[idx], []byte(comment), false, m); err != nil { 693 return nil, err 694 } 695 idx = catalog.MO_TABLES_REL_CREATESQL_IDX 696 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // rel_createsql 697 if err := vector.AppendBytes(bat.Vecs[idx], []byte(sql), false, m); err != nil { 698 return nil, err 699 } 700 idx = catalog.MO_TABLES_CREATED_TIME_IDX 701 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // created_time 702 if err := vector.AppendFixed(bat.Vecs[idx], types.Timestamp(time.Now().Unix()), false, m); err != nil { 703 return nil, err 704 } 705 idx = catalog.MO_TABLES_CREATOR_IDX 706 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // creator 707 if err := vector.AppendFixed(bat.Vecs[idx], userId, false, m); err != nil { 708 return nil, err 709 } 710 idx = catalog.MO_TABLES_OWNER_IDX 711 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // owner 712 if err := vector.AppendFixed(bat.Vecs[idx], roleId, false, m); err != nil { 713 return nil, err 714 } 715 idx = catalog.MO_TABLES_ACCOUNT_ID_IDX 716 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // account_id 717 if err := vector.AppendFixed(bat.Vecs[idx], accountId, false, m); err != nil { 718 return nil, err 719 } 720 idx = catalog.MO_TABLES_PARTITIONED_IDX 721 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // partition 722 if err := vector.AppendFixed(bat.Vecs[idx], int8(0), false, m); err != nil { 723 return nil, err 724 } 725 idx = catalog.MO_TABLES_PARTITION_INFO_IDX 726 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // partition_info 727 if err := vector.AppendBytes(bat.Vecs[idx], []byte(""), false, m); err != nil { 728 return nil, err 729 } 730 idx = catalog.MO_TABLES_VIEWDEF_IDX 731 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // viewdef 732 if err := vector.AppendBytes(bat.Vecs[idx], []byte(""), false, m); err != nil { 733 return nil, err 734 } 735 idx = catalog.MO_TABLES_CONSTRAINT_IDX 736 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // constraint 737 if err := vector.AppendBytes(bat.Vecs[idx], []byte(constraint), false, m); err != nil { 738 return nil, err 739 } 740 idx = catalog.MO_TABLES_VERSION_IDX 741 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // version 742 if err := vector.AppendFixed(bat.Vecs[idx], uint32(0), false, m); err != nil { 743 return nil, err 744 } 745 idx = catalog.MO_TABLES_CATALOG_VERSION_IDX 746 bat.Vecs[idx] = vector.NewVec(catalog.MoTablesTypes[idx]) // catalog version 747 if err := vector.AppendFixed(bat.Vecs[idx], uint32(0), false, m); err != nil { 748 return nil, err 749 } 750 751 } 752 return bat, nil 753 } 754 755 func toPBEntry(e Entry) (*api.Entry, error) { 756 bat, err := toPBBatch(e.bat) 757 if err != nil { 758 return nil, err 759 } 760 typ := api.Entry_Insert 761 if e.typ == DELETE { 762 typ = api.Entry_Delete 763 } 764 return &api.Entry{ 765 Bat: bat, 766 EntryType: typ, 767 TableId: e.tableId, 768 DatabaseId: e.databaseId, 769 TableName: e.tableName, 770 DatabaseName: e.databaseName, 771 FileName: e.fileName, 772 }, nil 773 } 774 775 func toPBBatch(bat *batch.Batch) (*api.Batch, error) { 776 rbat := new(api.Batch) 777 rbat.Attrs = bat.Attrs 778 for _, vec := range bat.Vecs { 779 pbVector, err := vector.VectorToProtoVector(vec) 780 if err != nil { 781 return nil, err 782 } 783 rbat.Vecs = append(rbat.Vecs, pbVector) 784 } 785 return rbat, nil 786 }