Skip to content

Commit

Permalink
use big float
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Sep 30, 2024
1 parent e779d9f commit caa0862
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 76 deletions.
75 changes: 37 additions & 38 deletions internal/planner/max.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package planner

import (
"math"
"math/big"

"github.com/sourcenetwork/immutable"

Expand Down Expand Up @@ -141,35 +141,35 @@ func (n *maxNode) Next() (bool, error) {
}
n.currentValue = n.plan.Value()

var max *float64
var max *big.Float
for _, source := range n.aggregateMapping {
child := n.currentValue.Fields[source.Index]
var collectionMax *float64
var collectionMax *big.Float
var err error
switch childCollection := child.(type) {
case []core.Doc:
collectionMax = reduceDocs(
childCollection,
nil,
func(childItem core.Doc, value *float64) *float64 {
func(childItem core.Doc, value *big.Float) *big.Float {
childProperty := childItem.Fields[source.ChildTarget.Index]
var res float64
res := &big.Float{}
switch v := childProperty.(type) {
case int:
res = float64(v)
res = res.SetInt64(int64(v))
case int64:
res = float64(v)
res = res.SetInt64(v)
case uint64:
res = float64(v)
res = res.SetUint64(v)
case float64:
res = float64(v)
res = res.SetFloat64(v)
default:
return nil
}
if value != nil {
res = math.Max(*value, res)
if value == nil || res.Cmp(value) > 0 {
return res
}
return &res
return value
},
)

Expand All @@ -179,12 +179,12 @@ func (n *maxNode) Next() (bool, error) {
&source,
lessN[int64],
nil,
func(childItem int64, value *float64) *float64 {
res := float64(childItem)
if value != nil {
res = math.Max(*value, res)
func(childItem int64, value *big.Float) *big.Float {
res := (&big.Float{}).SetInt64(childItem)
if value == nil || res.Cmp(value) > 0 {
return res
}
return &res
return value
},
)

Expand All @@ -194,15 +194,15 @@ func (n *maxNode) Next() (bool, error) {
&source,
lessO[int64],
nil,
func(childItem immutable.Option[int64], value *float64) *float64 {
func(childItem immutable.Option[int64], value *big.Float) *big.Float {
if !childItem.HasValue() {
return value
}
res := float64(childItem.Value())
if value != nil {
res = math.Max(*value, res)
res := (&big.Float{}).SetInt64(childItem.Value())
if value == nil || res.Cmp(value) > 0 {
return res
}
return &res
return value
},
)

Expand All @@ -212,12 +212,12 @@ func (n *maxNode) Next() (bool, error) {
&source,
lessN[float64],
nil,
func(childItem float64, value *float64) *float64 {
res := childItem
if value != nil {
res = math.Max(*value, res)
func(childItem float64, value *big.Float) *big.Float {
res := big.NewFloat(childItem)
if value == nil || res.Cmp(value) > 0 {
return res
}
return &res
return value
},
)

Expand All @@ -227,35 +227,34 @@ func (n *maxNode) Next() (bool, error) {
&source,
lessO[float64],
nil,
func(childItem immutable.Option[float64], value *float64) *float64 {
func(childItem immutable.Option[float64], value *big.Float) *big.Float {
if !childItem.HasValue() {
return value
}
res := childItem.Value()
if value != nil {
res = math.Max(*value, res)
res := big.NewFloat(childItem.Value())
if value == nil || res.Cmp(value) > 0 {
return res
}
return &res
return value
},
)
}
if err != nil {
return false, err
}
if max == nil {
if max == nil || collectionMax == nil || collectionMax.Cmp(max) > 0 {
max = collectionMax
} else {
res := math.Max(*max, *collectionMax)
max = &res
}
}

if max == nil {
n.currentValue.Fields[n.virtualFieldIndex] = nil
} else if n.isFloat {
n.currentValue.Fields[n.virtualFieldIndex] = float64(*max)
res, _ := max.Float64()
n.currentValue.Fields[n.virtualFieldIndex] = res
} else {
n.currentValue.Fields[n.virtualFieldIndex] = int64(*max)
res, _ := max.Int64()
n.currentValue.Fields[n.virtualFieldIndex] = res
}
return true, nil
}
75 changes: 37 additions & 38 deletions internal/planner/min.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
package planner

import (
"math"
"math/big"

"github.com/sourcenetwork/immutable"

Expand Down Expand Up @@ -141,35 +141,35 @@ func (n *minNode) Next() (bool, error) {
}
n.currentValue = n.plan.Value()

var min *float64
var min *big.Float
for _, source := range n.aggregateMapping {
child := n.currentValue.Fields[source.Index]
var collectionMin *float64
var collectionMin *big.Float
var err error
switch childCollection := child.(type) {
case []core.Doc:
collectionMin = reduceDocs(
childCollection,
nil,
func(childItem core.Doc, value *float64) *float64 {
func(childItem core.Doc, value *big.Float) *big.Float {
childProperty := childItem.Fields[source.ChildTarget.Index]
var res float64
res := &big.Float{}
switch v := childProperty.(type) {
case int:
res = float64(v)
res = res.SetInt64(int64(v))
case int64:
res = float64(v)
res = res.SetInt64(v)
case uint64:
res = float64(v)
res = res.SetUint64(v)
case float64:
res = float64(v)
res = res.SetFloat64(v)
default:
return nil
}
if value != nil {
res = math.Min(*value, res)
if value == nil || res.Cmp(value) < 0 {
return res
}
return &res
return value
},
)

Expand All @@ -179,12 +179,12 @@ func (n *minNode) Next() (bool, error) {
&source,
lessN[int64],
nil,
func(childItem int64, value *float64) *float64 {
res := float64(childItem)
if value != nil {
res = math.Min(*value, res)
func(childItem int64, value *big.Float) *big.Float {
res := (&big.Float{}).SetInt64(childItem)
if value == nil || res.Cmp(value) < 0 {
return res
}
return &res
return value
},
)

Expand All @@ -194,15 +194,15 @@ func (n *minNode) Next() (bool, error) {
&source,
lessO[int64],
nil,
func(childItem immutable.Option[int64], value *float64) *float64 {
func(childItem immutable.Option[int64], value *big.Float) *big.Float {
if !childItem.HasValue() {
return value
}
res := float64(childItem.Value())
if value != nil {
res = math.Min(*value, res)
res := (&big.Float{}).SetInt64(childItem.Value())
if value == nil || res.Cmp(value) < 0 {
return res
}
return &res
return value
},
)

Expand All @@ -212,12 +212,12 @@ func (n *minNode) Next() (bool, error) {
&source,
lessN[float64],
nil,
func(childItem float64, value *float64) *float64 {
res := childItem
if value != nil {
res = math.Min(*value, res)
func(childItem float64, value *big.Float) *big.Float {
res := big.NewFloat(childItem)
if value == nil || res.Cmp(value) < 0 {
return res
}
return &res
return value
},
)

Expand All @@ -227,35 +227,34 @@ func (n *minNode) Next() (bool, error) {
&source,
lessO[float64],
nil,
func(childItem immutable.Option[float64], value *float64) *float64 {
func(childItem immutable.Option[float64], value *big.Float) *big.Float {
if !childItem.HasValue() {
return value
}
res := childItem.Value()
if value != nil {
res = math.Min(*value, res)
res := big.NewFloat(childItem.Value())
if value == nil || res.Cmp(value) < 0 {
return res
}
return &res
return value
},
)
}
if err != nil {
return false, err
}
if min == nil {
if min == nil || collectionMin == nil || collectionMin.Cmp(min) < 0 {
min = collectionMin
} else {
res := math.Min(*min, *collectionMin)
min = &res
}
}

if min == nil {
n.currentValue.Fields[n.virtualFieldIndex] = nil
} else if n.isFloat {
n.currentValue.Fields[n.virtualFieldIndex] = float64(*min)
res, _ := min.Float64()
n.currentValue.Fields[n.virtualFieldIndex] = res
} else {
n.currentValue.Fields[n.virtualFieldIndex] = int64(*min)
res, _ := min.Int64()
n.currentValue.Fields[n.virtualFieldIndex] = res
}
return true, nil
}

0 comments on commit caa0862

Please sign in to comment.