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

RegExp filter work without filter #2913

Merged
merged 17 commits into from
Jan 24, 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
11 changes: 11 additions & 0 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ func (s *state) TokenizerNames(pred string) []string {
return names
}

// HasTokenizer is a convenience func that checks if a given tokenizer is found in pred.
// Returns true if found, else false.
func (s *state) HasTokenizer(id byte, pred string) bool {
for _, t := range s.Tokenizer(pred) {
if t.Identifier() == id {
return false
}
}
return false
}

// IsReversed returns whether the predicate has reverse edge or not
func (s *state) IsReversed(pred string) bool {
s.RLock()
Expand Down
126 changes: 69 additions & 57 deletions worker/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,88 +876,100 @@ func (qs *queryState) handleCompareScalarFunction(arg funcArgs) error {
}

func (qs *queryState) handleRegexFunction(ctx context.Context, arg funcArgs) error {
span := otrace.FromContext(ctx)
stop := x.SpanTimer(span, "handleRegexFunction")
defer stop()
if span != nil {
span.Annotatef(nil, "Number of uids: %d. args.srcFn: %+v", arg.srcFn.n, arg.srcFn)
}

attr := arg.q.Attr
typ, err := schema.State().TypeOf(attr)
span.Annotatef(nil, "Attr: %s. Type: %s", attr, typ.Name())
if err != nil || !typ.IsScalar() {
return x.Errorf("Attribute not scalar: %s %v", attr, typ)
}
if typ != types.StringID {
return x.Errorf("Got non-string type. Regex match is allowed only on string type.")
}
tokenizers := schema.State().TokenizerNames(attr)
var found bool
for _, t := range tokenizers {
if t == "trigram" { // TODO(tzdybal) - maybe just rename to 'regex' tokenizer?
found = true
}
}
if !found {
return x.Errorf("Attribute %v does not have trigram index for regex matching.", attr)
}
useIndex := schema.State().HasTokenizer(tok.IdentTrigram, attr)
span.Annotatef(nil, "Trigram index found: %t", useIndex)

query := cindex.RegexpQuery(arg.srcFn.regex.Syntax)
empty := pb.List{}
uids, err := uidsForRegex(attr, arg, query, &empty)
uids := &pb.List{}
if useIndex {
uids, err = uidsForRegex(attr, arg, query, &empty)
if err != nil {
return err
}
} else {
out := &pb.Result{}
err = qs.handleHasFunction(ctx, arg.q, out)
if err != nil {
return err
}
uids = algo.MergeSorted(out.UidMatrix)
}
span.Annotatef(nil, "Number of uids to match: %d", len(uids.Uids))
arg.out.UidMatrix = append(arg.out.UidMatrix, uids)

isList := schema.State().IsList(attr)
lang := langForFunc(arg.q.Langs)
if uids != nil {
arg.out.UidMatrix = append(arg.out.UidMatrix, uids)

filtered := &pb.List{}
for _, uid := range uids.Uids {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
pl, err := qs.cache.Get(x.DataKey(attr, uid))
if err != nil {
return err
}

var val types.Val
if lang != "" {
val, err = pl.ValueForTag(arg.q.ReadTs, lang)
} else if isList {
vals, err := pl.AllUntaggedValues(arg.q.ReadTs)
if err == posting.ErrNoValue {
continue
} else if err != nil {
return err
}
for _, val := range vals {
// convert data from binary to appropriate format
strVal, err := types.Convert(val, types.StringID)
if err == nil && matchRegex(strVal, arg.srcFn.regex) {
filtered.Uids = append(filtered.Uids, uid)
break
}
}

continue
} else {
val, err = pl.Value(arg.q.ReadTs)
}
filtered := &pb.List{}
for _, uid := range uids.Uids {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
pl, err := qs.cache.Get(x.DataKey(attr, uid))
if err != nil {
return err
}

var val types.Val
if lang != "" {
val, err = pl.ValueForTag(arg.q.ReadTs, lang)
} else if isList {
vals, err := pl.AllUntaggedValues(arg.q.ReadTs)
if err == posting.ErrNoValue {
continue
} else if err != nil {
return err
}

// convert data from binary to appropriate format
strVal, err := types.Convert(val, types.StringID)
if err == nil && matchRegex(strVal, arg.srcFn.regex) {
filtered.Uids = append(filtered.Uids, uid)
for _, val := range vals {
// convert data from binary to appropriate format
strVal, err := types.Convert(val, types.StringID)
if err == nil && matchRegex(strVal, arg.srcFn.regex) {
filtered.Uids = append(filtered.Uids, uid)
break
}
}

continue
} else {
val, err = pl.Value(arg.q.ReadTs)
}

if err == posting.ErrNoValue {
continue
} else if err != nil {
return err
}

for i := 0; i < len(arg.out.UidMatrix); i++ {
algo.IntersectWith(arg.out.UidMatrix[i], filtered, arg.out.UidMatrix[i])
// convert data from binary to appropriate format
strVal, err := types.Convert(val, types.StringID)
if err == nil && matchRegex(strVal, arg.srcFn.regex) {
filtered.Uids = append(filtered.Uids, uid)
}
} else {
return err
}

for i := 0; i < len(arg.out.UidMatrix); i++ {
algo.IntersectWith(arg.out.UidMatrix[i], filtered, arg.out.UidMatrix[i])
}

return nil
}

Expand Down