Skip to content

Commit b9da4b4

Browse files
author
James Cor
committed
opt
1 parent 2ed39f8 commit b9da4b4

File tree

8 files changed

+41
-42
lines changed

8 files changed

+41
-42
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ require (
1414
github.com/lestrrat-go/strftime v1.0.4
1515
github.com/pkg/errors v0.9.1
1616
github.com/pmezard/go-difflib v1.0.0
17-
github.com/shopspring/decimal v1.3.1
17+
github.com/shopspring/decimal v1.4.0
1818
github.com/sirupsen/logrus v1.8.1
1919
github.com/stretchr/testify v1.9.0
2020
go.opentelemetry.io/otel v1.31.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
6666
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
6767
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
6868
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
69-
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
70-
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
69+
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
70+
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
7171
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
7272
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
7373
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

sql/types/datetime.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,15 +487,12 @@ func (t datetimeType) SQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqlt
487487
}
488488
switch t.baseType {
489489
case sqltypes.Date:
490-
// TODO: move this to values package
491490
t := values.ReadDate(v.Val)
492491
dest = t.AppendFormat(dest, sql.DateLayout)
493-
494492
case sqltypes.Datetime, sqltypes.Timestamp:
495493
x := values.ReadInt64(v.Val)
496494
t := time.UnixMicro(x).UTC()
497495
dest = t.AppendFormat(dest, sql.TimestampDatetimeLayout)
498-
499496
default:
500497
return sqltypes.Value{}, sql.ErrInvalidBaseType.New(t.baseType.String(), "datetime")
501498
}

sql/types/decimal.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (t DecimalType_) ConvertToNullDecimal(v interface{}) (decimal.NullDecimal,
216216
case int64:
217217
return t.ConvertToNullDecimal(decimal.NewFromInt(value))
218218
case uint64:
219-
return t.ConvertToNullDecimal(decimal.NewFromBigInt(new(big.Int).SetUint64(value), 0))
219+
return t.ConvertToNullDecimal(decimal.NewFromUint64(value))
220220
case float32:
221221
return t.ConvertToNullDecimal(decimal.NewFromFloat32(value))
222222
case float64:
@@ -351,8 +351,7 @@ func (t DecimalType_) SQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqlt
351351
return sqltypes.NULL, nil
352352
}
353353
d := values.ReadDecimal(v.Val)
354-
val := AppendAndSliceString(dest, t.DecimalValueStringFixed(d))
355-
return sqltypes.MakeTrusted(sqltypes.Decimal, val), nil
354+
return sqltypes.MakeTrusted(sqltypes.Decimal, []byte(t.DecimalValueStringFixed(d))), nil
356355
}
357356

358357
// String implements Type interface.
@@ -437,8 +436,7 @@ func convertValueToDecimal(ctx *sql.Context, v sql.Value) (decimal.Decimal, erro
437436
return decimal.NewFromInt(int64(x)), nil
438437
case sqltypes.Uint64:
439438
x := values.ReadUint64(v.Val)
440-
bi := new(big.Int).SetUint64(x)
441-
return decimal.NewFromBigInt(bi, 0), nil
439+
return decimal.NewFromUint64(x), nil
442440
case sqltypes.Float32:
443441
x := values.ReadFloat32(v.Val)
444442
return decimal.NewFromFloat32(x), nil
@@ -450,8 +448,7 @@ func convertValueToDecimal(ctx *sql.Context, v sql.Value) (decimal.Decimal, erro
450448
return x, nil
451449
case sqltypes.Bit:
452450
x := values.ReadUint64(v.Val)
453-
bi := new(big.Int).SetUint64(x)
454-
return decimal.NewFromBigInt(bi, 0), nil
451+
return decimal.NewFromUint64(x), nil
455452
case sqltypes.Year:
456453
x := values.ReadUint16(v.Val)
457454
return decimal.NewFromInt(int64(x)), nil

sql/types/decimal_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ func TestConvertValueToDecimal(t *testing.T) {
701701
Val: binary.LittleEndian.AppendUint64(nil, math.MaxUint64),
702702
Typ: sqltypes.Uint64,
703703
},
704-
exp: decimal.NewFromBigInt(new(big.Int).SetUint64(math.MaxUint64), 0),
704+
exp: decimal.NewFromUint64(math.MaxUint64),
705705
},
706706

707707
// Float32 -> Decimal
@@ -835,7 +835,7 @@ func TestConvertValueToDecimal(t *testing.T) {
835835
Val: binary.LittleEndian.AppendUint64(nil, math.MaxUint64),
836836
Typ: sqltypes.Bit,
837837
},
838-
exp: decimal.NewFromBigInt(new(big.Int).SetUint64(math.MaxUint64), 0),
838+
exp: decimal.NewFromUint64(math.MaxUint64),
839839
},
840840

841841
// Year -> Decimal

sql/types/set.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (t SetType) SQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqltypes.
285285
}
286286

287287
// TODO: write append style encoder
288-
res, ok := resultCharset.Encoder().Encode(encodings.StringToBytes(value)) // TODO: use unsafe string to byte
288+
res, ok := resultCharset.Encoder().Encode([]byte(value))
289289
if !ok {
290290
if len(value) > 50 {
291291
value = value[:50]

sql/types/time.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,8 @@ func (t TimespanType_) SQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sql
279279
return sqltypes.NULL, nil
280280
}
281281
x := values.ReadInt64(v.Val)
282-
// TODO: write version of this that takes advantage of dest
283-
v.Val = Timespan(x).Bytes()
284-
return sqltypes.MakeTrusted(sqltypes.Time, v.Val), nil
282+
dest = Timespan(x).AppendBytes(dest)
283+
return sqltypes.MakeTrusted(sqltypes.Time, dest), nil
285284
}
286285

287286
// String implements Type interface.
@@ -502,6 +501,35 @@ func (t Timespan) Bytes() []byte {
502501
return ret[:i]
503502
}
504503

504+
func (t Timespan) AppendBytes(dest []byte) []byte {
505+
isNegative, hours, minutes, seconds, microseconds := t.timespanToUnits()
506+
sz := 10
507+
if microseconds > 0 {
508+
sz += 7
509+
}
510+
511+
i := 0
512+
if isNegative {
513+
dest = append(dest, '-')
514+
i++
515+
}
516+
517+
i = appendDigit(int64(hours), 2, dest, i)
518+
dest[i] = ':'
519+
i++
520+
i = appendDigit(int64(minutes), 2, dest, i)
521+
dest[i] = ':'
522+
i++
523+
i = appendDigit(int64(seconds), 2, dest, i)
524+
if microseconds > 0 {
525+
dest[i] = '.'
526+
i++
527+
i = appendDigit(int64(microseconds), 6, dest, i)
528+
}
529+
530+
return dest[:i]
531+
}
532+
505533
// appendDigit format prints 0-entended integer into buffer
506534
func appendDigit(v int64, extend int, buf []byte, i int) int {
507535
cmp := int64(1)

sql/values/encoding.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -250,29 +250,6 @@ func WriteUint16(buf []byte, val uint16) []byte {
250250
return buf
251251
}
252252

253-
func WriteInt24(buf []byte, val int32) []byte {
254-
expectSize(buf, Int24Size)
255-
256-
var tmp [4]byte
257-
binary.LittleEndian.PutUint32(tmp[:], uint32(val))
258-
// copy |tmp| to |buf|
259-
buf[2], buf[1], buf[0] = tmp[2], tmp[1], tmp[0]
260-
return buf
261-
}
262-
263-
func WriteUint24(buf []byte, val uint32) []byte {
264-
expectSize(buf, Uint24Size)
265-
if val > maxUint24 {
266-
panic("uint is greater than max uint24")
267-
}
268-
269-
var tmp [4]byte
270-
binary.LittleEndian.PutUint32(tmp[:], uint32(val))
271-
// copy |tmp| to |buf|
272-
buf[2], buf[1], buf[0] = tmp[2], tmp[1], tmp[0]
273-
return buf
274-
}
275-
276253
func WriteInt32(buf []byte, val int32) []byte {
277254
expectSize(buf, Int32Size)
278255
binary.LittleEndian.PutUint32(buf, uint32(val))

0 commit comments

Comments
 (0)