Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Evaluate expressions which involve meta tags at the end #1541

Merged
merged 1 commit into from
Dec 4, 2019
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
2 changes: 1 addition & 1 deletion expr/tagquery/expression_not_equal.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (e *expressionNotEqual) GetOperator() ExpressionOperator {
}

func (e *expressionNotEqual) GetOperatorCost() uint32 {
return 1
return 2
}

func (e *expressionNotEqual) RequiresNonEmptyValue() bool {
Expand Down
2 changes: 1 addition & 1 deletion expr/tagquery/expression_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (e *expressionPrefix) GetOperator() ExpressionOperator {
}

func (e *expressionPrefix) GetOperatorCost() uint32 {
return 2
return 3
}

func (e *expressionPrefix) RequiresNonEmptyValue() bool {
Expand Down
21 changes: 21 additions & 0 deletions idx/memory/tag_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func NewTagQueryContext(query tagquery.Query) TagQueryContext {
type expressionCost struct {
operatorCost uint32
cardinality uint32
metaTag bool
expressionIdx int
}

Expand All @@ -70,22 +71,42 @@ func (q *TagQueryContext) evaluateExpressionCosts() []expressionCost {
if expr.MatchesExactly() {
costs[i].operatorCost = expr.GetOperatorCost()
costs[i].cardinality = uint32(len(q.index[expr.GetKey()]))
_, costs[i].metaTag = q.metaTagIndex[expr.GetKey()]
} else {
costs[i].operatorCost = expr.GetOperatorCost()
costs[i].cardinality = uint32(len(q.index))

// if MetaTagIndex is disabled q.metaTagIndex is nil,
// so this will not loop at all
for tag := range q.metaTagIndex {
if expr.Matches(tag) {
costs[i].metaTag = true
}
}
}
} else {
if expr.MatchesExactly() {
costs[i].operatorCost = expr.GetOperatorCost()
costs[i].cardinality = uint32(len(q.index[expr.GetKey()][expr.GetValue()]))
_, costs[i].metaTag = q.metaTagIndex[expr.GetKey()][expr.GetValue()]
} else {
costs[i].operatorCost = expr.GetOperatorCost()
costs[i].cardinality = uint32(len(q.index[expr.GetKey()]))
_, costs[i].metaTag = q.metaTagIndex[expr.GetKey()]
}
}
}

sort.Slice(costs, func(i, j int) bool {
// if one of the two is a meta tag, but the other isn't, then we always
// want to move the meta tag to the back
if costs[i].metaTag != costs[j].metaTag {
if costs[i].metaTag {
return false
}
return true
}

if costs[i].operatorCost == costs[j].operatorCost {
return costs[i].cardinality < costs[j].cardinality
}
Expand Down
38 changes: 38 additions & 0 deletions idx/memory/tag_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,41 @@ func testGetByTag(t *testing.T) {
}
}
}

// TestExpressionSortingByCost instantiates a query with various different expressions,
// some of which involve meta tags, then it sorts them by cost and verifies that the
// resulting order is as expected.
func TestExpressionSortingByCost(t *testing.T) {
query, err := tagquery.NewQueryFromStrings([]string{
"a=~b", // meta tag expected to be 6.
"c!=d", // metric tag expected to be 2.
"e!=f", // meta tag expected to be 5.
"g=h", // metric tag expected to be 1.
"i=~j", // metric tag expected to be 3.
"k=l", // metric and meta tag expected to be 4.
}, 0)
if err != nil {
t.Fatalf("Unexpected error when instantiating query: %s", err)
}

queryCtx := NewTagQueryContext(query)
queryCtx.index = TagIndex{
"c": {"d": {}},
"g": {"f": {}},
"i": {"j": {}},
"k": {"l": {}},
}
queryCtx.metaTagIndex = metaTagIndex{
"a": {"b": {}},
"e": {"f": {}},
"k": {"l": {}},
}

costs := queryCtx.evaluateExpressionCosts()
expectedIdxPositions := []int{3, 1, 4, 5, 2, 0}
for i, expectedIdxPosition := range expectedIdxPositions {
if costs[i].expressionIdx != expectedIdxPosition {
t.Fatalf("Order of expressions is not as expected\nExpected:\n%+v\nGot:\n%+v\n", expectedIdxPositions, costs)
}
}
}