github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/helper_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  	"strings"
    18  	"time"
    19  
    20  	. "github.com/whtcorpsinc/check"
    21  	"github.com/whtcorpsinc/BerolinaSQL/ast"
    22  	"github.com/whtcorpsinc/BerolinaSQL/charset"
    23  	"github.com/whtcorpsinc/BerolinaSQL/perceptron"
    24  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    25  	"github.com/whtcorpsinc/milevadb/stochastikctx/variable"
    26  	"github.com/whtcorpsinc/milevadb/types"
    27  	driver "github.com/whtcorpsinc/milevadb/types/BerolinaSQL_driver"
    28  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    29  )
    30  
    31  func (s *testExpressionSuite) TestGetTimeValue(c *C) {
    32  	ctx := mock.NewContext()
    33  	v, err := GetTimeValue(ctx, "2012-12-12 00:00:00", allegrosql.TypeTimestamp, types.MinFsp)
    34  	c.Assert(err, IsNil)
    35  
    36  	c.Assert(v.HoTT(), Equals, types.HoTTMysqlTime)
    37  	timeValue := v.GetMysqlTime()
    38  	c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")
    39  	stochastikVars := ctx.GetStochastikVars()
    40  	variable.SetStochastikSystemVar(stochastikVars, "timestamp", types.NewStringCauset(""))
    41  	v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", allegrosql.TypeTimestamp, types.MinFsp)
    42  	c.Assert(err, IsNil)
    43  
    44  	c.Assert(v.HoTT(), Equals, types.HoTTMysqlTime)
    45  	timeValue = v.GetMysqlTime()
    46  	c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")
    47  
    48  	variable.SetStochastikSystemVar(stochastikVars, "timestamp", types.NewStringCauset("0"))
    49  	v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", allegrosql.TypeTimestamp, types.MinFsp)
    50  	c.Assert(err, IsNil)
    51  
    52  	c.Assert(v.HoTT(), Equals, types.HoTTMysqlTime)
    53  	timeValue = v.GetMysqlTime()
    54  	c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")
    55  
    56  	variable.SetStochastikSystemVar(stochastikVars, "timestamp", types.Causet{})
    57  	v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", allegrosql.TypeTimestamp, types.MinFsp)
    58  	c.Assert(err, IsNil)
    59  
    60  	c.Assert(v.HoTT(), Equals, types.HoTTMysqlTime)
    61  	timeValue = v.GetMysqlTime()
    62  	c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")
    63  
    64  	variable.SetStochastikSystemVar(stochastikVars, "timestamp", types.NewStringCauset("1234"))
    65  
    66  	tbl := []struct {
    67  		Expr interface{}
    68  		Ret  interface{}
    69  	}{
    70  		{"2012-12-12 00:00:00", "2012-12-12 00:00:00"},
    71  		{ast.CurrentTimestamp, time.Unix(1234, 0).Format(types.TimeFormat)},
    72  		{types.ZeroDatetimeStr, "0000-00-00 00:00:00"},
    73  		{ast.NewValueExpr("2012-12-12 00:00:00", charset.CharsetUTF8MB4, charset.DefCauslationUTF8MB4), "2012-12-12 00:00:00"},
    74  		{ast.NewValueExpr(int64(0), "", ""), "0000-00-00 00:00:00"},
    75  		{ast.NewValueExpr(nil, "", ""), nil},
    76  		{&ast.FuncCallExpr{FnName: perceptron.NewCIStr(ast.CurrentTimestamp)}, strings.ToUpper(ast.CurrentTimestamp)},
    77  		//{&ast.UnaryOperationExpr{Op: opcode.Minus, V: ast.NewValueExpr(int64(0))}, "0000-00-00 00:00:00"},
    78  	}
    79  
    80  	for i, t := range tbl {
    81  		comment := Commentf("expr: %d", i)
    82  		v, err := GetTimeValue(ctx, t.Expr, allegrosql.TypeTimestamp, types.MinFsp)
    83  		c.Assert(err, IsNil)
    84  
    85  		switch v.HoTT() {
    86  		case types.HoTTMysqlTime:
    87  			c.Assert(v.GetMysqlTime().String(), DeepEquals, t.Ret, comment)
    88  		default:
    89  			c.Assert(v.GetValue(), DeepEquals, t.Ret, comment)
    90  		}
    91  	}
    92  
    93  	errTbl := []struct {
    94  		Expr interface{}
    95  	}{
    96  		{"2012-13-12 00:00:00"},
    97  		{ast.NewValueExpr("2012-13-12 00:00:00", charset.CharsetUTF8MB4, charset.DefCauslationUTF8MB4)},
    98  		{ast.NewValueExpr(int64(1), "", "")},
    99  		{&ast.FuncCallExpr{FnName: perceptron.NewCIStr("xxx")}},
   100  		//{&ast.UnaryOperationExpr{Op: opcode.Minus, V: ast.NewValueExpr(int64(1))}},
   101  	}
   102  
   103  	for _, t := range errTbl {
   104  		_, err := GetTimeValue(ctx, t.Expr, allegrosql.TypeTimestamp, types.MinFsp)
   105  		c.Assert(err, NotNil)
   106  	}
   107  }
   108  
   109  func (s *testExpressionSuite) TestIsCurrentTimestampExpr(c *C) {
   110  	buildTimestampFuncCallExpr := func(i int64) *ast.FuncCallExpr {
   111  		var args []ast.ExprNode
   112  		if i != 0 {
   113  			args = []ast.ExprNode{&driver.ValueExpr{Causet: types.NewIntCauset(i)}}
   114  		}
   115  		return &ast.FuncCallExpr{FnName: perceptron.NewCIStr("CURRENT_TIMESTAMP"), Args: args}
   116  	}
   117  
   118  	v := IsValidCurrentTimestampExpr(ast.NewValueExpr("abc", charset.CharsetUTF8MB4, charset.DefCauslationUTF8MB4), nil)
   119  	c.Assert(v, IsFalse)
   120  	v = IsValidCurrentTimestampExpr(buildTimestampFuncCallExpr(0), nil)
   121  	c.Assert(v, IsTrue)
   122  	v = IsValidCurrentTimestampExpr(buildTimestampFuncCallExpr(3), &types.FieldType{Decimal: 3})
   123  	c.Assert(v, IsTrue)
   124  	v = IsValidCurrentTimestampExpr(buildTimestampFuncCallExpr(1), &types.FieldType{Decimal: 3})
   125  	c.Assert(v, IsFalse)
   126  	v = IsValidCurrentTimestampExpr(buildTimestampFuncCallExpr(0), &types.FieldType{Decimal: 3})
   127  	c.Assert(v, IsFalse)
   128  	v = IsValidCurrentTimestampExpr(buildTimestampFuncCallExpr(2), &types.FieldType{Decimal: 0})
   129  	c.Assert(v, IsFalse)
   130  	v = IsValidCurrentTimestampExpr(buildTimestampFuncCallExpr(2), nil)
   131  	c.Assert(v, IsFalse)
   132  }
   133  
   134  func (s *testExpressionSuite) TestCurrentTimestampTimeZone(c *C) {
   135  	ctx := mock.NewContext()
   136  	stochastikVars := ctx.GetStochastikVars()
   137  
   138  	variable.SetStochastikSystemVar(stochastikVars, "timestamp", types.NewStringCauset("1234"))
   139  	variable.SetStochastikSystemVar(stochastikVars, "time_zone", types.NewStringCauset("+00:00"))
   140  	v, err := GetTimeValue(ctx, ast.CurrentTimestamp, allegrosql.TypeTimestamp, types.MinFsp)
   141  	c.Assert(err, IsNil)
   142  	c.Assert(v.GetMysqlTime(), DeepEquals, types.NewTime(
   143  		types.FromDate(1970, 1, 1, 0, 20, 34, 0),
   144  		allegrosql.TypeTimestamp, types.DefaultFsp))
   145  
   146  	// CurrentTimestamp from "timestamp" stochastik variable is based on UTC, so change timezone
   147  	// would get different value.
   148  	variable.SetStochastikSystemVar(stochastikVars, "time_zone", types.NewStringCauset("+08:00"))
   149  	v, err = GetTimeValue(ctx, ast.CurrentTimestamp, allegrosql.TypeTimestamp, types.MinFsp)
   150  	c.Assert(err, IsNil)
   151  	c.Assert(v.GetMysqlTime(), DeepEquals, types.NewTime(
   152  		types.FromDate(1970, 1, 1, 8, 20, 34, 0),
   153  		allegrosql.TypeTimestamp, types.DefaultFsp))
   154  }