diff --git a/executor/executor_test.go b/executor/executor_test.go index 13efa78e0eecb..02961c018c7d5 100644 --- a/executor/executor_test.go +++ b/executor/executor_test.go @@ -4428,3 +4428,168 @@ func (s *testSuite1) TestPartitionHashCode(c *C) { } wg.Wait() } +<<<<<<< HEAD +======= + +func (s *testSuite1) TestAlterDefaultValue(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("drop table if exists t1") + tk.MustExec("create table t(a int, primary key(a))") + tk.MustExec("insert into t(a) values(1)") + tk.MustExec("alter table t add column b int default 1") + tk.MustExec("alter table t alter b set default 2") + tk.MustQuery("select b from t where a = 1").Check(testkit.Rows("1")) +} + +type testClusterTableSuite struct { + testSuiteWithCliBase + rpcserver *grpc.Server + listenAddr string +} + +func (s *testClusterTableSuite) SetUpSuite(c *C) { + s.testSuiteWithCliBase.SetUpSuite(c) + s.rpcserver, s.listenAddr = s.setUpRPCService(c, ":0") +} + +func (s *testClusterTableSuite) setUpRPCService(c *C, addr string) (*grpc.Server, string) { + sm := &mockSessionManager1{} + sm.PS = append(sm.PS, &util.ProcessInfo{ + ID: 1, + User: "root", + Host: "127.0.0.1", + Command: mysql.ComQuery, + }) + lis, err := net.Listen("tcp", addr) + c.Assert(err, IsNil) + srv := server.NewRPCServer(config.GetGlobalConfig(), s.dom, sm) + port := lis.Addr().(*net.TCPAddr).Port + addr = fmt.Sprintf("127.0.0.1:%d", port) + go func() { + err = srv.Serve(lis) + c.Assert(err, IsNil) + }() + cfg := config.GetGlobalConfig() + cfg.Status.StatusPort = uint(port) + config.StoreGlobalConfig(cfg) + return srv, addr +} +func (s *testClusterTableSuite) TearDownSuite(c *C) { + if s.rpcserver != nil { + s.rpcserver.Stop() + s.rpcserver = nil + } + s.testSuiteWithCliBase.TearDownSuite(c) +} + +func (s *testClusterTableSuite) TestSlowQuery(c *C) { + writeFile := func(file string, data string) { + f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY, 0644) + c.Assert(err, IsNil) + _, err = f.Write([]byte(data)) + c.Assert(f.Close(), IsNil) + c.Assert(err, IsNil) + } + + logData0 := "" + logData1 := ` +# Time: 2020-02-15T18:00:01.000000+08:00 +select 1; +# Time: 2020-02-15T19:00:05.000000+08:00 +select 2;` + logData2 := ` +# Time: 2020-02-16T18:00:01.000000+08:00 +select 3; +# Time: 2020-02-16T18:00:05.000000+08:00 +select 4;` + logData3 := ` +# Time: 2020-02-16T19:00:00.000000+08:00 +select 5; +# Time: 2020-02-17T18:00:05.000000+08:00 +select 6;` + fileName0 := "tidb-slow-2020-02-14T19-04-05.01.log" + fileName1 := "tidb-slow-2020-02-15T19-04-05.01.log" + fileName2 := "tidb-slow-2020-02-16T19-04-05.01.log" + fileName3 := "tidb-slow.log" + writeFile(fileName0, logData0) + writeFile(fileName1, logData1) + writeFile(fileName2, logData2) + writeFile(fileName3, logData3) + defer func() { + os.Remove(fileName0) + os.Remove(fileName1) + os.Remove(fileName2) + os.Remove(fileName3) + }() + tk := testkit.NewTestKitWithInit(c, s.store) + loc, err := time.LoadLocation("Asia/Shanghai") + c.Assert(err, IsNil) + tk.Se.GetSessionVars().TimeZone = loc + tk.MustExec("use information_schema") + cases := []struct { + sql string + result []string + }{ + { + sql: "select count(*),min(time),max(time) from %s where time > '2019-01-26 21:51:00' and time < now()", + result: []string{"6|2020-02-15 18:00:01.000000|2020-02-17 18:00:05.000000"}, + }, + { + sql: "select count(*),min(time),max(time) from %s where time > '2020-02-15 19:00:00' and time < '2020-02-16 18:00:02'", + result: []string{"2|2020-02-15 19:00:05.000000|2020-02-16 18:00:01.000000"}, + }, + { + sql: "select count(*),min(time),max(time) from %s where time > '2020-02-16 18:00:02' and time < '2020-02-17 17:00:00'", + result: []string{"2|2020-02-16 18:00:05.000000|2020-02-16 19:00:00.000000"}, + }, + { + sql: "select count(*),min(time),max(time) from %s where time > '2020-02-16 18:00:02' and time < '2020-02-17 20:00:00'", + result: []string{"3|2020-02-16 18:00:05.000000|2020-02-17 18:00:05.000000"}, + }, + { + sql: "select count(*),min(time),max(time) from %s", + result: []string{"2|2020-02-16 19:00:00.000000|2020-02-17 18:00:05.000000"}, + }, + { + sql: "select count(*),min(time) from %s where time > '2020-02-16 20:00:00'", + result: []string{"1|2020-02-17 18:00:05.000000"}, + }, + { + sql: "select count(*) from %s where time > '2020-02-17 20:00:00'", + result: []string{"0"}, + }, + { + sql: "select query from %s where time > '2019-01-26 21:51:00' and time < now()", + result: []string{"select 1;", "select 2;", "select 3;", "select 4;", "select 5;", "select 6;"}, + }, + } + for _, cas := range cases { + sql := fmt.Sprintf(cas.sql, "slow_query") + tk.MustQuery(sql).Check(testutil.RowsWithSep("|", cas.result...)) + sql = fmt.Sprintf(cas.sql, "cluster_slow_query") + tk.MustQuery(sql).Check(testutil.RowsWithSep("|", cas.result...)) + } +} + +func (s *testSuite1) TestIssue15718(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists tt;") + tk.MustExec("create table tt(a decimal(10, 0), b varchar(1), c time);") + tk.MustExec("insert into tt values(0, '2', null), (7, null, '1122'), (NULL, 'w', null), (NULL, '2', '3344'), (NULL, NULL, '0'), (7, 'f', '33');") + tk.MustQuery("select a and b as d, a or c as e from tt;").Check(testkit.Rows("0 ", " 1", "0 ", " 1", " ", "0 1")) +} + +func (s *testSuite1) TestIssue15767(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists tt;") + tk.MustExec("create table t(a int, b char);") + tk.MustExec("insert into t values (1,'s'),(2,'b'),(1,'c'),(2,'e'),(1,'a');") + tk.MustExec("insert into t select * from t;") + tk.MustExec("insert into t select * from t;") + tk.MustExec("insert into t select * from t;") + tk.MustQuery("select b, count(*) from ( select b from t order by a limit 20 offset 2) as s group by b order by b;").Check(testkit.Rows("a 6", "c 7", "s 7")) +} +>>>>>>> 6a45a7d... mocktikv: avoid sorting multiple times with unstable order (#15898) diff --git a/store/mockstore/mocktikv/executor.go b/store/mockstore/mocktikv/executor.go index 7808e18cff554..522b23a2b64e3 100644 --- a/store/mockstore/mocktikv/executor.go +++ b/store/mockstore/mocktikv/executor.go @@ -562,6 +562,7 @@ func (e *topNExec) Next(ctx context.Context) (value [][]byte, err error) { return nil, errors.Trace(err) } if !hasMore { + sort.Sort(&e.heap.topNSorter) break } } @@ -570,7 +571,6 @@ func (e *topNExec) Next(ctx context.Context) (value [][]byte, err error) { if e.cursor >= len(e.heap.rows) { return nil, nil } - sort.Sort(&e.heap.topNSorter) row := e.heap.rows[e.cursor] e.cursor++