github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/update_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  	"context"
    19  
    20  	"strings"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    23  	"github.com/matrixorigin/matrixone/pkg/pb/plan"
    24  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    25  )
    26  
    27  // use for on duplicate key update clause:  eg: insert into t1 values(1,1),(2,2) on duplicate key update a = a + abs(b), b = values(b)-2
    28  func NewUpdateBinder(sysCtx context.Context, builder *QueryBuilder, ctx *BindContext, cols []*ColDef) *UpdateBinder {
    29  	b := &UpdateBinder{cols: cols}
    30  	b.sysCtx = sysCtx
    31  	b.builder = builder
    32  	b.ctx = ctx
    33  	b.impl = b
    34  
    35  	return b
    36  }
    37  
    38  func (b *UpdateBinder) BindExpr(astExpr tree.Expr, depth int32, isRoot bool) (*plan.Expr, error) {
    39  	return b.baseBindExpr(astExpr, depth, isRoot)
    40  }
    41  
    42  func (b *UpdateBinder) BindColRef(astExpr *tree.UnresolvedName, depth int32, isRoot bool) (*plan.Expr, error) {
    43  	if b.cols != nil {
    44  		return b.bindColRef(astExpr, depth, isRoot)
    45  	}
    46  	return b.baseBindColRef(astExpr, depth, isRoot)
    47  }
    48  
    49  func (b *UpdateBinder) bindColRef(astExpr *tree.UnresolvedName, _ int32, _ bool) (expr *plan.Expr, err error) {
    50  	col := strings.ToLower(astExpr.Parts[0])
    51  	idx := -1
    52  	var typ *Type
    53  	for i, c := range b.cols {
    54  		if c.Name == col {
    55  			idx = i
    56  			typ = &c.Typ
    57  			break
    58  		}
    59  	}
    60  	if idx == -1 {
    61  		err = moerr.NewInvalidInput(b.GetContext(), "column '%s' does not exist", col)
    62  		return
    63  	}
    64  	expr = &plan.Expr{
    65  		Typ: *typ,
    66  	}
    67  	// a = a +1 ,  a = values(a) + 1.  'a' actually have different idx. you need replace to exact idx before eval expr.
    68  	// binder only check the column name exists.
    69  	expr.Expr = &plan.Expr_Col{
    70  		Col: &plan.ColRef{
    71  			RelPos: 0,
    72  			ColPos: int32(idx),
    73  			Name:   col,
    74  		},
    75  	}
    76  	return
    77  }
    78  
    79  func (b *UpdateBinder) BindAggFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) {
    80  	return nil, moerr.NewInvalidInput(b.GetContext(), "cannot bind agregate functions '%s'", funcName)
    81  }
    82  
    83  func (b *UpdateBinder) BindWinFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) {
    84  	return nil, moerr.NewInvalidInput(b.GetContext(), "cannot bind window functions '%s'", funcName)
    85  }
    86  
    87  func (b *UpdateBinder) BindSubquery(astExpr *tree.Subquery, isRoot bool) (*plan.Expr, error) {
    88  	return nil, moerr.NewNYI(b.GetContext(), "subquery in JOIN condition")
    89  }
    90  
    91  func (b *UpdateBinder) BindTimeWindowFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) {
    92  	return nil, moerr.NewInvalidInput(b.GetContext(), "cannot bind time window functions '%s'", funcName)
    93  }