From 6e38182dc6af9c8e3badfcaa716cf3a38020ecb5 Mon Sep 17 00:00:00 2001 From: Arenatlx <314806019@qq.com> Date: Fri, 13 Oct 2023 19:30:27 +0800 Subject: [PATCH] planner: fix group concat projection elimination logic (#47612) close pingcap/tidb#41957 --- planner/core/casetest/BUILD.bazel | 2 +- planner/core/casetest/integration_test.go | 8 ++++++++ planner/core/rule_eliminate_projection.go | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/planner/core/casetest/BUILD.bazel b/planner/core/casetest/BUILD.bazel index 77f83962239e3..09f18249bbaca 100644 --- a/planner/core/casetest/BUILD.bazel +++ b/planner/core/casetest/BUILD.bazel @@ -12,7 +12,7 @@ go_test( ], data = glob(["testdata/**"]), flaky = True, - shard_count = 18, + shard_count = 19, deps = [ "//domain", "//parser", diff --git a/planner/core/casetest/integration_test.go b/planner/core/casetest/integration_test.go index 1f3e77d5d5036..80b6b3d35d2d8 100644 --- a/planner/core/casetest/integration_test.go +++ b/planner/core/casetest/integration_test.go @@ -431,3 +431,11 @@ func TestFixControl45132(t *testing.T) { tk.MustExec(`set @@tidb_opt_fix_control = "45132:0"`) tk.MustHavePlan(`select * from t where a=2`, `TableFullScan`) } + +func TestIssue41957(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec(`use test`) + tk.MustExec("CREATE TABLE `github_events` (\n `id` bigint(20) NOT NULL DEFAULT '0',\n `type` varchar(29) NOT NULL DEFAULT 'Event',\n `created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',\n `repo_id` bigint(20) NOT NULL DEFAULT '0',\n `repo_name` varchar(140) NOT NULL DEFAULT '',\n `actor_id` bigint(20) NOT NULL DEFAULT '0',\n `actor_login` varchar(40) NOT NULL DEFAULT '',\n `language` varchar(26) NOT NULL DEFAULT '',\n `additions` bigint(20) NOT NULL DEFAULT '0',\n `deletions` bigint(20) NOT NULL DEFAULT '0',\n `action` varchar(11) NOT NULL DEFAULT '',\n `number` int(11) NOT NULL DEFAULT '0',\n `commit_id` varchar(40) NOT NULL DEFAULT '',\n `comment_id` bigint(20) NOT NULL DEFAULT '0',\n `org_login` varchar(40) NOT NULL DEFAULT '',\n `org_id` bigint(20) NOT NULL DEFAULT '0',\n `state` varchar(6) NOT NULL DEFAULT '',\n `closed_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',\n `comments` int(11) NOT NULL DEFAULT '0',\n `pr_merged_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',\n `pr_merged` tinyint(1) NOT NULL DEFAULT '0',\n `pr_changed_files` int(11) NOT NULL DEFAULT '0',\n `pr_review_comments` int(11) NOT NULL DEFAULT '0',\n `pr_or_issue_id` bigint(20) NOT NULL DEFAULT '0',\n `event_day` date NOT NULL,\n `event_month` date NOT NULL,\n `event_year` int(11) NOT NULL,\n `push_size` int(11) NOT NULL DEFAULT '0',\n `push_distinct_size` int(11) NOT NULL DEFAULT '0',\n `creator_user_login` varchar(40) NOT NULL DEFAULT '',\n `creator_user_id` bigint(20) NOT NULL DEFAULT '0',\n `pr_or_issue_created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',\n KEY `index_github_events_on_id` (`id`),\n KEY `index_github_events_on_created_at` (`created_at`),\n KEY `index_github_events_on_repo_id_type_action_month_actor_login` (`repo_id`,`type`,`action`,`event_month`,`actor_login`),\n KEY `index_ge_on_repo_id_type_action_pr_merged_created_at_add_del` (`repo_id`,`type`,`action`,`pr_merged`,`created_at`,`additions`,`deletions`),\n KEY `index_ge_on_creator_id_type_action_merged_created_at_add_del` (`creator_user_id`,`type`,`action`,`pr_merged`,`created_at`,`additions`,`deletions`),\n KEY `index_ge_on_actor_id_type_action_created_at_repo_id_commits` (`actor_id`,`type`,`action`,`created_at`,`repo_id`,`push_distinct_size`),\n KEY `index_ge_on_repo_id_type_action_created_at_number_pdsize_psize` (`repo_id`,`type`,`action`,`created_at`,`number`,`push_distinct_size`,`push_size`),\n KEY `index_ge_on_repo_id_type_action_created_at_actor_login` (`repo_id`,`type`,`action`,`created_at`,`actor_login`),\n KEY `index_ge_on_repo_name_type` (`repo_name`,`type`),\n KEY `index_ge_on_actor_login_type` (`actor_login`,`type`),\n KEY `index_ge_on_org_login_type` (`org_login`,`type`),\n KEY `index_ge_on_language` (`language`),\n KEY `index_ge_on_org_id_type` (`org_id`,`type`),\n KEY `index_ge_on_actor_login_lower` ((lower(`actor_login`))),\n KEY `index_ge_on_repo_name_lower` ((lower(`repo_name`))),\n KEY `index_ge_on_language_lower` ((lower(`language`))),\n KEY `index_ge_on_type_action` (`type`,`action`) /*!80000 INVISIBLE */,\n KEY `index_ge_on_repo_id_type_created_at` (`repo_id`,`type`,`created_at`),\n KEY `index_ge_on_repo_id_created_at` (`repo_id`,`created_at`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin\nPARTITION BY LIST COLUMNS(`type`)\n(PARTITION `push_event` VALUES IN ('PushEvent'),\n PARTITION `create_event` VALUES IN ('CreateEvent'),\n PARTITION `pull_request_event` VALUES IN ('PullRequestEvent'),\n PARTITION `watch_event` VALUES IN ('WatchEvent'),\n PARTITION `issue_comment_event` VALUES IN ('IssueCommentEvent'),\n PARTITION `issues_event` VALUES IN ('IssuesEvent'),\n PARTITION `delete_event` VALUES IN ('DeleteEvent'),\n PARTITION `fork_event` VALUES IN ('ForkEvent'),\n PARTITION `pull_request_review_comment_event` VALUES IN ('PullRequestReviewCommentEvent'),\n PARTITION `pull_request_review_event` VALUES IN ('PullRequestReviewEvent'),\n PARTITION `gollum_event` VALUES IN ('GollumEvent'),\n PARTITION `release_event` VALUES IN ('ReleaseEvent'),\n PARTITION `member_event` VALUES IN ('MemberEvent'),\n PARTITION `commit_comment_event` VALUES IN ('CommitCommentEvent'),\n PARTITION `public_event` VALUES IN ('PublicEvent'),\n PARTITION `gist_event` VALUES IN ('GistEvent'),\n PARTITION `follow_event` VALUES IN ('FollowEvent'),\n PARTITION `event` VALUES IN ('Event'),\n PARTITION `download_event` VALUES IN ('DownloadEvent'),\n PARTITION `team_add_event` VALUES IN ('TeamAddEvent'),\n PARTITION `fork_apply_event` VALUES IN ('ForkApplyEvent'))\n") + tk.MustQuery("SELECT\n repo_id, GROUP_CONCAT(\n DISTINCT actor_login\n ORDER BY cnt DESC\n SEPARATOR ','\n ) AS actor_logins\nFROM (\n SELECT\n ge.repo_id AS repo_id,\n ge.actor_login AS actor_login,\n COUNT(*) AS cnt\n FROM github_events ge\n WHERE\n type = 'PullRequestEvent' AND action = 'opened'\n AND (ge.created_at >= DATE_SUB(NOW(), INTERVAL 1 DAY) AND ge.created_at <= NOW())\n GROUP BY ge.repo_id, ge.actor_login\n ORDER BY cnt DESC\n) sub\nGROUP BY repo_id").Check(testkit.Rows()) +} diff --git a/planner/core/rule_eliminate_projection.go b/planner/core/rule_eliminate_projection.go index 00b274da76db2..97b8ade37cb0f 100644 --- a/planner/core/rule_eliminate_projection.go +++ b/planner/core/rule_eliminate_projection.go @@ -276,6 +276,9 @@ func (la *LogicalAggregation) ReplaceExprColumns(replace map[string]*expression. for _, aggExpr := range agg.Args { ResolveExprAndReplace(aggExpr, replace) } + for _, orderExpr := range agg.OrderByItems { + ResolveExprAndReplace(orderExpr.Expr, replace) + } } for _, gbyItem := range la.GroupByItems { ResolveExprAndReplace(gbyItem, replace)