From 2b7b657f42880df9771cb82c75c19f16aa5d7952 Mon Sep 17 00:00:00 2001 From: yuanjize <1224500078@qq.com> Date: Fri, 3 Jan 2020 19:41:09 +0800 Subject: [PATCH] ddl: throw warning when `ALTER TABLE ORDER BY` on table with primary key (#14324) --- ddl/db_test.go | 18 ++++++++++++++++++ ddl/ddl_api.go | 13 +++++++++++++ 2 files changed, 31 insertions(+) diff --git a/ddl/db_test.go b/ddl/db_test.go index 474090c0bd..44e254376c 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -4295,6 +4295,24 @@ func (s *testDBSuite2) TestDDLWithInvalidTableInfo(c *C) { c.Assert(err.Error(), Equals, "[parser:1064]You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 94 near \"then (b / a) end));\" ") } +func (s *testDBSuite1) TestAlterOrderBy(c *C) { + s.tk = testkit.NewTestKit(c, s.store) + s.tk.MustExec("use " + s.schemaName) + s.tk.MustExec("create table ob (pk int primary key, c int default 1, c1 int default 1, KEY cl(c1))") + + // Test order by with primary key + s.tk.MustExec("alter table ob order by c") + c.Assert(s.tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(1)) + s.tk.MustQuery("show warnings").Check(testutil.RowsWithSep("|", "Warning|1105|ORDER BY ignored as there is a user-defined clustered index in the table 'ob'")) + + // Test order by with no primary key + s.tk.MustExec("drop table if exists ob") + s.tk.MustExec("create table ob (c int default 1, c1 int default 1, KEY cl(c1))") + s.tk.MustExec("alter table ob order by c") + c.Assert(s.tk.Se.GetSessionVars().StmtCtx.WarningCount(), Equals, uint16(0)) + s.tk.MustExec("drop table if exists ob") +} + func init() { // Make sure it will only be executed once. domain.SchemaOutOfDateRetryInterval = int64(50 * time.Millisecond) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index c1c21599c9..644ba98ab2 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -2068,6 +2068,8 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A } case ast.AlterTableSetTiFlashReplica: err = d.AlterTableSetTiFlashReplica(ctx, ident, spec.TiFlashReplica) + case ast.AlterTableOrderByColumns: + err = d.OrderByColumns(ctx, ident) default: // Nothing to do now. } @@ -4201,3 +4203,14 @@ func (d *ddl) RepairTable(ctx sessionctx.Context, table *ast.TableName, createSt err = d.callHookOnChanged(err) return errors.Trace(err) } + +func (d *ddl) OrderByColumns(ctx sessionctx.Context, ident ast.Ident) error { + _, tb, err := d.getSchemaAndTableByIdent(ctx, ident) + if err != nil { + return errors.Trace(err) + } + if tb.Meta().GetPkColInfo() != nil { + ctx.GetSessionVars().StmtCtx.AppendWarning(errors.Errorf("ORDER BY ignored as there is a user-defined clustered index in the table '%s'", ident.Name)) + } + return nil +}