github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/aggregation/agg_to_fidel_test.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  	"encoding/json"
    18  	"testing"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/BerolinaSQL"
    22  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    23  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    24  	"github.com/whtcorpsinc/milevadb/memex"
    25  	"github.com/whtcorpsinc/milevadb/stochastikctx"
    26  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    27  	"github.com/whtcorpsinc/milevadb/types"
    28  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    29  )
    30  
    31  var _ = Suite(&testEvaluatorSuite{})
    32  
    33  func TestT(t *testing.T) {
    34  	CustomVerboseFlag = true
    35  	TestingT(t)
    36  }
    37  
    38  type dataGen4Expr2PbTest struct {
    39  }
    40  
    41  func (dg *dataGen4Expr2PbTest) genDeferredCauset(tp byte, id int64) *memex.DeferredCauset {
    42  	return &memex.DeferredCauset{
    43  		RetType: types.NewFieldType(tp),
    44  		ID:      id,
    45  		Index:   int(id),
    46  	}
    47  }
    48  
    49  type testEvaluatorSuite struct {
    50  	*BerolinaSQL.BerolinaSQL
    51  	ctx stochastikctx.Context
    52  }
    53  
    54  func (s *testEvaluatorSuite) SetUpSuite(c *C) {
    55  	s.BerolinaSQL = BerolinaSQL.New()
    56  	s.ctx = mock.NewContext()
    57  }
    58  
    59  func (s *testEvaluatorSuite) TearDownSuite(c *C) {
    60  }
    61  
    62  func (s *testEvaluatorSuite) TestAggFunc2Pb(c *C) {
    63  	sc := new(stmtctx.StatementContext)
    64  	client := new(mock.Client)
    65  	dg := new(dataGen4Expr2PbTest)
    66  
    67  	funcNames := []string{ast.AggFuncSum, ast.AggFuncCount, ast.AggFuncAvg, ast.AggFuncGroupConcat, ast.AggFuncMax, ast.AggFuncMin, ast.AggFuncFirstEvent}
    68  	funcTypes := []*types.FieldType{
    69  		types.NewFieldType(allegrosql.TypeDouble),
    70  		types.NewFieldType(allegrosql.TypeLonglong),
    71  		types.NewFieldType(allegrosql.TypeDouble),
    72  		types.NewFieldType(allegrosql.TypeVarchar),
    73  		types.NewFieldType(allegrosql.TypeDouble),
    74  		types.NewFieldType(allegrosql.TypeDouble),
    75  		types.NewFieldType(allegrosql.TypeDouble),
    76  	}
    77  
    78  	jsons := []string{
    79  		`{"tp":3002,"children":[{"tp":201,"val":"gAAAAAAAAAE=","sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}],"sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}`,
    80  		`{"tp":3001,"children":[{"tp":201,"val":"gAAAAAAAAAE=","sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}],"sig":0,"field_type":{"tp":8,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}`,
    81  		`{"tp":3003,"children":[{"tp":201,"val":"gAAAAAAAAAE=","sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}],"sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}`,
    82  		"null",
    83  		`{"tp":3005,"children":[{"tp":201,"val":"gAAAAAAAAAE=","sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}],"sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}`,
    84  		`{"tp":3004,"children":[{"tp":201,"val":"gAAAAAAAAAE=","sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}],"sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}`,
    85  		`{"tp":3006,"children":[{"tp":201,"val":"gAAAAAAAAAE=","sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}],"sig":0,"field_type":{"tp":5,"flag":0,"flen":-1,"decimal":-1,"defCauslate":63,"charset":""}}`,
    86  	}
    87  	for i, funcName := range funcNames {
    88  		for _, hasDistinct := range []bool{true, false} {
    89  			args := []memex.Expression{dg.genDeferredCauset(allegrosql.TypeDouble, 1)}
    90  			aggFunc, err := NewAggFuncDesc(s.ctx, funcName, args, hasDistinct)
    91  			c.Assert(err, IsNil)
    92  			aggFunc.RetTp = funcTypes[i]
    93  			pbExpr := AggFuncToPBExpr(sc, client, aggFunc)
    94  			js, err := json.Marshal(pbExpr)
    95  			c.Assert(err, IsNil)
    96  			c.Assert(string(js), Equals, jsons[i])
    97  		}
    98  	}
    99  }