github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/colexec/deletion/deletion.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 deletion
    16  
    17  import (
    18  	"bytes"
    19  	"sync/atomic"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/sql/colexec"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    24  )
    25  
    26  func String(arg any, buf *bytes.Buffer) {
    27  	buf.WriteString("delete rows")
    28  }
    29  
    30  func Prepare(_ *process.Process, _ any) error {
    31  	return nil
    32  }
    33  
    34  // the bool return value means whether it completed its work or not
    35  func Call(_ int, proc *process.Process, arg any, isFirst bool, isLast bool) (bool, error) {
    36  	p := arg.(*Argument)
    37  	bat := proc.Reg.InputBatch
    38  
    39  	// last batch of block
    40  	if bat == nil {
    41  		return true, nil
    42  	}
    43  
    44  	// empty batch
    45  	if len(bat.Zs) == 0 {
    46  		return false, nil
    47  	}
    48  
    49  	defer bat.Clean(proc.Mp())
    50  	var affectedRows uint64
    51  	var err error
    52  	delCtx := p.DeleteCtx
    53  
    54  	// check OnRestrict, if is not all null, throw error
    55  	for _, idx := range delCtx.OnRestrictIdx {
    56  		if bat.Vecs[idx].Length() != bat.Vecs[idx].Nsp.Np.Count() {
    57  			return false, moerr.NewInternalError(proc.Ctx, "Cannot delete or update a parent row: a foreign key constraint fails")
    58  		}
    59  	}
    60  
    61  	// delete unique index
    62  	_, err = colexec.FilterAndDelByRowId(proc, bat, delCtx.IdxIdx, delCtx.IdxSource)
    63  	if err != nil {
    64  		return false, err
    65  	}
    66  
    67  	// delete child table(which ref on delete cascade)
    68  	_, err = colexec.FilterAndDelByRowId(proc, bat, delCtx.OnCascadeIdx, delCtx.OnCascadeSource)
    69  	if err != nil {
    70  		return false, err
    71  	}
    72  
    73  	// update child table(which ref on delete set null)
    74  	_, err = colexec.FilterAndUpdateByRowId(p.Engine, proc, bat, delCtx.OnSetIdx, delCtx.OnSetSource,
    75  		delCtx.OnSetRef, delCtx.OnSetTableDef, delCtx.OnSetUpdateCol, nil, delCtx.OnSetUniqueSource)
    76  	if err != nil {
    77  		return false, err
    78  	}
    79  
    80  	// delete origin table
    81  	idxList := make([]int32, len(delCtx.DelSource))
    82  	for i := 0; i < len(delCtx.DelSource); i++ {
    83  		idxList[i] = int32(i)
    84  	}
    85  	affectedRows, err = colexec.FilterAndDelByRowId(proc, bat, idxList, delCtx.DelSource)
    86  	if err != nil {
    87  		return false, err
    88  	}
    89  
    90  	atomic.AddUint64(&p.AffectedRows, affectedRows)
    91  	return false, nil
    92  }