Skip to content

Commit 4c76b12

Browse files
authored
importinto/lightning: fix incorrect datatime value for zero date (#51201)
close #50757
1 parent 11f716e commit 4c76b12

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

br/pkg/lightning/backend/kv/session.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,8 @@ func NewSession(options *encode.SessionOptions, logger log.Logger) *Session {
292292
typeFlags := vars.StmtCtx.TypeFlags().
293293
WithTruncateAsWarning(!sqlMode.HasStrictMode()).
294294
WithIgnoreInvalidDateErr(sqlMode.HasAllowInvalidDatesMode()).
295-
WithIgnoreZeroInDate(!sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode())
295+
WithIgnoreZeroInDate(!sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode() ||
296+
!sqlMode.HasNoZeroInDateMode() || !sqlMode.HasNoZeroDateMode())
296297
vars.StmtCtx.SetTypeFlags(typeFlags)
297298

298299
errLevels := vars.StmtCtx.ErrLevels()

tests/realtikvtest/importintotest/import_into_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,3 +1166,26 @@ func (s *mockGCSSuite) TestAnalyze() {
11661166
}, 60*time.Second, time.Second)
11671167
s.tk.MustQuery("SHOW ANALYZE STATUS;").CheckContain("analyze_table")
11681168
}
1169+
1170+
func (s *mockGCSSuite) TestZeroDateTime() {
1171+
s.tk.MustExec("DROP DATABASE IF EXISTS import_into;")
1172+
s.tk.MustExec("CREATE DATABASE import_into;")
1173+
1174+
s.tk.MustExec("create table import_into.zero_time_table(t datetime)")
1175+
s.tk.MustExec("set @@sql_mode='STRICT_TRANS_TABLES'")
1176+
1177+
s.server.CreateObject(fakestorage.Object{
1178+
ObjectAttrs: fakestorage.ObjectAttrs{BucketName: "test-load", Name: "zero_time.csv"},
1179+
Content: []byte("1990-01-00 00:00:00\n"),
1180+
})
1181+
1182+
sql := fmt.Sprintf(`IMPORT INTO import_into.zero_time_table FROM 'gs://test-load/zero_time.csv?endpoint=%s'`, gcsEndpoint)
1183+
s.tk.MustQuery(sql)
1184+
s.tk.MustQuery("SELECT * FROM import_into.zero_time_table;").Sort().Check(testkit.Rows("1990-01-00 00:00:00"))
1185+
1186+
// set default value
1187+
s.tk.MustExec("set @@sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'")
1188+
s.tk.MustExec("truncate table import_into.zero_time_table")
1189+
err := s.tk.QueryToErr(sql)
1190+
s.ErrorContains(err, `Incorrect datetime value: '1990-01-00 00:00:00'`)
1191+
}

0 commit comments

Comments
 (0)