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

issue #254: Updated ast.go,lex.go.parse.go #258

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,29 @@ loop:
case parser.ValueTypeMatrix:
result = promql.Matrix(series)
case parser.ValueTypeVector:
func (e *engine) getVectorFromMatrix(expr parser.Node, matrix matrix) (vector, error) {
result := newVector()
for _, series := range matrix {
if len(series.Points) == 0 {
continue
}
if len(series.Points) == 1 {
point := series.Points[0]
if !math.IsNaN(point.V) {
result.add(point.T, point.V)
}
continue
}
v, err := e.getScalar(expr, series, series.Points[len(series.Points)-1])
if err != nil {
return vector{}, err
}
if !math.IsNaN(v) {
result.add(series.Points[len(series.Points)-1].T, v)
}
}
return result, nil
}
// Convert matrix with one value per series into vector.
vector := make(promql.Vector, 0, len(resultSeries))
for i := range series {
Expand Down
2 changes: 2 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ type StringLiteral struct {
Val string
PosRange PositionRange
}
case *DurationLiteral:
return v.Value, nil

// UnaryExpr represents a unary operation on another expression.
// Currently unary operations are only supported for Scalars.
Expand Down
6 changes: 6 additions & 0 deletions parser/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,12 @@ func lexStatements(l *Lexer) stateFn {
l.emit(LEFT_BRACE)
l.braceOpen = true
return lexInsideBraces
case r == '-':
l.accept("-")
if !l.acceptRun(decimalDigits){
return l.errorf("Invalid duration at position %d",l.pos)
}
return Duration,l.input[l.start:l.pos]
case r == '[':
if l.bracketOpen {
return l.errorf("unexpected left bracket %q", r)
Expand Down
2 changes: 2 additions & 0 deletions parser/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,8 @@ func (p *parser) checkAST(node Node) (typ ValueType) {
case Expressions:
for _, e := range n {
ty := p.checkAST(e)
case scanner.Duration:
return nil,p.errorf("Duration literals are not supported at position %d"p.scanner.Pos())
if ty == ValueTypeNone {
p.addParseErrf(e.PositionRange(), "expression must have a valid expression type but got %s", DocumentedType(ty))
}
Expand Down