Skip to content
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
10 changes: 10 additions & 0 deletions binary_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ func isBinaryOpBoolModifier(s string) bool {
return s == "bool"
}

func isBinaryOpFillModifier(s string) bool {
s = strings.ToLower(s)
switch s {
case "fill", "fill_left", "fill_right":
return true
default:
return false
}
}

// IsBinaryOpCmp returns true if op is comparison operator such as '==', '!=', etc.
func IsBinaryOpCmp(op string) bool {
switch op {
Expand Down
89 changes: 88 additions & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,15 @@ func (p *parser) parseExpr() (Expr, error) {
}
}
}
for {
ok, err := p.parseFillModifier(&be)
if err != nil {
return nil, err
}
if !ok {
break
}
}
e2, err := p.parseSingleExpr()
if err != nil {
return nil, err
Expand Down Expand Up @@ -506,6 +515,62 @@ func (p *parser) parseSingleExprWithoutRollupSuffix() (Expr, error) {
}
}

func (p *parser) parseFillModifier(be *BinaryOpExpr) (bool, error) {
if !isBinaryOpFillModifier(p.lex.Token) {
return false, nil
}
op := strings.ToLower(p.lex.Token)
if err := p.lex.Next(); err != nil {
return false, err
}
if p.lex.Token != "(" {
p.lex.Prev()
return false, nil
}
if err := p.lex.Next(); err != nil {
return false, err
}
ne, err := p.parseFillValue()
if err != nil {
return false, fmt.Errorf("cannot parse %s fill value: %w", op, err)
}
if p.lex.Token != ")" {
return false, fmt.Errorf(`%s: unexpected token %q; want ")"`, op, p.lex.Token)
}
if err := p.lex.Next(); err != nil {
return false, err
}
switch op {
case "fill":
be.FillLeft = ne
be.FillRight = ne
case "fill_left":
be.FillLeft = ne
case "fill_right":
be.FillRight = ne
}
return true, nil
}

func (p *parser) parseFillValue() (*NumberExpr, error) {
neg := false
if p.lex.Token == "-" {
neg = true
if err := p.lex.Next(); err != nil {
return nil, err
}
}
ne, err := p.parsePositiveNumberExpr()
if err != nil {
return nil, err
}
if neg {
ne.N = -ne.N
ne.s = "-" + ne.s
}
return ne, nil
}

func (p *parser) parsePositiveNumberExpr() (*NumberExpr, error) {
if !isPositiveNumberPrefix(p.lex.Token) && !isInfOrNaN(p.lex.Token) {
return nil, fmt.Errorf(`positiveNumberExpr: unexpected token %q; want "number"`, p.lex.Token)
Expand Down Expand Up @@ -1896,6 +1961,12 @@ type BinaryOpExpr struct {
// If KeepMetricNames is set to true, then the operation should keep metric names.
KeepMetricNames bool

// FillLeft contains the fill value for fill_left() or fill() modifier.
FillLeft *NumberExpr

// FillRight contains the fill value for fill_right() or fill() modifier.
FillRight *NumberExpr

// Left contains left arg for the `left op right` expression.
Left Expr

Expand Down Expand Up @@ -1971,6 +2042,22 @@ func (be *BinaryOpExpr) appendModifiers(dst []byte) []byte {
dst = prefix.AppendString(dst)
}
}
if be.FillLeft != nil && be.FillLeft == be.FillRight {
dst = append(dst, " fill("...)
dst = be.FillLeft.AppendString(dst)
dst = append(dst, ')')
} else {
if be.FillLeft != nil {
dst = append(dst, " fill_left("...)
dst = be.FillLeft.AppendString(dst)
dst = append(dst, ')')
}
if be.FillRight != nil {
dst = append(dst, " fill_right("...)
dst = be.FillRight.AppendString(dst)
dst = append(dst, ')')
}
}
return dst
}

Expand All @@ -1989,7 +2076,7 @@ func needBinaryOpArgParens(arg Expr) bool {
}

func isReservedBinaryOpIdent(s string) bool {
return isBinaryOpGroupModifier(s) || isBinaryOpJoinModifier(s) || isBinaryOpBoolModifier(s) || isPrefixModifier(s)
return isBinaryOpGroupModifier(s) || isBinaryOpJoinModifier(s) || isBinaryOpBoolModifier(s) || isPrefixModifier(s) || isBinaryOpFillModifier(s)
}

func isPrefixModifier(s string) bool {
Expand Down
16 changes: 16 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,22 @@ func TestParseSuccess(t *testing.T) {
another(`a + oN() gROUp_rigHt(*) PREfix "bar" b`, `a + on() group_right(*) prefix "bar" b`)
same(`a + on(a) group_left(x,y) prefix "foo" b`)
same(`a + on(a,b) group_right(z) prefix "bar" b`)

// fill modifiers
same(`m * on(job) fill_right(0) n`)
same(`m * on(job) fill_left(0) n`)
same(`m * on(job) fill_left(0) fill_right(1) n`)
same(`m * on(job) fill(0) n`)
another(`m * on(job) FILL_RIGHT(0) n`, `m * on(job) fill_right(0) n`)
another(`m * on(job) FILL(0) n`, `m * on(job) fill(0) n`)
same(`m * on(job) fill_right(-1) n`)
same(`m * on(job) fill_left(inf) n`)
same(`m * on(job) fill(NaN) n`)
same(`m * on(job) group_left() fill_right(0) n`)
same(`m * fill_right(0) n`)
another(`a + fill_right`, `a + (fill_right)`)
another(`a + fill_left`, `a + (fill_left)`)
another(`a + fill`, `a + (fill)`)
another(`5 - 1 + 3 * 2 ^ 2 ^ 3 - 2 OR Metric {Bar= "Baz", aaa!="bb",cc=~"dd" ,zz !~"ff" } `,
`770 or Metric{Bar="Baz",aaa!="bb",cc=~"dd",zz!~"ff"}`)

Expand Down