xs/query.xs

filter: Statementstmt 97.9% 96 / 98 Branchbran 96.1% 50 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 97.3% 146 / 150 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk_observe/po_bloom.h ↑ Summary include/punk_observe/po_hash.h »
1 MODULE = Punk::Observe PACKAGE = Punk::Observe::Query PREFIX = poq_
2  
3 # Parse a query and return a canonical dump of the AST, so a test reads as a
4 # table rather than as a walk over an opaque structure.
5 # THE LANGUAGE, DESCRIBED BY THE CODE THAT IMPLEMENTS IT.
6 #
7 # The help page is generated from this rather than written out, because a
8 # reference maintained by hand is a reference that goes wrong - and this one
9 # would be wrong about a language whose entire surface is four static tables
10 # a few lines apart. Every list here is read from the table the parser itself
11 # consults, so a column added to PO_COLUMNS appears on the page without
12 # anybody having to remember.
13 void
14 poq_grammar()
15 PPCODE:
16 {
17 25 HV *out = newHV();
18 int i;
19  
20 { /* Sources. The canonical spelling, and the alias that also works
21 * - accepted, and not what the page should teach. */
22 25 AV *src = newAV();
23 static const char *const NAME[] = { "metric", "log", "trace", "spans" };
24 static const char *const FORM[] = { "metric NAME", "log", "trace", "spans" };
25 static const char *const ALIAS[] = { NULL, "logs", "traces", "span" };
26 125 ▶ for (i = 0; i < 4; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 4; i++) { 100 25 125
27 100 HV *h = newHV();
28 100 hv_stores(h, "name", newSVpv(NAME[i], 0));
29 100 hv_stores(h, "form", newSVpv(FORM[i], 0));
30 100 ▶ if (ALIAS[i]) hv_stores(h, "alias", newSVpv(ALIAS[i], 0));
Branch: 1 branch
BranchTrueFalseTotal
if (ALIAS[i]) hv_stores(h, "alias", newSVpv(ALIAS[i], 0)); 75 25 100
31 100 av_push(src, newRV_noinc((SV *)h));
32 }
33 25 hv_stores(out, "sources", newRV_noinc((SV *)src));
34 }
35  
36 { /* Columns per source, straight off the table the parser
37 * validates against. */
38 25 HV *cols = newHV();
39 static const char *const K[] = { "metric", "log", "trace", "spans" };
40 static const int BIT[] = { PO_C_METRIC, PO_C_LOG, PO_C_TRACE, PO_C_SPAN };
41 int sx;
42 125 ▶ for (sx = 0; sx < 4; sx++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sx = 0; sx < 4; sx++) { 100 25 125
43 100 AV *list = newAV();
44 1300 ▶ for (i = 0; PO_COLUMNS[i].name; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; PO_COLUMNS[i].name; i++) 1200 100 1300
45 1200 ▶ if (PO_COLUMNS[i].sources & BIT[sx])
Branch: 1 branch
BranchTrueFalseTotal
if (PO_COLUMNS[i].sources & BIT[sx]) 725 475 1200
46 725 av_push(list, newSVpv(PO_COLUMNS[i].name, 0));
47 100 (void)hv_store(cols, K[sx], (I32)strlen(K[sx]),
48 newRV_noinc((SV *)list), 0);
49 }
50 25 hv_stores(out, "columns", newRV_noinc((SV *)cols));
51 }
52  
53 { /* Aggregates. Every one takes NO argument and reads a column
54 * chosen by the row kind, which is the thing nothing documents. */
55 25 AV *agg = newAV();
56 static const char *const A[] = { "count", "sum", "avg", "min",
57 "max", "p50", "p90", "p95",
58 "p99", "distinct", NULL };
59 275 ▶ for (i = 0; A[i]; i++) av_push(agg, newSVpv(A[i], 0));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; A[i]; i++) av_push(agg, newSVpv(A[i], 0)); 250 25 275
60 25 hv_stores(out, "aggregates", newRV_noinc((SV *)agg));
61 }
62  
63 { /* Severity names, with the numbers they resolve to at parse
64 * time, from po_severity_value itself. */
65 25 AV *sev = newAV();
66 static const char *const N[] = { "trace", "debug", "info", "warn",
67 "warning", "error", "fatal", NULL };
68 200 ▶ for (i = 0; N[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; N[i]; i++) { 175 25 200
69 175 HV *h = newHV();
70 175 hv_stores(h, "name", newSVpv(N[i], 0));
71 { /* It answers through an out-parameter and returns
72 * whether the word was one, which is the shape the
73 * parser needs and not the shape a list does. */
74 175 po_u64 v = 0;
75 175 (void)po_severity_value(N[i], strlen(N[i]), &v);
76 175 hv_stores(h, "value", newSViv((IV)v));
77 }
78 175 av_push(sev, newRV_noinc((SV *)h));
79 }
80 25 hv_stores(out, "severities", newRV_noinc((SV *)sev));
81 }
82  
83 { /* Duration units, from po_dur_unit, so the page cannot offer one
84 * the lexer does not take. */
85 25 AV *un = newAV();
86 static const char *const U[] = { "ns", "us", "ms", "s", "m", "h",
87 "d", "w", "y", NULL };
88 250 ▶ for (i = 0; U[i]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; U[i]; i++) { 225 25 250
89 225 HV *h = newHV();
90 225 hv_stores(h, "name", newSVpv(U[i], 0));
91 225 hv_stores(h, "ns",
92 po_u64_to_sv(po_dur_unit(U[i], strlen(U[i]))));
93 225 av_push(un, newRV_noinc((SV *)h));
94 }
95 25 hv_stores(out, "units", newRV_noinc((SV *)un));
96 }
97  
98 {
99 25 AV *ops = newAV();
100 static const char *const O[] = { "=", "==", "!=", "<", "<=", ">",
101 ">=", "=~", "!~", NULL };
102 250 ▶ for (i = 0; O[i]; i++) av_push(ops, newSVpv(O[i], 0));
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; O[i]; i++) av_push(ops, newSVpv(O[i], 0)); 225 25 250
103 25 hv_stores(out, "operators", newRV_noinc((SV *)ops));
104 }
105  
106 25 ▶ mXPUSHs(newRV_noinc((SV *)out));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)out)); 0 25 25
107 25 XSRETURN(1);
108 }
109  
110 SV *
111 poq_parse(SV *src)
112 CODE:
113 {
114 po_query q;
115 STRLEN len;
116 const char *p;
117 232 HV *res = newHV();
118  
119 232 p = SvPV(src, len); /* own line: sibling-arg order is UB */
120 232 po_parse(&q, p, (size_t)len);
121  
122 232 hv_stores(res, "ok", newSViv(q.failed ? 0 : 1));
123 232 ▶ if (q.failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (q.failed) { 59 173 232
124 59 hv_stores(res, "error", newSVpv(q.err, 0));
125 59 hv_stores(res, "offset", newSVuv((UV)q.err_off));
126 }
127 else {
128 173 AV *stages = newAV();
129 po_stage *s;
130  
131 173 hv_stores(res, "source", newSVpv(po_src_name(q.source), 0));
132 173 ▶ if (q.name)
Branch: 1 branch
BranchTrueFalseTotal
if (q.name) 14 159 173
133 14 hv_stores(res, "name", newSVpvn(q.name, q.name_len));
134 173 ▶ if (q.selector)
Branch: 1 branch
BranchTrueFalseTotal
if (q.selector) 5 168 173
135 5 hv_stores(res, "selector", poq_expr_sv(aTHX_ q.selector));
136  
137 383 ▶ for (s = q.stages; s; s = s->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = q.stages; s; s = s->next) { 210 173 383
138 210 HV *h = newHV();
139 210 const char *k = "?";
140 210 switch (s->kind) {
141 80 case PO_ST_WHERE: k = "where"; break;
142 4 case PO_ST_SEARCH: k = "search"; break;
143 18 case PO_ST_BY: k = "by"; break;
144 42 case PO_ST_AGG: k = "agg"; break;
145 5 case PO_ST_RATE: k = "rate"; break;
146 4 case PO_ST_TOPN: k = "top"; break;
147 /* `bucket` HAD NO CASE, so Query::parse reported its
148 * kind as "?" - the stage parsed and executed
149 * correctly and was the only one the AST could not
150 * name. It is also missing from Query.pm's own list
151 * of kinds, which is where it went unnoticed. */
152 24 case PO_ST_BUCKET: k = "bucket"; break;
153 6 case PO_ST_SLOWEST: k = "slowest"; break;
154 6 case PO_ST_LIMIT: k = "limit"; break;
155 4 case PO_ST_SORT: k = "sort"; break;
156 6 case PO_ST_EXEMPLARS: k = "exemplars"; break;
157 7 case PO_ST_TRACES: k = "traces"; break;
158 4 case PO_ST_LOGS: k = "logs"; break;
159 0 case PO_ST_SPANS: k = "spans"; break;
160 0 default: break;
161 }
162 210 hv_stores(h, "kind", newSVpv(k, 0));
163 210 ▶ if (s->expr) hv_stores(h, "expr", poq_expr_sv(aTHX_ s->expr));
Branch: 1 branch
BranchTrueFalseTotal
if (s->expr) hv_stores(h, "expr", poq_expr_sv(aTHX_ s->expr)); 80 130 210
164 210 ▶ if (s->str) hv_stores(h, "text", newSVpvn(s->str, s->str_len));
Branch: 1 branch
BranchTrueFalseTotal
if (s->str) hv_stores(h, "text", newSVpvn(s->str, s->str_len)); 4 206 210
165 210 ▶ if (s->agg) hv_stores(h, "agg", newSVpv(poq_agg_name(s->agg), 0));
Branch: 1 branch
BranchTrueFalseTotal
if (s->agg) hv_stores(h, "agg", newSVpv(poq_agg_name(s->agg), 0)); 68 142 210
166 210 ▶ if (s->dur) hv_stores(h, "window", po_u64_to_sv(s->dur));
Branch: 1 branch
BranchTrueFalseTotal
if (s->dur) hv_stores(h, "window", po_u64_to_sv(s->dur)); 29 181 210
167 210 ▶ if (s->num) hv_stores(h, "n", po_u64_to_sv(s->num));
Branch: 1 branch
BranchTrueFalseTotal
if (s->num) hv_stores(h, "n", po_u64_to_sv(s->num)); 16 194 210
168 210 ▶ if (s->desc) hv_stores(h, "desc", newSViv(1));
Branch: 1 branch
BranchTrueFalseTotal
if (s->desc) hv_stores(h, "desc", newSViv(1)); 4 206 210
169 210 ▶ if (s->nfields) {
Branch: 1 branch
BranchTrueFalseTotal
if (s->nfields) { 36 174 210
170 36 AV *f = newAV();
171 int i;
172 73 ▶ for (i = 0; i < s->nfields; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < s->nfields; i++) 37 36 73
173 37 av_push(f, newSVpvn(s->fields[i], s->field_lens[i]));
174 36 hv_stores(h, "fields", newRV_noinc((SV *)f));
175 }
176 210 av_push(stages, newRV_noinc((SV *)h));
177 }
178 173 hv_stores(res, "stages", newRV_noinc((SV *)stages));
179 173 ▶ if (q.viz)
Branch: 1 branch
BranchTrueFalseTotal
if (q.viz) 11 162 173
180 11 hv_stores(res, "viz", newSVpvn(q.viz, q.viz_len));
181 }
182 232 po_query_free(&q);
183 232 RETVAL = newRV_noinc((SV *)res);
184 }
185 OUTPUT:
186 RETVAL
187  
188 # Parse and immediately free, so a leak check has something to run many times.
189 UV
190 poq_parse_free_cycles(SV *src, SV *n)
191 CODE:
192 {
193 STRLEN len;
194 const char *p;
195 2 IV i, cycles = SvIV(n);
196 2 UV ok = 0;
197 2 p = SvPV(src, len);
198 15002 ▶ for (i = 0; i < cycles; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < cycles; i++) { 15000 2 15002
199 po_query q;
200 15000 ▶ if (po_parse(&q, p, (size_t)len)) ok++;
Branch: 1 branch
BranchTrueFalseTotal
if (po_parse(&q, p, (size_t)len)) ok++; 10000 5000 15000
201 15000 po_query_free(&q);
202 }
203 2 ▶ RETVAL = ok;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ok; 0 2 2
204 }
205 OUTPUT:
206 RETVAL
« include/punk_observe/po_bloom.h ↑ Summary include/punk_observe/po_hash.h »