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

ddl: support specifying expr rand() as column default value #32608

Merged
merged 32 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1720c3e
Support rand() as column default expr.
CbcWestwolf Mar 29, 2022
f86c1b3
Remove ColumnDefaultType
CbcWestwolf Mar 29, 2022
9b2c973
Merge branch 'master' into column_default_expr
CbcWestwolf Mar 30, 2022
0246e99
Merge branch 'master' into column_default_expr
CbcWestwolf Mar 30, 2022
328a043
Fix
CbcWestwolf Mar 31, 2022
e1787f1
Merge branch 'master' into column_default_expr
CbcWestwolf Mar 31, 2022
644bc0e
Fix
CbcWestwolf Mar 31, 2022
d9da08c
Modify grammar.
CbcWestwolf Mar 31, 2022
4940b06
Remove 'ast.Now'.
CbcWestwolf Mar 31, 2022
414a8bc
Merge branch 'master' into column_default_expr
CbcWestwolf Apr 1, 2022
7f1f986
Add more test.
CbcWestwolf Apr 1, 2022
14341dc
Merge branch 'master' into column_default_expr
CbcWestwolf Apr 1, 2022
9b35bf2
Merge branch 'master' into column_default_expr
CbcWestwolf Apr 2, 2022
c4bf8d8
Merge branch 'master' into column_default_expr
CbcWestwolf Apr 2, 2022
aee1f45
Introduce a new errno from MySQL 8.0
CbcWestwolf Apr 2, 2022
efaf2ef
Fix check_dev fail.
CbcWestwolf Apr 2, 2022
99d40fe
Reformat getDefaultValue.
CbcWestwolf Apr 2, 2022
307d733
Fix UT fail.
CbcWestwolf Apr 2, 2022
a53ce57
Merge branch 'master' into column_default_expr
CbcWestwolf Apr 2, 2022
c5b2b87
Update ddl/ddl_api.go
CbcWestwolf Apr 6, 2022
c00e526
Remove the redundant type assertion.
CbcWestwolf Apr 6, 2022
5dcddc9
Update comments.
CbcWestwolf Apr 6, 2022
eec35a3
Introduce NowSymOptionFractionParentheses.
CbcWestwolf Apr 6, 2022
1851df0
Merge branch 'master' into column_default_expr
CbcWestwolf Apr 6, 2022
f9d0aae
Extract getFuncCallDefaultValue from getDefaultValue.
CbcWestwolf Apr 6, 2022
2ccca80
Fix CI fail.
CbcWestwolf Apr 6, 2022
007fce4
getFuncCallDefaultValue.
CbcWestwolf Apr 6, 2022
e098fa4
Introduce ErrBinlogUnsafeSystemFunction.
CbcWestwolf Apr 6, 2022
388d507
Merge branch 'master' into column_default_expr
CbcWestwolf Apr 7, 2022
a289bdb
Update ddl/ddl_api.go
CbcWestwolf Apr 7, 2022
df005bc
Merge branch 'master' into column_default_expr
ti-chi-bot Apr 7, 2022
299bf2e
Merge branch 'master' into column_default_expr
ti-chi-bot Apr 7, 2022
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
17 changes: 17 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2031,6 +2031,23 @@ func TestDefaultValueIsString(t *testing.T) {
require.Equal(t, "1", tbl.Meta().Columns[0].DefaultValue)
}

func TestDefaultColumnWithRand(t *testing.T) {
// Related issue: https://github.com/pingcap/tidb/issues/10377
store, clean := testkit.CreateMockStoreWithSchemaLease(t, testLease)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

tk.MustExec("drop table if exists t, t1, t2")
tk.MustExec("create table t (c int(10) default rand())")
tk.MustExec("create table t1 (c int default rand(), c1 double default rand())")
tk.MustExec("create table t2 (c int default rand(1), c1 double default rand(2))")
tk.MustExec("alter table t add column c1 int(10) default rand(2)")
tk.MustExec("insert into t(c) values (1),(2),(3)")
tk.MustExec("insert into t1(c) values (1),(2),(3)")
tk.MustExec("insert into t2(c) values (1),(2),(3)")
}

func TestChangingDBCharset(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()
Expand Down
21 changes: 20 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ func getDefaultValue(ctx sessionctx.Context, col *table.Column, c *ast.ColumnOpt
if tp == mysql.TypeTimestamp || tp == mysql.TypeDatetime {
switch x := c.Expr.(type) {
case *ast.FuncCallExpr:
if x.FnName.L == ast.CurrentTimestamp {
if x.FnName.L == ast.CurrentTimestamp || x.FnName.L == ast.Now {
defaultFsp := 0
if len(x.Args) == 1 {
if val := x.Args[0].(*driver.ValueExpr); val != nil {
Expand Down Expand Up @@ -1073,6 +1073,25 @@ func getDefaultValue(ctx sessionctx.Context, col *table.Column, c *ast.ColumnOpt

return value, false, nil
}

switch x := c.Expr.(type) {
case *ast.FuncCallExpr:
if x.FnName.L == ast.Rand {
if err := expression.VerifyArgsWrapper(ast.Rand, len(x.Args)); err != nil {
return nil, false, expression.ErrIncorrectParameterCount.GenWithStackByArgs(ast.Rand)
}
col.DefaultIsExpr = true
var sb strings.Builder
restoreFlags := format.RestoreStringSingleQuotes | format.RestoreKeyWordLowercase | format.RestoreNameBackQuotes |
format.RestoreSpacesAroundBinaryOperation
restoreCtx := format.NewRestoreCtx(restoreFlags, &sb)
if err := c.Expr.Restore(restoreCtx); err != nil {
return "", false, err
}
return sb.String(), false, nil
}
}

// handle default next value of sequence. (keep the expr string)
str, isSeqExpr, err := tryToGetSequenceDefaultValue(c)
if err != nil {
Expand Down
Loading