From 4e5a219b6e32512423cfa9ea53f30aea193aad68 Mon Sep 17 00:00:00 2001 From: Shenghui Wu <793703860@qq.com> Date: Tue, 30 Aug 2022 11:00:23 +0800 Subject: [PATCH] cherry pick #37442 to release-6.1 Signed-off-by: ti-srebot --- executor/tiflash_test.go | 22 ++++++++++++++++++++++ planner/core/fragment.go | 12 +++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/executor/tiflash_test.go b/executor/tiflash_test.go index e35ee76350714..0720e61188959 100644 --- a/executor/tiflash_test.go +++ b/executor/tiflash_test.go @@ -1247,3 +1247,25 @@ func TestAggPushDownCountStar(t *testing.T) { tk.MustQuery("select count(*) from c, o where c.c_id=o.c_id").Check(testkit.Rows("5")) } + +func TestTiflashEmptyDynamicPruneResult(t *testing.T) { + store := testkit.CreateMockStore(t, withMockTiFlash(2)) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + tk.MustExec("drop table if exists t") + tk.MustExec("CREATE TABLE `IDT_RP24833` ( `COL1` bigint(16) DEFAULT '15' COMMENT 'NUMERIC UNIQUE INDEX',\n `COL2` varchar(20) DEFAULT NULL,\n `COL4` datetime DEFAULT NULL,\n `COL3` bigint(20) DEFAULT NULL,\n `COL5` float DEFAULT NULL,\n KEY `UK_COL1` (`COL1`) /*!80000 INVISIBLE */\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\nPARTITION BY RANGE ((`COL1`-57))\n(PARTITION `P0` VALUES LESS THAN (-3503857335115112215),\n PARTITION `P1` VALUES LESS THAN (-2987877108151063747),\n PARTITION `P2` VALUES LESS THAN (-1981049919102122710),\n PARTITION `P3` VALUES LESS THAN (-1635802972727465681),\n PARTITION `P4` VALUES LESS THAN (1186020639986357714),\n PARTITION `P5` VALUES LESS THAN (1220018677454711359),\n PARTITION `PMX` VALUES LESS THAN (MAXVALUE));") + tk.MustExec("alter table IDT_RP24833 set tiflash replica 1") + tb := external.GetTableByName(t, tk, "test", "IDT_RP24833") + err := domain.GetDomain(tk.Session()).DDL().UpdateTableReplicaInfo(tk.Session(), tb.Meta().ID, true) + require.NoError(t, err) + time.Sleep(2 * time.Second) + + tk.MustExec("insert into IDT_RP24833 values(-8448770111093677011, \"郇鋺篤堯擈斥鍮啸赠璭饱磟朅闑傒聎疫ᛄ怖霃\", \"8781-05-02 04:23:03\", -27252736532807028, -1.34554e38);") + tk.MustExec("set @@tidb_partition_prune_mode = 'dynamic';") + tk.MustExec("set @@session.tidb_isolation_read_engines=\"tiflash\";") + tk.MustExec("set @@session.tidb_allow_mpp=ON;") + tk.MustExec("set @@session.tidb_enforce_mpp = on;") + tk.MustQuery("select /*+ read_from_storage(tiflash[t1]) */ * from IDT_RP24833 partition(p3, p4) t1 where t1. col1 between -8448770111093677011 and -8448770111093677011;").Check(testkit.Rows()) + tk.MustQuery("select /*+ read_from_storage(tiflash[t2]) */ * from IDT_RP24833 partition(p2) t2 where t2. col1 <= -8448770111093677011;").Check(testkit.Rows()) + tk.MustQuery("select /*+ read_from_storage(tiflash[t1, t2]) */ * from IDT_RP24833 partition(p3, p4) t1 join IDT_RP24833 partition(p2) t2 on t1.col1 = t2.col1 where t1. col1 between -8448770111093677011 and -8448770111093677011 and t2. col1 <= -8448770111093677011;").Check(testkit.Rows()) +} diff --git a/planner/core/fragment.go b/planner/core/fragment.go index ff013fa241e78..179983f011a91 100644 --- a/planner/core/fragment.go +++ b/planner/core/fragment.go @@ -301,7 +301,17 @@ func partitionPruning(ctx sessionctx.Context, tbl table.PartitionedTable, conds } } if len(ret) == 0 { - ret = []table.PhysicalTable{tbl.GetPartition(pi.Definitions[0].ID)} + // TiFlash cannot process an empty task correctly, so choose to leave it with some data to read. + if len(partitionNames) == 0 { + ret = []table.PhysicalTable{tbl.GetPartition(pi.Definitions[0].ID)} + } else { + for _, def := range pi.Definitions { + if def.Name.L == partitionNames[0].L { + ret = []table.PhysicalTable{tbl.GetPartition(def.ID)} + break + } + } + } } return ret, nil }