Skip to content

Commit 901cb25

Browse files
committed
Bug Fix: Has function should consider reverses
Fix a bug caused by #2585, where I mistook reverse to mean reverse iteration, when it actually meant reverse index. Fixed it to its original meaning.
1 parent 6123d25 commit 901cb25

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

systest/mutations_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func TestSystem(t *testing.T) {
7777
t.Run("graphql var child", wrap(GraphQLVarChild))
7878
t.Run("math ge", wrap(MathGe))
7979
t.Run("has should not have deleted edge", wrap(HasDeletedEdge))
80+
t.Run("has should have reverse edges", wrap(HasReverseEdge))
8081
}
8182

8283
func ExpandAllLangTest(t *testing.T, c *dgo.Dgraph) {
@@ -1523,3 +1524,55 @@ func HasDeletedEdge(t *testing.T, c *dgo.Dgraph) {
15231524
require.Contains(t, ids, uid)
15241525
}
15251526
}
1527+
1528+
func HasReverseEdge(t *testing.T, c *dgo.Dgraph) {
1529+
ctx := context.Background()
1530+
1531+
check(t, c.Alter(ctx, &api.Operation{
1532+
Schema: `
1533+
follow: uid @reverse .
1534+
`,
1535+
}))
1536+
txn := c.NewTxn()
1537+
defer txn.Discard(ctx)
1538+
1539+
_, err := txn.Mutate(ctx, &api.Mutation{
1540+
CommitNow: true,
1541+
SetNquads: []byte(`
1542+
_:alice <name> "alice" .
1543+
_:bob <name> "bob" .
1544+
_:carol <name> "carol" .
1545+
_:alice <follow> _:carol .
1546+
_:bob <follow> _:carol .
1547+
`),
1548+
})
1549+
check(t, err)
1550+
1551+
type F struct {
1552+
Name string `json:"name"`
1553+
}
1554+
1555+
txn = c.NewTxn()
1556+
defer txn.Discard(ctx)
1557+
resp, err := txn.Query(ctx, `{
1558+
fwd(func: has(follow)) { name }
1559+
rev(func: has(~follow)) { name }
1560+
}`)
1561+
check(t, err)
1562+
1563+
t.Logf("resp: %s\n", resp.GetJson())
1564+
m := make(map[string][]F)
1565+
err = json.Unmarshal(resp.GetJson(), &m)
1566+
check(t, err)
1567+
1568+
fwds := m["fwd"]
1569+
revs := m["rev"]
1570+
1571+
require.Equal(t, len(fwds), 2)
1572+
require.Contains(t, []string{"alice", "bob"}, fwds[0].Name)
1573+
require.Contains(t, []string{"alice", "bob"}, fwds[1].Name)
1574+
require.NotEqual(t, fwds[0].Name, fwds[1].Name)
1575+
1576+
require.Equal(t, len(revs), 1)
1577+
require.Equal(t, revs[0].Name, "carol")
1578+
}

worker/task.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import (
3939
"github.com/dgraph-io/dgraph/types/facets"
4040
"github.com/dgraph-io/dgraph/x"
4141

42-
"github.com/golang/glog"
4342
cindex "github.com/google/codesearch/index"
4443
cregexp "github.com/google/codesearch/regexp"
4544
"golang.org/x/net/context"
@@ -1630,7 +1629,10 @@ func handleHasFunction(ctx context.Context, q *pb.Query, out *pb.Result) error {
16301629
startKey := x.DataKey(q.Attr, q.AfterUid+1)
16311630
prefix := initKey.DataPrefix()
16321631
if q.Reverse {
1633-
glog.Warningln("has function does not handle reverse iteration.")
1632+
// Reverse does not mean reverse iteration. It means we're looking for
1633+
// the reverse index.
1634+
startKey = x.ReverseKey(q.Attr, q.AfterUid+1)
1635+
prefix = initKey.ReversePrefix()
16341636
}
16351637

16361638
result := &pb.List{}

0 commit comments

Comments
 (0)