Skip to content

Commit

Permalink
cherry pick pingcap#20206 to release-4.0
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
  • Loading branch information
jsvisa authored and ti-srebot committed Dec 8, 2020
1 parent 189ef5e commit 3721f58
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 14 deletions.
19 changes: 17 additions & 2 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
sc.TruncateAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.DividedByZeroAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode()
sc.IgnoreZeroInDate = !vars.StrictSQLMode || stmt.IgnoreErr || sc.AllowInvalidDate
sc.IgnoreZeroInDate = !vars.SQLMode.HasNoZeroInDateMode() || !vars.SQLMode.HasNoZeroDateMode() || !vars.StrictSQLMode || stmt.IgnoreErr || sc.AllowInvalidDate
sc.Priority = stmt.Priority
case *ast.InsertStmt:
sc.InInsertStmt = true
Expand All @@ -1631,7 +1631,7 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
sc.TruncateAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.DividedByZeroAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode()
sc.IgnoreZeroInDate = !vars.StrictSQLMode || stmt.IgnoreErr || sc.AllowInvalidDate
sc.IgnoreZeroInDate = !vars.SQLMode.HasNoZeroInDateMode() || !vars.SQLMode.HasNoZeroDateMode() || !vars.StrictSQLMode || stmt.IgnoreErr || sc.AllowInvalidDate
sc.Priority = stmt.Priority
case *ast.CreateTableStmt, *ast.AlterTableStmt:
// Make sure the sql_mode is strict when checking column default value.
Expand Down Expand Up @@ -1710,6 +1710,21 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
return
}

<<<<<<< HEAD
=======
// ResetUpdateStmtCtx resets statement context for UpdateStmt.
func ResetUpdateStmtCtx(sc *stmtctx.StatementContext, stmt *ast.UpdateStmt, vars *variable.SessionVars) {
sc.InUpdateStmt = true
sc.DupKeyAsWarning = stmt.IgnoreErr
sc.BadNullAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.TruncateAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.DividedByZeroAsWarning = !vars.StrictSQLMode || stmt.IgnoreErr
sc.AllowInvalidDate = vars.SQLMode.HasAllowInvalidDatesMode()
sc.IgnoreZeroInDate = !vars.SQLMode.HasNoZeroInDateMode() || !vars.SQLMode.HasNoZeroDateMode() || !vars.StrictSQLMode || stmt.IgnoreErr || sc.AllowInvalidDate
sc.Priority = stmt.Priority
}

>>>>>>> afb2ab95f... table: fix zero date in different sqlmode (#20206)
// FillVirtualColumnValue will calculate the virtual column value by evaluating generated
// expression using rows from a chunk, and then fill this value into the chunk
func FillVirtualColumnValue(virtualRetTypes []*types.FieldType, virtualColumnIndex []int,
Expand Down
4 changes: 3 additions & 1 deletion executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1448,6 +1448,7 @@ func (s *testSuite8) TestUpdate(c *C) {
"`ts` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
"KEY `idx` (`ts`)" +
");")
tk.MustExec("set @orig_sql_mode=@@sql_mode; set @@sql_mode='';")
tk.MustExec("insert into tsup values(1, '0000-00-00 00:00:00');")
tk.MustExec("update tsup set a=5;")
tk.CheckLastMessage("Rows matched: 1 Changed: 1 Warnings: 0")
Expand All @@ -1456,6 +1457,7 @@ func (s *testSuite8) TestUpdate(c *C) {
r1.Check(r2.Rows())
tk.MustExec("update tsup set ts='2019-01-01';")
tk.MustQuery("select ts from tsup;").Check(testkit.Rows("2019-01-01 00:00:00"))
tk.MustExec("set @@sql_mode=@orig_sql_mode;")

// issue 5532
tk.MustExec("create table decimals (a decimal(20, 0) not null)")
Expand Down Expand Up @@ -1508,7 +1510,7 @@ func (s *testSuite8) TestUpdate(c *C) {
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a datetime not null, b datetime)")
tk.MustExec("insert into t value('1999-12-12', '1999-12-13')")
tk.MustExec(" set @orig_sql_mode=@@sql_mode; set @@sql_mode='';")
tk.MustExec("set @orig_sql_mode=@@sql_mode; set @@sql_mode='';")
tk.MustQuery("select * from t").Check(testkit.Rows("1999-12-12 00:00:00 1999-12-13 00:00:00"))
tk.MustExec("update t set a = ''")
tk.MustQuery("select * from t").Check(testkit.Rows("0000-00-00 00:00:00 1999-12-13 00:00:00"))
Expand Down
247 changes: 242 additions & 5 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1781,8 +1781,7 @@ func (s *testIntegrationSuite2) TestTimeBuiltin(c *C) {
_, err = tk.Exec(`delete from t where a = dayOfWeek(123)`)
c.Assert(err, IsNil)

_, err = tk.Exec("insert into t value(dayOfMonth('2017-00-00'))")
c.Assert(table.ErrTruncatedWrongValueForField.Equal(err), IsTrue)
tk.MustExec("insert into t value(dayOfMonth('2017-00-00'))")
tk.MustExec("insert into t value(dayOfMonth('0000-00-00'))")
tk.MustExec(`update t set a = dayOfMonth("0000-00-00")`)
tk.MustExec("set sql_mode = 'NO_ZERO_DATE';")
Expand Down Expand Up @@ -3808,7 +3807,7 @@ func (s *testIntegrationSuite) TestAggregationBuiltinJSONObjectAgg(c *C) {
i char(36),
j text(50));`)

tk.MustExec(`insert into t values(1, 'ab', 5.5, '{"id": 1}', '2020-01-10', '11:12:13', '2020-01-11', '0000-00-00 00:00:00', 'first', 'json_objectagg_test');`)
tk.MustExec(`insert into t values(1, 'ab', 5.5, '{"id": 1}', '2020-01-10', '11:12:13', '2020-01-11', '2020-10-18 00:00:00', 'first', 'json_objectagg_test');`)

result := tk.MustQuery("select json_objectagg(a, b) from t group by a order by a;")
result.Check(testkit.Rows(`{"1": "ab"}`))
Expand All @@ -3819,9 +3818,9 @@ func (s *testIntegrationSuite) TestAggregationBuiltinJSONObjectAgg(c *C) {
result = tk.MustQuery("select json_objectagg(f, g) from t group by f order by f;")
result.Check(testkit.Rows(`{"11:12:13": "2020-01-11 00:00:00"}`))
result = tk.MustQuery("select json_objectagg(g, h) from t group by g order by g;")
result.Check(testkit.Rows(`{"2020-01-11 00:00:00": "0000-00-00 00:00:00"}`))
result.Check(testkit.Rows(`{"2020-01-11 00:00:00": "2020-10-18 00:00:00"}`))
result = tk.MustQuery("select json_objectagg(h, i) from t group by h order by h;")
result.Check(testkit.Rows(`{"0000-00-00 00:00:00": "first"}`))
result.Check(testkit.Rows(`{"2020-10-18 00:00:00": "first"}`))
result = tk.MustQuery("select json_objectagg(i, j) from t group by i order by i;")
result.Check(testkit.Rows(`{"first": "json_objectagg_test"}`))
result = tk.MustQuery("select json_objectagg(a, null) from t group by a order by a;")
Expand Down Expand Up @@ -7327,3 +7326,241 @@ func (s *testIntegrationSerialSuite) TestCollationIndexJoin(c *C) {
tk.MustQuery("select /*+ inl_merge_join(t2) */ t1.b, t2.b from t1 join t2 where t1.b=t2.b").Check(testkit.Rows("a A"))
tk.MustQuery("show warnings").Check(testkit.Rows("Warning 1815 Optimizer Hint /*+ INL_MERGE_JOIN(t2) */ is inapplicable"))
}

func (s *testIntegrationSuite) TestIssue19892(c *C) {
defer s.cleanEnv(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("USE test")
tk.MustExec("CREATE TABLE dd(a date, b datetime, c timestamp)")

// check NO_ZERO_DATE
{
tk.MustExec("SET sql_mode=''")
{
tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(a) values('0000-00-00')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT a FROM dd").Check(testkit.Rows("0000-00-00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(b) values('2000-10-01')")
tk.MustExec("UPDATE dd SET b = '0000-00-00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(c) values('0000-00-00 20:00:00')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '0000-00-00 20:00:00'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(c) values('2000-10-01 20:00:00')")
tk.MustExec("UPDATE dd SET c = '0000-00-00 20:00:00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '0000-00-00 20:00:00'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
}

tk.MustExec("SET sql_mode='NO_ZERO_DATE'")
{
tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(b) values('0000-0-00')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '0000-0-00'"))
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(a) values('2000-10-01')")
tk.MustExec("UPDATE dd SET a = '0000-00-00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect date value: '0000-00-00'"))
tk.MustQuery("SELECT a FROM dd").Check(testkit.Rows("0000-00-00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(c) values('2000-10-01 10:00:00')")
tk.MustExec("UPDATE dd SET c = '0000-00-00 10:00:00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '0000-00-00 10:00:00'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
}

tk.MustExec("SET sql_mode='NO_ZERO_DATE,STRICT_TRANS_TABLES'")
{
tk.MustExec("TRUNCATE TABLE dd")
tk.MustGetErrMsg("INSERT INTO dd(c) VALUES ('0000-00-00 20:00:00')", "[table:1292]Incorrect timestamp value: '0000-00-00 20:00:00' for column 'c' at row 1")
tk.MustExec("INSERT IGNORE INTO dd(c) VALUES ('0000-00-00 20:00:00')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '0000-00-00 20:00:00'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(b) values('2000-10-01')")
tk.MustGetErrMsg("UPDATE dd SET b = '0000-00-00'", "[types:1292]Incorrect datetime value: '0000-00-00'")
tk.MustExec("UPDATE IGNORE dd SET b = '0000-00-00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '0000-00-00'"))
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(c) values('2000-10-01 10:00:00')")
tk.MustGetErrMsg("UPDATE dd SET c = '0000-00-00 00:00:00'", "[types:1292]Incorrect timestamp value: '0000-00-00 00:00:00'")
tk.MustExec("UPDATE IGNORE dd SET c = '0000-00-00 00:00:00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '0000-00-00 00:00:00'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
}
}

// check NO_ZERO_IN_DATE
{
tk.MustExec("SET sql_mode=''")
{
tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(a) values('2000-01-00')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT a FROM dd").Check(testkit.Rows("2000-01-00"))
tk.MustExec("INSERT INTO dd(a) values('2000-00-01')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT a FROM dd").Check(testkit.Rows("2000-01-00", "2000-00-01"))
tk.MustExec("INSERT INTO dd(a) values('0-01-02')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT a FROM dd").Check(testkit.Rows("2000-01-00", "2000-00-01", "0000-01-02"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(b) values('2000-01-02')")
tk.MustExec("UPDATE dd SET b = '2000-00-02'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("2000-00-02 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(c) values('2000-01-02 20:00:00')")
tk.MustExec("UPDATE dd SET c = '0000-01-02 20:00:00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '0000-01-02 20:00:00'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
}

tk.MustExec("SET sql_mode='NO_ZERO_IN_DATE'")
{
tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(a) values('2000-01-00')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect date value: '2000-01-00'"))
tk.MustQuery("SELECT a FROM dd").Check(testkit.Rows("0000-00-00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(a) values('2000-01-02')")
tk.MustExec("UPDATE dd SET a = '2000-00-02'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect date value: '2000-00-02'"))
tk.MustQuery("SELECT a FROM dd").Check(testkit.Rows("0000-00-00"))
tk.MustExec("UPDATE dd SET b = '2000-01-0'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '2000-01-0'"))
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
// consistent with Mysql8
tk.MustExec("UPDATE dd SET b = '0-01-02'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-01-02 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(c) values('2000-01-02 20:00:00')")
tk.MustExec("UPDATE dd SET c = '2000-00-02 20:00:00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '2000-00-02 20:00:00'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
}

tk.MustExec("SET sql_mode='NO_ZERO_IN_DATE,STRICT_TRANS_TABLES'")
{
tk.MustExec("TRUNCATE TABLE dd")
tk.MustGetErrMsg("INSERT INTO dd(b) VALUES ('2000-01-00')", "[table:1292]Incorrect datetime value: '2000-01-00' for column 'b' at row 1")
tk.MustExec("INSERT IGNORE INTO dd(b) VALUES ('2000-00-01')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '2000-00-01'"))
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(b) VALUES ('2000-01-02')")
tk.MustGetErrMsg("UPDATE dd SET b = '2000-01-00'", "[types:1292]Incorrect datetime value: '2000-01-00'")
tk.MustExec("UPDATE IGNORE dd SET b = '2000-01-0'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '2000-01-0'"))
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
tk.MustExec("UPDATE dd SET b = '0000-1-2'")
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-01-02 00:00:00"))
tk.MustGetErrMsg("UPDATE dd SET c = '0000-01-05'", "[types:1292]Incorrect timestamp value: '0000-01-05'")
tk.MustExec("UPDATE IGNORE dd SET c = '0000-01-5'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '0000-01-5'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustGetErrMsg("INSERT INTO dd(c) VALUES ('2000-01-00 20:00:00')", "[table:1292]Incorrect timestamp value: '2000-01-00 20:00:00' for column 'c' at row 1")
tk.MustExec("INSERT INTO dd(c) VALUES ('2000-01-02')")
tk.MustGetErrMsg("UPDATE dd SET c = '2000-01-00 20:00:00'", "[types:1292]Incorrect timestamp value: '2000-01-00 20:00:00'")
tk.MustExec("UPDATE IGNORE dd SET b = '2000-01-00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '2000-01-00'"))
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
}
}

// check !NO_ZERO_DATE
tk.MustExec("SET sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'")
{
tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(a) values('0000-00-00')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT a FROM dd").Check(testkit.Rows("0000-00-00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(b) values('2000-10-01')")
tk.MustExec("UPDATE dd SET b = '0000-00-00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(c) values('0000-00-00 00:00:00')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(c) values('2000-10-01 10:00:00')")
tk.MustExec("UPDATE dd SET c = '0000-00-00 00:00:00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustGetErrMsg("INSERT INTO dd(b) VALUES ('2000-01-00')", "[table:1292]Incorrect datetime value: '2000-01-00' for column 'b' at row 1")
tk.MustExec("INSERT IGNORE INTO dd(b) VALUES ('2000-00-01')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '2000-00-01'"))
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(b) VALUES ('2000-01-02')")
tk.MustGetErrMsg("UPDATE dd SET b = '2000-01-00'", "[types:1292]Incorrect datetime value: '2000-01-00'")
tk.MustExec("UPDATE IGNORE dd SET b = '2000-01-0'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '2000-01-0'"))
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
tk.MustExec("UPDATE dd SET b = '0000-1-2'")
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-01-02 00:00:00"))
tk.MustGetErrMsg("UPDATE dd SET c = '0000-01-05'", "[types:1292]Incorrect timestamp value: '0000-01-05'")
tk.MustExec("UPDATE IGNORE dd SET c = '0000-01-5'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '0000-01-5'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustGetErrMsg("INSERT INTO dd(c) VALUES ('2000-01-00 20:00:00')", "[table:1292]Incorrect timestamp value: '2000-01-00 20:00:00' for column 'c' at row 1")
tk.MustExec("INSERT INTO dd(c) VALUES ('2000-01-02')")
tk.MustGetErrMsg("UPDATE dd SET c = '2000-01-00 20:00:00'", "[types:1292]Incorrect timestamp value: '2000-01-00 20:00:00'")
tk.MustExec("UPDATE IGNORE dd SET b = '2000-01-00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect datetime value: '2000-01-00'"))
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
}

// check !NO_ZERO_IN_DATE
tk.MustExec("SET sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'")
{
tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(a) values('2000-00-10')")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT a FROM dd").Check(testkit.Rows("2000-00-10"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(b) values('2000-10-01')")
tk.MustExec("UPDATE dd SET b = '2000-00-10'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows())
tk.MustQuery("SELECT b FROM dd").Check(testkit.Rows("2000-00-10 00:00:00"))

tk.MustExec("TRUNCATE TABLE dd")
tk.MustExec("INSERT INTO dd(c) values('2000-10-01 10:00:00')")
tk.MustGetErrMsg("UPDATE dd SET c = '2000-00-10 00:00:00'", "[types:1292]Incorrect timestamp value: '2000-00-10 00:00:00'")
tk.MustExec("UPDATE IGNORE dd SET c = '2000-01-00 00:00:00'")
tk.MustQuery("SHOW WARNINGS").Check(testkit.Rows("Warning 1292 Incorrect timestamp value: '2000-01-00 00:00:00'"))
tk.MustQuery("SELECT c FROM dd").Check(testkit.Rows("0000-00-00 00:00:00"))
}
}
4 changes: 3 additions & 1 deletion session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func (s *testSessionSuite) TestAffectedRows(c *C) {

tk.MustExec("drop table if exists t")
tk.MustExec("create table t (id int, c1 timestamp);")
tk.MustExec(`insert t values(1, 0);`)
tk.MustExec(`insert t(id) values(1);`)
tk.MustExec(`UPDATE t set id = 1 where id = 1;`)
c.Assert(int(tk.Se.AffectedRows()), Equals, 0)

Expand Down Expand Up @@ -1019,8 +1019,10 @@ func (s *testSessionSuite) TestPrepareZero(c *C) {
_, rs := tk.Exec("execute s1 using @v1")
c.Assert(rs, NotNil)
tk.MustExec("set @v2='" + types.ZeroDatetimeStr + "'")
tk.MustExec("set @orig_sql_mode=@@sql_mode; set @@sql_mode='';")
tk.MustExec("execute s1 using @v2")
tk.MustQuery("select v from t").Check(testkit.Rows("0000-00-00 00:00:00"))
tk.MustExec("set @@sql_mode=@orig_sql_mode;")
}

func (s *testSessionSuite) TestPrimaryKeyAutoIncrement(c *C) {
Expand Down
Loading

0 comments on commit 3721f58

Please sign in to comment.