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(query): fix has function in filter #9043

Merged
merged 1 commit into from
Mar 13, 2024
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
46 changes: 46 additions & 0 deletions query/query0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,52 @@ func TestGetUID(t *testing.T) {
js)
}

func TestFilterHas(t *testing.T) {
// Query untagged values
query := `
{
me(func: has(alias)) @filter(has(alias_lang)) {
uid
}
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data":{"me":[]}}`, js)

// Query all tagged values
query = `
{
me(func: has(alias)) @filter(has(alias_lang@.)) {
alias_lang@.
}
}
`
js = processQueryNoErr(t, query)
require.JSONEq(t, `{"data":{"me":[{"alias_lang@.":"Zambo Alice"},{"alias_lang@.":"John Alice"},{"alias_lang@.":"Bob Joe"},{"alias_lang@.":"Allan Matt"},{"alias_lang@.":"John Oliver"}]}}`, js)

// All tagged values in root function
query = `
{
me(func: has(lossy@.)){
lossy@.
}
}
`
js = processQueryNoErr(t, query)
require.JSONEq(t, `{"data":{"me":[{"lossy@.":"Badger"},{"lossy@.":"Honey badger"}]}}`, js)

// Query specific language
query = `
{
me(func: has(lossy@.)) @filter(has(lossy@fr)) {
lossy@fr
}
}
`
js = processQueryNoErr(t, query)
require.JSONEq(t, `{"data":{"me":[{"lossy@fr":"Blaireau européen"}]}}`, js)
}

func TestQueryEmptyDefaultNames(t *testing.T) {
query := `{
people(func: eq(name, "")) {
Expand Down
31 changes: 30 additions & 1 deletion worker/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,10 @@ func (qs *queryState) handleUidPostings(
x.AssertTrue(width > 0)
span.Annotatef(nil, "Width: %d. NumGo: %d", width, numGo)

lang := langForFunc(q.Langs)
needFiltering := needsStringFiltering(srcFn, q.Langs, q.Attr)
isList := schema.State().IsList(q.Attr)

errCh := make(chan error, numGo)
outputs := make([]*pb.Result, numGo)

Expand Down Expand Up @@ -784,7 +788,32 @@ func (qs *queryState) handleUidPostings(
if i == 0 {
span.Annotate(nil, "HasFn")
}
empty, err := pl.IsEmpty(args.q.ReadTs, 0)
// We figure out if need to filter on bases of lang attribute or not.
// If we don't need to do so, we can just check if the posting list
// is empty. If we need to filter on basis of lang, we need to check
// the value with its tag. if lang == "", in that case, we need to
// return if there are any untagged values. If lang != "", in that
// case we need to check exact value.
empty := false
var err error
if !needFiltering {
empty, err = pl.IsEmpty(args.q.ReadTs, 0)
} else {
if lang == "" {
if isList {
_, err = pl.AllValues(args.q.ReadTs)
} else {
_, err = pl.Value(args.q.ReadTs)
}
} else {
_, err = pl.ValueForTag(args.q.ReadTs, lang)
}

if err == posting.ErrNoValue {
empty = true
err = nil
}
}
if err != nil {
return err
}
Expand Down
Loading