Skip to content

feature: Use has instead of arrayExists where possible #311

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions autocomplete/autocomplete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestHandler_ServeValues(t *testing.T) {
fromDate, untilDate := dateString(h.config.ClickHouse.TaggedAutocompleDays, now)

srv.AddResponce(
"SELECT substr(arrayJoin(Tags), 6) AS value FROM graphite_tagged WHERE (((Tag1='environment=production') AND (arrayExists((x) -> x='project=web', Tags))) AND (arrayJoin(Tags) LIKE 'host=%')) AND "+
"SELECT substr(arrayJoin(Tags), 6) AS value FROM graphite_tagged WHERE (((Tag1='environment=production') AND (has(Tags, 'project=web'))) AND (arrayJoin(Tags) LIKE 'host=%')) AND "+
"(Date >= '"+fromDate+"' AND Date <= '"+untilDate+"') GROUP BY value ORDER BY value LIMIT 10000",
&chtest.TestResponse{
Body: []byte("host1\nhost2\ndc-host2\ndc-host3\n"),
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestTagsAutocomplete_ServeValuesCached(t *testing.T) {
fromDate, untilDate := dateString(h.config.ClickHouse.TaggedAutocompleDays, now)

srv.AddResponce(
"SELECT substr(arrayJoin(Tags), 6) AS value FROM graphite_tagged WHERE (((Tag1='environment=production') AND (arrayExists((x) -> x='project=web', Tags))) AND (arrayJoin(Tags) LIKE 'host=%')) AND "+
"SELECT substr(arrayJoin(Tags), 6) AS value FROM graphite_tagged WHERE (((Tag1='environment=production') AND (has(Tags, 'project=web'))) AND (arrayJoin(Tags) LIKE 'host=%')) AND "+
"(Date >= '"+fromDate+"' AND Date <= '"+untilDate+"') GROUP BY value ORDER BY value LIMIT 10000",
&chtest.TestResponse{
Body: []byte("host1\nhost2\ndc-host2\ndc-host3\n"),
Expand Down
10 changes: 7 additions & 3 deletions finder/tagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,15 @@ func TaggedTermWhereN(term *TaggedTerm, useCarbonBehaviour, dontMatchMissingTags
return "", err
}
if len(values) == 1 {
return "arrayExists((x) -> " + where.Eq("x", values[0]) + ", Tags)", nil
return where.ArrayHas("Tags", values[0]), nil
} else if len(values) > 1 {
return "arrayExists((x) -> " + where.In("x", values) + ", Tags)", nil
w := where.New()
for _, v := range values {
w.Or(where.ArrayHas("Tags", v))
}
return w.String(), nil
} else {
return "arrayExists((x) -> " + where.Eq("x", term.concat()) + ", Tags)", nil
return where.ArrayHas("Tags", term.concat()), nil
}
case TaggedTermNe:
if term.Value == "" {
Expand Down
12 changes: 6 additions & 6 deletions finder/tagged_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ func TestTaggedWhere(t *testing.T) {
{"seriesByTag('name=m{in}')", 0, "Tag1='__name__=min'", "", false},
{"seriesByTag('name=m{in,ax}')", 0, "Tag1 IN ('__name__=min','__name__=max')", "", false},
{"seriesByTag('name=m{in,ax')", 0, "", "", true},
{"seriesByTag('name=value','what={avg,max}')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
{"seriesByTag('name=value','what={avg,max}')", 0, "(Tag1='__name__=value') AND ((has(Tags, 'what=avg')) OR (has(Tags, 'what=max')))", "", false},
{"seriesByTag('name=value','what!={avg,max}')", 0, "(Tag1='__name__=value') AND (NOT arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
// grafana workaround for multi-value variables default, masked with *
{"seriesByTag('name=value','what=~*')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x LIKE 'what=%', Tags))", "", false}, // If All masked to value with *
// empty tag value during autocompletion
{"seriesByTag('name=value','what=~')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x LIKE 'what=%', Tags))", "", false}, // If All masked to value with *
// testcases for useCarbonBehaviour=false
{"seriesByTag('what=')", 0, "Tag1='what='", "", false},
{"seriesByTag('name=value','what=')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x='what=', Tags))", "", false},
{"seriesByTag('name=value','what=')", 0, "(Tag1='__name__=value') AND (has(Tags, 'what='))", "", false},
// testcases for dontMatchMissingTags=false
{"seriesByTag('key!=value')", 0, "NOT arrayExists((x) -> x='key=value', Tags)", "", false},
{"seriesByTag('dc!=de', 'cpu=cpu-total')", 0, "(Tag1='cpu=cpu-total') AND (NOT arrayExists((x) -> x='dc=de', Tags))", "", false},
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestTaggedWhere_UseCarbonBehaviourFlag(t *testing.T) {
{"seriesByTag('name=m{in}')", 0, "Tag1='__name__=min'", "", false},
{"seriesByTag('name=m{in,ax}')", 0, "Tag1 IN ('__name__=min','__name__=max')", "", false},
{"seriesByTag('name=m{in,ax')", 0, "", "", true},
{"seriesByTag('name=value','what={avg,max}')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
{"seriesByTag('name=value','what={avg,max}')", 0, "(Tag1='__name__=value') AND ((has(Tags, 'what=avg')) OR (has(Tags, 'what=max')))", "", false},
{"seriesByTag('name=value','what!={avg,max}')", 0, "(Tag1='__name__=value') AND (NOT arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
// grafana workaround for multi-value variables default, masked with *
{"seriesByTag('name=value','what=~*')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x LIKE 'what=%', Tags))", "", false}, // If All masked to value with *
Expand Down Expand Up @@ -264,7 +264,7 @@ func TestTaggedWhere_DontMatchMissingTagsFlag(t *testing.T) {
{"seriesByTag('name=m{in}')", 0, "Tag1='__name__=min'", "", false},
{"seriesByTag('name=m{in,ax}')", 0, "Tag1 IN ('__name__=min','__name__=max')", "", false},
{"seriesByTag('name=m{in,ax')", 0, "", "", true},
{"seriesByTag('name=value','what={avg,max}')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
{"seriesByTag('name=value','what={avg,max}')", 0, "(Tag1='__name__=value') AND ((has(Tags, 'what=avg')) OR (has(Tags, 'what=max')))", "", false},
// {"seriesByTag('name=value','what!={avg,max}')", 0, "(Tag1='__name__=value') AND (NOT arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
{"seriesByTag('name=value','what!={avg,max}')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x LIKE 'what=%', Tags) AND NOT arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
// grafana workaround for multi-value variables default, masked with *
Expand All @@ -273,7 +273,7 @@ func TestTaggedWhere_DontMatchMissingTagsFlag(t *testing.T) {
{"seriesByTag('name=value','what=~')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x LIKE 'what=%', Tags))", "", false}, // If All masked to value with *
// testcases for useCarbonBehaviour=false
{"seriesByTag('what=')", 0, "Tag1='what='", "", false},
{"seriesByTag('name=value','what=')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x='what=', Tags))", "", false},
{"seriesByTag('name=value','what=')", 0, "(Tag1='__name__=value') AND (has(Tags, 'what='))", "", false},
// testcases for dontMatchMissingTags=true
{"seriesByTag('key!=value')", 0, "Tag1 LIKE 'key=%' AND NOT arrayExists((x) -> x='key=value', Tags)", "", false},
{"seriesByTag('dc!=de', 'cpu=cpu-total')", 0, "(Tag1='cpu=cpu-total') AND (arrayExists((x) -> x LIKE 'dc=%', Tags) AND NOT arrayExists((x) -> x='dc=de', Tags))", "", false},
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestTaggedWhere_BothFeatureFlags(t *testing.T) {
{"seriesByTag('name=m{in}')", 0, "Tag1='__name__=min'", "", false},
{"seriesByTag('name=m{in,ax}')", 0, "Tag1 IN ('__name__=min','__name__=max')", "", false},
{"seriesByTag('name=m{in,ax')", 0, "", "", true},
{"seriesByTag('name=value','what={avg,max}')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
{"seriesByTag('name=value','what={avg,max}')", 0, "(Tag1='__name__=value') AND ((has(Tags, 'what=avg')) OR (has(Tags, 'what=max')))", "", false},
// {"seriesByTag('name=value','what!={avg,max}')", 0, "(Tag1='__name__=value') AND (NOT arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
{"seriesByTag('name=value','what!={avg,max}')", 0, "(Tag1='__name__=value') AND (arrayExists((x) -> x LIKE 'what=%', Tags) AND NOT arrayExists((x) -> x IN ('what=avg','what=max'), Tags))", "", false},
// grafana workaround for multi-value variables default, masked with *
Expand Down
4 changes: 4 additions & 0 deletions pkg/where/where.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ func HasPrefixBytes(field, prefix []byte) string {
return fmt.Sprintf("%s LIKE '%s%%'", field, likeEscape(unsafeString(prefix)))
}

func ArrayHas(field, element string) string {
return fmt.Sprintf("has(%s, %s)", field, quote(element))
}

func In(field string, list []string) string {
if len(list) == 1 {
return Eq(field, list[0])
Expand Down
Loading