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 expand(_all_) in recurse queries #2600

Merged
merged 2 commits into from
Sep 29, 2018
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
11 changes: 11 additions & 0 deletions query/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@ office.room : uid .
addPassword(t, 1, "password", "123456")
addPassword(t, 23, "pass", "654321")

addEdgeToUID(t, "school", 32, 33, nil)
addEdgeToUID(t, "district", 33, 34, nil)
addEdgeToUID(t, "county", 34, 35, nil)
addEdgeToUID(t, "state", 35, 36, nil)

addEdgeToValue(t, "name", 33, "San Mateo High School", nil)
addEdgeToValue(t, "name", 34, "San Mateo School District", nil)
addEdgeToValue(t, "name", 35, "San Mateo County", nil)
addEdgeToValue(t, "name", 36, "California", nil)
addEdgeToValue(t, "abbr", 36, "CA", nil)

// So, user we're interested in has uid: 1.
// She has 5 friends: 23, 24, 25, 31, and 101
addEdgeToUID(t, "friend", 1, 23, nil)
Expand Down
28 changes: 28 additions & 0 deletions query/query3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ func TestRecurseQuery(t *testing.T) {
`{"data": {"me":[{"name":"Michonne", "friend":[{"name":"Rick Grimes", "friend":[{"name":"Michonne"}]},{"name":"Glenn Rhee"},{"name":"Daryl Dixon"},{"name":"Andrea", "friend":[{"name":"Glenn Rhee"}]}]}]}}`, js)
}

func TestRecurseExpand(t *testing.T) {

query := `
{
me(func: uid(32)) @recurse {
expand(_all_)
}
}`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t, `{"data":{"me":[{"school":[{"name":"San Mateo High School","district":[{"name":"San Mateo School District","county":[{"state":[{"name":"California","abbr":"CA"}],"name":"San Mateo County"}]}]}]}]}}`, js)
}

func TestRecurseExpandRepeatedPredError(t *testing.T) {

query := `
{
me(func: uid(32)) @recurse {
name
expand(_all_)
}
}`

ctx := defaultContext()
_, err := processToFastJsonCtxVars(t, query, ctx, nil)
require.Error(t, err)
require.Contains(t, err.Error(), "Repeated subgraph: [name] while using expand()")
}

func TestRecurseQueryOrder(t *testing.T) {

query := `
Expand Down
52 changes: 32 additions & 20 deletions query/recurse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package query

import (
"context"
"errors"
"fmt"
"math"

Expand Down Expand Up @@ -49,22 +50,11 @@ func (start *SubGraph) expandRecurse(ctx context.Context, maxDepth uint64) error
return ctx.Err()
}

// Add children back so that expandSubgraph can expand them if needed.
start.Children = append(start.Children, startChildren...)
if startChildren, err = expandSubgraph(ctx, start); err != nil {
// Add children back and expand if necessary
if exec, err = expandChildren(ctx, start, startChildren); err != nil {
return err
}

start.Children = start.Children[:0]
for _, child := range startChildren {
temp := new(SubGraph)
temp.copyFiltersRecurse(child)
temp.SrcUIDs = start.DestUIDs
temp.Params.Var = child.Params.Var
exec = append(exec, temp)
start.Children = append(start.Children, temp)
}

dummy := &SubGraph{}
var depth uint64
for {
Expand Down Expand Up @@ -140,18 +130,15 @@ func (start *SubGraph) expandRecurse(ctx context.Context, maxDepth uint64) error

// modify the exec and attach child nodes.
var out []*SubGraph
var exp []*SubGraph
for _, sg := range exec {
if len(sg.DestUIDs.Uids) == 0 {
continue
}
for _, child := range startChildren {
temp := new(SubGraph)
temp.copyFiltersRecurse(child)
temp.SrcUIDs = sg.DestUIDs
temp.Params.Var = child.Params.Var
sg.Children = append(sg.Children, temp)
out = append(out, temp)
if exp, err = expandChildren(ctx, sg, startChildren); err != nil {
return err
}
out = append(out, exp...)
}

if numEdges > x.Config.QueryEdgeLimit {
Expand All @@ -166,6 +153,31 @@ func (start *SubGraph) expandRecurse(ctx context.Context, maxDepth uint64) error
}
}

// expandChildren adds child nodes to a SubGraph with no children, expanding them if necessary.
func expandChildren(ctx context.Context, sg *SubGraph, children []*SubGraph) ([]*SubGraph, error) {
if len(sg.Children) > 0 {
return nil, errors.New("Subgraph should not have any children")
}
// Add children and expand if necessary
sg.Children = append(sg.Children, children...)
expandedChildren, err := expandSubgraph(ctx, sg)
if err != nil {
return nil, err
}
out := make([]*SubGraph, 0, len(expandedChildren))
sg.Children = sg.Children[:0]
// Link new child nodes back to parent destination UIDs
for _, child := range expandedChildren {
newChild := new(SubGraph)
newChild.copyFiltersRecurse(child)
newChild.SrcUIDs = sg.DestUIDs
newChild.Params.Var = child.Params.Var
sg.Children = append(sg.Children, newChild)
out = append(out, newChild)
}
return out, nil
}

func Recurse(ctx context.Context, sg *SubGraph) error {
if !sg.Params.Recurse {
return x.Errorf("Invalid recurse path query")
Expand Down