include/punk_observe/po_parse.h

filter: Statementstmt 96.8% 427 / 441 Branchbran 87.7% 300 / 342 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 92.8% 727 / 783 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_live.h ↑ Summary include/punk_observe/po_tattr.h »
1 /* po_parse.h - recursive descent, into a bump allocator.
2 *
3 * The stage list is trivially LL(1); only the `where` expression needs
4 * precedence, and that is precedence climbing rather than a generator.
5 *
6 * EVERY NODE IS BUMP-ALLOCATED. The whole AST frees in one call, there is no
7 * ownership question, and no node can outlive the tree. Identifiers are
8 * COPIED rather than borrowed from the query string, because the string is
9 * an SV that Perl may free while the AST is still being planned.
10 *
11 * PRECEDENCE: `not` binds tightest, then `and`, then `or`. Getting that wrong
12 * does not produce an error, it produces plausible WRONG ANSWERS.
13 */
14 #ifndef PO_PARSE_H
15 #define PO_PARSE_H
16  
17 #include "punk_observe/po_compat.h"
18 #include "punk_observe/po_lex.h"
19 #include "punk_observe/po_query.h"
20  
21 /* The input is untrusted, so recursion is bounded. A thousand nested
22 * parentheses must be a refusal, not a blown C stack. */
23 #define PO_MAX_DEPTH 64
24  
25 typedef struct {
26 po_lex lex;
27 po_query *q;
28 int depth;
29 } po_parser;
30  
31 5076 static void po_perr(po_parser *p, size_t off, const char *fmt, const char *a) {
32 5076 ▶ if (p->q->failed) return;
Branch: 1 branch
BranchTrueFalseTotal
if (p->q->failed) return; 0 5076 5076
33  
34 /* THE LEXER'S MESSAGE WINS.
35 *
36 * When the lexer has already failed, the current token is PO_T_ERROR and
37 * the parser will duly complain that it "expected a value" - which is
38 * true and useless. The lexer knows it was an unterminated string, or an
39 * unknown duration unit, or a lone '!', and it knows where. Reporting the
40 * parser's generic disappointment instead throws that away and points at
41 * the wrong character. */
42 5076 ▶ if (p->lex.failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.failed) { 6 5070 5076
43 6 size_t n = strlen(p->lex.err);
44 6 ▶ if (n >= sizeof(p->q->err)) n = sizeof(p->q->err) - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= sizeof(p->q->err)) n = sizeof(p->q->err) - 1; 0 6 6
45 6 p->q->failed = 1;
46 6 p->q->err_off = p->lex.err_off;
47 6 memcpy(p->q->err, p->lex.err, n);
48 6 p->q->err[n] = '\0';
49 6 return;
50 }
51  
52 5070 p->q->failed = 1;
53 5070 p->q->err_off = off;
54 5070 ▶ if (a) {
Branch: 1 branch
BranchTrueFalseTotal
if (a) { 13 5057 5070
55 /* my_snprintf is not available in a plain header; build by hand so
56 * nothing here reaches for a Perl-flavoured formatter. */
57 13 size_t n = 0, i;
58 305 ▶ for (i = 0; fmt[i] && n < sizeof(p->q->err) - 1; i++) {
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; fmt[i] && n < sizeof(p->q->err) - 1; i++) { 292 13 305
for (i = 0; fmt[i] && n < sizeof(p->q->err) - 1; i++) { 292 0 292
59 292 ▶ if (fmt[i] == '%' && fmt[i + 1] == 's') {
Branch: 2 branches
BranchTrueFalseTotal
if (fmt[i] == '%' && fmt[i + 1] == 's') { 13 279 292
if (fmt[i] == '%' && fmt[i + 1] == 's') { 13 0 13
60 size_t k;
61 97 ▶ for (k = 0; a[k] && n < sizeof(p->q->err) - 1; k++)
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; a[k] && n < sizeof(p->q->err) - 1; k++) 84 13 97
for (k = 0; a[k] && n < sizeof(p->q->err) - 1; k++) 84 0 84
62 84 p->q->err[n++] = a[k];
63 13 i++;
64 }
65 279 else p->q->err[n++] = fmt[i];
66 }
67 13 p->q->err[n] = '\0';
68 }
69 else {
70 5057 size_t n = strlen(fmt);
71 5057 ▶ if (n >= sizeof(p->q->err)) n = sizeof(p->q->err) - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n >= sizeof(p->q->err)) n = sizeof(p->q->err) - 1; 0 5057 5057
72 5057 memcpy(p->q->err, fmt, n);
73 5057 p->q->err[n] = '\0';
74 }
75 }
76  
77 92451 static void *po_alloc(po_parser *p, size_t n) {
78 92451 return po_bump_alloc(&p->q->bump, n);
79 }
80  
81 /* Copy a token's text into the bump and return a stable pointer.
82 *
83 * COPIED, not borrowed from the query string: that string is an SV, and Perl
84 * may free it while the AST is still being planned. A borrowed field name is
85 * a use-after-free that only shows when the caller does something ordinary
86 * between parsing and executing. */
87 46204 static const char *po_intern_tok(po_parser *p, const po_tok *t, size_t *len) {
88 46204 char *d = (char *)po_bump_alloc(&p->q->bump, t->len + 1);
89 46204 ▶ if (!d) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return NULL; 0 46204 46204
90 46204 ▶ if (t->len) memcpy(d, t->p, t->len);
Branch: 1 branch
BranchTrueFalseTotal
if (t->len) memcpy(d, t->p, t->len); 46204 0 46204
91 46204 d[t->len] = '\0';
92 46204 *len = t->len;
93 46204 return d;
94 }
95  
96 /* A STRING token's text, with its QUOTING escapes resolved. The lexer skips
97 * `\"` without ending the string, and copying the bytes raw after that
98 * stored `he said \"hi\"` as the comparison value - a filter that parsed
99 * cleanly and could never match the body it plainly named. The two halves
100 * agree now.
101 *
102 * ONLY the quoting escapes: `\"`, `\'` and `\\`. Anything else keeps its
103 * backslash, because `=~ "a\db"` is somebody reaching for a regex class,
104 * and resolving it to `adb` would turn the plan's honest refusal into a
105 * silent substring match for a string they never wrote. */
106 20790 static const char *po_intern_str(po_parser *p, const po_tok *t, size_t *len) {
107 20790 char *d = (char *)po_bump_alloc(&p->q->bump, t->len + 1);
108 20790 size_t i, n = 0;
109 20790 ▶ if (!d) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!d) return NULL; 0 20790 20790
110 107006 ▶ for (i = 0; i < t->len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < t->len; i++) { 86216 20790 107006
111 86216 ▶ if (t->p[i] == '\\' && i + 1 < t->len
Branch: 2 branches
BranchTrueFalseTotal
if (t->p[i] == '\\' && i + 1 < t->len 6 86210 86216
if (t->p[i] == '\\' && i + 1 < t->len 6 0 6
112 6 ▶ && (t->p[i + 1] == '"' || t->p[i + 1] == '\''
Branch: 2 branches
BranchTrueFalseTotal
&& (t->p[i + 1] == '"' || t->p[i + 1] == '\'' 3 3 6
&& (t->p[i + 1] == '"' || t->p[i + 1] == '\'' 3 0 3
113 3 ▶ || t->p[i + 1] == '\\'))
Branch: 1 branch
BranchTrueFalseTotal
|| t->p[i + 1] == '\\')) 1 2 3
114 4 i++;
115 86216 d[n++] = t->p[i];
116 }
117 20790 d[n] = '\0';
118 20790 *len = n;
119 20790 return d;
120 }
121  
122 static po_expr *po_parse_or(po_parser *p);
123  
124 /* field op value */
125 25407 static po_expr *po_parse_cmp(po_parser *p) {
126 po_expr *e;
127 po_tok f;
128  
129 25407 ▶ if (p->lex.cur.kind != PO_T_IDENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_IDENT) { 3 25404 25407
130 3 po_perr(p, p->lex.cur.off, "expected a column or attribute name", NULL);
131 3 return NULL;
132 }
133 25404 f = p->lex.cur;
134 25404 po_lex_next(&p->lex);
135  
136 25404 ▶ if (p->lex.cur.kind != PO_T_OP) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_OP) { 3 25401 25404
137 3 po_perr(p, p->lex.cur.off,
138 "expected a comparison operator after the field", NULL);
139 3 return NULL;
140 }
141  
142 25401 e = (po_expr *)po_alloc(p, sizeof(po_expr));
143 25401 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 25401 25401
144 25401 e->kind = PO_E_CMP;
145 25401 e->op = p->lex.cur.op;
146 25401 e->field = po_intern_tok(p, &f, &e->field_len);
147 25401 po_lex_next(&p->lex);
148  
149 25401 switch (p->lex.cur.kind) {
150 20153 case PO_T_STRING:
151 20153 e->vkind = PO_V_STRING;
152 20153 e->sval = po_intern_str(p, &p->lex.cur, &e->sval_len);
153 20153 break;
154 109 case PO_T_NUMBER:
155 109 e->vkind = PO_V_NUMBER;
156 109 e->nval = p->lex.cur.num;
157 109 e->uval = p->lex.cur.inum;
158 109 break;
159 5039 case PO_T_DURATION:
160 5039 e->vkind = PO_V_DURATION;
161 5039 e->uval = p->lex.cur.dur;
162 5039 break;
163 94 case PO_T_IDENT: {
164 /* A bare word on the right is a SEVERITY name or nothing.
165 *
166 * It is deliberately NOT an implicit string: `where service = api`
167 * looks reasonable and is ambiguous with a column reference, so it
168 * is refused with a message saying to quote it. Accepting it would
169 * make a typo in a column name silently become a string
170 * comparison that never matches. */
171 po_u64 sev;
172 /* WHICH VOCABULARY depends on the column. `error` is a severity
173 * name AND a status name, and they are different numbers - 17 and
174 * 2 - so the field decides. Without this, `status = error` either
175 * compared against 17 and matched nothing, or went down the
176 * string path where `status` is not resolvable and matched
177 * nothing: the documented SYNOPSIS example returned an empty
178 * table on data that contained exactly what it asked for. */
179 188 int is_status = (e->field_len == 6
180 94 ▶ && memcmp(e->field, "status", 6) == 0);
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(e->field, "status", 6) == 0); 8 86 94
&& memcmp(e->field, "status", 6) == 0); 8 0 8
181 94 ▶ if (p->lex.cur.len == 4
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.len == 4 20 74 94
182 20 ▶ && memcmp(p->lex.cur.p, "null", 4) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(p->lex.cur.p, "null", 4) == 0) { 15 5 20
183 /* The absence test reads naturally only two ways round. An
184 * ordering against nothing would have to invent a meaning,
185 * and inventing meanings is how absent stopped being absent
186 * in every store that treats it as zero. */
187 15 ▶ if (e->op != PO_OP_EQ && e->op != PO_OP_NE) {
Branch: 2 branches
BranchTrueFalseTotal
if (e->op != PO_OP_EQ && e->op != PO_OP_NE) { 9 6 15
if (e->op != PO_OP_EQ && e->op != PO_OP_NE) { 2 7 9
188 2 po_perr(p, p->lex.cur.off,
189 "null only compares with = and !=", NULL);
190 5 return NULL;
191 }
192 13 e->vkind = PO_V_NULL;
193 }
194 79 ▶ else if (is_status
Branch: 1 branch
BranchTrueFalseTotal
else if (is_status 8 71 79
195 8 ▶ && po_status_value(p->lex.cur.p, p->lex.cur.len, &sev)) {
Branch: 1 branch
BranchTrueFalseTotal
&& po_status_value(p->lex.cur.p, p->lex.cur.len, &sev)) { 8 0 8
196 8 e->vkind = PO_V_SEVERITY; /* a name that became a number */
197 8 e->uval = sev;
198 }
199 71 ▶ else if (!is_status
Branch: 1 branch
BranchTrueFalseTotal
else if (!is_status 71 0 71
200 71 ▶ && po_severity_value(p->lex.cur.p, p->lex.cur.len, &sev)) {
Branch: 1 branch
BranchTrueFalseTotal
&& po_severity_value(p->lex.cur.p, p->lex.cur.len, &sev)) { 68 3 71
201 68 e->vkind = PO_V_SEVERITY;
202 68 e->uval = sev;
203 }
204 else {
205 3 po_perr(p, p->lex.cur.off,
206 "a bare word is not a value: quote it, as in \"...\"",
207 NULL);
208 3 return NULL;
209 }
210 89 break;
211 }
212 6 default:
213 6 po_perr(p, p->lex.cur.off, "expected a value", NULL);
214 6 return NULL;
215 }
216 25390 po_lex_next(&p->lex);
217 25390 return e;
218 }
219  
220 25498 static po_expr *po_parse_unary(po_parser *p) {
221 25498 ▶ if (++p->depth > PO_MAX_DEPTH) {
Branch: 1 branch
BranchTrueFalseTotal
if (++p->depth > PO_MAX_DEPTH) { 1 25497 25498
222 1 po_perr(p, p->lex.cur.off, "expression nested too deeply", NULL);
223 1 p->depth--;
224 1 return NULL;
225 }
226 25497 ▶ if (po_tok_is(&p->lex.cur, "not")) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "not")) { 2 25495 25497
227 2 po_expr *e = (po_expr *)po_alloc(p, sizeof(po_expr));
228 2 po_lex_next(&p->lex);
229 2 ▶ if (!e) { p->depth--; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { p->depth--; return NULL; } 0 2 2
230 2 e->kind = PO_E_NOT;
231 2 e->a = po_parse_unary(p);
232 2 p->depth--;
233 2 ▶ return e->a ? e : NULL;
Branch: 1 branch
BranchTrueFalseTotal
return e->a ? e : NULL; 2 0 2
234 }
235 25495 ▶ if (p->lex.cur.kind == PO_T_LPAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind == PO_T_LPAREN) { 98 25397 25495
236 po_expr *e;
237 98 po_lex_next(&p->lex);
238 98 e = po_parse_or(p);
239 98 ▶ if (!e) { p->depth--; return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!e) { p->depth--; return NULL; } 64 34 98
240 34 ▶ if (p->lex.cur.kind != PO_T_RPAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_RPAREN) { 1 33 34
241 1 po_perr(p, p->lex.cur.off, "expected ')'", NULL);
242 1 p->depth--;
243 1 return NULL;
244 }
245 33 po_lex_next(&p->lex);
246 33 p->depth--;
247 33 return e;
248 }
249 {
250 25397 po_expr *e = po_parse_cmp(p);
251 25397 p->depth--;
252 25397 return e;
253 }
254 }
255  
256 /* `and` binds tighter than `or`. */
257 15471 static po_expr *po_parse_and(po_parser *p) {
258 15471 po_expr *l = po_parse_unary(p);
259 15471 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 83 15388 15471
260 25413 ▶ while (po_tok_is(&p->lex.cur, "and")) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_tok_is(&p->lex.cur, "and")) { 10025 15388 25413
261 10025 po_expr *n = (po_expr *)po_alloc(p, sizeof(po_expr));
262 10025 po_lex_next(&p->lex);
263 10025 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 10025 10025
264 10025 n->kind = PO_E_AND;
265 10025 n->a = l;
266 10025 n->b = po_parse_unary(p);
267 10025 ▶ if (!n->b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n->b) return NULL; 0 10025 10025
268 10025 l = n;
269 }
270 15388 return l;
271 }
272  
273 15471 static po_expr *po_parse_or(po_parser *p) {
274 15471 po_expr *l = po_parse_and(p);
275 15471 ▶ if (!l) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!l) return NULL; 83 15388 15471
276 15396 ▶ while (po_tok_is(&p->lex.cur, "or")) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_tok_is(&p->lex.cur, "or")) { 8 15388 15396
277 8 po_expr *n = (po_expr *)po_alloc(p, sizeof(po_expr));
278 8 po_lex_next(&p->lex);
279 8 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 8 8
280 8 n->kind = PO_E_OR;
281 8 n->a = l;
282 8 n->b = po_parse_or(p); /* right-assoc is fine for a boolean or */
283 8 ▶ if (!n->b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n->b) return NULL; 0 8 8
284 8 l = n;
285 }
286 15388 return l;
287 }
288  
289 /* Walk an expression checking every field against the source. */
290 35425 static int po_check_expr(po_parser *p, const po_expr *e, int src) {
291 35425 ▶ if (!e) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return 1; 1 35424 35425
292 35424 ▶ if (e->kind == PO_E_CMP) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->kind == PO_E_CMP) { 25389 10035 35424
293 25389 int elsewhere = 0;
294 25389 int ok = po_column_ok(e->field, e->field_len, src, &elsewhere);
295 25389 ▶ if (ok == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok == 0) { 5010 20379 25389
296 char msg[192];
297 5010 size_t n = 0, i;
298 5010 const char *pre = "column '";
299 5010 const char *mid = "' does not exist on ";
300 5010 const char *suf = " (it belongs to another signal)";
301 45090 ▶ for (i = 0; pre[i]; i++) msg[n++] = pre[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; pre[i]; i++) msg[n++] = pre[i]; 40080 5010 45090
302 45081 ▶ for (i = 0; i < e->field_len && n < 150; i++) msg[n++] = e->field[i];
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < e->field_len && n < 150; i++) msg[n++] = e->field[i]; 40071 5010 45081
for (i = 0; i < e->field_len && n < 150; i++) msg[n++] = e->field[i]; 40071 0 40071
303 105210 ▶ for (i = 0; mid[i]; i++) msg[n++] = mid[i];
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; mid[i]; i++) msg[n++] = mid[i]; 100200 5010 105210
304 5010 { const char *s = po_src_name(src);
305 20049 ▶ for (i = 0; s[i]; i++) msg[n++] = s[i]; }
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; s[i]; i++) msg[n++] = s[i]; } 15039 5010 20049
306 160320 ▶ for (i = 0; suf[i] && n < 190; i++) msg[n++] = suf[i];
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; suf[i] && n < 190; i++) msg[n++] = suf[i]; 155310 5010 160320
for (i = 0; suf[i] && n < 190; i++) msg[n++] = suf[i]; 155310 0 155310
307 5010 msg[n] = '\0';
308 5010 po_perr(p, 0, msg, NULL);
309 5010 return 0;
310 }
311 20379 return 1;
312 }
313 10035 ▶ if (!po_check_expr(p, e->a, src)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_check_expr(p, e->a, src)) return 0; 1 10034 10035
314 10034 ▶ if (!po_check_expr(p, e->b, src)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_check_expr(p, e->b, src)) return 0; 2 10032 10034
315 10032 return 1;
316 }
317  
318 40854 static int po_agg_of(const po_tok *t) {
319 40854 ▶ if (po_tok_is(t, "count")) return PO_AGG_COUNT;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "count")) return PO_AGG_COUNT; 684 40170 40854
320 40170 ▶ if (po_tok_is(t, "sum")) return PO_AGG_SUM;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "sum")) return PO_AGG_SUM; 3 40167 40170
321 40167 ▶ if (po_tok_is(t, "avg")) return PO_AGG_AVG;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "avg")) return PO_AGG_AVG; 2 40165 40167
322 40165 ▶ if (po_tok_is(t, "min")) return PO_AGG_MIN;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "min")) return PO_AGG_MIN; 22 40143 40165
323 40143 ▶ if (po_tok_is(t, "max")) return PO_AGG_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "max")) return PO_AGG_MAX; 18 40125 40143
324 40125 ▶ if (po_tok_is(t, "p50")) return PO_AGG_P50;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "p50")) return PO_AGG_P50; 2 40123 40125
325 40123 ▶ if (po_tok_is(t, "p90")) return PO_AGG_P90;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "p90")) return PO_AGG_P90; 1 40122 40123
326 40122 ▶ if (po_tok_is(t, "p95")) return PO_AGG_P95;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "p95")) return PO_AGG_P95; 18 40104 40122
327 40104 ▶ if (po_tok_is(t, "p99")) return PO_AGG_P99;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "p99")) return PO_AGG_P99; 10005 30099 40104
328 30099 ▶ if (po_tok_is(t, "distinct")) return PO_AGG_DISTINCT;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(t, "distinct")) return PO_AGG_DISTINCT; 4 30095 30099
329 30095 return 0;
330 }
331  
332 /* ---- stages ---------------------------------------------------------------
333 *
334 * THE RE-KEYING RULE, which is the feature.
335 *
336 * `| traces` needs a trace_id on the incoming rows. Logs and spans have one;
337 * `| exemplars` produces one; a bare metric stream does NOT. So
338 *
339 * metric x | traces
340 *
341 * is a parse error, and the message says to add `| exemplars` first. That
342 * message is part of the deliverable: this is the thing the whole project is
343 * built for, and it has to be discoverable by typing rather than by reading
344 * the manual.
345 */
346 20034 static int po_has_trace_key(int src, const po_stage *upto) {
347 const po_stage *s;
348 20034 ▶ if (src == PO_SRC_LOG || src == PO_SRC_TRACE || src == PO_SRC_SPAN) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (src == PO_SRC_LOG || src == PO_SRC_TRACE || src == PO_SRC_SPAN) return 1; 20033 1 20034
if (src == PO_SRC_LOG || src == PO_SRC_TRACE || src == PO_SRC_SPAN) return 1; 20030 3 20033
if (src == PO_SRC_LOG || src == PO_SRC_TRACE || src == PO_SRC_SPAN) return 1; 0 20030 20030
349 60044 ▶ for (s = upto; s; s = s->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = upto; s; s = s->next) { 60039 5 60044
350 60039 ▶ if (s->kind == PO_ST_EXEMPLARS) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (s->kind == PO_ST_EXEMPLARS) return 1; 20025 40014 60039
351 40014 ▶ if (s->kind == PO_ST_TRACES || s->kind == PO_ST_LOGS
Branch: 2 branches
BranchTrueFalseTotal
if (s->kind == PO_ST_TRACES || s->kind == PO_ST_LOGS 40014 0 40014
if (s->kind == PO_ST_TRACES || s->kind == PO_ST_LOGS 40014 0 40014
352 40014 ▶ || s->kind == PO_ST_SPANS) return 1;
Branch: 1 branch
BranchTrueFalseTotal
|| s->kind == PO_ST_SPANS) return 1; 0 40014 40014
353 }
354 5 return 0;
355 }
356  
357 /* `by` attaches to the aggregate it groups.
358 *
359 * The plan's grammar listed `by` as its own stage, but every example in it
360 * writes `| count by service` and `| p99 by http.route` as ONE thing - and so
361 * does everybody who tries the language. The examples are the specification
362 * here: an aggregate and its grouping are one stage. */
363 10768 static int po_absorb_by(po_parser *p, po_stage *st) {
364 10768 ▶ if (!po_tok_is(&p->lex.cur, "by")) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_tok_is(&p->lex.cur, "by")) return 1; 216 10552 10768
365 10552 po_lex_next(&p->lex);
366 for (;;) {
367 10565 ▶ if (p->lex.cur.kind != PO_T_IDENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_IDENT) { 3 10562 10565
368 3 po_perr(p, p->lex.cur.off, "by takes field names", NULL);
369 3 return 0;
370 }
371 10562 ▶ if (st->nfields >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->nfields >= 8) { 0 10562 10562
372 0 po_perr(p, p->lex.cur.off, "too many group-by fields", NULL);
373 0 return 0;
374 }
375 21124 st->fields[st->nfields] =
376 10562 po_intern_tok(p, &p->lex.cur, &st->field_lens[st->nfields]);
377 10562 st->nfields++;
378 10562 po_lex_next(&p->lex);
379 10562 ▶ if (p->lex.cur.kind != PO_T_COMMA) break;
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_COMMA) break; 10549 13 10562
380 13 po_lex_next(&p->lex);
381 }
382 10549 return 1;
383 }
384  
385 57014 static po_stage *po_parse_stage(po_parser *p, int *src) {
386 57014 po_stage *st = (po_stage *)po_alloc(p, sizeof(po_stage));
387 57014 ▶ if (!st) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!st) return NULL; 0 57014 57014
388  
389 57014 ▶ if (po_tok_is(&p->lex.cur, "where")) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "where")) { 15365 41649 57014
390 15365 st->kind = PO_ST_WHERE;
391 15365 po_lex_next(&p->lex);
392 15365 st->expr = po_parse_or(p);
393 15365 ▶ if (!st->expr) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!st->expr) return NULL; 19 15346 15365
394 15346 ▶ if (!po_check_expr(p, st->expr, *src)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_check_expr(p, st->expr, *src)) return NULL; 5009 10337 15346
395 10337 return st;
396 }
397 41649 ▶ if (po_tok_is(&p->lex.cur, "search")) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "search")) { 638 41011 41649
398 638 st->kind = PO_ST_SEARCH;
399 638 po_lex_next(&p->lex);
400 638 ▶ if (p->lex.cur.kind != PO_T_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_STRING) { 1 637 638
401 1 po_perr(p, p->lex.cur.off, "search takes a quoted string", NULL);
402 1 return NULL;
403 }
404 637 st->str = po_intern_str(p, &p->lex.cur, &st->str_len);
405 637 po_lex_next(&p->lex);
406 637 ▶ if (*src == PO_SRC_METRIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (*src == PO_SRC_METRIC) { 1 636 637
407 1 po_perr(p, 0,
408 "search needs text: a metric stream has no body to search",
409 NULL);
410 1 return NULL;
411 }
412 636 return st;
413 }
414 41011 ▶ if (po_tok_is(&p->lex.cur, "by")) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "by")) { 56 40955 41011
415 56 st->kind = PO_ST_BY;
416 56 po_lex_next(&p->lex);
417 for (;;) {
418 56 ▶ if (p->lex.cur.kind != PO_T_IDENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_IDENT) { 0 56 56
419 0 po_perr(p, p->lex.cur.off, "by takes field names", NULL);
420 0 return NULL;
421 }
422 56 ▶ if (st->nfields >= 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (st->nfields >= 8) { 0 56 56
423 0 po_perr(p, p->lex.cur.off, "too many group-by fields", NULL);
424 0 return NULL;
425 }
426 112 st->fields[st->nfields] =
427 56 po_intern_tok(p, &p->lex.cur, &st->field_lens[st->nfields]);
428 56 st->nfields++;
429 56 po_lex_next(&p->lex);
430 56 ▶ if (p->lex.cur.kind != PO_T_COMMA) break;
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_COMMA) break; 56 0 56
431 0 po_lex_next(&p->lex);
432 }
433 56 return st;
434 }
435 /* BUCKET AND RATE ARE THE SAME STAGE WITH DIFFERENT UNITS.
436 *
437 * Both cut the window into equal spans and aggregate within each. The
438 * only difference is what the number means at the end: `bucket` reports
439 * the aggregate, `rate` divides it by the span so the answer is per
440 * second and does not change when somebody widens the window.
441 *
442 * One parse, therefore, and one execution path. Two would be two places
443 * for the boundary arithmetic to disagree, and the disagreement would
444 * show up as a rate chart and a count chart that do not line up. */
445 40955 ▶ if (po_tok_is(&p->lex.cur, "bucket") || po_tok_is(&p->lex.cur, "rate")) {
Branch: 2 branches
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "bucket") || po_tok_is(&p->lex.cur, "rate")) { 40375 580 40955
if (po_tok_is(&p->lex.cur, "bucket") || po_tok_is(&p->lex.cur, "rate")) { 16 40359 40375
446 596 const int is_rate = po_tok_is(&p->lex.cur, "rate");
447 /* The example carries the stage's own name, which is why the messages
448 * below need only one substitution: po_perr expands every %s from the
449 * same argument, so a message wanting both the word and an example of
450 * it could not have them. */
451 596 ▶ const char *eg = is_rate ? "rate(5m)" : "bucket(5m)";
Branch: 1 branch
BranchTrueFalseTotal
const char *eg = is_rate ? "rate(5m)" : "bucket(5m)"; 16 580 596
452 /* THE MESSAGE NAMES THE STAGE. "takes a window" does not say what
453 * takes one, and a parse error that does not name the thing it is
454 * about sends the reader back to count pipes. po_perr substitutes one
455 * argument, so the two messages are written out rather than built. */
456 596 const char *want = is_rate ? "rate takes a window, as in rate(5m)"
457 596 ▶ : "bucket takes a window, as in bucket(5m)";
Branch: 1 branch
BranchTrueFalseTotal
: "bucket takes a window, as in bucket(5m)"; 16 580 596
458 596 ▶ st->kind = is_rate ? PO_ST_RATE : PO_ST_BUCKET;
Branch: 1 branch
BranchTrueFalseTotal
st->kind = is_rate ? PO_ST_RATE : PO_ST_BUCKET; 16 580 596
459 596 po_lex_next(&p->lex);
460 596 ▶ if (p->lex.cur.kind != PO_T_LPAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_LPAREN) { 2 594 596
461 2 po_perr(p, p->lex.cur.off, want, NULL);
462 2 return NULL;
463 }
464 594 po_lex_next(&p->lex);
465 594 ▶ if (p->lex.cur.kind != PO_T_DURATION) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_DURATION) { 2 592 594
466 2 po_perr(p, p->lex.cur.off, "needs a duration, as in %s", eg);
467 2 return NULL;
468 }
469 /* A ZERO WINDOW IS A DIVISION BY ZERO AND AN UNBOUNDED BUCKET COUNT.
470 * The lexer will not produce one from `5m`, but `0s` lexes fine. */
471 592 ▶ if (p->lex.cur.dur == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.dur == 0) { 2 590 592
472 2 po_perr(p, p->lex.cur.off, "the window cannot be zero, as in %s", eg);
473 2 return NULL;
474 }
475 590 st->dur = p->lex.cur.dur;
476 590 po_lex_next(&p->lex);
477 590 ▶ if (p->lex.cur.kind != PO_T_RPAREN) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_RPAREN) { 1 589 590
478 1 po_perr(p, p->lex.cur.off, "expected ')' after the window, as in %s", eg);
479 1 return NULL;
480 }
481 589 po_lex_next(&p->lex);
482  
483 /* An aggregate may follow, and counting is what it means without one:
484 * `bucket(1m)` alone is the histogram of arrivals, which is the
485 * commonest thing anybody wants from a bucketed query.
486 *
487 * Left at 0 rather than defaulted to count HERE, because the stage
488 * can also be written apart from its aggregate - `| p95 | bucket(1m)`
489 * - and a default applied at parse time would overwrite the p95 with
490 * a count of rows. The planner applies it only if nothing else did. */
491 589 st->agg = po_agg_of(&p->lex.cur);
492 589 ▶ if (st->agg) po_lex_next(&p->lex);
Branch: 1 branch
BranchTrueFalseTotal
if (st->agg) po_lex_next(&p->lex); 566 23 589
493  
494 589 ▶ if (!po_absorb_by(p, st)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_absorb_by(p, st)) return NULL; 0 589 589
495 589 return st;
496 }
497 40359 ▶ if (po_tok_is(&p->lex.cur, "limit") || po_tok_is(&p->lex.cur, "slowest")) {
Branch: 2 branches
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "limit") || po_tok_is(&p->lex.cur, "slowest")) { 40303 56 40359
if (po_tok_is(&p->lex.cur, "limit") || po_tok_is(&p->lex.cur, "slowest")) { 21 40282 40303
498 77 ▶ st->kind = po_tok_is(&p->lex.cur, "limit") ? PO_ST_LIMIT : PO_ST_SLOWEST;
Branch: 1 branch
BranchTrueFalseTotal
st->kind = po_tok_is(&p->lex.cur, "limit") ? PO_ST_LIMIT : PO_ST_SLOWEST; 56 21 77
499 77 ▶ if (st->kind == PO_ST_SLOWEST && *src == PO_SRC_METRIC) {
Branch: 2 branches
BranchTrueFalseTotal
if (st->kind == PO_ST_SLOWEST && *src == PO_SRC_METRIC) { 21 56 77
if (st->kind == PO_ST_SLOWEST && *src == PO_SRC_METRIC) { 1 20 21
500 1 po_perr(p, p->lex.cur.off,
501 "slowest needs a duration: a metric stream has none", NULL);
502 1 return NULL;
503 }
504 76 po_lex_next(&p->lex);
505 76 ▶ if (p->lex.cur.kind != PO_T_NUMBER || !p->lex.cur.is_int) {
Branch: 2 branches
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_NUMBER || !p->lex.cur.is_int) { 75 1 76
if (p->lex.cur.kind != PO_T_NUMBER || !p->lex.cur.is_int) { 1 74 75
506 2 po_perr(p, p->lex.cur.off, "expected a whole number", NULL);
507 2 return NULL;
508 }
509 74 st->num = p->lex.cur.inum;
510 74 po_lex_next(&p->lex);
511 74 return st;
512 }
513 40282 ▶ if (po_tok_is(&p->lex.cur, "sort")) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "sort")) { 16 40266 40282
514 16 st->kind = PO_ST_SORT;
515 16 po_lex_next(&p->lex);
516 16 ▶ if (p->lex.cur.kind != PO_T_IDENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_IDENT) { 1 15 16
517 1 po_perr(p, p->lex.cur.off, "sort takes a field name", NULL);
518 1 return NULL;
519 }
520 15 st->fields[0] = po_intern_tok(p, &p->lex.cur, &st->field_lens[0]);
521 15 st->nfields = 1;
522 15 po_lex_next(&p->lex);
523 15 ▶ if (po_tok_is(&p->lex.cur, "desc")) { st->desc = 1; po_lex_next(&p->lex); }
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "desc")) { st->desc = 1; po_lex_next(&p->lex); } 8 7 15
524 7 ▶ else if (po_tok_is(&p->lex.cur, "asc")) po_lex_next(&p->lex);
Branch: 1 branch
BranchTrueFalseTotal
else if (po_tok_is(&p->lex.cur, "asc")) po_lex_next(&p->lex); 2 5 7
525 15 return st;
526 }
527 40266 ▶ if (po_tok_is(&p->lex.cur, "top")) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "top")) { 15 40251 40266
528 15 st->kind = PO_ST_TOPN;
529 15 po_lex_next(&p->lex);
530 15 ▶ if (p->lex.cur.kind != PO_T_NUMBER || !p->lex.cur.is_int) {
Branch: 2 branches
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_NUMBER || !p->lex.cur.is_int) { 15 0 15
if (p->lex.cur.kind != PO_T_NUMBER || !p->lex.cur.is_int) { 0 15 15
531 0 po_perr(p, p->lex.cur.off, "top takes a whole number", NULL);
532 0 return NULL;
533 }
534 15 st->num = p->lex.cur.inum;
535 15 po_lex_next(&p->lex);
536 15 ▶ if (!po_tok_is(&p->lex.cur, "by")) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_tok_is(&p->lex.cur, "by")) { 1 14 15
537 1 po_perr(p, p->lex.cur.off, "top N needs 'by', as in top 10 by count", NULL);
538 1 return NULL;
539 }
540 14 po_lex_next(&p->lex);
541 14 st->agg = po_agg_of(&p->lex.cur);
542 14 ▶ if (!st->agg) {
Branch: 1 branch
BranchTrueFalseTotal
if (!st->agg) { 1 13 14
543 1 po_perr(p, p->lex.cur.off, "top N by needs an aggregate", NULL);
544 1 return NULL;
545 }
546 13 po_lex_next(&p->lex);
547 13 return st;
548 }
549 {
550 40251 int a = po_agg_of(&p->lex.cur);
551 40251 ▶ if (a) {
Branch: 1 branch
BranchTrueFalseTotal
if (a) { 10180 30071 40251
552 10180 st->kind = PO_ST_AGG;
553 10180 st->agg = a;
554 /* A percentile over a log BODY is meaningless. Named here rather
555 * than discovered as a strange number later. */
556 10180 ▶ if (*src == PO_SRC_LOG && a >= PO_AGG_SUM && a != PO_AGG_DISTINCT) {
Branch: 3 branches
BranchTrueFalseTotal
if (*src == PO_SRC_LOG && a >= PO_AGG_SUM && a != PO_AGG_DISTINCT) { 113 10067 10180
if (*src == PO_SRC_LOG && a >= PO_AGG_SUM && a != PO_AGG_DISTINCT) { 3 110 113
if (*src == PO_SRC_LOG && a >= PO_AGG_SUM && a != PO_AGG_DISTINCT) { 1 2 3
557 1 po_perr(p, p->lex.cur.off,
558 "that aggregate needs a numeric column; a log stream has none",
559 NULL);
560 1 return NULL;
561 }
562 10179 po_lex_next(&p->lex);
563 10179 ▶ if (!po_absorb_by(p, st)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_absorb_by(p, st)) return NULL; 3 10176 10179
564 10176 return st;
565 }
566 }
567  
568 /* the re-keying stages */
569 30071 ▶ if (po_tok_is(&p->lex.cur, "exemplars")) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "exemplars")) { 10028 20043 30071
570 10028 ▶ if (*src != PO_SRC_METRIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (*src != PO_SRC_METRIC) { 2 10026 10028
571 2 po_perr(p, p->lex.cur.off,
572 "exemplars come from a metric stream", NULL);
573 2 return NULL;
574 }
575 10026 st->kind = PO_ST_EXEMPLARS;
576 10026 po_lex_next(&p->lex);
577 10026 return st;
578 }
579 20043 ▶ if (po_tok_is(&p->lex.cur, "traces") || po_tok_is(&p->lex.cur, "logs")
Branch: 2 branches
BranchTrueFalseTotal
if (po_tok_is(&p->lex.cur, "traces") || po_tok_is(&p->lex.cur, "logs") 10024 10019 20043
if (po_tok_is(&p->lex.cur, "traces") || po_tok_is(&p->lex.cur, "logs") 10 10014 10024
580 10 ▶ || po_tok_is(&p->lex.cur, "spans")) {
Branch: 1 branch
BranchTrueFalseTotal
|| po_tok_is(&p->lex.cur, "spans")) { 1 9 10
581 20034 int k = po_tok_is(&p->lex.cur, "traces") ? PO_ST_TRACES
582 20034 ▶ : po_tok_is(&p->lex.cur, "logs") ? PO_ST_LOGS : PO_ST_SPANS;
Branch: 2 branches
BranchTrueFalseTotal
: po_tok_is(&p->lex.cur, "logs") ? PO_ST_LOGS : PO_ST_SPANS; 10015 10019 20034
: po_tok_is(&p->lex.cur, "logs") ? PO_ST_LOGS : PO_ST_SPANS; 10014 1 10015
583 20034 st->kind = k;
584 20034 po_lex_next(&p->lex);
585 20034 return st;
586 }
587  
588 /* NAMED, because this message now travels: it is written onto a broken
589 * alert rule's state row and read days later, where "unknown stage" with
590 * nothing to say which one is a red word whose next action is guessing. */
591 9 ▶ if (p->lex.cur.kind == PO_T_IDENT && p->lex.cur.len) {
Branch: 2 branches
BranchTrueFalseTotal
if (p->lex.cur.kind == PO_T_IDENT && p->lex.cur.len) { 8 1 9
if (p->lex.cur.kind == PO_T_IDENT && p->lex.cur.len) { 8 0 8
592 char w[48];
593 8 size_t n = p->lex.cur.len < sizeof(w) - 1 ? p->lex.cur.len
594 : sizeof(w) - 1;
595 8 memcpy(w, p->lex.cur.p, n);
596 8 w[n] = '\0';
597 8 po_perr(p, p->lex.cur.off, "unknown stage '%s'", w);
598 8 return NULL;
599 }
600 1 po_perr(p, p->lex.cur.off, "unknown stage", NULL);
601 1 return NULL;
602 }
603  
604 /* The {a="b", c="d"} selector: sugar for a leading `where` of equalities.
605 *
606 * `log {service="api"} | search "refused"` is the shape people type, and
607 * forcing `| where` for the common case is friction for nothing. */
608 9 static po_expr *po_parse_selector(po_parser *p, int src) {
609 9 po_expr *acc = NULL;
610 9 po_lex_next(&p->lex); /* past '{' */
611 9 ▶ if (p->lex.cur.kind == PO_T_RBRACE) { po_lex_next(&p->lex); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind == PO_T_RBRACE) { po_lex_next(&p->lex); return NULL; } 9 0 9
612 1 for (;;) {
613 10 po_expr *e = po_parse_cmp(p);
614 10 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 10 10
615 10 ▶ if (!po_check_expr(p, e, src)) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_check_expr(p, e, src)) return NULL; 1 9 10
616 9 ▶ if (!acc) acc = e;
Branch: 1 branch
BranchTrueFalseTotal
if (!acc) acc = e; 8 1 9
617 else {
618 1 po_expr *n = (po_expr *)po_alloc(p, sizeof(po_expr));
619 1 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 1 1
620 1 n->kind = PO_E_AND; n->a = acc; n->b = e;
621 1 acc = n;
622 }
623 9 ▶ if (p->lex.cur.kind == PO_T_COMMA) { po_lex_next(&p->lex); continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind == PO_T_COMMA) { po_lex_next(&p->lex); continue; } 1 8 9
624 8 break;
625 }
626 8 ▶ if (p->lex.cur.kind != PO_T_RBRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if (p->lex.cur.kind != PO_T_RBRACE) { 1 7 8
627 1 po_perr(p, p->lex.cur.off, "expected '}'", NULL);
628 1 return NULL;
629 }
630 7 po_lex_next(&p->lex);
631 7 return acc;
632 }
633  
634 16933 static int po_parse(po_query *q, const char *src, size_t len) {
635 po_parser p;
636 16933 po_stage *tail = NULL;
637  
638 16933 memset(q, 0, sizeof(*q));
639 16933 memset(&p, 0, sizeof(p));
640 16933 p.q = q;
641 16933 po_lex_init(&p.lex, src, len);
642 16933 po_lex_next(&p.lex);
643  
644 16933 ▶ if (p.lex.failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (p.lex.failed) { 0 16933 16933
645 0 q->failed = 1; q->err_off = p.lex.err_off;
646 0 memcpy(q->err, p.lex.err, sizeof(p.lex.err) < sizeof(q->err)
647 ? sizeof(p.lex.err) : sizeof(q->err) - 1);
648 0 return 0;
649 }
650  
651 16933 ▶ if (po_tok_is(&p.lex.cur, "metric")) q->source = PO_SRC_METRIC;
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p.lex.cur, "metric")) q->source = PO_SRC_METRIC; 10138 6795 16933
652 6795 ▶ else if (po_tok_is(&p.lex.cur, "log")
Branch: 1 branch
BranchTrueFalseTotal
else if (po_tok_is(&p.lex.cur, "log") 608 6187 6795
653 6795 ▶ || po_tok_is(&p.lex.cur, "logs")) q->source = PO_SRC_LOG;
Branch: 1 branch
BranchTrueFalseTotal
|| po_tok_is(&p.lex.cur, "logs")) q->source = PO_SRC_LOG; 1 607 608
654 607 ▶ else if (po_tok_is(&p.lex.cur, "trace")
Branch: 1 branch
BranchTrueFalseTotal
else if (po_tok_is(&p.lex.cur, "trace") 571 36 607
655 607 ▶ || po_tok_is(&p.lex.cur, "traces")) q->source = PO_SRC_TRACE;
Branch: 1 branch
BranchTrueFalseTotal
|| po_tok_is(&p.lex.cur, "traces")) q->source = PO_SRC_TRACE; 1 570 571
656 570 ▶ else if (po_tok_is(&p.lex.cur, "span")
Branch: 1 branch
BranchTrueFalseTotal
else if (po_tok_is(&p.lex.cur, "span") 569 1 570
657 570 ▶ || po_tok_is(&p.lex.cur, "spans")) q->source = PO_SRC_SPAN;
Branch: 1 branch
BranchTrueFalseTotal
|| po_tok_is(&p.lex.cur, "spans")) q->source = PO_SRC_SPAN; 562 7 569
658 else {
659 7 po_perr(&p, p.lex.cur.off,
660 "a query starts with metric, log, trace or spans", NULL);
661 7 return 0;
662 }
663 16926 po_lex_next(&p.lex);
664  
665 16926 ▶ if (q->source == PO_SRC_METRIC) {
Branch: 1 branch
BranchTrueFalseTotal
if (q->source == PO_SRC_METRIC) { 10138 6788 16926
666 10138 ▶ if (p.lex.cur.kind != PO_T_IDENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (p.lex.cur.kind != PO_T_IDENT) { 1 10137 10138
667 1 po_perr(&p, p.lex.cur.off, "metric needs a name", NULL);
668 1 return 0;
669 }
670 10137 q->name = po_intern_tok(&p, &p.lex.cur, &q->name_len);
671 10137 po_lex_next(&p.lex);
672 }
673  
674 16925 ▶ if (p.lex.cur.kind == PO_T_LBRACE) {
Branch: 1 branch
BranchTrueFalseTotal
if (p.lex.cur.kind == PO_T_LBRACE) { 9 16916 16925
675 9 q->selector = po_parse_selector(&p, q->source);
676 9 ▶ if (q->failed) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (q->failed) return 0; 2 7 9
677 }
678  
679 68905 ▶ while (p.lex.cur.kind == PO_T_PIPE) {
Branch: 1 branch
BranchTrueFalseTotal
while (p.lex.cur.kind == PO_T_PIPE) { 57048 11857 68905
680 po_stage *st;
681 57048 po_lex_next(&p.lex);
682  
683 /* `viz` is consumed HERE, before the stage parser, so it never
684 * enters the stage list: presentation is a property of the query,
685 * not a transform of its rows. Validated strictly against the five
686 * the panel renderer knows - po_viz_from's fall-back-to-table is
687 * right for a parser reading a stored row and wrong for a person
688 * typing, where an unknown name is a typo to name, not a table to
689 * silently draw. t/0912 asserts this list against viz_names(). */
690 57048 ▶ if (po_tok_is(&p.lex.cur, "viz")) {
Branch: 1 branch
BranchTrueFalseTotal
if (po_tok_is(&p.lex.cur, "viz")) { 34 57014 57048
691 static const char *const VZ[] =
692 { "line", "area", "bar", "stat", "table", NULL };
693 34 const char *vp; size_t vl; int ok = 0, vi;
694 34 ▶ if (q->viz) {
Branch: 1 branch
BranchTrueFalseTotal
if (q->viz) { 1 33 34
695 1 po_perr(&p, p.lex.cur.off,
696 "viz appears twice; one chart per answer", NULL);
697 2 return 0;
698 }
699 33 po_lex_next(&p.lex);
700 33 ▶ if (p.lex.cur.kind != PO_T_IDENT
Branch: 1 branch
BranchTrueFalseTotal
if (p.lex.cur.kind != PO_T_IDENT 1 32 33
701 1 ▶ && p.lex.cur.kind != PO_T_STRING) {
Branch: 1 branch
BranchTrueFalseTotal
&& p.lex.cur.kind != PO_T_STRING) { 0 1 1
702 0 po_perr(&p, p.lex.cur.off,
703 "viz takes a chart kind: line, area, bar, stat or table",
704 NULL);
705 0 return 0;
706 }
707 33 vp = po_intern_tok(&p, &p.lex.cur, &vl);
708 33 ▶ if (!vp) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!vp) return 0; 0 33 33
709 98 ▶ for (vi = 0; VZ[vi]; vi++)
Branch: 1 branch
BranchTrueFalseTotal
for (vi = 0; VZ[vi]; vi++) 97 1 98
710 97 ▶ if (strlen(VZ[vi]) == vl && memcmp(VZ[vi], vp, vl) == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (strlen(VZ[vi]) == vl && memcmp(VZ[vi], vp, vl) == 0) 37 60 97
if (strlen(VZ[vi]) == vl && memcmp(VZ[vi], vp, vl) == 0) 32 5 37
711 32 { ok = 1; break; }
712 33 ▶ if (!ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) { 1 32 33
713 1 po_perr(&p, p.lex.cur.off,
714 "that is not a chart kind - viz takes line, area, bar, "
715 "stat or table", NULL);
716 1 return 0;
717 }
718 32 q->viz = vp; q->viz_len = vl;
719 32 po_lex_next(&p.lex);
720 32 continue;
721 }
722  
723 57014 st = po_parse_stage(&p, &q->source);
724 57014 ▶ if (!st) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!st) return 0; 5058 51956 57014
725  
726 /* The re-keying check runs against what came BEFORE this stage. */
727 51956 ▶ if (st->kind == PO_ST_TRACES || st->kind == PO_ST_LOGS
Branch: 2 branches
BranchTrueFalseTotal
if (st->kind == PO_ST_TRACES || st->kind == PO_ST_LOGS 41937 10019 51956
if (st->kind == PO_ST_TRACES || st->kind == PO_ST_LOGS 31923 10014 41937
728 31923 ▶ || st->kind == PO_ST_SPANS) {
Branch: 1 branch
BranchTrueFalseTotal
|| st->kind == PO_ST_SPANS) { 1 31922 31923
729 20034 ▶ if (!po_has_trace_key(q->source, q->stages)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!po_has_trace_key(q->source, q->stages)) { 5 20029 20034
730 5 po_perr(&p, p.lex.cur.off,
731 "this stage needs a trace id on its rows; add | exemplars "
732 "first to get one from the metric", NULL);
733 5 return 0;
734 }
735 }
736  
737 51951 ▶ if (tail) tail->next = st; else q->stages = st;
Branch: 1 branch
BranchTrueFalseTotal
if (tail) tail->next = st; else q->stages = st; 40226 11725 51951
738 51951 tail = st;
739  
740 51951 ▶ if (p.lex.failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (p.lex.failed) { 1 51950 51951
741 1 q->failed = 1; q->err_off = p.lex.err_off;
742 1 memcpy(q->err, p.lex.err, sizeof(q->err) - 1);
743 1 return 0;
744 }
745 }
746  
747 11857 ▶ if (p.lex.cur.kind != PO_T_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
if (p.lex.cur.kind != PO_T_EOF) { 1 11856 11857
748 1 po_perr(&p, p.lex.cur.off, "expected '|' or the end of the query", NULL);
749 1 return 0;
750 }
751 11856 return !q->failed;
752 }
753  
754 #endif /* PO_PARSE_H */
« include/punk_observe/po_live.h ↑ Summary include/punk_observe/po_tattr.h »