From 3f3ead2d594507a24e1e86b8318463f3352fac16 Mon Sep 17 00:00:00 2001 From: miraclesu Date: Mon, 5 Aug 2019 06:14:11 -0500 Subject: [PATCH] executor: fix #10513, alias identifier maximum length compatible with MySQL (#10597) --- executor/adapter.go | 9 +++++++++ server/tidb_test.go | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/executor/adapter.go b/executor/adapter.go index a7c63c854d081..cf61065498f1c 100644 --- a/executor/adapter.go +++ b/executor/adapter.go @@ -90,6 +90,15 @@ func schema2ResultFields(schema *expression.Schema, defaultDB string) (rfs []*as Name: origColName, }, } + // This is for compatibility. + if len(rf.ColumnAsName.O) > mysql.MaxAliasIdentifierLen { + rf.ColumnAsName.O = rf.ColumnAsName.O[:mysql.MaxAliasIdentifierLen] + } + // Usually the length of O equals the length of L. + // Add this len judgement to avoid panic. + if len(rf.ColumnAsName.L) > mysql.MaxAliasIdentifierLen { + rf.ColumnAsName.L = rf.ColumnAsName.L[:mysql.MaxAliasIdentifierLen] + } rfs = append(rfs, rf) } return rfs diff --git a/server/tidb_test.go b/server/tidb_test.go index 84bb7c5921055..cf7677c46473c 100644 --- a/server/tidb_test.go +++ b/server/tidb_test.go @@ -544,6 +544,22 @@ func (ts *TidbTestSuite) TestFieldList(c *C) { // c_decimal decimal(6, 3) c.Assert(colInfos[5].Decimal, Equals, uint8(3)) + + // for issue#10513 + tooLongColumnAsName := "COALESCE(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)" + columnAsName := tooLongColumnAsName[:tmysql.MaxAliasIdentifierLen] + + rs, err := qctx.Execute(ctx, "select "+tooLongColumnAsName) + c.Assert(err, IsNil) + cols := rs[0].Columns() + c.Assert(cols[0].OrgName, Equals, tooLongColumnAsName) + c.Assert(cols[0].Name, Equals, columnAsName) + + rs, err = qctx.Execute(ctx, "select c_bit as '"+tooLongColumnAsName+"' from t") + c.Assert(err, IsNil) + cols = rs[0].Columns() + c.Assert(cols[0].OrgName, Equals, "c_bit") + c.Assert(cols[0].Name, Equals, columnAsName) } func (ts *TidbTestSuite) TestSumAvg(c *C) {