From d0150c0a686b62e38ed6b8ba9077d17ded4433a8 Mon Sep 17 00:00:00 2001 From: Mattias Jonsson Date: Tue, 19 Nov 2024 12:56:03 +0100 Subject: [PATCH] planner: ONLY_FULL_GROUP_BY sql_mode was not working with VIEWs (#57473) close pingcap/tidb#53175 --- pkg/planner/core/issuetest/BUILD.bazel | 2 +- pkg/planner/core/issuetest/planner_issue_test.go | 15 +++++++++++++++ pkg/planner/core/logical_plan_builder.go | 12 ++++++++++-- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/pkg/planner/core/issuetest/BUILD.bazel b/pkg/planner/core/issuetest/BUILD.bazel index e12f14bea8454..2fa7a2e123983 100644 --- a/pkg/planner/core/issuetest/BUILD.bazel +++ b/pkg/planner/core/issuetest/BUILD.bazel @@ -10,7 +10,7 @@ go_test( data = glob(["testdata/**"]), flaky = True, race = "on", - shard_count = 3, + shard_count = 4, deps = [ "//pkg/parser", "//pkg/planner", diff --git a/pkg/planner/core/issuetest/planner_issue_test.go b/pkg/planner/core/issuetest/planner_issue_test.go index 0474e03f97543..fda17d6cfce17 100644 --- a/pkg/planner/core/issuetest/planner_issue_test.go +++ b/pkg/planner/core/issuetest/planner_issue_test.go @@ -119,3 +119,18 @@ func TestIssue54535(t *testing.T) { tk.MustQuery("select /*+ inl_join(tmp) */ * from t1 inner join (select col_1, group_concat(col_2) from t2 group by col_1) tmp on t1.col_1 = tmp.col_1;").Check(testkit.Rows()) tk.MustQuery("select /*+ inl_join(tmp) */ * from t1 inner join (select col_1, group_concat(distinct col_2 order by col_2) from t2 group by col_1) tmp on t1.col_1 = tmp.col_1;").Check(testkit.Rows()) } + +func TestIssue53175(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec(`create table t(a int)`) + tk.MustExec(`set @@sql_mode = default`) + tk.MustQuery(`select @@sql_mode REGEXP 'ONLY_FULL_GROUP_BY'`).Check(testkit.Rows("1")) + tk.MustContainErrMsg(`select * from t group by null`, "[planner:1055]Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by") + tk.MustExec(`create view v as select * from t group by null`) + tk.MustContainErrMsg(`select * from v`, "[planner:1055]Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.t.a' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by") + tk.MustExec(`set @@sql_mode = ''`) + tk.MustQuery(`select * from t group by null`) + tk.MustQuery(`select * from v`) +} diff --git a/pkg/planner/core/logical_plan_builder.go b/pkg/planner/core/logical_plan_builder.go index a684f746e0721..6e760c868e0ff 100644 --- a/pkg/planner/core/logical_plan_builder.go +++ b/pkg/planner/core/logical_plan_builder.go @@ -2940,11 +2940,19 @@ func (b *PlanBuilder) tblInfoFromCol(from ast.ResultSetNode, name *types.FieldNa for _, field := range tableList { if field.Name.L == name.TblName.L { tnW := b.resolveCtx.GetTableName(field) - // when the Select is inside a view, it's not pre-processed, tnW is nil. if tnW != nil { return tnW.TableInfo } - return nil + // when the Select is inside a view, it's not pre-processed, tnW is nil. + if b.isCreateView { + // Ignore during create + return nil + } + tblInfo, err := b.is.TableInfoByName(name.DBName, name.TblName) + if err != nil { + return nil + } + return tblInfo } } return nil