Skip to content

Commit

Permalink
optimizer.go: properly propagate label filters for label_transform() …
Browse files Browse the repository at this point in the history
…function
  • Loading branch information
valyala committed Feb 18, 2024
1 parent 97f5f82 commit fda7eea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
15 changes: 9 additions & 6 deletions optimizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func getCommonLabelFilters(e Expr) []LabelFilter {
switch strings.ToLower(t.Name) {
case "label_set":
return getCommonLabelFiltersForLabelSet(t.Args)
case "label_replace", "label_join", "label_map", "label_match", "label_mismatch":
case "label_replace", "label_join", "label_map", "label_match", "label_mismatch", "label_transform":
return getCommonLabelFiltersForLabelReplace(t.Args)
case "label_copy", "label_move":
return getCommonLabelFiltersForLabelCopy(t.Args)
Expand Down Expand Up @@ -339,7 +339,7 @@ func pushdownBinaryOpFiltersInplace(e Expr, lfs []LabelFilter) {
switch strings.ToLower(t.Name) {
case "label_set":
pushdownLabelFiltersForLabelSet(t.Args, lfs)
case "label_replace", "label_join", "label_map", "label_match", "label_mismatch":
case "label_replace", "label_join", "label_map", "label_match", "label_mismatch", "label_transform":
pushdownLabelFiltersForLabelReplace(t.Args, lfs)
case "label_copy", "label_move":
pushdownLabelFiltersForLabelCopy(t.Args, lfs)
Expand Down Expand Up @@ -630,7 +630,13 @@ func getRollupArgIdxForOptimization(funcName string, args []Expr) int {

func getTransformArgIdxForOptimization(funcName string, args []Expr) int {
switch strings.ToLower(funcName) {
case "", "absent", "scalar", "union", "vector", "range_normalize":
case "label_copy", "label_del", "label_join", "label_keep", "label_lowercase", "label_map",
"label_match", "label_mismatch", "label_move", "label_replace", "label_set", "label_transform",
"label_uppercase", "labels_equal":
panic(fmt.Errorf("BUG: unexpected funcName passed to getTransformArgIdxForOptimization: %s", funcName))
case "drop_common_labels", "range_normalize":
return -1
case "", "absent", "scalar", "union", "vector":
return -1
case "end", "now", "pi", "ru", "start", "step", "time":
return -1
Expand All @@ -641,9 +647,6 @@ func getTransformArgIdxForOptimization(funcName string, args []Expr) int {
return 1
case "histogram_quantiles":
return len(args) - 1
case "drop_common_labels", "label_copy", "label_del", "label_join", "label_keep", "label_lowercase",
"label_map", "label_move", "label_replace", "label_set", "label_transform", "label_uppercase":
return -1
case "label_graphite_group":
return 0
default:
Expand Down
4 changes: 4 additions & 0 deletions optimizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ func TestOptimize(t *testing.T) {
f(`label_mismatch(foo, "a", "x", "y") + bar{x="y"}`, `label_mismatch(foo{x="y"}, "a", "x", "y") + bar{x="y"}`)
f(`label_mismatch(foo{a="qwe",b="c"}, "a", "x", "y") + bar{a="rt",x="y"}`, `label_mismatch(foo{a="qwe",b="c",x="y"}, "a", "x", "y") + bar{a="rt",b="c",x="y"}`)

// label_transform
f(`label_transform(foo, "a", "x", "y") + bar{x="y"}`, `label_transform(foo{x="y"}, "a", "x", "y") + bar{x="y"}`)
f(`label_transform(foo{a="qwe",b="c"}, "a", "x", "y") + bar{a="rt",x="y"}`, `label_transform(foo{a="qwe",b="c",x="y"}, "a", "x", "y") + bar{a="rt",b="c",x="y"}`)

// label_copy
f(`label_copy(foo, "a", "b") + bar{x="y"}`, `label_copy(foo{x="y"}, "a", "b") + bar{x="y"}`)
f(`label_copy(foo, "a", "b", "c", "d") + bar{a="y",b="z"}`, `label_copy(foo{a="y"}, "a", "b", "c", "d") + bar{a="y",b="z"}`)
Expand Down

0 comments on commit fda7eea

Please sign in to comment.