forked from cloudflare/pint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
350 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package utils | ||
|
||
import ( | ||
promParser "github.com/prometheus/prometheus/promql/parser" | ||
"github.com/rs/zerolog/log" | ||
|
||
"github.com/cloudflare/pint/internal/parser" | ||
) | ||
|
||
func HasOuterSum(node *parser.PromQLNode) (calls []*promParser.AggregateExpr) { | ||
if n, ok := node.Node.(*promParser.AggregateExpr); ok { | ||
if n.Op == promParser.SUM { | ||
calls = append(calls, n) | ||
return calls | ||
} | ||
} | ||
|
||
if n, ok := node.Node.(*promParser.AggregateExpr); ok { | ||
switch n.Op { | ||
case promParser.COUNT: | ||
return nil | ||
case promParser.COUNT_VALUES: | ||
return nil | ||
} | ||
} | ||
|
||
if n, ok := node.Node.(*promParser.BinaryExpr); ok { | ||
if n.VectorMatching != nil { | ||
switch n.VectorMatching.Card { | ||
case promParser.CardOneToOne: | ||
case promParser.CardOneToMany: | ||
for i, child := range node.Children { | ||
if i == len(node.Children)-1 { | ||
return HasOuterSum(child) | ||
} | ||
} | ||
case promParser.CardManyToOne: | ||
return HasOuterSum(node.Children[0]) | ||
case promParser.CardManyToMany: | ||
default: | ||
log.Warn().Str("matching", n.VectorMatching.Card.String()).Msg("Unsupported VectorMatching operation") | ||
} | ||
} | ||
|
||
if n.Op.IsComparisonOperator() { | ||
for i, child := range node.Children { | ||
if i == 0 { | ||
return HasOuterSum(child) | ||
} | ||
} | ||
} else { | ||
switch n.Op { | ||
case promParser.LOR: | ||
for _, child := range node.Children { | ||
calls = append(calls, HasOuterSum(child)...) | ||
} | ||
return calls | ||
case promParser.DIV, promParser.LUNLESS, promParser.LAND: | ||
for _, child := range node.Children { | ||
return HasOuterSum(child) | ||
} | ||
} | ||
} | ||
} | ||
|
||
for _, child := range node.Children { | ||
calls = append(calls, HasOuterSum(child)...) | ||
} | ||
|
||
return calls | ||
} |
Oops, something went wrong.