github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/querier/astmapper/parallel_test.go (about)

     1  package astmapper
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/prometheus/common/model"
     8  	"github.com/prometheus/prometheus/pkg/labels"
     9  	"github.com/prometheus/prometheus/promql/parser"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestCanParallel(t *testing.T) {
    14  	var testExpr = []struct {
    15  		input    parser.Expr
    16  		expected bool
    17  	}{
    18  		// simple sum
    19  		{
    20  			&parser.AggregateExpr{
    21  				Op:      parser.SUM,
    22  				Without: true,
    23  				Expr: &parser.VectorSelector{
    24  					Name: "some_metric",
    25  					LabelMatchers: []*labels.Matcher{
    26  						mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "some_metric"),
    27  					},
    28  				},
    29  				Grouping: []string{"foo"},
    30  			},
    31  			true,
    32  		},
    33  		/*
    34  			  sum(
    35  				  sum by (foo) bar1{baz=”blip”}[1m])
    36  				/
    37  				  sum by (foo) bar2{baz=”blip”}[1m]))
    38  			  )
    39  		*/
    40  		{
    41  			&parser.AggregateExpr{
    42  				Op: parser.SUM,
    43  				Expr: &parser.BinaryExpr{
    44  					Op: parser.DIV,
    45  					LHS: &parser.AggregateExpr{
    46  						Op:       parser.SUM,
    47  						Grouping: []string{"foo"},
    48  						Expr: &parser.VectorSelector{
    49  							Name: "idk",
    50  							LabelMatchers: []*labels.Matcher{
    51  								mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar1"),
    52  							}},
    53  					},
    54  					RHS: &parser.AggregateExpr{
    55  						Op:       parser.SUM,
    56  						Grouping: []string{"foo"},
    57  						Expr: &parser.VectorSelector{
    58  							Name: "idk",
    59  							LabelMatchers: []*labels.Matcher{
    60  								mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar2"),
    61  							}},
    62  					},
    63  				},
    64  			},
    65  			false,
    66  		},
    67  		// sum by (foo) bar1{baz=”blip”}[1m]) ---- this is the first leg of the above
    68  		{
    69  			&parser.AggregateExpr{
    70  				Op:       parser.SUM,
    71  				Grouping: []string{"foo"},
    72  				Expr: &parser.VectorSelector{
    73  					Name: "idk",
    74  					LabelMatchers: []*labels.Matcher{
    75  						mustLabelMatcher(labels.MatchEqual, string(model.MetricNameLabel), "bar1"),
    76  					}},
    77  			},
    78  			true,
    79  		},
    80  	}
    81  
    82  	for i, c := range testExpr {
    83  		t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {
    84  			res := CanParallelize(c.input)
    85  			require.Equal(t, c.expected, res)
    86  		})
    87  	}
    88  }
    89  
    90  func TestCanParallel_String(t *testing.T) {
    91  	var testExpr = []struct {
    92  		input    string
    93  		expected bool
    94  	}{
    95  		{
    96  			`sum by (foo) (rate(bar1{baz="blip"}[1m]))`,
    97  			true,
    98  		},
    99  		{
   100  			`sum by (foo) (histogram_quantile(0.9, rate(http_request_duration_seconds_bucket[10m])))`,
   101  			false,
   102  		},
   103  		{
   104  			`sum by (foo) (
   105  			  quantile_over_time(0.9, http_request_duration_seconds_bucket[10m])
   106  			)`,
   107  			false,
   108  		},
   109  		{
   110  			`sum(
   111  				count(
   112  					count(
   113  						foo{bar="baz"}
   114  					)  by (a,b)
   115  				)  by (instance)
   116  			)`,
   117  			false,
   118  		},
   119  	}
   120  
   121  	for i, c := range testExpr {
   122  		t.Run(fmt.Sprintf("[%d]", i), func(t *testing.T) {
   123  			expr, err := parser.ParseExpr(c.input)
   124  			require.Nil(t, err)
   125  			res := CanParallelize(expr)
   126  			require.Equal(t, c.expected, res)
   127  		})
   128  	}
   129  }