github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/moengine/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  	plan2 "github.com/matrixorigin/matrixone/pkg/sql/plan"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    22  	"github.com/matrixorigin/matrixone/pkg/container/batch"
    23  	"github.com/matrixorigin/matrixone/pkg/logutil"
    24  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    27  )
    28  
    29  var (
    30  	_ engine.Relation = (*baseRelation)(nil)
    31  )
    32  
    33  const ADDR = "localhost:20000"
    34  
    35  func (*baseRelation) Size(context.Context, string) (int64, error) {
    36  	return 0, nil
    37  }
    38  
    39  func (*baseRelation) CardinalNumber(string) int64 {
    40  	return 0
    41  }
    42  
    43  func (*baseRelation) Ranges(_ context.Context, _ *plan.Expr) ([][]byte, error) {
    44  	return nil, nil
    45  }
    46  
    47  func (*baseRelation) AddTableDef(_ context.Context, def engine.TableDef) error {
    48  	panic(any("implement me"))
    49  }
    50  
    51  func (*baseRelation) DelTableDef(_ context.Context, def engine.TableDef) error {
    52  	panic(any("implement me"))
    53  }
    54  
    55  func (rel *baseRelation) TableDefs(_ context.Context) ([]engine.TableDef, error) {
    56  	schema := rel.handle.GetMeta().(*catalog.TableEntry).GetSchema()
    57  	defs, _ := SchemaToDefs(schema)
    58  	return defs, nil
    59  }
    60  
    61  func (rel *baseRelation) UpdateConstraint(_ context.Context, def *engine.ConstraintDef) error {
    62  	bin, err := def.MarshalBinary()
    63  	if err != nil {
    64  		return err
    65  	}
    66  	return rel.handle.UpdateConstraint(bin)
    67  }
    68  
    69  func (rel *baseRelation) UpdateConstraintWithBin(_ context.Context, bin []byte) error {
    70  	return rel.handle.UpdateConstraint(bin)
    71  }
    72  
    73  func (rel *baseRelation) TableColumns(_ context.Context) ([]*engine.Attribute, error) {
    74  	colDefs := rel.handle.GetMeta().(*catalog.TableEntry).GetColDefs()
    75  	cols, _ := ColDefsToAttrs(colDefs)
    76  	return cols, nil
    77  }
    78  
    79  func (rel *baseRelation) Stats(context.Context, *plan2.Expr) (*plan2.Stats, error) {
    80  	//for tae, it does not matter and will be deleted in the future
    81  	return plan2.DefaultStats(), nil
    82  }
    83  
    84  func (rel *baseRelation) Rows(c context.Context) (int64, error) {
    85  	rows := rel.handle.Rows()
    86  	return rows, nil
    87  }
    88  
    89  func (rel *baseRelation) GetSchema(_ context.Context) *catalog.Schema {
    90  	return rel.handle.GetMeta().(*catalog.TableEntry).GetSchema()
    91  }
    92  
    93  func (rel *baseRelation) GetPrimaryKeys(_ context.Context) ([]*engine.Attribute, error) {
    94  	schema := rel.handle.GetMeta().(*catalog.TableEntry).GetSchema()
    95  	if !schema.HasPK() {
    96  		return nil, nil
    97  	}
    98  	attrs := make([]*engine.Attribute, 0, len(schema.SortKey.Defs))
    99  	for _, def := range schema.SortKey.Defs {
   100  		attr := new(engine.Attribute)
   101  		attr.Name = def.Name
   102  		attr.Type = def.Type
   103  		attrs = append(attrs, attr)
   104  	}
   105  	logutil.Debugf("GetPrimaryKeys: %v", attrs[0])
   106  	return attrs, nil
   107  }
   108  
   109  // The hidden column in tae has been renamed to PhyAddr, while GetHideKeys method remains untouched.
   110  // As @nnsgmsone suggests, it is better to only retain TableDefs and discard other column-info-related methods.
   111  // Might that can be done in the future
   112  
   113  func (rel *baseRelation) GetHideKeys(_ context.Context) ([]*engine.Attribute, error) {
   114  	schema := rel.handle.GetMeta().(*catalog.TableEntry).GetSchema()
   115  	if schema.PhyAddrKey == nil {
   116  		return nil, moerr.NewNotSupportedNoCtx("system table has no rowid")
   117  	}
   118  	key := new(engine.Attribute)
   119  	key.Name = schema.PhyAddrKey.Name
   120  	key.Type = schema.PhyAddrKey.Type
   121  	key.IsRowId = true
   122  	// key.IsHidden = true
   123  	logutil.Debugf("GetHideKey: %v", key)
   124  	return []*engine.Attribute{key}, nil
   125  }
   126  
   127  func (rel *baseRelation) Write(_ context.Context, _ *batch.Batch) error {
   128  	return nil
   129  }
   130  
   131  func (rel *baseRelation) Update(_ context.Context, _ *batch.Batch) error {
   132  	return nil
   133  }
   134  
   135  func (rel *baseRelation) Delete(_ context.Context, _ *batch.Batch, _ string) error {
   136  	return nil
   137  }
   138  
   139  func (rel *baseRelation) NewReader(_ context.Context, num int, _ *plan.Expr, _ [][]byte) ([]engine.Reader, error) {
   140  	var rds []engine.Reader
   141  
   142  	it := rel.handle.MakeBlockIt()
   143  	for i := 0; i < num; i++ {
   144  		reader := newReader(rel.handle, it)
   145  		rds = append(rds, reader)
   146  	}
   147  	return rds, nil
   148  }
   149  
   150  func (rel *baseRelation) GetTableID(_ context.Context) uint64 {
   151  	return rel.handle.ID()
   152  }
   153  
   154  func (rel *baseRelation) GetRelationID(_ context.Context) uint64 {
   155  	return rel.handle.ID()
   156  }
   157  
   158  func (rel *baseRelation) MaxAndMinValues(ctx context.Context) ([][2]any, []uint8, error) {
   159  	return nil, nil, nil
   160  }