github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/compile/dml.go (about) 1 // Copyright 2021 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 compile 16 17 import ( 18 "github.com/matrixorigin/matrixone/pkg/sql/colexec" 19 "github.com/matrixorigin/matrixone/pkg/sql/colexec/deletion" 20 "github.com/matrixorigin/matrixone/pkg/sql/colexec/insert" 21 "github.com/matrixorigin/matrixone/pkg/sql/colexec/update" 22 ) 23 24 func (s *Scope) Delete(c *Compile) (uint64, error) { 25 s.Magic = Merge 26 arg := s.Instructions[len(s.Instructions)-1].Arg.(*deletion.Argument) 27 28 if arg.DeleteCtx.CanTruncate { 29 var err error 30 var affectRows int64 31 32 for i, rel := range arg.DeleteCtx.DelSource { 33 _, err = rel.Ranges(c.ctx, nil) 34 if err != nil { 35 return 0, err 36 } 37 affectRow, err := rel.Rows(s.Proc.Ctx) 38 if err != nil { 39 return 0, err 40 } 41 affectRows = affectRows + affectRow 42 43 dbName := arg.DeleteCtx.DelRef[i].SchemaName 44 tblName := arg.DeleteCtx.DelRef[i].ObjName 45 oldId := uint64(arg.DeleteCtx.DelRef[i].Obj) 46 dbSource, err := c.e.Database(c.ctx, dbName, c.proc.TxnOperator) 47 if err != nil { 48 return 0, err 49 } 50 51 // truncate origin table 52 newId, err := dbSource.Truncate(c.ctx, tblName) 53 if err != nil { 54 return 0, err 55 } 56 57 // truncate autoIncr table 58 err = colexec.MoveAutoIncrCol(c.e, c.ctx, tblName, dbSource, c.proc, oldId, newId, dbName) 59 if err != nil { 60 return 0, err 61 } 62 63 } 64 65 return uint64(affectRows), nil 66 } 67 68 if err := s.MergeRun(c); err != nil { 69 return 0, err 70 } 71 return arg.AffectedRows, nil 72 } 73 74 func (s *Scope) Insert(c *Compile) (uint64, error) { 75 s.Magic = Merge 76 arg := s.Instructions[len(s.Instructions)-1].Arg.(*insert.Argument) 77 if err := s.MergeRun(c); err != nil { 78 return 0, err 79 } 80 return arg.Affected, nil 81 } 82 83 func (s *Scope) Update(c *Compile) (uint64, error) { 84 s.Magic = Merge 85 arg := s.Instructions[len(s.Instructions)-1].Arg.(*update.Argument) 86 if err := s.MergeRun(c); err != nil { 87 return 0, err 88 } 89 return arg.AffectedRows, nil 90 }