Skip to content

Commit

Permalink
add max value tests. refactor min max return type handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf committed Oct 1, 2024
1 parent 9a10cb1 commit 623e275
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 40 deletions.
35 changes: 15 additions & 20 deletions internal/planner/max.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type maxNode struct {
documentIterator
docMapper

p *Planner
plan planNode
p *Planner
plan planNode
parent *mapper.Select

isFloat bool
// virtualFieldIndex is the index of the field
// that contains the result of the aggregate.
virtualFieldIndex int
Expand All @@ -45,22 +45,9 @@ func (p *Planner) Max(
field *mapper.Aggregate,
parent *mapper.Select,
) (*maxNode, error) {
isFloat := false
for _, target := range field.AggregateTargets {
isTargetFloat, err := p.isValueFloat(parent, &target)
if err != nil {
return nil, err
}
// If one source property is a float, the result will be a float - no need to check the rest
if isTargetFloat {
isFloat = true
break
}
}

return &maxNode{
p: p,
isFloat: isFloat,
parent: parent,
aggregateMapping: field.AggregateTargets,
virtualFieldIndex: field.Index,
docMapper: docMapper{field.DocumentMapping},
Expand Down Expand Up @@ -142,6 +129,8 @@ func (n *maxNode) Next() (bool, error) {
n.currentValue = n.plan.Value()

var max *big.Float
isFloat := false

for _, source := range n.aggregateMapping {
child := n.currentValue.Fields[source.Index]
var collectionMax *big.Float
Expand Down Expand Up @@ -242,14 +231,20 @@ func (n *maxNode) Next() (bool, error) {
if err != nil {
return false, err

Check warning on line 232 in internal/planner/max.go

View check run for this annotation

Codecov / codecov/patch

internal/planner/max.go#L232

Added line #L232 was not covered by tests
}
if max == nil || collectionMax == nil || collectionMax.Cmp(max) > 0 {
max = collectionMax
if collectionMax == nil || (max != nil && collectionMax.Cmp(max) <= 0) {
continue
}
isTargetFloat, err := n.p.isValueFloat(n.parent, &source)
if err != nil {
return false, err

Check warning on line 239 in internal/planner/max.go

View check run for this annotation

Codecov / codecov/patch

internal/planner/max.go#L239

Added line #L239 was not covered by tests
}
isFloat = isTargetFloat
max = collectionMax
}

if max == nil {
n.currentValue.Fields[n.virtualFieldIndex] = nil
} else if n.isFloat {
} else if isFloat {
res, _ := max.Float64()
n.currentValue.Fields[n.virtualFieldIndex] = res
} else {
Expand Down
35 changes: 15 additions & 20 deletions internal/planner/min.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ type minNode struct {
documentIterator
docMapper

p *Planner
plan planNode
p *Planner
plan planNode
parent *mapper.Select

isFloat bool
// virtualFieldIndex is the index of the field
// that contains the result of the aggregate.
virtualFieldIndex int
Expand All @@ -45,22 +45,9 @@ func (p *Planner) Min(
field *mapper.Aggregate,
parent *mapper.Select,
) (*minNode, error) {
isFloat := false
for _, target := range field.AggregateTargets {
isTargetFloat, err := p.isValueFloat(parent, &target)
if err != nil {
return nil, err
}
// If one source property is a float, the result will be a float - no need to check the rest
if isTargetFloat {
isFloat = true
break
}
}

return &minNode{
p: p,
isFloat: isFloat,
parent: parent,
aggregateMapping: field.AggregateTargets,
virtualFieldIndex: field.Index,
docMapper: docMapper{field.DocumentMapping},
Expand Down Expand Up @@ -142,6 +129,8 @@ func (n *minNode) Next() (bool, error) {
n.currentValue = n.plan.Value()

var min *big.Float
isFloat := false

for _, source := range n.aggregateMapping {
child := n.currentValue.Fields[source.Index]
var collectionMin *big.Float
Expand Down Expand Up @@ -242,14 +231,20 @@ func (n *minNode) Next() (bool, error) {
if err != nil {
return false, err

Check warning on line 232 in internal/planner/min.go

View check run for this annotation

Codecov / codecov/patch

internal/planner/min.go#L232

Added line #L232 was not covered by tests
}
if min == nil || collectionMin == nil || collectionMin.Cmp(min) < 0 {
min = collectionMin
if collectionMin == nil || (min != nil && collectionMin.Cmp(min) >= 0) {
continue
}
isTargetFloat, err := n.p.isValueFloat(n.parent, &source)
if err != nil {
return false, err

Check warning on line 239 in internal/planner/min.go

View check run for this annotation

Codecov / codecov/patch

internal/planner/min.go#L239

Added line #L239 was not covered by tests
}
isFloat = isTargetFloat
min = collectionMin
}

if min == nil {
n.currentValue.Fields[n.virtualFieldIndex] = nil
} else if n.isFloat {
} else if isFloat {
res, _ := min.Float64()
n.currentValue.Fields[n.virtualFieldIndex] = res
} else {
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/query/simple/with_max_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
package simple

import (
"math"
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"

"github.com/sourcenetwork/immutable"
)

func TestQuerySimple_WithMaxOnUndefinedObject_ReturnsError(t *testing.T) {
Expand Down Expand Up @@ -95,3 +98,32 @@ func TestQuerySimple_WithMax_Succeeds(t *testing.T) {

executeTestCase(t, test)
}

func TestQuerySimple_WithMaxAndMaxValueInt_Succeeds(t *testing.T) {
test := testUtils.TestCase{
SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
// GraphQL does not support 64 bit int
testUtils.CollectionSaveMutationType,
testUtils.CollectionNamedMutationType,
}),
Description: "Simple query max and max value int",
Actions: []any{
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "John",
"Age": int64(math.MaxInt64),
},
},
testUtils.Request{
Request: `query {
_max(Users: {field: Age})
}`,
Results: map[string]any{
"_max": int64(math.MaxInt64),
},
},
},
}

executeTestCase(t, test)
}
32 changes: 32 additions & 0 deletions tests/integration/query/simple/with_min_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
package simple

import (
"math"
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"

"github.com/sourcenetwork/immutable"
)

func TestQuerySimple_WithMinOnUndefinedObject_ReturnsError(t *testing.T) {
Expand Down Expand Up @@ -95,3 +98,32 @@ func TestQuerySimple_WithMin_Succeeds(t *testing.T) {

executeTestCase(t, test)
}

func TestQuerySimple_WithMinAndMaxValueInt_Succeeds(t *testing.T) {
test := testUtils.TestCase{
SupportedMutationTypes: immutable.Some([]testUtils.MutationType{
// GraphQL does not support 64 bit int
testUtils.CollectionSaveMutationType,
testUtils.CollectionNamedMutationType,
}),
Description: "Simple query min and max value int",
Actions: []any{
testUtils.CreateDoc{
DocMap: map[string]any{
"Name": "John",
"Age": int64(math.MaxInt64),
},
},
testUtils.Request{
Request: `query {
_max(Users: {field: Age})
}`,
Results: map[string]any{
"_max": int64(math.MaxInt64),
},
},
},
}

executeTestCase(t, test)
}

0 comments on commit 623e275

Please sign in to comment.