Fix numeric NaN support

fixes #93
non-blocking
Jack Christensen 2021-03-11 19:48:47 -06:00
parent abeb337246
commit 0f1bda20b0
2 changed files with 9 additions and 10 deletions

View File

@ -16,8 +16,8 @@ import (
const nbase = 10000
const (
pgNumericNaN = 0x000000000c000000
pgNumericNaNSign = 0x0c00
pgNumericNaN = 0x00000000c0000000
pgNumericNaNSign = 0xc000
)
var big0 *big.Int = big.NewInt(0)
@ -406,7 +406,7 @@ func (dst *Numeric) DecodeText(ci *ConnInfo, src []byte) error {
return nil
}
if string(src) == "'NaN'" { // includes single quotes, see EncodeText for details.
if string(src) == "NaN" {
*dst = Numeric{Status: Present, NaN: true}
return nil
}
@ -456,7 +456,7 @@ func (dst *Numeric) DecodeBinary(ci *ConnInfo, src []byte) error {
rp += 2
weight := int16(binary.BigEndian.Uint16(src[rp:]))
rp += 2
sign := int16(binary.BigEndian.Uint16(src[rp:]))
sign := uint16(binary.BigEndian.Uint16(src[rp:]))
rp += 2
dscale := int16(binary.BigEndian.Uint16(src[rp:]))
rp += 2
@ -573,11 +573,7 @@ func (src Numeric) EncodeText(ci *ConnInfo, buf []byte) ([]byte, error) {
}
if src.NaN {
// encode as 'NaN' including single quotes,
// "When writing this value [NaN] as a constant in an SQL command,
// you must put quotes around it, for example UPDATE table SET x = 'NaN'"
// https://www.postgresql.org/docs/9.3/datatype-numeric.html
buf = append(buf, "'NaN'"...)
buf = append(buf, "NaN"...)
return buf, nil
}

View File

@ -15,7 +15,8 @@ import (
func numericEqual(left, right *pgtype.Numeric) bool {
return left.Status == right.Status &&
left.Exp == right.Exp &&
((left.Int == nil && right.Int == nil) || (left.Int != nil && right.Int != nil && left.Int.Cmp(right.Int) == 0))
((left.Int == nil && right.Int == nil) || (left.Int != nil && right.Int != nil && left.Int.Cmp(right.Int) == 0)) &&
left.NaN == right.NaN
}
// For test purposes only.
@ -117,6 +118,8 @@ func TestNumericNormalize(t *testing.T) {
func TestNumericTranscode(t *testing.T) {
testutil.TestSuccessfulTranscodeEqFunc(t, "numeric", []interface{}{
&pgtype.Numeric{NaN: true, Status: pgtype.Present},
&pgtype.Numeric{Int: big.NewInt(0), Exp: 0, Status: pgtype.Present},
&pgtype.Numeric{Int: big.NewInt(1), Exp: 0, Status: pgtype.Present},
&pgtype.Numeric{Int: big.NewInt(-1), Exp: 0, Status: pgtype.Present},