github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/builtin_control_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 "errors" 18 "time" 19 20 . "github.com/whtcorpsinc/check" 21 "github.com/whtcorpsinc/BerolinaSQL/ast" 22 "github.com/whtcorpsinc/milevadb/types" 23 "github.com/whtcorpsinc/milevadb/soliton/chunk" 24 "github.com/whtcorpsinc/milevadb/soliton/solitonutil" 25 ) 26 27 func (s *testEvaluatorSuite) TestCaseWhen(c *C) { 28 tbl := []struct { 29 Arg []interface{} 30 Ret interface{} 31 }{ 32 {[]interface{}{true, 1, true, 2, 3}, 1}, 33 {[]interface{}{false, 1, true, 2, 3}, 2}, 34 {[]interface{}{nil, 1, true, 2, 3}, 2}, 35 {[]interface{}{false, 1, false, 2, 3}, 3}, 36 {[]interface{}{nil, 1, nil, 2, 3}, 3}, 37 {[]interface{}{false, 1, nil, 2, 3}, 3}, 38 {[]interface{}{nil, 1, false, 2, 3}, 3}, 39 {[]interface{}{1, jsonInt.GetMysqlJSON(), nil}, 3}, 40 {[]interface{}{0, jsonInt.GetMysqlJSON(), nil}, nil}, 41 {[]interface{}{0.1, 1, 2}, 1}, 42 {[]interface{}{0.0, 1, 0.1, 2}, 2}, 43 } 44 fc := funcs[ast.Case] 45 for _, t := range tbl { 46 f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeCausets(t.Arg...))) 47 c.Assert(err, IsNil) 48 d, err := evalBuiltinFunc(f, chunk.Event{}) 49 c.Assert(err, IsNil) 50 c.Assert(d, solitonutil.CausetEquals, types.NewCauset(t.Ret)) 51 } 52 f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeCausets(errors.New("can't convert string to bool"), 1, true))) 53 c.Assert(err, IsNil) 54 _, err = evalBuiltinFunc(f, chunk.Event{}) 55 c.Assert(err, NotNil) 56 } 57 58 func (s *testEvaluatorSuite) TestIf(c *C) { 59 stmtCtx := s.ctx.GetStochastikVars().StmtCtx 60 origin := stmtCtx.IgnoreTruncate 61 stmtCtx.IgnoreTruncate = true 62 defer func() { 63 stmtCtx.IgnoreTruncate = origin 64 }() 65 tbl := []struct { 66 Arg1 interface{} 67 Arg2 interface{} 68 Arg3 interface{} 69 Ret interface{} 70 }{ 71 {1, 1, 2, 1}, 72 {nil, 1, 2, 2}, 73 {0, 1, 2, 2}, 74 {"abc", 1, 2, 2}, 75 {"1abc", 1, 2, 1}, 76 {tm, 1, 2, 1}, 77 {duration, 1, 2, 1}, 78 {types.Duration{Duration: time.Duration(0)}, 1, 2, 2}, 79 {types.NewDecFromStringForTest("1.2"), 1, 2, 1}, 80 {jsonInt.GetMysqlJSON(), 1, 2, 1}, 81 {0.1, 1, 2, 1}, 82 {0.0, 1, 2, 2}, 83 {types.NewDecFromStringForTest("0.1"), 1, 2, 1}, 84 {types.NewDecFromStringForTest("0.0"), 1, 2, 2}, 85 {"0.1", 1, 2, 1}, 86 {"0.0", 1, 2, 2}, 87 } 88 89 fc := funcs[ast.If] 90 for _, t := range tbl { 91 f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeCausets(t.Arg1, t.Arg2, t.Arg3))) 92 c.Assert(err, IsNil) 93 d, err := evalBuiltinFunc(f, chunk.Event{}) 94 c.Assert(err, IsNil) 95 c.Assert(d, solitonutil.CausetEquals, types.NewCauset(t.Ret)) 96 } 97 f, err := fc.getFunction(s.ctx, s.datumsToConstants(types.MakeCausets(errors.New("must error"), 1, 2))) 98 c.Assert(err, IsNil) 99 _, err = evalBuiltinFunc(f, chunk.Event{}) 100 c.Assert(err, NotNil) 101 _, err = fc.getFunction(s.ctx, s.datumsToConstants(types.MakeCausets(1, 2))) 102 c.Assert(err, NotNil) 103 } 104 105 func (s *testEvaluatorSuite) TestIfNull(c *C) { 106 tbl := []struct { 107 arg1 interface{} 108 arg2 interface{} 109 expected interface{} 110 isNil bool 111 getErr bool 112 }{ 113 {1, 2, int64(1), false, false}, 114 {nil, 2, int64(2), false, false}, 115 {nil, nil, nil, true, false}, 116 {tm, nil, tm, false, false}, 117 {nil, duration, duration, false, false}, 118 {nil, types.NewDecFromFloatForTest(123.123), types.NewDecFromFloatForTest(123.123), false, false}, 119 {nil, types.NewBinaryLiteralFromUint(0x01, -1), uint64(1), false, false}, 120 {nil, types.Set{Value: 1, Name: "abc"}, "abc", false, false}, 121 {nil, jsonInt.GetMysqlJSON(), jsonInt.GetMysqlJSON(), false, false}, 122 {"abc", nil, "abc", false, false}, 123 {errors.New(""), nil, "", true, true}, 124 } 125 126 for _, t := range tbl { 127 f, err := newFunctionForTest(s.ctx, ast.Ifnull, s.primitiveValsToConstants([]interface{}{t.arg1, t.arg2})...) 128 c.Assert(err, IsNil) 129 d, err := f.Eval(chunk.Event{}) 130 if t.getErr { 131 c.Assert(err, NotNil) 132 } else { 133 c.Assert(err, IsNil) 134 if t.isNil { 135 c.Assert(d.HoTT(), Equals, types.HoTTNull) 136 } else { 137 c.Assert(d.GetValue(), DeepEquals, t.expected) 138 } 139 } 140 } 141 142 _, err := funcs[ast.Ifnull].getFunction(s.ctx, []Expression{NewZero(), NewZero()}) 143 c.Assert(err, IsNil) 144 145 _, err = funcs[ast.Ifnull].getFunction(s.ctx, []Expression{NewZero()}) 146 c.Assert(err, NotNil) 147 }