Skip to content

Commit

Permalink
Merge pull request #91 from cortexproject/fix-unary-expr-range-vector…
Browse files Browse the repository at this point in the history
…-error

fix unary expression generates bad query
  • Loading branch information
yeya24 authored Jan 15, 2024
2 parents e3aada4 + bc748f1 commit f98713a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
7 changes: 4 additions & 3 deletions walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ func (s *PromQLSmith) walkAggregateParam(op parser.ItemType) parser.Expr {
// or function that returns matrix.
func (s *PromQLSmith) walkBinaryExpr(valueTypes ...parser.ValueType) parser.Expr {
valueTypes = keepValueTypes(valueTypes, vectorAndScalarValueTypes)
if len(valueTypes) == 0 {
valueTypes = vectorAndScalarValueTypes
}
expr := &parser.BinaryExpr{
Op: s.walkBinaryOp(!slices.Contains(valueTypes, parser.ValueTypeVector)),
VectorMatching: &parser.VectorMatching{
Expand Down Expand Up @@ -399,7 +396,11 @@ func wrapParenExpr(expr parser.Expr) parser.Expr {

// keepValueTypes picks value types that we should keep from the input.
// input shouldn't contain duplicate value types.
// If no input value types are provided, use value types to keep as result.
func keepValueTypes(input []parser.ValueType, keep []parser.ValueType) []parser.ValueType {
if len(input) == 0 {
return keep
}
out := make([]parser.ValueType, 0, len(keep))
s := make(map[parser.ValueType]struct{})
for _, vt := range keep {
Expand Down
5 changes: 5 additions & 0 deletions walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ func TestKeepValueTypes(t *testing.T) {
keep: []parser.ValueType{parser.ValueTypeMatrix},
expected: []parser.ValueType{},
},
{
input: []parser.ValueType{},
keep: vectorAndScalarValueTypes,
expected: vectorAndScalarValueTypes,
},
{
input: []parser.ValueType{parser.ValueTypeMatrix},
keep: []parser.ValueType{parser.ValueTypeMatrix},
Expand Down

0 comments on commit f98713a

Please sign in to comment.