Skip to content

Commit

Permalink
planner: fix tablesample 'order by' clause from partitioned table (#2…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Sep 3, 2021
1 parent 23b180f commit 1b5c1e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
11 changes: 11 additions & 0 deletions executor/sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,17 @@ func (s *testTableSampleSuite) TestTableSampleWithPartition(c *C) {
c.Assert(len(rows), Equals, 0)
rows = tk.MustQuery("select * from t partition (p1) tablesample regions();").Rows()
c.Assert(len(rows), Equals, 1)

// Test https://github.com/pingcap/tidb/issues/27349.
tk.MustExec("drop table if exists t;")
tk.MustExec(`create table t (a int, b int, unique key idx(a)) partition by range (a) (
partition p0 values less than (0),
partition p1 values less than (10),
partition p2 values less than (30),
partition p3 values less than (maxvalue));`)
tk.MustExec("insert into t values (2, 2), (31, 31), (12, 12);")
tk.MustQuery("select _tidb_rowid from t tablesample regions() order by _tidb_rowid;").
Check(testkit.Rows("1", "2", "3")) // The order of _tidb_rowid should be correct.
}

func (s *testTableSampleSuite) TestTableSampleGeneratedColumns(c *C) {
Expand Down
6 changes: 6 additions & 0 deletions planner/core/find_best_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,12 @@ func (ds *DataSource) convertToSampleTable(prop *property.PhysicalProperty, cand
if !prop.IsEmpty() && !candidate.isMatchProp {
return invalidTask, nil
}
if candidate.isMatchProp {
// TableSample on partition table can't keep order.
if ds.tableInfo.GetPartitionInfo() != nil {
return invalidTask, nil
}
}
p := PhysicalTableSample{
TableSampleInfo: ds.SampleInfo,
TableInfo: ds.table,
Expand Down

0 comments on commit 1b5c1e6

Please sign in to comment.