@@ -85,18 +85,26 @@ func (c *Char) CollationCoercibility(ctx *sql.Context) (collation sql.CollationI
8585 return sql .Collation_binary , 5
8686}
8787
88- // char converts num into a byte array
89- // This function is essentially converting the number to base 256
90- func char (num uint32 ) []byte {
91- if num == 0 {
92- return []byte {}
88+ // encodeUint32 converts uint32 `num` into a []byte using the fewest number of bytes in big endian (no leading 0s)
89+ func encodeUint32 (num uint32 ) []byte {
90+ res := []byte {
91+ byte (num >> 24 ),
92+ byte (num >> 16 ),
93+ byte (num >> 8 ),
94+ byte (num ),
9395 }
94- return append (char (num >> 8 ), byte (num & 255 ))
96+ var i int
97+ for i = 0 ; i < 3 ; i ++ {
98+ if res [i ] != 0 {
99+ break
100+ }
101+ }
102+ return res [i :]
95103}
96104
97105// Eval implements the sql.Expression interface
98106func (c * Char ) Eval (ctx * sql.Context , row sql.Row ) (interface {}, error ) {
99- res := []byte {}
107+ var res []byte
100108 for _ , arg := range c .args {
101109 if arg == nil {
102110 continue
@@ -114,11 +122,9 @@ func (c *Char) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
114122 v , _ , err := types .Uint32 .Convert (ctx , val )
115123 if err != nil {
116124 ctx .Warn (1292 , "Truncated incorrect INTEGER value: '%v'" , val )
117- res = append (res , 0 )
118- continue
119125 }
120126
121- res = append (res , char (v .(uint32 ))... )
127+ res = append (res , encodeUint32 (v .(uint32 ))... )
122128 }
123129
124130 result , _ , err := c .Type ().Convert (ctx , res )
0 commit comments