From 18d69e521f06d46d9917dec6d5d2e418e2861694 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Mon, 28 Jun 2021 16:31:25 +0800 Subject: [PATCH] executor: fix incorrect rows returned by `TABLESAMPLE` (#25357) (#25795) --- executor/sample.go | 2 +- executor/sample_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/executor/sample.go b/executor/sample.go index 2edd189c02cc9..a2447557d6342 100644 --- a/executor/sample.go +++ b/executor/sample.go @@ -220,7 +220,7 @@ func splitIntoMultiRanges(store kv.Storage, startKey, endKey kv.Key) ([]kv.KeyRa if kv.Key(start).Cmp(startKey) < 0 { start = startKey } - if kv.Key(end).Cmp(endKey) > 0 { + if end == nil || kv.Key(end).Cmp(endKey) > 0 { end = endKey } ranges = append(ranges, kv.KeyRange{StartKey: start, EndKey: end}) diff --git a/executor/sample_test.go b/executor/sample_test.go index 6760fc5135d35..42aed46448881 100644 --- a/executor/sample_test.go +++ b/executor/sample_test.go @@ -119,6 +119,20 @@ func (s *testTableSampleSuite) TestTableSampleMultiRegions(c *C) { tk.MustExec("drop table t2;") } +func (s *testTableSampleSuite) TestTableSampleNoSplitTable(c *C) { + tk := s.initSampleTest(c) + atomic.StoreUint32(&ddl.EnableSplitTableRegion, 0) + tk.MustExec("drop table if exists t1;") + tk.MustExec("drop table if exists t2;") + tk.MustExec("create table t1 (id int primary key);") + tk.MustExec("create table t2 (id int primary key);") + tk.MustExec("insert into t2 values(1);") + rows := tk.MustQuery("select * from t1 tablesample regions();").Rows() + rows2 := tk.MustQuery("select * from t2 tablesample regions();").Rows() + c.Assert(len(rows), Equals, 0) + c.Assert(len(rows2), Equals, 1) +} + func (s *testTableSampleSuite) TestTableSampleSchema(c *C) { tk := s.initSampleTest(c) tk.Se.GetSessionVars().EnableClusteredIndex = variable.ClusteredIndexDefModeOn