github.com/matrixorigin/matrixone@v0.7.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 "github.com/matrixorigin/matrixone/pkg/pb/plan" 18 19 func DeepCopyExprList(list []*Expr) []*Expr { 20 newList := make([]*Expr, len(list)) 21 for idx, expr := range list { 22 newList[idx] = DeepCopyExpr(expr) 23 } 24 return newList 25 } 26 27 func DeepCopyOrderBy(orderBy *plan.OrderBySpec) *plan.OrderBySpec { 28 if orderBy == nil { 29 return nil 30 } 31 return &plan.OrderBySpec{ 32 Expr: DeepCopyExpr(orderBy.Expr), 33 Collation: orderBy.Collation, 34 Flag: orderBy.Flag, 35 } 36 } 37 38 func DeepCopyObjectRef(ref *plan.ObjectRef) *plan.ObjectRef { 39 if ref == nil { 40 return nil 41 } 42 return &plan.ObjectRef{ 43 Server: ref.Server, 44 Db: ref.Db, 45 Schema: ref.Schema, 46 Obj: ref.Obj, 47 ServerName: ref.ServerName, 48 DbName: ref.DbName, 49 SchemaName: ref.SchemaName, 50 ObjName: ref.ObjName, 51 } 52 } 53 54 func DeepCopyInsertCtx(ctx *plan.InsertCtx) *plan.InsertCtx { 55 if ctx == nil { 56 return nil 57 } 58 newCtx := &plan.InsertCtx{ 59 Ref: DeepCopyObjectRef(ctx.Ref), 60 Idx: make([]int32, len(ctx.Idx)), 61 TableDef: DeepCopyTableDef(ctx.TableDef), 62 63 ClusterTable: DeepCopyClusterTable(ctx.ClusterTable), 64 } 65 66 copy(newCtx.Idx, ctx.Idx) 67 68 if ctx.ParentIdx != nil { 69 newCtx.ParentIdx = make(map[string]int32) 70 for k, v := range ctx.ParentIdx { 71 newCtx.ParentIdx[k] = v 72 } 73 } 74 75 return newCtx 76 } 77 78 func DeepCopyDeleteCtx(ctx *plan.DeleteCtx) *plan.DeleteCtx { 79 if ctx == nil { 80 return nil 81 } 82 newCtx := &plan.DeleteCtx{ 83 CanTruncate: ctx.CanTruncate, 84 OnRestrictIdx: make([]int32, len(ctx.OnRestrictIdx)), 85 IdxIdx: make([]int32, len(ctx.IdxIdx)), 86 OnCascadeIdx: make([]int32, len(ctx.OnCascadeIdx)), 87 88 Ref: make([]*plan.ObjectRef, len(ctx.Ref)), 89 IdxRef: make([]*plan.ObjectRef, len(ctx.IdxRef)), 90 91 OnRestrictRef: make([]*plan.ObjectRef, len(ctx.OnRestrictRef)), 92 93 OnCascadeRef: make([]*plan.ObjectRef, len(ctx.OnCascadeRef)), 94 95 OnSetRef: make([]*plan.ObjectRef, len(ctx.OnSetRef)), 96 OnSetDef: make([]*plan.TableDef, len(ctx.OnSetDef)), 97 OnSetIdx: make([]*plan.IdList, len(ctx.OnSetIdx)), 98 OnSetUpdateCol: make([]*plan.ColPosMap, len(ctx.OnSetUpdateCol)), 99 } 100 101 copy(newCtx.OnRestrictIdx, ctx.OnRestrictIdx) 102 copy(newCtx.IdxIdx, ctx.IdxIdx) 103 copy(newCtx.OnCascadeIdx, ctx.OnCascadeIdx) 104 105 for i, ref := range ctx.Ref { 106 newCtx.Ref[i] = DeepCopyObjectRef(ref) 107 } 108 for i, ref := range ctx.IdxRef { 109 newCtx.IdxRef[i] = DeepCopyObjectRef(ref) 110 } 111 for i, ref := range ctx.OnRestrictRef { 112 newCtx.OnRestrictRef[i] = DeepCopyObjectRef(ref) 113 } 114 for i, ref := range ctx.OnCascadeRef { 115 newCtx.OnCascadeRef[i] = DeepCopyObjectRef(ref) 116 } 117 for i, ref := range ctx.OnSetRef { 118 newCtx.OnSetRef[i] = DeepCopyObjectRef(ref) 119 } 120 for i, def := range ctx.OnSetDef { 121 newCtx.OnSetDef[i] = DeepCopyTableDef(def) 122 } 123 for i, list := range ctx.OnSetIdx { 124 if list != nil { 125 newCtx.OnSetIdx[i] = &plan.IdList{ 126 List: make([]int64, len(list.List)), 127 } 128 copy(newCtx.OnSetIdx[i].List, list.List) 129 } 130 } 131 for i, m := range ctx.OnSetUpdateCol { 132 newMap := make(map[string]int32) 133 for k, v := range m.Map { 134 newMap[k] = v 135 } 136 newCtx.OnSetUpdateCol[i] = &plan.ColPosMap{Map: newMap} 137 } 138 return newCtx 139 } 140 141 func DeepCopyUpdateCtx(ctx *plan.UpdateCtx) *plan.UpdateCtx { 142 if ctx == nil { 143 return nil 144 } 145 newCtx := &plan.UpdateCtx{ 146 Ref: make([]*plan.ObjectRef, len(ctx.Ref)), 147 Idx: make([]*plan.IdList, len(ctx.Idx)), 148 TableDefs: make([]*plan.TableDef, len(ctx.TableDefs)), 149 UpdateCol: make([]*plan.ColPosMap, len(ctx.UpdateCol)), 150 151 IdxRef: make([]*plan.ObjectRef, len(ctx.IdxRef)), 152 IdxIdx: make([]int32, len(ctx.IdxIdx)), 153 154 OnRestrictRef: make([]*plan.ObjectRef, len(ctx.OnRestrictRef)), 155 OnRestrictIdx: make([]int32, len(ctx.OnRestrictIdx)), 156 157 OnCascadeRef: make([]*plan.ObjectRef, len(ctx.OnCascadeRef)), 158 OnCascadeIdx: make([]*plan.IdList, len(ctx.OnCascadeIdx)), 159 OnCascadeDef: make([]*plan.TableDef, len(ctx.OnCascadeDef)), 160 OnCascadeUpdateCol: make([]*plan.ColPosMap, len(ctx.OnCascadeUpdateCol)), 161 162 OnSetRef: make([]*plan.ObjectRef, len(ctx.OnSetRef)), 163 OnSetIdx: make([]*plan.IdList, len(ctx.OnSetIdx)), 164 OnSetDef: make([]*plan.TableDef, len(ctx.OnSetDef)), 165 OnSetUpdateCol: make([]*plan.ColPosMap, len(ctx.OnSetUpdateCol)), 166 167 ParentIdx: make([]*plan.ColPosMap, len(ctx.ParentIdx)), 168 } 169 170 for i, ref := range ctx.Ref { 171 newCtx.Ref[i] = DeepCopyObjectRef(ref) 172 } 173 for i, def := range ctx.TableDefs { 174 newCtx.TableDefs[i] = DeepCopyTableDef(def) 175 } 176 for i, m := range ctx.UpdateCol { 177 newMap := make(map[string]int32) 178 for k, v := range m.Map { 179 newMap[k] = v 180 } 181 newCtx.UpdateCol[i] = &plan.ColPosMap{Map: newMap} 182 } 183 for i, list := range ctx.Idx { 184 if list != nil { 185 newCtx.Idx[i] = &plan.IdList{ 186 List: make([]int64, len(list.List)), 187 } 188 copy(newCtx.Idx[i].List, list.List) 189 } 190 } 191 192 for i, ref := range ctx.IdxRef { 193 newCtx.IdxRef[i] = DeepCopyObjectRef(ref) 194 } 195 copy(newCtx.IdxIdx, ctx.IdxIdx) 196 197 for i, ref := range ctx.OnRestrictRef { 198 newCtx.OnRestrictRef[i] = DeepCopyObjectRef(ref) 199 } 200 copy(newCtx.OnRestrictIdx, ctx.OnRestrictIdx) 201 202 for i, ref := range ctx.OnSetRef { 203 newCtx.OnSetRef[i] = DeepCopyObjectRef(ref) 204 } 205 for i, def := range ctx.OnSetDef { 206 newCtx.OnSetDef[i] = DeepCopyTableDef(def) 207 } 208 for i, list := range ctx.OnSetIdx { 209 if list != nil { 210 newCtx.OnSetIdx[i] = &plan.IdList{ 211 List: make([]int64, len(list.List)), 212 } 213 copy(newCtx.OnSetIdx[i].List, list.List) 214 } 215 } 216 for i, m := range ctx.OnSetUpdateCol { 217 newMap := make(map[string]int32) 218 for k, v := range m.Map { 219 newMap[k] = v 220 } 221 newCtx.OnSetUpdateCol[i] = &plan.ColPosMap{Map: newMap} 222 } 223 224 for i, ref := range ctx.OnCascadeRef { 225 newCtx.OnCascadeRef[i] = DeepCopyObjectRef(ref) 226 } 227 for i, def := range ctx.OnCascadeDef { 228 newCtx.OnCascadeDef[i] = DeepCopyTableDef(def) 229 } 230 for i, list := range ctx.OnCascadeIdx { 231 if list != nil { 232 newCtx.OnCascadeIdx[i] = &plan.IdList{ 233 List: make([]int64, len(list.List)), 234 } 235 copy(newCtx.OnCascadeIdx[i].List, list.List) 236 } 237 } 238 for i, m := range ctx.OnCascadeUpdateCol { 239 newMap := make(map[string]int32) 240 for k, v := range m.Map { 241 newMap[k] = v 242 } 243 newCtx.OnCascadeUpdateCol[i] = &plan.ColPosMap{Map: newMap} 244 } 245 246 for i, m := range ctx.ParentIdx { 247 newMap := make(map[string]int32) 248 for k, v := range m.Map { 249 newMap[k] = v 250 } 251 newCtx.ParentIdx[i] = &plan.ColPosMap{Map: newMap} 252 } 253 return newCtx 254 } 255 256 func DeepCopyNode(node *plan.Node) *plan.Node { 257 newNode := &Node{ 258 NodeType: node.NodeType, 259 NodeId: node.NodeId, 260 ExtraOptions: node.ExtraOptions, 261 Children: make([]int32, len(node.Children)), 262 JoinType: node.JoinType, 263 BindingTags: make([]int32, len(node.BindingTags)), 264 Limit: DeepCopyExpr(node.Limit), 265 Offset: DeepCopyExpr(node.Offset), 266 ProjectList: make([]*plan.Expr, len(node.ProjectList)), 267 OnList: make([]*plan.Expr, len(node.OnList)), 268 FilterList: make([]*plan.Expr, len(node.FilterList)), 269 GroupBy: make([]*plan.Expr, len(node.GroupBy)), 270 GroupingSet: make([]*plan.Expr, len(node.GroupingSet)), 271 AggList: make([]*plan.Expr, len(node.AggList)), 272 OrderBy: make([]*plan.OrderBySpec, len(node.OrderBy)), 273 DeleteCtx: DeepCopyDeleteCtx(node.DeleteCtx), 274 UpdateCtx: DeepCopyUpdateCtx(node.UpdateCtx), 275 TableDefVec: make([]*plan.TableDef, len(node.TableDefVec)), 276 TblFuncExprList: make([]*plan.Expr, len(node.TblFuncExprList)), 277 ClusterTable: DeepCopyClusterTable(node.GetClusterTable()), 278 InsertCtx: DeepCopyInsertCtx(node.InsertCtx), 279 } 280 281 copy(newNode.Children, node.Children) 282 copy(newNode.BindingTags, node.BindingTags) 283 284 for idx, expr := range node.ProjectList { 285 newNode.ProjectList[idx] = DeepCopyExpr(expr) 286 } 287 288 for idx, expr := range node.OnList { 289 newNode.OnList[idx] = DeepCopyExpr(expr) 290 } 291 292 for idx, expr := range node.FilterList { 293 newNode.FilterList[idx] = DeepCopyExpr(expr) 294 } 295 296 for idx, expr := range node.GroupBy { 297 newNode.GroupBy[idx] = DeepCopyExpr(expr) 298 } 299 300 for idx, expr := range node.GroupingSet { 301 newNode.GroupingSet[idx] = DeepCopyExpr(expr) 302 } 303 304 for idx, expr := range node.AggList { 305 newNode.AggList[idx] = DeepCopyExpr(expr) 306 } 307 308 for idx, orderBy := range node.OrderBy { 309 newNode.OrderBy[idx] = DeepCopyOrderBy(orderBy) 310 } 311 312 for i, tbl := range node.TableDefVec { 313 newNode.TableDefVec[i] = DeepCopyTableDef(tbl) 314 } 315 316 if node.Stats != nil { 317 newNode.Stats = &plan.Stats{ 318 BlockNum: node.Stats.BlockNum, 319 Rowsize: node.Stats.Rowsize, 320 HashmapSize: node.Stats.HashmapSize, 321 Cost: node.Stats.Cost, 322 Outcnt: node.Stats.Outcnt, 323 } 324 } 325 326 newNode.ObjRef = DeepCopyObjectRef(node.ObjRef) 327 328 if node.WinSpec != nil { 329 newNode.WinSpec = &plan.WindowSpec{ 330 PartitionBy: make([]*plan.Expr, len(node.WinSpec.PartitionBy)), 331 OrderBy: make([]*plan.OrderBySpec, len(node.WinSpec.OrderBy)), 332 Lead: node.WinSpec.Lead, 333 Lag: node.WinSpec.Lag, 334 } 335 for idx, pb := range node.WinSpec.PartitionBy { 336 newNode.WinSpec.PartitionBy[idx] = DeepCopyExpr(pb) 337 } 338 for idx, orderBy := range node.WinSpec.OrderBy { 339 newNode.WinSpec.OrderBy[idx] = DeepCopyOrderBy(orderBy) 340 } 341 } 342 343 if node.TableDef != nil { 344 newNode.TableDef = DeepCopyTableDef(node.TableDef) 345 } 346 347 if node.RowsetData != nil { 348 newNode.RowsetData = &plan.RowsetData{ 349 Cols: make([]*plan.ColData, len(node.RowsetData.Cols)), 350 } 351 352 for idx, col := range node.RowsetData.Cols { 353 newNode.RowsetData.Cols[idx] = DeepCopyColData(col) 354 } 355 } 356 for idx, expr := range node.TblFuncExprList { 357 newNode.TblFuncExprList[idx] = DeepCopyExpr(expr) 358 } 359 360 return newNode 361 } 362 363 func DeepCopyDefault(def *plan.Default) *plan.Default { 364 if def == nil { 365 return nil 366 } 367 return &plan.Default{ 368 NullAbility: def.NullAbility, 369 Expr: DeepCopyExpr(def.Expr), 370 OriginString: def.OriginString, 371 } 372 } 373 374 func DeepCopyTyp(typ *plan.Type) *plan.Type { 375 if typ == nil { 376 return nil 377 } 378 return &plan.Type{ 379 Id: typ.Id, 380 NotNullable: typ.NotNullable, 381 Width: typ.Width, 382 Precision: typ.Precision, 383 Size: typ.Size, 384 Scale: typ.Scale, 385 AutoIncr: typ.AutoIncr, 386 } 387 } 388 389 func DeepCopyColDef(col *plan.ColDef) *plan.ColDef { 390 if col == nil { 391 return nil 392 } 393 return &plan.ColDef{ 394 Name: col.Name, 395 Alg: col.Alg, 396 Typ: DeepCopyTyp(col.Typ), 397 Default: DeepCopyDefault(col.Default), 398 Primary: col.Primary, 399 Pkidx: col.Pkidx, 400 Comment: col.Comment, 401 OnUpdate: DeepCopyOnUpdate(col.OnUpdate), 402 ClusterBy: col.ClusterBy, 403 } 404 } 405 406 func DeepCopyPrimaryKeyDef(pkeyDef *plan.PrimaryKeyDef) *plan.PrimaryKeyDef { 407 if pkeyDef == nil { 408 return nil 409 } 410 def := &plan.PrimaryKeyDef{ 411 PkeyColName: pkeyDef.PkeyColName, 412 Names: make([]string, len(pkeyDef.Names)), 413 } 414 copy(def.Names, pkeyDef.Names) 415 return def 416 } 417 418 func DeepCopyIndexDef(indexDef *plan.IndexDef) *plan.IndexDef { 419 if indexDef == nil { 420 return nil 421 } 422 newindexDef := &plan.IndexDef{ 423 IdxId: indexDef.IdxId, 424 IndexName: indexDef.IndexName, 425 Unique: indexDef.Unique, 426 TableExist: indexDef.TableExist, 427 IndexTableName: indexDef.IndexTableName, 428 Comment: indexDef.Comment, 429 } 430 431 newParts := make([]string, len(indexDef.Parts)) 432 copy(newParts, indexDef.Parts) 433 newindexDef.Parts = newParts 434 return newindexDef 435 } 436 437 func DeepCopyOnUpdate(old *plan.OnUpdate) *plan.OnUpdate { 438 if old == nil { 439 return nil 440 } 441 return &plan.OnUpdate{ 442 Expr: DeepCopyExpr(old.Expr), 443 OriginString: old.OriginString, 444 } 445 } 446 447 func DeepCopyTableDef(table *plan.TableDef) *plan.TableDef { 448 if table == nil { 449 return nil 450 } 451 newTable := &plan.TableDef{ 452 Name: table.Name, 453 Cols: make([]*plan.ColDef, len(table.Cols)), 454 Defs: make([]*plan.TableDef_DefType, len(table.Defs)), 455 TableType: table.TableType, 456 Createsql: table.Createsql, 457 Name2ColIndex: table.Name2ColIndex, 458 CompositePkey: nil, 459 OriginCols: make([]*plan.ColDef, len(table.OriginCols)), 460 Indexes: make([]*IndexDef, len(table.Indexes)), 461 } 462 463 for idx, col := range table.Cols { 464 newTable.Cols[idx] = DeepCopyColDef(col) 465 } 466 467 for idx, col := range table.OriginCols { 468 newTable.OriginCols[idx] = DeepCopyColDef(col) 469 } 470 471 if table.TblFunc != nil { 472 newTable.TblFunc = &plan.TableFunction{ 473 Name: table.TblFunc.Name, 474 Param: make([]byte, len(table.TblFunc.Param)), 475 } 476 copy(newTable.TblFunc.Param, table.TblFunc.Param) 477 } 478 479 if table.Pkey != nil { 480 newTable.Pkey = &plan.PrimaryKeyDef{ 481 Names: make([]string, len(table.Pkey.Names)), 482 } 483 copy(newTable.Pkey.Names, table.Pkey.Names) 484 } 485 486 if table.CompositePkey != nil { 487 newTable.CompositePkey = DeepCopyColDef(table.CompositePkey) 488 } 489 if table.ClusterBy != nil { 490 newTable.ClusterBy = &plan.ClusterByDef{ 491 Parts: make([]*plan.Expr, len(table.ClusterBy.Parts)), 492 Name: table.ClusterBy.Name, 493 } 494 for i, part := range table.ClusterBy.Parts { 495 newTable.ClusterBy.Parts[i] = DeepCopyExpr(part) 496 } 497 } 498 499 if table.ViewSql != nil { 500 newTable.ViewSql = &plan.ViewDef{ 501 View: table.ViewSql.View, 502 } 503 } 504 505 if table.Partition != nil { 506 partitionDef := &plan.PartitionByDef{ 507 Type: table.Partition.GetType(), 508 PartitionExpression: table.Partition.GetPartitionExpression(), 509 PartitionNum: table.Partition.GetPartitionNum(), 510 Partitions: make([]*plan.PartitionItem, len(table.Partition.Partitions)), 511 Algorithm: table.Partition.GetAlgorithm(), 512 IsSubPartition: table.Partition.GetIsSubPartition(), 513 PartitionMsg: table.Partition.GetPartitionMsg(), 514 } 515 if table.Partition.PartitionExpr != nil { 516 partitionDef.PartitionExpr = &plan.PartitionExpr{ 517 Expr: DeepCopyExpr(table.Partition.PartitionExpr.Expr), 518 ExprStr: table.Partition.PartitionExpr.GetExprStr(), 519 } 520 } 521 522 if table.Partition.PartitionColumns != nil { 523 partitionDef.PartitionColumns = &plan.PartitionColumns{ 524 Columns: make([]*plan.Expr, len(table.Partition.PartitionColumns.Columns)), 525 PartitionColumns: make([]string, len(table.Partition.PartitionColumns.PartitionColumns)), 526 } 527 for i, e := range table.Partition.PartitionColumns.Columns { 528 partitionDef.PartitionColumns.Columns[i] = DeepCopyExpr(e) 529 } 530 copy(partitionDef.PartitionColumns.PartitionColumns, table.Partition.PartitionColumns.PartitionColumns) 531 } 532 533 for i, e := range table.Partition.Partitions { 534 partitionDef.Partitions[i] = &plan.PartitionItem{ 535 PartitionName: e.PartitionName, 536 OrdinalPosition: e.OrdinalPosition, 537 Description: e.Description, 538 Comment: e.Comment, 539 LessThan: make([]*plan.Expr, len(e.LessThan)), 540 InValues: make([]*plan.Expr, len(e.InValues)), 541 } 542 for j, ee := range e.LessThan { 543 partitionDef.Partitions[i].LessThan[j] = DeepCopyExpr(ee) 544 } 545 for j, ee := range e.InValues { 546 partitionDef.Partitions[i].InValues[j] = DeepCopyExpr(ee) 547 } 548 } 549 newTable.Partition = partitionDef 550 } 551 552 if table.Indexes != nil { 553 for i, indexdef := range table.Indexes { 554 newTable.Indexes[i] = DeepCopyIndexDef(indexdef) 555 } 556 } 557 558 for idx, def := range table.Defs { 559 switch defImpl := def.Def.(type) { 560 case *plan.TableDef_DefType_Properties: 561 propDef := &plan.PropertiesDef{ 562 Properties: make([]*plan.Property, len(defImpl.Properties.Properties)), 563 } 564 for i, p := range defImpl.Properties.Properties { 565 propDef.Properties[i] = &plan.Property{ 566 Key: p.Key, 567 Value: p.Value, 568 } 569 } 570 newTable.Defs[idx] = &plan.TableDef_DefType{ 571 Def: &plan.TableDef_DefType_Properties{ 572 Properties: propDef, 573 }, 574 } 575 } 576 } 577 578 return newTable 579 } 580 581 func DeepCopyColData(col *plan.ColData) *plan.ColData { 582 newCol := &plan.ColData{ 583 Data: make([]*plan.Expr, len(col.Data)), 584 } 585 for i, e := range col.Data { 586 newCol.Data[i] = DeepCopyExpr(e) 587 } 588 589 return newCol 590 } 591 592 func DeepCopyQuery(qry *plan.Query) *plan.Query { 593 newQry := &plan.Query{ 594 StmtType: qry.StmtType, 595 Steps: qry.Steps, 596 Nodes: make([]*plan.Node, len(qry.Nodes)), 597 Params: make([]*plan.Expr, len(qry.Params)), 598 Headings: qry.Headings, 599 } 600 for idx, param := range qry.Params { 601 newQry.Params[idx] = DeepCopyExpr(param) 602 } 603 for idx, node := range qry.Nodes { 604 newQry.Nodes[idx] = DeepCopyNode(node) 605 } 606 return newQry 607 } 608 609 func DeepCopyPlan(pl *Plan) *Plan { 610 switch pl := pl.Plan.(type) { 611 case *Plan_Query: 612 return &Plan{ 613 Plan: &plan.Plan_Query{ 614 Query: DeepCopyQuery(pl.Query), 615 }, 616 } 617 618 case *plan.Plan_Ddl: 619 return &Plan{ 620 Plan: &plan.Plan_Ddl{ 621 Ddl: DeepCopyDataDefinition(pl.Ddl), 622 }, 623 } 624 625 default: 626 // only support query/insert plan now 627 return nil 628 } 629 } 630 631 func DeepCopyDataDefinition(old *plan.DataDefinition) *plan.DataDefinition { 632 newDf := &plan.DataDefinition{ 633 DdlType: old.DdlType, 634 } 635 if old.Query != nil { 636 newDf.Query = DeepCopyQuery(old.Query) 637 } 638 639 switch df := old.Definition.(type) { 640 case *plan.DataDefinition_CreateDatabase: 641 newDf.Definition = &plan.DataDefinition_CreateDatabase{ 642 CreateDatabase: &plan.CreateDatabase{ 643 IfNotExists: df.CreateDatabase.IfNotExists, 644 Database: df.CreateDatabase.Database, 645 }, 646 } 647 648 case *plan.DataDefinition_AlterDatabase: 649 newDf.Definition = &plan.DataDefinition_AlterDatabase{ 650 AlterDatabase: &plan.AlterDatabase{ 651 IfExists: df.AlterDatabase.IfExists, 652 Database: df.AlterDatabase.Database, 653 }, 654 } 655 656 case *plan.DataDefinition_DropDatabase: 657 newDf.Definition = &plan.DataDefinition_DropDatabase{ 658 DropDatabase: &plan.DropDatabase{ 659 IfExists: df.DropDatabase.IfExists, 660 Database: df.DropDatabase.Database, 661 }, 662 } 663 664 case *plan.DataDefinition_CreateTable: 665 newDf.Definition = &plan.DataDefinition_CreateTable{ 666 CreateTable: &plan.CreateTable{ 667 IfNotExists: df.CreateTable.IfNotExists, 668 Temporary: df.CreateTable.Temporary, 669 Database: df.CreateTable.Database, 670 TableDef: DeepCopyTableDef(df.CreateTable.TableDef), 671 }, 672 } 673 674 case *plan.DataDefinition_AlterTable: 675 newDf.Definition = &plan.DataDefinition_AlterTable{ 676 AlterTable: &plan.AlterTable{ 677 Table: df.AlterTable.Table, 678 TableDef: DeepCopyTableDef(df.AlterTable.TableDef), 679 }, 680 } 681 682 case *plan.DataDefinition_DropTable: 683 newDf.Definition = &plan.DataDefinition_DropTable{ 684 DropTable: &plan.DropTable{ 685 IfExists: df.DropTable.IfExists, 686 Database: df.DropTable.Database, 687 Table: df.DropTable.Table, 688 ClusterTable: DeepCopyClusterTable(df.DropTable.GetClusterTable()), 689 }, 690 } 691 692 case *plan.DataDefinition_CreateIndex: 693 newDf.Definition = &plan.DataDefinition_CreateIndex{ 694 CreateIndex: &plan.CreateIndex{ 695 Database: df.CreateIndex.Database, 696 Table: df.CreateIndex.Table, 697 Index: &plan.CreateTable{ 698 IfNotExists: df.CreateIndex.Index.IfNotExists, 699 Temporary: df.CreateIndex.Index.Temporary, 700 Database: df.CreateIndex.Index.Database, 701 TableDef: DeepCopyTableDef(df.CreateIndex.Index.TableDef), 702 }, 703 OriginTablePrimaryKey: df.CreateIndex.OriginTablePrimaryKey, 704 }, 705 } 706 707 case *plan.DataDefinition_AlterIndex: 708 newDf.Definition = &plan.DataDefinition_AlterIndex{ 709 AlterIndex: &plan.AlterIndex{ 710 Index: df.AlterIndex.Index, 711 }, 712 } 713 714 case *plan.DataDefinition_DropIndex: 715 newDf.Definition = &plan.DataDefinition_DropIndex{ 716 DropIndex: &plan.DropIndex{ 717 Database: df.DropIndex.Database, 718 Table: df.DropIndex.Table, 719 IndexName: df.DropIndex.IndexName, 720 IndexTableName: df.DropIndex.IndexTableName, 721 }, 722 } 723 724 case *plan.DataDefinition_TruncateTable: 725 truncateTable := &plan.TruncateTable{ 726 Database: df.TruncateTable.Database, 727 Table: df.TruncateTable.Table, 728 ClusterTable: DeepCopyClusterTable(df.TruncateTable.GetClusterTable()), 729 IndexTableNames: make([]string, len(df.TruncateTable.IndexTableNames)), 730 } 731 copy(truncateTable.IndexTableNames, df.TruncateTable.IndexTableNames) 732 newDf.Definition = &plan.DataDefinition_TruncateTable{ 733 TruncateTable: truncateTable, 734 } 735 736 case *plan.DataDefinition_ShowVariables: 737 showVariables := &plan.ShowVariables{ 738 Global: df.ShowVariables.Global, 739 Where: make([]*plan.Expr, len(df.ShowVariables.Where)), 740 } 741 for i, e := range df.ShowVariables.Where { 742 showVariables.Where[i] = DeepCopyExpr(e) 743 } 744 745 newDf.Definition = &plan.DataDefinition_ShowVariables{ 746 ShowVariables: showVariables, 747 } 748 749 } 750 751 return newDf 752 } 753 754 func DeepCopyExpr(expr *Expr) *Expr { 755 if expr == nil { 756 return nil 757 } 758 newExpr := &Expr{ 759 Typ: DeepCopyTyp(expr.Typ), 760 } 761 762 switch item := expr.Expr.(type) { 763 case *plan.Expr_C: 764 pc := &plan.Const{ 765 Isnull: item.C.GetIsnull(), 766 Src: item.C.Src, 767 } 768 769 switch c := item.C.Value.(type) { 770 case *plan.Const_I8Val: 771 pc.Value = &plan.Const_I8Val{I8Val: c.I8Val} 772 case *plan.Const_I16Val: 773 pc.Value = &plan.Const_I16Val{I16Val: c.I16Val} 774 case *plan.Const_I32Val: 775 pc.Value = &plan.Const_I32Val{I32Val: c.I32Val} 776 case *plan.Const_I64Val: 777 pc.Value = &plan.Const_I64Val{I64Val: c.I64Val} 778 case *plan.Const_Dval: 779 pc.Value = &plan.Const_Dval{Dval: c.Dval} 780 case *plan.Const_Sval: 781 pc.Value = &plan.Const_Sval{Sval: c.Sval} 782 case *plan.Const_Bval: 783 pc.Value = &plan.Const_Bval{Bval: c.Bval} 784 case *plan.Const_U8Val: 785 pc.Value = &plan.Const_U8Val{U8Val: c.U8Val} 786 case *plan.Const_U16Val: 787 pc.Value = &plan.Const_U16Val{U16Val: c.U16Val} 788 case *plan.Const_U32Val: 789 pc.Value = &plan.Const_U32Val{U32Val: c.U32Val} 790 case *plan.Const_U64Val: 791 pc.Value = &plan.Const_U64Val{U64Val: c.U64Val} 792 case *plan.Const_Fval: 793 pc.Value = &plan.Const_Fval{Fval: c.Fval} 794 case *plan.Const_Dateval: 795 pc.Value = &plan.Const_Dateval{Dateval: c.Dateval} 796 case *plan.Const_Timeval: 797 pc.Value = &plan.Const_Timeval{Timeval: c.Timeval} 798 case *plan.Const_Datetimeval: 799 pc.Value = &plan.Const_Datetimeval{Datetimeval: c.Datetimeval} 800 case *plan.Const_Decimal64Val: 801 pc.Value = &plan.Const_Decimal64Val{Decimal64Val: &plan.Decimal64{A: c.Decimal64Val.A}} 802 case *plan.Const_Decimal128Val: 803 pc.Value = &plan.Const_Decimal128Val{Decimal128Val: &plan.Decimal128{A: c.Decimal128Val.A, B: c.Decimal128Val.B}} 804 case *plan.Const_Timestampval: 805 pc.Value = &plan.Const_Timestampval{Timestampval: c.Timestampval} 806 case *plan.Const_Jsonval: 807 pc.Value = &plan.Const_Jsonval{Jsonval: c.Jsonval} 808 case *plan.Const_Defaultval: 809 pc.Value = &plan.Const_Defaultval{Defaultval: c.Defaultval} 810 case *plan.Const_UpdateVal: 811 pc.Value = &plan.Const_UpdateVal{UpdateVal: c.UpdateVal} 812 } 813 814 newExpr.Expr = &plan.Expr_C{ 815 C: pc, 816 } 817 818 case *plan.Expr_P: 819 newExpr.Expr = &plan.Expr_P{ 820 P: &plan.ParamRef{ 821 Pos: item.P.GetPos(), 822 }, 823 } 824 825 case *plan.Expr_V: 826 newExpr.Expr = &plan.Expr_V{ 827 V: &plan.VarRef{ 828 Name: item.V.GetName(), 829 }, 830 } 831 832 case *plan.Expr_Col: 833 newExpr.Expr = &plan.Expr_Col{ 834 Col: &plan.ColRef{ 835 RelPos: item.Col.GetRelPos(), 836 ColPos: item.Col.GetColPos(), 837 Name: item.Col.GetName(), 838 }, 839 } 840 841 case *plan.Expr_F: 842 newArgs := make([]*Expr, len(item.F.Args)) 843 for idx, arg := range item.F.Args { 844 newArgs[idx] = DeepCopyExpr(arg) 845 } 846 newExpr.Expr = &plan.Expr_F{ 847 F: &plan.Function{ 848 Func: DeepCopyObjectRef(item.F.Func), 849 Args: newArgs, 850 }, 851 } 852 853 case *plan.Expr_Sub: 854 newExpr.Expr = &plan.Expr_Sub{ 855 Sub: &plan.SubqueryRef{ 856 NodeId: item.Sub.GetNodeId(), 857 }, 858 } 859 860 case *plan.Expr_Corr: 861 newExpr.Expr = &plan.Expr_Corr{ 862 Corr: &plan.CorrColRef{ 863 ColPos: item.Corr.GetColPos(), 864 RelPos: item.Corr.GetRelPos(), 865 Depth: item.Corr.GetDepth(), 866 }, 867 } 868 869 case *plan.Expr_T: 870 newExpr.Expr = &plan.Expr_T{ 871 T: &plan.TargetType{ 872 Typ: DeepCopyTyp(item.T.Typ), 873 }, 874 } 875 876 case *plan.Expr_Max: 877 newExpr.Expr = &plan.Expr_Max{ 878 Max: &plan.MaxValue{ 879 Value: item.Max.GetValue(), 880 }, 881 } 882 883 case *plan.Expr_List: 884 e := &plan.ExprList{ 885 List: make([]*plan.Expr, len(item.List.List)), 886 } 887 for i, ie := range item.List.List { 888 e.List[i] = DeepCopyExpr(ie) 889 } 890 newExpr.Expr = &plan.Expr_List{ 891 List: e, 892 } 893 } 894 895 return newExpr 896 } 897 898 func DeepCopyClusterTable(cluster *plan.ClusterTable) *plan.ClusterTable { 899 if cluster == nil { 900 return nil 901 } 902 903 accountIds := make([]uint32, len(cluster.GetAccountIDs())) 904 copy(accountIds, cluster.GetAccountIDs()) 905 newClusterTable := &plan.ClusterTable{ 906 IsClusterTable: cluster.GetIsClusterTable(), 907 AccountIDs: accountIds, 908 ColumnIndexOfAccountId: cluster.GetColumnIndexOfAccountId(), 909 } 910 return newClusterTable 911 } 912 913 func DeepCopyAnalyzeInfo(analyzeinfo *plan.AnalyzeInfo) *plan.AnalyzeInfo { 914 if analyzeinfo == nil { 915 return nil 916 } 917 918 return &plan.AnalyzeInfo{ 919 InputRows: analyzeinfo.GetInputRows(), 920 OutputRows: analyzeinfo.GetOutputRows(), 921 InputSize: analyzeinfo.GetInputSize(), 922 OutputSize: analyzeinfo.GetOutputSize(), 923 TimeConsumed: analyzeinfo.GetTimeConsumed(), 924 MemorySize: analyzeinfo.GetMemorySize(), 925 WaitTimeConsumed: analyzeinfo.GetWaitTimeConsumed(), 926 DiskIO: analyzeinfo.GetDiskIO(), 927 S3IOByte: analyzeinfo.GetS3IOByte(), 928 S3IOCount: analyzeinfo.GetS3IOCount(), 929 NetworkIO: analyzeinfo.GetNetworkIO(), 930 ScanTime: analyzeinfo.GetScanTime(), 931 InsertTime: analyzeinfo.GetInsertTime(), 932 } 933 }