From c7adba54e67935a6a033b6c882e7539cdd492569 Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 20 Dec 2022 09:44:54 +0100 Subject: [PATCH 1/6] This is an automated cherry-pick of #40003 Signed-off-by: ti-chi-bot --- executor/builder.go | 99 ++++++++++++++++++------------ executor/index_lookup_join_test.go | 14 +++++ executor/partition_table_test.go | 91 +++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 39 deletions(-) diff --git a/executor/builder.go b/executor/builder.go index eae7d7f0e435c..76459fb572003 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -3221,17 +3221,39 @@ func buildIndexRangeForEachPartition(ctx sessionctx.Context, usedPartitions []ta return nextRange, nil } -func keyColumnsIncludeAllPartitionColumns(keyColumns []int, pe *tables.PartitionExpr) bool { - tmp := make(map[int]struct{}, len(keyColumns)) - for _, offset := range keyColumns { - tmp[offset] = struct{}{} +func getPartitionKeyColOffsets(keyColIDs []int64, pt table.PartitionedTable) []int { + keyColOffsets := make([]int, len(keyColIDs)) + for i, colID := range keyColIDs { + offset := -1 + for j, col := range pt.Cols() { + if colID == col.ID { + offset = j + break + } + } + if offset == -1 { + return nil + } + keyColOffsets[i] = offset + } + + pe, err := pt.(interface { + PartitionExpr() (*tables.PartitionExpr, error) + }).PartitionExpr() + if err != nil { + return nil + } + + offsetMap := make(map[int]struct{}) + for _, offset := range keyColOffsets { + offsetMap[offset] = struct{}{} } for _, offset := range pe.ColumnOffset { - if _, ok := tmp[offset]; !ok { - return false + if _, ok := offsetMap[offset]; !ok { + return nil } } - return true + return keyColOffsets } func prunePartitionForInnerExecutor(ctx sessionctx.Context, tbl table.Table, schema *expression.Schema, partitionInfo *plannercore.PartitionInfo, @@ -3243,15 +3265,6 @@ func prunePartitionForInnerExecutor(ctx sessionctx.Context, tbl table.Table, sch return nil, false, nil, err } - // check whether can runtime prune. - type partitionExpr interface { - PartitionExpr() (*tables.PartitionExpr, error) - } - pe, err := tbl.(partitionExpr).PartitionExpr() - if err != nil { - return nil, false, nil, err - } - // recalculate key column offsets if len(lookUpContent) == 0 { return nil, false, nil, nil @@ -3259,29 +3272,9 @@ func prunePartitionForInnerExecutor(ctx sessionctx.Context, tbl table.Table, sch if lookUpContent[0].keyColIDs == nil { return nil, false, nil, plannercore.ErrInternal.GenWithStack("cannot get column IDs when dynamic pruning") } - keyColOffsets := make([]int, len(lookUpContent[0].keyColIDs)) - for i, colID := range lookUpContent[0].keyColIDs { - offset := -1 - for j, col := range partitionTbl.Cols() { - if colID == col.ID { - offset = j - break - } - } - if offset == -1 { - return nil, false, nil, plannercore.ErrInternal.GenWithStack("invalid column offset when dynamic pruning") - } - keyColOffsets[i] = offset - } - - offsetMap := make(map[int]bool) - for _, offset := range keyColOffsets { - offsetMap[offset] = true - } - for _, offset := range pe.ColumnOffset { - if _, ok := offsetMap[offset]; !ok { - return condPruneResult, false, nil, nil - } + keyColOffsets := getPartitionKeyColOffsets(lookUpContent[0].keyColIDs, partitionTbl) + if len(keyColOffsets) == 0 { + return condPruneResult, false, nil, nil } locateKey := make([]types.Datum, len(partitionTbl.Cols())) @@ -3802,6 +3795,7 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte } return builder.buildTableReaderFromKvRanges(ctx, e, kvRanges) } +<<<<<<< HEAD tbl, _ := builder.is.TableByID(tbInfo.ID) pt := tbl.(table.PartitionedTable) @@ -3814,6 +3808,29 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte var kvRanges []kv.KeyRange if len(lookUpContents) > 0 && keyColumnsIncludeAllPartitionColumns(lookUpContents[0].keyCols, pe) { // In this case we can use dynamic partition pruning. +======= + handles, _ := dedupHandles(lookUpContents) + return builder.buildTableReaderFromHandles(ctx, e, handles, canReorderHandles) + } + tbl, _ := builder.is.TableByID(tbInfo.ID) + pt := tbl.(table.PartitionedTable) + partitionInfo := &v.PartitionInfo + usedPartitionList, err := builder.partitionPruning(pt, partitionInfo.PruningConds, partitionInfo.PartitionNames, partitionInfo.Columns, partitionInfo.ColumnNames) + if err != nil { + return nil, err + } + usedPartitions := make(map[int64]table.PhysicalTable, len(usedPartitionList)) + for _, p := range usedPartitionList { + usedPartitions[p.GetPhysicalID()] = p + } + var kvRanges []kv.KeyRange + var keyColOffsets []int + if len(lookUpContents) > 0 { + keyColOffsets = getPartitionKeyColOffsets(lookUpContents[0].keyColIDs, pt) + } + if v.IsCommonHandle { + if len(keyColOffsets) > 0 { +>>>>>>> 4a72171ffb (*: Fix issue 39999, used wrong column id list for checking partitions (#40003)) locateKey := make([]types.Datum, e.Schema().Len()) kvRanges = make([]kv.KeyRange, 0, len(lookUpContents)) for _, content := range lookUpContents { @@ -3858,6 +3875,7 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte return builder.buildTableReaderFromHandles(ctx, e, handles, canReorderHandles) } +<<<<<<< HEAD tbl, _ := builder.is.TableByID(tbInfo.ID) pt := tbl.(table.PartitionedTable) pe, err := tbl.(interface { @@ -3868,6 +3886,9 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte } var kvRanges []kv.KeyRange if len(lookUpContents) > 0 && keyColumnsIncludeAllPartitionColumns(lookUpContents[0].keyCols, pe) { +======= + if len(keyColOffsets) > 0 { +>>>>>>> 4a72171ffb (*: Fix issue 39999, used wrong column id list for checking partitions (#40003)) locateKey := make([]types.Datum, e.Schema().Len()) kvRanges = make([]kv.KeyRange, 0, len(lookUpContents)) for _, content := range lookUpContents { diff --git a/executor/index_lookup_join_test.go b/executor/index_lookup_join_test.go index 262190dd1ac81..a8b3e925791eb 100644 --- a/executor/index_lookup_join_test.go +++ b/executor/index_lookup_join_test.go @@ -438,8 +438,11 @@ PARTITIONS 1`) // Why does the t2.prefiller need be at least 2^32 ? If smaller the bug will not appear!?! tk.MustExec("insert into t2 values ( pow(2,32), 1, 1), ( pow(2,32)+1, 2, 0)") + tk.MustExec(`analyze table t1`) + tk.MustExec(`analyze table t2`) // Why must it be = 1 and not 2? +<<<<<<< HEAD tk.MustQuery("explain select /* +INL_JOIN(t1,t2) */ t1.id, t1.pc from t1 where id in ( select prefiller from t2 where t2.postfiller = 1 )").Check(testkit.Rows("" + "IndexJoin_15 10.00 root inner join, inner:TableReader_14, outer key:test.t2.prefiller, inner key:test.t1.id, equal cond:eq(test.t2.prefiller, test.t1.id)]\n" + "[├─HashAgg_25(Build) 8.00 root group by:test.t2.prefiller, funcs:firstrow(test.t2.prefiller)->test.t2.prefiller]\n" + @@ -449,6 +452,17 @@ PARTITIONS 1`) "[│ └─TableFullScan_23 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo]\n" + "[└─TableReader_14(Probe) 1.00 root partition:all data:TableRangeScan_13]\n" + "[ └─TableRangeScan_13 1.00 cop[tikv] table:t1 range: decided by [test.t2.prefiller], keep order:false, stats:pseudo")) +======= + tk.MustQuery("explain format='brief' select /* +INL_JOIN(t1,t2) */ t1.id, t1.pc from t1 where id in ( select prefiller from t2 where t2.postfiller = 1 )").Check(testkit.Rows(""+ + `IndexJoin 1.25 root inner join, inner:TableReader, outer key:test.t2.prefiller, inner key:test.t1.id, equal cond:eq(test.t2.prefiller, test.t1.id)`, + `├─HashAgg(Build) 1.00 root group by:test.t2.prefiller, funcs:firstrow(test.t2.prefiller)->test.t2.prefiller`, + `│ └─TableReader 1.00 root data:HashAgg`, + `│ └─HashAgg 1.00 cop[tikv] group by:test.t2.prefiller, `, + `│ └─Selection 1.00 cop[tikv] eq(test.t2.postfiller, 1)`, + `│ └─TableFullScan 2.00 cop[tikv] table:t2 keep order:false`, + `└─TableReader(Probe) 1.00 root partition:all data:TableRangeScan`, + ` └─TableRangeScan 1.00 cop[tikv] table:t1 range: decided by [eq(test.t1.id, test.t2.prefiller)], keep order:false, stats:pseudo`)) +>>>>>>> 4a72171ffb (*: Fix issue 39999, used wrong column id list for checking partitions (#40003)) tk.MustQuery("show warnings").Check(testkit.Rows()) // without fix it fails with: "runtime error: index out of range [0] with length 0" tk.MustQuery("select /* +INL_JOIN(t1,t2) */ t1.id, t1.pc from t1 where id in ( select prefiller from t2 where t2.postfiller = 1 )").Check(testkit.Rows()) diff --git a/executor/partition_table_test.go b/executor/partition_table_test.go index 534260a3ac8d8..c21fbafa52336 100644 --- a/executor/partition_table_test.go +++ b/executor/partition_table_test.go @@ -3132,3 +3132,94 @@ func (s *partitionTableSuite) TestIssue35181(c *C) { tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'") tk.MustExec(`insert into t select * from t where a=3000`) } +<<<<<<< HEAD +======= + +func TestIssue21732(t *testing.T) { + store := testkit.CreateMockStore(t) + + tk := testkit.NewTestKit(t, store) + for _, mode := range []variable.PartitionPruneMode{variable.StaticOnly, variable.DynamicOnly} { + testkit.WithPruneMode(tk, mode, func() { + tk.MustExec("create database TestIssue21732") + tk.MustExec("use TestIssue21732") + tk.MustExec("drop table if exists p") + tk.MustExec(`create table p (a int, b int GENERATED ALWAYS AS (3*a-2*a) VIRTUAL) partition by hash(b) partitions 2;`) + tk.MustExec("alter table p add unique index idx (a, b);") + tk.MustExec("insert into p (a) values (1),(2),(3);") + tk.MustExec("select * from p ignore index (idx);") + tk.MustQuery("select * from p use index (idx)").Sort().Check(testkit.Rows("1 1", "2 2", "3 3")) + tk.MustExec("drop database TestIssue21732") + }) + } +} + +func TestIssue39999(t *testing.T) { + store := testkit.CreateMockStore(t) + + tk := testkit.NewTestKit(t, store) + + tk.MustExec(`create schema test39999`) + tk.MustExec(`use test39999`) + tk.MustExec(`drop table if exists c, t`) + tk.MustExec("CREATE TABLE `c` (" + + "`serial_id` varchar(24)," + + "`occur_trade_date` date," + + "`txt_account_id` varchar(24)," + + "`capital_sub_class` varchar(10)," + + "`occur_amount` decimal(16,2)," + + "`broker` varchar(10)," + + "PRIMARY KEY (`txt_account_id`,`occur_trade_date`,`serial_id`) /*T![clustered_index] CLUSTERED */," + + "KEY `idx_serial_id` (`serial_id`)" + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci " + + "PARTITION BY RANGE COLUMNS(`serial_id`) (" + + "PARTITION `p202209` VALUES LESS THAN ('20221001')," + + "PARTITION `p202210` VALUES LESS THAN ('20221101')," + + "PARTITION `p202211` VALUES LESS THAN ('20221201')" + + ")") + + tk.MustExec("CREATE TABLE `t` ( " + + "`txn_account_id` varchar(24), " + + "`account_id` varchar(32), " + + "`broker` varchar(10), " + + "PRIMARY KEY (`txn_account_id`) /*T![clustered_index] CLUSTERED */ " + + ") ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci") + + tk.MustExec("INSERT INTO `c` (serial_id, txt_account_id, capital_sub_class, occur_trade_date, occur_amount, broker) VALUES ('2022111700196920','04482786','CUST','2022-11-17',-2.01,'0009')") + tk.MustExec("INSERT INTO `t` VALUES ('04482786','1142927','0009')") + + tk.MustExec(`set tidb_partition_prune_mode='dynamic'`) + tk.MustExec(`analyze table c`) + tk.MustExec(`analyze table t`) + query := `select + /*+ inl_join(c) */ + c.occur_amount +from + c + join t on c.txt_account_id = t.txn_account_id + and t.broker = '0009' + and c.occur_trade_date = '2022-11-17'` + tk.MustQuery("explain " + query).Check(testkit.Rows(""+ + "IndexJoin_22 1.00 root inner join, inner:TableReader_21, outer key:test39999.t.txn_account_id, inner key:test39999.c.txt_account_id, equal cond:eq(test39999.t.txn_account_id, test39999.c.txt_account_id)", + "├─TableReader_27(Build) 1.00 root data:Selection_26", + "│ └─Selection_26 1.00 cop[tikv] eq(test39999.t.broker, \"0009\")", + "│ └─TableFullScan_25 1.00 cop[tikv] table:t keep order:false", + "└─TableReader_21(Probe) 1.00 root partition:all data:Selection_20", + " └─Selection_20 1.00 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)", + " └─TableRangeScan_19 1.00 cop[tikv] table:c range: decided by [eq(test39999.c.txt_account_id, test39999.t.txn_account_id) eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)], keep order:false")) + tk.MustQuery(query).Check(testkit.Rows("-2.01")) + + // Add the missing partition key part. + tk.MustExec(`alter table t add column serial_id varchar(24) default '2022111700196920'`) + query += ` and c.serial_id = t.serial_id` + tk.MustQuery(query).Check(testkit.Rows("-2.01")) + tk.MustQuery("explain " + query).Check(testkit.Rows(""+ + `IndexJoin_20 0.80 root inner join, inner:TableReader_19, outer key:test39999.t.txn_account_id, test39999.t.serial_id, inner key:test39999.c.txt_account_id, test39999.c.serial_id, equal cond:eq(test39999.t.serial_id, test39999.c.serial_id), eq(test39999.t.txn_account_id, test39999.c.txt_account_id)`, + `├─TableReader_25(Build) 0.80 root data:Selection_24`, + `│ └─Selection_24 0.80 cop[tikv] eq(test39999.t.broker, "0009"), not(isnull(test39999.t.serial_id))`, + `│ └─TableFullScan_23 1.00 cop[tikv] table:t keep order:false`, + `└─TableReader_19(Probe) 0.80 root partition:all data:Selection_18`, + ` └─Selection_18 0.80 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)`, + ` └─TableRangeScan_17 0.80 cop[tikv] table:c range: decided by [eq(test39999.c.txt_account_id, test39999.t.txn_account_id) eq(test39999.c.serial_id, test39999.t.serial_id) eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)], keep order:false`)) +} +>>>>>>> 4a72171ffb (*: Fix issue 39999, used wrong column id list for checking partitions (#40003)) From 923bc35f94644ad5ec3e10273d2f81da7d983206 Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 20 Dec 2022 14:27:56 +0000 Subject: [PATCH 2/6] Fixed merge issues --- executor/builder.go | 50 ++++++------------------------ executor/index_lookup_join_test.go | 21 +++---------- executor/partition_table_test.go | 28 ++--------------- 3 files changed, 15 insertions(+), 84 deletions(-) diff --git a/executor/builder.go b/executor/builder.go index 76459fb572003..3092000ac9f57 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -3795,42 +3795,16 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte } return builder.buildTableReaderFromKvRanges(ctx, e, kvRanges) } -<<<<<<< HEAD tbl, _ := builder.is.TableByID(tbInfo.ID) pt := tbl.(table.PartitionedTable) - pe, err := tbl.(interface { - PartitionExpr() (*tables.PartitionExpr, error) - }).PartitionExpr() - if err != nil { - return nil, err - } var kvRanges []kv.KeyRange - if len(lookUpContents) > 0 && keyColumnsIncludeAllPartitionColumns(lookUpContents[0].keyCols, pe) { - // In this case we can use dynamic partition pruning. -======= - handles, _ := dedupHandles(lookUpContents) - return builder.buildTableReaderFromHandles(ctx, e, handles, canReorderHandles) - } - tbl, _ := builder.is.TableByID(tbInfo.ID) - pt := tbl.(table.PartitionedTable) - partitionInfo := &v.PartitionInfo - usedPartitionList, err := builder.partitionPruning(pt, partitionInfo.PruningConds, partitionInfo.PartitionNames, partitionInfo.Columns, partitionInfo.ColumnNames) - if err != nil { - return nil, err - } - usedPartitions := make(map[int64]table.PhysicalTable, len(usedPartitionList)) - for _, p := range usedPartitionList { - usedPartitions[p.GetPhysicalID()] = p - } - var kvRanges []kv.KeyRange - var keyColOffsets []int - if len(lookUpContents) > 0 { - keyColOffsets = getPartitionKeyColOffsets(lookUpContents[0].keyColIDs, pt) - } - if v.IsCommonHandle { + var keyColOffsets []int + if len(lookUpContents) > 0 { + keyColOffsets = getPartitionKeyColOffsets(lookUpContents[0].keyColIDs, pt) + } if len(keyColOffsets) > 0 { ->>>>>>> 4a72171ffb (*: Fix issue 39999, used wrong column id list for checking partitions (#40003)) + // In this case we can use dynamic partition pruning. locateKey := make([]types.Datum, e.Schema().Len()) kvRanges = make([]kv.KeyRange, 0, len(lookUpContents)) for _, content := range lookUpContents { @@ -3875,20 +3849,14 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte return builder.buildTableReaderFromHandles(ctx, e, handles, canReorderHandles) } -<<<<<<< HEAD tbl, _ := builder.is.TableByID(tbInfo.ID) pt := tbl.(table.PartitionedTable) - pe, err := tbl.(interface { - PartitionExpr() (*tables.PartitionExpr, error) - }).PartitionExpr() - if err != nil { - return nil, err - } var kvRanges []kv.KeyRange - if len(lookUpContents) > 0 && keyColumnsIncludeAllPartitionColumns(lookUpContents[0].keyCols, pe) { -======= + var keyColOffsets []int + if len(lookUpContents) > 0 { + keyColOffsets = getPartitionKeyColOffsets(lookUpContents[0].keyColIDs, pt) + } if len(keyColOffsets) > 0 { ->>>>>>> 4a72171ffb (*: Fix issue 39999, used wrong column id list for checking partitions (#40003)) locateKey := make([]types.Datum, e.Schema().Len()) kvRanges = make([]kv.KeyRange, 0, len(lookUpContents)) for _, content := range lookUpContents { diff --git a/executor/index_lookup_join_test.go b/executor/index_lookup_join_test.go index a8b3e925791eb..be7bd780c5079 100644 --- a/executor/index_lookup_join_test.go +++ b/executor/index_lookup_join_test.go @@ -442,27 +442,14 @@ PARTITIONS 1`) tk.MustExec(`analyze table t2`) // Why must it be = 1 and not 2? -<<<<<<< HEAD - tk.MustQuery("explain select /* +INL_JOIN(t1,t2) */ t1.id, t1.pc from t1 where id in ( select prefiller from t2 where t2.postfiller = 1 )").Check(testkit.Rows("" + - "IndexJoin_15 10.00 root inner join, inner:TableReader_14, outer key:test.t2.prefiller, inner key:test.t1.id, equal cond:eq(test.t2.prefiller, test.t1.id)]\n" + - "[├─HashAgg_25(Build) 8.00 root group by:test.t2.prefiller, funcs:firstrow(test.t2.prefiller)->test.t2.prefiller]\n" + - "[│ └─TableReader_26 8.00 root data:HashAgg_20]\n" + - "[│ └─HashAgg_20 8.00 cop[tikv] group by:test.t2.prefiller, ]\n" + - "[│ └─Selection_24 10.00 cop[tikv] eq(test.t2.postfiller, 1)]\n" + - "[│ └─TableFullScan_23 10000.00 cop[tikv] table:t2 keep order:false, stats:pseudo]\n" + - "[└─TableReader_14(Probe) 1.00 root partition:all data:TableRangeScan_13]\n" + - "[ └─TableRangeScan_13 1.00 cop[tikv] table:t1 range: decided by [test.t2.prefiller], keep order:false, stats:pseudo")) -======= tk.MustQuery("explain format='brief' select /* +INL_JOIN(t1,t2) */ t1.id, t1.pc from t1 where id in ( select prefiller from t2 where t2.postfiller = 1 )").Check(testkit.Rows(""+ `IndexJoin 1.25 root inner join, inner:TableReader, outer key:test.t2.prefiller, inner key:test.t1.id, equal cond:eq(test.t2.prefiller, test.t1.id)`, `├─HashAgg(Build) 1.00 root group by:test.t2.prefiller, funcs:firstrow(test.t2.prefiller)->test.t2.prefiller`, - `│ └─TableReader 1.00 root data:HashAgg`, - `│ └─HashAgg 1.00 cop[tikv] group by:test.t2.prefiller, `, - `│ └─Selection 1.00 cop[tikv] eq(test.t2.postfiller, 1)`, - `│ └─TableFullScan 2.00 cop[tikv] table:t2 keep order:false`, + `│ └─TableReader 1.00 root data:Selection`, + `│ └─Selection 1.00 cop[tikv] eq(test.t2.postfiller, 1)`, + `│ └─TableFullScan 2.00 cop[tikv] table:t2 keep order:false`, `└─TableReader(Probe) 1.00 root partition:all data:TableRangeScan`, - ` └─TableRangeScan 1.00 cop[tikv] table:t1 range: decided by [eq(test.t1.id, test.t2.prefiller)], keep order:false, stats:pseudo`)) ->>>>>>> 4a72171ffb (*: Fix issue 39999, used wrong column id list for checking partitions (#40003)) + ` └─TableRangeScan 1.00 cop[tikv] table:t1 range: decided by [test.t2.prefiller], keep order:false, stats:pseudo`)) tk.MustQuery("show warnings").Check(testkit.Rows()) // without fix it fails with: "runtime error: index out of range [0] with length 0" tk.MustQuery("select /* +INL_JOIN(t1,t2) */ t1.id, t1.pc from t1 where id in ( select prefiller from t2 where t2.postfiller = 1 )").Check(testkit.Rows()) diff --git a/executor/partition_table_test.go b/executor/partition_table_test.go index c21fbafa52336..006b817277470 100644 --- a/executor/partition_table_test.go +++ b/executor/partition_table_test.go @@ -3132,32 +3132,9 @@ func (s *partitionTableSuite) TestIssue35181(c *C) { tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic'") tk.MustExec(`insert into t select * from t where a=3000`) } -<<<<<<< HEAD -======= - -func TestIssue21732(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) - for _, mode := range []variable.PartitionPruneMode{variable.StaticOnly, variable.DynamicOnly} { - testkit.WithPruneMode(tk, mode, func() { - tk.MustExec("create database TestIssue21732") - tk.MustExec("use TestIssue21732") - tk.MustExec("drop table if exists p") - tk.MustExec(`create table p (a int, b int GENERATED ALWAYS AS (3*a-2*a) VIRTUAL) partition by hash(b) partitions 2;`) - tk.MustExec("alter table p add unique index idx (a, b);") - tk.MustExec("insert into p (a) values (1),(2),(3);") - tk.MustExec("select * from p ignore index (idx);") - tk.MustQuery("select * from p use index (idx)").Sort().Check(testkit.Rows("1 1", "2 2", "3 3")) - tk.MustExec("drop database TestIssue21732") - }) - } -} -func TestIssue39999(t *testing.T) { - store := testkit.CreateMockStore(t) - - tk := testkit.NewTestKit(t, store) +func (s *partitionTableSuite) TestIssue39999(c *C) { + tk := testkit.NewTestKit(c, s.store) tk.MustExec(`create schema test39999`) tk.MustExec(`use test39999`) @@ -3222,4 +3199,3 @@ from ` └─Selection_18 0.80 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)`, ` └─TableRangeScan_17 0.80 cop[tikv] table:c range: decided by [eq(test39999.c.txt_account_id, test39999.t.txn_account_id) eq(test39999.c.serial_id, test39999.t.serial_id) eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)], keep order:false`)) } ->>>>>>> 4a72171ffb (*: Fix issue 39999, used wrong column id list for checking partitions (#40003)) From 573bfb331bfe4c7fe73c6e6b068b1644c39acec5 Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 20 Dec 2022 14:58:27 +0000 Subject: [PATCH 3/6] Updated test case (different explain format) --- executor/partition_table_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/executor/partition_table_test.go b/executor/partition_table_test.go index 006b817277470..c4e34047da694 100644 --- a/executor/partition_table_test.go +++ b/executor/partition_table_test.go @@ -3176,14 +3176,14 @@ from join t on c.txt_account_id = t.txn_account_id and t.broker = '0009' and c.occur_trade_date = '2022-11-17'` - tk.MustQuery("explain " + query).Check(testkit.Rows(""+ - "IndexJoin_22 1.00 root inner join, inner:TableReader_21, outer key:test39999.t.txn_account_id, inner key:test39999.c.txt_account_id, equal cond:eq(test39999.t.txn_account_id, test39999.c.txt_account_id)", - "├─TableReader_27(Build) 1.00 root data:Selection_26", - "│ └─Selection_26 1.00 cop[tikv] eq(test39999.t.broker, \"0009\")", - "│ └─TableFullScan_25 1.00 cop[tikv] table:t keep order:false", - "└─TableReader_21(Probe) 1.00 root partition:all data:Selection_20", - " └─Selection_20 1.00 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)", - " └─TableRangeScan_19 1.00 cop[tikv] table:c range: decided by [eq(test39999.c.txt_account_id, test39999.t.txn_account_id) eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)], keep order:false")) + tk.MustQuery("explain " + query).Check(testkit.Rows("" + + "[IndexJoin_22 1.00 root inner join, inner:TableReader_21, outer key:test39999.t.txn_account_id, inner key:test39999.c.txt_account_id, equal cond:eq(test39999.t.txn_account_id, test39999.c.txt_account_id)]\n" + + "[├─TableReader_27(Build) 1.00 root data:Selection_26]\n" + + "[│ └─Selection_26 1.00 cop[tikv] eq(test39999.t.broker, \"0009\")]\n" + + "[│ └─TableFullScan_25 1.00 cop[tikv] table:t keep order:false]\n" + + "[└─TableReader_21(Probe) 1.00 root partition:all data:Selection_20]\n" + + "[ └─Selection_20 1.00 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)]\n" + + "[ └─TableRangeScan_19 1.00 cop[tikv] table:c range: decided by [test39999.t.txn_account_id], keep order:false]\n")) tk.MustQuery(query).Check(testkit.Rows("-2.01")) // Add the missing partition key part. From 09b16df04a051f9442995128e475a5b6d8a4dd41 Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 20 Dec 2022 17:22:33 +0000 Subject: [PATCH 4/6] explain output updated --- executor/partition_table_test.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/executor/partition_table_test.go b/executor/partition_table_test.go index c4e34047da694..dfcb0b21fa379 100644 --- a/executor/partition_table_test.go +++ b/executor/partition_table_test.go @@ -3176,26 +3176,26 @@ from join t on c.txt_account_id = t.txn_account_id and t.broker = '0009' and c.occur_trade_date = '2022-11-17'` - tk.MustQuery("explain " + query).Check(testkit.Rows("" + - "[IndexJoin_22 1.00 root inner join, inner:TableReader_21, outer key:test39999.t.txn_account_id, inner key:test39999.c.txt_account_id, equal cond:eq(test39999.t.txn_account_id, test39999.c.txt_account_id)]\n" + - "[├─TableReader_27(Build) 1.00 root data:Selection_26]\n" + - "[│ └─Selection_26 1.00 cop[tikv] eq(test39999.t.broker, \"0009\")]\n" + - "[│ └─TableFullScan_25 1.00 cop[tikv] table:t keep order:false]\n" + - "[└─TableReader_21(Probe) 1.00 root partition:all data:Selection_20]\n" + - "[ └─Selection_20 1.00 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)]\n" + - "[ └─TableRangeScan_19 1.00 cop[tikv] table:c range: decided by [test39999.t.txn_account_id], keep order:false]\n")) + tk.MustQuery("explain format='brief' " + query).Check(testkit.Rows("" + + "[IndexJoin 1.00 root inner join, inner:TableReader, outer key:test39999.t.txn_account_id, inner key:test39999.c.txt_account_id, equal cond:eq(test39999.t.txn_account_id, test39999.c.txt_account_id)]\n" + + "[├─TableReader(Build) 1.00 root data:Selection]\n" + + "[│ └─Selection 1.00 cop[tikv] eq(test39999.t.broker, \"0009\")]\n" + + "[│ └─TableFullScan 1.00 cop[tikv] table:t keep order:false]\n" + + "[└─TableReader(Probe) 1.00 root partition:all data:Selection]\n" + + "[ └─Selection 1.00 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)]\n" + + "[ └─TableRangeScan 1.00 cop[tikv] table:c range: decided by [test39999.t.txn_account_id], keep order:false]\n")) tk.MustQuery(query).Check(testkit.Rows("-2.01")) // Add the missing partition key part. tk.MustExec(`alter table t add column serial_id varchar(24) default '2022111700196920'`) query += ` and c.serial_id = t.serial_id` tk.MustQuery(query).Check(testkit.Rows("-2.01")) - tk.MustQuery("explain " + query).Check(testkit.Rows(""+ - `IndexJoin_20 0.80 root inner join, inner:TableReader_19, outer key:test39999.t.txn_account_id, test39999.t.serial_id, inner key:test39999.c.txt_account_id, test39999.c.serial_id, equal cond:eq(test39999.t.serial_id, test39999.c.serial_id), eq(test39999.t.txn_account_id, test39999.c.txt_account_id)`, - `├─TableReader_25(Build) 0.80 root data:Selection_24`, - `│ └─Selection_24 0.80 cop[tikv] eq(test39999.t.broker, "0009"), not(isnull(test39999.t.serial_id))`, - `│ └─TableFullScan_23 1.00 cop[tikv] table:t keep order:false`, - `└─TableReader_19(Probe) 0.80 root partition:all data:Selection_18`, - ` └─Selection_18 0.80 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)`, - ` └─TableRangeScan_17 0.80 cop[tikv] table:c range: decided by [eq(test39999.c.txt_account_id, test39999.t.txn_account_id) eq(test39999.c.serial_id, test39999.t.serial_id) eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)], keep order:false`)) + tk.MustQuery("explain format='brief' " + query).Check(testkit.Rows(""+ + `IndexJoin 0.80 root inner join, inner:TableReader, outer key:test39999.t.txn_account_id, test39999.t.serial_id, inner key:test39999.c.txt_account_id, test39999.c.serial_id, equal cond:eq(test39999.t.serial_id, test39999.c.serial_id), eq(test39999.t.txn_account_id, test39999.c.txt_account_id)`, + `├─TableReader(Build) 0.80 root data:Selection`, + `│ └─Selection 0.80 cop[tikv] eq(test39999.t.broker, "0009"), not(isnull(test39999.t.serial_id))`, + `│ └─TableFullScan 1.00 cop[tikv] table:t keep order:false`, + `└─TableReader(Probe) 0.80 root partition:all data:Selection`, + ` └─Selection 0.80 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)`, + ` └─TableRangeScan 0.80 cop[tikv] table:c range: decided by [eq(test39999.c.txt_account_id, test39999.t.txn_account_id) eq(test39999.c.serial_id, test39999.t.serial_id) eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)], keep order:false`)) } From 6060d190eaba2b45196b1664acbd5de810fc40e0 Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 20 Dec 2022 17:57:54 +0000 Subject: [PATCH 5/6] Updated explain output --- executor/partition_table_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/executor/partition_table_test.go b/executor/partition_table_test.go index dfcb0b21fa379..50ed2c68a7720 100644 --- a/executor/partition_table_test.go +++ b/executor/partition_table_test.go @@ -3176,14 +3176,14 @@ from join t on c.txt_account_id = t.txn_account_id and t.broker = '0009' and c.occur_trade_date = '2022-11-17'` - tk.MustQuery("explain format='brief' " + query).Check(testkit.Rows("" + - "[IndexJoin 1.00 root inner join, inner:TableReader, outer key:test39999.t.txn_account_id, inner key:test39999.c.txt_account_id, equal cond:eq(test39999.t.txn_account_id, test39999.c.txt_account_id)]\n" + - "[├─TableReader(Build) 1.00 root data:Selection]\n" + - "[│ └─Selection 1.00 cop[tikv] eq(test39999.t.broker, \"0009\")]\n" + - "[│ └─TableFullScan 1.00 cop[tikv] table:t keep order:false]\n" + - "[└─TableReader(Probe) 1.00 root partition:all data:Selection]\n" + - "[ └─Selection 1.00 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)]\n" + - "[ └─TableRangeScan 1.00 cop[tikv] table:c range: decided by [test39999.t.txn_account_id], keep order:false]\n")) + tk.MustQuery("explain format='brief' " + query).Check(testkit.Rows(""+ + "IndexJoin 1.00 root inner join, inner:TableReader, outer key:test39999.t.txn_account_id, inner key:test39999.c.txt_account_id, equal cond:eq(test39999.t.txn_account_id, test39999.c.txt_account_id)", + "├─TableReader(Build) 1.00 root data:Selection", + "│ └─Selection 1.00 cop[tikv] eq(test39999.t.broker, \"0009\")", + "│ └─TableFullScan 1.00 cop[tikv] table:t keep order:false", + "└─TableReader(Probe) 1.00 root partition:all data:Selection", + " └─Selection 1.00 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)", + " └─TableRangeScan 1.00 cop[tikv] table:c range: decided by [test39999.t.txn_account_id], keep order:false")) tk.MustQuery(query).Check(testkit.Rows("-2.01")) // Add the missing partition key part. From b2656fe951b61a9d92e0a6e16b685cdbc06387f0 Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 20 Dec 2022 18:52:59 +0000 Subject: [PATCH 6/6] Updated explain output --- executor/partition_table_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/executor/partition_table_test.go b/executor/partition_table_test.go index 50ed2c68a7720..955d23ce93bb8 100644 --- a/executor/partition_table_test.go +++ b/executor/partition_table_test.go @@ -3191,11 +3191,11 @@ from query += ` and c.serial_id = t.serial_id` tk.MustQuery(query).Check(testkit.Rows("-2.01")) tk.MustQuery("explain format='brief' " + query).Check(testkit.Rows(""+ - `IndexJoin 0.80 root inner join, inner:TableReader, outer key:test39999.t.txn_account_id, test39999.t.serial_id, inner key:test39999.c.txt_account_id, test39999.c.serial_id, equal cond:eq(test39999.t.serial_id, test39999.c.serial_id), eq(test39999.t.txn_account_id, test39999.c.txt_account_id)`, - `├─TableReader(Build) 0.80 root data:Selection`, - `│ └─Selection 0.80 cop[tikv] eq(test39999.t.broker, "0009"), not(isnull(test39999.t.serial_id))`, - `│ └─TableFullScan 1.00 cop[tikv] table:t keep order:false`, - `└─TableReader(Probe) 0.80 root partition:all data:Selection`, - ` └─Selection 0.80 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)`, - ` └─TableRangeScan 0.80 cop[tikv] table:c range: decided by [eq(test39999.c.txt_account_id, test39999.t.txn_account_id) eq(test39999.c.serial_id, test39999.t.serial_id) eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)], keep order:false`)) + "IndexJoin 0.80 root inner join, inner:TableReader, outer key:test39999.t.txn_account_id, test39999.t.serial_id, inner key:test39999.c.txt_account_id, test39999.c.serial_id, equal cond:eq(test39999.t.serial_id, test39999.c.serial_id), eq(test39999.t.txn_account_id, test39999.c.txt_account_id)", + "├─TableReader(Build) 0.80 root data:Selection", + "│ └─Selection 0.80 cop[tikv] eq(test39999.t.broker, \"0009\"), not(isnull(test39999.t.serial_id))", + "│ └─TableFullScan 1.00 cop[tikv] table:t keep order:false", + "└─TableReader(Probe) 1.00 root partition:all data:Selection", + " └─Selection 1.00 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)", + " └─TableRangeScan 1.00 cop[tikv] table:c range: decided by [test39999.t.txn_account_id test39999.t.serial_id], keep order:false")) }