github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/colexec/mergedelete/mergedelete.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 mergedelete
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    22  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    23  	"github.com/matrixorigin/matrixone/pkg/vm"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    25  )
    26  
    27  const argName = "merge_delete"
    28  
    29  func (arg *Argument) String(buf *bytes.Buffer) {
    30  	buf.WriteString(argName)
    31  	buf.WriteString(": MergeS3DeleteInfo ")
    32  }
    33  
    34  func (arg *Argument) Prepare(proc *process.Process) error {
    35  	return nil
    36  }
    37  
    38  func (arg *Argument) Call(proc *process.Process) (vm.CallResult, error) {
    39  	if err, isCancel := vm.CancelCheck(proc); isCancel {
    40  		return vm.CancelResult, err
    41  	}
    42  
    43  	var err error
    44  	var name string
    45  	ap := arg
    46  
    47  	result, err := arg.GetChildren(0).Call(proc)
    48  	if err != nil {
    49  		return result, err
    50  	}
    51  
    52  	anal := proc.GetAnalyze(arg.GetIdx(), arg.GetParallelIdx(), arg.GetParallelMajor())
    53  	anal.Start()
    54  	defer anal.Stop()
    55  
    56  	if result.Batch == nil || result.Batch.IsEmpty() {
    57  		return result, nil
    58  	}
    59  	bat := result.Batch
    60  
    61  	// 	  blkId           deltaLoc                        type                                 partitionIdx
    62  	// |----------|-----------------------------|-------------------------------------------|---------------------
    63  	// |  blk_id  | batch.Marshal(deltaLoc)     | FlushDeltaLoc  (DN Block )                |  partitionIdx
    64  	// |  blk_id  | batch.Marshal(int64 offset) | CNBlockOffset (CN Block )                 |  partitionIdx
    65  	// |  blk_id  | batch.Marshal(rowId)        | RawRowIdBatch (DN Blcok )                 |  partitionIdx
    66  	// |  blk_id  | batch.Marshal(int64 offset) | RawBatchOffset(RawBatch[in txn workspace])|  partitionIdx
    67  	blkIds := vector.MustStrCol(bat.GetVector(0))
    68  	deltaLocs := vector.MustBytesCol(bat.GetVector(1))
    69  	typs := vector.MustFixedCol[int8](bat.GetVector(2))
    70  
    71  	// If the target table is a partition table, Traverse partition subtables for separate processing
    72  	if len(ap.PartitionSources) > 0 {
    73  		partitionIdxs := vector.MustFixedCol[int32](bat.GetVector(3))
    74  		for i := 0; i < bat.RowCount(); i++ {
    75  			name = fmt.Sprintf("%s|%d", blkIds[i], typs[i])
    76  			bat := &batch.Batch{}
    77  			if err := bat.UnmarshalBinary(deltaLocs[i]); err != nil {
    78  				return result, err
    79  			}
    80  			bat.Cnt = 1
    81  			pIndex := partitionIdxs[i]
    82  			err = ap.PartitionSources[pIndex].Delete(proc.Ctx, bat, name)
    83  			if err != nil {
    84  				return result, err
    85  			}
    86  		}
    87  	} else {
    88  		// If the target table is a general table
    89  		for i := 0; i < bat.RowCount(); i++ {
    90  			name = fmt.Sprintf("%s|%d", blkIds[i], typs[i])
    91  			bat := &batch.Batch{}
    92  			if err := bat.UnmarshalBinary(deltaLocs[i]); err != nil {
    93  				return result, err
    94  			}
    95  			bat.Cnt = 1
    96  			err = ap.DelSource.Delete(proc.Ctx, bat, name)
    97  			if err != nil {
    98  				return result, err
    99  			}
   100  		}
   101  	}
   102  	// and there are another attr used to record how many rows are deleted
   103  	ap.AffectedRows += uint64(vector.GetFixedAt[uint32](bat.GetVector(4), 0))
   104  	return result, nil
   105  }