Skip to content

Commit

Permalink
executor: fix #10513, alias identifier maximum length compatible with…
Browse files Browse the repository at this point in the history
… MySQL (#10597)
  • Loading branch information
miraclesu authored and sre-bot committed Aug 5, 2019
1 parent 5f9fe27 commit 3f3ead2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions server/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3f3ead2

Please sign in to comment.