Skip to content

Commit

Permalink
wip: Find all the names using indexes of all the ordering fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
shahzadlone committed Jul 2, 2022
1 parent 8daa8bd commit 596f6fd
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 61 deletions.
17 changes: 17 additions & 0 deletions core/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Package core provides commonly shared interfaces and building blocks.
*/
package core

import "fmt"

const DocKeyFieldIndex int = 0

type DocFields []interface{}
Expand Down Expand Up @@ -222,3 +224,18 @@ func (m *DocumentMapping) SetChildAt(index int, childMapping DocumentMapping) {
newMappings[index] = childMapping
m.ChildMappings = newMappings
}

// FindNameFromIndex returns the corresponding name of the given index.
//
// Will return error if the index is not found.
func (mapping *DocumentMapping) FindNameFromIndex(targetIndex int) (string, error) {
for name, indexes := range mapping.IndexesByName {
for _, index := range indexes {
if index == targetIndex {
return name, nil
}
}
}

return "", fmt.Errorf("No corresponding name was found for index=%d", targetIndex)
}
7 changes: 7 additions & 0 deletions query/graphql/mapper/targetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ type OrderCondition struct {
Direction SortDirection
}

func (oc OrderCondition) IsEmpty() bool {
if oc.Direction == "" && len(oc.FieldIndexes) == 0 {
return true
}
return false
}

type OrderBy struct {
Conditions []OrderCondition
}
Expand Down
20 changes: 19 additions & 1 deletion query/graphql/planner/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,27 @@ func (n *sortNode) Explain() (map[string]interface{}, error) {
orderings := []map[string]interface{}{}

for _, element := range n.ordering {
// Skip all empty elements.
if element.IsEmpty() {
continue
}

// Build the list containing the corresponding names of all the indexes.
fieldNames := []string{}
for _, fieldIndex := range element.FieldIndexes {
// Try to find the name of this index.
fieldName, err := n.documentMapping.FindNameFromIndex(fieldIndex)
if err != nil {
return nil, err
}

fieldNames = append(fieldNames, fieldName)
}

// Put it all together for this order element.
orderings = append(orderings,
map[string]interface{}{
"field": element.Field,
"fields": fieldNames,
"direction": string(element.Direction),
},
)
Expand Down
124 changes: 64 additions & 60 deletions tests/integration/query/explain/with_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,26 +116,26 @@ func TestExplainQuerySimpleSort(t *testing.T) {
{
"explain": dataMap{
"selectTopNode": dataMap{
"renderNode": dataMap{
"sortNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"filter": nil,
"collectionID": "3",
"collectionName": "author",
"spans": []dataMap{
{
"start": "/3",
"end": "/4",
},
"sortNode": dataMap{
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"filter": nil,
"collectionID": "3",
"collectionName": "author",
"spans": []dataMap{
{
"start": "/3",
"end": "/4",
},
},
},
"orderings": []dataMap{
{
"direction": "ASC",
"field": "age",
},
"orderings": []dataMap{
{
"direction": "ASC",
"fields": []string{
"age",
},
},
},
Expand Down Expand Up @@ -252,57 +252,61 @@ func TestExplainQuerySortAscendingOnParentAndDescendingOnChild(t *testing.T) {
{
"explain": dataMap{
"selectTopNode": dataMap{
"renderNode": dataMap{
"sortNode": dataMap{
"orderings": []dataMap{
{
"direction": "ASC",
"field": "name",
"sortNode": dataMap{
"orderings": []dataMap{
{
"direction": "ASC",
"fields": []string{
"name",
},
{
"direction": "ASC",
"field": "age",
},
{
"direction": "ASC",
"fields": []string{
"age",
},
},
"selectNode": dataMap{
"filter": nil,
"typeIndexJoin": dataMap{
"joinType": "typeJoinMany",
"rootName": "author",
"root": dataMap{
"scanNode": dataMap{
"collectionID": "3",
"collectionName": "author",
"filter": nil,
"spans": []dataMap{
{
"start": "/3",
"end": "/4",
},
},
"selectNode": dataMap{
"filter": nil,
"typeIndexJoin": dataMap{
"joinType": "typeJoinMany",
"rootName": "author",
"root": dataMap{
"scanNode": dataMap{
"collectionID": "3",
"collectionName": "author",
"filter": nil,
"spans": []dataMap{
{
"start": "/3",
"end": "/4",
},
},
},
"subTypeName": "articles",
"subType": dataMap{
"selectTopNode": dataMap{
"sortNode": dataMap{
"orderings": []dataMap{
{
"direction": "DESC",
"field": "name",
},
"subTypeName": "articles",
"subType": dataMap{
"selectTopNode": dataMap{
"sortNode": dataMap{
"orderings": []dataMap{
{
"direction": "DESC",
"fields": []string{
"name",
},
},
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "1",
"collectionName": "article",
"filter": nil,
"spans": []dataMap{
{
"start": "/1",
"end": "/2",
},
},
"selectNode": dataMap{
"filter": nil,
"scanNode": dataMap{
"collectionID": "1",
"collectionName": "article",
"filter": nil,
"spans": []dataMap{
{
"start": "/1",
"end": "/2",
},
},
},
Expand Down

0 comments on commit 596f6fd

Please sign in to comment.