Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix unary expression generates bad query #91

Merged
merged 1 commit into from
Jan 15, 2024
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
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