Skip to content

Commit

Permalink
Output non-list uid predicates as a map. (#2921)
Browse files Browse the repository at this point in the history
Currently these predicates are outputted as a list, despite the fact
that only one value is ever allowed. This PR changes the query code so
that these predicates are outputted to json as a map.
  • Loading branch information
martinmr authored Jan 22, 2019
1 parent 2129101 commit 025ae34
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
4 changes: 2 additions & 2 deletions contrib/integration/testtxn/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ func TestSPStar(t *testing.T) {
require.NoError(t, s.dg.Alter(context.Background(), op))

op = &api.Operation{}
op.Schema = `friend: uid .`
op.Schema = `friend: [uid] .`
require.NoError(t, s.dg.Alter(context.Background(), op))

txn := s.dg.NewTxn()
Expand Down Expand Up @@ -620,7 +620,7 @@ func TestSPStar2(t *testing.T) {
require.NoError(t, s.dg.Alter(context.Background(), op))

op = &api.Operation{}
op.Schema = `friend: uid .`
op.Schema = `friend: [uid] .`
require.NoError(t, s.dg.Alter(context.Background(), op))

// Add edge
Expand Down
3 changes: 3 additions & 0 deletions query/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ password : password .
symbol : string @index(exact) .
room : string @index(term) .
office.room : [uid] .
best_friend : uid .
`

err := schema.ParseBytes([]byte(schemaStr), 1)
Expand Down Expand Up @@ -359,6 +360,8 @@ office.room : [uid] .
addEdgeToUID(t, "friend", 31, 24, nil)
addEdgeToUID(t, "friend", 23, 1, nil)

addEdgeToUID(t, "best_friend", 2, 64, nil)

addEdgeToUID(t, "school", 1, 5000, nil)
addEdgeToUID(t, "school", 23, 5001, nil)
addEdgeToUID(t, "school", 24, 5000, nil)
Expand Down
6 changes: 5 additions & 1 deletion query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ func (sg *SubGraph) preTraverse(uid uint64, dst outputNode) error {
if sg.Params.GetUid {
uc.SetUID(childUID, "uid")
}
dst.AddListChild(fieldName, uc)
if pc.List {
dst.AddListChild(fieldName, uc)
} else {
dst.AddMapChild(fieldName, uc, false)
}
}
}
if pc.Params.uidCount && !(pc.Params.uidCountAlias == "" && pc.Params.Normalize) {
Expand Down
17 changes: 17 additions & 0 deletions query/query0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,23 @@ func TestFindFriendsWhoAreBetween15And19(t *testing.T) {
js)
}

func TestGetNonListUidPredicate(t *testing.T) {
query := `
{
me(func: uid(0x02)) {
uid
best_friend {
uid
}
}
}
`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data": {"me":[{"uid":"0x2", "best_friend": {"uid": "0x40"}}]}}`,
js)
}

func TestGeAge(t *testing.T) {
query := `{
senior_citizens(func: ge(age, 75)) {
Expand Down
18 changes: 9 additions & 9 deletions query/query3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func TestKShortestPathWeighted(t *testing.T) {
// We only get one path in this case as the facet is present only in one path.
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data":{"_path_":[{"uid":"0x1","path":[{"uid":"0x1f","path":[{"uid":"0x3e8","path":[{"uid":"0x3e9","path|weight":0.100000}],"path|weight":0.100000}],"path|weight":0.100000}]}]}}`,
`{"data":{"_path_":[{"uid":"0x1","path":{"uid":"0x1f","path":{"uid":"0x3e8","path":{"uid":"0x3e9","path|weight":0.100000},"path|weight":0.100000},"path|weight":0.100000}}]}}`,
js)
}

Expand Down Expand Up @@ -334,7 +334,7 @@ func TestKShortestPathWeighted1(t *testing.T) {
}`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data":{"_path_":[{"uid":"0x1","path":[{"uid":"0x1f","path":[{"uid":"0x3e8","path":[{"uid":"0x3e9","path":[{"uid":"0x3ea","path":[{"uid":"0x3eb","path|weight":0.600000}],"path|weight":0.100000}],"path|weight":0.100000}],"path|weight":0.100000}],"path|weight":0.100000}]},{"uid":"0x1","path":[{"uid":"0x1f","path":[{"uid":"0x3e8","path":[{"uid":"0x3ea","path":[{"uid":"0x3eb","path|weight":0.600000}],"path|weight":0.700000}],"path|weight":0.100000}],"path|weight":0.100000}]},{"uid":"0x1","path":[{"uid":"0x1f","path":[{"uid":"0x3e8","path":[{"uid":"0x3e9","path":[{"uid":"0x3eb","path|weight":1.500000}],"path|weight":0.100000}],"path|weight":0.100000}],"path|weight":0.100000}]}]}}`,
`{"data":{"_path_":[{"uid":"0x1","path":{"uid":"0x1f","path":{"uid":"0x3e8","path":{"uid":"0x3e9","path":{"uid":"0x3ea","path":{"uid":"0x3eb","path|weight":0.600000},"path|weight":0.100000},"path|weight":0.100000},"path|weight":0.100000},"path|weight":0.100000}},{"uid":"0x1","path":{"uid":"0x1f","path":{"uid":"0x3e8","path":{"uid":"0x3ea","path":{"uid":"0x3eb","path|weight":0.600000},"path|weight":0.700000},"path|weight":0.100000},"path|weight":0.100000}},{"uid":"0x1","path":{"uid":"0x1f","path":{"uid":"0x3e8","path":{"uid":"0x3e9","path":{"uid":"0x3eb","path|weight":1.500000},"path|weight":0.100000},"path|weight":0.100000},"path|weight":0.100000}}]}}`,
js)
}

Expand All @@ -352,7 +352,7 @@ func TestTwoShortestPath(t *testing.T) {
}`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data": {"_path_":[{"uid":"0x1","path":[{"uid":"0x1f","path":[{"uid":"0x3e8","path":[{"uid":"0x3ea"}]}]}]},{"uid":"0x1","path":[{"uid":"0x1f","path":[{"uid":"0x3e8","path":[{"uid":"0x3e9","path":[{"uid":"0x3ea"}]}]}]}]}],"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Alice"},{"name":"Matt"}]}}`,
`{"data": {"_path_":[{"uid":"0x1","path":{"uid":"0x1f","path":{"uid":"0x3e8","path":{"uid":"0x3ea"}}}},{"uid":"0x1","path":{"uid":"0x1f","path":{"uid":"0x3e8","path":{"uid":"0x3e9","path":{"uid":"0x3ea"}}}}}],"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Alice"},{"name":"Matt"}]}}`,
js)
}

Expand All @@ -370,7 +370,7 @@ func TestShortestPath(t *testing.T) {
}`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data": {"_path_":[{"uid":"0x1","friend":[{"uid":"0x1f"}]}],"me":[{"name":"Michonne"},{"name":"Andrea"}]}}`,
`{"data": {"_path_":[{"uid":"0x1","friend":{"uid":"0x1f"}}],"me":[{"name":"Michonne"},{"name":"Andrea"}]}}`,
js)
}

Expand All @@ -388,7 +388,7 @@ func TestShortestPathRev(t *testing.T) {
}`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data": {"_path_":[{"uid":"0x17","friend":[{"uid":"0x1"}]}],"me":[{"name":"Rick Grimes"},{"name":"Michonne"}]}}`,
`{"data": {"_path_":[{"uid":"0x17","friend":{"uid":"0x1"}}],"me":[{"name":"Rick Grimes"},{"name":"Michonne"}]}}`,
js)
}

Expand Down Expand Up @@ -461,7 +461,7 @@ func TestShortestPathWeights(t *testing.T) {
}`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data":{"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Alice"},{"name":"Bob"},{"name":"Matt"}],"_path_":[{"uid":"0x1","path":[{"uid":"0x1f","path":[{"uid":"0x3e8","path":[{"uid":"0x3e9","path":[{"uid":"0x3ea","path|weight":0.100000}],"path|weight":0.100000}],"path|weight":0.100000}],"path|weight":0.100000}]}]}}`,
`{"data":{"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Alice"},{"name":"Bob"},{"name":"Matt"}],"_path_":[{"uid":"0x1","path":{"uid":"0x1f","path":{"uid":"0x3e8","path":{"uid":"0x3e9","path":{"uid":"0x3ea","path|weight":0.100000},"path|weight":0.100000},"path|weight":0.100000},"path|weight":0.100000}}]}}`,
js)
}

Expand All @@ -479,7 +479,7 @@ func TestShortestPath2(t *testing.T) {
}`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data": {"_path_":[{"uid":"0x1","path":[{"uid":"0x1f","path":[{"uid":"0x3e8"}]}]}],"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Alice"}]}}
`{"data": {"_path_":[{"uid":"0x1","path":{"uid":"0x1f","path":{"uid":"0x3e8"}}}],"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Alice"}]}}
`,
js)
}
Expand All @@ -499,7 +499,7 @@ func TestShortestPath4(t *testing.T) {
}`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data": {"_path_":[{"uid":"0x1","follow":[{"uid":"0x1f","follow":[{"uid":"0x3e9","follow":[{"uid":"0x3eb"}]}]}]}],"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Bob"},{"name":"John"}]}}`,
`{"data": {"_path_":[{"uid":"0x1","follow":{"uid":"0x1f","follow":{"uid":"0x3e9","follow":{"uid":"0x3eb"}}}}],"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Bob"},{"name":"John"}]}}`,
js)
}

Expand All @@ -518,7 +518,7 @@ func TestShortestPath_filter(t *testing.T) {
}`
js := processToFastJsonNoErr(t, query)
require.JSONEq(t,
`{"data": {"_path_":[{"uid":"0x1","follow":[{"uid":"0x1f","follow":[{"uid":"0x3e9","path":[{"uid":"0x3ea"}]}]}]}],"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Bob"},{"name":"Matt"}]}}`,
`{"data": {"_path_":[{"uid":"0x1","follow":{"uid":"0x1f","follow":{"uid":"0x3e9","path":{"uid":"0x3ea"}}}}],"me":[{"name":"Michonne"},{"name":"Andrea"},{"name":"Bob"},{"name":"Matt"}]}}`,
js)
}

Expand Down
2 changes: 1 addition & 1 deletion systest/mutations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func NQuadMutationTest(t *testing.T, c *dgo.Dgraph) {

func DeleteAllReverseIndex(t *testing.T, c *dgo.Dgraph) {
ctx := context.Background()
require.NoError(t, c.Alter(ctx, &api.Operation{Schema: "link: uid @reverse ."}))
require.NoError(t, c.Alter(ctx, &api.Operation{Schema: "link: [uid] @reverse ."}))
assignedIds, err := c.NewTxn().Mutate(ctx, &api.Mutation{
CommitNow: true,
SetNquads: []byte("_:a <link> _:b ."),
Expand Down
2 changes: 1 addition & 1 deletion systest/queries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func MultipleBlockEval(t *testing.T, c *dgo.Dgraph) {
require.NoError(t, c.Alter(ctx, &api.Operation{
Schema: `
entity: string @index(exact) .
stock: uid @reverse .
stock: [uid] @reverse .
`,
}))

Expand Down

0 comments on commit 025ae34

Please sign in to comment.