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

Handle NaNs for Floats and Float Arrays #1030

Merged
merged 3 commits into from
Jan 9, 2024
Merged
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
70 changes: 70 additions & 0 deletions flow/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"math"
"math/big"
"slices"
"strconv"
"sync/atomic"
"time"

"github.com/PeerDB-io/peer-flow/generated/protos"
"github.com/PeerDB-io/peer-flow/model/qvalue"
"github.com/PeerDB-io/peer-flow/peerdbenv"
"golang.org/x/exp/constraints"
)

type NameAndExclude struct {
Expand Down Expand Up @@ -129,6 +133,14 @@ func (r *RecordItems) Len() int {
return len(r.Values)
}

func convertFloatArrayToStringArray[T constraints.Float](floatArr []T) []string {
strArr := make([]string, len(floatArr))
for i, v := range floatArr {
strArr[i] = strconv.FormatFloat(float64(v), 'f', -1, 64)
}
return strArr
}

func (r *RecordItems) toMap() (map[string]interface{}, error) {
if r.ColToValIdx == nil {
return nil, errors.New("colToValIdx is nil")
Expand Down Expand Up @@ -167,6 +179,64 @@ func (r *RecordItems) toMap() (map[string]interface{}, error) {
return nil, errors.New("expected *big.Rat value")
}
jsonStruct[col] = bigRat.FloatString(9)
case qvalue.QValueKindFloat64:
floatVal, ok := v.Value.(float64)
if !ok {
return nil, errors.New("expected float64 value")
}
if math.IsNaN(floatVal) {
jsonStruct[col] = nil
} else {
jsonStruct[col] = floatVal
}
case qvalue.QValueKindFloat32:
floatVal, ok := v.Value.(float32)
if !ok {
return nil, errors.New("expected float64 value")
}
if math.IsNaN(float64(floatVal)) {
jsonStruct[col] = nil
} else {
jsonStruct[col] = floatVal
}
case qvalue.QValueKindArrayFloat64:
floatArr, ok := v.Value.([]float64)
if !ok {
return nil, errors.New("expected []float64 value")
}

// json.Marshal cannot support NaN or INF values
// BigQuery cannot support NULL values in arrays
// Solution: Skip NaN values in float arrays we get
cleanedArr := make([]float64, 0, len(floatArr))
for _, val := range floatArr {
if !math.IsNaN(val) && !math.IsInf(val, 0) {
cleanedArr = append(cleanedArr, val)
} else {
slog.Warn("NaN or INF value found in float array - skipping",
slog.Any("array", convertFloatArrayToStringArray(floatArr)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
cannot infer T (model/model.go:136:37) (typecheck)

}
}
jsonStruct[col] = cleanedArr
case qvalue.QValueKindArrayFloat32:
floatArr, ok := v.Value.([]float32)
if !ok {
return nil, errors.New("expected []float32 value")
}

// json.Marshal cannot support NaN or INF values
// BigQuery cannot support NULL values in arrays
// Solution: Skip NaN values in float arrays we get
cleanedArr := make([]float32, 0, len(floatArr))
for _, val := range floatArr {
if !math.IsNaN(float64(val)) && !math.IsInf(float64(val), 0) {
cleanedArr = append(cleanedArr, val)
} else {
slog.Warn("NaN or INF value found in float array - skipping",
slog.Any("array", convertFloatArrayToStringArray(floatArr)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
cannot infer T (model/model.go:136:37) (typecheck)

}
}
jsonStruct[col] = cleanedArr
default:
jsonStruct[col] = v.Value
}
Expand Down
Loading