Skip to content

Commit 43725dd

Browse files
author
James Cor
committed
char refactor
1 parent c1c0891 commit 43725dd

File tree

3 files changed

+61
-483
lines changed

3 files changed

+61
-483
lines changed

sql/expression/function/char.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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
98106
func (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)

sql/types/conversion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ func TypeAwareConversion(ctx *sql.Context, val interface{}, originalType sql.Typ
767767
// cleanly and the type is automatically coerced (i.e. string and numeric types), then a warning is logged and the
768768
// value is truncated to the Zero value for type |t|. If the value does not convert and the type is not automatically
769769
// coerced, then return an error.
770-
// TODO: This logic should be in types.Convert()
770+
// TODO: Should truncate to number prefix instead of Zero.
771771
func ConvertOrTruncate(ctx *sql.Context, i interface{}, t sql.Type) (interface{}, error) {
772772
converted, _, err := t.Convert(ctx, i)
773773
if err == nil {

0 commit comments

Comments
 (0)