From 0f1bda20b06513437fbbe380d444cf1404ce6a2c Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Thu, 11 Mar 2021 19:48:47 -0600 Subject: [PATCH] Fix numeric NaN support fixes #93 --- numeric.go | 14 +++++--------- numeric_test.go | 5 ++++- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/numeric.go b/numeric.go index f2b04006..4d966d5e 100644 --- a/numeric.go +++ b/numeric.go @@ -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 } diff --git a/numeric_test.go b/numeric_test.go index 675eddc4..81595cb3 100644 --- a/numeric_test.go +++ b/numeric_test.go @@ -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},