From a046a2d89b4b40306768a829c691030140856e44 Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 31 Jan 2023 05:31:55 +0100 Subject: [PATCH] executor: backport of #40674 (#40869) close pingcap/tidb#40803 --- executor/builder.go | 12 +++---- executor/partition_table_test.go | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/executor/builder.go b/executor/builder.go index 5b011227f73e2..0c33214f847e4 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -4009,13 +4009,13 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte } if v.IsCommonHandle { if len(keyColOffsets) > 0 { - locateKey := make([]types.Datum, e.Schema().Len()) + locateKey := make([]types.Datum, len(pt.Cols())) kvRanges = make([]kv.KeyRange, 0, len(lookUpContents)) // lookUpContentsByPID groups lookUpContents by pid(partition) so that kv ranges for same partition can be merged. lookUpContentsByPID := make(map[int64][]*indexJoinLookUpContent) for _, content := range lookUpContents { - for i, date := range content.keys { - locateKey[content.keyCols[i]] = date + for i, data := range content.keys { + locateKey[keyColOffsets[i]] = data } p, err := pt.GetPartitionByRow(e.ctx, locateKey) if err != nil { @@ -4051,11 +4051,11 @@ func (builder *dataReaderBuilder) buildTableReaderForIndexJoin(ctx context.Conte handles, lookUpContents := dedupHandles(lookUpContents) if len(keyColOffsets) > 0 { - locateKey := make([]types.Datum, e.Schema().Len()) + locateKey := make([]types.Datum, len(pt.Cols())) kvRanges = make([]kv.KeyRange, 0, len(lookUpContents)) for _, content := range lookUpContents { - for i, date := range content.keys { - locateKey[content.keyCols[i]] = date + for i, data := range content.keys { + locateKey[keyColOffsets[i]] = data } p, err := pt.GetPartitionByRow(e.ctx, locateKey) if err != nil { diff --git a/executor/partition_table_test.go b/executor/partition_table_test.go index 703f04c690af8..24472ca2b4319 100644 --- a/executor/partition_table_test.go +++ b/executor/partition_table_test.go @@ -3685,3 +3685,58 @@ from ` └─Selection_18 1.00 cop[tikv] eq(test39999.c.occur_trade_date, 2022-11-17 00:00:00.000000)`, ` └─TableRangeScan_17 1.00 cop[tikv] table:c range: decided by [test39999.t.txn_account_id test39999.t.serial_id], keep order:false`)) } + +func TestIssue40596(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + tk := testkit.NewTestKit(t, store) + + tk.MustExec("use test") + tk.MustExec(`CREATE TABLE t1 ( + c1 double DEFAULT '1.335088259490289', + c2 set('mj','4s7ht','z','3i','b26','9','cg11','uvzcp','c','ns','fl9') NOT NULL DEFAULT 'mj,z,3i,9,cg11,c', + PRIMARY KEY (c2) /*T![clustered_index] CLUSTERED */, + KEY i1 (c1), + KEY i2 (c1), + KEY i3 (c1) +) ENGINE=InnoDB DEFAULT CHARSET=gbk COLLATE=gbk_chinese_ci;`) + tk.MustExec("INSERT INTO t1 VALUES (634.2783557491367,''),(2000.5041449792013,'4s7ht'),(634.2783557491367,'3i'),(634.2783557491367,'9'),(7803.173688589342,'uvzcp'),(634.2783557491367,'ns'),(634.2783557491367,'fl9');") + tk.MustExec(`CREATE TABLE t2 ( + c3 decimal(56,16) DEFAULT '931359772706767457132645278260455518957.9866038319986886', + c4 set('3bqx','g','6op3','2g','jf','arkd3','y0b','jdy','1g','ff5z','224b') DEFAULT '3bqx,2g,ff5z,224b', + c5 smallint(6) NOT NULL DEFAULT '-25973', + c6 year(4) DEFAULT '2122', + c7 text DEFAULT NULL, + PRIMARY KEY (c5) /*T![clustered_index] CLUSTERED */, + KEY i4 (c6), + KEY i5 (c5) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='' +PARTITION BY HASH (c5) PARTITIONS 4;`) + tk.MustExec("INSERT INTO t2 VALUES (465.0000000000000000,'jdy',-8542,2008,'FgZXe');") + tk.MustExec("set @@sql_mode='';") + tk.MustExec("set tidb_partition_prune_mode=dynamic;") + tk.MustExec("analyze table t1;") + tk.MustExec("analyze table t2;") + + // No nil pointer panic + tk.MustQuery("select /*+ inl_join( t1 , t2 ) */ avg( t2.c5 ) as r0 , repeat( t2.c7 , t2.c5 ) as r1 , locate( t2.c7 , t2.c7 ) as r2 , unhex( t1.c1 ) as r3 from t1 right join t2 on t1.c2 = t2.c5 where not( t2.c5 in ( -7860 ,-13384 ,-12940 ) ) and not( t1.c2 between '4s7ht' and 'mj' );").Check(testkit.Rows(" ")) + // Again, a simpler reproduce. + tk.MustQuery("select /*+ inl_join (t1, t2) */ t2.c5 from t1 right join t2 on t1.c2 = t2.c5 where not( t1.c2 between '4s7ht' and 'mj' );").Check(testkit.Rows()) +} + +func TestIssue40803(t *testing.T) { + store, clean := testkit.CreateMockStore(t) + defer clean() + tk := testkit.NewTestKit(t, store) + + tk.MustExec(`use test`) + tk.MustExec(`set tidb_enable_clustered_index=1`) + tk.MustExec(`set tidb_partition_prune_mode=dynamic`) + tk.MustExec(`drop table if exists t1, t2`) + tk.MustExec(`create table t1 (c_int int, c_str varchar(40) character set latin1 collate latin1_bin, c_datetime datetime, c_timestamp timestamp, c_double double, c_decimal decimal(12, 6), c_enum enum('blue','green','red','yellow','white','orange','purple'), primary key (c_datetime) , key(c_int) , key(c_str(27)) , key(c_decimal) ) partition by range (to_days(c_datetime)) ( partition p0 values less than (to_days('2020-02-01')), partition p1 values less than (to_days('2020-04-01')), partition p2 values less than (to_days('2020-06-01')), partition p3 values less than maxvalue) `) + tk.MustExec(`create table t2 like t1 `) + tk.MustExec(`insert into t1 values (1, 'laughing brahmagupta', '2020-04-21 22:48:57', '2020-04-13 15:32:09', 67.158610, 6.460, 'white'), (2, 'silly brattain', '2020-03-13 12:46:14', '2020-01-31 23:32:03', 39.159617, 9.997, 'yellow'), (3, 'keen proskuriakova', '2020-06-28 16:29:23', '2020-05-01 02:48:38', 10.036821, 5.438, 'red'), (4, 'festive faraday', '2020-05-31 22:20:23', '2020-03-21 03:20:34', 88.259904, 2.554, 'white'), (5, 'peaceful bouman', '2020-04-08 18:39:24', '2020-02-10 03:48:01', 51.904213, 9.230, 'yellow')`) + tk.MustExec(`insert into t1 values (6, 'stupefied jepsen', '2020-03-19 20:09:25', '2020-01-11 02:01:41', 32.827268, 1.358, 'yellow'), (7, 'exciting engelbart', '2020-02-28 17:51:30', '2020-04-22 04:00:33', 20.161705, 9.528, 'red'), (8, 'flamboyant stonebraker', '2020-05-07 10:34:51', '2020-02-07 11:48:33', 43.514339, 9.248, 'white'), (9, 'dazzling lalande', '2020-04-10 03:40:15', '2020-04-04 21:38:41', 44.208521, 2.658, 'blue'), (10, 'hopeful gagarin', '2020-01-13 16:55:42', '2020-06-06 06:37:03', 79.028624, 3.146, 'blue')`) + tk.MustExec(`insert into t2 select * from t1`) + tk.MustQuery(`select * from t1 where c_datetime in (select c_datetime from t2 where t1.c_int = 2)`).Check(testkit.Rows("2 silly brattain 2020-03-13 12:46:14 2020-01-31 23:32:03 39.159617 9.997000 yellow")) +}