github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/memoryengine/compiler_context.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 memoryengine
    16  
    17  import (
    18  	"context"
    19  	"strconv"
    20  
    21  	"github.com/matrixorigin/matrixone/pkg/catalog"
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/defines"
    24  	planpb "github.com/matrixorigin/matrixone/pkg/pb/plan"
    25  	pb "github.com/matrixorigin/matrixone/pkg/pb/statsinfo"
    26  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    27  	"github.com/matrixorigin/matrixone/pkg/sql/plan"
    28  	"github.com/matrixorigin/matrixone/pkg/sql/plan/function"
    29  	"github.com/matrixorigin/matrixone/pkg/testutil"
    30  	"github.com/matrixorigin/matrixone/pkg/txn/client"
    31  	"github.com/matrixorigin/matrixone/pkg/vm/engine"
    32  	"github.com/matrixorigin/matrixone/pkg/vm/process"
    33  )
    34  
    35  type CompilerContext struct {
    36  	ctx       context.Context
    37  	defaultDB string
    38  	engine    *Engine
    39  	txnOp     client.TxnOperator
    40  }
    41  
    42  func (c *CompilerContext) GetViews() []string {
    43  	//TODO implement me
    44  	panic("implement me")
    45  }
    46  
    47  func (c *CompilerContext) SetViews(views []string) {
    48  	//TODO implement me
    49  	panic("implement me")
    50  }
    51  
    52  func (c *CompilerContext) GetSnapshot() *plan.Snapshot {
    53  	//TODO implement me
    54  	panic("implement me")
    55  }
    56  
    57  func (c *CompilerContext) SetSnapshot(snapshot *plan.Snapshot) {
    58  	//TODO implement me
    59  	panic("implement me")
    60  }
    61  
    62  func (c *CompilerContext) ReplacePlan(execPlan *planpb.Execute) (*planpb.Plan, tree.Statement, error) {
    63  	//TODO implement me
    64  	panic("implement me")
    65  }
    66  
    67  func (c *CompilerContext) CheckSubscriptionValid(subName, accName string, pubName string) error {
    68  	//TODO implement me
    69  	panic("implement me")
    70  }
    71  
    72  func (c *CompilerContext) ResolveSubscriptionTableById(tableId uint64, pubmeta *plan.SubscriptionMeta) (*plan.ObjectRef, *plan.TableDef) {
    73  	//TODO implement me
    74  	panic("implement me")
    75  }
    76  
    77  func (c *CompilerContext) IsPublishing(dbName string) (bool, error) {
    78  	//TODO implement me
    79  	panic("implement me")
    80  }
    81  
    82  func (c *CompilerContext) ResolveSnapshotWithSnapshotName(snapshotName string) (*plan.Snapshot, error) {
    83  	//TODO implement me
    84  	panic("implement me")
    85  }
    86  
    87  func (c *CompilerContext) CheckTimeStampValid(ts int64) (bool, error) {
    88  	//TODO implement me
    89  	panic("implement me")
    90  }
    91  
    92  func (c *CompilerContext) SetQueryingSubscription(meta *plan.SubscriptionMeta) {
    93  	//TODO implement me
    94  	panic("implement me")
    95  }
    96  
    97  func (c *CompilerContext) GetQueryingSubscription() *plan.SubscriptionMeta {
    98  	return nil
    99  }
   100  
   101  func (e *Engine) NewCompilerContext(
   102  	ctx context.Context,
   103  	defaultDB string,
   104  	txnOp client.TxnOperator,
   105  ) *CompilerContext {
   106  	return &CompilerContext{
   107  		ctx:       ctx,
   108  		defaultDB: defaultDB,
   109  		engine:    e,
   110  		txnOp:     txnOp,
   111  	}
   112  }
   113  
   114  var _ plan.CompilerContext = new(CompilerContext)
   115  
   116  func (c *CompilerContext) ResolveUdf(name string, ast []*plan.Expr) (*function.Udf, error) {
   117  	return nil, nil
   118  }
   119  
   120  func (c *CompilerContext) ResolveAccountIds(accountNames []string) ([]uint32, error) {
   121  	return []uint32{catalog.System_Account}, nil
   122  }
   123  
   124  func (*CompilerContext) Stats(obj *plan.ObjectRef, snapshot plan.Snapshot) (*pb.StatsInfo, error) {
   125  	return nil, nil
   126  }
   127  
   128  func (*CompilerContext) GetStatsCache() *plan.StatsCache {
   129  	return nil
   130  }
   131  
   132  func (c *CompilerContext) GetSubscriptionMeta(dbName string, snapshot plan.Snapshot) (*plan.SubscriptionMeta, error) {
   133  	return nil, nil
   134  }
   135  
   136  func (c *CompilerContext) GetProcess() *process.Process {
   137  	proc := testutil.NewProcess()
   138  	proc.Ctx = context.Background()
   139  	return proc
   140  }
   141  
   142  func (c *CompilerContext) GetQueryResultMeta(uuid string) ([]*plan.ColDef, string, error) {
   143  	return nil, "", nil
   144  }
   145  
   146  func (c *CompilerContext) DatabaseExists(name string, snapshot plan.Snapshot) bool {
   147  	ctx := c.GetContext()
   148  	txnOpt := c.txnOp
   149  
   150  	if plan.IsSnapshotValid(&snapshot) && snapshot.TS.Less(c.txnOp.Txn().SnapshotTS) {
   151  		txnOpt = c.txnOp.CloneSnapshotOp(*snapshot.TS)
   152  
   153  		if snapshot.Tenant != nil {
   154  			ctx = context.WithValue(ctx, defines.TenantIDKey{}, snapshot.Tenant.TenantID)
   155  		}
   156  	}
   157  
   158  	_, err := c.engine.Database(
   159  		ctx,
   160  		name,
   161  		txnOpt,
   162  	)
   163  	return err == nil
   164  }
   165  
   166  func (c *CompilerContext) GetDatabaseId(dbName string, snapshot plan.Snapshot) (uint64, error) {
   167  	ctx := c.GetContext()
   168  	txnOpt := c.txnOp
   169  
   170  	if plan.IsSnapshotValid(&snapshot) && snapshot.TS.Less(c.txnOp.Txn().SnapshotTS) {
   171  		txnOpt = c.txnOp.CloneSnapshotOp(*snapshot.TS)
   172  
   173  		if snapshot.Tenant != nil {
   174  			ctx = context.WithValue(ctx, defines.TenantIDKey{}, snapshot.Tenant.TenantID)
   175  		}
   176  	}
   177  
   178  	database, err := c.engine.Database(ctx, dbName, txnOpt)
   179  	if err != nil {
   180  		return 0, err
   181  	}
   182  	databaseId, err := strconv.ParseUint(database.GetDatabaseId(ctx), 10, 64)
   183  	if err != nil {
   184  		return 0, moerr.NewInternalError(ctx, "The databaseid of '%s' is not a valid number", dbName)
   185  	}
   186  	return databaseId, nil
   187  }
   188  
   189  func (c *CompilerContext) DefaultDatabase() string {
   190  	return c.defaultDB
   191  }
   192  
   193  func (c *CompilerContext) GetPrimaryKeyDef(dbName string, tableName string, snapshot plan.Snapshot) (defs []*plan.ColDef) {
   194  	attrs, err := c.getTableAttrs(dbName, tableName, snapshot)
   195  	if err != nil {
   196  		panic(err)
   197  	}
   198  	for i, attr := range attrs {
   199  		if !attr.Primary {
   200  			continue
   201  		}
   202  		defs = append(defs, engineAttrToPlanColDef(i, attr))
   203  	}
   204  	return
   205  }
   206  
   207  func (*CompilerContext) GetRootSql() string {
   208  	return ""
   209  }
   210  
   211  func (*CompilerContext) GetUserName() string {
   212  	return "root"
   213  }
   214  
   215  func (c *CompilerContext) GetAccountId() (uint32, error) {
   216  	return defines.GetAccountId(c.ctx)
   217  }
   218  
   219  func (c *CompilerContext) GetContext() context.Context {
   220  	return c.ctx
   221  }
   222  
   223  func (c *CompilerContext) ResolveById(tableId uint64, snapshot plan.Snapshot) (objRef *plan.ObjectRef, tableDef *plan.TableDef) {
   224  	dbName, tableName, _ := c.engine.GetNameById(c.ctx, c.txnOp, tableId)
   225  	if dbName == "" || tableName == "" {
   226  		return nil, nil
   227  	}
   228  	return c.Resolve(dbName, tableName, snapshot)
   229  }
   230  
   231  func (c *CompilerContext) Resolve(schemaName string, tableName string, snapshot plan.Snapshot) (objRef *plan.ObjectRef, tableDef *plan.TableDef) {
   232  	if schemaName == "" {
   233  		schemaName = c.defaultDB
   234  	}
   235  
   236  	objRef = &plan.ObjectRef{
   237  		SchemaName: schemaName,
   238  		ObjName:    tableName,
   239  	}
   240  
   241  	tableDef = &plan.TableDef{
   242  		Name:   tableName,
   243  		DbName: schemaName,
   244  	}
   245  
   246  	attrs, err := c.getTableAttrs(schemaName, tableName, snapshot)
   247  	if err != nil {
   248  		return nil, nil
   249  	}
   250  
   251  	for i, attr := range attrs {
   252  		// return hidden columns for update or detete statement
   253  		//if attr.IsHidden {
   254  		//	switch e.stmt.(type) {
   255  		//	case *tree.Update, *tree.Delete:
   256  		//	default:
   257  		//		continue
   258  		//	}
   259  		//}
   260  		if attr.Primary {
   261  			tableDef.Pkey = &plan.PrimaryKeyDef{
   262  				Cols:        []uint64{uint64(i)},
   263  				PkeyColId:   uint64(i),
   264  				PkeyColName: attr.Name,
   265  				Names:       []string{attr.Name},
   266  			}
   267  		}
   268  		tableDef.Cols = append(tableDef.Cols, engineAttrToPlanColDef(i, attr))
   269  	}
   270  
   271  	//TODO properties
   272  	//TODO view
   273  
   274  	return
   275  }
   276  
   277  func (*CompilerContext) ResolveVariable(varName string, isSystemVar bool, isGlobalVar bool) (interface{}, error) {
   278  	return nil, nil
   279  }
   280  
   281  func (c *CompilerContext) getTableAttrs(dbName string, tableName string, snapshot plan.Snapshot) (attrs []*engine.Attribute, err error) {
   282  	ctx := c.GetContext()
   283  	txnOpt := c.txnOp
   284  
   285  	if plan.IsSnapshotValid(&snapshot) && snapshot.TS.Less(c.txnOp.Txn().SnapshotTS) {
   286  		txnOpt = c.txnOp.CloneSnapshotOp(*snapshot.TS)
   287  
   288  		if snapshot.Tenant != nil {
   289  			ctx = context.WithValue(ctx, defines.TenantIDKey{}, snapshot.Tenant.TenantID)
   290  		}
   291  	}
   292  
   293  	db, err := c.engine.Database(
   294  		ctx,
   295  		dbName,
   296  		txnOpt,
   297  	)
   298  	if err != nil {
   299  		return nil, err
   300  	}
   301  	table, err := db.Relation(
   302  		ctx,
   303  		tableName,
   304  		nil,
   305  	)
   306  	if err != nil {
   307  		return nil, err
   308  	}
   309  	defs, err := table.TableDefs(ctx)
   310  	if err != nil {
   311  		return nil, err
   312  	}
   313  	for _, def := range defs {
   314  		attr, ok := def.(*engine.AttributeDef)
   315  		if !ok {
   316  			continue
   317  		}
   318  		attrs = append(attrs, &attr.Attr)
   319  	}
   320  	return
   321  }
   322  
   323  func (c *CompilerContext) SetBuildingAlterView(yesOrNo bool, dbName, viewName string) {}
   324  func (c *CompilerContext) GetBuildingAlterView() (bool, string, string) {
   325  	return false, "", ""
   326  }
   327  
   328  func engineAttrToPlanColDef(idx int, attr *engine.Attribute) *plan.ColDef {
   329  	return &plan.ColDef{
   330  		ColId: uint64(attr.ID),
   331  		Name:  attr.Name,
   332  		Typ: plan.Type{
   333  			Id:          int32(attr.Type.Oid),
   334  			NotNullable: attr.Default != nil && !(attr.Default.NullAbility),
   335  			Width:       attr.Type.Width,
   336  			Scale:       attr.Type.Scale,
   337  			Enumvalues:  attr.EnumVlaues,
   338  		},
   339  		Default:   attr.Default,
   340  		Primary:   attr.Primary,
   341  		Pkidx:     int32(idx),
   342  		Comment:   attr.Comment,
   343  		ClusterBy: attr.ClusterBy,
   344  		Seqnum:    uint32(attr.Seqnum),
   345  	}
   346  }