From 4cd48db304e1ab9fa77585d88c455516c4e1d7c5 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Tue, 15 Apr 2025 16:22:02 +0530 Subject: [PATCH 1/3] perf(core): improve performance for ineq filters when uidlist is small --- worker/task.go | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/worker/task.go b/worker/task.go index cb34531e570..15ad9cf4c22 100644 --- a/worker/task.go +++ b/worker/task.go @@ -1819,17 +1819,17 @@ func langForFunc(langs []string) string { return langs[0] } -func planForEqFilter(fc *functionContext, pred string, uidlist []uint64) { - checkUidEmpty := func(uids []uint64) bool { - for _, i := range uids { - if i == 0 { - return false - } +func checkUidEmpty(uids []uint64) bool { + for _, i := range uids { + if i == 0 { + return true } - return true } + return false +} - if !checkUidEmpty(uidlist) { +func planForEqFilter(fc *functionContext, pred string, uidlist []uint64) { + if checkUidEmpty(uidlist) { // We have a uid which has 0 in it. Mostly it would happen when there is only 0. But any one item // being 0 could cause the query planner to fail. In case of 0 being present, we neeed to query the // index itself. @@ -1912,6 +1912,14 @@ func parseSrcFn(ctx context.Context, q *pb.Query) (*functionContext, error) { } } + generateIneqTokens := true + if fc.fname != eq && uint64(len(q.UidList.Uids)) < Config.TypeFilterUidLimit { + if !checkUidEmpty(q.UidList.Uids) { + fc.n = len(q.UidList.Uids) + generateIneqTokens = false + } + } + var tokens []string var ineqValues []types.Val // eq can have multiple args. @@ -1947,6 +1955,9 @@ func parseSrcFn(ctx context.Context, q *pb.Query) (*functionContext, error) { lang = q.Langs[0] } + if !generateIneqTokens { + continue + } // Get tokens ge/le ineqValueToken. if tokens, fc.ineqValueToken, err = getInequalityTokens(ctx, q.ReadTs, attr, f, lang, ineqValues); err != nil { From 0e01972d7ed787fc039588239576a17918a7cef4 Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Thu, 17 Apr 2025 18:27:17 +0530 Subject: [PATCH 2/3] fixed comments and tests --- worker/task.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/worker/task.go b/worker/task.go index 15ad9cf4c22..8f3ffa1f49d 100644 --- a/worker/task.go +++ b/worker/task.go @@ -1819,9 +1819,9 @@ func langForFunc(langs []string) string { return langs[0] } -func checkUidEmpty(uids []uint64) bool { - for _, i := range uids { - if i == 0 { +func checkUidZero(uids []uint64) bool { + for _, uid := range uids { + if uid == 0 { return true } } @@ -1829,7 +1829,7 @@ func checkUidEmpty(uids []uint64) bool { } func planForEqFilter(fc *functionContext, pred string, uidlist []uint64) { - if checkUidEmpty(uidlist) { + if checkUidZero(uidlist) { // We have a uid which has 0 in it. Mostly it would happen when there is only 0. But any one item // being 0 could cause the query planner to fail. In case of 0 being present, we neeed to query the // index itself. @@ -1913,8 +1913,8 @@ func parseSrcFn(ctx context.Context, q *pb.Query) (*functionContext, error) { } generateIneqTokens := true - if fc.fname != eq && uint64(len(q.UidList.Uids)) < Config.TypeFilterUidLimit { - if !checkUidEmpty(q.UidList.Uids) { + if fc.fname != eq && q.UidList != nil && uint64(len(q.UidList.Uids)) < Config.TypeFilterUidLimit { + if !checkUidZero(q.UidList.Uids) { fc.n = len(q.UidList.Uids) generateIneqTokens = false } From 6c05d9bfd95e4c2c700f2ea01dc8033771f44a1d Mon Sep 17 00:00:00 2001 From: Harshil Goel Date: Wed, 23 Apr 2025 12:04:35 +0530 Subject: [PATCH 3/3] fixed a test --- worker/task.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/worker/task.go b/worker/task.go index 8f3ffa1f49d..87fb1a9a40a 100644 --- a/worker/task.go +++ b/worker/task.go @@ -1969,6 +1969,10 @@ func parseSrcFn(ctx context.Context, q *pb.Query) (*functionContext, error) { fc.tokens = append(fc.tokens, tokens...) } + if !generateIneqTokens { + return fc, nil + } + // In case of non-indexed predicate, there won't be any tokens. We will fetch value // from data keys. // If number of index keys is more than no. of uids to filter, so its better to fetch values