From 3806ef7cab1b24e232d228cc5ae1308d6e8d9c20 Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Wed, 19 Feb 2020 18:50:21 +0800 Subject: [PATCH 1/3] planner, executor: adjustOverlongColName for CreateView --- executor/ddl_test.go | 35 +++++++++++++++++++++++++++++++++++ planner/core/planbuilder.go | 31 ++++++++++++++++++++++++------- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/executor/ddl_test.go b/executor/ddl_test.go index 6d002c3a28211..d5e9b17ed8d74 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -255,6 +255,41 @@ func (s *testSuite6) TestCreateView(c *C) { tk.MustExec("drop view v") } +func (s *testSuite6) TestCreateViewWithOverlongColName(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create table t(a int)") + defer tk.MustExec("drop table t") + tk.MustExec("create view v as select distinct'" + strings.Repeat("a", 65) + "', " + + "max('" + strings.Repeat("b", 65) + "'), " + + "'cccccccccc', '" + strings.Repeat("d", 65) + "';") + resultCreateStmt := "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`new_exp_1`, `new_exp_2`, `cccccccccc`, `new_exp_4`) AS SELECT DISTINCT '" + strings.Repeat("a", 65) + "',MAX('" + strings.Repeat("b", 65) + "'),'cccccccccc','" + strings.Repeat("d", 65) + "'" + tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) + tk.MustExec("drop view v;") + tk.MustExec(resultCreateStmt) + + tk.MustExec("drop view v ") + tk.MustExec("create view v as select 'a', '" + strings.Repeat("b", 65) + "' from t " + + "union select '" + strings.Repeat("c", 65) + "', " + + "count(distinct '" + strings.Repeat("b", 65) + "', " + + "'c');") + resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`a`, `new_exp_2`) AS SELECT 'a','" + strings.Repeat("b", 65) + "' FROM `test`.`t` UNION SELECT '" + strings.Repeat("c", 65) + "',COUNT(DISTINCT '" + strings.Repeat("b", 65) + "', 'c')" + tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) + tk.MustExec("drop view v;") + tk.MustExec(resultCreateStmt) + + tk.MustExec("drop view v ") + tk.MustExec("create view v as select 'a' as '" + strings.Repeat("b", 65) + "' from t;") + resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`new_exp_1`) AS SELECT 'a' AS `" + strings.Repeat("b", 65) + "` FROM `test`.`t`" + tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) + tk.MustExec("drop view v;") + tk.MustExec(resultCreateStmt) + + tk.MustExec("drop view v ") + err := tk.ExecToErr("create view v(`" + strings.Repeat("b", 65) + "`) as select a from t;") + c.Assert(err.Error(), Equals, "[ddl:1059]Identifier name 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' is too long") +} + func (s *testSuite6) TestCreateDropDatabase(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("create database if not exists drop_test;") diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 95aa67009b073..6b158fbd1bb8e 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -2530,6 +2530,7 @@ func (b *PlanBuilder) buildDDL(ctx context.Context, node ast.DDLNode) (Plan, err schema := plan.Schema() names := plan.OutputNames() if v.Cols == nil { + adjustOverlongViewColname(plan.(LogicalPlan)) v.Cols = make([]model.CIStr, len(schema.Columns)) for i, name := range names { v.Cols[i] = name.ColName @@ -2538,14 +2539,12 @@ func (b *PlanBuilder) buildDDL(ctx context.Context, node ast.DDLNode) (Plan, err if len(v.Cols) != schema.Len() { return nil, ddl.ErrViewWrongList } - if _, ok := plan.(LogicalPlan); ok { - if b.ctx.GetSessionVars().User != nil { - authErr = ErrTableaccessDenied.GenWithStackByArgs("CREATE VIEW", b.ctx.GetSessionVars().User.Hostname, - b.ctx.GetSessionVars().User.Username, v.ViewName.Name.L) - } - b.visitInfo = appendVisitInfo(b.visitInfo, mysql.CreateViewPriv, v.ViewName.Schema.L, - v.ViewName.Name.L, "", authErr) + if b.ctx.GetSessionVars().User != nil { + authErr = ErrTableaccessDenied.GenWithStackByArgs("CREATE VIEW", b.ctx.GetSessionVars().User.Hostname, + b.ctx.GetSessionVars().User.Username, v.ViewName.Name.L) } + b.visitInfo = appendVisitInfo(b.visitInfo, mysql.CreateViewPriv, v.ViewName.Schema.L, + v.ViewName.Name.L, "", authErr) if v.Definer.CurrentUser && b.ctx.GetSessionVars().User != nil { v.Definer = b.ctx.GetSessionVars().User } @@ -2948,3 +2947,21 @@ func buildChecksumTableSchema() (*expression.Schema, []*types.FieldName) { schema.Append(buildColumnWithName("", "Total_bytes", mysql.TypeLonglong, 22)) return schema.col2Schema(), schema.names } + +func adjustOverlongViewColname(plan LogicalPlan) { + outputNames := plan.OutputNames() + switch plan.(type) { + case *LogicalProjection, *LogicalAggregation, *LogicalUnionAll: + for i := range outputNames { + if outputName := outputNames[i].ColName.L; len(outputName) > mysql.MaxColumnNameLength { + outputNames[i].ColName = model.NewCIStr(fmt.Sprintf("new_exp_%d", i+1)) + } + } + default: + if len(plan.Children()) == 1 { + adjustOverlongViewColname(plan.Children()[0]) + return + } + } + return +} From 2f14235885a281c2c37bcdf472f55f1f1806b92f Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Thu, 20 Feb 2020 11:06:48 +0800 Subject: [PATCH 2/3] refine cde --- executor/ddl_test.go | 5 +++++ planner/core/planbuilder.go | 18 ++++++------------ 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/executor/ddl_test.go b/executor/ddl_test.go index d5e9b17ed8d74..53de7f76508f3 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -265,6 +265,8 @@ func (s *testSuite6) TestCreateViewWithOverlongColName(c *C) { "'cccccccccc', '" + strings.Repeat("d", 65) + "';") resultCreateStmt := "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`new_exp_1`, `new_exp_2`, `cccccccccc`, `new_exp_4`) AS SELECT DISTINCT '" + strings.Repeat("a", 65) + "',MAX('" + strings.Repeat("b", 65) + "'),'cccccccccc','" + strings.Repeat("d", 65) + "'" tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) + tk.MustQuery("select * from v") + tk.MustQuery("select name_exp_1 from v") tk.MustExec("drop view v;") tk.MustExec(resultCreateStmt) @@ -275,6 +277,7 @@ func (s *testSuite6) TestCreateViewWithOverlongColName(c *C) { "'c');") resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`a`, `new_exp_2`) AS SELECT 'a','" + strings.Repeat("b", 65) + "' FROM `test`.`t` UNION SELECT '" + strings.Repeat("c", 65) + "',COUNT(DISTINCT '" + strings.Repeat("b", 65) + "', 'c')" tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) + tk.MustQuery("select a, name_exp_2 from v") tk.MustExec("drop view v;") tk.MustExec(resultCreateStmt) @@ -282,6 +285,8 @@ func (s *testSuite6) TestCreateViewWithOverlongColName(c *C) { tk.MustExec("create view v as select 'a' as '" + strings.Repeat("b", 65) + "' from t;") resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`new_exp_1`) AS SELECT 'a' AS `" + strings.Repeat("b", 65) + "` FROM `test`.`t`" tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) + tk.MustQuery("select * from v") + tk.MustQuery("select new_exp_1 from v") tk.MustExec("drop view v;") tk.MustExec(resultCreateStmt) diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 6b158fbd1bb8e..12ebbbe858dd9 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -2948,20 +2948,14 @@ func buildChecksumTableSchema() (*expression.Schema, []*types.FieldName) { return schema.col2Schema(), schema.names } +// adjustOverlongViewColname adjusts the overlong outputNames of a view to +// `new_exp_$off` where `$off` is the offset of the output column, $off starts from 1. +// There is still some MySQL compatible problems. func adjustOverlongViewColname(plan LogicalPlan) { outputNames := plan.OutputNames() - switch plan.(type) { - case *LogicalProjection, *LogicalAggregation, *LogicalUnionAll: - for i := range outputNames { - if outputName := outputNames[i].ColName.L; len(outputName) > mysql.MaxColumnNameLength { - outputNames[i].ColName = model.NewCIStr(fmt.Sprintf("new_exp_%d", i+1)) - } - } - default: - if len(plan.Children()) == 1 { - adjustOverlongViewColname(plan.Children()[0]) - return + for i := range outputNames { + if outputName := outputNames[i].ColName.L; len(outputName) > mysql.MaxColumnNameLength { + outputNames[i].ColName = model.NewCIStr(fmt.Sprintf("new_exp_%d", i+1)) } } - return } From 3a7d83628a30fa798ec1edd290d572cceffd420c Mon Sep 17 00:00:00 2001 From: xuhuaiyu <391585975@qq.com> Date: Thu, 20 Feb 2020 14:39:53 +0800 Subject: [PATCH 3/3] fix ci --- executor/ddl_test.go | 21 +++++++++++---------- planner/core/planbuilder.go | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/executor/ddl_test.go b/executor/ddl_test.go index 53de7f76508f3..71a7087eaa050 100644 --- a/executor/ddl_test.go +++ b/executor/ddl_test.go @@ -263,30 +263,31 @@ func (s *testSuite6) TestCreateViewWithOverlongColName(c *C) { tk.MustExec("create view v as select distinct'" + strings.Repeat("a", 65) + "', " + "max('" + strings.Repeat("b", 65) + "'), " + "'cccccccccc', '" + strings.Repeat("d", 65) + "';") - resultCreateStmt := "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`new_exp_1`, `new_exp_2`, `cccccccccc`, `new_exp_4`) AS SELECT DISTINCT '" + strings.Repeat("a", 65) + "',MAX('" + strings.Repeat("b", 65) + "'),'cccccccccc','" + strings.Repeat("d", 65) + "'" - tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) + resultCreateStmt := "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`name_exp_1`, `name_exp_2`, `cccccccccc`, `name_exp_4`) AS SELECT DISTINCT '" + strings.Repeat("a", 65) + "',MAX('" + strings.Repeat("b", 65) + "'),'cccccccccc','" + strings.Repeat("d", 65) + "'" tk.MustQuery("select * from v") - tk.MustQuery("select name_exp_1 from v") + tk.MustQuery("select name_exp_1, name_exp_2, cccccccccc, name_exp_4 from v") + tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) tk.MustExec("drop view v;") tk.MustExec(resultCreateStmt) tk.MustExec("drop view v ") - tk.MustExec("create view v as select 'a', '" + strings.Repeat("b", 65) + "' from t " + + tk.MustExec("create definer='root'@'localhost' view v as select 'a', '" + strings.Repeat("b", 65) + "' from t " + "union select '" + strings.Repeat("c", 65) + "', " + "count(distinct '" + strings.Repeat("b", 65) + "', " + "'c');") - resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`a`, `new_exp_2`) AS SELECT 'a','" + strings.Repeat("b", 65) + "' FROM `test`.`t` UNION SELECT '" + strings.Repeat("c", 65) + "',COUNT(DISTINCT '" + strings.Repeat("b", 65) + "', 'c')" - tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) + resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` (`a`, `name_exp_2`) AS SELECT 'a','" + strings.Repeat("b", 65) + "' FROM `test`.`t` UNION SELECT '" + strings.Repeat("c", 65) + "',COUNT(DISTINCT '" + strings.Repeat("b", 65) + "', 'c')" + tk.MustQuery("select * from v") tk.MustQuery("select a, name_exp_2 from v") + tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) tk.MustExec("drop view v;") tk.MustExec(resultCreateStmt) tk.MustExec("drop view v ") - tk.MustExec("create view v as select 'a' as '" + strings.Repeat("b", 65) + "' from t;") - resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=``@`` SQL SECURITY DEFINER VIEW `v` (`new_exp_1`) AS SELECT 'a' AS `" + strings.Repeat("b", 65) + "` FROM `test`.`t`" - tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) + tk.MustExec("create definer='root'@'localhost' view v as select 'a' as '" + strings.Repeat("b", 65) + "' from t;") tk.MustQuery("select * from v") - tk.MustQuery("select new_exp_1 from v") + tk.MustQuery("select name_exp_1 from v") + resultCreateStmt = "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v` (`name_exp_1`) AS SELECT 'a' AS `" + strings.Repeat("b", 65) + "` FROM `test`.`t`" + tk.MustQuery("show create view v").Check(testkit.Rows("v " + resultCreateStmt + " ")) tk.MustExec("drop view v;") tk.MustExec(resultCreateStmt) diff --git a/planner/core/planbuilder.go b/planner/core/planbuilder.go index 12ebbbe858dd9..7f15c32c6f7b5 100644 --- a/planner/core/planbuilder.go +++ b/planner/core/planbuilder.go @@ -2955,7 +2955,7 @@ func adjustOverlongViewColname(plan LogicalPlan) { outputNames := plan.OutputNames() for i := range outputNames { if outputName := outputNames[i].ColName.L; len(outputName) > mysql.MaxColumnNameLength { - outputNames[i].ColName = model.NewCIStr(fmt.Sprintf("new_exp_%d", i+1)) + outputNames[i].ColName = model.NewCIStr(fmt.Sprintf("name_exp_%d", i+1)) } } }