cloudflare/pint
Publicmirrored from https://github.com/cloudflare/pintAvailable
internal/parser/utils/source.go
1048lines · modeblame
b4490cabLukasz Mierzwa1 years ago | 1 | package utils |
| 2 | | |
| 3 | import ( | |
13c2f1c2Lukasz Mierzwa1 years ago | 4 | "fmt" |
aaea75cfLukasz Mierzwa1 years ago | 5 | "math" |
f7d9a38aLukasz Mierzwa1 years ago | 6 | "regexp" |
b4490cabLukasz Mierzwa1 years ago | 7 | "slices" |
c269e3b9Lukasz Mierzwa1 years ago | 8 | "strconv" |
13c2f1c2Lukasz Mierzwa1 years ago | 9 | "strings" |
b4490cabLukasz Mierzwa1 years ago | 10 | |
| 11 | "github.com/prometheus/prometheus/model/labels" | |
| 12 | promParser "github.com/prometheus/prometheus/promql/parser" | |
1331c38cLukasz Mierzwa1 years ago | 13 | "github.com/prometheus/prometheus/promql/parser/posrange" |
b4490cabLukasz Mierzwa1 years ago | 14 | ) |
| 15 | | |
c269e3b9Lukasz Mierzwa1 years ago | 16 | var guaranteedLabelsMatches = []labels.MatchType{labels.MatchEqual, labels.MatchRegexp} |
| 17 | | |
b4490cabLukasz Mierzwa1 years ago | 18 | type SourceType int |
| 19 | | |
| 20 | const ( | |
| 21 | UnknownSource SourceType = iota | |
| 22 | NumberSource | |
| 23 | StringSource | |
| 24 | SelectorSource | |
| 25 | FuncSource | |
| 26 | AggregateSource | |
| 27 | ) | |
| 28 | | |
1331c38cLukasz Mierzwa1 years ago | 29 | type ExcludedLabel struct { |
| 30 | Reason string | |
f7d9a38aLukasz Mierzwa1 years ago | 31 | Fragment posrange.PositionRange |
1331c38cLukasz Mierzwa1 years ago | 32 | } |
| 33 | | |
c269e3b9Lukasz Mierzwa1 years ago | 34 | type Join struct { |
| 35 | Src Source | |
| 36 | } | |
| 37 | | |
1bca8feeLukasz Mierzwa1 years ago | 38 | // FIXME remove Selector/Call/Aggregation? |
| 39 | // Use a single parser.Node instead? | |
b4490cabLukasz Mierzwa1 years ago | 40 | type Source struct { |
c269e3b9Lukasz Mierzwa1 years ago | 41 | Selector *promParser.VectorSelector // Vector selector used for this source. |
| 42 | Call *promParser.Call // Most outer call used inside this source. | |
| 43 | Aggregation *promParser.AggregateExpr // Most outer aggregation expression used inside this source. | |
| 44 | ExcludeReason map[string]ExcludedLabel // Reason why a label was excluded | |
b4490cabLukasz Mierzwa1 years ago | 45 | Operation string |
c269e3b9Lukasz Mierzwa1 years ago | 46 | IsDeadReason string |
0fff8c1eLukasz Mierzwa1 years ago | 47 | Returns promParser.ValueType |
c269e3b9Lukasz Mierzwa1 years ago | 48 | Joins []Join // Any other sources this source joins with. |
| 49 | Unless []Join // Any other sources this source is suppressed by. | |
| 50 | IncludedLabels []string // Labels that are included by filters, they will be present if exist on source series (by). | |
| 51 | ExcludedLabels []string // Labels guaranteed to be excluded from the results (without). | |
| 52 | GuaranteedLabels []string // Labels guaranteed to be present on the results (matchers). | |
1bca8feeLukasz Mierzwa1 years ago | 53 | Position posrange.PositionRange |
| 54 | ReturnedNumber float64 // If AlwaysReturns=true this is the number that's returned | |
b4490cabLukasz Mierzwa1 years ago | 55 | Type SourceType |
12308218Lukasz Mierzwa1 years ago | 56 | FixedLabels bool // Labels are fixed and only allowed labels can be present. |
aaea75cfLukasz Mierzwa1 years ago | 57 | IsDead bool // True if this source cannot be reached and is dead code. |
| 58 | AlwaysReturns bool // True if this source always returns results. | |
73d0e49eLukasz Mierzwa1 years ago | 59 | KnownReturn bool // True if we always know the return value. |
17fb6517Lukasz Mierzwa1 years ago | 60 | IsConditional bool // True if this source is guarded by 'foo > 5' or other condition. |
b4490cabLukasz Mierzwa1 years ago | 61 | } |
| 62 | | |
c269e3b9Lukasz Mierzwa1 years ago | 63 | func (s Source) Fragment(expr string) string { |
| 64 | switch { | |
| 65 | case s.Type == FuncSource && s.Call != nil: | |
f7d9a38aLukasz Mierzwa1 years ago | 66 | return GetQueryFragment(expr, s.Call.PosRange) |
c269e3b9Lukasz Mierzwa1 years ago | 67 | case s.Call != nil: |
f7d9a38aLukasz Mierzwa1 years ago | 68 | return GetQueryFragment(expr, s.Call.PosRange) |
c269e3b9Lukasz Mierzwa1 years ago | 69 | case s.Type == AggregateSource && s.Aggregation != nil: |
f7d9a38aLukasz Mierzwa1 years ago | 70 | return GetQueryFragment(expr, s.Aggregation.PosRange) |
c269e3b9Lukasz Mierzwa1 years ago | 71 | case s.Selector != nil: |
f7d9a38aLukasz Mierzwa1 years ago | 72 | return GetQueryFragment(expr, s.Selector.PosRange) |
c269e3b9Lukasz Mierzwa1 years ago | 73 | default: |
| 74 | return "" | |
| 75 | } | |
| 76 | } | |
| 77 | | |
1bca8feeLukasz Mierzwa1 years ago | 78 | func (s Source) GetSmallestPosition() (pr posrange.PositionRange) { |
| 79 | pr.Start = s.Position.Start | |
| 80 | pr.End = s.Position.End | |
| 81 | | |
| 82 | if s.Selector != nil { | |
700dca72Lukasz Mierzwa1 years ago | 83 | if s.Selector.PosRange.Start > pr.Start { |
| 84 | pr.Start = s.Selector.PosRange.Start | |
| 85 | pr.End = s.Selector.PosRange.End | |
| 86 | } | |
1bca8feeLukasz Mierzwa1 years ago | 87 | } |
| 88 | if s.Call != nil { | |
700dca72Lukasz Mierzwa1 years ago | 89 | if s.Call.PosRange.Start > pr.Start { |
| 90 | pr.Start = s.Call.PosRange.Start | |
| 91 | pr.End = s.Call.PosRange.End | |
| 92 | } | |
1bca8feeLukasz Mierzwa1 years ago | 93 | } |
| 94 | if s.Aggregation != nil { | |
700dca72Lukasz Mierzwa1 years ago | 95 | if s.Aggregation.PosRange.Start > pr.Start { |
| 96 | pr.Start = s.Aggregation.PosRange.Start | |
| 97 | pr.End = s.Aggregation.PosRange.End | |
| 98 | } | |
1bca8feeLukasz Mierzwa1 years ago | 99 | } |
| 100 | return pr | |
| 101 | } | |
| 102 | | |
| 103 | func (s Source) CanHaveLabel(name string) bool { | |
| 104 | if slices.Contains(s.ExcludedLabels, name) { | |
| 105 | return false | |
| 106 | } | |
| 107 | if slices.Contains(s.IncludedLabels, name) { | |
| 108 | return true | |
| 109 | } | |
| 110 | if slices.Contains(s.GuaranteedLabels, name) { | |
| 111 | return true | |
| 112 | } | |
| 113 | return !s.FixedLabels | |
| 114 | } | |
| 115 | | |
91a38afdLukasz Mierzwa1 years ago | 116 | func (s Source) LabelExcludeReason(name string) ExcludedLabel { |
| 117 | if el, ok := s.ExcludeReason[name]; ok { | |
| 118 | return el | |
| 119 | } | |
b3b2247eLukasz Mierzwa1 years ago | 120 | return s.ExcludeReason[""] |
91a38afdLukasz Mierzwa1 years ago | 121 | } |
| 122 | | |
c269e3b9Lukasz Mierzwa1 years ago | 123 | type Visitor func(s Source) |
| 124 | | |
| 125 | func (s Source) WalkSources(fn Visitor) { | |
| 126 | fn(s) | |
| 127 | for _, j := range s.Joins { | |
| 128 | j.Src.WalkSources(fn) | |
| 129 | } | |
| 130 | for _, u := range s.Unless { | |
| 131 | u.Src.WalkSources(fn) | |
| 132 | } | |
| 133 | } | |
| 134 | | |
4b70d4afLukasz Mierzwa1 years ago | 135 | func LabelsSource(expr string, node promParser.Node) (src []Source) { |
f97e7fc2Lukasz Mierzwa1 years ago | 136 | return walkNode(expr, node) |
b4490cabLukasz Mierzwa1 years ago | 137 | } |
| 138 | | |
f97e7fc2Lukasz Mierzwa1 years ago | 139 | func walkNode(expr string, node promParser.Node) (src []Source) { |
| 140 | var s Source | |
b4490cabLukasz Mierzwa1 years ago | 141 | switch n := node.(type) { |
| 142 | case *promParser.AggregateExpr: | |
f97e7fc2Lukasz Mierzwa1 years ago | 143 | src = append(src, walkAggregation(expr, n)...) |
b4490cabLukasz Mierzwa1 years ago | 144 | |
| 145 | case *promParser.BinaryExpr: | |
f97e7fc2Lukasz Mierzwa1 years ago | 146 | src = append(src, parseBinOps(expr, n)...) |
b4490cabLukasz Mierzwa1 years ago | 147 | |
2bc6b9e5Lukasz Mierzwa1 years ago | 148 | case *promParser.Call: |
17fb6517Lukasz Mierzwa1 years ago | 149 | src = append(src, parseCall(expr, n)...) |
2bc6b9e5Lukasz Mierzwa1 years ago | 150 | |
b4490cabLukasz Mierzwa1 years ago | 151 | case *promParser.MatrixSelector: |
f97e7fc2Lukasz Mierzwa1 years ago | 152 | src = append(src, walkNode(expr, n.VectorSelector)...) |
b4490cabLukasz Mierzwa1 years ago | 153 | |
| 154 | case *promParser.SubqueryExpr: | |
f97e7fc2Lukasz Mierzwa1 years ago | 155 | src = append(src, walkNode(expr, n.Expr)...) |
b4490cabLukasz Mierzwa1 years ago | 156 | |
| 157 | case *promParser.NumberLiteral: | |
| 158 | s.Type = NumberSource | |
0fff8c1eLukasz Mierzwa1 years ago | 159 | s.Returns = promParser.ValueTypeScalar |
73d0e49eLukasz Mierzwa1 years ago | 160 | s.KnownReturn = true |
c269e3b9Lukasz Mierzwa1 years ago | 161 | s.ReturnedNumber = n.Val |
1887ad0aLukasz Mierzwa1 years ago | 162 | s.IncludedLabels = nil |
| 163 | s.GuaranteedLabels = nil | |
0fff8c1eLukasz Mierzwa1 years ago | 164 | s.FixedLabels = true |
aaea75cfLukasz Mierzwa1 years ago | 165 | s.AlwaysReturns = true |
1887ad0aLukasz Mierzwa1 years ago | 166 | s.ExcludeReason = setInMap( |
| 167 | s.ExcludeReason, | |
| 168 | "", | |
| 169 | ExcludedLabel{ | |
f7d9a38aLukasz Mierzwa1 years ago | 170 | Reason: "This query returns a number value with no labels.", |
| 171 | Fragment: n.PosRange, | |
1887ad0aLukasz Mierzwa1 years ago | 172 | }, |
| 173 | ) | |
1bca8feeLukasz Mierzwa1 years ago | 174 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 175 | src = append(src, s) |
b4490cabLukasz Mierzwa1 years ago | 176 | |
| 177 | case *promParser.ParenExpr: | |
f97e7fc2Lukasz Mierzwa1 years ago | 178 | src = append(src, walkNode(expr, n.Expr)...) |
b4490cabLukasz Mierzwa1 years ago | 179 | |
| 180 | case *promParser.StringLiteral: | |
| 181 | s.Type = StringSource | |
0fff8c1eLukasz Mierzwa1 years ago | 182 | s.Returns = promParser.ValueTypeString |
1887ad0aLukasz Mierzwa1 years ago | 183 | s.IncludedLabels = nil |
| 184 | s.GuaranteedLabels = nil | |
0fff8c1eLukasz Mierzwa1 years ago | 185 | s.FixedLabels = true |
aaea75cfLukasz Mierzwa1 years ago | 186 | s.AlwaysReturns = true |
1887ad0aLukasz Mierzwa1 years ago | 187 | s.ExcludeReason = setInMap( |
| 188 | s.ExcludeReason, | |
| 189 | "", | |
| 190 | ExcludedLabel{ | |
f7d9a38aLukasz Mierzwa1 years ago | 191 | Reason: "This query returns a string value with no labels.", |
| 192 | Fragment: n.PosRange, | |
1887ad0aLukasz Mierzwa1 years ago | 193 | }, |
| 194 | ) | |
1bca8feeLukasz Mierzwa1 years ago | 195 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 196 | src = append(src, s) |
b4490cabLukasz Mierzwa1 years ago | 197 | |
| 198 | case *promParser.UnaryExpr: | |
f97e7fc2Lukasz Mierzwa1 years ago | 199 | src = append(src, walkNode(expr, n.Expr)...) |
b4490cabLukasz Mierzwa1 years ago | 200 | |
| 201 | case *promParser.StepInvariantExpr: | |
| 202 | // Not possible to get this from the parser. | |
| 203 | | |
| 204 | case *promParser.VectorSelector: | |
| 205 | s.Type = SelectorSource | |
0fff8c1eLukasz Mierzwa1 years ago | 206 | s.Returns = promParser.ValueTypeVector |
bd876148Lukasz Mierzwa1 years ago | 207 | s.Selector = n |
17fb6517Lukasz Mierzwa1 years ago | 208 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, n)...) |
c0bbcf74Lukasz Mierzwa1 years ago | 209 | for _, name := range labelsWithEmptyValueSelector(n) { |
| 210 | s = excludeLabel(s, name) | |
| 211 | s.ExcludeReason = setInMap( | |
| 212 | s.ExcludeReason, | |
| 213 | name, | |
| 214 | ExcludedLabel{ | |
| 215 | Reason: fmt.Sprintf("Query uses `{%s=\"\"}` selector which will filter out any time series with the `%s` label set.", name, name), | |
f7d9a38aLukasz Mierzwa1 years ago | 216 | Fragment: n.PosRange, |
c0bbcf74Lukasz Mierzwa1 years ago | 217 | }, |
| 218 | ) | |
| 219 | } | |
1bca8feeLukasz Mierzwa1 years ago | 220 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 221 | src = append(src, s) |
b4490cabLukasz Mierzwa1 years ago | 222 | |
| 223 | default: | |
| 224 | // unhandled type | |
| 225 | } | |
f97e7fc2Lukasz Mierzwa1 years ago | 226 | return src |
b4490cabLukasz Mierzwa1 years ago | 227 | } |
2bc6b9e5Lukasz Mierzwa1 years ago | 228 | |
| 229 | func removeFromSlice(sl []string, s ...string) []string { | |
| 230 | for _, v := range s { | |
| 231 | idx := slices.Index(sl, v) | |
| 232 | if idx >= 0 { | |
| 233 | if len(sl) == 1 { | |
| 234 | return nil | |
| 235 | } | |
0545edccLukasz Mierzwa1 years ago | 236 | sl = slices.Delete(sl, idx, idx+1) |
2bc6b9e5Lukasz Mierzwa1 years ago | 237 | } |
| 238 | } | |
| 239 | return sl | |
| 240 | } | |
| 241 | | |
| 242 | func appendToSlice(dst []string, values ...string) []string { | |
| 243 | for _, v := range values { | |
| 244 | if !slices.Contains(dst, v) { | |
| 245 | dst = append(dst, v) | |
| 246 | } | |
| 247 | } | |
| 248 | return dst | |
| 249 | } | |
| 250 | | |
17fb6517Lukasz Mierzwa1 years ago | 251 | func includeLabel(s Source, names ...string) Source { |
| 252 | s.ExcludedLabels = removeFromSlice(s.ExcludedLabels, names...) | |
| 253 | for _, name := range names { | |
| 254 | delete(s.ExcludeReason, name) | |
| 255 | } | |
| 256 | s.IncludedLabels = appendToSlice(s.IncludedLabels, names...) | |
| 257 | return s | |
| 258 | } | |
| 259 | | |
91a38afdLukasz Mierzwa1 years ago | 260 | // Include labels that were not already excluded. |
73d0e49eLukasz Mierzwa1 years ago | 261 | // FIXME most should use this? |
91a38afdLukasz Mierzwa1 years ago | 262 | func maybeIncludeLabel(s Source, names ...string) Source { |
| 263 | for _, name := range names { | |
| 264 | if !slices.Contains(s.ExcludedLabels, name) { | |
| 265 | s.IncludedLabels = appendToSlice(s.IncludedLabels, names...) | |
| 266 | } | |
| 267 | } | |
| 268 | return s | |
| 269 | } | |
| 270 | | |
993781f3Lukasz Mierzwa1 years ago | 271 | func restrictIncludedLabels(s Source, names []string) Source { |
| 272 | todo := []string{} | |
| 273 | for _, name := range s.IncludedLabels { | |
| 274 | if !slices.Contains(names, name) { | |
| 275 | todo = append(todo, name) | |
| 276 | } | |
| 277 | } | |
| 278 | s.IncludedLabels = removeFromSlice(s.IncludedLabels, todo...) | |
| 279 | return s | |
| 280 | } | |
| 281 | | |
17fb6517Lukasz Mierzwa1 years ago | 282 | func guaranteeLabel(s Source, names ...string) Source { |
| 283 | s.ExcludedLabels = removeFromSlice(s.ExcludedLabels, names...) | |
| 284 | for _, name := range names { | |
| 285 | delete(s.ExcludeReason, name) | |
| 286 | } | |
| 287 | s.GuaranteedLabels = appendToSlice(s.GuaranteedLabels, names...) | |
| 288 | return s | |
| 289 | } | |
| 290 | | |
c269e3b9Lukasz Mierzwa1 years ago | 291 | func restrictGuaranteedLabels(s Source, names []string) Source { |
| 292 | todo := []string{} | |
| 293 | for _, name := range s.GuaranteedLabels { | |
| 294 | if !slices.Contains(names, name) { | |
| 295 | todo = append(todo, name) | |
| 296 | } | |
| 297 | } | |
| 298 | s.GuaranteedLabels = removeFromSlice(s.GuaranteedLabels, todo...) | |
| 299 | return s | |
| 300 | } | |
| 301 | | |
17fb6517Lukasz Mierzwa1 years ago | 302 | func excludeLabel(s Source, names ...string) Source { |
| 303 | s.ExcludedLabels = appendToSlice(s.ExcludedLabels, names...) | |
| 304 | s.IncludedLabels = removeFromSlice(s.IncludedLabels, names...) | |
| 305 | s.GuaranteedLabels = removeFromSlice(s.GuaranteedLabels, names...) | |
| 306 | return s | |
| 307 | } | |
| 308 | | |
1331c38cLukasz Mierzwa1 years ago | 309 | func setInMap(dst map[string]ExcludedLabel, key string, val ExcludedLabel) map[string]ExcludedLabel { |
13c2f1c2Lukasz Mierzwa1 years ago | 310 | if dst == nil { |
1331c38cLukasz Mierzwa1 years ago | 311 | dst = map[string]ExcludedLabel{} |
13c2f1c2Lukasz Mierzwa1 years ago | 312 | } |
| 313 | dst[key] = val | |
| 314 | return dst | |
| 315 | } | |
| 316 | | |
bd876148Lukasz Mierzwa1 years ago | 317 | func labelsFromSelectors(matches []labels.MatchType, selector *promParser.VectorSelector) (names []string) { |
e718edd9Lukasz Mierzwa1 years ago | 318 | if selector == nil { |
| 319 | return nil | |
| 320 | } | |
bd876148Lukasz Mierzwa1 years ago | 321 | // Any label used in positive filters is gurnateed to be present. |
| 322 | for _, lm := range selector.LabelMatchers { | |
| 323 | if lm.Name == labels.MetricName { | |
| 324 | continue | |
| 325 | } | |
| 326 | if !slices.Contains(matches, lm.Type) { | |
| 327 | continue | |
| 328 | } | |
| 329 | names = appendToSlice(names, lm.Name) | |
c0bbcf74Lukasz Mierzwa1 years ago | 330 | } |
| 331 | return names | |
| 332 | } | |
| 333 | | |
| 334 | func labelsWithEmptyValueSelector(selector *promParser.VectorSelector) (names []string) { | |
| 335 | for _, lm := range selector.LabelMatchers { | |
| 336 | if lm.Name == labels.MetricName { | |
| 337 | continue | |
| 338 | } | |
| 339 | if lm.Type == labels.MatchEqual && lm.Value == "" { | |
| 340 | names = appendToSlice(names, lm.Name) | |
0fff8c1eLukasz Mierzwa1 years ago | 341 | } |
| 342 | } | |
| 343 | return names | |
| 344 | } | |
| 345 | | |
f7d9a38aLukasz Mierzwa1 years ago | 346 | func GetQueryFragment(expr string, pos posrange.PositionRange) string { |
1331c38cLukasz Mierzwa1 years ago | 347 | return expr[pos.Start:pos.End] |
| 348 | } | |
| 349 | | |
c269e3b9Lukasz Mierzwa1 years ago | 350 | // FIXME Aggregations strip __name__. |
f97e7fc2Lukasz Mierzwa1 years ago | 351 | func walkAggregation(expr string, n *promParser.AggregateExpr) (src []Source) { |
| 352 | var s Source | |
| 353 | switch n.Op { | |
| 354 | case promParser.SUM: | |
| 355 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 356 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 357 | s.Operation = "sum" |
1bca8feeLukasz Mierzwa1 years ago | 358 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 359 | src = append(src, s) |
13c2f1c2Lukasz Mierzwa1 years ago | 360 | } |
f97e7fc2Lukasz Mierzwa1 years ago | 361 | case promParser.MIN: |
| 362 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 363 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 364 | s.Operation = "min" |
1bca8feeLukasz Mierzwa1 years ago | 365 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 366 | src = append(src, s) |
| 367 | } | |
| 368 | case promParser.MAX: | |
| 369 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 370 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 371 | s.Operation = "max" |
1bca8feeLukasz Mierzwa1 years ago | 372 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 373 | src = append(src, s) |
| 374 | } | |
| 375 | case promParser.AVG: | |
| 376 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 377 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 378 | s.Operation = "avg" |
1bca8feeLukasz Mierzwa1 years ago | 379 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 380 | src = append(src, s) |
| 381 | } | |
| 382 | case promParser.GROUP: | |
| 383 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 384 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 385 | s.Operation = "group" |
1bca8feeLukasz Mierzwa1 years ago | 386 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 387 | src = append(src, s) |
| 388 | } | |
| 389 | case promParser.STDDEV: | |
| 390 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 391 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 392 | s.Operation = "stddev" |
1bca8feeLukasz Mierzwa1 years ago | 393 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 394 | src = append(src, s) |
| 395 | } | |
| 396 | case promParser.STDVAR: | |
| 397 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 398 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 399 | s.Operation = "stdvar" |
1bca8feeLukasz Mierzwa1 years ago | 400 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 401 | src = append(src, s) |
| 402 | } | |
| 403 | case promParser.COUNT: | |
| 404 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 405 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 406 | s.Operation = "count" |
1bca8feeLukasz Mierzwa1 years ago | 407 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 408 | src = append(src, s) |
| 409 | } | |
| 410 | case promParser.COUNT_VALUES: | |
| 411 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 412 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 413 | s.Operation = "count_values" |
| 414 | // Param is the label to store the count value in. | |
17fb6517Lukasz Mierzwa1 years ago | 415 | s = includeLabel(s, n.Param.(*promParser.StringLiteral).Val) |
| 416 | s = guaranteeLabel(s, n.Param.(*promParser.StringLiteral).Val) | |
1bca8feeLukasz Mierzwa1 years ago | 417 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 418 | src = append(src, s) |
| 419 | } | |
| 420 | case promParser.QUANTILE: | |
| 421 | for _, s = range parseAggregation(expr, n) { | |
c269e3b9Lukasz Mierzwa1 years ago | 422 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 423 | s.Operation = "quantile" |
1bca8feeLukasz Mierzwa1 years ago | 424 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 425 | src = append(src, s) |
| 426 | } | |
| 427 | case promParser.TOPK: | |
| 428 | for _, s = range walkNode(expr, n.Expr) { | |
| 429 | s.Type = AggregateSource | |
c269e3b9Lukasz Mierzwa1 years ago | 430 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 431 | s.Operation = "topk" |
1bca8feeLukasz Mierzwa1 years ago | 432 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 433 | src = append(src, s) |
| 434 | } | |
| 435 | case promParser.BOTTOMK: | |
| 436 | for _, s = range walkNode(expr, n.Expr) { | |
| 437 | s.Type = AggregateSource | |
c269e3b9Lukasz Mierzwa1 years ago | 438 | s.Aggregation = n |
f97e7fc2Lukasz Mierzwa1 years ago | 439 | s.Operation = "bottomk" |
1bca8feeLukasz Mierzwa1 years ago | 440 | s.Position = n.PosRange |
f97e7fc2Lukasz Mierzwa1 years ago | 441 | src = append(src, s) |
| 442 | } | |
| 443 | /* | |
| 444 | TODO these are experimental and promParser.EnableExperimentalFunctions must be set to true to enable parsing of these. | |
| 445 | case promParser.LIMITK: | |
| 446 | s = walkNode(expr, n.Expr) | |
| 447 | s.Type = AggregateSource | |
| 448 | s.Operation = "limitk" | |
| 449 | case promParser.LIMIT_RATIO: | |
| 450 | s = walkNode(expr, n.Expr) | |
| 451 | s.Type = AggregateSource | |
| 452 | s.Operation = "limit_ratio" | |
| 453 | */ | |
| 454 | } | |
| 455 | return src | |
| 456 | } | |
| 457 | | |
| 458 | func parseAggregation(expr string, n *promParser.AggregateExpr) (src []Source) { | |
| 459 | var s Source | |
| 460 | for _, s = range walkNode(expr, n.Expr) { | |
| 461 | if n.Without { | |
17fb6517Lukasz Mierzwa1 years ago | 462 | s = excludeLabel(s, n.Grouping...) |
2bc6b9e5Lukasz Mierzwa1 years ago | 463 | for _, name := range n.Grouping { |
f97e7fc2Lukasz Mierzwa1 years ago | 464 | s.ExcludeReason = setInMap( |
| 465 | s.ExcludeReason, | |
| 466 | name, | |
| 467 | ExcludedLabel{ | |
| 468 | Reason: fmt.Sprintf("Query is using aggregation with `without(%s)`, all labels included inside `without(...)` will be removed from the results.", | |
| 469 | strings.Join(n.Grouping, ", ")), | |
b7f6e358Lukasz Mierzwa1 years ago | 470 | Fragment: FindPosition(expr, n.PosRange, "without"), |
f97e7fc2Lukasz Mierzwa1 years ago | 471 | }, |
| 472 | ) | |
| 473 | } | |
| 474 | } else { | |
| 475 | if len(n.Grouping) == 0 { | |
| 476 | s.IncludedLabels = nil | |
| 477 | s.GuaranteedLabels = nil | |
| 478 | s.ExcludeReason = setInMap( | |
| 479 | s.ExcludeReason, | |
| 480 | "", | |
| 481 | ExcludedLabel{ | |
| 482 | Reason: "Query is using aggregation that removes all labels.", | |
b7f6e358Lukasz Mierzwa1 years ago | 483 | Fragment: FindPosition(expr, n.PosRange, "sum"), |
f97e7fc2Lukasz Mierzwa1 years ago | 484 | }, |
| 485 | ) | |
| 486 | } else { | |
1887ad0aLukasz Mierzwa1 years ago | 487 | // Check if source of labels already fixes them. |
| 488 | if !s.FixedLabels { | |
91a38afdLukasz Mierzwa1 years ago | 489 | s = maybeIncludeLabel(s, n.Grouping...) |
1887ad0aLukasz Mierzwa1 years ago | 490 | s.ExcludeReason = setInMap( |
| 491 | s.ExcludeReason, | |
| 492 | "", | |
| 493 | ExcludedLabel{ | |
| 494 | Reason: fmt.Sprintf("Query is using aggregation with `by(%s)`, only labels included inside `by(...)` will be present on the results.", | |
| 495 | strings.Join(n.Grouping, ", ")), | |
b7f6e358Lukasz Mierzwa1 years ago | 496 | Fragment: FindPosition(expr, n.PosRange, "by"), |
1887ad0aLukasz Mierzwa1 years ago | 497 | }, |
| 498 | ) | |
| 499 | } | |
c269e3b9Lukasz Mierzwa1 years ago | 500 | s = restrictGuaranteedLabels(s, n.Grouping) |
45b5cf5eLukasz Mierzwa1 years ago | 501 | s = restrictIncludedLabels(s, n.Grouping) |
2bc6b9e5Lukasz Mierzwa1 years ago | 502 | } |
1887ad0aLukasz Mierzwa1 years ago | 503 | s.FixedLabels = true |
2bc6b9e5Lukasz Mierzwa1 years ago | 504 | } |
f97e7fc2Lukasz Mierzwa1 years ago | 505 | s.Type = AggregateSource |
| 506 | s.Returns = promParser.ValueTypeVector | |
| 507 | src = append(src, s) | |
2bc6b9e5Lukasz Mierzwa1 years ago | 508 | } |
f97e7fc2Lukasz Mierzwa1 years ago | 509 | return src |
2bc6b9e5Lukasz Mierzwa1 years ago | 510 | } |
| 511 | | |
17fb6517Lukasz Mierzwa1 years ago | 512 | func parsePromQLFunc(s Source, expr string, n *promParser.Call) Source { |
0fff8c1eLukasz Mierzwa1 years ago | 513 | switch n.Func.Name { |
| 514 | case "abs", "sgn", "acos", "acosh", "asin", "asinh", "atan", "atanh", "cos", "cosh", "sin", "sinh", "tan", "tanh": | |
| 515 | // No change to labels. | |
| 516 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 517 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 518 | |
| 519 | case "ceil", "floor", "round": | |
| 520 | // No change to labels. | |
| 521 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 522 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 523 | |
| 524 | case "changes", "resets": | |
| 525 | // No change to labels. | |
| 526 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 527 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 528 | |
| 529 | case "clamp", "clamp_max", "clamp_min": | |
| 530 | // No change to labels. | |
| 531 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 532 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 533 | |
| 534 | case "absent", "absent_over_time": | |
| 535 | s.Returns = promParser.ValueTypeVector | |
2bc6b9e5Lukasz Mierzwa1 years ago | 536 | s.FixedLabels = true |
17fb6517Lukasz Mierzwa1 years ago | 537 | s.IncludedLabels = nil |
| 538 | s.GuaranteedLabels = nil | |
bd876148Lukasz Mierzwa1 years ago | 539 | for _, name := range labelsFromSelectors([]labels.MatchType{labels.MatchEqual}, s.Selector) { |
17fb6517Lukasz Mierzwa1 years ago | 540 | s = includeLabel(s, name) |
| 541 | s = guaranteeLabel(s, name) | |
2bc6b9e5Lukasz Mierzwa1 years ago | 542 | } |
1887ad0aLukasz Mierzwa1 years ago | 543 | s.ExcludeReason = setInMap( |
| 544 | s.ExcludeReason, | |
| 545 | "", | |
| 546 | ExcludedLabel{ | |
| 547 | Reason: fmt.Sprintf(`The [%s()](https://prometheus.io/docs/prometheus/latest/querying/functions/#%s) function is used to check if provided query doesn't match any time series. | |
| 548 | You will only get any results back if the metric selector you pass doesn't match anything. | |
| 549 | Since there are no matching time series there are also no labels. If some time series is missing you cannot read its labels. | |
| 550 | This means that the only labels you can get back from absent call are the ones you pass to it. | |
| 551 | If you're hoping to get instance specific labels this way and alert when some target is down then that won't work, use the `+"`up`"+` metric instead.`, | |
| 552 | n.Func.Name, n.Func.Name), | |
b7f6e358Lukasz Mierzwa1 years ago | 553 | Fragment: FindPosition(expr, n.PosRange, n.Func.Name), |
1887ad0aLukasz Mierzwa1 years ago | 554 | }, |
| 555 | ) | |
0fff8c1eLukasz Mierzwa1 years ago | 556 | |
| 557 | case "avg_over_time", "count_over_time", "last_over_time", "max_over_time", "min_over_time", "present_over_time", "quantile_over_time", "stddev_over_time", "stdvar_over_time", "sum_over_time": | |
| 558 | // No change to labels. | |
| 559 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 560 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 561 | |
| 562 | case "days_in_month", "day_of_month", "day_of_week", "day_of_year", "hour", "minute", "month", "year": | |
| 563 | s.Returns = promParser.ValueTypeVector | |
| 564 | // No labels if we don't pass any arguments. | |
| 565 | // Otherwise no change to labels. | |
| 566 | if len(s.Call.Args) == 0 { | |
| 567 | s.FixedLabels = true | |
aaea75cfLukasz Mierzwa1 years ago | 568 | s.AlwaysReturns = true |
1887ad0aLukasz Mierzwa1 years ago | 569 | s.IncludedLabels = nil |
| 570 | s.GuaranteedLabels = nil | |
| 571 | s.ExcludeReason = setInMap( | |
| 572 | s.ExcludeReason, | |
| 573 | "", | |
| 574 | ExcludedLabel{ | |
| 575 | Reason: fmt.Sprintf("Calling `%s()` with no arguments will return an empty time series with no labels.", | |
| 576 | n.Func.Name), | |
f7d9a38aLukasz Mierzwa1 years ago | 577 | Fragment: n.PosRange, |
1887ad0aLukasz Mierzwa1 years ago | 578 | }, |
| 579 | ) | |
0fff8c1eLukasz Mierzwa1 years ago | 580 | } else { |
bd876148Lukasz Mierzwa1 years ago | 581 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 582 | } |
| 583 | | |
| 584 | case "deg", "rad", "ln", "log10", "log2", "sqrt", "exp": | |
| 585 | // No change to labels. | |
| 586 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 587 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 588 | |
| 589 | case "delta", "idelta", "increase", "deriv", "irate", "rate": | |
| 590 | // No change to labels. | |
| 591 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 592 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 593 | |
| 594 | case "histogram_avg", "histogram_count", "histogram_sum", "histogram_stddev", "histogram_stdvar", "histogram_fraction", "histogram_quantile": | |
| 595 | // No change to labels. | |
| 596 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 597 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 598 | |
| 599 | case "holt_winters", "predict_linear": | |
| 600 | // No change to labels. | |
| 601 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 602 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 603 | |
| 604 | case "label_replace", "label_join": | |
| 605 | // One label added to the results. | |
| 606 | s.Returns = promParser.ValueTypeVector | |
6144bc7bLukasz Mierzwa1 years ago | 607 | s = guaranteeLabel(s, n.Args[1].(*promParser.StringLiteral).Val) |
0fff8c1eLukasz Mierzwa1 years ago | 608 | |
| 609 | case "pi": | |
| 610 | s.Returns = promParser.ValueTypeScalar | |
1887ad0aLukasz Mierzwa1 years ago | 611 | s.IncludedLabels = nil |
| 612 | s.GuaranteedLabels = nil | |
0fff8c1eLukasz Mierzwa1 years ago | 613 | s.FixedLabels = true |
aaea75cfLukasz Mierzwa1 years ago | 614 | s.AlwaysReturns = true |
1887ad0aLukasz Mierzwa1 years ago | 615 | s.ExcludeReason = setInMap( |
| 616 | s.ExcludeReason, | |
| 617 | "", | |
| 618 | ExcludedLabel{ | |
| 619 | Reason: fmt.Sprintf("Calling `%s()` will return a scalar value with no labels.", n.Func.Name), | |
f7d9a38aLukasz Mierzwa1 years ago | 620 | Fragment: n.PosRange, |
1887ad0aLukasz Mierzwa1 years ago | 621 | }, |
| 622 | ) | |
0fff8c1eLukasz Mierzwa1 years ago | 623 | |
| 624 | case "scalar": | |
| 625 | s.Returns = promParser.ValueTypeScalar | |
1887ad0aLukasz Mierzwa1 years ago | 626 | s.IncludedLabels = nil |
| 627 | s.GuaranteedLabels = nil | |
0fff8c1eLukasz Mierzwa1 years ago | 628 | s.FixedLabels = true |
aaea75cfLukasz Mierzwa1 years ago | 629 | s.AlwaysReturns = true |
1887ad0aLukasz Mierzwa1 years ago | 630 | s.ExcludeReason = setInMap( |
| 631 | s.ExcludeReason, | |
| 632 | "", | |
| 633 | ExcludedLabel{ | |
| 634 | Reason: fmt.Sprintf("Calling `%s()` will return a scalar value with no labels.", n.Func.Name), | |
b7f6e358Lukasz Mierzwa1 years ago | 635 | Fragment: FindPosition(expr, n.PositionRange(), n.Func.Name), |
1887ad0aLukasz Mierzwa1 years ago | 636 | }, |
| 637 | ) | |
0fff8c1eLukasz Mierzwa1 years ago | 638 | |
| 639 | case "sort", "sort_desc": | |
| 640 | // No change to labels. | |
| 641 | s.Returns = promParser.ValueTypeVector | |
| 642 | | |
| 643 | case "time": | |
| 644 | s.Returns = promParser.ValueTypeScalar | |
1887ad0aLukasz Mierzwa1 years ago | 645 | s.IncludedLabels = nil |
| 646 | s.GuaranteedLabels = nil | |
0fff8c1eLukasz Mierzwa1 years ago | 647 | s.FixedLabels = true |
aaea75cfLukasz Mierzwa1 years ago | 648 | s.AlwaysReturns = true |
1887ad0aLukasz Mierzwa1 years ago | 649 | s.ExcludeReason = setInMap( |
| 650 | s.ExcludeReason, | |
| 651 | "", | |
| 652 | ExcludedLabel{ | |
| 653 | Reason: fmt.Sprintf("Calling `%s()` will return a scalar value with no labels.", n.Func.Name), | |
f7d9a38aLukasz Mierzwa1 years ago | 654 | Fragment: n.PosRange, |
1887ad0aLukasz Mierzwa1 years ago | 655 | }, |
| 656 | ) | |
0fff8c1eLukasz Mierzwa1 years ago | 657 | |
| 658 | case "timestamp": | |
| 659 | // No change to labels. | |
| 660 | s.Returns = promParser.ValueTypeVector | |
bd876148Lukasz Mierzwa1 years ago | 661 | s = guaranteeLabel(s, labelsFromSelectors(guaranteedLabelsMatches, s.Selector)...) |
0fff8c1eLukasz Mierzwa1 years ago | 662 | |
| 663 | case "vector": | |
| 664 | s.Returns = promParser.ValueTypeVector | |
1887ad0aLukasz Mierzwa1 years ago | 665 | s.IncludedLabels = nil |
| 666 | s.GuaranteedLabels = nil | |
0fff8c1eLukasz Mierzwa1 years ago | 667 | s.FixedLabels = true |
aaea75cfLukasz Mierzwa1 years ago | 668 | s.AlwaysReturns = true |
f7d9a38aLukasz Mierzwa1 years ago | 669 | for _, vs := range walkNode(expr, n.Args[0]) { |
73d0e49eLukasz Mierzwa1 years ago | 670 | if vs.KnownReturn { |
f7d9a38aLukasz Mierzwa1 years ago | 671 | s.ReturnedNumber = vs.ReturnedNumber |
73d0e49eLukasz Mierzwa1 years ago | 672 | s.KnownReturn = true |
f7d9a38aLukasz Mierzwa1 years ago | 673 | } |
f481b96aLukasz Mierzwa1 years ago | 674 | } |
1887ad0aLukasz Mierzwa1 years ago | 675 | s.ExcludeReason = setInMap( |
| 676 | s.ExcludeReason, | |
| 677 | "", | |
| 678 | ExcludedLabel{ | |
| 679 | Reason: fmt.Sprintf("Calling `%s()` will return a vector value with no labels.", n.Func.Name), | |
b7f6e358Lukasz Mierzwa1 years ago | 680 | Fragment: FindPosition(expr, n.PosRange, n.Func.Name), |
1887ad0aLukasz Mierzwa1 years ago | 681 | }, |
| 682 | ) | |
0fff8c1eLukasz Mierzwa1 years ago | 683 | |
| 684 | default: | |
| 685 | // Unsupported function | |
| 686 | s.Returns = promParser.ValueTypeNone | |
| 687 | s.Call = nil | |
2bc6b9e5Lukasz Mierzwa1 years ago | 688 | } |
| 689 | return s | |
| 690 | } | |
13c2f1c2Lukasz Mierzwa1 years ago | 691 | |
17fb6517Lukasz Mierzwa1 years ago | 692 | func parseCall(expr string, n *promParser.Call) (src []Source) { |
| 693 | var vt promParser.ValueType | |
| 694 | for i, e := range n.Args { | |
| 695 | if i >= len(n.Func.ArgTypes) { | |
| 696 | vt = n.Func.ArgTypes[len(n.Func.ArgTypes)-1] | |
| 697 | } else { | |
| 698 | vt = n.Func.ArgTypes[i] | |
| 699 | } | |
| 700 | | |
| 701 | switch vt { | |
| 702 | case promParser.ValueTypeVector, promParser.ValueTypeMatrix: | |
| 703 | for _, es := range walkNode(expr, e) { | |
| 704 | es.Type = FuncSource | |
| 705 | es.Operation = n.Func.Name | |
| 706 | es.Call = n | |
| 707 | src = append(src, parsePromQLFunc(es, expr, n)) | |
| 708 | } | |
| 709 | case promParser.ValueTypeNone, promParser.ValueTypeScalar, promParser.ValueTypeString: | |
| 710 | } | |
| 711 | } | |
| 712 | | |
| 713 | if len(src) == 0 { | |
| 714 | var s Source | |
| 715 | s.Type = FuncSource | |
| 716 | s.Operation = n.Func.Name | |
| 717 | s.Call = n | |
1bca8feeLukasz Mierzwa1 years ago | 718 | s.Position = n.PosRange |
17fb6517Lukasz Mierzwa1 years ago | 719 | src = append(src, parsePromQLFunc(s, expr, n)) |
| 720 | } | |
| 721 | | |
| 722 | return src | |
| 723 | } | |
| 724 | | |
f97e7fc2Lukasz Mierzwa1 years ago | 725 | func parseBinOps(expr string, n *promParser.BinaryExpr) (src []Source) { |
| 726 | var s Source | |
13c2f1c2Lukasz Mierzwa1 years ago | 727 | switch { |
aaea75cfLukasz Mierzwa1 years ago | 728 | |
| 729 | // foo{} + 1 | |
| 730 | // 1 + foo{} | |
| 731 | // foo{} > 1 | |
| 732 | // 1 > foo{} | |
13c2f1c2Lukasz Mierzwa1 years ago | 733 | case n.VectorMatching == nil: |
aaea75cfLukasz Mierzwa1 years ago | 734 | lhs := walkNode(expr, n.LHS) |
| 735 | rhs := walkNode(expr, n.RHS) | |
| 736 | for _, ls := range lhs { | |
bf45639fLukasz Mierzwa1 years ago | 737 | ls.IsConditional = isConditional(ls, n.Op) |
aaea75cfLukasz Mierzwa1 years ago | 738 | for _, rs := range rhs { |
bf45639fLukasz Mierzwa1 years ago | 739 | rs.IsConditional = isConditional(rs, n.Op) |
171c7f7eLukasz Mierzwa1 years ago | 740 | var side Source |
aaea75cfLukasz Mierzwa1 years ago | 741 | switch { |
171c7f7eLukasz Mierzwa1 years ago | 742 | case ls.Returns == promParser.ValueTypeVector, ls.Returns == promParser.ValueTypeMatrix: |
| 743 | // Use labels from LHS | |
| 744 | side = ls | |
| 745 | case rs.Returns == promParser.ValueTypeVector, rs.Returns == promParser.ValueTypeMatrix: | |
| 746 | // Use labels from RHS | |
| 747 | side = rs | |
| 748 | default: | |
| 749 | side = ls | |
| 750 | } | |
| 751 | if ls.AlwaysReturns && rs.AlwaysReturns && ls.KnownReturn && rs.KnownReturn { | |
aaea75cfLukasz Mierzwa1 years ago | 752 | // Both sides always return something |
171c7f7eLukasz Mierzwa1 years ago | 753 | side.ReturnedNumber, side.IsDead, side.IsDeadReason = calculateStaticReturn( |
c269e3b9Lukasz Mierzwa1 years ago | 754 | expr, |
| 755 | ls, rs, | |
| 756 | n.Op, | |
| 757 | ls.IsDead, | |
| 758 | ) | |
1887ad0aLukasz Mierzwa1 years ago | 759 | } |
171c7f7eLukasz Mierzwa1 years ago | 760 | src = append(src, side) |
1887ad0aLukasz Mierzwa1 years ago | 761 | } |
| 762 | } | |
13c2f1c2Lukasz Mierzwa1 years ago | 763 | |
128fada0Lukasz Mierzwa1 years ago | 764 | // foo{} + bar{} |
| 765 | // foo{} + on(...) bar{} | |
| 766 | // foo{} + ignoring(...) bar{} | |
17fb6517Lukasz Mierzwa1 years ago | 767 | // foo{} / bar{} |
13c2f1c2Lukasz Mierzwa1 years ago | 768 | case n.VectorMatching.Card == promParser.CardOneToOne: |
17fb6517Lukasz Mierzwa1 years ago | 769 | rhs := walkNode(expr, n.RHS) |
f97e7fc2Lukasz Mierzwa1 years ago | 770 | for _, s = range walkNode(expr, n.LHS) { |
| 771 | if n.VectorMatching.On { | |
| 772 | s.FixedLabels = true | |
17fb6517Lukasz Mierzwa1 years ago | 773 | s = includeLabel(s, n.VectorMatching.MatchingLabels...) |
c0e1a8c4Lukasz Mierzwa1 years ago | 774 | s = restrictIncludedLabels(s, n.VectorMatching.MatchingLabels) |
| 775 | s = restrictGuaranteedLabels(s, n.VectorMatching.MatchingLabels) | |
1331c38cLukasz Mierzwa1 years ago | 776 | s.ExcludeReason = setInMap( |
| 777 | s.ExcludeReason, | |
f97e7fc2Lukasz Mierzwa1 years ago | 778 | "", |
1331c38cLukasz Mierzwa1 years ago | 779 | ExcludedLabel{ |
| 780 | Reason: fmt.Sprintf( | |
f97e7fc2Lukasz Mierzwa1 years ago | 781 | "Query is using %s vector matching with `on(%s)`, only labels included inside `on(...)` will be present on the results.", |
1331c38cLukasz Mierzwa1 years ago | 782 | n.VectorMatching.Card, strings.Join(n.VectorMatching.MatchingLabels, ", "), |
| 783 | ), | |
b7f6e358Lukasz Mierzwa1 years ago | 784 | Fragment: FindPosition(expr, n.PositionRange(), "on"), |
1331c38cLukasz Mierzwa1 years ago | 785 | }, |
| 786 | ) | |
f97e7fc2Lukasz Mierzwa1 years ago | 787 | } else { |
17fb6517Lukasz Mierzwa1 years ago | 788 | s = excludeLabel(s, n.VectorMatching.MatchingLabels...) |
f97e7fc2Lukasz Mierzwa1 years ago | 789 | for _, name := range n.VectorMatching.MatchingLabels { |
| 790 | s.ExcludeReason = setInMap( | |
| 791 | s.ExcludeReason, | |
| 792 | name, | |
| 793 | ExcludedLabel{ | |
| 794 | Reason: fmt.Sprintf( | |
| 795 | "Query is using %s vector matching with `ignoring(%s)`, all labels included inside `ignoring(...)` will be removed on the results.", | |
| 796 | n.VectorMatching.Card, strings.Join(n.VectorMatching.MatchingLabels, ", "), | |
| 797 | ), | |
b7f6e358Lukasz Mierzwa1 years ago | 798 | Fragment: FindPosition(expr, n.PositionRange(), "ignoring"), |
f97e7fc2Lukasz Mierzwa1 years ago | 799 | }, |
| 800 | ) | |
| 801 | } | |
993781f3Lukasz Mierzwa1 years ago | 802 | for _, rs := range rhs { |
bf45639fLukasz Mierzwa1 years ago | 803 | rs.IsConditional = isConditional(rs, n.Op) |
73d0e49eLukasz Mierzwa1 years ago | 804 | if s.AlwaysReturns && rs.AlwaysReturns && s.KnownReturn && rs.KnownReturn { |
993781f3Lukasz Mierzwa1 years ago | 805 | // Both sides always return something |
| 806 | s.ReturnedNumber, s.IsDead, s.IsDeadReason = calculateStaticReturn( | |
| 807 | expr, | |
| 808 | s, rs, | |
| 809 | n.Op, | |
| 810 | s.IsDead, | |
| 811 | ) | |
| 812 | } | |
| 813 | } | |
f97e7fc2Lukasz Mierzwa1 years ago | 814 | } |
| 815 | if s.Operation == "" { | |
| 816 | s.Operation = n.VectorMatching.Card.String() | |
128fada0Lukasz Mierzwa1 years ago | 817 | } |
c269e3b9Lukasz Mierzwa1 years ago | 818 | for _, rs := range rhs { |
| 819 | if ok, s := canJoin(s, rs, n.VectorMatching); !ok { | |
| 820 | rs.IsDead = true | |
| 821 | rs.IsDeadReason = s | |
| 822 | } | |
| 823 | s.Joins = append(s.Joins, Join{ | |
| 824 | Src: rs, | |
| 825 | }) | |
| 826 | } | |
bf45639fLukasz Mierzwa1 years ago | 827 | s.IsConditional = isConditional(s, n.Op) |
f97e7fc2Lukasz Mierzwa1 years ago | 828 | src = append(src, s) |
13c2f1c2Lukasz Mierzwa1 years ago | 829 | } |
| 830 | | |
c269e3b9Lukasz Mierzwa1 years ago | 831 | // foo{} + on(...) group_right(...) bar{} |
| 832 | // foo{} + ignoring(...) group_right(...) bar{} | |
13c2f1c2Lukasz Mierzwa1 years ago | 833 | case n.VectorMatching.Card == promParser.CardOneToMany: |
17fb6517Lukasz Mierzwa1 years ago | 834 | lhs := walkNode(expr, n.LHS) |
f97e7fc2Lukasz Mierzwa1 years ago | 835 | for _, s = range walkNode(expr, n.RHS) { |
17fb6517Lukasz Mierzwa1 years ago | 836 | s = includeLabel(s, n.VectorMatching.Include...) |
c269e3b9Lukasz Mierzwa1 years ago | 837 | // If we have: |
| 838 | // foo * on(instance) group_left(a,b) bar{x="y"} | |
| 839 | // then only group_left() labels will be included. | |
f97e7fc2Lukasz Mierzwa1 years ago | 840 | if n.VectorMatching.On { |
17fb6517Lukasz Mierzwa1 years ago | 841 | s = includeLabel(s, n.VectorMatching.MatchingLabels...) |
128fada0Lukasz Mierzwa1 years ago | 842 | } |
f97e7fc2Lukasz Mierzwa1 years ago | 843 | if s.Operation == "" { |
| 844 | s.Operation = n.VectorMatching.Card.String() | |
| 845 | } | |
c269e3b9Lukasz Mierzwa1 years ago | 846 | for _, ls := range lhs { |
| 847 | if ok, s := canJoin(s, ls, n.VectorMatching); !ok { | |
| 848 | ls.IsDead = true | |
| 849 | ls.IsDeadReason = s | |
| 850 | } | |
| 851 | s.Joins = append(s.Joins, Join{ | |
| 852 | Src: ls, | |
| 853 | }) | |
| 854 | } | |
bf45639fLukasz Mierzwa1 years ago | 855 | s.IsConditional = isConditional(s, n.Op) |
f97e7fc2Lukasz Mierzwa1 years ago | 856 | src = append(src, s) |
13c2f1c2Lukasz Mierzwa1 years ago | 857 | } |
| 858 | | |
c269e3b9Lukasz Mierzwa1 years ago | 859 | // foo{} + on(...) group_left(...) bar{} |
| 860 | // foo{} + ignoring(...) group_left(...) bar{} | |
13c2f1c2Lukasz Mierzwa1 years ago | 861 | case n.VectorMatching.Card == promParser.CardManyToOne: |
17fb6517Lukasz Mierzwa1 years ago | 862 | rhs := walkNode(expr, n.RHS) |
f97e7fc2Lukasz Mierzwa1 years ago | 863 | for _, s = range walkNode(expr, n.LHS) { |
17fb6517Lukasz Mierzwa1 years ago | 864 | s = includeLabel(s, n.VectorMatching.Include...) |
f97e7fc2Lukasz Mierzwa1 years ago | 865 | if n.VectorMatching.On { |
17fb6517Lukasz Mierzwa1 years ago | 866 | s = includeLabel(s, n.VectorMatching.MatchingLabels...) |
13c2f1c2Lukasz Mierzwa1 years ago | 867 | } |
f97e7fc2Lukasz Mierzwa1 years ago | 868 | if s.Operation == "" { |
| 869 | s.Operation = n.VectorMatching.Card.String() | |
| 870 | } | |
c269e3b9Lukasz Mierzwa1 years ago | 871 | for _, rs := range rhs { |
| 872 | if ok, s := canJoin(s, rs, n.VectorMatching); !ok { | |
| 873 | rs.IsDead = true | |
| 874 | rs.IsDeadReason = s | |
| 875 | } | |
| 876 | s.Joins = append(s.Joins, Join{ | |
| 877 | Src: rs, | |
| 878 | }) | |
| 879 | } | |
bf45639fLukasz Mierzwa1 years ago | 880 | s.IsConditional = isConditional(s, n.Op) |
f97e7fc2Lukasz Mierzwa1 years ago | 881 | src = append(src, s) |
13c2f1c2Lukasz Mierzwa1 years ago | 882 | } |
128fada0Lukasz Mierzwa1 years ago | 883 | |
| 884 | // foo{} and on(...) bar{} | |
| 885 | // foo{} and ignoring(...) bar{} | |
17fb6517Lukasz Mierzwa1 years ago | 886 | // foo{} unless bar{} |
128fada0Lukasz Mierzwa1 years ago | 887 | case n.VectorMatching.Card == promParser.CardManyToMany: |
aaea75cfLukasz Mierzwa1 years ago | 888 | var lhsCanBeEmpty bool // true if any of the LHS query can produce empty results. |
17fb6517Lukasz Mierzwa1 years ago | 889 | rhs := walkNode(expr, n.RHS) |
f97e7fc2Lukasz Mierzwa1 years ago | 890 | for _, s = range walkNode(expr, n.LHS) { |
bf45639fLukasz Mierzwa1 years ago | 891 | var rhsConditional bool |
f97e7fc2Lukasz Mierzwa1 years ago | 892 | if n.VectorMatching.On { |
17fb6517Lukasz Mierzwa1 years ago | 893 | s = includeLabel(s, n.VectorMatching.MatchingLabels...) |
f97e7fc2Lukasz Mierzwa1 years ago | 894 | } |
| 895 | if s.Operation == "" { | |
| 896 | s.Operation = n.VectorMatching.Card.String() | |
128fada0Lukasz Mierzwa1 years ago | 897 | } |
ee5af5fbLukasz Mierzwa1 years ago | 898 | if !s.AlwaysReturns || s.IsConditional { |
aaea75cfLukasz Mierzwa1 years ago | 899 | lhsCanBeEmpty = true |
| 900 | } | |
c269e3b9Lukasz Mierzwa1 years ago | 901 | for _, rs := range rhs { |
bf45639fLukasz Mierzwa1 years ago | 902 | if isConditional(rs, n.Op) { |
| 903 | rhsConditional = true | |
| 904 | } | |
c269e3b9Lukasz Mierzwa1 years ago | 905 | if ok, s := canJoin(s, rs, n.VectorMatching); !ok { |
| 906 | rs.IsDead = true | |
| 907 | rs.IsDeadReason = s | |
| 908 | } | |
| 909 | switch { | |
| 910 | case n.Op == promParser.LUNLESS: | |
947a0055Lukasz Mierzwa1 years ago | 911 | if n.VectorMatching.On && len(n.VectorMatching.MatchingLabels) == 0 && rs.AlwaysReturns && !rs.IsConditional { |
c269e3b9Lukasz Mierzwa1 years ago | 912 | s.IsDead = true |
| 913 | s.IsDeadReason = "this query will never return anything because the `unless` query always returns something" | |
| 914 | } | |
| 915 | s.Unless = append(s.Unless, Join{ | |
| 916 | Src: rs, | |
| 917 | }) | |
| 918 | case n.Op != promParser.LOR: | |
| 919 | s.Joins = append(s.Joins, Join{ | |
| 920 | Src: rs, | |
| 921 | }) | |
| 922 | } | |
17fb6517Lukasz Mierzwa1 years ago | 923 | } |
bf45639fLukasz Mierzwa1 years ago | 924 | if n.Op == promParser.LAND && rhsConditional { |
| 925 | s.IsConditional = true | |
| 926 | } | |
f97e7fc2Lukasz Mierzwa1 years ago | 927 | src = append(src, s) |
128fada0Lukasz Mierzwa1 years ago | 928 | } |
| 929 | if n.Op == promParser.LOR { | |
17fb6517Lukasz Mierzwa1 years ago | 930 | for _, s = range rhs { |
f97e7fc2Lukasz Mierzwa1 years ago | 931 | if s.Operation == "" { |
| 932 | s.Operation = n.VectorMatching.Card.String() | |
| 933 | } | |
aaea75cfLukasz Mierzwa1 years ago | 934 | // If LHS can NOT be empty then RHS is dead code. |
| 935 | if !lhsCanBeEmpty { | |
| 936 | s.IsDead = true | |
c269e3b9Lukasz Mierzwa1 years ago | 937 | s.IsDeadReason = "the left hand side always returs something and so the right hand side is never used" |
aaea75cfLukasz Mierzwa1 years ago | 938 | } |
f97e7fc2Lukasz Mierzwa1 years ago | 939 | src = append(src, s) |
| 940 | } | |
128fada0Lukasz Mierzwa1 years ago | 941 | } |
| 942 | } | |
f97e7fc2Lukasz Mierzwa1 years ago | 943 | return src |
13c2f1c2Lukasz Mierzwa1 years ago | 944 | } |
aaea75cfLukasz Mierzwa1 years ago | 945 | |
bf45639fLukasz Mierzwa1 years ago | 946 | func isConditional(s Source, op promParser.ItemType) bool { |
| 947 | if s.IsConditional { | |
| 948 | return true | |
| 949 | } | |
| 950 | return op.IsComparisonOperator() | |
| 951 | } | |
| 952 | | |
c269e3b9Lukasz Mierzwa1 years ago | 953 | func canJoin(ls, rs Source, vm *promParser.VectorMatching) (bool, string) { |
| 954 | var side string | |
| 955 | if vm.Card == promParser.CardOneToMany { | |
| 956 | side = "left" | |
| 957 | } else { | |
| 958 | side = "right" | |
| 959 | } | |
| 960 | | |
| 961 | switch { | |
| 962 | case vm.On && len(vm.MatchingLabels) == 0: // ls on() unless rs | |
| 963 | return true, "" | |
| 964 | case vm.On: // ls on(...) unless rs | |
| 965 | for _, name := range vm.MatchingLabels { | |
1bca8feeLukasz Mierzwa1 years ago | 966 | if ls.CanHaveLabel(name) && !rs.CanHaveLabel(name) { |
947a0055Lukasz Mierzwa1 years ago | 967 | return false, fmt.Sprintf("The %s hand side will never be matched because it doesn't have the `%s` label from `on(...)`. %s", |
| 968 | side, name, rs.LabelExcludeReason(name).Reason) | |
c269e3b9Lukasz Mierzwa1 years ago | 969 | } |
| 970 | } | |
| 971 | default: // ls unless rs | |
| 972 | for _, name := range ls.GuaranteedLabels { | |
1bca8feeLukasz Mierzwa1 years ago | 973 | if ls.CanHaveLabel(name) && !rs.CanHaveLabel(name) { |
947a0055Lukasz Mierzwa1 years ago | 974 | return false, fmt.Sprintf("The %s hand side will never be matched because it doesn't have the `%s` label while the left hand side will. %s", |
| 975 | side, name, rs.LabelExcludeReason(name).Reason) | |
c269e3b9Lukasz Mierzwa1 years ago | 976 | } |
| 977 | } | |
| 978 | } | |
| 979 | return true, "" | |
| 980 | } | |
| 981 | | |
| 982 | func ftos(v float64) string { | |
| 983 | return strconv.FormatFloat(v, 'f', -1, 64) | |
| 984 | } | |
| 985 | | |
| 986 | func calculateStaticReturn(expr string, ls, rs Source, op promParser.ItemType, isDead bool) (float64, bool, string) { | |
| 987 | lf := ls.Fragment(expr) | |
| 988 | rf := rs.Fragment(expr) | |
| 989 | var cmpPrefix string | |
| 990 | if lf != "" && rf != "" { | |
| 991 | cmpPrefix = fmt.Sprintf("`%s %s %s` always evaluates to", lf, op, rf) | |
| 992 | } else { | |
| 993 | cmpPrefix = "this query always evaluates to" | |
| 994 | } | |
| 995 | cmpSuffix := "which is not possible, so it will never return anything" | |
aaea75cfLukasz Mierzwa1 years ago | 996 | switch op { |
| 997 | case promParser.EQLC: | |
c269e3b9Lukasz Mierzwa1 years ago | 998 | if ls.ReturnedNumber != rs.ReturnedNumber { |
| 999 | return ls.ReturnedNumber, true, fmt.Sprintf("%s `%s == %s` %s", cmpPrefix, ftos(ls.ReturnedNumber), ftos(rs.ReturnedNumber), cmpSuffix) | |
aaea75cfLukasz Mierzwa1 years ago | 1000 | } |
| 1001 | case promParser.NEQ: | |
c269e3b9Lukasz Mierzwa1 years ago | 1002 | if ls.ReturnedNumber == rs.ReturnedNumber { |
| 1003 | return ls.ReturnedNumber, true, fmt.Sprintf("%s `%s != %s` %s", cmpPrefix, ftos(ls.ReturnedNumber), ftos(rs.ReturnedNumber), cmpSuffix) | |
aaea75cfLukasz Mierzwa1 years ago | 1004 | } |
| 1005 | case promParser.LTE: | |
c269e3b9Lukasz Mierzwa1 years ago | 1006 | if ls.ReturnedNumber > rs.ReturnedNumber { |
| 1007 | return ls.ReturnedNumber, true, fmt.Sprintf("%s `%s <= %s` %s", cmpPrefix, ftos(ls.ReturnedNumber), ftos(rs.ReturnedNumber), cmpSuffix) | |
aaea75cfLukasz Mierzwa1 years ago | 1008 | } |
| 1009 | case promParser.LSS: | |
c269e3b9Lukasz Mierzwa1 years ago | 1010 | if ls.ReturnedNumber >= rs.ReturnedNumber { |
| 1011 | return ls.ReturnedNumber, true, fmt.Sprintf("%s `%s < %s` %s", cmpPrefix, ftos(ls.ReturnedNumber), ftos(rs.ReturnedNumber), cmpSuffix) | |
aaea75cfLukasz Mierzwa1 years ago | 1012 | } |
| 1013 | case promParser.GTE: | |
c269e3b9Lukasz Mierzwa1 years ago | 1014 | if ls.ReturnedNumber < rs.ReturnedNumber { |
| 1015 | return ls.ReturnedNumber, true, fmt.Sprintf("%s `%s >= %s` %s", cmpPrefix, ftos(ls.ReturnedNumber), ftos(rs.ReturnedNumber), cmpSuffix) | |
aaea75cfLukasz Mierzwa1 years ago | 1016 | } |
| 1017 | case promParser.GTR: | |
c269e3b9Lukasz Mierzwa1 years ago | 1018 | if ls.ReturnedNumber <= rs.ReturnedNumber { |
| 1019 | return ls.ReturnedNumber, true, fmt.Sprintf("%s `%s > %s` %s", cmpPrefix, ftos(ls.ReturnedNumber), ftos(rs.ReturnedNumber), cmpSuffix) | |
aaea75cfLukasz Mierzwa1 years ago | 1020 | } |
| 1021 | case promParser.ADD: | |
c269e3b9Lukasz Mierzwa1 years ago | 1022 | return ls.ReturnedNumber + rs.ReturnedNumber, isDead, "" |
aaea75cfLukasz Mierzwa1 years ago | 1023 | case promParser.SUB: |
c269e3b9Lukasz Mierzwa1 years ago | 1024 | return ls.ReturnedNumber - rs.ReturnedNumber, isDead, "" |
aaea75cfLukasz Mierzwa1 years ago | 1025 | case promParser.MUL: |
c269e3b9Lukasz Mierzwa1 years ago | 1026 | return ls.ReturnedNumber * rs.ReturnedNumber, isDead, "" |
aaea75cfLukasz Mierzwa1 years ago | 1027 | case promParser.DIV: |
c269e3b9Lukasz Mierzwa1 years ago | 1028 | return ls.ReturnedNumber / rs.ReturnedNumber, isDead, "" |
aaea75cfLukasz Mierzwa1 years ago | 1029 | case promParser.MOD: |
c269e3b9Lukasz Mierzwa1 years ago | 1030 | return math.Mod(ls.ReturnedNumber, rs.ReturnedNumber), isDead, "" |
aaea75cfLukasz Mierzwa1 years ago | 1031 | case promParser.POW: |
c269e3b9Lukasz Mierzwa1 years ago | 1032 | return math.Pow(ls.ReturnedNumber, rs.ReturnedNumber), isDead, "" |
aaea75cfLukasz Mierzwa1 years ago | 1033 | } |
c269e3b9Lukasz Mierzwa1 years ago | 1034 | return ls.ReturnedNumber, isDead, "" |
aaea75cfLukasz Mierzwa1 years ago | 1035 | } |
f7d9a38aLukasz Mierzwa1 years ago | 1036 | |
| 1037 | // FIXME sum() on (). | |
b7f6e358Lukasz Mierzwa1 years ago | 1038 | func FindPosition(expr string, within posrange.PositionRange, fn string) posrange.PositionRange { |
7b0ce529Lukasz Mierzwa1 years ago | 1039 | re := regexp.MustCompile("(?i)(" + regexp.QuoteMeta(fn) + ")[ \n\t]*\\(") |
f7d9a38aLukasz Mierzwa1 years ago | 1040 | idx := re.FindStringSubmatchIndex(GetQueryFragment(expr, within)) |
| 1041 | if idx == nil { | |
| 1042 | return within | |
| 1043 | } | |
| 1044 | return posrange.PositionRange{ | |
| 1045 | Start: within.Start + posrange.Pos(idx[0]), | |
efdc4479Lukasz Mierzwa1 years ago | 1046 | End: within.Start + posrange.Pos(idx[1]-1), |
f7d9a38aLukasz Mierzwa1 years ago | 1047 | } |
| 1048 | } |