Skip to content

Commit

Permalink
expression: fix err check (#22992)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tjianke authored Mar 3, 2021
1 parent 93c1779 commit d6ee6d3
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 26 deletions.
3 changes: 2 additions & 1 deletion expression/builtin_cast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,8 @@ func (s *testEvaluatorSuite) TestCastXXX(c *C) {
res, err = f.Eval(chunk.Row{})
c.Assert(err, IsNil)
resDecimal := new(types.MyDecimal)
resDecimal.FromString([]byte("99999.99"))
err = resDecimal.FromString([]byte("99999.99"))
c.Assert(err, IsNil)
c.Assert(res.GetMysqlDecimal().Compare(resDecimal), Equals, 0)

warnings = sc.GetWarnings()
Expand Down
23 changes: 15 additions & 8 deletions expression/builtin_encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ var aesTests = []struct {
func (s *testEvaluatorSuite) TestAESEncrypt(c *C) {
fc := funcs[ast.AesEncrypt]
for _, tt := range aesTests {
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum(tt.mode))
err := variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum(tt.mode))
c.Assert(err, IsNil)
args := []types.Datum{types.NewDatum(tt.origin)}
for _, param := range tt.params {
args = append(args, types.NewDatum(param))
Expand All @@ -131,15 +132,17 @@ func (s *testEvaluatorSuite) TestAESEncrypt(c *C) {
c.Assert(err, IsNil)
c.Assert(toHex(crypt), DeepEquals, types.NewDatum(tt.crypt))
}
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-ecb"))
err := variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-ecb"))
c.Assert(err, IsNil)
s.testNullInput(c, ast.AesEncrypt)
s.testAmbiguousInput(c, ast.AesEncrypt)
}

func (s *testEvaluatorSuite) TestAESDecrypt(c *C) {
fc := funcs[ast.AesDecrypt]
for _, tt := range aesTests {
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum(tt.mode))
err := variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum(tt.mode))
c.Assert(err, IsNil)
args := []types.Datum{fromHex(tt.crypt)}
for _, param := range tt.params {
args = append(args, types.NewDatum(param))
Expand All @@ -154,13 +157,15 @@ func (s *testEvaluatorSuite) TestAESDecrypt(c *C) {
}
c.Assert(str, DeepEquals, types.NewCollationStringDatum(tt.origin.(string), charset.CollationBin, collate.DefaultLen))
}
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-ecb"))
err := variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-ecb"))
c.Assert(err, IsNil)
s.testNullInput(c, ast.AesDecrypt)
s.testAmbiguousInput(c, ast.AesDecrypt)
}

func (s *testEvaluatorSuite) testNullInput(c *C, fnName string) {
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-ecb"))
err := variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-ecb"))
c.Assert(err, IsNil)
fc := funcs[fnName]
arg := types.NewStringDatum("str")
var argNull types.Datum
Expand All @@ -181,16 +186,18 @@ func (s *testEvaluatorSuite) testAmbiguousInput(c *C, fnName string) {
fc := funcs[fnName]
arg := types.NewStringDatum("str")
// test for modes that require init_vector
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-cbc"))
_, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Datum{arg, arg}))
err := variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-cbc"))
c.Assert(err, IsNil)
_, err = fc.getFunction(s.ctx, s.datumsToConstants([]types.Datum{arg, arg}))
c.Assert(err, NotNil)
f, err := fc.getFunction(s.ctx, s.datumsToConstants([]types.Datum{arg, arg, types.NewStringDatum("iv < 16 bytes")}))
c.Assert(err, IsNil)
_, err = evalBuiltinFunc(f, chunk.Row{})
c.Assert(err, NotNil)

// test for modes that do not require init_vector
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-ecb"))
err = variable.SetSessionSystemVar(s.ctx.GetSessionVars(), variable.BlockEncryptionMode, types.NewDatum("aes-128-ecb"))
c.Assert(err, IsNil)
f, err = fc.getFunction(s.ctx, s.datumsToConstants([]types.Datum{arg, arg, arg}))
c.Assert(err, IsNil)
_, err = evalBuiltinFunc(f, chunk.Row{})
Expand Down
24 changes: 16 additions & 8 deletions expression/builtin_time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,8 @@ func (s *testEvaluatorSuite) TestDate(c *C) {
}

dtblNil = tblToDtbl(tblNil)
s.ctx.GetSessionVars().SetSystemVar("sql_mode", "NO_ZERO_DATE")
err := s.ctx.GetSessionVars().SetSystemVar("sql_mode", "NO_ZERO_DATE")
c.Assert(err, IsNil)
for _, t := range dtblNil {
fc := funcs[ast.Year]
f, err := fc.getFunction(s.ctx, s.datumsToConstants(t["Input"]))
Expand Down Expand Up @@ -864,8 +865,10 @@ func (s *testEvaluatorSuite) TestNowAndUTCTimestamp(c *C) {
}

// Test that "timestamp" and "time_zone" variable may affect the result of Now() builtin function.
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), "time_zone", types.NewDatum("+00:00"))
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), "timestamp", types.NewDatum(1234))
err := variable.SetSessionSystemVar(s.ctx.GetSessionVars(), "time_zone", types.NewDatum("+00:00"))
c.Assert(err, IsNil)
err = variable.SetSessionSystemVar(s.ctx.GetSessionVars(), "timestamp", types.NewDatum(1234))
c.Assert(err, IsNil)
fc := funcs[ast.Now]
resetStmtContext(s.ctx)
f, err := fc.getFunction(s.ctx, s.datumsToConstants(nil))
Expand All @@ -875,8 +878,10 @@ func (s *testEvaluatorSuite) TestNowAndUTCTimestamp(c *C) {
result, err := v.ToString()
c.Assert(err, IsNil)
c.Assert(result, Equals, "1970-01-01 00:20:34")
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), "timestamp", types.NewDatum(0))
variable.SetSessionSystemVar(s.ctx.GetSessionVars(), "time_zone", types.NewDatum("system"))
err = variable.SetSessionSystemVar(s.ctx.GetSessionVars(), "timestamp", types.NewDatum(0))
c.Assert(err, IsNil)
err = variable.SetSessionSystemVar(s.ctx.GetSessionVars(), "time_zone", types.NewDatum("system"))
c.Assert(err, IsNil)
}

func (s *testEvaluatorSuite) TestIsDuration(c *C) {
Expand Down Expand Up @@ -1119,7 +1124,8 @@ func (s *testEvaluatorSuite) TestSysDate(c *C) {
timezones := []types.Datum{types.NewDatum(1234), types.NewDatum(0)}
for _, timezone := range timezones {
// sysdate() result is not affected by "timestamp" session variable.
variable.SetSessionSystemVar(ctx.GetSessionVars(), "timestamp", timezone)
err := variable.SetSessionSystemVar(ctx.GetSessionVars(), "timestamp", timezone)
c.Assert(err, IsNil)
f, err := fc.getFunction(ctx, s.datumsToConstants(nil))
c.Assert(err, IsNil)
resetStmtContext(s.ctx)
Expand Down Expand Up @@ -1645,9 +1651,11 @@ func (s *testEvaluatorSuite) TestWeekWithoutModeSig(c *C) {
c.Assert(err, IsNil)
c.Assert(result.GetInt64(), Equals, test.expect)
if i == 1 {
s.ctx.GetSessionVars().SetSystemVar("default_week_format", "6")
err = s.ctx.GetSessionVars().SetSystemVar("default_week_format", "6")
c.Assert(err, IsNil)
} else if i == 3 {
s.ctx.GetSessionVars().SetSystemVar("default_week_format", "")
err = s.ctx.GetSessionVars().SetSystemVar("default_week_format", "")
c.Assert(err, IsNil)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion expression/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ func (s *testEvaluatorSuiteBase) SetUpTest(c *C) {
s.ctx.GetSessionVars().StmtCtx.TimeZone = time.Local
sc := s.ctx.GetSessionVars().StmtCtx
sc.TruncateAsWarning = true
s.ctx.GetSessionVars().SetSystemVar("max_allowed_packet", "67108864")
err := s.ctx.GetSessionVars().SetSystemVar("max_allowed_packet", "67108864")
c.Assert(err, IsNil)
s.ctx.GetSessionVars().PlanColumnID = 0
}

Expand Down
21 changes: 14 additions & 7 deletions expression/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,35 @@ func (s *testExpressionSuite) TestGetTimeValue(c *C) {
timeValue := v.GetMysqlTime()
c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")
sessionVars := ctx.GetSessionVars()
variable.SetSessionSystemVar(sessionVars, "timestamp", types.NewStringDatum(""))
err = variable.SetSessionSystemVar(sessionVars, "timestamp", types.NewStringDatum(""))
c.Assert(err, IsNil)
v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", mysql.TypeTimestamp, types.MinFsp)
c.Assert(err, IsNil)

c.Assert(v.Kind(), Equals, types.KindMysqlTime)
timeValue = v.GetMysqlTime()
c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")

variable.SetSessionSystemVar(sessionVars, "timestamp", types.NewStringDatum("0"))
err = variable.SetSessionSystemVar(sessionVars, "timestamp", types.NewStringDatum("0"))
c.Assert(err, IsNil)
v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", mysql.TypeTimestamp, types.MinFsp)
c.Assert(err, IsNil)

c.Assert(v.Kind(), Equals, types.KindMysqlTime)
timeValue = v.GetMysqlTime()
c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")

variable.SetSessionSystemVar(sessionVars, "timestamp", types.Datum{})
err = variable.SetSessionSystemVar(sessionVars, "timestamp", types.Datum{})
c.Assert(err, IsNil)
v, err = GetTimeValue(ctx, "2012-12-12 00:00:00", mysql.TypeTimestamp, types.MinFsp)
c.Assert(err, IsNil)

c.Assert(v.Kind(), Equals, types.KindMysqlTime)
timeValue = v.GetMysqlTime()
c.Assert(timeValue.String(), Equals, "2012-12-12 00:00:00")

variable.SetSessionSystemVar(sessionVars, "timestamp", types.NewStringDatum("1234"))
err = variable.SetSessionSystemVar(sessionVars, "timestamp", types.NewStringDatum("1234"))
c.Assert(err, IsNil)

tbl := []struct {
Expr interface{}
Expand Down Expand Up @@ -135,8 +139,10 @@ func (s *testExpressionSuite) TestCurrentTimestampTimeZone(c *C) {
ctx := mock.NewContext()
sessionVars := ctx.GetSessionVars()

variable.SetSessionSystemVar(sessionVars, "timestamp", types.NewStringDatum("1234"))
variable.SetSessionSystemVar(sessionVars, "time_zone", types.NewStringDatum("+00:00"))
err := variable.SetSessionSystemVar(sessionVars, "timestamp", types.NewStringDatum("1234"))
c.Assert(err, IsNil)
err = variable.SetSessionSystemVar(sessionVars, "time_zone", types.NewStringDatum("+00:00"))
c.Assert(err, IsNil)
v, err := GetTimeValue(ctx, ast.CurrentTimestamp, mysql.TypeTimestamp, types.MinFsp)
c.Assert(err, IsNil)
c.Assert(v.GetMysqlTime(), DeepEquals, types.NewTime(
Expand All @@ -145,7 +151,8 @@ func (s *testExpressionSuite) TestCurrentTimestampTimeZone(c *C) {

// CurrentTimestamp from "timestamp" session variable is based on UTC, so change timezone
// would get different value.
variable.SetSessionSystemVar(sessionVars, "time_zone", types.NewStringDatum("+08:00"))
err = variable.SetSessionSystemVar(sessionVars, "time_zone", types.NewStringDatum("+08:00"))
c.Assert(err, IsNil)
v, err = GetTimeValue(ctx, ast.CurrentTimestamp, mysql.TypeTimestamp, types.MinFsp)
c.Assert(err, IsNil)
c.Assert(v.GetMysqlTime(), DeepEquals, types.NewTime(
Expand Down
3 changes: 2 additions & 1 deletion expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3922,7 +3922,8 @@ func (s *testIntegrationSuite) TestAggregationBuiltinGroupConcat(c *C) {
_, err := tk.Exec("insert into d select group_concat(a) from t")
c.Assert(errors.Cause(err).(*terror.Error).Code(), Equals, errors.ErrCode(mysql.ErrCutValueGroupConcat))

tk.Exec("set sql_mode=''")
_, err = tk.Exec("set sql_mode=''")
c.Assert(err, IsNil)
tk.MustExec("insert into d select group_concat(a) from t")
tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning 1260 Some rows were cut by GROUPCONCAT(test.t.a)"))
tk.MustQuery("select * from d").Check(testkit.Rows("hello,h"))
Expand Down

0 comments on commit d6ee6d3

Please sign in to comment.