github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/set_binder.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 plan
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    19  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    20  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    21  	"strings"
    22  )
    23  
    24  func NewSetVarBinder(builder *QueryBuilder, ctx *BindContext) *SetBinder {
    25  	p := &SetBinder{}
    26  	p.sysCtx = builder.GetContext()
    27  	p.builder = builder
    28  	p.ctx = ctx
    29  	p.impl = p
    30  	return p
    31  }
    32  
    33  var funcNeedsTxn = map[string]int{
    34  	"nextval":          0,
    35  	"setval":           0,
    36  	"mo_ctl":           0,
    37  	"mo_table_rows":    0,
    38  	"mo_table_size":    0,
    39  	"mo_table_col_max": 0,
    40  	"mo_table_col_min": 0,
    41  }
    42  
    43  // BindExpr prohibits functions in set expr
    44  func (s *SetBinder) BindExpr(expr tree.Expr, i int32, b bool) (*plan.Expr, error) {
    45  	switch exprImpl := expr.(type) {
    46  	case *tree.FuncExpr:
    47  		//supported function
    48  		funcRef, ok := exprImpl.Func.FunctionReference.(*tree.UnresolvedName)
    49  		if !ok {
    50  			return nil, moerr.NewNYI(s.GetContext(), "invalid function expr '%v'", exprImpl)
    51  		}
    52  		funcName := strings.ToLower(funcRef.Parts[0])
    53  		if _, ok := funcNeedsTxn[funcName]; ok {
    54  			return nil, moerr.NewInvalidInput(s.GetContext(), "function %s is not allowed in the set expression", funcName)
    55  		}
    56  	}
    57  	return s.baseBindExpr(expr, i, b)
    58  }
    59  
    60  func (s *SetBinder) BindColRef(name *tree.UnresolvedName, i int32, b bool) (*plan.Expr, error) {
    61  	return s.baseBindColRef(name, i, b)
    62  }
    63  
    64  func (s *SetBinder) BindAggFunc(_ string, expr *tree.FuncExpr, i int32, b bool) (*plan.Expr, error) {
    65  	return nil, moerr.NewSyntaxError(s.GetContext(), "aggregate functions not allowed in partition clause")
    66  }
    67  
    68  func (s *SetBinder) BindWinFunc(_ string, expr *tree.FuncExpr, i int32, b bool) (*plan.Expr, error) {
    69  	return nil, moerr.NewSyntaxError(s.GetContext(), "window functions not allowed in partition clause")
    70  }
    71  
    72  func (s *SetBinder) BindSubquery(subquery *tree.Subquery, b bool) (*plan.Expr, error) {
    73  	return s.baseBindSubquery(subquery, b)
    74  }
    75  
    76  func (s *SetBinder) BindTimeWindowFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) {
    77  	return nil, moerr.NewInvalidInput(s.GetContext(), "cannot bind time window functions '%s'", funcName)
    78  }