@@ -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
350350func (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.
404407func (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+
413428func convertValueToDecimal (ctx * sql.Context , v sql.Value ) (* apd.Decimal , error ) {
414429 switch v .Typ {
415430 case sqltypes .Int8 :
0 commit comments