From 9661084f8b6c7290e936c803af591b8b9342d078 Mon Sep 17 00:00:00 2001 From: ti-srebot <66930949+ti-srebot@users.noreply.github.com> Date: Wed, 31 Aug 2022 11:24:23 +0800 Subject: [PATCH] planner: fix wrong result when enabling dynamic mode in partition table for tiflash (#37442) (#37451) close pingcap/tidb#37254 --- executor/tiflash_test.go | 20 ++++++++++++++++++++ planner/core/fragment.go | 12 +++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/executor/tiflash_test.go b/executor/tiflash_test.go index 1e618181d40ba..8ac56d7690379 100644 --- a/executor/tiflash_test.go +++ b/executor/tiflash_test.go @@ -981,3 +981,23 @@ func (s *tiflashTestSuite) TestForbidTiflashDuringStaleRead(c *C) { c.Assert(strings.Contains(res, "tiflash"), IsFalse) c.Assert(strings.Contains(res, "tikv"), IsTrue) } + +func (s *tiflashTestSuite) TestTiflashEmptyDynamicPruneResult(c *C) { + tk := testkit.NewTestKit(c, s.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 := testGetTableByName(c, tk.Se, "test", "IDT_RP24833") + err := domain.GetDomain(tk.Se).DDL().UpdateTableReplicaInfo(tk.Se, tb.Meta().ID, true) + c.Assert(err, IsNil) + + 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 da81c591aca9d..f690dc7941ad3 100644 --- a/planner/core/fragment.go +++ b/planner/core/fragment.go @@ -300,7 +300,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 }