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: Resolve the extra typeIndexJoins for _avg aggregate #774

Merged
merged 4 commits into from
Sep 15, 2022
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
2 changes: 2 additions & 0 deletions connor/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ type FilterKey interface {
// GetOperatorOrDefault returns either the operator that corresponds
// to this key, or the given default.
GetOperatorOrDefault(defaultOp string) string
// Equal returns true if other is equal, otherwise returns false.
Equal(other FilterKey) bool
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice :) Was hoping you would do this

}
63 changes: 57 additions & 6 deletions query/graphql/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@ func resolveAggregates(
} else {
childObjectIndex := mapping.FirstIndexOfName(target.hostExternalName)
convertedFilter = ToFilter(target.filter, mapping.ChildMappings[childObjectIndex])

host, hasHost = tryGetTarget(target.hostExternalName, convertedFilter, fields)
host, hasHost = tryGetTarget(
target.hostExternalName,
convertedFilter,
target.limit,
fields,
)
}
}

Expand Down Expand Up @@ -752,7 +756,7 @@ func toField(index int, parsed *parser.Select) Field {
}
}

// ConvertFilter converts the given `source` parser filter to a Filter using the given mapping.
// ToFilter converts the given `source` parser filter to a Filter using the given mapping.
//
// Any requestables identified by name will be converted to being identified by index instead.
func ToFilter(source *parser.Filter, mapping *core.DocumentMapping) *Filter {
Expand All @@ -772,7 +776,7 @@ func ToFilter(source *parser.Filter, mapping *core.DocumentMapping) *Filter {
}
}

// convertFilterMap converts a consumer-defined filter key-value into a filter clause
// toFilterMap converts a consumer-defined filter key-value into a filter clause
// keyed by field index.
//
// Return key will either be an int (field index), or a string (operator).
Expand Down Expand Up @@ -961,7 +965,48 @@ func (f *Filter) equal(other *Filter) bool {
return f == nil
}

return reflect.DeepEqual(f.Conditions, other.Conditions)
return deepEqualConditions(f.Conditions, other.Conditions)
}

// deepEqualConditions performs a deep equality of two conditions.
// Handles: map[0xc00069cfd0:map[0xc005eda8c0:<nil>]] -> map[{5}:map[{_ne}:<nil>]]
func deepEqualConditions(x map[connor.FilterKey]any, y map[connor.FilterKey]any) bool {
if len(x) != len(y) {
return false
}

for xKey, xValue := range x {
var isFoundInY bool

// Ensure a matching key exists in the other map.
for yKey, yValue := range y {
if !xKey.Equal(yKey) {
continue
}

xValueConditions, xOk := xValue.(map[connor.FilterKey]any)
yValueConditions, yOk := yValue.(map[connor.FilterKey]any)
if xOk && yOk {
if deepEqualConditions(xValueConditions, yValueConditions) {
isFoundInY = true
break
}
} else if !xOk && !yOk {
// Both are some basic values.
if reflect.DeepEqual(xValue, yValue) {
isFoundInY = true
break
}
}
}

// No matching key (including matching data, of the pointer keys) found, so exit early.
if !isFoundInY {
return false
}
}

return true
}

// aggregateRequest is an intermediary struct defining a consumer-requested
Expand Down Expand Up @@ -1137,12 +1182,18 @@ collectionLoop:
//
// If a match is found the matching field will be returned along with true. If a match is not
// found, nil and false will be returned.
func tryGetTarget(name string, filter *Filter, collection []Requestable) (Requestable, bool) {
func tryGetTarget(
name string,
filter *Filter,
limit *Limit,
collection []Requestable,
) (Requestable, bool) {
dummyTarget := Targetable{
Field: Field{
Name: name,
},
Filter: filter,
Limit: limit,
}

for _, field := range collection {
Expand Down
14 changes: 14 additions & 0 deletions query/graphql/mapper/targetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ func (k *PropertyIndex) GetOperatorOrDefault(defaultOp string) string {
return defaultOp
}

func (k *PropertyIndex) Equal(other connor.FilterKey) bool {
if otherKey, isOk := other.(*PropertyIndex); isOk && *k == *otherKey {
return true
}
return false
}

// Operator is a FilterKey that represents a filter operator.
type Operator struct {
// The filter operation string that this Operator represents.
Expand All @@ -54,6 +61,13 @@ func (k *Operator) GetOperatorOrDefault(defaultOp string) string {
return k.Operation
}

func (k *Operator) Equal(other connor.FilterKey) bool {
if otherKey, isOk := other.(*Operator); isOk && *k == *otherKey {
return true
}
return false
}

// Filter represents a series of conditions that may reduce the number of
// records that a query returns.
type Filter struct {
Expand Down
24 changes: 0 additions & 24 deletions tests/integration/query/explain/group_with_average_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,6 @@ func TestExplainGroupByWithAverageOnAnInnerField(t *testing.T) {
},
"groupNode": dataMap{
"childSelects": []dataMap{
{
"collectionName": "author",
"docKeys": nil,
"filter": dataMap{
"age": dataMap{
"_ne": nil,
},
},
"groupBy": nil,
"limit": nil,
"orderBy": nil,
},
{
"collectionName": "author",
"docKeys": nil,
"filter": dataMap{
"age": dataMap{
"_ne": nil,
},
},
"groupBy": nil,
"limit": nil,
"orderBy": nil,
},
{
"collectionName": "author",
"docKeys": nil,
Expand Down
90 changes: 0 additions & 90 deletions tests/integration/query/explain/top_with_average_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,50 +48,6 @@ func TestExplainTopLevelAverageQuery(t *testing.T) {
{
"explain": dataMap{
"topLevelNode": []dataMap{
{
"selectTopNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "3",
"collectionName": "author",
"filter": dataMap{
"age": dataMap{
"_ne": nil,
},
},
"spans": []dataMap{
{
"end": "/4",
"start": "/3",
},
},
},
},
},
},
{
"selectTopNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "3",
"collectionName": "author",
"filter": dataMap{
"age": dataMap{
"_ne": nil,
},
},
"spans": []dataMap{
{
"end": "/4",
"start": "/3",
},
},
},
},
},
},
{
"selectTopNode": dataMap{
"selectNode": dataMap{
Expand Down Expand Up @@ -196,52 +152,6 @@ func TestExplainTopLevelAverageQueryWithFilter(t *testing.T) {
{
"explain": dataMap{
"topLevelNode": []dataMap{
{
"selectTopNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "3",
"collectionName": "author",
"filter": dataMap{
"age": dataMap{
"_gt": int64(26),
"_ne": nil,
},
},
"spans": []dataMap{
{
"end": "/4",
"start": "/3",
},
},
},
},
},
},
{
"selectTopNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "3",
"collectionName": "author",
"filter": dataMap{
"age": dataMap{
"_gt": int64(26),
"_ne": nil,
},
},
"spans": []dataMap{
{
"end": "/4",
"start": "/3",
},
},
},
},
},
},
{
"selectTopNode": dataMap{
"selectNode": dataMap{
Expand Down
Loading