-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fixing a bug that meant
Duration
was considered a valid Expr
- Introduced a depth-first visitor that can extract all descendant `Expr` nodes from a given `Expr`
- Loading branch information
Showing
4 changed files
with
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using PromQL.Parser.Ast; | ||
|
||
namespace PromQL.Parser | ||
{ | ||
/// <summary> | ||
/// A depth-first visitor that can find all descendant <see cref="Expr"/> nodes from a given <see cref="Expr"/>. | ||
/// </summary> | ||
public class DepthFirstExpressionVisitor : IVisitor | ||
{ | ||
private List<Expr> _expressions = new(); | ||
|
||
void IVisitor.Visit(StringLiteral expr) => _expressions.Add(expr); | ||
|
||
void IVisitor.Visit(SubqueryExpr sq) | ||
{ | ||
_expressions.Add(sq); | ||
sq.Expr.Accept(this); | ||
} | ||
|
||
void IVisitor.Visit(Duration d) { } | ||
|
||
void IVisitor.Visit(NumberLiteral n) => _expressions.Add(n); | ||
|
||
void IVisitor.Visit(MetricIdentifier mi) { } | ||
|
||
void IVisitor.Visit(LabelMatcher expr) { } | ||
|
||
void IVisitor.Visit(UnaryExpr unary) | ||
{ | ||
_expressions.Add(unary); | ||
unary.Expr.Accept(this); | ||
} | ||
|
||
void IVisitor.Visit(MatrixSelector ms) | ||
{ | ||
_expressions.Add(ms); | ||
// No need to visit vector selector, it's accessible from matrix selector | ||
} | ||
|
||
void IVisitor.Visit(OffsetExpr offset) | ||
{ | ||
_expressions.Add(offset); | ||
offset.Expr.Accept(this); | ||
} | ||
|
||
void IVisitor.Visit(ParenExpression paren) | ||
{ | ||
_expressions.Add(paren); | ||
paren.Expr.Accept(this); | ||
} | ||
|
||
void IVisitor.Visit(FunctionCall fnCall) | ||
{ | ||
_expressions.Add(fnCall); | ||
foreach (var a in fnCall.Args) | ||
a.Accept(this); | ||
} | ||
|
||
void IVisitor.Visit(VectorMatching vm) { } | ||
|
||
void IVisitor.Visit(BinaryExpr expr) | ||
{ | ||
_expressions.Add(expr); | ||
expr.LeftHandSide.Accept(this); | ||
expr.RightHandSide.Accept(this); | ||
} | ||
|
||
void IVisitor.Visit(AggregateExpr expr) | ||
{ | ||
_expressions.Add(expr); | ||
expr.Param?.Accept(this); | ||
expr.Expr.Accept(this); | ||
} | ||
|
||
void IVisitor.Visit(VectorSelector vs) => _expressions.Add(vs); | ||
|
||
void IVisitor.Visit(LabelMatchers lms) { } | ||
|
||
public IEnumerable<Expr> GetExpressions(Expr expr) | ||
{ | ||
_expressions.Clear(); | ||
expr.Accept(this); | ||
return _expressions; | ||
} | ||
} | ||
} |
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
51 changes: 51 additions & 0 deletions
51
tests/PromQL.Parser.Tests/DepthFirstExpressionVisitorTests.cs
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,51 @@ | ||
using System.Linq; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using PromQL.Parser.Ast; | ||
|
||
namespace PromQL.Parser.Tests | ||
{ | ||
[TestFixture] | ||
public class DepthFirstExpressionVisitorTests | ||
{ | ||
[Test] | ||
public void Visit_Basic_Types() | ||
{ | ||
// Not semantically valid but syntactically valid! | ||
const string toParse = "'hello' + 1"; | ||
var expr = Parser.ParseExpression(toParse); | ||
var visitor = new DepthFirstExpressionVisitor(); | ||
visitor.GetExpressions(expr) | ||
.Select(x => x.GetType()) | ||
.Should() | ||
.Equal( | ||
typeof(BinaryExpr), | ||
typeof(StringLiteral), | ||
typeof(NumberLiteral) | ||
); | ||
} | ||
|
||
[Test] | ||
public void Visit_Complex_Expression() | ||
{ | ||
const string toParse = "sum(rate(my_vector[1m] offset 5m)) + -(some_metric[1m:])"; | ||
var expr = Parser.ParseExpression(toParse); | ||
var visitor = new DepthFirstExpressionVisitor(); | ||
visitor.GetExpressions(expr) | ||
.Select(x => x.GetType()) | ||
.Should() | ||
.Equal( | ||
typeof(BinaryExpr), | ||
typeof(AggregateExpr), | ||
typeof(FunctionCall), | ||
typeof(OffsetExpr), | ||
typeof(MatrixSelector), | ||
typeof(UnaryExpr), | ||
typeof(ParenExpression), | ||
typeof(SubqueryExpr), | ||
typeof(VectorSelector) | ||
) | ||
; | ||
} | ||
} | ||
} |