github.com/matrixorigin/matrixone@v0.7.0/pkg/vm/engine/tae/moengine/helper.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  	"strings"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/container/types"
    22  	"github.com/matrixorigin/matrixone/pkg/defines"
    23  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    24  
    25  	pkgcatalog "github.com/matrixorigin/matrixone/pkg/catalog"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    27  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    28  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/iface/txnif"
    29  )
    30  
    31  func txnBindAccessInfoFromCtx(txn txnif.AsyncTxn, ctx context.Context) {
    32  	if ctx == nil {
    33  		return
    34  	}
    35  	tid, okt := ctx.Value(defines.TenantIDKey{}).(uint32)
    36  	uid, _ := ctx.Value(defines.UserIDKey{}).(uint32)
    37  	rid, _ := ctx.Value(defines.RoleIDKey{}).(uint32)
    38  	if okt {
    39  		txn.BindAccessInfo(tid, uid, rid)
    40  	}
    41  }
    42  
    43  func ColDefsToAttrs(colDefs []*catalog.ColDef) (attrs []*engine.Attribute, err error) {
    44  	for _, col := range colDefs {
    45  		attr, err := AttrFromColDef(col)
    46  		if err != nil {
    47  			return nil, err
    48  		}
    49  		attrs = append(attrs, attr)
    50  	}
    51  	return
    52  }
    53  
    54  func AttrFromColDef(col *catalog.ColDef) (attrs *engine.Attribute, err error) {
    55  	var defaultVal *plan.Default
    56  	if len(col.Default) > 0 {
    57  		defaultVal = &plan.Default{}
    58  		if err := types.Decode(col.Default, defaultVal); err != nil {
    59  			return nil, err
    60  		}
    61  	}
    62  
    63  	var onUpdate *plan.OnUpdate
    64  	if len(col.OnUpdate) > 0 {
    65  		onUpdate = new(plan.OnUpdate)
    66  		if err := types.Decode(col.OnUpdate, onUpdate); err != nil {
    67  			return nil, err
    68  		}
    69  	}
    70  
    71  	attr := &engine.Attribute{
    72  		Name:          col.Name,
    73  		Type:          col.Type,
    74  		Primary:       col.IsPrimary(),
    75  		IsHidden:      col.IsHidden(),
    76  		IsRowId:       col.IsPhyAddr(),
    77  		Comment:       col.Comment,
    78  		Default:       defaultVal,
    79  		OnUpdate:      onUpdate,
    80  		AutoIncrement: col.IsAutoIncrement(),
    81  		ClusterBy:     col.IsClusterBy(),
    82  	}
    83  	return attr, nil
    84  }
    85  
    86  func SchemaToDefs(schema *catalog.Schema) (defs []engine.TableDef, err error) {
    87  	if schema.Comment != "" {
    88  		commentDef := new(engine.CommentDef)
    89  		commentDef.Comment = schema.Comment
    90  		defs = append(defs, commentDef)
    91  	}
    92  
    93  	if schema.Partition != "" {
    94  		partitionDef := new(engine.PartitionDef)
    95  		partitionDef.Partition = schema.Partition
    96  		defs = append(defs, partitionDef)
    97  	}
    98  
    99  	if schema.View != "" {
   100  		viewDef := new(engine.ViewDef)
   101  		viewDef.View = schema.View
   102  		defs = append(defs, viewDef)
   103  	}
   104  
   105  	if len(schema.Constraint) > 0 {
   106  		c := new(engine.ConstraintDef)
   107  		if err := c.UnmarshalBinary(schema.Constraint); err != nil {
   108  			return nil, err
   109  		}
   110  		defs = append(defs, c)
   111  	}
   112  
   113  	for _, col := range schema.ColDefs {
   114  		if col.IsPhyAddr() {
   115  			continue
   116  		}
   117  		attr, err := AttrFromColDef(col)
   118  		if err != nil {
   119  			return nil, err
   120  		}
   121  		defs = append(defs, &engine.AttributeDef{Attr: *attr})
   122  	}
   123  	pro := new(engine.PropertiesDef)
   124  	pro.Properties = append(pro.Properties, engine.Property{
   125  		Key:   pkgcatalog.SystemRelAttr_Kind,
   126  		Value: string(schema.Relkind),
   127  	})
   128  	if schema.Createsql != "" {
   129  		pro.Properties = append(pro.Properties, engine.Property{
   130  			Key:   pkgcatalog.SystemRelAttr_CreateSQL,
   131  			Value: schema.Createsql,
   132  		})
   133  	}
   134  	defs = append(defs, pro)
   135  
   136  	return
   137  }
   138  
   139  func DefsToSchema(name string, defs []engine.TableDef) (schema *catalog.Schema, err error) {
   140  	schema = catalog.NewEmptySchema(name)
   141  	var pkeyColName string
   142  	for _, def := range defs {
   143  		switch defVal := def.(type) {
   144  		case *engine.ConstraintDef:
   145  			primaryKeyDef := defVal.GetPrimaryKeyDef()
   146  			if primaryKeyDef != nil {
   147  				pkeyColName = primaryKeyDef.Pkey.PkeyColName
   148  				break
   149  			}
   150  		}
   151  	}
   152  	for _, def := range defs {
   153  		switch defVal := def.(type) {
   154  		case *engine.AttributeDef:
   155  			if pkeyColName == defVal.Attr.Name {
   156  				if err = schema.AppendSortColWithAttribute(defVal.Attr, 0, true); err != nil {
   157  					return
   158  				}
   159  			} else if defVal.Attr.ClusterBy {
   160  				if err = schema.AppendSortColWithAttribute(defVal.Attr, 0, false); err != nil {
   161  					return
   162  				}
   163  			} else {
   164  				if err = schema.AppendColWithAttribute(defVal.Attr); err != nil {
   165  					return
   166  				}
   167  			}
   168  
   169  		case *engine.PropertiesDef:
   170  			for _, property := range defVal.Properties {
   171  				switch strings.ToLower(property.Key) {
   172  				case pkgcatalog.SystemRelAttr_Comment:
   173  					schema.Comment = property.Value
   174  				case pkgcatalog.SystemRelAttr_Kind:
   175  					schema.Relkind = property.Value
   176  				case pkgcatalog.SystemRelAttr_CreateSQL:
   177  					schema.Createsql = property.Value
   178  				default:
   179  				}
   180  			}
   181  
   182  		case *engine.PartitionDef:
   183  			schema.Partition = defVal.Partition
   184  		case *engine.ViewDef:
   185  			schema.View = defVal.View
   186  		case *engine.CommentDef:
   187  			schema.Comment = defVal.Comment
   188  		case *engine.ConstraintDef:
   189  			schema.Constraint, err = defVal.MarshalBinary()
   190  			if err != nil {
   191  				return nil, err
   192  			}
   193  		default:
   194  			// We will not deal with other cases for the time being
   195  		}
   196  	}
   197  	if err = schema.Finalize(false); err != nil {
   198  		return
   199  	}
   200  	return
   201  }