github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/scalar_function_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/ast"
    21  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    22  	"github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx"
    23  	"github.com/whtcorpsinc/milevadb/types"
    24  )
    25  
    26  func (s *testEvaluatorSuite) TestScalarFunction(c *C) {
    27  	a := &DeferredCauset{
    28  		UniqueID: 1,
    29  		RetType:  types.NewFieldType(allegrosql.TypeDouble),
    30  	}
    31  	sc := &stmtctx.StatementContext{TimeZone: time.Local}
    32  	sf := newFunction(ast.LT, a, NewOne())
    33  	res, err := sf.MarshalJSON()
    34  	c.Assert(err, IsNil)
    35  	c.Assert(res, DeepEquals, []byte{0x22, 0x6c, 0x74, 0x28, 0x43, 0x6f, 0x6c, 0x75, 0x6d, 0x6e, 0x23, 0x31, 0x2c, 0x20, 0x31, 0x29, 0x22})
    36  	c.Assert(sf.IsCorrelated(), IsFalse)
    37  	c.Assert(sf.ConstItem(s.ctx.GetStochastikVars().StmtCtx), IsFalse)
    38  	c.Assert(sf.Decorrelate(nil).Equal(s.ctx, sf), IsTrue)
    39  	c.Assert(sf.HashCode(sc), DeepEquals, []byte{0x3, 0x4, 0x6c, 0x74, 0x1, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x5, 0xbf, 0xf0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0})
    40  
    41  	sf = NewValuesFunc(s.ctx, 0, types.NewFieldType(allegrosql.TypeLonglong))
    42  	newSf, ok := sf.Clone().(*ScalarFunction)
    43  	c.Assert(ok, IsTrue)
    44  	c.Assert(newSf.FuncName.O, Equals, "values")
    45  	c.Assert(newSf.RetType.Tp, Equals, allegrosql.TypeLonglong)
    46  	_, ok = newSf.Function.(*builtinValuesIntSig)
    47  	c.Assert(ok, IsTrue)
    48  }
    49  
    50  func (s *testEvaluatorSuite) TestScalarFuncs2Exprs(c *C) {
    51  	a := &DeferredCauset{
    52  		UniqueID: 1,
    53  		RetType:  types.NewFieldType(allegrosql.TypeDouble),
    54  	}
    55  	sf0, _ := newFunction(ast.LT, a, NewZero()).(*ScalarFunction)
    56  	sf1, _ := newFunction(ast.LT, a, NewOne()).(*ScalarFunction)
    57  
    58  	funcs := []*ScalarFunction{sf0, sf1}
    59  	exprs := ScalarFuncs2Exprs(funcs)
    60  	for i := range exprs {
    61  		c.Assert(exprs[i].Equal(s.ctx, funcs[i]), IsTrue)
    62  	}
    63  }