Skip to content

Commit 845dc6a

Browse files
author
Pawan Rawal
committed
Fix panic in expand(_all_) on empty database.
1 parent a947a11 commit 845dc6a

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

query/query.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,14 +1695,21 @@ func (sg *SubGraph) appendDummyValues() {
16951695
}
16961696
}
16971697

1698-
func uniquePreds(vl []*intern.ValueList) map[string]struct{} {
1699-
preds := make(map[string]struct{})
1698+
func uniquePreds(vl []*intern.ValueList) []string {
1699+
predMap := make(map[string]struct{})
17001700

17011701
for _, l := range vl {
17021702
for _, v := range l.Values {
1703-
preds[string(v.Val)] = struct{}{}
1703+
if len(v.Val) > 0 {
1704+
predMap[string(v.Val)] = struct{}{}
1705+
}
17041706
}
17051707
}
1708+
1709+
preds := make([]string, 0, len(predMap))
1710+
for pred := range predMap {
1711+
preds = append(preds, pred)
1712+
}
17061713
return preds
17071714
}
17081715

@@ -1746,23 +1753,27 @@ func expandSubgraph(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
17461753
x.Errorf("Cannot run expand() query when ExpandEdge(--expand_edge) is false.")
17471754
}
17481755

1756+
var preds []string
17491757
// It could be expand(_all_) or expand(val(x)).
17501758
if child.Params.Expand == "_all_" {
1751-
// Get the predicate list for expansion. Otherwise we already
1752-
// have the list populated from the var.
1759+
// Get the predicate list for expansion.
17531760
child.ExpandPreds, err = getNodePredicates(ctx, sg)
17541761
if err != nil {
17551762
return out, err
17561763
}
1764+
preds = uniquePreds(child.ExpandPreds)
1765+
17571766
rpreds, err := getReversePredicates(ctx)
17581767
if err != nil {
17591768
return out, err
17601769
}
1761-
child.ExpandPreds[0].Values = append(child.ExpandPreds[0].Values, rpreds...)
1770+
preds = append(preds, rpreds...)
1771+
} else {
1772+
// We already have the predicates populated from the var.
1773+
preds = uniquePreds(child.ExpandPreds)
17621774
}
17631775

1764-
up := uniquePreds(child.ExpandPreds)
1765-
for pred, _ := range up {
1776+
for _, pred := range preds {
17661777
temp := &SubGraph{
17671778
ReadTs: sg.ReadTs,
17681779
LinRead: sg.LinRead,
@@ -1795,23 +1806,20 @@ func expandSubgraph(ctx context.Context, sg *SubGraph) ([]*SubGraph, error) {
17951806
return out, nil
17961807
}
17971808

1798-
func getReversePredicates(ctx context.Context) ([]*intern.TaskValue, error) {
1809+
func getReversePredicates(ctx context.Context) ([]string, error) {
17991810
schs, err := worker.GetSchemaOverNetwork(ctx, &intern.SchemaRequest{})
18001811
if err != nil {
18011812
return nil, err
18021813
}
1803-
var preds []*intern.TaskValue
1814+
preds := make([]string, 0, len(schs))
18041815
for _, sch := range schs {
18051816
if !sch.Reverse {
18061817
continue
18071818
}
18081819
pred := make([]byte, 1+len(sch.Predicate))
18091820
pred[0] = '~'
18101821
copy(pred[1:], sch.Predicate)
1811-
preds = append(preds, &intern.TaskValue{
1812-
Val: pred,
1813-
ValType: intern.Posting_DEFAULT,
1814-
})
1822+
preds = append(preds, string(pred))
18151823
}
18161824
return preds, nil
18171825
}

query/query_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7781,3 +7781,17 @@ func TestUidWithoutDebug2(t *testing.T) {
77817781
js := processToFastJsonNoErr(t, query)
77827782
require.JSONEq(t, `{"data":{"q":[{"uid":"0x1","friend":[{"uid":"0x17"},{"uid":"0x18"},{"uid":"0x19"},{"uid":"0x1f"},{"uid":"0x65"}]}]}}`, js)
77837783
}
7784+
7785+
func TestExpandAll_empty_panic(t *testing.T) {
7786+
populateGraph(t)
7787+
7788+
query := `
7789+
{
7790+
me(func: uid(0x01)) @filter(eq(name,"foobar")){
7791+
expand(_all_)
7792+
}
7793+
}
7794+
`
7795+
js := processToFastJsonNoErr(t, query)
7796+
require.JSONEq(t, `{"data":{"me":[]}}`, js)
7797+
}

0 commit comments

Comments
 (0)