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

executor: handle missing timestamp value for load data (#11093) #11250

Merged
merged 2 commits into from
Jul 15, 2019
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
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