github.com/matrixorigin/matrixone@v0.7.0/pkg/sql/plan/projection_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 NewProjectionBinder(builder *QueryBuilder, ctx *BindContext, havingBinder *HavingBinder) *ProjectionBinder {
    25  	b := &ProjectionBinder{
    26  		havingBinder: havingBinder,
    27  	}
    28  	b.sysCtx = builder.GetContext()
    29  	b.builder = builder
    30  	b.ctx = ctx
    31  	b.impl = b
    32  
    33  	return b
    34  }
    35  
    36  func (b *ProjectionBinder) BindExpr(astExpr tree.Expr, depth int32, isRoot bool) (*plan.Expr, error) {
    37  	astStr := tree.String(astExpr, dialect.MYSQL)
    38  
    39  	if colPos, ok := b.ctx.groupByAst[astStr]; ok {
    40  		return &plan.Expr{
    41  			Typ: b.ctx.groups[colPos].Typ,
    42  			Expr: &plan.Expr_Col{
    43  				Col: &plan.ColRef{
    44  					RelPos: b.ctx.groupTag,
    45  					ColPos: colPos,
    46  				},
    47  			},
    48  		}, nil
    49  	}
    50  
    51  	if colPos, ok := b.ctx.aggregateByAst[astStr]; ok {
    52  		return &plan.Expr{
    53  			Typ: b.ctx.aggregates[colPos].Typ,
    54  			Expr: &plan.Expr_Col{
    55  				Col: &plan.ColRef{
    56  					RelPos: b.ctx.aggregateTag,
    57  					ColPos: colPos,
    58  				},
    59  			},
    60  		}, nil
    61  	}
    62  
    63  	return b.baseBindExpr(astExpr, depth, isRoot)
    64  }
    65  
    66  func (b *ProjectionBinder) BindColRef(astExpr *tree.UnresolvedName, depth int32, isRoot bool) (*plan.Expr, error) {
    67  	return b.baseBindColRef(astExpr, depth, isRoot)
    68  }
    69  
    70  func (b *ProjectionBinder) BindAggFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) {
    71  	return b.havingBinder.BindAggFunc(funcName, astExpr, depth, isRoot)
    72  }
    73  
    74  func (b *ProjectionBinder) BindWinFunc(funcName string, astExpr *tree.FuncExpr, depth int32, isRoot bool) (*plan.Expr, error) {
    75  	return nil, moerr.NewNYI(b.GetContext(), "window functions")
    76  }
    77  
    78  func (b *ProjectionBinder) BindSubquery(astExpr *tree.Subquery, isRoot bool) (*plan.Expr, error) {
    79  	return b.baseBindSubquery(astExpr, isRoot)
    80  }