Skip to content

Commit

Permalink
executor: handle missing timestamp value for load data (#11093) (#11250)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lingyu Song authored and zz-jason committed Jul 15, 2019
1 parent 9e4e8da commit 6e0277b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions executor/load_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,14 @@ func (e *LoadDataInfo) SetMessage() {
}

func (e *LoadDataInfo) colsToRow(ctx context.Context, cols []field) []types.Datum {
totalCols := e.Table.Cols()
for i := 0; i < len(e.row); i++ {
if i >= len(cols) {
// If some columns is missing and their type is time and has not null flag, they should be set as current time.
if types.IsTypeTime(totalCols[i].Tp) && mysql.HasNotNullFlag(totalCols[i].Flag) {
e.row[i].SetMysqlTime(types.CurrentTime(totalCols[i].Tp))
continue
}
e.row[i].SetNull()
continue
}
Expand Down
38 changes: 38 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/planner/core"
Expand Down Expand Up @@ -1775,6 +1776,43 @@ func (s *testSuite4) TestQualifiedDelete(c *C) {
c.Assert(err, NotNil)
}

func (s *testSuite4) TestLoadDataMissingColumn(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
createSQL := `create table load_data_missing (id int, t timestamp not null)`
tk.MustExec(createSQL)
tk.MustExec("load data local infile '/tmp/nonexistence.csv' ignore into table load_data_missing")
ctx := tk.Se.(sessionctx.Context)
ld, ok := ctx.Value(executor.LoadDataVarKey).(*executor.LoadDataInfo)
c.Assert(ok, IsTrue)
defer ctx.SetValue(executor.LoadDataVarKey, nil)
c.Assert(ld, NotNil)

deleteSQL := "delete from load_data_missing"
selectSQL := "select * from load_data_missing;"
_, reachLimit, err := ld.InsertData(context.Background(), nil, nil)
c.Assert(err, IsNil)
c.Assert(reachLimit, IsFalse)
r := tk.MustQuery(selectSQL)
r.Check(nil)

curTime := types.CurrentTime(mysql.TypeTimestamp)
timeStr := curTime.String()
tests := []testCase{
{nil, []byte("12\n"), []string{fmt.Sprintf("12|%v", timeStr)}, nil, "Records: 1 Deleted: 0 Skipped: 0 Warnings: 0"},
}
checkCases(tests, ld, c, tk, ctx, selectSQL, deleteSQL)

tk.MustExec("alter table load_data_missing add column t2 timestamp null")
curTime = types.CurrentTime(mysql.TypeTimestamp)
timeStr = curTime.String()
tests = []testCase{
{nil, []byte("12\n"), []string{fmt.Sprintf("12|%v|<nil>", timeStr)}, nil, "Records: 1 Deleted: 0 Skipped: 0 Warnings: 0"},
}
checkCases(tests, ld, c, tk, ctx, selectSQL, deleteSQL)

}

func (s *testSuite4) TestLoadData(c *C) {
trivialMsg := "Records: 1 Deleted: 0 Skipped: 0 Warnings: 0"
tk := testkit.NewTestKit(c, s.store)
Expand Down

0 comments on commit 6e0277b

Please sign in to comment.