Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expression: return correct results for user variables of datetime type (#22078) #22143

Merged
merged 3 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion expression/builtin_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,18 @@ func (b *builtinGetStringVarSig) evalString(row chunk.Row) (string, bool, error)
sessionVars.UsersLock.RLock()
defer sessionVars.UsersLock.RUnlock()
if v, ok := sessionVars.Users[varName]; ok {
return v.GetString(), false, nil
// We cannot use v.GetString() here, because the datum may be in KindMysqlTime, which
// stores the data in datum.x.
// This seems controversial with https://dev.mysql.com/doc/refman/8.0/en/user-variables.html:
// > User variables can be assigned a value from a limited set of data types: integer, decimal,
// > floating-point, binary or nonbinary string, or NULL value.
// However, MySQL actually does support query like `set @p = now()`, so we should not assume the datum stored
// must be of one of the following types: string, decimal, int, float.
res, err := v.ToString()
if err != nil {
return "", false, err
}
return res, false, nil
}
return "", true, nil
}
Expand Down
6 changes: 5 additions & 1 deletion expression/builtin_other_vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ func (b *builtinGetStringVarSig) vecEvalString(input *chunk.Chunk, result *chunk
}
varName := strings.ToLower(buf0.GetString(i))
if v, ok := sessionVars.Users[varName]; ok {
result.AppendString(v.GetString())
res, err := v.ToString()
if err != nil {
return err
}
result.AppendString(res)
continue
}
result.AppendNull()
Expand Down
9 changes: 9 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7327,3 +7327,12 @@ func (s *testIntegrationSerialSuite) TestCollationIndexJoin(c *C) {
tk.MustQuery("select /*+ inl_merge_join(t2) */ t1.b, t2.b from t1 join t2 where t1.b=t2.b").Check(testkit.Rows("a A"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 Optimizer Hint /*+ INL_MERGE_JOIN(t2) */ is inapplicable"))
}

func (s *testIntegrationSuite) TestDatetimeUserVariable(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @p = now()")
tk.MustExec("set @@tidb_enable_vectorized_expression = false")
c.Check(tk.MustQuery("select @p").Rows()[0][0] != "", IsTrue)
tk.MustExec("set @@tidb_enable_vectorized_expression = true")
c.Check(tk.MustQuery("select @p").Rows()[0][0] != "", IsTrue)
}