diff --git a/ddl/db_partition_test.go b/ddl/db_partition_test.go index 647c421ab6b0d..cb012e8b9668d 100644 --- a/ddl/db_partition_test.go +++ b/ddl/db_partition_test.go @@ -1304,6 +1304,18 @@ func (s *testIntegrationSuite3) TestCreateTableWithKeyPartition(c *C) { tk.MustExec(`create table tm2 (a char(5), unique key(a(5))) partition by key() partitions 5;`) } +func (s *testIntegrationSuite5) TestTry(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test;") + tk.MustExec("create table t (x int) partition by hash(x) partitions 4;") + ctx := tk.Se.(sessionctx.Context) + is := domain.GetDomain(ctx).InfoSchema() + tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t")) + c.Assert(err, IsNil) + p := tbl.Meta().Partition + fmt.Println(p) +} + func (s *testIntegrationSuite5) TestAlterTableAddPartition(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test;") diff --git a/planner/core/logical_plan_test.go b/planner/core/logical_plan_test.go index 5b65ffbb8ad79..e381da64fcdb6 100644 --- a/planner/core/logical_plan_test.go +++ b/planner/core/logical_plan_test.go @@ -59,7 +59,8 @@ type testPlanSuite struct { } func (s *testPlanSuite) SetUpSuite(c *C) { - s.is = infoschema.MockInfoSchema([]*model.TableInfo{MockSignedTable(), MockUnsignedTable(), MockView(), MockNoPKTable(), MockPartitionTable()}) + s.is = infoschema.MockInfoSchema([]*model.TableInfo{MockSignedTable(), MockUnsignedTable(), MockView(), MockNoPKTable(), + MockRangePartitionTable(), MockHashPartitionTable(), MockListPartitionTable()}) s.ctx = MockContext() domain.GetDomain(s.ctx).MockInfoCacheAndLoadInfoSchema(s.is) s.ctx.GetSessionVars().EnableWindowFunction = true diff --git a/planner/core/logical_plan_trace_test.go b/planner/core/logical_plan_trace_test.go index 7215a4f6647b4..6918105a03310 100644 --- a/planner/core/logical_plan_trace_test.go +++ b/planner/core/logical_plan_trace_test.go @@ -86,6 +86,50 @@ func (s *testPlanSuite) TestSingleRuleTraceStep(c *C) { assertRuleName string assertRuleSteps []assertTraceStep }{ + { + sql: "select * from pt3 where ptn > 3;", + flags: []uint64{flagPartitionProcessor, flagPredicatePushDown, flagBuildKeyInfo, flagPrunColumns}, + assertRuleName: "partition_processor", + assertRuleSteps: []assertTraceStep{ + { + assertReason: "Datasource[1] have multi available partition tables[p1,p2] after partition pruning", + assertAction: "Datasource[1] becomes PartitionUnion[6] with children[TableScan[1],TableScan[1]]", + }, + }, + }, + { + sql: "select * from pt3 where ptn = 1;", + flags: []uint64{flagPartitionProcessor, flagPredicatePushDown, flagBuildKeyInfo, flagPrunColumns}, + assertRuleName: "partition_processor", + assertRuleSteps: []assertTraceStep{ + { + assertReason: "Datasource[1] has one available partiton table[p1] after partition pruning", + assertAction: "Datasource[1] becomes TableScan[1]", + }, + }, + }, + { + sql: "select * from pt2 where ptn in (1,2,3);", + flags: []uint64{flagPartitionProcessor, flagPredicatePushDown, flagBuildKeyInfo, flagPrunColumns}, + assertRuleName: "partition_processor", + assertRuleSteps: []assertTraceStep{ + { + assertReason: "Datasource[1] have multi available partition tables[p1,p2] after partition pruning", + assertAction: "Datasource[1] becomes PartitionUnion[7] with children[TableScan[1],TableScan[1]]", + }, + }, + }, + { + sql: "select * from pt2 where ptn = 1;", + flags: []uint64{flagPartitionProcessor, flagPredicatePushDown, flagBuildKeyInfo, flagPrunColumns}, + assertRuleName: "partition_processor", + assertRuleSteps: []assertTraceStep{ + { + assertReason: "Datasource[1] has one available partiton table[p2] after partition pruning", + assertAction: "Datasource[1] becomes TableScan[1]", + }, + }, + }, { sql: "select * from pt1 where ptn > 100;", flags: []uint64{flagPartitionProcessor, flagPredicatePushDown, flagBuildKeyInfo, flagPrunColumns}, diff --git a/planner/core/mock.go b/planner/core/mock.go index e4f9afa830c15..4419c6f1c8b7e 100644 --- a/planner/core/mock.go +++ b/planner/core/mock.go @@ -433,8 +433,8 @@ func MockPartitionInfoSchema(definitions []model.PartitionDefinition) infoschema return is } -// MockPartitionTable mocks a partition table for test -func MockPartitionTable() *model.TableInfo { +// MockRangePartitionTable mocks a range partition table for test +func MockRangePartitionTable() *model.TableInfo { definitions := []model.PartitionDefinition{ { ID: 41, @@ -469,3 +469,83 @@ func MockPartitionTable() *model.TableInfo { tableInfo.Partition = partition return tableInfo } + +func MockHashPartitionTable() *model.TableInfo { + definitions := []model.PartitionDefinition{ + { + ID: 51, + Name: model.NewCIStr("p1"), + }, + { + ID: 52, + Name: model.NewCIStr("p2"), + }, + } + tableInfo := MockSignedTable() + tableInfo.Name = model.NewCIStr("pt2") + cols := make([]*model.ColumnInfo, 0, len(tableInfo.Columns)) + cols = append(cols, tableInfo.Columns...) + last := tableInfo.Columns[len(tableInfo.Columns)-1] + cols = append(cols, &model.ColumnInfo{ + State: model.StatePublic, + Offset: last.Offset + 1, + Name: model.NewCIStr("ptn"), + FieldType: newLongType(), + ID: last.ID + 1, + }) + partition := &model.PartitionInfo{ + Type: model.PartitionTypeHash, + Expr: "ptn", + Enable: true, + Definitions: definitions, + Num: 2, + } + tableInfo.Columns = cols + tableInfo.Partition = partition + return tableInfo +} + +func MockListPartitionTable() *model.TableInfo { + definitions := []model.PartitionDefinition{ + { + ID: 61, + Name: model.NewCIStr("p1"), + InValues: [][]string{ + { + "1", + }, + }, + }, + { + ID: 62, + Name: model.NewCIStr("p2"), + InValues: [][]string{ + { + "2", + }, + }, + }, + } + tableInfo := MockSignedTable() + tableInfo.Name = model.NewCIStr("pt3") + cols := make([]*model.ColumnInfo, 0, len(tableInfo.Columns)) + cols = append(cols, tableInfo.Columns...) + last := tableInfo.Columns[len(tableInfo.Columns)-1] + cols = append(cols, &model.ColumnInfo{ + State: model.StatePublic, + Offset: last.Offset + 1, + Name: model.NewCIStr("ptn"), + FieldType: newLongType(), + ID: last.ID + 1, + }) + partition := &model.PartitionInfo{ + Type: model.PartitionTypeList, + Expr: "ptn", + Enable: true, + Definitions: definitions, + Num: 2, + } + tableInfo.Columns = cols + tableInfo.Partition = partition + return tableInfo +}