Skip to content

Commit

Permalink
planner: use a deep copy for accessPath when clone DataSource (#13620)
Browse files Browse the repository at this point in the history
  • Loading branch information
francis0407 authored Nov 20, 2019
1 parent 1d527fb commit 8a4c016
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
24 changes: 24 additions & 0 deletions planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,27 @@ func (s *testIntegrationSuite) TestNoneAccessPathsFoundByIsolationRead(c *C) {
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[planner:1815]Internal : Can not find access path matching 'tidb_isolation_read_engines'(value: 'tiflash'). Available values are 'tikv'.")
}

func (s *testIntegrationSuite) TestPartitionTableStats(c *C) {
tk := testkit.NewTestKit(c, s.store)

tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int)partition by range columns(a)(partition p0 values less than (10), partition p1 values less than(20), partition p2 values less than(30));")
tk.MustExec("insert into t values(21, 1), (22, 2), (23, 3), (24, 4), (15, 5)")
tk.MustExec("analyze table t")

var input []string
var output []struct {
SQL string
Result []string
}
s.testData.GetTestCases(c, &input, &output)
for i, tt := range input {
s.testData.OnRecord(func() {
output[i].SQL = tt
output[i].Result = s.testData.ConvertRowsToStrings(tk.MustQuery(tt).Rows())
})
tk.MustQuery(tt).Check(testkit.Rows(output[i].Result...))
}
}
5 changes: 5 additions & 0 deletions planner/core/rule_partition_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ func (s *partitionProcessor) prune(ds *DataSource) (LogicalPlan, error) {
newDataSource.baseLogicalPlan = newBaseLogicalPlan(ds.SCtx(), plancodec.TypeTableScan, &newDataSource, ds.blockOffset)
newDataSource.isPartition = true
newDataSource.physicalTableID = pi.Definitions[i].ID
newDataSource.possibleAccessPaths = make([]*accessPath, len(ds.possibleAccessPaths))
for i := range ds.possibleAccessPaths {
newPath := *ds.possibleAccessPaths[i]
newDataSource.possibleAccessPaths[i] = &newPath
}
// There are many expression nodes in the plan tree use the original datasource
// id as FromID. So we set the id of the newDataSource with the original one to
// avoid traversing the whole plan tree to update the references.
Expand Down
9 changes: 9 additions & 0 deletions planner/core/testdata/integration_suite_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,14 @@
// LeftOuterJoin should no be simplified to InnerJoin.
"explain select * from t t1 left join t t2 on t1.a = t2.a where cast(t1.b as date) >= '2019-01-01'"
]
},
{
"name": "TestPartitionTableStats",
"cases": [
"explain select * from t order by a",
"select * from t order by a",
"explain select * from t order by a limit 3",
"select * from t order by a limit 3"
]
}
]
54 changes: 54 additions & 0 deletions planner/core/testdata/integration_suite_out.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,59 @@
]
}
]
},
{
"Name": "TestPartitionTableStats",
"Cases": [
{
"SQL": "explain select * from t order by a",
"Result": [
"Sort_8 10005.00 root Column#1:asc",
"└─Union_11 10005.00 root ",
" ├─TableReader_13 10000.00 root data:TableScan_12",
" │ └─TableScan_12 10000.00 cop[tikv] table:t, partition:p0, range:[-inf,+inf], keep order:false, stats:pseudo",
" ├─TableReader_15 1.00 root data:TableScan_14",
" │ └─TableScan_14 1.00 cop[tikv] table:t, partition:p1, range:[-inf,+inf], keep order:false",
" └─TableReader_17 4.00 root data:TableScan_16",
" └─TableScan_16 4.00 cop[tikv] table:t, partition:p2, range:[-inf,+inf], keep order:false"
]
},
{
"SQL": "select * from t order by a",
"Result": [
"15 5",
"21 1",
"22 2",
"23 3",
"24 4"
]
},
{
"SQL": "explain select * from t order by a limit 3",
"Result": [
"TopN_16 3.00 root Column#1:asc, offset:0, count:3",
"└─Union_20 7.00 root ",
" ├─TopN_21 3.00 root Column#1:asc, offset:0, count:3",
" │ └─TableReader_29 3.00 root data:TopN_28",
" │ └─TopN_28 3.00 cop[tikv] Column#1:asc, offset:0, count:3",
" │ └─TableScan_27 10000.00 cop[tikv] table:t, partition:p0, range:[-inf,+inf], keep order:false, stats:pseudo",
" ├─TopN_34 1.00 root Column#1:asc, offset:0, count:3",
" │ └─TableReader_42 1.00 root data:TableScan_41",
" │ └─TableScan_41 1.00 cop[tikv] table:t, partition:p1, range:[-inf,+inf], keep order:false",
" └─TopN_43 3.00 root Column#1:asc, offset:0, count:3",
" └─TableReader_51 3.00 root data:TopN_50",
" └─TopN_50 3.00 cop[tikv] Column#1:asc, offset:0, count:3",
" └─TableScan_49 4.00 cop[tikv] table:t, partition:p2, range:[-inf,+inf], keep order:false"
]
},
{
"SQL": "select * from t order by a limit 3",
"Result": [
"15 5",
"21 1",
"22 2"
]
}
]
}
]

0 comments on commit 8a4c016

Please sign in to comment.