Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Update PromQL upto Jan 11, 2022.
Browse files Browse the repository at this point in the history
Signed-off-by: Harkishen-Singh <[email protected]>

This commit updates the pkg/promql package upto and including commit
c2b80d86437fc7766d65fd4f6b7503f0dd618be7
  • Loading branch information
Harkishen-Singh committed Jan 25, 2022
1 parent 9a81191 commit b57a759
Show file tree
Hide file tree
Showing 13 changed files with 745 additions and 463 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ require (
github.com/prometheus/client_golang v1.12.0
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.32.1
github.com/prometheus/prometheus v1.8.2-0.20211119115433-692a54649ed7
github.com/prometheus/prometheus v1.8.2-0.20220117154355-4855a0c067e2
github.com/schollz/progressbar/v3 v3.8.5
github.com/sergi/go-diff v1.2.0
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
Expand All @@ -56,4 +56,4 @@ require (
)

// Make sure Prometheus version is pinned as Prometheus semver does not include Go APIs.
replace github.com/prometheus/prometheus => github.com/prometheus/prometheus v1.8.2-0.20211119115433-692a54649ed7
replace github.com/prometheus/prometheus => github.com/prometheus/prometheus v1.8.2-0.20220117154355-4855a0c067e2
76 changes: 42 additions & 34 deletions go.sum

Large diffs are not rendered by default.

61 changes: 40 additions & 21 deletions pkg/promql/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,17 @@ type EngineOpts struct {
// a subquery in milliseconds if no step in range vector was specified `[30m:<step>]`.
NoStepSubqueryIntervalFn func(rangeMillis int64) int64

// EnableAtModifier if true enables @ modifier. Disabled otherwise.
// EnableAtModifier if true enables @ modifier. Disabled otherwise. This
// is supposed to be enabled for regular PromQL (as of Prometheus v2.33)
// but the option to disable it is still provided here for those using
// the Engine outside of Prometheus.
EnableAtModifier bool
// EnableNegativeOffset if true enables negative (-) offset values. Disabled otherwise.

// EnableNegativeOffset if true enables negative (-) offset
// values. Disabled otherwise. This is supposed to be enabled for
// regular PromQL (as of Prometheus v2.33) but the option to disable it
// is still provided here for those using the Engine outside of
// Prometheus.
EnableNegativeOffset bool
}

Expand Down Expand Up @@ -431,8 +439,10 @@ func (ng *Engine) newQuery(q Queryable, expr parser.Expr, start, end time.Time,
return qry, nil
}

var ErrValidationAtModifierDisabled = errors.New("@ modifier is disabled")
var ErrValidationNegativeOffsetDisabled = errors.New("negative offset is disabled")
var (
ErrValidationAtModifierDisabled = errors.New("@ modifier is disabled")
ErrValidationNegativeOffsetDisabled = errors.New("negative offset is disabled")
)

func (ng *Engine) validateOpts(expr parser.Expr) error {
if ng.enableAtModifier && ng.enableNegativeOffset {
Expand Down Expand Up @@ -1283,7 +1293,9 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, storage.Warnings) {
}

unwrapParenExpr(&e.Param)
if s, ok := unwrapStepInvariantExpr(e.Param).(*parser.StringLiteral); ok {
param := unwrapStepInvariantExpr(e.Param)
unwrapParenExpr(&param)
if s, ok := param.(*parser.StringLiteral); ok {
return ev.rangeEval(initSeries, func(v []parser.Value, sh [][]EvalSeriesHelper, enh *EvalNodeHelper) (Vector, storage.Warnings) {
return ev.aggregation(e.Op, sortedGrouping, e.Without, s.Val, v[0].(Vector), sh[0], enh), nil
}, e.Expr)
Expand All @@ -1304,6 +1316,7 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, storage.Warnings) {
// a vector selector.
unwrapParenExpr(&e.Args[0])
arg := unwrapStepInvariantExpr(e.Args[0])
unwrapParenExpr(&arg)
vs, ok := arg.(*parser.VectorSelector)
if ok {
return ev.rangeEval(nil, func(v []parser.Value, _ [][]EvalSeriesHelper, enh *EvalNodeHelper) (Vector, storage.Warnings) {
Expand All @@ -1327,6 +1340,7 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, storage.Warnings) {
for i := range e.Args {
unwrapParenExpr(&e.Args[i])
a := unwrapStepInvariantExpr(e.Args[i])
unwrapParenExpr(&a)
if _, ok := a.(*parser.MatrixSelector); ok {
matrixArgIndex = i
matrixArg = true
Expand Down Expand Up @@ -1369,7 +1383,10 @@ func (ev *evaluator) eval(expr parser.Expr) (parser.Value, storage.Warnings) {
}
}

sel := unwrapStepInvariantExpr(e.Args[matrixArgIndex]).(*parser.MatrixSelector)
unwrapParenExpr(&e.Args[matrixArgIndex])
arg := unwrapStepInvariantExpr(e.Args[matrixArgIndex])
unwrapParenExpr(&arg)
sel := arg.(*parser.MatrixSelector)
selVS := sel.VectorSelector.(*parser.VectorSelector)

ws, err := checkAndExpandSeriesSet(ev.ctx, sel)
Expand Down Expand Up @@ -1742,7 +1759,7 @@ func (ev *evaluator) vectorSelectorSingle(it *storage.MemoizedSeriesIterator, no
}

if ok {
t, v = it.Values()
t, v = it.At()
}

if !ok || t > refTime {
Expand Down Expand Up @@ -1862,7 +1879,7 @@ func (ev *evaluator) matrixIterSlice(it *storage.BufferedSeriesIterator, mint, m
}
// The seeked sample might also be in the range.
if ok {
t, v := it.Values()
t, v := it.At()
if t == maxt && !value.IsStaleNaN(v) {
if ev.currentSamples >= ev.maxSamples {
ev.error(ErrTooManySamples(env))
Expand Down Expand Up @@ -1903,9 +1920,11 @@ func (ev *evaluator) VectorOr(lhs, rhs Vector, matching *parser.VectorMatching,
panic("set operations must only use many-to-many matching")
}
if len(lhs) == 0 { // Short-circuit.
return rhs
enh.Out = append(enh.Out, rhs...)
return enh.Out
} else if len(rhs) == 0 {
return lhs
enh.Out = append(enh.Out, lhs...)
return enh.Out
}

leftSigs := map[string]struct{}{}
Expand All @@ -1930,7 +1949,8 @@ func (ev *evaluator) VectorUnless(lhs, rhs Vector, matching *parser.VectorMatchi
// Short-circuit: empty rhs means we will return everything in lhs;
// empty lhs means we will return empty - don't need to build a map.
if len(lhs) == 0 || len(rhs) == 0 {
return lhs
enh.Out = append(enh.Out, lhs...)
return enh.Out
}

rightSigs := map[string]struct{}{}
Expand Down Expand Up @@ -2198,6 +2218,8 @@ func scalarBinop(op parser.ItemType, lhs, rhs float64) float64 {
return btos(lhs >= rhs)
case parser.LTE:
return btos(lhs <= rhs)
case parser.ATAN2:
return math.Atan2(lhs, rhs)
}
panic(errors.Errorf("operator %q not allowed for Scalar operations", op))
}
Expand Down Expand Up @@ -2247,8 +2269,8 @@ type groupedAggregation struct {
// aggregation evaluates an aggregation operation on a Vector. The provided grouping labels
// must be sorted.
func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without bool, param interface{}, vec Vector, seriesHelper []EvalSeriesHelper, enh *EvalNodeHelper) Vector {

result := map[uint64]*groupedAggregation{}
orderedResult := []*groupedAggregation{}
var k int64
if op == parser.TOPK || op == parser.BOTTOMK {
f := param.(float64)
Expand Down Expand Up @@ -2317,12 +2339,16 @@ func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without
} else {
m = metric.WithLabels(grouping...)
}
result[groupingKey] = &groupedAggregation{
newAgg := &groupedAggregation{
labels: m,
value: s.V,
mean: s.V,
groupCount: 1,
}

result[groupingKey] = newAgg
orderedResult = append(orderedResult, newAgg)

inputVecLen := int64(len(vec))
resultSize := k
if k > inputVecLen {
Expand Down Expand Up @@ -2444,7 +2470,7 @@ func (ev *evaluator) aggregation(op parser.ItemType, grouping []string, without
}

// Construct the result Vector from the aggregated groups.
for _, aggr := range result {
for _, aggr := range orderedResult {
switch op {
case parser.AVG:
aggr.value = aggr.mean
Expand Down Expand Up @@ -2612,7 +2638,6 @@ func preprocessExprHelper(expr parser.Expr, start, end time.Time) bool {
}

if isStepInvariant {

// The function and all arguments are step invariant.
return true
}
Expand Down Expand Up @@ -2658,12 +2683,6 @@ func preprocessExprHelper(expr parser.Expr, start, end time.Time) bool {
}

func newStepInvariantExpr(expr parser.Expr) parser.Expr {
if e, ok := expr.(*parser.ParenExpr); ok {
// Wrapping the inside of () makes it easy to unwrap the paren later.
// But this effectively unwraps the paren.
return newStepInvariantExpr(e.Expr)

}
return &parser.StepInvariantExpr{Expr: expr}
}

Expand Down
Loading

0 comments on commit b57a759

Please sign in to comment.