diff --git a/planner/core/issuetest/BUILD.bazel b/planner/core/issuetest/BUILD.bazel index a6c9ab8a9e24f..ac31cded370be 100644 --- a/planner/core/issuetest/BUILD.bazel +++ b/planner/core/issuetest/BUILD.bazel @@ -6,6 +6,6 @@ go_test( srcs = ["planner_issue_test.go"], flaky = True, race = "on", - shard_count = 8, + shard_count = 9, deps = ["//testkit"], ) diff --git a/planner/core/issuetest/planner_issue_test.go b/planner/core/issuetest/planner_issue_test.go index 4d0397d93b899..7e699b0a30b70 100644 --- a/planner/core/issuetest/planner_issue_test.go +++ b/planner/core/issuetest/planner_issue_test.go @@ -285,3 +285,21 @@ func Test53726(t *testing.T) { " └─TableReader_11 2.00 root data:TableFullScan_10", " └─TableFullScan_10 2.00 cop[tikv] table:t7 keep order:false")) } + +func TestIssue58476(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test;") + tk.MustExec("CREATE TABLE t3 (id int PRIMARY KEY,c1 varchar(256),c2 varchar(256) GENERATED ALWAYS AS (concat(c1, c1)) VIRTUAL,KEY (id));") + tk.MustExec("insert into t3(id, c1) values (50, 'c');") + tk.MustQuery("SELECT /*+ USE_INDEX_MERGE(`t3`)*/ id FROM `t3` WHERE c2 BETWEEN 'a' AND 'b' GROUP BY id HAVING id < 100 or id > 0;").Check(testkit.Rows()) + tk.MustQuery("explain format='brief' SELECT /*+ USE_INDEX_MERGE(`t3`)*/ id FROM `t3` WHERE c2 BETWEEN 'a' AND 'b' GROUP BY id HAVING id < 100 or id > 0;"). + Check(testkit.Rows( + `Projection 249.75 root test.t3.id`, + `└─Selection 249.75 root ge(test.t3.c2, "a"), le(test.t3.c2, "b")`, + ` └─Projection 9990.00 root test.t3.id, test.t3.c2`, + ` └─IndexMerge 9990.00 root type: union`, + ` ├─IndexRangeScan(Build) 3323.33 cop[tikv] table:t3, index:id(id) range:[-inf,100), keep order:false, stats:pseudo`, + ` ├─TableRangeScan(Build) 3333.33 cop[tikv] table:t3 range:(0,+inf], keep order:false, stats:pseudo`, + ` └─TableRowIDScan(Probe) 9990.00 cop[tikv] table:t3 keep order:false, stats:pseudo`)) +} diff --git a/planner/core/task.go b/planner/core/task.go index a838de8704985..241ce37472237 100644 --- a/planner/core/task.go +++ b/planner/core/task.go @@ -697,7 +697,6 @@ func (t *copTask) convertToRootTaskImpl(ctx sessionctx.Context) *rootTask { p.PartitionInfo = t.partitionInfo setTableScanToTableRowIDScan(p.tablePlan) newTask.p = p - t.handleRootTaskConds(ctx, newTask) if t.needExtraProj { schema := t.originSchema proj := PhysicalProjection{Exprs: expression.Column2Exprs(schema.Columns)}.Init(ctx, p.stats, t.idxMergePartPlans[0].SelectBlockOffset(), nil) @@ -705,6 +704,7 @@ func (t *copTask) convertToRootTaskImpl(ctx sessionctx.Context) *rootTask { proj.SetChildren(p) newTask.p = proj } + t.handleRootTaskConds(ctx, newTask) return newTask } if t.indexPlan != nil && t.tablePlan != nil {