github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/distsql_builtin_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 memex
    15  
    16  import (
    17  	"time"
    18  
    19  	. "github.com/whtcorpsinc/check"
    20  	"github.com/whtcorpsinc/BerolinaSQL/charset"
    21  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    22  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    23  	"github.com/whtcorpsinc/milevadb/types"
    24  	"github.com/whtcorpsinc/milevadb/types/json"
    25  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    26  	"github.com/whtcorpsinc/milevadb/soliton/codec"
    27  	"github.com/whtcorpsinc/milevadb/soliton/defCauslate"
    28  	"github.com/whtcorpsinc/fidelpb/go-fidelpb"
    29  )
    30  
    31  var _ = Suite(&testEvalSuite{})
    32  var _ = SerialSuites(&testEvalSerialSuite{})
    33  
    34  type testEvalSuite struct {
    35  	defCausID int64
    36  }
    37  
    38  type testEvalSerialSuite struct {
    39  }
    40  
    41  func (s *testEvalSerialSuite) TestPBToExprWithNewDefCauslation(c *C) {
    42  	sc := new(stmtctx.StatementContext)
    43  	fieldTps := make([]*types.FieldType, 1)
    44  
    45  	cases := []struct {
    46  		name    string
    47  		expName string
    48  		id      int32
    49  		pbID    int32
    50  	}{
    51  		{"utf8_general_ci", "utf8_general_ci", 33, 33},
    52  		{"UTF8MB4_BIN", "utf8mb4_bin", 46, 46},
    53  		{"utf8mb4_bin", "utf8mb4_bin", 46, 46},
    54  		{"utf8mb4_general_ci", "utf8mb4_general_ci", 45, 45},
    55  		{"", "utf8mb4_bin", 46, 46},
    56  		{"some_error_defCauslation", "utf8mb4_bin", 46, 46},
    57  		{"utf8_unicode_ci", "utf8_unicode_ci", 192, 192},
    58  		{"utf8mb4_unicode_ci", "utf8mb4_unicode_ci", 224, 224},
    59  	}
    60  
    61  	for _, cs := range cases {
    62  		ft := types.NewFieldType(allegrosql.TypeString)
    63  		ft.DefCauslate = cs.name
    64  		expr := new(fidelpb.Expr)
    65  		expr.Tp = fidelpb.ExprType_String
    66  		expr.FieldType = toPBFieldType(ft)
    67  		c.Assert(expr.FieldType.DefCauslate, Equals, cs.pbID)
    68  
    69  		e, err := PBToExpr(expr, fieldTps, sc)
    70  		c.Assert(err, IsNil)
    71  		cons, ok := e.(*Constant)
    72  		c.Assert(ok, IsTrue)
    73  		c.Assert(cons.Value.DefCauslation(), Equals, cs.expName)
    74  	}
    75  	defCauslate.SetNewDefCauslationEnabledForTest(true)
    76  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
    77  
    78  	for _, cs := range cases {
    79  		ft := types.NewFieldType(allegrosql.TypeString)
    80  		ft.DefCauslate = cs.name
    81  		expr := new(fidelpb.Expr)
    82  		expr.Tp = fidelpb.ExprType_String
    83  		expr.FieldType = toPBFieldType(ft)
    84  		c.Assert(expr.FieldType.DefCauslate, Equals, -cs.pbID)
    85  
    86  		e, err := PBToExpr(expr, fieldTps, sc)
    87  		c.Assert(err, IsNil)
    88  		cons, ok := e.(*Constant)
    89  		c.Assert(ok, IsTrue)
    90  		c.Assert(cons.Value.DefCauslation(), Equals, cs.expName)
    91  	}
    92  }
    93  
    94  func (s *testEvalSuite) SetUpSuite(c *C) {
    95  	s.defCausID = 0
    96  }
    97  
    98  func (s *testEvalSuite) allocDefCausID() int64 {
    99  	s.defCausID++
   100  	return s.defCausID
   101  }
   102  
   103  func (s *testEvalSuite) TearDownTest(c *C) {
   104  	s.defCausID = 0
   105  }
   106  
   107  func (s *testEvalSuite) TestPBToExpr(c *C) {
   108  	sc := new(stmtctx.StatementContext)
   109  	fieldTps := make([]*types.FieldType, 1)
   110  	ds := []types.Causet{types.NewIntCauset(1), types.NewUintCauset(1), types.NewFloat64Causet(1),
   111  		types.NewDecimalCauset(newMyDecimal(c, "1")), types.NewDurationCauset(newDuration(time.Second))}
   112  
   113  	for _, d := range ds {
   114  		expr := datumExpr(c, d)
   115  		expr.Val = expr.Val[:len(expr.Val)/2]
   116  		_, err := PBToExpr(expr, fieldTps, sc)
   117  		c.Assert(err, NotNil)
   118  	}
   119  
   120  	expr := &fidelpb.Expr{
   121  		Tp: fidelpb.ExprType_ScalarFunc,
   122  		Children: []*fidelpb.Expr{
   123  			{
   124  				Tp: fidelpb.ExprType_ValueList,
   125  			},
   126  		},
   127  	}
   128  	_, err := PBToExpr(expr, fieldTps, sc)
   129  	c.Assert(err, IsNil)
   130  
   131  	val := make([]byte, 0, 32)
   132  	val = codec.EncodeInt(val, 1)
   133  	expr = &fidelpb.Expr{
   134  		Tp: fidelpb.ExprType_ScalarFunc,
   135  		Children: []*fidelpb.Expr{
   136  			{
   137  				Tp:  fidelpb.ExprType_ValueList,
   138  				Val: val[:len(val)/2],
   139  			},
   140  		},
   141  	}
   142  	_, err = PBToExpr(expr, fieldTps, sc)
   143  	c.Assert(err, NotNil)
   144  
   145  	expr = &fidelpb.Expr{
   146  		Tp: fidelpb.ExprType_ScalarFunc,
   147  		Children: []*fidelpb.Expr{
   148  			{
   149  				Tp:  fidelpb.ExprType_ValueList,
   150  				Val: val,
   151  			},
   152  		},
   153  		Sig:       fidelpb.ScalarFuncSig_AbsInt,
   154  		FieldType: ToPBFieldType(newIntFieldType()),
   155  	}
   156  	_, err = PBToExpr(expr, fieldTps, sc)
   157  	c.Assert(err, NotNil)
   158  }
   159  
   160  // TestEval test expr.Eval().
   161  func (s *testEvalSuite) TestEval(c *C) {
   162  	event := chunk.MutEventFromCausets([]types.Causet{types.NewCauset(100)}).ToEvent()
   163  	fieldTps := make([]*types.FieldType, 1)
   164  	fieldTps[0] = types.NewFieldType(allegrosql.TypeLonglong)
   165  	tests := []struct {
   166  		expr   *fidelpb.Expr
   167  		result types.Causet
   168  	}{
   169  		// Causets.
   170  		{
   171  			datumExpr(c, types.NewFloat32Causet(1.1)),
   172  			types.NewFloat32Causet(1.1),
   173  		},
   174  		{
   175  			datumExpr(c, types.NewFloat64Causet(1.1)),
   176  			types.NewFloat64Causet(1.1),
   177  		},
   178  		{
   179  			datumExpr(c, types.NewIntCauset(1)),
   180  			types.NewIntCauset(1),
   181  		},
   182  		{
   183  			datumExpr(c, types.NewUintCauset(1)),
   184  			types.NewUintCauset(1),
   185  		},
   186  		{
   187  			datumExpr(c, types.NewBytesCauset([]byte("abc"))),
   188  			types.NewBytesCauset([]byte("abc")),
   189  		},
   190  		{
   191  			datumExpr(c, types.NewStringCauset("abc")),
   192  			types.NewStringCauset("abc"),
   193  		},
   194  		{
   195  			datumExpr(c, types.Causet{}),
   196  			types.Causet{},
   197  		},
   198  		{
   199  			datumExpr(c, types.NewDurationCauset(types.Duration{Duration: time.Hour})),
   200  			types.NewDurationCauset(types.Duration{Duration: time.Hour}),
   201  		},
   202  		{
   203  			datumExpr(c, types.NewDecimalCauset(types.NewDecFromFloatForTest(1.1))),
   204  			types.NewDecimalCauset(types.NewDecFromFloatForTest(1.1)),
   205  		},
   206  		// DeferredCausets.
   207  		{
   208  			defCausumnExpr(0),
   209  			types.NewIntCauset(100),
   210  		},
   211  		// Scalar Functions.
   212  		{
   213  			scalarFunctionExpr(fidelpb.ScalarFuncSig_JsonDepthSig,
   214  				toPBFieldType(newIntFieldType()),
   215  				jsonCausetExpr(c, `true`),
   216  			),
   217  			types.NewIntCauset(1),
   218  		},
   219  		{
   220  			scalarFunctionExpr(fidelpb.ScalarFuncSig_JsonDepthSig,
   221  				toPBFieldType(newIntFieldType()),
   222  				jsonCausetExpr(c, `[10, {"a": 20}]`),
   223  			),
   224  			types.NewIntCauset(3),
   225  		},
   226  		{
   227  			scalarFunctionExpr(fidelpb.ScalarFuncSig_JsonStorageSizeSig,
   228  				toPBFieldType(newIntFieldType()),
   229  				jsonCausetExpr(c, `[{"a":{"a":1},"b":2}]`),
   230  			),
   231  			types.NewIntCauset(25),
   232  		},
   233  		{
   234  			scalarFunctionExpr(fidelpb.ScalarFuncSig_JsonSearchSig,
   235  				toPBFieldType(newJSONFieldType()),
   236  				jsonCausetExpr(c, `["abc", [{"k": "10"}, "def"], {"x":"abc"}, {"y":"bcd"}]`),
   237  				datumExpr(c, types.NewBytesCauset([]byte(`all`))),
   238  				datumExpr(c, types.NewBytesCauset([]byte(`10`))),
   239  				datumExpr(c, types.NewBytesCauset([]byte(`\`))),
   240  				datumExpr(c, types.NewBytesCauset([]byte(`$**.k`))),
   241  			),
   242  			newJSONCauset(c, `"$[1][0].k"`),
   243  		},
   244  		{
   245  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastIntAsInt,
   246  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(2333))),
   247  			types.NewIntCauset(2333),
   248  		},
   249  		{
   250  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastRealAsInt,
   251  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewFloat64Causet(2333))),
   252  			types.NewIntCauset(2333),
   253  		},
   254  		{
   255  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastStringAsInt,
   256  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewStringCauset("2333"))),
   257  			types.NewIntCauset(2333),
   258  		},
   259  		{
   260  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastDecimalAsInt,
   261  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2333")))),
   262  			types.NewIntCauset(2333),
   263  		},
   264  		{
   265  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastIntAsReal,
   266  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewIntCauset(2333))),
   267  			types.NewFloat64Causet(2333),
   268  		},
   269  		{
   270  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastRealAsReal,
   271  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewFloat64Causet(2333))),
   272  			types.NewFloat64Causet(2333),
   273  		},
   274  		{
   275  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastStringAsReal,
   276  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewStringCauset("2333"))),
   277  			types.NewFloat64Causet(2333),
   278  		},
   279  		{
   280  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastDecimalAsReal,
   281  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2333")))),
   282  			types.NewFloat64Causet(2333),
   283  		},
   284  		{
   285  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastStringAsString,
   286  				toPBFieldType(newStringFieldType()), datumExpr(c, types.NewStringCauset("2333"))),
   287  			types.NewStringCauset("2333"),
   288  		},
   289  		{
   290  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastIntAsString,
   291  				toPBFieldType(newStringFieldType()), datumExpr(c, types.NewIntCauset(2333))),
   292  			types.NewStringCauset("2333"),
   293  		},
   294  		{
   295  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastRealAsString,
   296  				toPBFieldType(newStringFieldType()), datumExpr(c, types.NewFloat64Causet(2333))),
   297  			types.NewStringCauset("2333"),
   298  		},
   299  		{
   300  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastDecimalAsString,
   301  				toPBFieldType(newStringFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2333")))),
   302  			types.NewStringCauset("2333"),
   303  		},
   304  		{
   305  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastDecimalAsDecimal,
   306  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2333")))),
   307  			types.NewDecimalCauset(newMyDecimal(c, "2333")),
   308  		},
   309  		{
   310  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastIntAsDecimal,
   311  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewIntCauset(2333))),
   312  			types.NewDecimalCauset(newMyDecimal(c, "2333")),
   313  		},
   314  		{
   315  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastRealAsDecimal,
   316  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewFloat64Causet(2333))),
   317  			types.NewDecimalCauset(newMyDecimal(c, "2333")),
   318  		},
   319  		{
   320  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastStringAsDecimal,
   321  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewStringCauset("2333"))),
   322  			types.NewDecimalCauset(newMyDecimal(c, "2333")),
   323  		},
   324  		{
   325  			scalarFunctionExpr(fidelpb.ScalarFuncSig_GEInt,
   326  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(2)), datumExpr(c, types.NewIntCauset(1))),
   327  			types.NewIntCauset(1),
   328  		},
   329  		{
   330  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LEInt,
   331  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(2))),
   332  			types.NewIntCauset(1),
   333  		},
   334  		{
   335  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NEInt,
   336  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(2))),
   337  			types.NewIntCauset(1),
   338  		},
   339  		{
   340  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NullEQInt,
   341  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewCauset(nil))),
   342  			types.NewIntCauset(1),
   343  		},
   344  		{
   345  			scalarFunctionExpr(fidelpb.ScalarFuncSig_GEReal,
   346  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewFloat64Causet(2)), datumExpr(c, types.NewFloat64Causet(1))),
   347  			types.NewIntCauset(1),
   348  		},
   349  		{
   350  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LEReal,
   351  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewFloat64Causet(1)), datumExpr(c, types.NewFloat64Causet(2))),
   352  			types.NewIntCauset(1),
   353  		},
   354  		{
   355  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LTReal,
   356  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewFloat64Causet(1)), datumExpr(c, types.NewFloat64Causet(2))),
   357  			types.NewIntCauset(1),
   358  		},
   359  		{
   360  			scalarFunctionExpr(fidelpb.ScalarFuncSig_EQReal,
   361  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewFloat64Causet(1)), datumExpr(c, types.NewFloat64Causet(1))),
   362  			types.NewIntCauset(1),
   363  		},
   364  		{
   365  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NEReal,
   366  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewFloat64Causet(1)), datumExpr(c, types.NewFloat64Causet(2))),
   367  			types.NewIntCauset(1),
   368  		},
   369  		{
   370  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NullEQReal,
   371  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewCauset(nil))),
   372  			types.NewIntCauset(1),
   373  		},
   374  		{
   375  			scalarFunctionExpr(fidelpb.ScalarFuncSig_GEDecimal,
   376  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2"))), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   377  			types.NewIntCauset(1),
   378  		},
   379  		{
   380  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LEDecimal,
   381  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1"))), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2")))),
   382  			types.NewIntCauset(1),
   383  		},
   384  		{
   385  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LTDecimal,
   386  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1"))), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2")))),
   387  			types.NewIntCauset(1),
   388  		},
   389  		{
   390  			scalarFunctionExpr(fidelpb.ScalarFuncSig_EQDecimal,
   391  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1"))), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   392  			types.NewIntCauset(1),
   393  		},
   394  		{
   395  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NEDecimal,
   396  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1"))), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2")))),
   397  			types.NewIntCauset(1),
   398  		},
   399  		{
   400  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NullEQDecimal,
   401  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewCauset(nil))),
   402  			types.NewIntCauset(1),
   403  		},
   404  		{
   405  			scalarFunctionExpr(fidelpb.ScalarFuncSig_GEDuration,
   406  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDurationCauset(newDuration(time.Second*2))), datumExpr(c, types.NewDurationCauset(newDuration(time.Second)))),
   407  			types.NewIntCauset(1),
   408  		},
   409  		{
   410  			scalarFunctionExpr(fidelpb.ScalarFuncSig_GTDuration,
   411  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDurationCauset(newDuration(time.Second*2))), datumExpr(c, types.NewDurationCauset(newDuration(time.Second)))),
   412  			types.NewIntCauset(1),
   413  		},
   414  		{
   415  			scalarFunctionExpr(fidelpb.ScalarFuncSig_EQDuration,
   416  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDurationCauset(newDuration(time.Second))), datumExpr(c, types.NewDurationCauset(newDuration(time.Second)))),
   417  			types.NewIntCauset(1),
   418  		},
   419  		{
   420  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LEDuration,
   421  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDurationCauset(newDuration(time.Second))), datumExpr(c, types.NewDurationCauset(newDuration(time.Second*2)))),
   422  			types.NewIntCauset(1),
   423  		},
   424  		{
   425  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NEDuration,
   426  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDurationCauset(newDuration(time.Second))), datumExpr(c, types.NewDurationCauset(newDuration(time.Second*2)))),
   427  			types.NewIntCauset(1),
   428  		},
   429  		{
   430  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NullEQDuration,
   431  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewCauset(nil))),
   432  			types.NewIntCauset(1),
   433  		},
   434  		{
   435  			scalarFunctionExpr(fidelpb.ScalarFuncSig_GEString,
   436  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewStringCauset("1")), datumExpr(c, types.NewStringCauset("1"))),
   437  			types.NewIntCauset(1),
   438  		},
   439  		{
   440  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LEString,
   441  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewStringCauset("1")), datumExpr(c, types.NewStringCauset("1"))),
   442  			types.NewIntCauset(1),
   443  		},
   444  		{
   445  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NEString,
   446  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewStringCauset("2")), datumExpr(c, types.NewStringCauset("1"))),
   447  			types.NewIntCauset(1),
   448  		},
   449  		{
   450  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NullEQString,
   451  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewCauset(nil))),
   452  			types.NewIntCauset(1),
   453  		},
   454  		{
   455  			scalarFunctionExpr(fidelpb.ScalarFuncSig_GTJson,
   456  				toPBFieldType(newIntFieldType()), jsonCausetExpr(c, "[2]"), jsonCausetExpr(c, "[1]")),
   457  			types.NewIntCauset(1),
   458  		},
   459  		{
   460  			scalarFunctionExpr(fidelpb.ScalarFuncSig_GEJson,
   461  				toPBFieldType(newIntFieldType()), jsonCausetExpr(c, "[2]"), jsonCausetExpr(c, "[1]")),
   462  			types.NewIntCauset(1),
   463  		},
   464  		{
   465  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LTJson,
   466  				toPBFieldType(newIntFieldType()), jsonCausetExpr(c, "[1]"), jsonCausetExpr(c, "[2]")),
   467  			types.NewIntCauset(1),
   468  		},
   469  		{
   470  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LEJson,
   471  				toPBFieldType(newIntFieldType()), jsonCausetExpr(c, "[1]"), jsonCausetExpr(c, "[2]")),
   472  			types.NewIntCauset(1),
   473  		},
   474  		{
   475  			scalarFunctionExpr(fidelpb.ScalarFuncSig_EQJson,
   476  				toPBFieldType(newIntFieldType()), jsonCausetExpr(c, "[1]"), jsonCausetExpr(c, "[1]")),
   477  			types.NewIntCauset(1),
   478  		},
   479  		{
   480  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NEJson,
   481  				toPBFieldType(newIntFieldType()), jsonCausetExpr(c, "[1]"), jsonCausetExpr(c, "[2]")),
   482  			types.NewIntCauset(1),
   483  		},
   484  		{
   485  			scalarFunctionExpr(fidelpb.ScalarFuncSig_NullEQJson,
   486  				toPBFieldType(newIntFieldType()), jsonCausetExpr(c, "[1]"), jsonCausetExpr(c, "[1]")),
   487  			types.NewIntCauset(1),
   488  		},
   489  		{
   490  			scalarFunctionExpr(fidelpb.ScalarFuncSig_DecimalIsNull,
   491  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(nil))),
   492  			types.NewIntCauset(1),
   493  		},
   494  		{
   495  			scalarFunctionExpr(fidelpb.ScalarFuncSig_DurationIsNull,
   496  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(nil))),
   497  			types.NewIntCauset(1),
   498  		},
   499  		{
   500  			scalarFunctionExpr(fidelpb.ScalarFuncSig_RealIsNull,
   501  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(nil))),
   502  			types.NewIntCauset(1),
   503  		},
   504  		{
   505  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LeftShift,
   506  				ToPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(1)), datumExpr(c, types.NewIntCauset(1))),
   507  			types.NewIntCauset(2),
   508  		},
   509  		{
   510  			scalarFunctionExpr(fidelpb.ScalarFuncSig_AbsInt,
   511  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(-1))),
   512  			types.NewIntCauset(1),
   513  		},
   514  		{
   515  			scalarFunctionExpr(fidelpb.ScalarFuncSig_AbsUInt,
   516  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewUintCauset(1))),
   517  			types.NewIntCauset(1),
   518  		},
   519  		{
   520  			scalarFunctionExpr(fidelpb.ScalarFuncSig_AbsReal,
   521  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewFloat64Causet(-1.23))),
   522  			types.NewFloat64Causet(1.23),
   523  		},
   524  		{
   525  			scalarFunctionExpr(fidelpb.ScalarFuncSig_AbsDecimal,
   526  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "-1.23")))),
   527  			types.NewDecimalCauset(newMyDecimal(c, "1.23")),
   528  		},
   529  		{
   530  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LogicalAnd,
   531  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(1))),
   532  			types.NewIntCauset(1),
   533  		},
   534  		{
   535  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LogicalOr,
   536  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(0))),
   537  			types.NewIntCauset(1),
   538  		},
   539  		{
   540  			scalarFunctionExpr(fidelpb.ScalarFuncSig_LogicalXor,
   541  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(0))),
   542  			types.NewIntCauset(1),
   543  		},
   544  		{
   545  			scalarFunctionExpr(fidelpb.ScalarFuncSig_BitAndSig,
   546  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(1))),
   547  			types.NewIntCauset(1),
   548  		},
   549  		{
   550  			scalarFunctionExpr(fidelpb.ScalarFuncSig_BitOrSig,
   551  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(0))),
   552  			types.NewIntCauset(1),
   553  		},
   554  		{
   555  			scalarFunctionExpr(fidelpb.ScalarFuncSig_BitXorSig,
   556  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(0))),
   557  			types.NewIntCauset(1),
   558  		},
   559  		{
   560  			scalarFunctionExpr(fidelpb.ScalarFuncSig_BitNegSig,
   561  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(0))),
   562  			types.NewIntCauset(-1),
   563  		},
   564  		{
   565  			scalarFunctionExpr(fidelpb.ScalarFuncSig_InReal,
   566  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewFloat64Causet(1)), datumExpr(c, types.NewFloat64Causet(1))),
   567  			types.NewIntCauset(1),
   568  		},
   569  		{
   570  			scalarFunctionExpr(fidelpb.ScalarFuncSig_InDecimal,
   571  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1"))), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   572  			types.NewIntCauset(1),
   573  		},
   574  		{
   575  			scalarFunctionExpr(fidelpb.ScalarFuncSig_InString,
   576  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewStringCauset("1")), datumExpr(c, types.NewStringCauset("1"))),
   577  			types.NewIntCauset(1),
   578  		},
   579  		//{
   580  		//	scalarFunctionExpr(fidelpb.ScalarFuncSig_InTime,
   581  		//		toPBFieldType(newIntFieldType()), datumExpr(c, types.NewTimeCauset(types.ZeroDate)), datumExpr(c, types.NewTimeCauset(types.ZeroDate))),
   582  		//	types.NewIntCauset(1),
   583  		//},
   584  		{
   585  			scalarFunctionExpr(fidelpb.ScalarFuncSig_InDuration,
   586  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDurationCauset(newDuration(time.Second))), datumExpr(c, types.NewDurationCauset(newDuration(time.Second)))),
   587  			types.NewIntCauset(1),
   588  		},
   589  		{
   590  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfNullInt,
   591  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewIntCauset(1))),
   592  			types.NewIntCauset(1),
   593  		},
   594  		{
   595  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfInt,
   596  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(2))),
   597  			types.NewIntCauset(2),
   598  		},
   599  		{
   600  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfNullReal,
   601  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewFloat64Causet(1))),
   602  			types.NewFloat64Causet(1),
   603  		},
   604  		{
   605  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfReal,
   606  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewFloat64Causet(1)), datumExpr(c, types.NewFloat64Causet(2))),
   607  			types.NewFloat64Causet(2),
   608  		},
   609  		{
   610  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfNullDecimal,
   611  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   612  			types.NewDecimalCauset(newMyDecimal(c, "1")),
   613  		},
   614  		{
   615  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfDecimal,
   616  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2")))),
   617  			types.NewDecimalCauset(newMyDecimal(c, "2")),
   618  		},
   619  		{
   620  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfNullString,
   621  				toPBFieldType(newStringFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewStringCauset("1"))),
   622  			types.NewStringCauset("1"),
   623  		},
   624  		{
   625  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfString,
   626  				toPBFieldType(newStringFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewStringCauset("2"))),
   627  			types.NewStringCauset("2"),
   628  		},
   629  		{
   630  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfNullDuration,
   631  				toPBFieldType(newDurFieldType()), datumExpr(c, types.NewCauset(nil)), datumExpr(c, types.NewDurationCauset(newDuration(time.Second)))),
   632  			types.NewDurationCauset(newDuration(time.Second)),
   633  		},
   634  		{
   635  			scalarFunctionExpr(fidelpb.ScalarFuncSig_IfDuration,
   636  				toPBFieldType(newDurFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewDurationCauset(newDuration(time.Second*2)))),
   637  			types.NewDurationCauset(newDuration(time.Second * 2)),
   638  		},
   639  
   640  		{
   641  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastIntAsDuration,
   642  				toPBFieldType(newDurFieldType()), datumExpr(c, types.NewIntCauset(1))),
   643  			types.NewDurationCauset(newDuration(time.Second * 1)),
   644  		},
   645  		{
   646  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastRealAsDuration,
   647  				toPBFieldType(newDurFieldType()), datumExpr(c, types.NewFloat64Causet(1))),
   648  			types.NewDurationCauset(newDuration(time.Second * 1)),
   649  		},
   650  		{
   651  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastDecimalAsDuration,
   652  				toPBFieldType(newDurFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   653  			types.NewDurationCauset(newDuration(time.Second * 1)),
   654  		},
   655  		{
   656  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastDurationAsDuration,
   657  				toPBFieldType(newDurFieldType()), datumExpr(c, types.NewDurationCauset(newDuration(time.Second*1)))),
   658  			types.NewDurationCauset(newDuration(time.Second * 1)),
   659  		},
   660  		{
   661  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastStringAsDuration,
   662  				toPBFieldType(newDurFieldType()), datumExpr(c, types.NewStringCauset("1"))),
   663  			types.NewDurationCauset(newDuration(time.Second * 1)),
   664  		},
   665  		{
   666  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastTimeAsTime,
   667  				toPBFieldType(newDateFieldType()), datumExpr(c, types.NewTimeCauset(newDateTime(c, "2000-01-01")))),
   668  			types.NewTimeCauset(newDateTime(c, "2000-01-01")),
   669  		},
   670  		{
   671  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastIntAsTime,
   672  				toPBFieldType(newDateFieldType()), datumExpr(c, types.NewIntCauset(20000101))),
   673  			types.NewTimeCauset(newDateTime(c, "2000-01-01")),
   674  		},
   675  		{
   676  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastRealAsTime,
   677  				toPBFieldType(newDateFieldType()), datumExpr(c, types.NewFloat64Causet(20000101))),
   678  			types.NewTimeCauset(newDateTime(c, "2000-01-01")),
   679  		},
   680  		{
   681  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastDecimalAsTime,
   682  				toPBFieldType(newDateFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "20000101")))),
   683  			types.NewTimeCauset(newDateTime(c, "2000-01-01")),
   684  		},
   685  		{
   686  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CastStringAsTime,
   687  				toPBFieldType(newDateFieldType()), datumExpr(c, types.NewStringCauset("20000101"))),
   688  			types.NewTimeCauset(newDateTime(c, "2000-01-01")),
   689  		},
   690  		{
   691  			scalarFunctionExpr(fidelpb.ScalarFuncSig_PlusInt,
   692  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(2))),
   693  			types.NewIntCauset(3),
   694  		},
   695  		{
   696  			scalarFunctionExpr(fidelpb.ScalarFuncSig_PlusDecimal,
   697  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1"))), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2")))),
   698  			types.NewDecimalCauset(newMyDecimal(c, "3")),
   699  		},
   700  		{
   701  			scalarFunctionExpr(fidelpb.ScalarFuncSig_PlusReal,
   702  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewFloat64Causet(1)), datumExpr(c, types.NewFloat64Causet(2))),
   703  			types.NewFloat64Causet(3),
   704  		},
   705  		{
   706  			scalarFunctionExpr(fidelpb.ScalarFuncSig_MinusInt,
   707  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(2))),
   708  			types.NewIntCauset(-1),
   709  		},
   710  		{
   711  			scalarFunctionExpr(fidelpb.ScalarFuncSig_MinusDecimal,
   712  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1"))), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2")))),
   713  			types.NewDecimalCauset(newMyDecimal(c, "-1")),
   714  		},
   715  		{
   716  			scalarFunctionExpr(fidelpb.ScalarFuncSig_MinusReal,
   717  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewFloat64Causet(1)), datumExpr(c, types.NewFloat64Causet(2))),
   718  			types.NewFloat64Causet(-1),
   719  		},
   720  		{
   721  			scalarFunctionExpr(fidelpb.ScalarFuncSig_MultiplyInt,
   722  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1)), datumExpr(c, types.NewIntCauset(2))),
   723  			types.NewIntCauset(2),
   724  		},
   725  		{
   726  			scalarFunctionExpr(fidelpb.ScalarFuncSig_MultiplyDecimal,
   727  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1"))), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "2")))),
   728  			types.NewDecimalCauset(newMyDecimal(c, "2")),
   729  		},
   730  		{
   731  			scalarFunctionExpr(fidelpb.ScalarFuncSig_MultiplyReal,
   732  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewFloat64Causet(1)), datumExpr(c, types.NewFloat64Causet(2))),
   733  			types.NewFloat64Causet(2),
   734  		},
   735  		{
   736  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CeilIntToInt,
   737  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1))),
   738  			types.NewIntCauset(1),
   739  		},
   740  		{
   741  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CeilIntToDec,
   742  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewIntCauset(1))),
   743  			types.NewDecimalCauset(newMyDecimal(c, "1")),
   744  		},
   745  		{
   746  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CeilDecToInt,
   747  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   748  			types.NewIntCauset(1),
   749  		},
   750  		{
   751  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CeilReal,
   752  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewFloat64Causet(1))),
   753  			types.NewFloat64Causet(1),
   754  		},
   755  		{
   756  			scalarFunctionExpr(fidelpb.ScalarFuncSig_FloorIntToInt,
   757  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1))),
   758  			types.NewIntCauset(1),
   759  		},
   760  		{
   761  			scalarFunctionExpr(fidelpb.ScalarFuncSig_FloorIntToDec,
   762  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewIntCauset(1))),
   763  			types.NewDecimalCauset(newMyDecimal(c, "1")),
   764  		},
   765  		{
   766  			scalarFunctionExpr(fidelpb.ScalarFuncSig_FloorDecToInt,
   767  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   768  			types.NewIntCauset(1),
   769  		},
   770  		{
   771  			scalarFunctionExpr(fidelpb.ScalarFuncSig_FloorReal,
   772  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewFloat64Causet(1))),
   773  			types.NewFloat64Causet(1),
   774  		},
   775  		{
   776  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CoalesceInt,
   777  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewIntCauset(1))),
   778  			types.NewIntCauset(1),
   779  		},
   780  		{
   781  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CoalesceReal,
   782  				toPBFieldType(newRealFieldType()), datumExpr(c, types.NewFloat64Causet(1))),
   783  			types.NewFloat64Causet(1),
   784  		},
   785  		{
   786  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CoalesceDecimal,
   787  				toPBFieldType(newDecimalFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   788  			types.NewDecimalCauset(newMyDecimal(c, "1")),
   789  		},
   790  		{
   791  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CoalesceString,
   792  				toPBFieldType(newStringFieldType()), datumExpr(c, types.NewStringCauset("1"))),
   793  			types.NewStringCauset("1"),
   794  		},
   795  		{
   796  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CoalesceDuration,
   797  				toPBFieldType(newDurFieldType()), datumExpr(c, types.NewDurationCauset(newDuration(time.Second)))),
   798  			types.NewDurationCauset(newDuration(time.Second)),
   799  		},
   800  		{
   801  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CoalesceTime,
   802  				toPBFieldType(newDateFieldType()), datumExpr(c, types.NewTimeCauset(newDateTime(c, "2000-01-01")))),
   803  			types.NewTimeCauset(newDateTime(c, "2000-01-01")),
   804  		},
   805  		{
   806  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CaseWhenInt,
   807  				toPBFieldType(newIntFieldType())),
   808  			types.NewCauset(nil),
   809  		},
   810  		{
   811  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CaseWhenReal,
   812  				toPBFieldType(newRealFieldType())),
   813  			types.NewCauset(nil),
   814  		},
   815  		{
   816  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CaseWhenDecimal,
   817  				toPBFieldType(newDecimalFieldType())),
   818  			types.NewCauset(nil),
   819  		},
   820  		{
   821  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CaseWhenDuration,
   822  				toPBFieldType(newDurFieldType())),
   823  			types.NewCauset(nil),
   824  		},
   825  		{
   826  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CaseWhenTime,
   827  				toPBFieldType(newDateFieldType())),
   828  			types.NewCauset(nil),
   829  		},
   830  		{
   831  			scalarFunctionExpr(fidelpb.ScalarFuncSig_CaseWhenJson,
   832  				toPBFieldType(newJSONFieldType())),
   833  			types.NewCauset(nil),
   834  		},
   835  		{
   836  			scalarFunctionExpr(fidelpb.ScalarFuncSig_RealIsFalse,
   837  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewFloat64Causet(1))),
   838  			types.NewIntCauset(0),
   839  		},
   840  		{
   841  			scalarFunctionExpr(fidelpb.ScalarFuncSig_DecimalIsFalse,
   842  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   843  			types.NewIntCauset(0),
   844  		},
   845  		{
   846  			scalarFunctionExpr(fidelpb.ScalarFuncSig_RealIsTrue,
   847  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewFloat64Causet(1))),
   848  			types.NewIntCauset(1),
   849  		},
   850  		{
   851  			scalarFunctionExpr(fidelpb.ScalarFuncSig_DecimalIsTrue,
   852  				toPBFieldType(newIntFieldType()), datumExpr(c, types.NewDecimalCauset(newMyDecimal(c, "1")))),
   853  			types.NewIntCauset(1),
   854  		},
   855  	}
   856  	sc := new(stmtctx.StatementContext)
   857  	for _, tt := range tests {
   858  		expr, err := PBToExpr(tt.expr, fieldTps, sc)
   859  		c.Assert(err, IsNil)
   860  		result, err := expr.Eval(event)
   861  		c.Assert(err, IsNil)
   862  		c.Assert(result.HoTT(), Equals, tt.result.HoTT())
   863  		cmp, err := result.CompareCauset(sc, &tt.result)
   864  		c.Assert(err, IsNil)
   865  		c.Assert(cmp, Equals, 0)
   866  	}
   867  }
   868  
   869  func datumExpr(c *C, d types.Causet) *fidelpb.Expr {
   870  	expr := new(fidelpb.Expr)
   871  	switch d.HoTT() {
   872  	case types.HoTTInt64:
   873  		expr.Tp = fidelpb.ExprType_Int64
   874  		expr.Val = codec.EncodeInt(nil, d.GetInt64())
   875  	case types.HoTTUint64:
   876  		expr.Tp = fidelpb.ExprType_Uint64
   877  		expr.Val = codec.EncodeUint(nil, d.GetUint64())
   878  	case types.HoTTString:
   879  		expr.Tp = fidelpb.ExprType_String
   880  		expr.FieldType = toPBFieldType(types.NewFieldType(allegrosql.TypeString))
   881  		expr.Val = d.GetBytes()
   882  	case types.HoTTBytes:
   883  		expr.Tp = fidelpb.ExprType_Bytes
   884  		expr.Val = d.GetBytes()
   885  	case types.HoTTFloat32:
   886  		expr.Tp = fidelpb.ExprType_Float32
   887  		expr.Val = codec.EncodeFloat(nil, d.GetFloat64())
   888  	case types.HoTTFloat64:
   889  		expr.Tp = fidelpb.ExprType_Float64
   890  		expr.Val = codec.EncodeFloat(nil, d.GetFloat64())
   891  	case types.HoTTMysqlDuration:
   892  		expr.Tp = fidelpb.ExprType_MysqlDuration
   893  		expr.Val = codec.EncodeInt(nil, int64(d.GetMysqlDuration().Duration))
   894  	case types.HoTTMysqlDecimal:
   895  		expr.Tp = fidelpb.ExprType_MysqlDecimal
   896  		var err error
   897  		expr.Val, err = codec.EncodeDecimal(nil, d.GetMysqlDecimal(), d.Length(), d.Frac())
   898  		c.Assert(err, IsNil)
   899  	case types.HoTTMysqlJSON:
   900  		expr.Tp = fidelpb.ExprType_MysqlJson
   901  		var err error
   902  		expr.Val = make([]byte, 0, 1024)
   903  		expr.Val, err = codec.EncodeValue(nil, expr.Val, d)
   904  		c.Assert(err, IsNil)
   905  	case types.HoTTMysqlTime:
   906  		expr.Tp = fidelpb.ExprType_MysqlTime
   907  		var err error
   908  		expr.Val, err = codec.EncodeMyALLEGROSQLTime(nil, d.GetMysqlTime(), allegrosql.TypeUnspecified, nil)
   909  		c.Assert(err, IsNil)
   910  		expr.FieldType = ToPBFieldType(newDateFieldType())
   911  	default:
   912  		expr.Tp = fidelpb.ExprType_Null
   913  	}
   914  	return expr
   915  }
   916  
   917  func newJSONCauset(c *C, s string) (d types.Causet) {
   918  	j, err := json.ParseBinaryFromString(s)
   919  	c.Assert(err, IsNil)
   920  	d.SetMysqlJSON(j)
   921  	return d
   922  }
   923  
   924  func jsonCausetExpr(c *C, s string) *fidelpb.Expr {
   925  	return datumExpr(c, newJSONCauset(c, s))
   926  }
   927  
   928  func defCausumnExpr(defCausumnID int64) *fidelpb.Expr {
   929  	expr := new(fidelpb.Expr)
   930  	expr.Tp = fidelpb.ExprType_DeferredCausetRef
   931  	expr.Val = codec.EncodeInt(nil, defCausumnID)
   932  	return expr
   933  }
   934  
   935  // toPBFieldType converts *types.FieldType to *fidelpb.FieldType.
   936  func toPBFieldType(ft *types.FieldType) *fidelpb.FieldType {
   937  	return &fidelpb.FieldType{
   938  		Tp:      int32(ft.Tp),
   939  		Flag:    uint32(ft.Flag),
   940  		Flen:    int32(ft.Flen),
   941  		Decimal: int32(ft.Decimal),
   942  		Charset: ft.Charset,
   943  		DefCauslate: defCauslationToProto(ft.DefCauslate),
   944  	}
   945  }
   946  
   947  func newMyDecimal(c *C, s string) *types.MyDecimal {
   948  	d := new(types.MyDecimal)
   949  	c.Assert(d.FromString([]byte(s)), IsNil)
   950  	return d
   951  }
   952  
   953  func newDuration(dur time.Duration) types.Duration {
   954  	return types.Duration{
   955  		Duration: dur,
   956  		Fsp:      types.DefaultFsp,
   957  	}
   958  }
   959  
   960  func newDateTime(c *C, s string) types.Time {
   961  	t, err := types.ParseDate(nil, s)
   962  	c.Assert(err, IsNil)
   963  	return t
   964  }
   965  
   966  func newDateFieldType() *types.FieldType {
   967  	return &types.FieldType{
   968  		Tp: allegrosql.TypeDate,
   969  	}
   970  }
   971  
   972  func newIntFieldType() *types.FieldType {
   973  	return &types.FieldType{
   974  		Tp:      allegrosql.TypeLonglong,
   975  		Flen:    allegrosql.MaxIntWidth,
   976  		Decimal: 0,
   977  		Flag:    allegrosql.BinaryFlag,
   978  	}
   979  }
   980  
   981  func newDurFieldType() *types.FieldType {
   982  	return &types.FieldType{
   983  		Tp:      allegrosql.TypeDuration,
   984  		Decimal: int(types.DefaultFsp),
   985  	}
   986  }
   987  
   988  func newStringFieldType() *types.FieldType {
   989  	return &types.FieldType{
   990  		Tp:   allegrosql.TypeVarString,
   991  		Flen: types.UnspecifiedLength,
   992  	}
   993  }
   994  
   995  func newRealFieldType() *types.FieldType {
   996  	return &types.FieldType{
   997  		Tp:   allegrosql.TypeFloat,
   998  		Flen: types.UnspecifiedLength,
   999  	}
  1000  }
  1001  
  1002  func newDecimalFieldType() *types.FieldType {
  1003  	return &types.FieldType{
  1004  		Tp:   allegrosql.TypeNewDecimal,
  1005  		Flen: types.UnspecifiedLength,
  1006  	}
  1007  }
  1008  
  1009  func newJSONFieldType() *types.FieldType {
  1010  	return &types.FieldType{
  1011  		Tp:      allegrosql.TypeJSON,
  1012  		Flen:    types.UnspecifiedLength,
  1013  		Decimal: 0,
  1014  		Charset: charset.CharsetBin,
  1015  		DefCauslate: charset.DefCauslationBin,
  1016  	}
  1017  }
  1018  
  1019  func newFloatFieldType() *types.FieldType {
  1020  	return &types.FieldType{
  1021  		Tp:      allegrosql.TypeFloat,
  1022  		Flen:    types.UnspecifiedLength,
  1023  		Decimal: 0,
  1024  		Charset: charset.CharsetBin,
  1025  		DefCauslate: charset.DefCauslationBin,
  1026  	}
  1027  }
  1028  
  1029  func newBinaryLiteralFieldType() *types.FieldType {
  1030  	return &types.FieldType{
  1031  		Tp:      allegrosql.TypeBit,
  1032  		Flen:    types.UnspecifiedLength,
  1033  		Decimal: 0,
  1034  		Charset: charset.CharsetBin,
  1035  		DefCauslate: charset.DefCauslationBin,
  1036  	}
  1037  }
  1038  
  1039  func newBlobFieldType() *types.FieldType {
  1040  	return &types.FieldType{
  1041  		Tp:      allegrosql.TypeBlob,
  1042  		Flen:    types.UnspecifiedLength,
  1043  		Decimal: 0,
  1044  		Charset: charset.CharsetBin,
  1045  		DefCauslate: charset.DefCauslationBin,
  1046  	}
  1047  }
  1048  
  1049  func newEnumFieldType() *types.FieldType {
  1050  	return &types.FieldType{
  1051  		Tp:      allegrosql.TypeEnum,
  1052  		Flen:    types.UnspecifiedLength,
  1053  		Decimal: 0,
  1054  		Charset: charset.CharsetBin,
  1055  		DefCauslate: charset.DefCauslationBin,
  1056  	}
  1057  }
  1058  
  1059  func scalarFunctionExpr(sigCode fidelpb.ScalarFuncSig, retType *fidelpb.FieldType, args ...*fidelpb.Expr) *fidelpb.Expr {
  1060  	return &fidelpb.Expr{
  1061  		Tp:        fidelpb.ExprType_ScalarFunc,
  1062  		Sig:       sigCode,
  1063  		Children:  args,
  1064  		FieldType: retType,
  1065  	}
  1066  }