github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/aggregation/agg_to_fidel.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 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 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package aggregation 15 16 import ( 17 "github.com/whtcorpsinc/BerolinaSQL/ast" 18 "github.com/whtcorpsinc/errors" 19 "github.com/whtcorpsinc/fidelpb/go-fidelpb" 20 "github.com/whtcorpsinc/milevadb/ekv" 21 "github.com/whtcorpsinc/milevadb/memex" 22 "github.com/whtcorpsinc/milevadb/stochastikctx" 23 "github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx" 24 "github.com/whtcorpsinc/milevadb/types" 25 ) 26 27 28 func AggFuncToPBExpr(sc *stmtctx.StatementContext, client ekv.Client, aggFunc *AggFuncDesc) *fidelpb.Expr { 29 30 if len(aggFunc.OrderByItems) > 0 { 31 return nil 32 } 33 pc := memex.NewPBConverter(client, sc) 34 var tp fidelpb.ExprType 35 switch aggFunc.Name { 36 case ast.AggFuncCount: 37 tp = fidelpb.ExprType_Count 38 case ast.AggFuncApproxCountDistinct: 39 tp = fidelpb.ExprType_ApproxCountDistinct 40 case ast.AggFuncFirstEvent: 41 tp = fidelpb.ExprType_First 42 case ast.AggFuncGroupConcat: 43 tp = fidelpb.ExprType_GroupConcat 44 case ast.AggFuncMax: 45 tp = fidelpb.ExprType_Max 46 case ast.AggFuncMin: 47 tp = fidelpb.ExprType_Min 48 case ast.AggFuncSum: 49 tp = fidelpb.ExprType_Sum 50 case ast.AggFuncAvg: 51 tp = fidelpb.ExprType_Avg 52 case ast.AggFuncBitOr: 53 tp = fidelpb.ExprType_Agg_BitOr 54 case ast.AggFuncBitXor: 55 tp = fidelpb.ExprType_Agg_BitXor 56 case ast.AggFuncBitAnd: 57 tp = fidelpb.ExprType_Agg_BitAnd 58 case ast.AggFuncVarPop: 59 tp = fidelpb.ExprType_VarPop 60 case ast.AggFuncJsonObjectAgg: 61 tp = fidelpb.ExprType_JsonObjectAgg 62 case ast.AggFuncStddevPop: 63 tp = fidelpb.ExprType_StddevPop 64 case ast.AggFuncVarSamp: 65 tp = fidelpb.ExprType_VarSamp 66 case ast.AggFuncStddevSamp: 67 tp = fidelpb.ExprType_StddevSamp 68 } 69 if !client.IsRequestTypeSupported(ekv.ReqTypeSelect, int64(tp)) { 70 return nil 71 } 72 73 children := make([]*fidelpb.Expr, 0, len(aggFunc.Args)) 74 for _, arg := range aggFunc.Args { 75 pbArg := pc.ExprToPB(arg) 76 if pbArg == nil { 77 return nil 78 } 79 children = append(children, pbArg) 80 } 81 return &fidelpb.Expr{Tp: tp, Children: children, FieldType: memex.ToPBFieldType(aggFunc.RetTp)} 82 } 83 84 // PBExprToAggFuncDesc converts pb to aggregate function. 85 func PBExprToAggFuncDesc(ctx stochastikctx.Context, aggFunc *fidelpb.Expr, fieldTps []*types.FieldType) (*AggFuncDesc, error) { 86 var name string 87 switch aggFunc.Tp { 88 case fidelpb.ExprType_Count: 89 name = ast.AggFuncCount 90 case fidelpb.ExprType_ApproxCountDistinct: 91 name = ast.AggFuncApproxCountDistinct 92 case fidelpb.ExprType_First: 93 name = ast.AggFuncFirstEvent 94 case fidelpb.ExprType_GroupConcat: 95 name = ast.AggFuncGroupConcat 96 case fidelpb.ExprType_Max: 97 name = ast.AggFuncMax 98 case fidelpb.ExprType_Min: 99 name = ast.AggFuncMin 100 case fidelpb.ExprType_Sum: 101 name = ast.AggFuncSum 102 case fidelpb.ExprType_Avg: 103 name = ast.AggFuncAvg 104 case fidelpb.ExprType_Agg_BitOr: 105 name = ast.AggFuncBitOr 106 case fidelpb.ExprType_Agg_BitXor: 107 name = ast.AggFuncBitXor 108 case fidelpb.ExprType_Agg_BitAnd: 109 name = ast.AggFuncBitAnd 110 default: 111 return nil, errors.Errorf("unknown aggregation function type: %v", aggFunc.Tp) 112 } 113 114 args, err := memex.PBToExprs(aggFunc.Children, fieldTps, ctx.GetStochastikVars().StmtCtx) 115 if err != nil { 116 return nil, err 117 } 118 base := baseFuncDesc{ 119 Name: name, 120 Args: args, 121 RetTp: memex.FieldTypeFromPB(aggFunc.FieldType), 122 } 123 base.WrapCastForAggArgs(ctx) 124 return &AggFuncDesc{ 125 baseFuncDesc: base, 126 Mode: Partial1Mode, 127 HasDistinct: false, 128 }, nil 129 }