github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/moengine/txn_relation.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 moengine
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    21  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    22  	"github.com/matrixorigin/matrixone/pkg/container/types"
    23  	"github.com/matrixorigin/matrixone/pkg/container/vector"
    24  	"github.com/matrixorigin/matrixone/pkg/logutil"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/handle"
    29  )
    30  
    31  var (
    32  	_ engine.Relation = (*txnRelation)(nil)
    33  )
    34  
    35  func newRelation(h handle.Relation) *txnRelation {
    36  	r := &txnRelation{}
    37  	r.handle = h
    38  	r.nodes = append(r.nodes, engine.Node{
    39  		Addr: ADDR,
    40  	})
    41  	return r
    42  }
    43  
    44  func (rel *txnRelation) Write(ctx context.Context, bat *batch.Batch) error {
    45  	schema := rel.handle.GetMeta().(*catalog.TableEntry).GetSchema()
    46  	allNullables := schema.AllNullables()
    47  	taeBatch := containers.NewEmptyBatch()
    48  	defer taeBatch.Close()
    49  	for i, vec := range bat.Vecs {
    50  		v := containers.NewVectorWithSharedMemory(vec, allNullables[i])
    51  		//v := MOToVector(vec, allNullables[i])
    52  		taeBatch.AddVector(bat.Attrs[i], v)
    53  	}
    54  	if schema.HasSortKey() && !schema.HasPK() { // has cluster by key
    55  		clusterby := schema.GetSingleSortKey()
    56  		vec := taeBatch.GetVectorByName(clusterby.Name)
    57  		if vec.HasNull() {
    58  			return moerr.NewInternalError(ctx, "null value is not supported in cluster by column for now")
    59  		}
    60  	}
    61  	return rel.handle.Append(taeBatch)
    62  }
    63  
    64  func (rel *txnRelation) AddBlksWithMetaLoc(
    65  	ctx context.Context,
    66  	pkVecs []containers.Vector,
    67  	file string,
    68  	metaLocs []string,
    69  	flag int32) error {
    70  	return rel.handle.AddBlksWithMetaLoc(pkVecs, file, metaLocs, flag)
    71  }
    72  
    73  func (rel *txnRelation) Update(_ context.Context, data *batch.Batch) error {
    74  	return moerr.NewNYINoCtx("Update not supported")
    75  }
    76  
    77  func (rel *txnRelation) DeleteByPhyAddrKeys(_ context.Context, keys *vector.Vector) error {
    78  	tvec := containers.NewVectorWithSharedMemory(keys, false)
    79  	defer tvec.Close()
    80  	return rel.handle.DeleteByPhyAddrKeys(tvec)
    81  }
    82  
    83  func (rel *txnRelation) Delete(_ context.Context, bat *batch.Batch, col string) error {
    84  	data := bat.Vecs[0]
    85  	schema := rel.handle.GetMeta().(*catalog.TableEntry).GetSchema()
    86  	logutil.Debugf("Delete col: %v", col)
    87  	allNullables := schema.AllNullables()
    88  	idx := catalog.GetAttrIdx(schema.AllNames(), col)
    89  	if data.Typ.Oid == types.T_any {
    90  		data.Typ = schema.ColDefs[idx].Type
    91  	}
    92  	vec := containers.NewVectorWithSharedMemory(data, allNullables[idx])
    93  	defer vec.Close()
    94  	if schema.PhyAddrKey.Name == col {
    95  		return rel.handle.DeleteByPhyAddrKeys(vec)
    96  	}
    97  	if !schema.HasPK() {
    98  		panic(any("No valid primary key found"))
    99  	}
   100  	if schema.SortKey.Defs[0].Name == col {
   101  		for i := 0; i < vec.Length(); i++ {
   102  			filter := handle.NewEQFilter(vec.Get(i))
   103  			err := rel.handle.DeleteByFilter(filter)
   104  			if err != nil {
   105  				return err
   106  			}
   107  		}
   108  		return nil
   109  	}
   110  	panic(any("Key not found"))
   111  }