parse.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. // Copyright 2015 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package promql
  14. import (
  15. "fmt"
  16. "math"
  17. "os"
  18. "runtime"
  19. "sort"
  20. "strconv"
  21. "strings"
  22. "time"
  23. "github.com/pkg/errors"
  24. "github.com/prometheus/common/model"
  25. "github.com/influxdata/promql/v2/pkg/labels"
  26. "github.com/influxdata/promql/v2/pkg/value"
  27. "github.com/influxdata/promql/v2/util/strutil"
  28. )
  29. type parser struct {
  30. lex *lexer
  31. token [3]item
  32. peekCount int
  33. }
  34. // ParseErr wraps a parsing error with line and position context.
  35. // If the parsing input was a single line, line will be 0 and omitted
  36. // from the error string.
  37. type ParseErr struct {
  38. Line, Pos int
  39. Err error
  40. }
  41. func (e *ParseErr) Error() string {
  42. if e.Line == 0 {
  43. return fmt.Sprintf("parse error at char %d: %s", e.Pos, e.Err)
  44. }
  45. return fmt.Sprintf("parse error at line %d, char %d: %s", e.Line, e.Pos, e.Err)
  46. }
  47. // ParseExpr returns the expression parsed from the input.
  48. func ParseExpr(input string) (Expr, error) {
  49. p := newParser(input)
  50. expr, err := p.parseExpr()
  51. if err != nil {
  52. return nil, err
  53. }
  54. err = p.typecheck(expr)
  55. return expr, err
  56. }
  57. // ParseMetric parses the input into a metric
  58. func ParseMetric(input string) (m labels.Labels, err error) {
  59. p := newParser(input)
  60. defer p.recover(&err)
  61. m = p.metric()
  62. if p.peek().typ != ItemEOF {
  63. p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
  64. }
  65. return m, nil
  66. }
  67. // ParseMetricSelector parses the provided textual metric selector into a list of
  68. // label matchers.
  69. func ParseMetricSelector(input string) (m []*labels.Matcher, err error) {
  70. p := newParser(input)
  71. defer p.recover(&err)
  72. name := ""
  73. if t := p.peek().typ; t == ItemMetricIdentifier || t == ItemIdentifier {
  74. name = p.next().val
  75. }
  76. vs := p.VectorSelector(name)
  77. if p.peek().typ != ItemEOF {
  78. p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
  79. }
  80. return vs.LabelMatchers, nil
  81. }
  82. // newParser returns a new parser.
  83. func newParser(input string) *parser {
  84. p := &parser{
  85. lex: lex(input),
  86. }
  87. return p
  88. }
  89. // parseExpr parses a single expression from the input.
  90. func (p *parser) parseExpr() (expr Expr, err error) {
  91. defer p.recover(&err)
  92. for p.peek().typ != ItemEOF {
  93. if p.peek().typ == ItemComment {
  94. continue
  95. }
  96. if expr != nil {
  97. p.errorf("could not parse remaining input %.15q...", p.lex.input[p.lex.lastPos:])
  98. }
  99. expr = p.expr()
  100. }
  101. if expr == nil {
  102. p.errorf("no expression found in input")
  103. }
  104. return
  105. }
  106. // sequenceValue is an omittable value in a sequence of time series values.
  107. type sequenceValue struct {
  108. value float64
  109. omitted bool
  110. }
  111. func (v sequenceValue) String() string {
  112. if v.omitted {
  113. return "_"
  114. }
  115. return fmt.Sprintf("%f", v.value)
  116. }
  117. // parseSeriesDesc parses the description of a time series.
  118. func parseSeriesDesc(input string) (labels.Labels, []sequenceValue, error) {
  119. p := newParser(input)
  120. p.lex.seriesDesc = true
  121. return p.parseSeriesDesc()
  122. }
  123. // parseSeriesDesc parses a description of a time series into its metric and value sequence.
  124. func (p *parser) parseSeriesDesc() (m labels.Labels, vals []sequenceValue, err error) {
  125. defer p.recover(&err)
  126. m = p.metric()
  127. const ctx = "series values"
  128. for {
  129. for p.peek().typ == ItemSpace {
  130. p.next()
  131. }
  132. if p.peek().typ == ItemEOF {
  133. break
  134. }
  135. // Extract blanks.
  136. if p.peek().typ == ItemBlank {
  137. p.next()
  138. times := uint64(1)
  139. if p.peek().typ == ItemTimes {
  140. p.next()
  141. times, err = strconv.ParseUint(p.expect(ItemNumber, ctx).val, 10, 64)
  142. if err != nil {
  143. p.errorf("invalid repetition in %s: %s", ctx, err)
  144. }
  145. }
  146. for i := uint64(0); i < times; i++ {
  147. vals = append(vals, sequenceValue{omitted: true})
  148. }
  149. // This is to ensure that there is a space between this and the next number.
  150. // This is especially required if the next number is negative.
  151. if t := p.expectOneOf(ItemSpace, ItemEOF, ctx).typ; t == ItemEOF {
  152. break
  153. }
  154. continue
  155. }
  156. // Extract values.
  157. sign := 1.0
  158. if t := p.peek().typ; t == ItemSUB || t == ItemADD {
  159. if p.next().typ == ItemSUB {
  160. sign = -1
  161. }
  162. }
  163. var k float64
  164. if t := p.peek().typ; t == ItemNumber {
  165. k = sign * p.number(p.expect(ItemNumber, ctx).val)
  166. } else if t == ItemIdentifier && p.peek().val == "stale" {
  167. p.next()
  168. k = math.Float64frombits(value.StaleNaN)
  169. } else {
  170. p.errorf("expected number or 'stale' in %s but got %s (value: %s)", ctx, t.desc(), p.peek())
  171. }
  172. vals = append(vals, sequenceValue{
  173. value: k,
  174. })
  175. // If there are no offset repetitions specified, proceed with the next value.
  176. if t := p.peek(); t.typ == ItemSpace {
  177. // This ensures there is a space between every value.
  178. continue
  179. } else if t.typ == ItemEOF {
  180. break
  181. } else if t.typ != ItemADD && t.typ != ItemSUB {
  182. p.errorf("expected next value or relative expansion in %s but got %s (value: %s)", ctx, t.desc(), p.peek())
  183. }
  184. // Expand the repeated offsets into values.
  185. sign = 1.0
  186. if p.next().typ == ItemSUB {
  187. sign = -1.0
  188. }
  189. offset := sign * p.number(p.expect(ItemNumber, ctx).val)
  190. p.expect(ItemTimes, ctx)
  191. times, err := strconv.ParseUint(p.expect(ItemNumber, ctx).val, 10, 64)
  192. if err != nil {
  193. p.errorf("invalid repetition in %s: %s", ctx, err)
  194. }
  195. for i := uint64(0); i < times; i++ {
  196. k += offset
  197. vals = append(vals, sequenceValue{
  198. value: k,
  199. })
  200. }
  201. // This is to ensure that there is a space between this expanding notation
  202. // and the next number. This is especially required if the next number
  203. // is negative.
  204. if t := p.expectOneOf(ItemSpace, ItemEOF, ctx).typ; t == ItemEOF {
  205. break
  206. }
  207. }
  208. return m, vals, nil
  209. }
  210. // typecheck checks correct typing of the parsed statements or expression.
  211. func (p *parser) typecheck(node Node) (err error) {
  212. defer p.recover(&err)
  213. p.checkType(node)
  214. return nil
  215. }
  216. // next returns the next token.
  217. func (p *parser) next() item {
  218. if p.peekCount > 0 {
  219. p.peekCount--
  220. } else {
  221. t := p.lex.nextItem()
  222. // Skip comments.
  223. for t.typ == ItemComment {
  224. t = p.lex.nextItem()
  225. }
  226. p.token[0] = t
  227. }
  228. if p.token[p.peekCount].typ == ItemError {
  229. p.errorf("%s", p.token[p.peekCount].val)
  230. }
  231. return p.token[p.peekCount]
  232. }
  233. // peek returns but does not consume the next token.
  234. func (p *parser) peek() item {
  235. if p.peekCount > 0 {
  236. return p.token[p.peekCount-1]
  237. }
  238. p.peekCount = 1
  239. t := p.lex.nextItem()
  240. // Skip comments.
  241. for t.typ == ItemComment {
  242. t = p.lex.nextItem()
  243. }
  244. p.token[0] = t
  245. return p.token[0]
  246. }
  247. // backup backs the input stream up one token.
  248. func (p *parser) backup() {
  249. p.peekCount++
  250. }
  251. // errorf formats the error and terminates processing.
  252. func (p *parser) errorf(format string, args ...interface{}) {
  253. p.error(errors.Errorf(format, args...))
  254. }
  255. // error terminates processing.
  256. func (p *parser) error(err error) {
  257. perr := &ParseErr{
  258. Line: p.lex.lineNumber(),
  259. Pos: p.lex.linePosition(),
  260. Err: err,
  261. }
  262. if strings.Count(strings.TrimSpace(p.lex.input), "\n") == 0 {
  263. perr.Line = 0
  264. }
  265. panic(perr)
  266. }
  267. // expect consumes the next token and guarantees it has the required type.
  268. func (p *parser) expect(exp ItemType, context string) item {
  269. token := p.next()
  270. if token.typ != exp {
  271. p.errorf("unexpected %s in %s, expected %s", token.desc(), context, exp.desc())
  272. }
  273. return token
  274. }
  275. // expectOneOf consumes the next token and guarantees it has one of the required types.
  276. func (p *parser) expectOneOf(exp1, exp2 ItemType, context string) item {
  277. token := p.next()
  278. if token.typ != exp1 && token.typ != exp2 {
  279. p.errorf("unexpected %s in %s, expected %s or %s", token.desc(), context, exp1.desc(), exp2.desc())
  280. }
  281. return token
  282. }
  283. var errUnexpected = errors.New("unexpected error")
  284. // recover is the handler that turns panics into returns from the top level of Parse.
  285. func (p *parser) recover(errp *error) {
  286. e := recover()
  287. if _, ok := e.(runtime.Error); ok {
  288. // Print the stack trace but do not inhibit the running application.
  289. buf := make([]byte, 64<<10)
  290. buf = buf[:runtime.Stack(buf, false)]
  291. fmt.Fprintf(os.Stderr, "parser panic: %v\n%s", e, buf)
  292. *errp = errUnexpected
  293. } else if e != nil {
  294. *errp = e.(error)
  295. }
  296. p.lex.close()
  297. }
  298. // expr parses any expression.
  299. func (p *parser) expr() Expr {
  300. // Parse the starting expression.
  301. expr := p.unaryExpr()
  302. // Loop through the operations and construct a binary operation tree based
  303. // on the operators' precedence.
  304. for {
  305. // If the next token is not an operator the expression is done.
  306. op := p.peek().typ
  307. if !op.isOperator() {
  308. // Check for subquery.
  309. if op == ItemLeftBracket {
  310. expr = p.subqueryOrRangeSelector(expr, false)
  311. if s, ok := expr.(*SubqueryExpr); ok {
  312. // Parse optional offset.
  313. if p.peek().typ == ItemOffset {
  314. offset := p.offset()
  315. s.Offset = offset
  316. }
  317. }
  318. }
  319. return expr
  320. }
  321. p.next() // Consume operator.
  322. // Parse optional operator matching options. Its validity
  323. // is checked in the type-checking stage.
  324. vecMatching := &VectorMatching{
  325. Card: CardOneToOne,
  326. }
  327. if op.isSetOperator() {
  328. vecMatching.Card = CardManyToMany
  329. }
  330. returnBool := false
  331. // Parse bool modifier.
  332. if p.peek().typ == ItemBool {
  333. if !op.isComparisonOperator() {
  334. p.errorf("bool modifier can only be used on comparison operators")
  335. }
  336. p.next()
  337. returnBool = true
  338. }
  339. // Parse ON/IGNORING clause.
  340. if p.peek().typ == ItemOn || p.peek().typ == ItemIgnoring {
  341. if p.peek().typ == ItemOn {
  342. vecMatching.On = true
  343. }
  344. p.next()
  345. vecMatching.MatchingLabels = p.labels()
  346. // Parse grouping.
  347. if t := p.peek().typ; t == ItemGroupLeft || t == ItemGroupRight {
  348. p.next()
  349. if t == ItemGroupLeft {
  350. vecMatching.Card = CardManyToOne
  351. } else {
  352. vecMatching.Card = CardOneToMany
  353. }
  354. if p.peek().typ == ItemLeftParen {
  355. vecMatching.Include = p.labels()
  356. }
  357. }
  358. }
  359. for _, ln := range vecMatching.MatchingLabels {
  360. for _, ln2 := range vecMatching.Include {
  361. if ln == ln2 && vecMatching.On {
  362. p.errorf("label %q must not occur in ON and GROUP clause at once", ln)
  363. }
  364. }
  365. }
  366. // Parse the next operand.
  367. rhs := p.unaryExpr()
  368. // Assign the new root based on the precedence of the LHS and RHS operators.
  369. expr = p.balance(expr, op, rhs, vecMatching, returnBool)
  370. }
  371. }
  372. func (p *parser) balance(lhs Expr, op ItemType, rhs Expr, vecMatching *VectorMatching, returnBool bool) *BinaryExpr {
  373. if lhsBE, ok := lhs.(*BinaryExpr); ok {
  374. precd := lhsBE.Op.precedence() - op.precedence()
  375. if (precd < 0) || (precd == 0 && op.isRightAssociative()) {
  376. balanced := p.balance(lhsBE.RHS, op, rhs, vecMatching, returnBool)
  377. if lhsBE.Op.isComparisonOperator() && !lhsBE.ReturnBool && balanced.Type() == ValueTypeScalar && lhsBE.LHS.Type() == ValueTypeScalar {
  378. p.errorf("comparisons between scalars must use BOOL modifier")
  379. }
  380. return &BinaryExpr{
  381. Op: lhsBE.Op,
  382. LHS: lhsBE.LHS,
  383. RHS: balanced,
  384. VectorMatching: lhsBE.VectorMatching,
  385. ReturnBool: lhsBE.ReturnBool,
  386. }
  387. }
  388. }
  389. if op.isComparisonOperator() && !returnBool && rhs.Type() == ValueTypeScalar && lhs.Type() == ValueTypeScalar {
  390. p.errorf("comparisons between scalars must use BOOL modifier")
  391. }
  392. return &BinaryExpr{
  393. Op: op,
  394. LHS: lhs,
  395. RHS: rhs,
  396. VectorMatching: vecMatching,
  397. ReturnBool: returnBool,
  398. }
  399. }
  400. // unaryExpr parses a unary expression.
  401. //
  402. // <Vector_selector> | <Matrix_selector> | (+|-) <number_literal> | '(' <expr> ')'
  403. //
  404. func (p *parser) unaryExpr() Expr {
  405. switch t := p.peek(); t.typ {
  406. case ItemADD, ItemSUB:
  407. p.next()
  408. e := p.unaryExpr()
  409. // Simplify unary expressions for number literals.
  410. if nl, ok := e.(*NumberLiteral); ok {
  411. if t.typ == ItemSUB {
  412. nl.Val *= -1
  413. }
  414. return nl
  415. }
  416. return &UnaryExpr{Op: t.typ, Expr: e}
  417. case ItemLeftParen:
  418. p.next()
  419. e := p.expr()
  420. p.expect(ItemRightParen, "paren expression")
  421. return &ParenExpr{Expr: e}
  422. }
  423. e := p.primaryExpr()
  424. // Expression might be followed by a range selector.
  425. if p.peek().typ == ItemLeftBracket {
  426. e = p.subqueryOrRangeSelector(e, true)
  427. }
  428. // Parse optional offset.
  429. if p.peek().typ == ItemOffset {
  430. offset := p.offset()
  431. switch s := e.(type) {
  432. case *VectorSelector:
  433. s.Offset = offset
  434. case *MatrixSelector:
  435. s.Offset = offset
  436. case *SubqueryExpr:
  437. s.Offset = offset
  438. default:
  439. p.errorf("offset modifier must be preceded by an instant or range selector, but follows a %T instead", e)
  440. }
  441. }
  442. return e
  443. }
  444. // subqueryOrRangeSelector parses a Subquery based on given Expr (or)
  445. // a Matrix (a.k.a. range) selector based on a given Vector selector.
  446. //
  447. // <Vector_selector> '[' <duration> ']' | <Vector_selector> '[' <duration> ':' [<duration>] ']'
  448. //
  449. func (p *parser) subqueryOrRangeSelector(expr Expr, checkRange bool) Expr {
  450. ctx := "subquery selector"
  451. if checkRange {
  452. ctx = "range/subquery selector"
  453. }
  454. p.next()
  455. var erange time.Duration
  456. var err error
  457. erangeStr := p.expect(ItemDuration, ctx).val
  458. erange, err = parseDuration(erangeStr)
  459. if err != nil {
  460. p.error(err)
  461. }
  462. var itm item
  463. if checkRange {
  464. itm = p.expectOneOf(ItemRightBracket, ItemColon, ctx)
  465. if itm.typ == ItemRightBracket {
  466. // Range selector.
  467. vs, ok := expr.(*VectorSelector)
  468. if !ok {
  469. p.errorf("range specification must be preceded by a metric selector, but follows a %T instead", expr)
  470. }
  471. return &MatrixSelector{
  472. Name: vs.Name,
  473. LabelMatchers: vs.LabelMatchers,
  474. Range: erange,
  475. }
  476. }
  477. } else {
  478. itm = p.expect(ItemColon, ctx)
  479. }
  480. // Subquery.
  481. var estep time.Duration
  482. itm = p.expectOneOf(ItemRightBracket, ItemDuration, ctx)
  483. if itm.typ == ItemDuration {
  484. estepStr := itm.val
  485. estep, err = parseDuration(estepStr)
  486. if err != nil {
  487. p.error(err)
  488. }
  489. p.expect(ItemRightBracket, ctx)
  490. }
  491. return &SubqueryExpr{
  492. Expr: expr,
  493. Range: erange,
  494. Step: estep,
  495. }
  496. }
  497. // number parses a number.
  498. func (p *parser) number(val string) float64 {
  499. n, err := strconv.ParseInt(val, 0, 64)
  500. f := float64(n)
  501. if err != nil {
  502. f, err = strconv.ParseFloat(val, 64)
  503. }
  504. if err != nil {
  505. p.errorf("error parsing number: %s", err)
  506. }
  507. return f
  508. }
  509. // primaryExpr parses a primary expression.
  510. //
  511. // <metric_name> | <function_call> | <Vector_aggregation> | <literal>
  512. //
  513. func (p *parser) primaryExpr() Expr {
  514. switch t := p.next(); {
  515. case t.typ == ItemNumber:
  516. f := p.number(t.val)
  517. return &NumberLiteral{f}
  518. case t.typ == ItemString:
  519. return &StringLiteral{p.unquoteString(t.val)}
  520. case t.typ == ItemLeftBrace:
  521. // Metric selector without metric name.
  522. p.backup()
  523. return p.VectorSelector("")
  524. case t.typ == ItemIdentifier:
  525. // Check for function call.
  526. if p.peek().typ == ItemLeftParen {
  527. return p.call(t.val)
  528. }
  529. fallthrough // Else metric selector.
  530. case t.typ == ItemMetricIdentifier:
  531. return p.VectorSelector(t.val)
  532. case t.typ.isAggregator():
  533. p.backup()
  534. return p.aggrExpr()
  535. default:
  536. p.errorf("no valid expression found")
  537. }
  538. return nil
  539. }
  540. // labels parses a list of labelnames.
  541. //
  542. // '(' <label_name>, ... ')'
  543. //
  544. func (p *parser) labels() []string {
  545. const ctx = "grouping opts"
  546. p.expect(ItemLeftParen, ctx)
  547. labels := []string{}
  548. if p.peek().typ != ItemRightParen {
  549. for {
  550. id := p.next()
  551. if !isLabel(id.val) {
  552. p.errorf("unexpected %s in %s, expected label", id.desc(), ctx)
  553. }
  554. labels = append(labels, id.val)
  555. if p.peek().typ != ItemComma {
  556. break
  557. }
  558. p.next()
  559. }
  560. }
  561. p.expect(ItemRightParen, ctx)
  562. return labels
  563. }
  564. // aggrExpr parses an aggregation expression.
  565. //
  566. // <aggr_op> (<Vector_expr>) [by|without <labels>]
  567. // <aggr_op> [by|without <labels>] (<Vector_expr>)
  568. //
  569. func (p *parser) aggrExpr() *AggregateExpr {
  570. const ctx = "aggregation"
  571. agop := p.next()
  572. if !agop.typ.isAggregator() {
  573. p.errorf("expected aggregation operator but got %s", agop)
  574. }
  575. var grouping []string
  576. var without bool
  577. modifiersFirst := false
  578. if t := p.peek().typ; t == ItemBy || t == ItemWithout {
  579. if t == ItemWithout {
  580. without = true
  581. }
  582. p.next()
  583. grouping = p.labels()
  584. modifiersFirst = true
  585. }
  586. p.expect(ItemLeftParen, ctx)
  587. var param Expr
  588. if agop.typ.isAggregatorWithParam() {
  589. param = p.expr()
  590. p.expect(ItemComma, ctx)
  591. }
  592. e := p.expr()
  593. p.expect(ItemRightParen, ctx)
  594. if !modifiersFirst {
  595. if t := p.peek().typ; t == ItemBy || t == ItemWithout {
  596. if len(grouping) > 0 {
  597. p.errorf("aggregation must only contain one grouping clause")
  598. }
  599. if t == ItemWithout {
  600. without = true
  601. }
  602. p.next()
  603. grouping = p.labels()
  604. }
  605. }
  606. return &AggregateExpr{
  607. Op: agop.typ,
  608. Expr: e,
  609. Param: param,
  610. Grouping: grouping,
  611. Without: without,
  612. }
  613. }
  614. // call parses a function call.
  615. //
  616. // <func_name> '(' [ <arg_expr>, ...] ')'
  617. //
  618. func (p *parser) call(name string) *Call {
  619. const ctx = "function call"
  620. fn, exist := getFunction(name)
  621. if !exist {
  622. p.errorf("unknown function with name %q", name)
  623. }
  624. p.expect(ItemLeftParen, ctx)
  625. // Might be call without args.
  626. if p.peek().typ == ItemRightParen {
  627. p.next() // Consume.
  628. return &Call{fn, nil}
  629. }
  630. var args []Expr
  631. for {
  632. e := p.expr()
  633. args = append(args, e)
  634. // Terminate if no more arguments.
  635. if p.peek().typ != ItemComma {
  636. break
  637. }
  638. p.next()
  639. }
  640. // Call must be closed.
  641. p.expect(ItemRightParen, ctx)
  642. return &Call{Func: fn, Args: args}
  643. }
  644. // labelSet parses a set of label matchers
  645. //
  646. // '{' [ <labelname> '=' <match_string>, ... ] '}'
  647. //
  648. func (p *parser) labelSet() labels.Labels {
  649. set := []labels.Label{}
  650. for _, lm := range p.labelMatchers(ItemEQL) {
  651. set = append(set, labels.Label{Name: lm.Name, Value: lm.Value})
  652. }
  653. return labels.New(set...)
  654. }
  655. // labelMatchers parses a set of label matchers.
  656. //
  657. // '{' [ <labelname> <match_op> <match_string>, ... ] '}'
  658. //
  659. func (p *parser) labelMatchers(operators ...ItemType) []*labels.Matcher {
  660. const ctx = "label matching"
  661. matchers := []*labels.Matcher{}
  662. p.expect(ItemLeftBrace, ctx)
  663. // Check if no matchers are provided.
  664. if p.peek().typ == ItemRightBrace {
  665. p.next()
  666. return matchers
  667. }
  668. for {
  669. label := p.expect(ItemIdentifier, ctx)
  670. op := p.next().typ
  671. if !op.isOperator() {
  672. p.errorf("expected label matching operator but got %s", op)
  673. }
  674. var validOp = false
  675. for _, allowedOp := range operators {
  676. if op == allowedOp {
  677. validOp = true
  678. }
  679. }
  680. if !validOp {
  681. p.errorf("operator must be one of %q, is %q", operators, op)
  682. }
  683. val := p.unquoteString(p.expect(ItemString, ctx).val)
  684. // Map the item to the respective match type.
  685. var matchType labels.MatchType
  686. switch op {
  687. case ItemEQL:
  688. matchType = labels.MatchEqual
  689. case ItemNEQ:
  690. matchType = labels.MatchNotEqual
  691. case ItemEQLRegex:
  692. matchType = labels.MatchRegexp
  693. case ItemNEQRegex:
  694. matchType = labels.MatchNotRegexp
  695. default:
  696. p.errorf("item %q is not a metric match type", op)
  697. }
  698. m, err := labels.NewMatcher(matchType, label.val, val)
  699. if err != nil {
  700. p.error(err)
  701. }
  702. matchers = append(matchers, m)
  703. if p.peek().typ == ItemIdentifier {
  704. p.errorf("missing comma before next identifier %q", p.peek().val)
  705. }
  706. // Terminate list if last matcher.
  707. if p.peek().typ != ItemComma {
  708. break
  709. }
  710. p.next()
  711. // Allow comma after each item in a multi-line listing.
  712. if p.peek().typ == ItemRightBrace {
  713. break
  714. }
  715. }
  716. p.expect(ItemRightBrace, ctx)
  717. return matchers
  718. }
  719. // metric parses a metric.
  720. //
  721. // <label_set>
  722. // <metric_identifier> [<label_set>]
  723. //
  724. func (p *parser) metric() labels.Labels {
  725. name := ""
  726. var m labels.Labels
  727. t := p.peek().typ
  728. if t == ItemIdentifier || t == ItemMetricIdentifier {
  729. name = p.next().val
  730. t = p.peek().typ
  731. }
  732. if t != ItemLeftBrace && name == "" {
  733. p.errorf("missing metric name or metric selector")
  734. }
  735. if t == ItemLeftBrace {
  736. m = p.labelSet()
  737. }
  738. if name != "" {
  739. m = append(m, labels.Label{Name: labels.MetricName, Value: name})
  740. sort.Sort(m)
  741. }
  742. return m
  743. }
  744. // offset parses an offset modifier.
  745. //
  746. // offset <duration>
  747. //
  748. func (p *parser) offset() time.Duration {
  749. const ctx = "offset"
  750. p.next()
  751. offi := p.expect(ItemDuration, ctx)
  752. offset, err := parseDuration(offi.val)
  753. if err != nil {
  754. p.error(err)
  755. }
  756. return offset
  757. }
  758. // VectorSelector parses a new (instant) vector selector.
  759. //
  760. // <metric_identifier> [<label_matchers>]
  761. // [<metric_identifier>] <label_matchers>
  762. //
  763. func (p *parser) VectorSelector(name string) *VectorSelector {
  764. var matchers []*labels.Matcher
  765. // Parse label matching if any.
  766. if t := p.peek(); t.typ == ItemLeftBrace {
  767. matchers = p.labelMatchers(ItemEQL, ItemNEQ, ItemEQLRegex, ItemNEQRegex)
  768. }
  769. // Metric name must not be set in the label matchers and before at the same time.
  770. if name != "" {
  771. for _, m := range matchers {
  772. if m.Name == labels.MetricName {
  773. p.errorf("metric name must not be set twice: %q or %q", name, m.Value)
  774. }
  775. }
  776. // Set name label matching.
  777. m, err := labels.NewMatcher(labels.MatchEqual, labels.MetricName, name)
  778. if err != nil {
  779. panic(err) // Must not happen with metric.Equal.
  780. }
  781. matchers = append(matchers, m)
  782. }
  783. if len(matchers) == 0 {
  784. p.errorf("vector selector must contain label matchers or metric name")
  785. }
  786. // A Vector selector must contain at least one non-empty matcher to prevent
  787. // implicit selection of all metrics (e.g. by a typo).
  788. notEmpty := false
  789. for _, lm := range matchers {
  790. if !lm.Matches("") {
  791. notEmpty = true
  792. break
  793. }
  794. }
  795. if !notEmpty {
  796. p.errorf("vector selector must contain at least one non-empty matcher")
  797. }
  798. return &VectorSelector{
  799. Name: name,
  800. LabelMatchers: matchers,
  801. }
  802. }
  803. // expectType checks the type of the node and raises an error if it
  804. // is not of the expected type.
  805. func (p *parser) expectType(node Node, want ValueType, context string) {
  806. t := p.checkType(node)
  807. if t != want {
  808. p.errorf("expected type %s in %s, got %s", documentedType(want), context, documentedType(t))
  809. }
  810. }
  811. // check the types of the children of each node and raise an error
  812. // if they do not form a valid node.
  813. //
  814. // Some of these checks are redundant as the parsing stage does not allow
  815. // them, but the costs are small and might reveal errors when making changes.
  816. func (p *parser) checkType(node Node) (typ ValueType) {
  817. // For expressions the type is determined by their Type function.
  818. // Lists do not have a type but are not invalid either.
  819. switch n := node.(type) {
  820. case Expressions:
  821. typ = ValueTypeNone
  822. case Expr:
  823. typ = n.Type()
  824. default:
  825. p.errorf("unknown node type: %T", node)
  826. }
  827. // Recursively check correct typing for child nodes and raise
  828. // errors in case of bad typing.
  829. switch n := node.(type) {
  830. case *EvalStmt:
  831. ty := p.checkType(n.Expr)
  832. if ty == ValueTypeNone {
  833. p.errorf("evaluation statement must have a valid expression type but got %s", documentedType(ty))
  834. }
  835. case Expressions:
  836. for _, e := range n {
  837. ty := p.checkType(e)
  838. if ty == ValueTypeNone {
  839. p.errorf("expression must have a valid expression type but got %s", documentedType(ty))
  840. }
  841. }
  842. case *AggregateExpr:
  843. if !n.Op.isAggregator() {
  844. p.errorf("aggregation operator expected in aggregation expression but got %q", n.Op)
  845. }
  846. p.expectType(n.Expr, ValueTypeVector, "aggregation expression")
  847. if n.Op == ItemTopK || n.Op == ItemBottomK || n.Op == ItemQuantile {
  848. p.expectType(n.Param, ValueTypeScalar, "aggregation parameter")
  849. }
  850. if n.Op == ItemCountValues {
  851. p.expectType(n.Param, ValueTypeString, "aggregation parameter")
  852. }
  853. case *BinaryExpr:
  854. lt := p.checkType(n.LHS)
  855. rt := p.checkType(n.RHS)
  856. if !n.Op.isOperator() {
  857. p.errorf("binary expression does not support operator %q", n.Op)
  858. }
  859. if (lt != ValueTypeScalar && lt != ValueTypeVector) || (rt != ValueTypeScalar && rt != ValueTypeVector) {
  860. p.errorf("binary expression must contain only scalar and instant vector types")
  861. }
  862. if (lt != ValueTypeVector || rt != ValueTypeVector) && n.VectorMatching != nil {
  863. if len(n.VectorMatching.MatchingLabels) > 0 {
  864. p.errorf("vector matching only allowed between instant vectors")
  865. }
  866. n.VectorMatching = nil
  867. } else {
  868. // Both operands are Vectors.
  869. if n.Op.isSetOperator() {
  870. if n.VectorMatching.Card == CardOneToMany || n.VectorMatching.Card == CardManyToOne {
  871. p.errorf("no grouping allowed for %q operation", n.Op)
  872. }
  873. if n.VectorMatching.Card != CardManyToMany {
  874. p.errorf("set operations must always be many-to-many")
  875. }
  876. }
  877. }
  878. if (lt == ValueTypeScalar || rt == ValueTypeScalar) && n.Op.isSetOperator() {
  879. p.errorf("set operator %q not allowed in binary scalar expression", n.Op)
  880. }
  881. case *Call:
  882. nargs := len(n.Func.ArgTypes)
  883. if n.Func.Variadic == 0 {
  884. if nargs != len(n.Args) {
  885. p.errorf("expected %d argument(s) in call to %q, got %d", nargs, n.Func.Name, len(n.Args))
  886. }
  887. } else {
  888. na := nargs - 1
  889. if na > len(n.Args) {
  890. p.errorf("expected at least %d argument(s) in call to %q, got %d", na, n.Func.Name, len(n.Args))
  891. } else if nargsmax := na + n.Func.Variadic; n.Func.Variadic > 0 && nargsmax < len(n.Args) {
  892. p.errorf("expected at most %d argument(s) in call to %q, got %d", nargsmax, n.Func.Name, len(n.Args))
  893. }
  894. }
  895. for i, arg := range n.Args {
  896. if i >= len(n.Func.ArgTypes) {
  897. i = len(n.Func.ArgTypes) - 1
  898. }
  899. p.expectType(arg, n.Func.ArgTypes[i], fmt.Sprintf("call to function %q", n.Func.Name))
  900. }
  901. case *ParenExpr:
  902. p.checkType(n.Expr)
  903. case *UnaryExpr:
  904. if n.Op != ItemADD && n.Op != ItemSUB {
  905. p.errorf("only + and - operators allowed for unary expressions")
  906. }
  907. if t := p.checkType(n.Expr); t != ValueTypeScalar && t != ValueTypeVector {
  908. p.errorf("unary expression only allowed on expressions of type scalar or instant vector, got %q", documentedType(t))
  909. }
  910. case *SubqueryExpr:
  911. ty := p.checkType(n.Expr)
  912. if ty != ValueTypeVector {
  913. p.errorf("subquery is only allowed on instant vector, got %s in %q instead", ty, n.String())
  914. }
  915. case *NumberLiteral, *MatrixSelector, *StringLiteral, *VectorSelector:
  916. // Nothing to do for terminals.
  917. default:
  918. p.errorf("unknown node type: %T", node)
  919. }
  920. return
  921. }
  922. func (p *parser) unquoteString(s string) string {
  923. unquoted, err := strutil.Unquote(s)
  924. if err != nil {
  925. p.errorf("error unquoting string %q: %s", s, err)
  926. }
  927. return unquoted
  928. }
  929. func parseDuration(ds string) (time.Duration, error) {
  930. dur, err := model.ParseDuration(ds)
  931. if err != nil {
  932. return 0, err
  933. }
  934. if dur == 0 {
  935. return 0, errors.New("duration must be greater than 0")
  936. }
  937. return time.Duration(dur), nil
  938. }
  939. // documentedType returns the internal type to the equivalent
  940. // user facing terminology as defined in the documentation.
  941. func documentedType(t ValueType) string {
  942. switch t {
  943. case "vector":
  944. return "instant vector"
  945. case "matrix":
  946. return "range vector"
  947. default:
  948. return string(t)
  949. }
  950. }