github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/default_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 "github.com/matrixorigin/matrixone/pkg/common/moerr" 21 "github.com/matrixorigin/matrixone/pkg/pb/plan" 22 "github.com/matrixorigin/matrixone/pkg/sql/parsers/tree" 23 "strings" 24 ) 25 26 func NewDefaultBinder(sysCtx context.Context, builder *QueryBuilder, ctx *BindContext, typ Type, cols []string) *DefaultBinder { 27 b := &DefaultBinder{typ: typ, cols: cols} 28 b.sysCtx = sysCtx 29 b.builder = builder 30 b.ctx = ctx 31 b.impl = b 32 33 return b 34 } 35 36 func (b *DefaultBinder) BindExpr(astExpr tree.Expr, depth int32, isRoot bool) (*plan.Expr, error) { 37 return b.baseBindExpr(astExpr, depth, isRoot) 38 } 39 40 func (b *DefaultBinder) BindColRef(astExpr *tree.UnresolvedName, depth int32, isRoot bool) (*plan.Expr, error) { 41 if b.cols != nil { 42 return b.bindColRef(astExpr, depth, isRoot) 43 } 44 return b.baseBindColRef(astExpr, depth, isRoot) 45 } 46 47 func (b *DefaultBinder) bindColRef(astExpr *tree.UnresolvedName, _ int32, _ bool) (expr *plan.Expr, err error) { 48 col := strings.ToLower(astExpr.Parts[0]) 49 idx := -1 50 for i, c := range b.cols { 51 if c == col { 52 idx = i 53 break 54 } 55 } 56 if idx == -1 { 57 err = moerr.NewInvalidInput(b.GetContext(), "column '%s' does not exist", col) 58 return 59 } 60 expr = &plan.Expr{ 61 Typ: b.typ, 62 } 63 expr.Expr = &plan.Expr_Col{ 64 Col: &plan.ColRef{ 65 RelPos: 0, 66 ColPos: int32(idx), 67 }, 68 } 69 return 70 } 71 72 func (b *DefaultBinder) BindAggFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) { 73 return nil, moerr.NewInvalidInput(b.GetContext(), "cannot bind agregate functions '%s'", funcName) 74 } 75 76 func (b *DefaultBinder) BindWinFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) { 77 return nil, moerr.NewInvalidInput(b.GetContext(), "cannot bind window functions '%s'", funcName) 78 } 79 80 func (b *DefaultBinder) BindSubquery(astExpr *tree.Subquery, isRoot bool) (*plan.Expr, error) { 81 return nil, moerr.NewNYI(b.GetContext(), "subquery in JOIN condition") 82 } 83 84 func (b *DefaultBinder) BindTimeWindowFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) { 85 return nil, moerr.NewInvalidInput(b.GetContext(), "cannot bind time window functions '%s'", funcName) 86 }