From dd6bf061141cb09cc9e8f335f4b82bf929fb5282 Mon Sep 17 00:00:00 2001 From: yibin Date: Fri, 10 May 2024 11:01:09 +0800 Subject: [PATCH] executor: Fix push downed topN won't replace correlated column problem (#53097) close pingcap/tidb#52777 --- pkg/executor/builder.go | 6 ++++++ pkg/executor/test/issuetest/BUILD.bazel | 2 +- pkg/executor/test/issuetest/executor_issue_test.go | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/executor/builder.go b/pkg/executor/builder.go index 822fd9d59b450..f580e74a9c9b4 100644 --- a/pkg/executor/builder.go +++ b/pkg/executor/builder.go @@ -2879,6 +2879,12 @@ func (*executorBuilder) corColInDistPlan(plans []plannercore.PhysicalPlan) bool return true } } + case *plannercore.PhysicalTopN: + for _, byItem := range x.ByItems { + if len(expression.ExtractCorColumns(byItem.Expr)) > 0 { + return true + } + } case *plannercore.PhysicalTableScan: for _, cond := range x.LateMaterializationFilterCondition { if len(expression.ExtractCorColumns(cond)) > 0 { diff --git a/pkg/executor/test/issuetest/BUILD.bazel b/pkg/executor/test/issuetest/BUILD.bazel index ebef648a5b266..23872e9643e84 100644 --- a/pkg/executor/test/issuetest/BUILD.bazel +++ b/pkg/executor/test/issuetest/BUILD.bazel @@ -8,7 +8,7 @@ go_test( "main_test.go", ], flaky = True, - shard_count = 18, + shard_count = 19, deps = [ "//pkg/autoid_service", "//pkg/config", diff --git a/pkg/executor/test/issuetest/executor_issue_test.go b/pkg/executor/test/issuetest/executor_issue_test.go index e165e982e6e59..49e4a6ce6d5d6 100644 --- a/pkg/executor/test/issuetest/executor_issue_test.go +++ b/pkg/executor/test/issuetest/executor_issue_test.go @@ -636,6 +636,20 @@ func TestIssue51874(t *testing.T) { tk.MustQuery("select (select sum(a) over () from t2 limit 1) from t;").Check(testkit.Rows("10", "2")) } +func TestIssue51777(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.Session().GetSessionVars().AllowProjectionPushDown = true + + tk.MustExec("use test") + tk.MustExec("drop table if exists t0, t1") + tk.MustExec("create table t0 (c_k int)") + tk.MustExec("create table t1 (c_pv int)") + tk.MustExec("insert into t0 values(-2127559046),(-190905159),(-171305020),(-59638845),(98004414),(2111663670),(2137868682),(2137868682),(2142611610)") + tk.MustExec("insert into t1 values(-2123227448), (2131706870), (-2071508387), (2135465388), (2052805244), (-2066000113)") + tk.MustQuery("SELECT ( select (ref_4.c_pv <= ref_3.c_k) as c0 from t1 as ref_4 order by c0 asc limit 1) as p2 FROM t0 as ref_3 order by p2;").Check(testkit.Rows("0", "0", "0", "0", "0", "0", "1", "1", "1")) +} + func TestIssue52978(t *testing.T) { store := testkit.CreateMockStore(t) tk := testkit.NewTestKit(t, store)