Skip to content

Commit 8079228

Browse files
author
James Cor
committed
fix types over wire
1 parent f44e789 commit 8079228

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

sql/types/bit.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,13 @@ import (
1818
"context"
1919
"encoding/binary"
2020
"fmt"
21-
"reflect"
22-
"strconv"
23-
2421
"github.com/dolthub/vitess/go/sqltypes"
2522
"github.com/dolthub/vitess/go/vt/proto/query"
2623
"github.com/shopspring/decimal"
2724
"gopkg.in/src-d/go-errors.v1"
25+
"reflect"
2826

2927
"github.com/dolthub/go-mysql-server/sql"
30-
"github.com/dolthub/go-mysql-server/sql/values"
3128
)
3229

3330
const (
@@ -218,9 +215,27 @@ func (t BitType_) ToSQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqltyp
218215
if v.IsNull() {
219216
return sqltypes.NULL, nil
220217
}
221-
// Assume this is uint64
222-
x := values.ReadUint64(v.Val)
223-
dest = strconv.AppendUint(dest, x, 10)
218+
219+
numBytes := t.numOfBits / 8
220+
if t.numOfBits%8 != 0 {
221+
numBytes += 1
222+
}
223+
224+
if uint8(len(v.Val)) < numBytes {
225+
// already in little endian, so just pad with trailing 0s
226+
for i := uint8(len(v.Val)); i <= t.numOfBits/8; i++ {
227+
v.Val = append(v.Val, 0)
228+
}
229+
} else {
230+
v.Val = v.Val[:numBytes]
231+
}
232+
233+
// TODO: for whatever reason, TestTypesOverWire only works when this is a deep copy?
234+
dest = append(dest, v.Val...)
235+
// want the results in big endian
236+
for i, j := 0, len(dest)-1; i < j; i, j = i+1, j-1 {
237+
dest[i], dest[j] = dest[j], dest[i]
238+
}
224239
return sqltypes.MakeTrusted(sqltypes.Bit, dest), nil
225240
}
226241

sql/types/datetime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func (t datetimeType) ToSQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sq
487487
m := (x & (255 << 8)) >> 8
488488
d := x & 255
489489
t := time.Date(int(y), time.Month(m), int(d), 0, 0, 0, 0, time.UTC)
490-
dest = t.AppendFormat(dest, sql.TimestampDatetimeLayout)
490+
dest = t.AppendFormat(dest, sql.DateLayout)
491491

492492
case sqltypes.Datetime, sqltypes.Timestamp:
493493
x := values.ReadInt64(v.Val)

sql/types/decimal.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package types
1717
import (
1818
"context"
1919
"fmt"
20+
"github.com/dolthub/go-mysql-server/sql/values"
2021
"math/big"
2122
"reflect"
2223
"strings"
@@ -329,6 +330,22 @@ func (t DecimalType_) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltype
329330
return sqltypes.MakeTrusted(sqltypes.Decimal, val), nil
330331
}
331332

333+
func (t DecimalType_) ToSQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqltypes.Value, error) {
334+
if v.IsNull() {
335+
return sqltypes.NULL, nil
336+
}
337+
// TODO: implement values.ReadDecimal
338+
e := values.ReadInt32(v.Val[:values.Int32Size])
339+
s := values.ReadInt8(v.Val[values.Int32Size : values.Int32Size+values.Int8Size])
340+
b := big.NewInt(0).SetBytes(v.Val[values.Int32Size+values.Int8Size:])
341+
if s < 0 {
342+
b = b.Neg(b)
343+
}
344+
d := decimal.NewFromBigInt(b, e)
345+
val := AppendAndSliceString(dest, t.DecimalValueStringFixed(d))
346+
return sqltypes.MakeTrusted(sqltypes.Decimal, val), nil
347+
}
348+
332349
// String implements Type interface.
333350
func (t DecimalType_) String() string {
334351
return fmt.Sprintf("decimal(%v,%v)", t.precision, t.scale)

sql/types/year.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func (t YearType_) ToSQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqlty
176176
if v.IsNull() {
177177
return sqltypes.NULL, nil
178178
}
179-
x := values.ReadUint8(v.Val)
179+
x := values.ReadUint16(v.Val)
180180
dest = strconv.AppendInt(dest, int64(x), 10)
181181
return sqltypes.MakeTrusted(sqltypes.Year, dest), nil
182182
}

sql/values/encoding.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,13 @@ func ReadUint16(val []byte) uint16 {
131131

132132
func ReadInt24(val []byte) (i int32) {
133133
expectSize(val, Int32Size)
134-
i = int32(binary.LittleEndian.Uint32([]byte{0, val[0], val[1], val[2]}))
134+
i = int32(binary.LittleEndian.Uint32(val))
135135
return
136136
}
137137

138138
func ReadUint24(val []byte) (u uint32) {
139139
expectSize(val, Int32Size)
140-
var tmp [4]byte
141-
// copy |val| to |tmp|
142-
tmp[3], tmp[2] = val[3], val[2]
143-
tmp[1], tmp[0] = val[1], val[0]
144-
u = binary.LittleEndian.Uint32(tmp[:])
140+
u = binary.LittleEndian.Uint32(val)
145141
return
146142
}
147143

0 commit comments

Comments
 (0)