Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix numeric #1198

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions flow/connectors/postgres/qvalue_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"math/big"
"strings"
"time"
Expand All @@ -19,6 +18,8 @@ import (
geom "github.com/twpayne/go-geos"
)

var big10 = big.NewInt(10)

func postgresOIDToQValueKind(recvOID uint32) qvalue.QValueKind {
switch recvOID {
case pgtype.BoolOID:
Expand Down Expand Up @@ -380,13 +381,14 @@ func numericToRat(numVal *pgtype.Numeric) (*big.Rat, error) {
case pgtype.NegativeInfinity, pgtype.Infinity:
return nil, errors.New("numeric value is infinity")
}

rat := new(big.Rat)

rat.SetInt(numVal.Int)
divisor := new(big.Rat).SetFloat64(math.Pow10(int(-numVal.Exp)))
rat.Quo(rat, divisor)

rat := new(big.Rat).SetInt(numVal.Int)
if numVal.Exp > 0 {
mul := new(big.Int).Exp(big10, big.NewInt(int64(numVal.Exp)), nil)
rat.Mul(rat, new(big.Rat).SetInt(mul))
} else if numVal.Exp < 0 {
mul := new(big.Int).Exp(big10, big.NewInt(int64(-numVal.Exp)), nil)
rat.Quo(rat, new(big.Rat).SetInt(mul))
}
return rat, nil
}

Expand Down
3 changes: 2 additions & 1 deletion flow/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/PeerDB-io/peer-flow/generated/protos"
"github.com/PeerDB-io/peer-flow/model/numeric"
"github.com/PeerDB-io/peer-flow/model/qvalue"
)

Expand Down Expand Up @@ -149,7 +150,7 @@ func (r *RecordItems) toMap() (map[string]interface{}, error) {
if !ok {
return nil, errors.New("expected *big.Rat value")
}
jsonStruct[col] = bigRat.FloatString(9)
jsonStruct[col] = bigRat.FloatString(numeric.PeerDBNumericScale)
default:
jsonStruct[col] = v.Value
}
Expand Down
11 changes: 11 additions & 0 deletions flow/model/numeric/scale.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package numeric

import "strings"

const PeerDBNumericScale = 9

func StripTrailingZeros(value string) string {
value = strings.TrimRight(value, "0")
value = strings.TrimSuffix(value, ".")
return value
}
3 changes: 3 additions & 0 deletions flow/model/qvalue/avro_converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"math/big"
"time"

"github.com/PeerDB-io/peer-flow/model/numeric"
"github.com/google/uuid"
"github.com/linkedin/goavro/v2"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -256,6 +257,8 @@ func (c *QValueAvroConverter) processNumeric() (interface{}, error) {
return nil, fmt.Errorf("invalid Numeric value: expected *big.Rat, got %T", c.Value.Value)
}

decimalValue := num.FloatString(numeric.PeerDBNumericScale)
num.SetString(decimalValue)
if c.Nullable {
return goavro.Union("bytes.decimal", num), nil
}
Expand Down
23 changes: 20 additions & 3 deletions flow/model/qvalue/qvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"math/big"
"reflect"
"strconv"
"strings"
"time"

"github.com/PeerDB-io/peer-flow/model/numeric"
"github.com/google/uuid"
)

Expand Down Expand Up @@ -188,9 +190,24 @@ func compareNumeric(value1, value2 interface{}) bool {
return false
}

// check if the difference is less than 1e-9
diff := new(big.Rat).Sub(rat1, rat2)
return diff.Abs(diff).Cmp(big.NewRat(1, 1000000000)) < 0
str1 := numeric.StripTrailingZeros(rat1.FloatString(10))
str2 := numeric.StripTrailingZeros(rat2.FloatString(10))
if len(str1) > 2 && len(str2) > 2 {
decimals1 := len(str1) - strings.Index(str1, ".") - 1
decimals2 := len(str2) - strings.Index(str2, ".") - 1
// for cases like 1.12345 or 1.12345678, check exact equality
if decimals1 <= 9 && decimals2 <= 9 {
return str1 == str2
} else {
// check if the difference is less than 1e-9
diff := new(big.Rat).Sub(rat1, rat2)
if diff.Cmp(big.NewRat(1, 1000000000)) < 0 {
return true
}
}
}

return str1 == str2
}

func compareString(value1, value2 interface{}) bool {
Expand Down
Loading