Skip to content
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
15 changes: 12 additions & 3 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1933,11 +1933,11 @@ func (be *BinaryOpExpr) appendStringNoKeepMetricNames(dst []byte) []byte {
}

func (be *BinaryOpExpr) needLeftParens() bool {
return needBinaryOpArgParens(be.Left, be.Op)
return needBinaryOpArgParens(be.Left, be.Op, false)
}

func (be *BinaryOpExpr) needRightParens() bool {
if needBinaryOpArgParens(be.Right, be.Op) {
if needBinaryOpArgParens(be.Right, be.Op, true) {
return true
}
switch t := be.Right.(type) {
Expand Down Expand Up @@ -1974,7 +1974,7 @@ func (be *BinaryOpExpr) appendModifiers(dst []byte) []byte {
return dst
}

func needBinaryOpArgParens(arg Expr, parentOp string) bool {
func needBinaryOpArgParens(arg Expr, parentOp string, isRight bool) bool {
switch t := arg.(type) {
case *BinaryOpExpr:
// Parens are required when the child op priority not equal to parent o one.
Expand All @@ -1984,6 +1984,15 @@ func needBinaryOpArgParens(arg Expr, parentOp string) bool {
}

// Same op: parens are only needed when the sub-expression is not a simple leaf chain.
if t.Op != parentOp {
if isRight && !isRightAssociativeBinaryOp(parentOp) {
return true
}
if !isRight && isRightAssociativeBinaryOp(parentOp) {
return true
}
}

return !isBinaryOpLeafSimple(t)
case *RollupExpr:
if be, ok := t.Expr.(*BinaryOpExpr); ok && be.KeepMetricNames {
Expand Down
20 changes: 20 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,26 @@ func TestParseSuccess(t *testing.T) {
// parensExpr
another(`(-foo + ((bar) / (baz))) + ((23))`, `0 - foo + (bar / baz) + 23`)
another(`(FOO + ((Bar) / (baZ))) + ((23))`, `FOO + (Bar / baZ) + 23`)

same(`a + (b - c)`)
same(`a - (b + c)`)
same(`a * (b / c)`)
same(`a * (b % c)`)
same(`a / (b * c)`)
same(`a / (b % c)`)
same(`a % (b * c)`)
another(`(a - b) + c`, `a - b + c`)
another(`(a / b) * c`, `a / b * c`)
another(`a + (b + c)`, `a + b + c`)
another(`a * (b * c)`, `a * b * c`)
another(`(a + b) - c`, `a + b - c`)
another(`(a * b) / c`, `a * b / c`)
same(`a and (b unless c)`)
same(`a unless (b and c)`)
another(`(a ^ b) ^ c`, `a ^ b ^ c`)
another(`a ^ (b ^ c)`, `a ^ b ^ c`)
same(`a / (b * c)`)

same(`(foo, bar)`)
another(`((foo, bar),(baz))`, `((foo, bar), baz)`)
same(`(foo, (bar, baz), ((x, y), (z, y), xx))`)
Expand Down
2 changes: 1 addition & 1 deletion utils_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ func ExampleExpandWithExprs() {
fmt.Printf("%s\n", pql)

// Output:
// 100 * disk_free_bytes{job="$job",instance="$instance"} / disk_total_bytes{job="$job",instance="$instance"}
// 100 * (disk_free_bytes{job="$job",instance="$instance"} / disk_total_bytes{job="$job",instance="$instance"})
}
Loading