github.com/matrixorigin/matrixone@v1.2.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 isNullExpr(expr) {
    41  		return nil, moerr.NewInternalErrorNoCtx("Invalid GROUP BY NULL")
    42  	}
    43  
    44  	if isRoot {
    45  		astStr := tree.String(astExpr, dialect.MYSQL)
    46  		if _, ok := b.ctx.groupByAst[astStr]; ok {
    47  			return nil, nil
    48  		}
    49  
    50  		b.ctx.groupByAst[astStr] = int32(len(b.ctx.groups))
    51  		b.ctx.groups = append(b.ctx.groups, expr)
    52  	}
    53  
    54  	return expr, err
    55  }
    56  
    57  func (b *GroupBinder) BindColRef(astExpr *tree.UnresolvedName, depth int32, isRoot bool) (*plan.Expr, error) {
    58  	expr, err := b.baseBindColRef(astExpr, depth, isRoot)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	if _, ok := expr.Expr.(*plan.Expr_Corr); ok {
    64  		return nil, moerr.NewNYI(b.GetContext(), "correlated columns in GROUP BY clause")
    65  	}
    66  
    67  	return expr, nil
    68  }
    69  
    70  func (b *GroupBinder) BindAggFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) {
    71  	return nil, moerr.NewInvalidInput(b.GetContext(), "GROUP BY clause cannot contain aggregate functions")
    72  }
    73  
    74  func (b *GroupBinder) BindWinFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) {
    75  	return nil, moerr.NewInvalidInput(b.GetContext(), "GROUP BY clause cannot contain window functions")
    76  }
    77  
    78  func (b *GroupBinder) BindSubquery(astExpr *tree.Subquery, isRoot bool) (*plan.Expr, error) {
    79  	return nil, moerr.NewNYI(b.GetContext(), "subquery in GROUP BY clause")
    80  }
    81  
    82  func (b *GroupBinder) BindTimeWindowFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) {
    83  	return nil, moerr.NewInvalidInput(b.GetContext(), "cannot bind time window functions '%s'", funcName)
    84  }