Skip to content

Commit

Permalink
Remove extra ","
Browse files Browse the repository at this point in the history
  • Loading branch information
jeschkies committed Nov 13, 2023
1 parent 9261f84 commit cfa48c9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
16 changes: 6 additions & 10 deletions pkg/logql/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ func EncodeJSON(e syntax.Expr, w io.Writer) error {
s := jsoniter.ConfigFastest.BorrowStream(w)
defer jsoniter.ConfigFastest.ReturnStream(s)
v := NewJSONSerializer(s)
err := syntax.Dispatch(e, v)
s.Flush()

return err
e.Accept(v)
return s.Flush()
}

func DecodeJSON(raw string) (syntax.Expr, error) {
Expand Down Expand Up @@ -63,19 +61,18 @@ func (v *JSONSerializer) VisitBinOp(e *syntax.BinOpExpr) {
v.WriteObjectField("bin")
v.WriteObjectStart()

v.WriteMore()
v.WriteObjectField("op")
v.WriteString(e.Op)

// TODO: encode options

v.WriteMore()
v.WriteObjectField("lhs")
syntax.DispatchSampleExpr(e.SampleExpr, v)
e.SampleExpr.Accept(v)

v.WriteMore()
v.WriteObjectField("rhs")
syntax.DispatchSampleExpr(e.RHS, v)
e.RHS.Accept(v)

v.WriteObjectEnd()
v.WriteObjectEnd()
Expand All @@ -99,7 +96,7 @@ func (v *JSONSerializer) VisitVectorAggregation(e *syntax.VectorAggregationExpr)

v.WriteMore()
v.WriteObjectField("inner")
syntax.DispatchSampleExpr(e.Left, v)
e.Left.Accept(v)

v.WriteObjectEnd()
v.WriteObjectEnd()
Expand Down Expand Up @@ -168,7 +165,7 @@ func (v *JSONSerializer) VisitLabelReplace(e *syntax.LabelReplaceExpr) {

v.WriteMore()
v.WriteObjectField("inner")
syntax.DispatchSampleExpr(e.Left, v)
e.Left.Accept(v)

v.WriteMore()
v.WriteObjectField("dst")
Expand Down Expand Up @@ -197,7 +194,6 @@ func (v *JSONSerializer) VisitLiteral(e *syntax.LiteralExpr) {
v.WriteObjectField("literal")
v.WriteObjectStart()

v.WriteMore()
v.WriteObjectField("val")
v.WriteFloat64(e.Val)

Expand Down
7 changes: 4 additions & 3 deletions pkg/logql/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func TestJSONSerializationRoundTrip(t *testing.T) {
tests := map[string]struct {
query string
}{
/*
"simple matchers": {
query: `{env="prod", app=~"loki.*"}`,
},
Expand All @@ -22,9 +23,9 @@ func TestJSONSerializationRoundTrip(t *testing.T) {
},
"simple aggregation with unwrap": {
query: `sum_over_time({env="prod", app=~"loki.*"} | unwrap bytes[5m])`,
},
"label filterer": {
query: `bytes >= 0`,
},*/
"bin op": {
query: `count_over_time({env="prod", app=~"loki.*"}[5m]) >= 0`,
},
}

Expand Down
53 changes: 48 additions & 5 deletions pkg/logql/syntax/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Expr interface {
Shardable() bool // A recursive check on the AST to see if it's shardable.
Walkable
fmt.Stringer
AcceptVisitor

// Pretty prettyfies any LogQL expression at given `level` of the whole LogQL query.
Pretty(level int) string
Expand Down Expand Up @@ -212,6 +213,8 @@ func (e *MatchersExpr) Shardable() bool { return true }

func (e *MatchersExpr) Walk(f WalkFn) { f(e) }

func (e *MatchersExpr) Accept(v RootVisitor) { v.VisitMatchers(e) }

func (e *MatchersExpr) String() string {
var sb strings.Builder
sb.WriteString("{")
Expand Down Expand Up @@ -272,6 +275,8 @@ func (e *PipelineExpr) Walk(f WalkFn) {
walkAll(f, xs...)
}

func (e *PipelineExpr) Accept(v RootVisitor) { v.VisitPipeline(e) }

func (e *PipelineExpr) Matchers() []*labels.Matcher {
return e.Left.Matchers()
}
Expand Down Expand Up @@ -349,6 +354,10 @@ func (e *LineFilterExpr) Walk(f WalkFn) {
e.Left.Walk(f)
}

func (e *LineFilterExpr) Accept(v RootVisitor) {
v.VisitLineFilter(e)
}

// AddFilterExpr adds a filter expression to a logselector expression.
func AddFilterExpr(expr LogSelectorExpr, ty labels.MatchType, op, match string) (LogSelectorExpr, error) {
filter := newLineFilterExpr(ty, op, match)
Expand Down Expand Up @@ -485,6 +494,8 @@ func (e *LogfmtParserExpr) Shardable() bool { return true }

func (e *LogfmtParserExpr) Walk(f WalkFn) { f(e) }

func (e *LogfmtParserExpr) Accept(v RootVisitor) { v.VisitLogfmtParser(e) }

func (e *LogfmtParserExpr) Stage() (log.Stage, error) {
return log.NewLogfmtParser(e.Strict, e.KeepEmpty), nil
}
Expand Down Expand Up @@ -540,6 +551,8 @@ func (e *LabelParserExpr) Shardable() bool { return true }

func (e *LabelParserExpr) Walk(f WalkFn) { f(e) }

func (e *LabelParserExpr) Accept(v RootVisitor) { v.VisitLabelParser(e) }

func (e *LabelParserExpr) Stage() (log.Stage, error) {
switch e.Op {
case OpParserTypeJSON:
Expand Down Expand Up @@ -587,6 +600,8 @@ func (e *LabelFilterExpr) Shardable() bool { return true }

func (e *LabelFilterExpr) Walk(f WalkFn) { f(e) }

func (e *LabelFilterExpr) Accept(v RootVisitor) { v.VisitLabelFilter(e) }

func (e *LabelFilterExpr) Stage() (log.Stage, error) {
switch ip := e.LabelFilterer.(type) {
case *log.IPLabelFilter:
Expand Down Expand Up @@ -632,6 +647,8 @@ func (e *DecolorizeExpr) String() string {
}
func (e *DecolorizeExpr) Walk(f WalkFn) { f(e) }

func (e *DecolorizeExpr) Accept(v RootVisitor) { v.VisitDecolorize(e) }

type DropLabelsExpr struct {
dropLabels []log.DropLabel
implicit
Expand Down Expand Up @@ -672,6 +689,8 @@ func (e *DropLabelsExpr) String() string {
}
func (e *DropLabelsExpr) Walk(f WalkFn) { f(e) }

func (e *DropLabelsExpr) Accept(v RootVisitor) { v.VisitDropLabels(e) }

type KeepLabelsExpr struct {
keepLabels []log.KeepLabel
implicit
Expand Down Expand Up @@ -714,12 +733,16 @@ func (e *KeepLabelsExpr) String() string {

func (e *KeepLabelsExpr) Walk(f WalkFn) { f(e) }

func (e *KeepLabelsExpr) Accept(v RootVisitor) { v.VisitKeekLabel(e) }

func (*LineFmtExpr) isStageExpr() {}

func (e *LineFmtExpr) Shardable() bool { return true }

func (e *LineFmtExpr) Walk(f WalkFn) { f(e) }

func (e *LineFmtExpr) Accept(v RootVisitor) { v.VisitLineFmt(e) }

func (e *LineFmtExpr) Stage() (log.Stage, error) {
return log.NewFormatter(e.Value)
}
Expand Down Expand Up @@ -749,6 +772,8 @@ func (e *LabelFmtExpr) Shardable() bool {

func (e *LabelFmtExpr) Walk(f WalkFn) { f(e) }

func (e *LabelFmtExpr) Accept(v RootVisitor) { v.VisitLabelFmt(e) }

func (e *LabelFmtExpr) Stage() (log.Stage, error) {
return log.NewLabelsFormatter(e.Formats)
}
Expand Down Expand Up @@ -791,6 +816,8 @@ func (j *JSONExpressionParser) Shardable() bool { return true }

func (j *JSONExpressionParser) Walk(f WalkFn) { f(j) }

func (j *JSONExpressionParser) Accept(v RootVisitor) { v.VisitJSONExpressionParser(j) }

func (j *JSONExpressionParser) Stage() (log.Stage, error) {
return log.NewJSONExpressionParser(j.Expressions)
}
Expand Down Expand Up @@ -845,6 +872,8 @@ func (l *LogfmtExpressionParser) Shardable() bool { return true }

func (l *LogfmtExpressionParser) Walk(f WalkFn) { f(l) }

func (l *LogfmtExpressionParser) Accept(v RootVisitor) { v.VisitLogfmtExpressionParser(l) }

func (l *LogfmtExpressionParser) Stage() (log.Stage, error) {
return log.NewLogfmtExpressionParser(l.Expressions, l.Strict)
}
Expand Down Expand Up @@ -970,6 +999,10 @@ func (r *LogRange) Walk(f WalkFn) {
r.Left.Walk(f)
}

func (r *LogRange) Accept(v RootVisitor) {
v.VisitLogRange(r)
}

// WithoutUnwrap returns a copy of the log range without the unwrap statement.
func (r *LogRange) WithoutUnwrap() (*LogRange, error) {
left, err := Clone(r.Left)
Expand Down Expand Up @@ -1267,6 +1300,8 @@ func (e *RangeAggregationExpr) Walk(f WalkFn) {
e.Left.Walk(f)
}

func (e *RangeAggregationExpr) Accept(v RootVisitor) { v.VisitRangeAggregation(e) }

// Grouping struct represents the grouping by/without label(s) for vector aggregators and range vector aggregators.
// The representation is as follows:
// - No Grouping (labels dismissed): <operation> (<expr>) => Grouping{Without: false, Groups: nil}
Expand Down Expand Up @@ -1310,11 +1345,11 @@ func (g Grouping) Singleton() bool {
// VectorAggregationExpr all vector aggregation expressions support grouping by/without label(s),
// therefore the Grouping struct can never be nil.
type VectorAggregationExpr struct {
Left SampleExpr
Left SampleExpr `json:"sample_expr"`

Grouping *Grouping
Params int
Operation string
Grouping *Grouping `json:"grouping,omitempty"`
Params int `json:"params"`
Operation string `json:"operation"`
err error
implicit
}
Expand Down Expand Up @@ -1472,6 +1507,8 @@ func (e *VectorAggregationExpr) Walk(f WalkFn) {
e.Left.Walk(f)
}

func (e *VectorAggregationExpr) Accept(v RootVisitor) { v.VisitVectorAggregation(e) }

// VectorMatchCardinality describes the cardinality relationship
// of two Vectors in a binary operation.
type VectorMatchCardinality int
Expand Down Expand Up @@ -1587,6 +1624,8 @@ func (e *BinOpExpr) Walk(f WalkFn) {
walkAll(f, e.SampleExpr, e.RHS)
}

func (e *BinOpExpr) Accept(v RootVisitor) { v.VisitBinOp(e) }

func mustNewBinOpExpr(op string, opts *BinOpOptions, lhs, rhs Expr) SampleExpr {
left, ok := lhs.(SampleExpr)
if !ok {
Expand Down Expand Up @@ -1886,7 +1925,7 @@ func MergeBinOp(op string, left, right *promql.Sample, swap, filter, isVectorCom
}

type LiteralExpr struct {
Val float64
Val float64 `json:"val"`
err error
implicit
}
Expand Down Expand Up @@ -1920,6 +1959,7 @@ func (e *LiteralExpr) Selector() (LogSelectorExpr, error) { return e, e.err
func (e *LiteralExpr) HasFilter() bool { return false }
func (e *LiteralExpr) Shardable() bool { return true }
func (e *LiteralExpr) Walk(f WalkFn) { f(e) }
func (e *LiteralExpr) Accept(v RootVisitor) { v.VisitLiteral(e) }
func (e *LiteralExpr) Pipeline() (log.Pipeline, error) { return log.NewNoopPipeline(), nil }
func (e *LiteralExpr) Matchers() []*labels.Matcher { return nil }
func (e *LiteralExpr) MatcherGroups() ([]MatcherRange, error) { return nil, e.err }
Expand Down Expand Up @@ -2016,6 +2056,8 @@ func (e *LabelReplaceExpr) Walk(f WalkFn) {
e.Left.Walk(f)
}

func (e *LabelReplaceExpr) Accept(v RootVisitor) { v.VisitLabelReplace(e) }

func (e *LabelReplaceExpr) String() string {
var sb strings.Builder
sb.WriteString(OpLabelReplace)
Expand Down Expand Up @@ -2143,6 +2185,7 @@ func (e *VectorExpr) Selector() (LogSelectorExpr, error) { return e, e.err
func (e *VectorExpr) HasFilter() bool { return false }
func (e *VectorExpr) Shardable() bool { return false }
func (e *VectorExpr) Walk(f WalkFn) { f(e) }
func (e *VectorExpr) Accept(v RootVisitor) { v.VisitVector(e) }
func (e *VectorExpr) Pipeline() (log.Pipeline, error) { return log.NewNoopPipeline(), nil }
func (e *VectorExpr) Matchers() []*labels.Matcher { return nil }
func (e *VectorExpr) MatcherGroups() ([]MatcherRange, error) { return nil, e.err }
Expand Down
4 changes: 4 additions & 0 deletions pkg/logql/syntax/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type Walkable interface {
Walk(f WalkFn)
}

type AcceptVisitor interface {
Accept(RootVisitor)
}

type RootVisitor interface {
SampleExprVisitor
LogSelectorExprVisitor
Expand Down

0 comments on commit cfa48c9

Please sign in to comment.