Skip to content

Commit

Permalink
*: use FieldType.Decimal to control the decimal length of double valu…
Browse files Browse the repository at this point in the history
…es to be shown in client (pingcap#4191)
  • Loading branch information
zz-jason authored and shenli committed Aug 15, 2017
1 parent a454c57 commit 1dc7bbe
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
4 changes: 2 additions & 2 deletions expression/builtin_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,8 +1370,8 @@ func (c *piFunctionClass) getFunction(args []Expression, ctx context.Context) (b
return nil, errors.Trace(err)
}

bf.tp.Decimal = 15
bf.tp.Flen = 17
bf.tp.Decimal = 6
bf.tp.Flen = 8
sig = &builtinPISig{baseRealBuiltinFunc{bf}}
return sig.setSelf(sig), nil
}
Expand Down
2 changes: 1 addition & 1 deletion plan/typeinfer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func (s *testPlanSuite) createTestCase4MathFuncs() []typeInferTestCase {
{"exp(c_time)", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxRealWidth, types.UnspecifiedLength},
{"exp(c_timestamp)", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxRealWidth, types.UnspecifiedLength},
{"exp(c_binary)", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, mysql.MaxRealWidth, types.UnspecifiedLength},
{"pi()", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, 17, 15},
{"pi()", mysql.TypeDouble, charset.CharsetBin, mysql.BinaryFlag, 8, 6},
{"~c_int", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag | mysql.UnsignedFlag, mysql.MaxIntWidth, 0},
{"!c_int", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag, 1, 0},
{"c_int & c_int", mysql.TypeLonglong, charset.CharsetBin, mysql.BinaryFlag | mysql.UnsignedFlag, mysql.MaxIntWidth, 0},
Expand Down
2 changes: 1 addition & 1 deletion server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ func (cc *clientConn) writeResultset(rs ResultSet, binary bool, more bool) error
continue
}
var valData []byte
valData, err = dumpTextValue(columns[i].Type, value)
valData, err = dumpTextValue(columns[i], value)
if err != nil {
return errors.Trace(err)
}
Expand Down
10 changes: 5 additions & 5 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,22 +318,22 @@ func dumpRowValuesBinary(alloc arena.Allocator, columns []*ColumnInfo, row []typ
return
}

func dumpTextValue(mysqlType uint8, value types.Datum) ([]byte, error) {
func dumpTextValue(colInfo *ColumnInfo, value types.Datum) ([]byte, error) {
switch value.Kind() {
case types.KindInt64:
return strconv.AppendInt(nil, value.GetInt64(), 10), nil
case types.KindUint64:
return strconv.AppendUint(nil, value.GetUint64(), 10), nil
case types.KindFloat32:
prec := -1
if frac := value.Frac(); frac > 0 {
prec = frac
if colInfo.Decimal > 0 && int(colInfo.Decimal) != mysql.NotFixedDec {
prec = int(colInfo.Decimal)
}
return strconv.AppendFloat(nil, value.GetFloat64(), 'f', prec, 32), nil
case types.KindFloat64:
prec := -1
if frac := value.Frac(); frac > 0 {
prec = frac
if colInfo.Decimal > 0 && int(colInfo.Decimal) != mysql.NotFixedDec {
prec = int(colInfo.Decimal)
}
return strconv.AppendFloat(nil, value.GetFloat64(), 'f', prec, 64), nil
case types.KindString, types.KindBytes:
Expand Down
42 changes: 29 additions & 13 deletions server/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,50 @@ func (s *testUtilSuite) TestDumpBinaryTime(c *C) {

func (s *testUtilSuite) TestDumpTextValue(c *C) {
defer testleak.AfterTest(c)()
bs, err := dumpTextValue(mysql.TypeLonglong, types.NewIntDatum(10))

colInfo := &ColumnInfo{
Type: mysql.TypeLonglong,
Decimal: mysql.NotFixedDec,
}
bs, err := dumpTextValue(colInfo, types.NewIntDatum(10))
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "10")

bs, err = dumpTextValue(mysql.TypeLonglong, types.NewUintDatum(11))
bs, err = dumpTextValue(colInfo, types.NewUintDatum(11))
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "11")

colInfo.Type = mysql.TypeFloat
colInfo.Decimal = 1
f32 := types.NewFloat32Datum(1.2)
bs, err = dumpTextValue(mysql.TypeDouble, f32)
bs, err = dumpTextValue(colInfo, f32)
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "1.2")
f32.SetFrac(2)
bs, err = dumpTextValue(mysql.TypeDouble, f32)

colInfo.Decimal = 2
bs, err = dumpTextValue(colInfo, f32)
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "1.20")

f64 := types.NewFloat64Datum(2.2)
bs, err = dumpTextValue(mysql.TypeDouble, f64)
colInfo.Type = mysql.TypeDouble
colInfo.Decimal = 1
bs, err = dumpTextValue(colInfo, f64)
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "2.2")
f64.SetFrac(2)
bs, err = dumpTextValue(mysql.TypeDouble, f64)

colInfo.Decimal = 2
bs, err = dumpTextValue(colInfo, f64)
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "2.20")

bs, err = dumpTextValue(mysql.TypeBlob, types.NewBytesDatum([]byte("foo")))
colInfo.Type = mysql.TypeBlob
bs, err = dumpTextValue(colInfo, types.NewBytesDatum([]byte("foo")))
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "foo")

bs, err = dumpTextValue(mysql.TypeVarchar, types.NewStringDatum("bar"))
colInfo.Type = mysql.TypeVarchar
bs, err = dumpTextValue(colInfo, types.NewStringDatum("bar"))
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "bar")

Expand All @@ -91,19 +104,22 @@ func (s *testUtilSuite) TestDumpTextValue(c *C) {
time, err := types.ParseTime("2017-01-05 23:59:59.575601", mysql.TypeDatetime, 0)
c.Assert(err, IsNil)
d.SetMysqlTime(time)
bs, err = dumpTextValue(mysql.TypeDatetime, d)
colInfo.Type = mysql.TypeDatetime
bs, err = dumpTextValue(colInfo, d)
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "2017-01-06 00:00:00")

duration, err := types.ParseDuration("11:30:45", 0)
c.Assert(err, IsNil)
d.SetMysqlDuration(duration)
bs, err = dumpTextValue(mysql.TypeDuration, d)
colInfo.Type = mysql.TypeDuration
bs, err = dumpTextValue(colInfo, d)
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "11:30:45")

d.SetMysqlDecimal(types.NewDecFromStringForTest("1.23"))
bs, err = dumpTextValue(mysql.TypeNewDecimal, d)
colInfo.Type = mysql.TypeNewDecimal
bs, err = dumpTextValue(colInfo, d)
c.Assert(err, IsNil)
c.Assert(string(bs), Equals, "1.23")
}

0 comments on commit 1dc7bbe

Please sign in to comment.