github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/group_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/dialect" 21 "github.com/matrixorigin/matrixone/pkg/sql/parsers/tree" 22 ) 23 24 func NewGroupBinder(builder *QueryBuilder, ctx *BindContext) *GroupBinder { 25 b := &GroupBinder{} 26 b.sysCtx = builder.GetContext() 27 b.builder = builder 28 b.ctx = ctx 29 b.impl = b 30 31 return b 32 } 33 34 func (b *GroupBinder) BindExpr(astExpr tree.Expr, depth int32, isRoot bool) (*plan.Expr, error) { 35 expr, err := b.baseBindExpr(astExpr, depth, isRoot) 36 if err != nil { 37 return nil, err 38 } 39 40 if isRoot { 41 astStr := tree.String(astExpr, dialect.MYSQL) 42 if _, ok := b.ctx.groupByAst[astStr]; ok { 43 return nil, nil 44 } 45 46 b.ctx.groupByAst[astStr] = int32(len(b.ctx.groups)) 47 b.ctx.groups = append(b.ctx.groups, expr) 48 } 49 50 return expr, err 51 } 52 53 func (b *GroupBinder) BindColRef(astExpr *tree.UnresolvedName, depth int32, isRoot bool) (*plan.Expr, error) { 54 expr, err := b.baseBindColRef(astExpr, depth, isRoot) 55 if err != nil { 56 return nil, err 57 } 58 59 if _, ok := expr.Expr.(*plan.Expr_Corr); ok { 60 return nil, moerr.NewNYI(b.GetContext(), "correlated columns in GROUP BY clause") 61 } 62 63 return expr, nil 64 } 65 66 func (b *GroupBinder) BindAggFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) { 67 return nil, moerr.NewInvalidInput(b.GetContext(), "GROUP BY clause cannot contain aggregate functions") 68 } 69 70 func (b *GroupBinder) BindWinFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) { 71 return nil, moerr.NewInvalidInput(b.GetContext(), "GROUP BY clause cannot contain window functions") 72 } 73 74 func (b *GroupBinder) BindSubquery(astExpr *tree.Subquery, isRoot bool) (*plan.Expr, error) { 75 return nil, moerr.NewNYI(b.GetContext(), "subquery in GROUP BY clause") 76 }