diff --git a/ddl/ddl.go b/ddl/ddl.go index 209abc29b352c..2a1fd8e34ef55 100644 --- a/ddl/ddl.go +++ b/ddl/ddl.go @@ -534,6 +534,8 @@ func updateDefaultValue(ctx context.Context, t *tables.Table, col *column.Col) e continue } + // TODO: check and get timestamp/datetime default value. + // refer to getDefaultValue in stmt/stmts/stmt_helper.go. if err0 = t.SetColValue(txn, k, col.DefaultValue); err0 != nil { return errors.Trace(err0) } diff --git a/ddl/ddl_test.go b/ddl/ddl_test.go index 95311f44f9295..7343b9674ff35 100644 --- a/ddl/ddl_test.go +++ b/ddl/ddl_test.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/kv" "github.com/pingcap/tidb/model" + mysql "github.com/pingcap/tidb/mysqldef" "github.com/pingcap/tidb/parser" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/stmt" @@ -81,20 +82,38 @@ func (ts *testSuite) TestT(c *C) { tb, err := sessionctx.GetDomain(ctx).InfoSchema().TableByName(tbIdent2.Schema, tbIdent2.Name) c.Assert(err, IsNil) c.Assert(tb, NotNil) - _, err = tb.AddRecord(ctx, []interface{}{1}) + rid0, err := tb.AddRecord(ctx, []interface{}{1}) c.Assert(err, IsNil) - _, err = tb.AddRecord(ctx, []interface{}{2}) + rid1, err := tb.AddRecord(ctx, []interface{}{2}) c.Assert(err, IsNil) - alterStmt := statement(`alter table t2 add b enum("bbb") first`).(*stmts.AlterTableStmt) + alterStmt := statement(`alter table t2 add b enum("bb") first`).(*stmts.AlterTableStmt) sessionctx.GetDomain(ctx).DDL().AlterTable(ctx, tbIdent2, alterStmt.Specs) c.Assert(alterStmt.Specs[0].String(), Not(Equals), "") - _, err = tb.Row(ctx, 1) + cols, err := tb.Row(ctx, rid0) c.Assert(err, IsNil) - alterStmt = statement("alter table t2 add c timestamp after b").(*stmts.AlterTableStmt) + c.Assert(len(cols), Equals, 2) + c.Assert(cols[0], Equals, nil) + c.Assert(cols[1], Equals, int64(1)) + alterStmt = statement("alter table t2 add c varchar(255) after b").(*stmts.AlterTableStmt) sessionctx.GetDomain(ctx).DDL().AlterTable(ctx, tbIdent2, alterStmt.Specs) c.Assert(alterStmt.Specs[0].String(), Not(Equals), "") - _, err = tb.Row(ctx, 2) + tb, err = sessionctx.GetDomain(ctx).InfoSchema().TableByName(tbIdent2.Schema, tbIdent2.Name) c.Assert(err, IsNil) + c.Assert(tb, NotNil) + cols, err = tb.Row(ctx, rid1) + c.Assert(err, IsNil) + c.Assert(len(cols), Equals, 3) + c.Assert(cols[0], Equals, nil) + c.Assert(cols[1], Equals, nil) + c.Assert(cols[2], Equals, int64(2)) + rid3, err := tb.AddRecord(ctx, []interface{}{mysql.Enum{Name: "bb", Value: 1}, "c", 3}) + c.Assert(err, IsNil) + cols, err = tb.Row(ctx, rid3) + c.Assert(err, IsNil) + c.Assert(len(cols), Equals, 3) + c.Assert(cols[0], Equals, mysql.Enum{Name: "bb", Value: 1}) + c.Assert(cols[1], Equals, "c") + c.Assert(cols[2], Equals, int64(3)) tb, err = sessionctx.GetDomain(ctx).InfoSchema().TableByName(tbIdent.Schema, tbIdent.Name) c.Assert(err, IsNil) diff --git a/table/tables/tables.go b/table/tables/tables.go index ee599c17d5dea..c825fe0feba42 100644 --- a/table/tables/tables.go +++ b/table/tables/tables.go @@ -39,8 +39,9 @@ import ( "github.com/pingcap/tidb/util/errors2" ) -// KeyLastItem is the last item in key. -const KeyLastItem = 3 +// The key has tableID, rowID/idx, columnID three parts. +// keyLastItem is the last item in key. +const keyLastItem = 3 // Table implements table.Table interface. type Table struct { @@ -138,11 +139,11 @@ func ColumnID(key []byte) (interface{}, error) { if err != nil { return 0, errors.Trace(err) } - if len(k) != KeyLastItem { + if len(k) != keyLastItem { return 0, errors.New("column not exist") } - return k[KeyLastItem-1], nil + return k[keyLastItem-1], nil } func (t *Table) unflatten(rec interface{}, col *column.Col) (interface{}, error) { @@ -285,10 +286,10 @@ func (t *Table) setOnUpdateData(ctx context.Context, touched []bool, data []inte func (t *Table) SetColValue(txn kv.Transaction, key []byte, data interface{}) error { v, err := t.EncodeValue(data) if err != nil { - return err + return errors.Trace(err) } if err := txn.Set(key, v); err != nil { - return err + return errors.Trace(err) } return nil }