github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/memoryengine/table.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 memoryengine 16 17 import ( 18 "context" 19 "fmt" 20 "github.com/matrixorigin/matrixone/pkg/catalog" 21 "github.com/matrixorigin/matrixone/pkg/container/batch" 22 "github.com/matrixorigin/matrixone/pkg/container/types" 23 "github.com/matrixorigin/matrixone/pkg/container/vector" 24 "github.com/matrixorigin/matrixone/pkg/objectio" 25 "github.com/matrixorigin/matrixone/pkg/pb/api" 26 "github.com/matrixorigin/matrixone/pkg/pb/plan" 27 pb "github.com/matrixorigin/matrixone/pkg/pb/statsinfo" 28 plan2 "github.com/matrixorigin/matrixone/pkg/sql/plan" 29 "github.com/matrixorigin/matrixone/pkg/sql/util" 30 "github.com/matrixorigin/matrixone/pkg/txn/client" 31 "github.com/matrixorigin/matrixone/pkg/vm/engine" 32 ) 33 34 type Table struct { 35 id ID 36 engine *Engine 37 txnOperator client.TxnOperator 38 databaseName string 39 tableName string 40 } 41 42 var _ engine.Relation = new(Table) 43 44 func (t *Table) Stats(ctx context.Context, sync bool) (*pb.StatsInfo, error) { 45 return nil, nil 46 } 47 48 func (t *Table) Rows(ctx context.Context) (uint64, error) { 49 resps, err := DoTxnRequest[TableStatsResp]( 50 ctx, 51 t.txnOperator, 52 true, 53 t.engine.anyShard, 54 OpTableStats, 55 &TableStatsReq{ 56 TableID: t.id, 57 }, 58 ) 59 if err != nil { 60 return 0, err 61 } 62 resp := resps[0] 63 return uint64(resp.Rows), err 64 } 65 66 func (t *Table) Size(ctx context.Context, columnName string) (uint64, error) { 67 return 1, nil 68 } 69 70 func (t *Table) AddTableDef(ctx context.Context, def engine.TableDef) error { 71 72 _, err := DoTxnRequest[AddTableDefResp]( 73 ctx, 74 t.txnOperator, 75 false, 76 t.engine.allShards, 77 OpAddTableDef, 78 &AddTableDefReq{ 79 TableID: t.id, 80 Def: def.ToPBVersion(), 81 DatabaseName: t.databaseName, 82 TableName: t.tableName, 83 }, 84 ) 85 if err != nil { 86 return err 87 } 88 89 return nil 90 } 91 92 func (t *Table) DelTableDef(ctx context.Context, def engine.TableDef) error { 93 94 _, err := DoTxnRequest[DelTableDefResp]( 95 ctx, 96 t.txnOperator, 97 false, 98 t.engine.allShards, 99 OpDelTableDef, 100 &DelTableDefReq{ 101 TableID: t.id, 102 DatabaseName: t.databaseName, 103 TableName: t.tableName, 104 Def: def.ToPBVersion(), 105 }, 106 ) 107 if err != nil { 108 return err 109 } 110 111 return nil 112 } 113 114 func (t *Table) Delete(ctx context.Context, bat *batch.Batch, colName string) error { 115 if bat == nil { 116 return nil 117 } 118 vec := bat.Vecs[0] 119 shards, err := t.engine.shardPolicy.Vector( 120 ctx, 121 t.id, 122 t.TableDefs, 123 colName, 124 vec, 125 getTNServices(t.engine.cluster), 126 ) 127 if err != nil { 128 return err 129 } 130 131 for _, shard := range shards { 132 _, err := DoTxnRequest[DeleteResp]( 133 ctx, 134 t.txnOperator, 135 false, 136 thisShard(shard.Shard), 137 OpDelete, 138 &DeleteReq{ 139 TableID: t.id, 140 DatabaseName: t.databaseName, 141 TableName: t.tableName, 142 ColumnName: colName, 143 Vector: shard.Vector, 144 }, 145 ) 146 if err != nil { 147 return err 148 } 149 } 150 151 return nil 152 } 153 154 func (*Table) GetHideKey() *engine.Attribute { 155 return nil 156 } 157 158 func (*Table) GetPriKeyOrHideKey() ([]engine.Attribute, bool) { 159 return nil, false 160 } 161 162 func (t *Table) GetPrimaryKeys(ctx context.Context) ([]*engine.Attribute, error) { 163 164 resps, err := DoTxnRequest[GetPrimaryKeysResp]( 165 ctx, 166 t.txnOperator, 167 true, 168 t.engine.anyShard, 169 OpGetPrimaryKeys, 170 &GetPrimaryKeysReq{ 171 TableID: t.id, 172 }, 173 ) 174 if err != nil { 175 return nil, err 176 } 177 178 resp := resps[0] 179 180 // convert from []engine.Attribute to []*engine.Attribute 181 attrs := make([]*engine.Attribute, 0, len(resp.Attrs)) 182 for i := 0; i < len(resp.Attrs); i++ { 183 attrs = append(attrs, &resp.Attrs[i]) 184 } 185 186 return attrs, nil 187 } 188 189 func (t *Table) TableColumns(ctx context.Context) ([]*engine.Attribute, error) { 190 191 resps, err := DoTxnRequest[GetTableColumnsResp]( 192 ctx, 193 t.txnOperator, 194 true, 195 t.engine.anyShard, 196 OpGetTableColumns, 197 &GetTableColumnsReq{ 198 TableID: t.id, 199 }, 200 ) 201 if err != nil { 202 return nil, err 203 } 204 205 resp := resps[0] 206 207 // convert from []engine.Attribute to []*engine.Attribute 208 attrs := make([]*engine.Attribute, 0, len(resp.Attrs)) 209 for i := 0; i < len(resp.Attrs); i++ { 210 attrs = append(attrs, &resp.Attrs[i]) 211 } 212 213 return attrs, nil 214 } 215 216 func (t *Table) TableDefs(ctx context.Context) ([]engine.TableDef, error) { 217 218 resps, err := DoTxnRequest[GetTableDefsResp]( 219 ctx, 220 t.txnOperator, 221 true, 222 t.engine.anyShard, 223 OpGetTableDefs, 224 &GetTableDefsReq{ 225 TableID: t.id, 226 }, 227 ) 228 if err != nil { 229 return nil, err 230 } 231 232 resp := resps[0] 233 234 // convert from PB version to interface version 235 defs := make([]engine.TableDef, 0, len(resp.Defs)) 236 for i := 0; i < len(resp.Defs); i++ { 237 defs = append(defs, resp.Defs[i].FromPBVersion()) 238 } 239 240 return defs, nil 241 } 242 243 func (t *Table) CopyTableDef(ctx context.Context) *plan.TableDef { 244 return t.GetTableDef(ctx) 245 } 246 func (t *Table) GetTableDef(ctx context.Context) *plan.TableDef { 247 engineDefs, err := t.TableDefs(ctx) 248 if err != nil { 249 return nil 250 } 251 252 var clusterByDef *plan2.ClusterByDef 253 var cols []*plan2.ColDef 254 var schemaVersion uint32 255 var defs []*plan2.TableDefType 256 var properties []*plan2.Property 257 var TableType, Createsql string 258 var partitionInfo *plan2.PartitionByDef 259 var viewSql *plan2.ViewDef 260 var foreignKeys []*plan2.ForeignKeyDef 261 var primarykey *plan2.PrimaryKeyDef 262 var indexes []*plan2.IndexDef 263 var refChildTbls []uint64 264 265 for _, def := range engineDefs { 266 if attr, ok := def.(*engine.AttributeDef); ok { 267 col := &plan2.ColDef{ 268 ColId: attr.Attr.ID, 269 Name: attr.Attr.Name, 270 Typ: plan2.Type{ 271 Id: int32(attr.Attr.Type.Oid), 272 Width: attr.Attr.Type.Width, 273 Scale: attr.Attr.Type.Scale, 274 AutoIncr: attr.Attr.AutoIncrement, 275 Table: t.tableName, 276 NotNullable: attr.Attr.Default != nil && !attr.Attr.Default.NullAbility, 277 Enumvalues: attr.Attr.EnumVlaues, 278 }, 279 Primary: attr.Attr.Primary, 280 Default: attr.Attr.Default, 281 OnUpdate: attr.Attr.OnUpdate, 282 Comment: attr.Attr.Comment, 283 ClusterBy: attr.Attr.ClusterBy, 284 Hidden: attr.Attr.IsHidden, 285 Seqnum: uint32(attr.Attr.Seqnum), 286 } 287 if attr.Attr.ClusterBy { 288 clusterByDef = &plan.ClusterByDef{ 289 Name: attr.Attr.Name, 290 } 291 } 292 cols = append(cols, col) 293 } else if pro, ok := def.(*engine.PropertiesDef); ok { 294 for _, p := range pro.Properties { 295 switch p.Key { 296 case catalog.SystemRelAttr_Kind: 297 TableType = p.Value 298 case catalog.SystemRelAttr_CreateSQL: 299 Createsql = p.Value 300 default: 301 } 302 properties = append(properties, &plan2.Property{ 303 Key: p.Key, 304 Value: p.Value, 305 }) 306 } 307 } else if viewDef, ok := def.(*engine.ViewDef); ok { 308 viewSql = &plan2.ViewDef{ 309 View: viewDef.View, 310 } 311 } else if c, ok := def.(*engine.ConstraintDef); ok { 312 for _, ct := range c.Cts { 313 switch k := ct.(type) { 314 case *engine.IndexDef: 315 indexes = k.Indexes 316 case *engine.ForeignKeyDef: 317 foreignKeys = k.Fkeys 318 case *engine.RefChildTableDef: 319 refChildTbls = k.Tables 320 case *engine.PrimaryKeyDef: 321 primarykey = k.Pkey 322 case *engine.StreamConfigsDef: 323 properties = append(properties, k.Configs...) 324 } 325 } 326 } else if commnetDef, ok := def.(*engine.CommentDef); ok { 327 properties = append(properties, &plan2.Property{ 328 Key: catalog.SystemRelAttr_Comment, 329 Value: commnetDef.Comment, 330 }) 331 } else if partitionDef, ok := def.(*engine.PartitionDef); ok { 332 if partitionDef.Partitioned > 0 { 333 p := &plan2.PartitionByDef{} 334 err = p.UnMarshalPartitionInfo(([]byte)(partitionDef.Partition)) 335 if err != nil { 336 panic(fmt.Sprintf("cannot unmarshal partition metadata information: %s", err)) 337 } 338 partitionInfo = p 339 } 340 } else if v, ok := def.(*engine.VersionDef); ok { 341 schemaVersion = v.Version 342 } 343 } 344 if len(properties) > 0 { 345 defs = append(defs, &plan2.TableDefType{ 346 Def: &plan2.TableDef_DefType_Properties{ 347 Properties: &plan2.PropertiesDef{ 348 Properties: properties, 349 }, 350 }, 351 }) 352 } 353 354 if primarykey != nil && primarykey.PkeyColName == catalog.CPrimaryKeyColName { 355 primarykey.CompPkeyCol = plan2.GetColDefFromTable(cols, catalog.CPrimaryKeyColName) 356 } 357 if clusterByDef != nil && util.JudgeIsCompositeClusterByColumn(clusterByDef.Name) { 358 clusterByDef.CompCbkeyCol = plan2.GetColDefFromTable(cols, clusterByDef.Name) 359 } 360 rowIdCol := plan2.MakeRowIdColDef() 361 cols = append(cols, rowIdCol) 362 363 tableDef := &plan2.TableDef{ 364 TblId: t.GetTableID(ctx), 365 Name: t.tableName, 366 Cols: cols, 367 Defs: defs, 368 TableType: TableType, 369 Createsql: Createsql, 370 Pkey: primarykey, 371 ViewSql: viewSql, 372 Partition: partitionInfo, 373 Fkeys: foreignKeys, 374 RefChildTbls: refChildTbls, 375 ClusterBy: clusterByDef, 376 Indexes: indexes, 377 Version: schemaVersion, 378 IsTemporary: t.GetEngineType() == engine.Memory, 379 } 380 return tableDef 381 } 382 383 //func (t *Table) Truncate(ctx context.Context) (uint64, error) { 384 // 385 // resps, err := DoTxnRequest[TruncateResp]( 386 // ctx, 387 // t.txnOperator, 388 // false, 389 // t.engine.allShards, 390 // OpTruncate, 391 // &TruncateReq{ 392 // TableID: t.id, 393 // DatabaseName: t.databaseName, 394 // TableName: t.tableName, 395 // }, 396 // ) 397 // if err != nil { 398 // return 0, err 399 // } 400 // 401 // var affectedRows int64 402 // for _, resp := range resps { 403 // affectedRows += resp.AffectedRows 404 // } 405 // 406 // return uint64(affectedRows), nil 407 //} 408 409 func (t *Table) UpdateConstraint(context.Context, *engine.ConstraintDef) error { 410 // implement me 411 return nil 412 } 413 414 func (t *Table) AlterTable(ctx context.Context, c *engine.ConstraintDef, constraint [][]byte) error { 415 // implement me 416 return nil 417 } 418 419 func (t *Table) TableRenameInTxn(ctx context.Context, constraint [][]byte) error { 420 // implement me 421 return nil 422 } 423 424 func (t *Table) Update(ctx context.Context, data *batch.Batch) error { 425 data.SetRowCount(data.RowCount()) 426 shards, err := t.engine.shardPolicy.Batch( 427 ctx, 428 t.id, 429 t.TableDefs, 430 data, 431 getTNServices(t.engine.cluster), 432 ) 433 if err != nil { 434 return err 435 } 436 437 for _, shard := range shards { 438 _, err := DoTxnRequest[UpdateResp]( 439 ctx, 440 t.txnOperator, 441 false, 442 thisShard(shard.Shard), 443 OpUpdate, 444 &UpdateReq{ 445 TableID: t.id, 446 DatabaseName: t.databaseName, 447 TableName: t.tableName, 448 Batch: shard.Batch, 449 }, 450 ) 451 if err != nil { 452 return err 453 } 454 } 455 456 return nil 457 } 458 459 func (t *Table) Write(ctx context.Context, data *batch.Batch) error { 460 data.SetRowCount(data.RowCount()) 461 shards, err := t.engine.shardPolicy.Batch( 462 ctx, 463 t.id, 464 t.TableDefs, 465 data, 466 getTNServices(t.engine.cluster), 467 ) 468 if err != nil { 469 return err 470 } 471 472 for _, shard := range shards { 473 _, err := DoTxnRequest[WriteResp]( 474 ctx, 475 t.txnOperator, 476 false, 477 thisShard(shard.Shard), 478 OpWrite, 479 &WriteReq{ 480 TableID: t.id, 481 DatabaseName: t.databaseName, 482 TableName: t.tableName, 483 Batch: shard.Batch, 484 }, 485 ) 486 if err != nil { 487 return err 488 } 489 } 490 491 return nil 492 } 493 494 func (t *Table) GetHideKeys(ctx context.Context) ([]*engine.Attribute, error) { 495 resps, err := DoTxnRequest[GetHiddenKeysResp]( 496 ctx, 497 t.txnOperator, 498 true, 499 t.engine.anyShard, 500 OpGetHiddenKeys, 501 &GetHiddenKeysReq{ 502 TableID: t.id, 503 }, 504 ) 505 if err != nil { 506 return nil, err 507 } 508 509 resp := resps[0] 510 511 // convert from []engine.Attribute to []*engine.Attribute 512 attrs := make([]*engine.Attribute, 0, len(resp.Attrs)) 513 for i := 0; i < len(resp.Attrs); i++ { 514 attrs = append(attrs, &resp.Attrs[i]) 515 } 516 517 return attrs, nil 518 } 519 520 func (t *Table) GetTableID(ctx context.Context) uint64 { 521 return uint64(t.id) 522 } 523 524 // GetTableName implements the engine.Relation interface. 525 func (t *Table) GetTableName() string { 526 return t.tableName 527 } 528 529 func (t *Table) GetDBID(ctx context.Context) uint64 { 530 return 0 531 } 532 533 func (t *Table) MaxAndMinValues(ctx context.Context) ([][2]any, []uint8, error) { 534 return nil, nil, nil 535 } 536 537 func (t *Table) GetColumMetadataScanInfo(ctx context.Context, name string) ([]*plan.MetadataScanInfo, error) { 538 return nil, nil 539 } 540 541 func (t *Table) PrimaryKeysMayBeModified(ctx context.Context, from types.TS, to types.TS, keyVector *vector.Vector) (bool, error) { 542 return true, nil 543 } 544 545 func (t *Table) ApproxObjectsNum(ctx context.Context) int { 546 return 0 547 } 548 549 func (t *Table) MergeObjects(ctx context.Context, objstats []objectio.ObjectStats, policyName string, targetObjSize uint32) (*api.MergeCommitEntry, error) { 550 return nil, nil 551 }