Skip to content

Commit 973649d

Browse files
authored
improvements to time to wire format (#3570)
1 parent f066a75 commit 973649d

4 files changed

Lines changed: 33 additions & 32 deletions

File tree

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 h1:bMGS25NWAGTE
2020
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71/go.mod h1:2/2zjLQ/JOOSbbSboojeg+cAwcRV0fDLzIiWch/lhqI=
2121
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216 h1:JWkKRE4EHUcEVQCMRBej8DYxjYjRz/9MdF/NNQh0o70=
2222
github.com/dolthub/sqllogictest/go v0.0.0-20240618184124-ca47f9354216/go.mod h1:e/FIZVvT2IR53HBCAo41NjqgtEnjMJGKca3Y/dAmZaA=
23-
github.com/dolthub/vitess v0.0.0-20260521165014-2fdb4300ae8d h1:rN3SAE6dOwH3NmqkTR8TL46nT5B1U00koXmSjUDFpeo=
24-
github.com/dolthub/vitess v0.0.0-20260521165014-2fdb4300ae8d/go.mod h1:dKAkzdfRkAudpc0g8JOQ0eiEjV83TYIFz/yNIEdcjXM=
2523
github.com/dolthub/vitess v0.0.0-20260528164423-e3f9fa81284c h1:DRmvqtt1OCIdtFAg8daaaIOGEe6gVWiSlhABDi+lNIg=
2624
github.com/dolthub/vitess v0.0.0-20260528164423-e3f9fa81284c/go.mod h1:dKAkzdfRkAudpc0g8JOQ0eiEjV83TYIFz/yNIEdcjXM=
2725
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=

sql/types/datetime.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -532,20 +532,16 @@ func appendDateFormat(dest []byte, t time.Time) []byte {
532532
} else {
533533
dest = strconv.AppendInt(dest, int64(year), 10)
534534
}
535-
dest = append(dest, '-')
536-
537535
month := int64(m)
538-
if month < 10 {
539-
dest = append(dest, '0')
540-
}
541-
dest = strconv.AppendInt(dest, month, 10)
542-
dest = append(dest, '-')
543-
544536
day := int64(d)
545-
if day < 10 {
546-
dest = append(dest, '0')
547-
}
548-
dest = strconv.AppendInt(dest, day, 10)
537+
dest = append(dest,
538+
'-',
539+
'0'+byte(month/10),
540+
'0'+byte(month%10),
541+
'-',
542+
'0'+byte(day/10),
543+
'0'+byte(day%10),
544+
)
549545
return dest
550546
}
551547

sql/types/decimal.go

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"gopkg.in/src-d/go-errors.v1"
2929

3030
"github.com/dolthub/go-mysql-server/sql"
31-
"github.com/dolthub/go-mysql-server/sql/encodings"
3231
"github.com/dolthub/go-mysql-server/sql/values"
3332
)
3433

@@ -343,16 +342,19 @@ func (t DecimalType_) SQL(ctx *sql.Context, dest []byte, v interface{}) (sqltype
343342
if err != nil {
344343
return sqltypes.Value{}, err
345344
}
346-
val := encodings.StringToBytes(t.DecimalValueStringFixed(value))
347-
return sqltypes.MakeTrusted(sqltypes.Decimal, val), nil
345+
// TODO: reslice?
346+
dest = t.AppendStringFixedDecimal(dest, value)
347+
return sqltypes.MakeTrusted(sqltypes.Decimal, dest), nil
348348
}
349349

350350
func (t DecimalType_) SQLValue(ctx *sql.Context, v sql.Value, dest []byte) (sqltypes.Value, error) {
351351
if v.IsNull() {
352352
return sqltypes.NULL, nil
353353
}
354354
d := values.ReadDecimal(v.Val)
355-
return sqltypes.MakeTrusted(sqltypes.Decimal, encodings.StringToBytes(t.DecimalValueStringFixed(d))), nil
355+
// TODO: reslice?
356+
dest = t.AppendStringFixedDecimal(dest, d)
357+
return sqltypes.MakeTrusted(sqltypes.Decimal, dest), nil
356358
}
357359

358360
// String implements Type interface.
@@ -399,8 +401,9 @@ func (t DecimalType_) Scale() uint8 {
399401
return t.scale
400402
}
401403

402-
// DecimalValueStringFixed returns string value for the given decimal value. If decimal type value is for valid table column only,
403-
// it should use scale defined by the column. Otherwise, the result value should use its own precision and scale.
404+
// DecimalValueStringFixed returns the string for the given decimal value.
405+
// If the decimal type value is for the valid table column only, it should use scale defined by the column.
406+
// Otherwise, the result value should use its own precision and scale.
404407
func (t DecimalType_) DecimalValueStringFixed(v *apd.Decimal) string {
405408
if t.definesColumn {
406409
if int32(t.scale) != v.Exponent {
@@ -410,6 +413,18 @@ func (t DecimalType_) DecimalValueStringFixed(v *apd.Decimal) string {
410413
return v.Text('f')
411414
}
412415

416+
// AppendStringFixedDecimal appends the decimal value to the given []byte dest.
417+
// If the decimal type value is for the valid table column only, it should use scale defined by the column.
418+
// Otherwise, the result value should use its own precision and scale.
419+
func (t DecimalType_) AppendStringFixedDecimal(dest []byte, v *apd.Decimal) []byte {
420+
if t.definesColumn {
421+
if int32(t.scale) != v.Exponent {
422+
v, _ = sql.DecimalRound(v, int32(t.scale))
423+
}
424+
}
425+
return v.Append(dest, 'f')
426+
}
427+
413428
func convertValueToDecimal(ctx *sql.Context, v sql.Value) (*apd.Decimal, error) {
414429
switch v.Typ {
415430
case sqltypes.Int8:

sql/types/time.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -520,18 +520,10 @@ func appendTimeFormat(dest []byte, h, m, s, ms int64, msPrecision int) []byte {
520520
dest = append(dest, '0')
521521
}
522522
dest = strconv.AppendInt(dest, h, 10)
523-
dest = append(dest, ':')
524-
525-
if m < 10 {
526-
dest = append(dest, '0')
527-
}
528-
dest = strconv.AppendInt(dest, m, 10)
529-
dest = append(dest, ':')
530-
531-
if s < 10 {
532-
dest = append(dest, '0')
533-
}
534-
dest = strconv.AppendInt(dest, s, 10)
523+
dest = append(dest,
524+
':',
525+
'0'+byte(m/10), '0'+byte(m%10), ':',
526+
'0'+byte(s/10), '0'+byte(s%10))
535527

536528
if msPrecision > 0 {
537529
dest = appendMicroseconds(dest, ms, msPrecision)

0 commit comments

Comments
 (0)