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 has pagination when predicate is queried with @lang #4331

Merged
merged 5 commits into from
Dec 11, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
51 changes: 51 additions & 0 deletions query/query4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,57 @@ func TestHasFirst(t *testing.T) {
}`, js)
}

// This test is not working currently, but start working after
// PR https://github.com/dgraph-io/dgraph/pull/4316 is merged.
// func TestHasFirstLangPredicate(t *testing.T) {
// query := `{
// q(func:has(name@lang), orderasc: name, first:5) {
// name@lang
// }
// }`
// js := processQueryNoErr(t, query)
// require.JSONEq(t, `{
// {
// "data":{
// "q":[
// {
// "name@en":"Alex"
// },
// {
// "name@en":"Amit"
// },
// {
// "name@en":"Andrew"
// },
// {
// "name@en":"Artem Tkachenko"
// },
// {
// "name@en":"European badger"
// }
// ]
// }
// }`, js)
// }

func TestHasCountPredicateWithLang(t *testing.T) {
query := `{
q(func:has(name@en), first: 5) {
count(uid)
}
}`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{
"data":{
"q":[
{
"count":5
}
]
}
}`, js)
}

func TestRegExpVariable(t *testing.T) {
query := `
query {
Expand Down
77 changes: 54 additions & 23 deletions worker/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ func (qs *queryState) helpProcessTask(ctx context.Context, q *pb.Query, gid uint

if srcFn.fnType == hasFn && srcFn.isFuncAtRoot {
span.Annotate(nil, "handleHasFunction")
if err := qs.handleHasFunction(ctx, q, out); err != nil {
if err := qs.handleHasFunction(ctx, q, out, srcFn); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -945,8 +945,9 @@ func (qs *queryState) helpProcessTask(ctx context.Context, q *pb.Query, gid uint
}
}

// For string matching functions, check the language.
if needsStringFiltering(srcFn, q.Langs, attr) {
// For string matching functions, check the language. We are not checking here
// for hasFn as filtering for it has already been done in handleHasFunction.
if srcFn.fnType != hasFn && needsStringFiltering(srcFn, q.Langs, attr) {
span.Annotate(nil, "filterStringFunction")
if err := qs.filterStringFunction(funcArgs{q, gid, srcFn, out}); err != nil {
return nil, err
Expand Down Expand Up @@ -1407,25 +1408,7 @@ func (qs *queryState) filterStringFunction(arg funcArgs) error {
// TODO: This function can be optimized by having a query specific cache, which can be populated
// by the handleHasFunction for e.g. for a `has(name)` query.
for _, uid := range uids.Uids {
key := x.DataKey(attr, uid)
pl, err := qs.cache.Get(key)
if err != nil {
return err
}

var vals []types.Val
var val types.Val
if lang == "" {
if schema.State().IsList(attr) {
vals, err = pl.AllValues(arg.q.ReadTs)
} else {
val, err = pl.Value(arg.q.ReadTs)
vals = append(vals, val)
}
} else {
val, err = pl.ValueForTag(arg.q.ReadTs, lang)
vals = append(vals, val)
}
vals, err := qs.getValsForUID(attr, lang, uid, arg.q.ReadTs)
if err == posting.ErrNoValue {
continue
} else if err != nil {
Expand Down Expand Up @@ -1486,6 +1469,30 @@ func (qs *queryState) filterStringFunction(arg funcArgs) error {
return nil
}

func (qs *queryState) getValsForUID(attr, lang string, uid, ReadTs uint64) ([]types.Val, error) {
key := x.DataKey(attr, uid)
pl, err := qs.cache.Get(key)
if err != nil {
return nil, err
}

var vals []types.Val
var val types.Val
if lang == "" {
if schema.State().IsList(attr) {
vals, err = pl.AllValues(ReadTs)
} else {
val, err = pl.Value(ReadTs)
vals = append(vals, val)
}
} else {
val, err = pl.ValueForTag(ReadTs, lang)
vals = append(vals, val)
}

return vals, err
}

func matchRegex(value types.Val, regex *cregexp.Regexp) bool {
return len(value.Value.(string)) > 0 && regex.MatchString(value.Value.(string), true, true) > 0
}
Expand Down Expand Up @@ -2051,7 +2058,8 @@ func (qs *queryState) evaluate(cp countParams, out *pb.Result) error {
return nil
}

func (qs *queryState) handleHasFunction(ctx context.Context, q *pb.Query, out *pb.Result) error {
func (qs *queryState) handleHasFunction(ctx context.Context, q *pb.Query, out *pb.Result,
srcFn *functionContext) error {
span := otrace.FromContext(ctx)
stop := x.SpanTimer(span, "handleHasFunction")
defer stop()
Expand Down Expand Up @@ -2083,6 +2091,17 @@ func (qs *queryState) handleHasFunction(ctx context.Context, q *pb.Query, out *p
it := txn.NewIterator(itOpt)
defer it.Close()

lang := langForFunc(q.Langs)
needFiltering := needsStringFiltering(srcFn, q.Langs, q.Attr)
checkInclusion := func(uid uint64) error {
if !needFiltering {
return nil
}

_, err := qs.getValsForUID(q.Attr, lang, uid, q.ReadTs)
return err
}

// This function could be switched to the stream.Lists framework, but after the change to use
// BitCompletePosting, the speed here is already pretty fast. The slowdown for @lang predicates
// occurs in filterStringFunction (like has(name) queries).
Expand All @@ -2109,7 +2128,13 @@ func (qs *queryState) handleHasFunction(ctx context.Context, q *pb.Query, out *p
}
if item.UserMeta()&posting.BitCompletePosting > 0 {
// This bit would only be set if there are valid uids in UidPack.
if err := checkInclusion(pk.Uid); err == posting.ErrNoValue {
continue
} else if err != nil {
return err
}
result.Uids = append(result.Uids, pk.Uid)

// We'll stop fetching if we fetch the required count.
if len(result.Uids) >= int(q.First) {
break
Expand All @@ -2125,7 +2150,13 @@ func (qs *queryState) handleHasFunction(ctx context.Context, q *pb.Query, out *p
if empty, err := l.IsEmpty(q.ReadTs, 0); err != nil {
return err
} else if !empty {
if err := checkInclusion(pk.Uid); err == posting.ErrNoValue {
continue
} else if err != nil {
return err
}
result.Uids = append(result.Uids, pk.Uid)

// We'll stop fetching if we fetch the required count.
if len(result.Uids) >= int(q.First) {
break
Expand Down