From fda7eea9a324eb61bc0598dbacec54d35b67268a Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 18 Feb 2024 16:55:53 +0200 Subject: [PATCH] optimizer.go: properly propagate label filters for label_transform() function --- optimizer.go | 15 +++++++++------ optimizer_test.go | 4 ++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/optimizer.go b/optimizer.go index ef6cdba..54302a2 100644 --- a/optimizer.go +++ b/optimizer.go @@ -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) @@ -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) @@ -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 @@ -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: diff --git a/optimizer_test.go b/optimizer_test.go index 0113dbd..59d869b 100644 --- a/optimizer_test.go +++ b/optimizer_test.go @@ -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"}`)