github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/deepcopy.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 plan 16 17 import ( 18 "bytes" 19 20 "github.com/matrixorigin/matrixone/pkg/pb/plan" 21 "golang.org/x/exp/constraints" 22 ) 23 24 func DeepCopyExprList(list []*Expr) []*Expr { 25 if list == nil { 26 return nil 27 } 28 newList := make([]*Expr, len(list)) 29 for idx, expr := range list { 30 newList[idx] = DeepCopyExpr(expr) 31 } 32 return newList 33 } 34 35 func DeepCopyOrderBy(orderBy *plan.OrderBySpec) *plan.OrderBySpec { 36 if orderBy == nil { 37 return nil 38 } 39 return &plan.OrderBySpec{ 40 Expr: DeepCopyExpr(orderBy.Expr), 41 Collation: orderBy.Collation, 42 Flag: orderBy.Flag, 43 } 44 } 45 46 func DeepCopyObjectRef(ref *plan.ObjectRef) *plan.ObjectRef { 47 if ref == nil { 48 return nil 49 } 50 return &plan.ObjectRef{ 51 Server: ref.Server, 52 Db: ref.Db, 53 Schema: ref.Schema, 54 Obj: ref.Obj, 55 ServerName: ref.ServerName, 56 DbName: ref.DbName, 57 SchemaName: ref.SchemaName, 58 ObjName: ref.ObjName, 59 PubInfo: ref.PubInfo, 60 } 61 } 62 63 func DeepCopyOnDupliateKeyCtx(ctx *plan.OnDuplicateKeyCtx) *plan.OnDuplicateKeyCtx { 64 if ctx == nil { 65 return nil 66 } 67 newCtx := &plan.OnDuplicateKeyCtx{ 68 OnDuplicateIdx: make([]int32, len(ctx.OnDuplicateIdx)), 69 } 70 71 copy(newCtx.OnDuplicateIdx, ctx.OnDuplicateIdx) 72 73 if ctx.OnDuplicateExpr != nil { 74 newCtx.OnDuplicateExpr = make(map[string]*Expr) 75 for k, v := range ctx.OnDuplicateExpr { 76 newCtx.OnDuplicateExpr[k] = DeepCopyExpr(v) 77 } 78 } 79 80 return newCtx 81 } 82 83 func DeepCopyInsertCtx(ctx *plan.InsertCtx) *plan.InsertCtx { 84 if ctx == nil { 85 return nil 86 } 87 newCtx := &plan.InsertCtx{ 88 Ref: DeepCopyObjectRef(ctx.Ref), 89 AddAffectedRows: ctx.AddAffectedRows, 90 IsClusterTable: ctx.IsClusterTable, 91 TableDef: DeepCopyTableDef(ctx.TableDef, true), 92 PartitionTableIds: make([]uint64, len(ctx.PartitionTableIds)), 93 PartitionTableNames: make([]string, len(ctx.PartitionTableNames)), 94 PartitionIdx: ctx.PartitionIdx, 95 } 96 copy(newCtx.PartitionTableIds, ctx.PartitionTableIds) 97 copy(newCtx.PartitionTableNames, ctx.PartitionTableNames) 98 return newCtx 99 } 100 101 func DeepCopyDeleteCtx(ctx *plan.DeleteCtx) *plan.DeleteCtx { 102 if ctx == nil { 103 return nil 104 } 105 newCtx := &plan.DeleteCtx{ 106 CanTruncate: ctx.CanTruncate, 107 AddAffectedRows: ctx.AddAffectedRows, 108 RowIdIdx: ctx.RowIdIdx, 109 Ref: DeepCopyObjectRef(ctx.Ref), 110 IsClusterTable: ctx.IsClusterTable, 111 TableDef: DeepCopyTableDef(ctx.TableDef, true), 112 PartitionTableIds: make([]uint64, len(ctx.PartitionTableIds)), 113 PartitionTableNames: make([]string, len(ctx.PartitionTableNames)), 114 PartitionIdx: ctx.PartitionIdx, 115 PrimaryKeyIdx: ctx.PrimaryKeyIdx, 116 } 117 copy(newCtx.PartitionTableIds, ctx.PartitionTableIds) 118 copy(newCtx.PartitionTableNames, ctx.PartitionTableNames) 119 return newCtx 120 } 121 122 func DeepCopyPreInsertCtx(ctx *plan.PreInsertCtx) *plan.PreInsertCtx { 123 if ctx == nil { 124 return nil 125 } 126 newCtx := &plan.PreInsertCtx{ 127 Ref: DeepCopyObjectRef(ctx.Ref), 128 TableDef: DeepCopyTableDef(ctx.TableDef, true), 129 HasAutoCol: ctx.HasAutoCol, 130 IsUpdate: ctx.IsUpdate, 131 } 132 133 return newCtx 134 } 135 136 func DeepCopyPreInsertUkCtx(ctx *plan.PreInsertUkCtx) *plan.PreInsertUkCtx { 137 if ctx == nil { 138 return nil 139 } 140 newCtx := &plan.PreInsertUkCtx{ 141 Columns: make([]int32, len(ctx.Columns)), 142 PkColumn: ctx.PkColumn, 143 PkType: ctx.PkType, 144 UkType: ctx.UkType, 145 } 146 copy(newCtx.Columns, ctx.Columns) 147 148 return newCtx 149 } 150 151 func DeepCopyPreDeleteCtx(ctx *plan.PreDeleteCtx) *plan.PreDeleteCtx { 152 if ctx == nil { 153 return nil 154 } 155 newCtx := &plan.PreDeleteCtx{ 156 Idx: make([]int32, len(ctx.Idx)), 157 } 158 copy(newCtx.Idx, ctx.Idx) 159 160 return newCtx 161 } 162 163 func DeepCopyLockTarget(target *plan.LockTarget) *plan.LockTarget { 164 if target == nil { 165 return nil 166 } 167 return &plan.LockTarget{ 168 TableId: target.TableId, 169 PrimaryColIdxInBat: target.PrimaryColIdxInBat, 170 PrimaryColTyp: target.PrimaryColTyp, 171 RefreshTsIdxInBat: target.RefreshTsIdxInBat, 172 FilterColIdxInBat: target.FilterColIdxInBat, 173 LockTable: target.LockTable, 174 Block: target.Block, 175 } 176 } 177 178 func DeepCopyNode(node *plan.Node) *plan.Node { 179 newNode := &Node{ 180 NodeType: node.NodeType, 181 NodeId: node.NodeId, 182 ExtraOptions: node.ExtraOptions, 183 Children: make([]int32, len(node.Children)), 184 JoinType: node.JoinType, 185 BuildOnLeft: node.BuildOnLeft, 186 BindingTags: make([]int32, len(node.BindingTags)), 187 Limit: DeepCopyExpr(node.Limit), 188 Offset: DeepCopyExpr(node.Offset), 189 ProjectList: make([]*plan.Expr, len(node.ProjectList)), 190 OnList: make([]*plan.Expr, len(node.OnList)), 191 FilterList: make([]*plan.Expr, len(node.FilterList)), 192 BlockFilterList: make([]*plan.Expr, len(node.BlockFilterList)), 193 GroupBy: make([]*plan.Expr, len(node.GroupBy)), 194 GroupingSet: make([]*plan.Expr, len(node.GroupingSet)), 195 AggList: make([]*plan.Expr, len(node.AggList)), 196 OrderBy: make([]*plan.OrderBySpec, len(node.OrderBy)), 197 DeleteCtx: DeepCopyDeleteCtx(node.DeleteCtx), 198 TblFuncExprList: make([]*plan.Expr, len(node.TblFuncExprList)), 199 ClusterTable: DeepCopyClusterTable(node.GetClusterTable()), 200 InsertCtx: DeepCopyInsertCtx(node.InsertCtx), 201 NotCacheable: node.NotCacheable, 202 SourceStep: node.SourceStep, 203 PreInsertCtx: DeepCopyPreInsertCtx(node.PreInsertCtx), 204 PreInsertUkCtx: DeepCopyPreInsertUkCtx(node.PreInsertUkCtx), 205 PreDeleteCtx: DeepCopyPreDeleteCtx(node.PreDeleteCtx), 206 OnDuplicateKey: DeepCopyOnDupliateKeyCtx(node.OnDuplicateKey), 207 LockTargets: make([]*plan.LockTarget, len(node.LockTargets)), 208 AnalyzeInfo: DeepCopyAnalyzeInfo(node.AnalyzeInfo), 209 IsEnd: node.IsEnd, 210 ExternScan: node.ExternScan, 211 PartitionPrune: DeepCopyPartitionPrune(node.PartitionPrune), 212 SampleFunc: DeepCopySampleFuncSpec(node.SampleFunc), 213 OnUpdateExprs: make([]*plan.Expr, len(node.OnUpdateExprs)), 214 } 215 newNode.Uuid = append(newNode.Uuid, node.Uuid...) 216 217 copy(newNode.Children, node.Children) 218 copy(newNode.BindingTags, node.BindingTags) 219 220 for idx, target := range node.LockTargets { 221 newNode.LockTargets[idx] = DeepCopyLockTarget(target) 222 } 223 224 for idx, expr := range node.ProjectList { 225 newNode.ProjectList[idx] = DeepCopyExpr(expr) 226 } 227 228 for idx, expr := range node.OnList { 229 newNode.OnList[idx] = DeepCopyExpr(expr) 230 } 231 232 for idx, expr := range node.FilterList { 233 newNode.FilterList[idx] = DeepCopyExpr(expr) 234 } 235 236 for idx, expr := range node.BlockFilterList { 237 newNode.BlockFilterList[idx] = DeepCopyExpr(expr) 238 } 239 240 for idx, expr := range node.GroupBy { 241 newNode.GroupBy[idx] = DeepCopyExpr(expr) 242 } 243 244 for idx, expr := range node.GroupingSet { 245 newNode.GroupingSet[idx] = DeepCopyExpr(expr) 246 } 247 248 for idx, expr := range node.AggList { 249 newNode.AggList[idx] = DeepCopyExpr(expr) 250 } 251 252 for idx, orderBy := range node.OrderBy { 253 newNode.OrderBy[idx] = DeepCopyOrderBy(orderBy) 254 } 255 256 for idx, expr := range node.OnUpdateExprs { 257 newNode.OnUpdateExprs[idx] = DeepCopyExpr(expr) 258 } 259 260 newNode.Stats = DeepCopyStats(node.Stats) 261 262 newNode.ObjRef = DeepCopyObjectRef(node.ObjRef) 263 newNode.ParentObjRef = DeepCopyObjectRef(node.ParentObjRef) 264 265 if node.WinSpecList != nil { 266 newNode.WinSpecList = make([]*Expr, len(node.WinSpecList)) 267 for i, w := range node.WinSpecList { 268 newNode.WinSpecList[i] = DeepCopyExpr(w) 269 } 270 } 271 272 if node.TableDef != nil { 273 newNode.TableDef = DeepCopyTableDef(node.TableDef, true) 274 } 275 276 if node.RowsetData != nil { 277 newNode.RowsetData = &plan.RowsetData{ 278 Cols: make([]*plan.ColData, len(node.RowsetData.Cols)), 279 RowCount: node.RowsetData.RowCount, 280 } 281 282 for idx, col := range node.RowsetData.Cols { 283 newNode.RowsetData.Cols[idx] = DeepCopyColData(col) 284 } 285 } 286 for idx, expr := range node.TblFuncExprList { 287 newNode.TblFuncExprList[idx] = DeepCopyExpr(expr) 288 } 289 290 return newNode 291 } 292 293 func DeepCopyDefault(def *plan.Default) *plan.Default { 294 if def == nil { 295 return nil 296 } 297 return &plan.Default{ 298 NullAbility: def.NullAbility, 299 Expr: DeepCopyExpr(def.Expr), 300 OriginString: def.OriginString, 301 } 302 } 303 304 func DeepCopyType(typ *plan.Type) *plan.Type { 305 if typ == nil { 306 return nil 307 } 308 return &plan.Type{ 309 Id: typ.Id, 310 NotNullable: typ.NotNullable, 311 Width: typ.Width, 312 Scale: typ.Scale, 313 AutoIncr: typ.AutoIncr, 314 Enumvalues: typ.Enumvalues, 315 } 316 } 317 318 func DeepCopyColDef(col *plan.ColDef) *plan.ColDef { 319 if col == nil { 320 return nil 321 } 322 return &plan.ColDef{ 323 ColId: col.ColId, 324 Name: col.Name, 325 Alg: col.Alg, 326 Typ: col.Typ, 327 Default: DeepCopyDefault(col.Default), 328 Primary: col.Primary, 329 Pkidx: col.Pkidx, 330 Comment: col.Comment, 331 OnUpdate: DeepCopyOnUpdate(col.OnUpdate), 332 ClusterBy: col.ClusterBy, 333 Hidden: col.Hidden, 334 Seqnum: col.Seqnum, 335 TblName: col.TblName, 336 DbName: col.DbName, 337 } 338 } 339 340 func DeepCopyPrimaryKeyDef(pkeyDef *plan.PrimaryKeyDef) *plan.PrimaryKeyDef { 341 if pkeyDef == nil { 342 return nil 343 } 344 def := &plan.PrimaryKeyDef{ 345 PkeyColName: pkeyDef.PkeyColName, 346 Names: make([]string, len(pkeyDef.Names)), 347 } 348 copy(def.Names, pkeyDef.Names) 349 // Check whether the composite primary key column is included 350 if pkeyDef.CompPkeyCol != nil { 351 def.CompPkeyCol = DeepCopyColDef(pkeyDef.CompPkeyCol) 352 } 353 return def 354 } 355 356 func DeepCopyIndexDef(indexDef *plan.IndexDef) *plan.IndexDef { 357 if indexDef == nil { 358 return nil 359 } 360 newindexDef := &plan.IndexDef{ 361 IdxId: indexDef.IdxId, 362 IndexName: indexDef.IndexName, 363 Unique: indexDef.Unique, 364 TableExist: indexDef.TableExist, 365 IndexTableName: indexDef.IndexTableName, 366 Comment: indexDef.Comment, 367 Visible: indexDef.Visible, 368 IndexAlgo: indexDef.IndexAlgo, 369 IndexAlgoTableType: indexDef.IndexAlgoTableType, 370 IndexAlgoParams: indexDef.IndexAlgoParams, 371 } 372 newindexDef.Option = DeepCopyIndexOption(indexDef.Option) 373 374 newParts := make([]string, len(indexDef.Parts)) 375 copy(newParts, indexDef.Parts) 376 newindexDef.Parts = newParts 377 return newindexDef 378 } 379 380 func DeepCopyIndexOption(indexOption *plan.IndexOption) *plan.IndexOption { 381 if indexOption == nil { 382 return nil 383 } 384 newIndexOption := &plan.IndexOption{ 385 CreateExtraTable: indexOption.CreateExtraTable, 386 } 387 388 return newIndexOption 389 } 390 391 func DeepCopyOnUpdate(old *plan.OnUpdate) *plan.OnUpdate { 392 if old == nil { 393 return nil 394 } 395 return &plan.OnUpdate{ 396 Expr: DeepCopyExpr(old.Expr), 397 OriginString: old.OriginString, 398 } 399 } 400 401 func DeepCopyTableDefList(src []*plan.TableDef) []*plan.TableDef { 402 if src == nil { 403 return nil 404 } 405 ret := make([]*plan.TableDef, len(src)) 406 for i, def := range src { 407 ret[i] = DeepCopyTableDef(def, true) 408 } 409 return ret 410 } 411 412 func DeepCopyPartitionPrune(partitionPrune *plan.PartitionPrune) *plan.PartitionPrune { 413 if partitionPrune == nil { 414 return nil 415 } 416 newPartitionPrune := &plan.PartitionPrune{ 417 IsPruned: partitionPrune.IsPruned, 418 SelectedPartitions: make([]*plan.PartitionItem, len(partitionPrune.SelectedPartitions)), 419 } 420 for i, e := range partitionPrune.SelectedPartitions { 421 newPartitionPrune.SelectedPartitions[i] = &plan.PartitionItem{ 422 PartitionName: e.PartitionName, 423 OrdinalPosition: e.OrdinalPosition, 424 Description: e.Description, 425 Comment: e.Comment, 426 LessThan: DeepCopyExprList(e.LessThan), 427 InValues: DeepCopyExprList(e.InValues), 428 PartitionTableName: e.PartitionTableName, 429 } 430 } 431 return newPartitionPrune 432 } 433 434 func DeepCopySampleFuncSpec(source *plan.SampleFuncSpec) *plan.SampleFuncSpec { 435 if source == nil { 436 return nil 437 } 438 return &plan.SampleFuncSpec{ 439 Rows: source.Rows, 440 Percent: source.Percent, 441 } 442 } 443 444 func DeepCopyTableDef(table *plan.TableDef, withCols bool) *plan.TableDef { 445 if table == nil { 446 return nil 447 } 448 newTable := &plan.TableDef{ 449 TblId: table.TblId, 450 Name: table.Name, 451 Hidden: table.Hidden, 452 TableType: table.TableType, 453 Createsql: table.Createsql, 454 Version: table.Version, 455 Pkey: DeepCopyPrimaryKeyDef(table.Pkey), 456 Indexes: make([]*IndexDef, len(table.Indexes)), 457 Fkeys: make([]*plan.ForeignKeyDef, len(table.Fkeys)), 458 RefChildTbls: make([]uint64, len(table.RefChildTbls)), 459 Checks: make([]*plan.CheckDef, len(table.Checks)), 460 Props: make([]*plan.PropertyDef, len(table.Props)), 461 Defs: make([]*plan.TableDef_DefType, len(table.Defs)), 462 Name2ColIndex: table.Name2ColIndex, 463 IsLocked: table.IsLocked, 464 TableLockType: table.TableLockType, 465 IsTemporary: table.IsTemporary, 466 AutoIncrOffset: table.AutoIncrOffset, 467 DbName: table.DbName, 468 } 469 470 copy(newTable.RefChildTbls, table.RefChildTbls) 471 472 if withCols { 473 newTable.Cols = make([]*plan.ColDef, len(table.Cols)) 474 for idx, col := range table.Cols { 475 newTable.Cols[idx] = DeepCopyColDef(col) 476 } 477 } 478 479 for idx, fkey := range table.Fkeys { 480 newTable.Fkeys[idx] = DeepCopyFkey(fkey) 481 } 482 483 for idx, col := range table.Checks { 484 newTable.Checks[idx] = &plan.CheckDef{ 485 Name: col.Name, 486 Check: DeepCopyExpr(col.Check), 487 } 488 } 489 490 for idx, prop := range table.Props { 491 newTable.Props[idx] = &plan.PropertyDef{ 492 Key: prop.Key, 493 Value: prop.Value, 494 } 495 } 496 497 if table.TblFunc != nil { 498 newTable.TblFunc = &plan.TableFunction{ 499 Name: table.TblFunc.Name, 500 Param: make([]byte, len(table.TblFunc.Param)), 501 } 502 copy(newTable.TblFunc.Param, table.TblFunc.Param) 503 } 504 505 if table.ClusterBy != nil { 506 newTable.ClusterBy = &plan.ClusterByDef{ 507 //Parts: make([]*plan.Expr, len(table.ClusterBy.Parts)), 508 Name: table.ClusterBy.Name, 509 CompCbkeyCol: DeepCopyColDef(table.ClusterBy.CompCbkeyCol), 510 } 511 //for i, part := range table.ClusterBy.Parts { 512 // newTable.ClusterBy.Parts[i] = DeepCopyExpr(part) 513 //} 514 } 515 516 if table.ViewSql != nil { 517 newTable.ViewSql = &plan.ViewDef{ 518 View: table.ViewSql.View, 519 } 520 } 521 522 if table.Partition != nil { 523 newTable.Partition = DeepCopyPartitionByDef(table.Partition) 524 } 525 526 if table.Indexes != nil { 527 for i, indexdef := range table.Indexes { 528 newTable.Indexes[i] = DeepCopyIndexDef(indexdef) 529 } 530 } 531 532 for idx, def := range table.Defs { 533 switch defImpl := def.Def.(type) { 534 case *plan.TableDef_DefType_Properties: 535 propDef := &plan.PropertiesDef{ 536 Properties: make([]*plan.Property, len(defImpl.Properties.Properties)), 537 } 538 for i, p := range defImpl.Properties.Properties { 539 propDef.Properties[i] = &plan.Property{ 540 Key: p.Key, 541 Value: p.Value, 542 } 543 } 544 newTable.Defs[idx] = &plan.TableDef_DefType{ 545 Def: &plan.TableDef_DefType_Properties{ 546 Properties: propDef, 547 }, 548 } 549 } 550 } 551 552 return newTable 553 } 554 555 func DeepCopyColData(col *plan.ColData) *plan.ColData { 556 newCol := &plan.ColData{ 557 Data: make([]*plan.RowsetExpr, len(col.Data)), 558 } 559 for i, e := range col.Data { 560 newCol.Data[i] = &plan.RowsetExpr{ 561 Pos: e.Pos, 562 RowPos: e.RowPos, 563 Expr: DeepCopyExpr(e.Expr), 564 } 565 } 566 567 return newCol 568 } 569 570 func DeepCopyQuery(qry *plan.Query) *plan.Query { 571 newQry := &plan.Query{ 572 StmtType: qry.StmtType, 573 Steps: qry.Steps, 574 Nodes: make([]*plan.Node, len(qry.Nodes)), 575 Params: make([]*plan.Expr, len(qry.Params)), 576 Headings: qry.Headings, 577 } 578 for idx, param := range qry.Params { 579 newQry.Params[idx] = DeepCopyExpr(param) 580 } 581 for idx, node := range qry.Nodes { 582 newQry.Nodes[idx] = DeepCopyNode(node) 583 } 584 return newQry 585 } 586 587 func DeepCopyPlan(pl *Plan) *Plan { 588 switch p := pl.Plan.(type) { 589 case *Plan_Query: 590 return &Plan{ 591 Plan: &plan.Plan_Query{ 592 Query: DeepCopyQuery(p.Query), 593 }, 594 IsPrepare: pl.IsPrepare, 595 TryRunTimes: pl.TryRunTimes, 596 } 597 598 case *plan.Plan_Ddl: 599 return &Plan{ 600 Plan: &plan.Plan_Ddl{ 601 Ddl: DeepCopyDataDefinition(p.Ddl), 602 }, 603 IsPrepare: pl.IsPrepare, 604 TryRunTimes: pl.TryRunTimes, 605 } 606 607 default: 608 // only support query/insert plan now 609 return nil 610 } 611 } 612 613 func DeepCopyDataDefinition(old *plan.DataDefinition) *plan.DataDefinition { 614 newDf := &plan.DataDefinition{ 615 DdlType: old.DdlType, 616 } 617 if old.Query != nil { 618 newDf.Query = DeepCopyQuery(old.Query) 619 } 620 621 switch df := old.Definition.(type) { 622 case *plan.DataDefinition_CreateDatabase: 623 newDf.Definition = &plan.DataDefinition_CreateDatabase{ 624 CreateDatabase: &plan.CreateDatabase{ 625 IfNotExists: df.CreateDatabase.IfNotExists, 626 Database: df.CreateDatabase.Database, 627 }, 628 } 629 630 case *plan.DataDefinition_AlterDatabase: 631 newDf.Definition = &plan.DataDefinition_AlterDatabase{ 632 AlterDatabase: &plan.AlterDatabase{ 633 IfExists: df.AlterDatabase.IfExists, 634 Database: df.AlterDatabase.Database, 635 }, 636 } 637 638 case *plan.DataDefinition_DropDatabase: 639 newDf.Definition = &plan.DataDefinition_DropDatabase{ 640 DropDatabase: &plan.DropDatabase{ 641 IfExists: df.DropDatabase.IfExists, 642 Database: df.DropDatabase.Database, 643 DatabaseId: df.DropDatabase.DatabaseId, 644 }, 645 } 646 647 case *plan.DataDefinition_CreateTable: 648 CreateTable := &plan.CreateTable{ 649 Replace: df.CreateTable.Replace, 650 IfNotExists: df.CreateTable.IfNotExists, 651 Temporary: df.CreateTable.Temporary, 652 Database: df.CreateTable.Database, 653 TableDef: DeepCopyTableDef(df.CreateTable.TableDef, true), 654 IndexTables: DeepCopyTableDefList(df.CreateTable.GetIndexTables()), 655 FkDbs: make([]string, len(df.CreateTable.FkDbs)), 656 FkTables: make([]string, len(df.CreateTable.FkTables)), 657 FkCols: make([]*plan.FkColName, len(df.CreateTable.FkCols)), 658 PartitionTables: DeepCopyTableDefList(df.CreateTable.GetPartitionTables()), 659 } 660 copy(CreateTable.FkDbs, df.CreateTable.FkDbs) 661 copy(CreateTable.FkTables, df.CreateTable.FkTables) 662 for i, val := range df.CreateTable.FkCols { 663 cols := &plan.FkColName{Cols: make([]string, len(val.Cols))} 664 copy(cols.Cols, val.Cols) 665 CreateTable.FkCols[i] = cols 666 } 667 newDf.Definition = &plan.DataDefinition_CreateTable{ 668 CreateTable: CreateTable, 669 } 670 671 case *plan.DataDefinition_AlterTable: 672 AlterTable := &plan.AlterTable{ 673 Database: df.AlterTable.Database, 674 TableDef: DeepCopyTableDef(df.AlterTable.TableDef, true), 675 CopyTableDef: DeepCopyTableDef(df.AlterTable.CopyTableDef, true), 676 IsClusterTable: df.AlterTable.IsClusterTable, 677 AlgorithmType: df.AlterTable.AlgorithmType, 678 CreateTableSql: df.AlterTable.CreateTableSql, 679 InsertDataSql: df.AlterTable.InsertDataSql, 680 Actions: make([]*plan.AlterTable_Action, len(df.AlterTable.Actions)), 681 } 682 for i, action := range df.AlterTable.Actions { 683 switch act := action.Action.(type) { 684 case *plan.AlterTable_Action_Drop: 685 AlterTable.Actions[i] = &plan.AlterTable_Action{ 686 Action: &plan.AlterTable_Action_Drop{ 687 Drop: &plan.AlterTableDrop{ 688 Typ: act.Drop.Typ, 689 Name: act.Drop.Name, 690 }, 691 }, 692 } 693 case *plan.AlterTable_Action_AddFk: 694 AddFk := &plan.AlterTable_Action_AddFk{ 695 AddFk: &plan.AlterTableAddFk{ 696 DbName: act.AddFk.DbName, 697 TableName: act.AddFk.TableName, 698 Cols: make([]string, len(act.AddFk.Cols)), 699 Fkey: DeepCopyFkey(act.AddFk.Fkey), 700 }, 701 } 702 copy(AddFk.AddFk.Cols, act.AddFk.Cols) 703 AlterTable.Actions[i] = &plan.AlterTable_Action{ 704 Action: AddFk, 705 } 706 } 707 } 708 709 newDf.Definition = &plan.DataDefinition_AlterTable{ 710 AlterTable: AlterTable, 711 } 712 713 case *plan.DataDefinition_DropTable: 714 newDf.Definition = &plan.DataDefinition_DropTable{ 715 DropTable: &plan.DropTable{ 716 IfExists: df.DropTable.IfExists, 717 Database: df.DropTable.Database, 718 Table: df.DropTable.Table, 719 IndexTableNames: DeepCopyStringList(df.DropTable.GetIndexTableNames()), 720 ClusterTable: DeepCopyClusterTable(df.DropTable.GetClusterTable()), 721 TableId: df.DropTable.GetTableId(), 722 ForeignTbl: DeepCopyNumberList(df.DropTable.GetForeignTbl()), 723 PartitionTableNames: DeepCopyStringList(df.DropTable.GetPartitionTableNames()), 724 IsView: df.DropTable.IsView, 725 TableDef: DeepCopyTableDef(df.DropTable.GetTableDef(), true), 726 }, 727 } 728 729 case *plan.DataDefinition_CreateIndex: 730 newDf.Definition = &plan.DataDefinition_CreateIndex{ 731 CreateIndex: &plan.CreateIndex{ 732 Database: df.CreateIndex.Database, 733 Table: df.CreateIndex.Table, 734 Index: &plan.CreateTable{ 735 IfNotExists: df.CreateIndex.Index.IfNotExists, 736 Temporary: df.CreateIndex.Index.Temporary, 737 Database: df.CreateIndex.Index.Database, 738 TableDef: DeepCopyTableDef(df.CreateIndex.Index.TableDef, true), 739 }, 740 OriginTablePrimaryKey: df.CreateIndex.OriginTablePrimaryKey, 741 }, 742 } 743 744 case *plan.DataDefinition_AlterIndex: 745 newDf.Definition = &plan.DataDefinition_AlterIndex{ 746 AlterIndex: &plan.AlterIndex{ 747 Index: df.AlterIndex.Index, 748 }, 749 } 750 751 case *plan.DataDefinition_DropIndex: 752 newDf.Definition = &plan.DataDefinition_DropIndex{ 753 DropIndex: &plan.DropIndex{ 754 Database: df.DropIndex.Database, 755 Table: df.DropIndex.Table, 756 IndexName: df.DropIndex.IndexName, 757 IndexTableName: df.DropIndex.IndexTableName, 758 }, 759 } 760 761 case *plan.DataDefinition_TruncateTable: 762 truncateTable := &plan.TruncateTable{ 763 Database: df.TruncateTable.Database, 764 Table: df.TruncateTable.Table, 765 ClusterTable: DeepCopyClusterTable(df.TruncateTable.GetClusterTable()), 766 IndexTableNames: make([]string, len(df.TruncateTable.IndexTableNames)), 767 } 768 copy(truncateTable.IndexTableNames, df.TruncateTable.IndexTableNames) 769 newDf.Definition = &plan.DataDefinition_TruncateTable{ 770 TruncateTable: truncateTable, 771 } 772 773 case *plan.DataDefinition_ShowVariables: 774 showVariables := &plan.ShowVariables{ 775 Global: df.ShowVariables.Global, 776 Where: make([]*plan.Expr, len(df.ShowVariables.Where)), 777 } 778 for i, e := range df.ShowVariables.Where { 779 showVariables.Where[i] = DeepCopyExpr(e) 780 } 781 782 newDf.Definition = &plan.DataDefinition_ShowVariables{ 783 ShowVariables: showVariables, 784 } 785 786 case *plan.DataDefinition_LockTables: 787 newDf.Definition = &plan.DataDefinition_LockTables{ 788 LockTables: &plan.LockTables{ 789 TableLocks: df.LockTables.TableLocks, 790 }, 791 } 792 793 case *plan.DataDefinition_UnlockTables: 794 newDf.Definition = &plan.DataDefinition_UnlockTables{ 795 UnlockTables: &plan.UnLockTables{}, 796 } 797 798 case *plan.DataDefinition_AlterSequence: 799 newDf.Definition = &plan.DataDefinition_AlterSequence{ 800 AlterSequence: &plan.AlterSequence{ 801 IfExists: df.AlterSequence.IfExists, 802 Database: df.AlterSequence.Database, 803 TableDef: df.AlterSequence.TableDef, 804 }, 805 } 806 807 } 808 809 return newDf 810 } 811 812 func DeepCopyFkey(fkey *ForeignKeyDef) *ForeignKeyDef { 813 def := &ForeignKeyDef{ 814 Name: fkey.Name, 815 Cols: make([]uint64, len(fkey.Cols)), 816 ForeignTbl: fkey.ForeignTbl, 817 ForeignCols: make([]uint64, len(fkey.ForeignCols)), 818 OnDelete: fkey.OnDelete, 819 OnUpdate: fkey.OnUpdate, 820 } 821 copy(def.Cols, fkey.Cols) 822 copy(def.ForeignCols, fkey.ForeignCols) 823 return def 824 } 825 826 func DeepCopyExpr(expr *Expr) *Expr { 827 if expr == nil { 828 return nil 829 } 830 newExpr := &Expr{ 831 Typ: expr.Typ, 832 Ndv: expr.Ndv, 833 Selectivity: expr.Selectivity, 834 } 835 836 switch item := expr.Expr.(type) { 837 case *plan.Expr_Lit: 838 pc := &plan.Literal{ 839 Isnull: item.Lit.GetIsnull(), 840 Src: item.Lit.Src, 841 } 842 843 switch c := item.Lit.Value.(type) { 844 case *plan.Literal_I8Val: 845 pc.Value = &plan.Literal_I8Val{I8Val: c.I8Val} 846 case *plan.Literal_I16Val: 847 pc.Value = &plan.Literal_I16Val{I16Val: c.I16Val} 848 case *plan.Literal_I32Val: 849 pc.Value = &plan.Literal_I32Val{I32Val: c.I32Val} 850 case *plan.Literal_I64Val: 851 pc.Value = &plan.Literal_I64Val{I64Val: c.I64Val} 852 case *plan.Literal_Dval: 853 pc.Value = &plan.Literal_Dval{Dval: c.Dval} 854 case *plan.Literal_Sval: 855 pc.Value = &plan.Literal_Sval{Sval: c.Sval} 856 case *plan.Literal_Bval: 857 pc.Value = &plan.Literal_Bval{Bval: c.Bval} 858 case *plan.Literal_U8Val: 859 pc.Value = &plan.Literal_U8Val{U8Val: c.U8Val} 860 case *plan.Literal_U16Val: 861 pc.Value = &plan.Literal_U16Val{U16Val: c.U16Val} 862 case *plan.Literal_U32Val: 863 pc.Value = &plan.Literal_U32Val{U32Val: c.U32Val} 864 case *plan.Literal_U64Val: 865 pc.Value = &plan.Literal_U64Val{U64Val: c.U64Val} 866 case *plan.Literal_Fval: 867 pc.Value = &plan.Literal_Fval{Fval: c.Fval} 868 case *plan.Literal_Dateval: 869 pc.Value = &plan.Literal_Dateval{Dateval: c.Dateval} 870 case *plan.Literal_Timeval: 871 pc.Value = &plan.Literal_Timeval{Timeval: c.Timeval} 872 case *plan.Literal_Datetimeval: 873 pc.Value = &plan.Literal_Datetimeval{Datetimeval: c.Datetimeval} 874 case *plan.Literal_Decimal64Val: 875 pc.Value = &plan.Literal_Decimal64Val{Decimal64Val: &plan.Decimal64{A: c.Decimal64Val.A}} 876 case *plan.Literal_Decimal128Val: 877 pc.Value = &plan.Literal_Decimal128Val{Decimal128Val: &plan.Decimal128{A: c.Decimal128Val.A, B: c.Decimal128Val.B}} 878 case *plan.Literal_Timestampval: 879 pc.Value = &plan.Literal_Timestampval{Timestampval: c.Timestampval} 880 case *plan.Literal_Jsonval: 881 pc.Value = &plan.Literal_Jsonval{Jsonval: c.Jsonval} 882 case *plan.Literal_Defaultval: 883 pc.Value = &plan.Literal_Defaultval{Defaultval: c.Defaultval} 884 case *plan.Literal_UpdateVal: 885 pc.Value = &plan.Literal_UpdateVal{UpdateVal: c.UpdateVal} 886 case *plan.Literal_EnumVal: 887 pc.Value = &plan.Literal_EnumVal{EnumVal: c.EnumVal} 888 } 889 890 newExpr.Expr = &plan.Expr_Lit{ 891 Lit: pc, 892 } 893 894 case *plan.Expr_P: 895 newExpr.Expr = &plan.Expr_P{ 896 P: &plan.ParamRef{ 897 Pos: item.P.GetPos(), 898 }, 899 } 900 901 case *plan.Expr_V: 902 newExpr.Expr = &plan.Expr_V{ 903 V: &plan.VarRef{ 904 Name: item.V.GetName(), 905 Global: item.V.GetGlobal(), 906 System: item.V.GetSystem(), 907 }, 908 } 909 910 case *plan.Expr_Col: 911 newExpr.Expr = &plan.Expr_Col{ 912 Col: &plan.ColRef{ 913 RelPos: item.Col.GetRelPos(), 914 ColPos: item.Col.GetColPos(), 915 Name: item.Col.GetName(), 916 }, 917 } 918 919 case *plan.Expr_F: 920 newArgs := make([]*Expr, len(item.F.Args)) 921 for idx, arg := range item.F.Args { 922 newArgs[idx] = DeepCopyExpr(arg) 923 } 924 newExpr.Expr = &plan.Expr_F{ 925 F: &plan.Function{ 926 Func: DeepCopyObjectRef(item.F.Func), 927 Args: newArgs, 928 }, 929 } 930 931 case *plan.Expr_W: 932 ps := make([]*Expr, len(item.W.PartitionBy)) 933 for i, p := range item.W.PartitionBy { 934 ps[i] = DeepCopyExpr(p) 935 } 936 os := make([]*OrderBySpec, len(item.W.OrderBy)) 937 for i, o := range item.W.OrderBy { 938 os[i] = DeepCopyOrderBy(o) 939 } 940 f := item.W.Frame 941 newExpr.Expr = &plan.Expr_W{ 942 W: &plan.WindowSpec{ 943 WindowFunc: DeepCopyExpr(item.W.WindowFunc), 944 PartitionBy: ps, 945 OrderBy: os, 946 Name: item.W.Name, 947 Frame: &plan.FrameClause{ 948 Type: f.Type, 949 Start: &plan.FrameBound{ 950 Type: f.Start.Type, 951 UnBounded: f.Start.UnBounded, 952 Val: DeepCopyExpr(f.Start.Val), 953 }, 954 End: &plan.FrameBound{ 955 Type: f.End.Type, 956 UnBounded: f.End.UnBounded, 957 Val: DeepCopyExpr(f.End.Val), 958 }, 959 }, 960 }, 961 } 962 963 case *plan.Expr_Sub: 964 newExpr.Expr = &plan.Expr_Sub{ 965 Sub: &plan.SubqueryRef{ 966 NodeId: item.Sub.GetNodeId(), 967 Typ: item.Sub.Typ, 968 Op: item.Sub.Op, 969 RowSize: item.Sub.RowSize, 970 Child: DeepCopyExpr(item.Sub.Child), 971 }, 972 } 973 974 case *plan.Expr_Corr: 975 newExpr.Expr = &plan.Expr_Corr{ 976 Corr: &plan.CorrColRef{ 977 ColPos: item.Corr.GetColPos(), 978 RelPos: item.Corr.GetRelPos(), 979 Depth: item.Corr.GetDepth(), 980 }, 981 } 982 983 case *plan.Expr_T: 984 newExpr.Expr = &plan.Expr_T{ 985 T: &plan.TargetType{}, 986 } 987 988 case *plan.Expr_Max: 989 newExpr.Expr = &plan.Expr_Max{ 990 Max: &plan.MaxValue{ 991 Value: item.Max.GetValue(), 992 }, 993 } 994 995 case *plan.Expr_List: 996 e := &plan.ExprList{ 997 List: make([]*plan.Expr, len(item.List.List)), 998 } 999 for i, ie := range item.List.List { 1000 e.List[i] = DeepCopyExpr(ie) 1001 } 1002 newExpr.Expr = &plan.Expr_List{ 1003 List: e, 1004 } 1005 1006 case *plan.Expr_Vec: 1007 newExpr.Expr = &plan.Expr_Vec{ 1008 Vec: &plan.LiteralVec{ 1009 Len: item.Vec.Len, 1010 Data: bytes.Clone(item.Vec.Data), 1011 }, 1012 } 1013 } 1014 1015 return newExpr 1016 } 1017 1018 func DeepCopyClusterTable(cluster *plan.ClusterTable) *plan.ClusterTable { 1019 if cluster == nil { 1020 return nil 1021 } 1022 1023 accountIds := make([]uint32, len(cluster.GetAccountIDs())) 1024 copy(accountIds, cluster.GetAccountIDs()) 1025 newClusterTable := &plan.ClusterTable{ 1026 IsClusterTable: cluster.GetIsClusterTable(), 1027 AccountIDs: accountIds, 1028 ColumnIndexOfAccountId: cluster.GetColumnIndexOfAccountId(), 1029 } 1030 return newClusterTable 1031 } 1032 1033 func DeepCopySliceInt64(s []int64) []int64 { 1034 if s == nil { 1035 return nil 1036 } 1037 result := make([]int64, 0, len(s)) 1038 result = append(result, s...) 1039 return result 1040 } 1041 1042 func DeepCopyAnalyzeInfo(analyzeinfo *plan.AnalyzeInfo) *plan.AnalyzeInfo { 1043 if analyzeinfo == nil { 1044 return nil 1045 } 1046 1047 return &plan.AnalyzeInfo{ 1048 InputRows: analyzeinfo.GetInputRows(), 1049 OutputRows: analyzeinfo.GetOutputRows(), 1050 InputSize: analyzeinfo.GetInputSize(), 1051 OutputSize: analyzeinfo.GetOutputSize(), 1052 TimeConsumed: analyzeinfo.GetTimeConsumed(), 1053 TimeConsumedArrayMajor: DeepCopySliceInt64(analyzeinfo.GetTimeConsumedArrayMajor()), 1054 TimeConsumedArrayMinor: DeepCopySliceInt64(analyzeinfo.GetTimeConsumedArrayMinor()), 1055 MemorySize: analyzeinfo.GetMemorySize(), 1056 WaitTimeConsumed: analyzeinfo.GetWaitTimeConsumed(), 1057 DiskIO: analyzeinfo.GetDiskIO(), 1058 S3IOByte: analyzeinfo.GetS3IOByte(), 1059 S3IOInputCount: analyzeinfo.GetS3IOInputCount(), 1060 S3IOOutputCount: analyzeinfo.GetS3IOOutputCount(), 1061 NetworkIO: analyzeinfo.GetNetworkIO(), 1062 ScanTime: analyzeinfo.GetScanTime(), 1063 InsertTime: analyzeinfo.GetInsertTime(), 1064 } 1065 } 1066 1067 func DeepCopyStringList(src []string) []string { 1068 if src == nil { 1069 return nil 1070 } 1071 ret := make([]string, len(src)) 1072 copy(ret, src) 1073 return ret 1074 } 1075 1076 func DeepCopyNumberList[T constraints.Integer](src []T) []T { 1077 if src == nil { 1078 return nil 1079 } 1080 ret := make([]T, len(src)) 1081 copy(ret, src) 1082 return ret 1083 } 1084 1085 func DeepCopyPartitionByDef(partiiondef *PartitionByDef) *PartitionByDef { 1086 partitionDef := &plan.PartitionByDef{ 1087 Type: partiiondef.GetType(), 1088 PartitionExpression: DeepCopyExpr(partiiondef.GetPartitionExpression()), 1089 PartitionNum: partiiondef.GetPartitionNum(), 1090 Partitions: make([]*plan.PartitionItem, len(partiiondef.Partitions)), 1091 Algorithm: partiiondef.GetAlgorithm(), 1092 IsSubPartition: partiiondef.GetIsSubPartition(), 1093 PartitionMsg: partiiondef.GetPartitionMsg(), 1094 PartitionTableNames: DeepCopyStringList(partiiondef.GetPartitionTableNames()), 1095 } 1096 if partiiondef.PartitionExpr != nil { 1097 partitionDef.PartitionExpr = &plan.PartitionExpr{ 1098 Expr: DeepCopyExpr(partiiondef.PartitionExpr.Expr), 1099 ExprStr: partiiondef.PartitionExpr.GetExprStr(), 1100 } 1101 } 1102 1103 if partiiondef.PartitionColumns != nil { 1104 partitionDef.PartitionColumns = &plan.PartitionColumns{ 1105 Columns: DeepCopyExprList(partiiondef.PartitionColumns.Columns), 1106 PartitionColumns: DeepCopyStringList(partiiondef.PartitionColumns.PartitionColumns), 1107 } 1108 } 1109 1110 for i, e := range partiiondef.Partitions { 1111 partitionDef.Partitions[i] = &plan.PartitionItem{ 1112 PartitionName: e.PartitionName, 1113 OrdinalPosition: e.OrdinalPosition, 1114 Description: e.Description, 1115 Comment: e.Comment, 1116 LessThan: DeepCopyExprList(e.LessThan), 1117 InValues: DeepCopyExprList(e.InValues), 1118 PartitionTableName: e.PartitionTableName, 1119 } 1120 } 1121 return partitionDef 1122 }