include/punk_observe/po_expr.h

filter: Statementstmt 89.9% 89 / 99 Branchbran 75.8% 129 / 170 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.0% 218 / 269 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/segment.xs ↑ Summary include/punk_observe/po_pb_read.h »
1 /* po_expr.h - evaluating a compiled predicate against a row.
2 *
3 * The AST from phase 8 is walked once here per row. That is deliberate: the
4 * tree is tiny (a handful of nodes), it is already in one bump chunk, and a
5 * bytecode pass over it would be a second representation to keep correct for
6 * no measurable gain at this size. What matters for speed is what the
7 * comparisons DO, and they compare resolved values rather than parsing
8 * anything per row.
9 *
10 * THREE THINGS THAT LOOK LIKE DETAILS AND ARE NOT.
11 *
12 * 1. A comparison against a MISSING field is FALSE, not zero. `duration > 0`
13 * must not match a log line that has no duration. Treating absent as zero
14 * silently widens every numeric filter.
15 * 2. `!=` against a missing field is also FALSE, not true. Both directions of
16 * a comparison on something that is not there are false; anything else
17 * makes `not (a = 1)` and `a != 1` differ on rows lacking `a`.
18 * 3. Regex is NOT a regex engine here. `=~` supports an anchored prefix, an
19 * anchored suffix and a bare substring, which is what real usage is; a
20 * pattern needing more is refused at plan time rather than silently
21 * treated as a literal.
22 */
23 #ifndef PO_EXPR_H
24 #define PO_EXPR_H
25  
26 #include "punk_observe/po_compat.h"
27 #include "punk_observe/po_query.h"
28 #include "punk_observe/po_row.h"
29  
30 /* Is this a pattern the matcher supports? Returns 0 if not, so the planner
31 * can refuse it with a message rather than the executor quietly matching
32 * something else. */
33 19 static int po_pattern_ok(const char *p, size_t n) {
34 size_t i;
35 70 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 62 8 70
36 62 char c = p[i];
37 62 ▶ if (c == '^' && i == 0) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c == '^' && i == 0) continue; 4 58 62
if (c == '^' && i == 0) continue; 4 0 4
38 58 ▶ if (c == '$' && i == n - 1) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c == '$' && i == n - 1) continue; 3 55 58
if (c == '$' && i == n - 1) continue; 3 0 3
39 55 ▶ if (c == '.' || c == '*' || c == '+' || c == '?' || c == '[' ||
Branch: 6 branches
BranchTrueFalseTotal
if (c == '.' || c == '*' || c == '+' || c == '?' || c == '[' || 53 2 55
if (c == '.' || c == '*' || c == '+' || c == '?' || c == '[' || 53 0 53
if (c == '.' || c == '*' || c == '+' || c == '?' || c == '[' || 52 1 53
if (c == '.' || c == '*' || c == '+' || c == '?' || c == '[' || 52 0 52
if (c == '.' || c == '*' || c == '+' || c == '?' || c == '[' || 51 1 52
if (c == '.' || c == '*' || c == '+' || c == '?' || c == '[' || 51 0 51
40 51 ▶ c == ']' || c == '(' || c == ')' || c == '|' || c == '\\' ||
Branch: 5 branches
BranchTrueFalseTotal
c == ']' || c == '(' || c == ')' || c == '|' || c == '\\' || 48 3 51
c == ']' || c == '(' || c == ')' || c == '|' || c == '\\' || 48 0 48
c == ']' || c == '(' || c == ')' || c == '|' || c == '\\' || 47 1 48
c == ']' || c == '(' || c == ')' || c == '|' || c == '\\' || 45 2 47
c == ']' || c == '(' || c == ')' || c == '|' || c == '\\' || 44 1 45
41 55 ▶ c == '{' || c == '}') return 0;
Branch: 1 branch
BranchTrueFalseTotal
c == '{' || c == '}') return 0; 0 44 44
42 }
43 8 return 1;
44 }
45  
46 16 static int po_match(const char *hay, size_t hn, const char *pat, size_t pn) {
47 16 int anchor_start = 0, anchor_end = 0;
48 16 ▶ if (pn && pat[0] == '^') { anchor_start = 1; pat++; pn--; }
Branch: 2 branches
BranchTrueFalseTotal
if (pn && pat[0] == '^') { anchor_start = 1; pat++; pn--; } 16 0 16
if (pn && pat[0] == '^') { anchor_start = 1; pat++; pn--; } 8 8 16
49 16 ▶ if (pn && pat[pn - 1] == '$') { anchor_end = 1; pn--; }
Branch: 2 branches
BranchTrueFalseTotal
if (pn && pat[pn - 1] == '$') { anchor_end = 1; pn--; } 16 0 16
if (pn && pat[pn - 1] == '$') { anchor_end = 1; pn--; } 5 11 16
50 16 ▶ if (pn > hn) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pn > hn) return 0; 6 10 16
51 10 ▶ if (anchor_start && anchor_end) return pn == hn && memcmp(hay, pat, pn) == 0;
Branch: 4 branches
BranchTrueFalseTotal
if (anchor_start && anchor_end) return pn == hn && memcmp(hay, pat, pn) == 0; 4 6 10
if (anchor_start && anchor_end) return pn == hn && memcmp(hay, pat, pn) == 0; 0 4 4
if (anchor_start && anchor_end) return pn == hn && memcmp(hay, pat, pn) == 0; 0 0 0
if (anchor_start && anchor_end) return pn == hn && memcmp(hay, pat, pn) == 0; 0 0 0
52 10 ▶ if (anchor_start) return memcmp(hay, pat, pn) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if (anchor_start) return memcmp(hay, pat, pn) == 0; 4 6 10
53 6 ▶ if (anchor_end) return memcmp(hay + hn - pn, pat, pn) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if (anchor_end) return memcmp(hay + hn - pn, pat, pn) == 0; 4 2 6
54 {
55 size_t i;
56 2 ▶ if (pn == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pn == 0) return 1; 0 2 2
57 10 ▶ for (i = 0; i + pn <= hn; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + pn <= hn; i++) 9 1 10
58 9 ▶ if (memcmp(hay + i, pat, pn) == 0) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(hay + i, pat, pn) == 0) return 1; 1 8 9
59 1 return 0;
60 }
61 }
62  
63 4104 static int po_cmp_num(double a, int op, double b) {
64 4104 switch (op) {
65 68 case PO_OP_EQ: return a == b;
66 0 case PO_OP_NE: return a != b;
67 0 case PO_OP_LT: return a < b;
68 3 case PO_OP_LE: return a <= b;
69 659 case PO_OP_GT: return a > b;
70 3374 case PO_OP_GE: return a >= b;
71 0 default: return 0;
72 }
73 }
74  
75 3420 static int po_cmp_str(const char *a, size_t an, int op,
76 const char *b, size_t bn) {
77 int c;
78 3420 size_t m = an < bn ? an : bn;
79 3420 switch (op) {
80 13 case PO_OP_MATCH: return po_match(a, an, b, bn);
81 3 case PO_OP_NMATCH: return !po_match(a, an, b, bn);
82 3404 default: break;
83 }
84 3404 ▶ c = m ? memcmp(a, b, m) : 0;
Branch: 1 branch
BranchTrueFalseTotal
c = m ? memcmp(a, b, m) : 0; 3363 41 3404
85 3404 ▶ if (c == 0) c = an == bn ? 0 : (an < bn ? -1 : 1);
Branch: 3 branches
BranchTrueFalseTotal
if (c == 0) c = an == bn ? 0 : (an < bn ? -1 : 1); 1359 2045 3404
if (c == 0) c = an == bn ? 0 : (an < bn ? -1 : 1); 25 1334 1359
if (c == 0) c = an == bn ? 0 : (an < bn ? -1 : 1); 0 25 25
86 3404 switch (op) {
87 3364 case PO_OP_EQ: return c == 0;
88 40 case PO_OP_NE: return c != 0;
89 0 case PO_OP_LT: return c < 0;
90 0 case PO_OP_LE: return c <= 0;
91 0 case PO_OP_GT: return c > 0;
92 0 case PO_OP_GE: return c >= 0;
93 0 default: return 0;
94 }
95 }
96  
97 9263 static int po_eval(const po_expr *e, const po_row *r) {
98 9263 ▶ if (!e) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return 1; 0 9263 9263
99 9263 switch (e->kind) {
100 1403 ▶ case PO_E_AND: return po_eval(e->a, r) && po_eval(e->b, r);
Branch: 2 branches
BranchTrueFalseTotal
case PO_E_AND: return po_eval(e->a, r) && po_eval(e->b, r); 294 1109 1403
case PO_E_AND: return po_eval(e->a, r) && po_eval(e->b, r); 109 185 294
101 3 ▶ case PO_E_OR: return po_eval(e->a, r) || po_eval(e->b, r);
Branch: 2 branches
BranchTrueFalseTotal
case PO_E_OR: return po_eval(e->a, r) || po_eval(e->b, r); 2 1 3
case PO_E_OR: return po_eval(e->a, r) || po_eval(e->b, r); 1 1 2
102 0 case PO_E_NOT: return !po_eval(e->a, r);
103 7857 case PO_E_CMP: break;
104 0 default: return 0;
105 }
106  
107 /* THE ABSENCE TEST. Presence means the row can answer the field at all
108 * - as bytes or as a number - so `risk.outcome != null` keeps the rows
109 * that carry the attribute whatever its value, including empty, which
110 * `!= ""` cannot say. This is the one comparison where absent gets to be
111 * an answer instead of a silent false. */
112 7857 ▶ if (e->vkind == PO_V_NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->vkind == PO_V_NULL) { 198 7659 7857
113 size_t sl; double nv;
114 198 int present = po_row_str(r, e->field, e->field_len, &sl) != NULL
115 198 ▶ || po_row_num(r, e->field, e->field_len, &nv);
Branch: 2 branches
BranchTrueFalseTotal
|| po_row_num(r, e->field, e->field_len, &nv); 129 69 198
|| po_row_num(r, e->field, e->field_len, &nv); 3 126 129
116 198 ▶ return e->op == PO_OP_NE ? present : !present;
Branch: 1 branch
BranchTrueFalseTotal
return e->op == PO_OP_NE ? present : !present; 99 99 198
117 }
118  
119 /* A duration or severity literal is numeric by construction. */
120 7659 ▶ if (e->vkind == PO_V_DURATION || e->vkind == PO_V_SEVERITY
Branch: 2 branches
BranchTrueFalseTotal
if (e->vkind == PO_V_DURATION || e->vkind == PO_V_SEVERITY 7044 615 7659
if (e->vkind == PO_V_DURATION || e->vkind == PO_V_SEVERITY 3657 3387 7044
121 3657 ▶ || e->vkind == PO_V_NUMBER) {
Branch: 1 branch
BranchTrueFalseTotal
|| e->vkind == PO_V_NUMBER) { 104 3553 3657
122 double lhs;
123 4106 ▶ double rhs = (e->vkind == PO_V_NUMBER) ? e->nval : (double)e->uval;
Branch: 1 branch
BranchTrueFalseTotal
double rhs = (e->vkind == PO_V_NUMBER) ? e->nval : (double)e->uval; 104 4002 4106
124 4106 ▶ if (!po_row_num(r, e->field, e->field_len, &lhs)) return 0; /* absent */
Branch: 1 branch
BranchTrueFalseTotal
if (!po_row_num(r, e->field, e->field_len, &lhs)) return 0; /* absent */ 2 4104 4106
125 4104 return po_cmp_num(lhs, e->op, rhs);
126 }
127  
128 /* THE IDENTIFIER COLUMNS, which the row holds as integers.
129 *
130 * `trace_id` and `span_id` are declared in PO_COLUMNS for the log, trace
131 * and span sources, so the parser accepts them and the planner builds a
132 * predicate - and neither one was resolvable by po_row_str or po_row_num.
133 * A comparison against a field the row cannot answer is false in both
134 * directions, so `log | where trace_id = "4bf8..."` matched nothing on a
135 * trace that had logs, and the page said "no log lines matched": the same
136 * words it uses when the trace genuinely had none.
137 *
138 * This is the identical defect po_row.h documents for `status` and
139 * `kind`. Those two were fixed; these two were missed in the same sweep
140 * and shipped, because every test on them was a parse test.
141 *
142 * Compared as INTEGERS against the parsed literal, not as text: the row
143 * has no string form of either without formatting one per row, and
144 * equality on a trace id has to hold across the two spellings a person
145 * might paste. Only = and != mean anything on an identifier; an ordering
146 * comparison is refused by matching nothing rather than inventing one. */
147 3553 ▶ if (PO_IS_TRACE_ID(e->field, e->field_len)
Branch: 2 branches
BranchTrueFalseTotal
if (PO_IS_TRACE_ID(e->field, e->field_len) 30 3523 3553
if (PO_IS_TRACE_ID(e->field, e->field_len) 10 20 30
148 3533 ▶ || PO_IS_SPAN_ID(e->field, e->field_len)) {
Branch: 2 branches
BranchTrueFalseTotal
|| PO_IS_SPAN_ID(e->field, e->field_len)) { 2759 774 3533
|| PO_IS_SPAN_ID(e->field, e->field_len)) { 15 2744 2759
149 35 po_u64 qhi = 0, qlo = 0;
150 35 ▶ int is_trace = PO_IS_TRACE_ID(e->field, e->field_len);
Branch: 2 branches
BranchTrueFalseTotal
int is_trace = PO_IS_TRACE_ID(e->field, e->field_len); 20 15 35
int is_trace = PO_IS_TRACE_ID(e->field, e->field_len); 20 0 20
151 int same;
152  
153 35 ▶ if (e->op != PO_OP_EQ && e->op != PO_OP_NE) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (e->op != PO_OP_EQ && e->op != PO_OP_NE) return 0; 0 35 35
if (e->op != PO_OP_EQ && e->op != PO_OP_NE) return 0; 0 0 0
154 35 ▶ if (e->vkind == PO_V_NUMBER) { qhi = 0; qlo = (po_u64)e->nval; }
Branch: 1 branch
BranchTrueFalseTotal
if (e->vkind == PO_V_NUMBER) { qhi = 0; qlo = (po_u64)e->nval; } 0 35 35
155 35 ▶ else if (!po_parse_id(e->sval, e->sval_len, &qhi, &qlo)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
else if (!po_parse_id(e->sval, e->sval_len, &qhi, &qlo)) return 0; 0 35 35
156  
157 20 ▶ same = is_trace ? (r->trace_hi == qhi && r->trace_lo == qlo)
Branch: 2 branches
BranchTrueFalseTotal
same = is_trace ? (r->trace_hi == qhi && r->trace_lo == qlo) 15 5 20
same = is_trace ? (r->trace_hi == qhi && r->trace_lo == qlo) 15 0 15
158 55 ▶ : (r->span_id == qlo);
Branch: 1 branch
BranchTrueFalseTotal
: (r->span_id == qlo); 20 15 35
159 35 ▶ return (e->op == PO_OP_EQ) ? same : !same;
Branch: 1 branch
BranchTrueFalseTotal
return (e->op == PO_OP_EQ) ? same : !same; 0 35 35
160 }
161  
162 {
163 size_t hn;
164 3518 const char *hay = po_row_str(r, e->field, e->field_len, &hn);
165 3518 ▶ if (!hay) return 0; /* absent */
Branch: 1 branch
BranchTrueFalseTotal
if (!hay) return 0; /* absent */ 98 3420 3518
166 3420 return po_cmp_str(hay, hn, e->op, e->sval, e->sval_len);
167 }
168 }
169  
170 /* Walk an expression looking for a pattern the matcher cannot honour, so the
171 * planner refuses it rather than the executor guessing. */
172 286 static const po_expr *po_expr_bad_pattern(const po_expr *e) {
173 const po_expr *r;
174 286 ▶ if (!e) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return NULL; 0 286 286
175 286 ▶ if (e->kind == PO_E_CMP) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->kind == PO_E_CMP) { 264 22 286
176 264 ▶ if ((e->op == PO_OP_MATCH || e->op == PO_OP_NMATCH)
Branch: 2 branches
BranchTrueFalseTotal
if ((e->op == PO_OP_MATCH || e->op == PO_OP_NMATCH) 246 18 264
if ((e->op == PO_OP_MATCH || e->op == PO_OP_NMATCH) 1 245 246
177 19 ▶ && e->vkind == PO_V_STRING
Branch: 1 branch
BranchTrueFalseTotal
&& e->vkind == PO_V_STRING 19 0 19
178 19 ▶ && !po_pattern_ok(e->sval, e->sval_len)) return e;
Branch: 1 branch
BranchTrueFalseTotal
&& !po_pattern_ok(e->sval, e->sval_len)) return e; 11 8 19
179 253 return NULL;
180 }
181 22 ▶ if ((r = po_expr_bad_pattern(e->a))) return r;
Branch: 1 branch
BranchTrueFalseTotal
if ((r = po_expr_bad_pattern(e->a))) return r; 0 22 22
182 22 return po_expr_bad_pattern(e->b);
183 }
184  
185 /* Does this expression constrain the time column? The planner uses it to
186 * decide which segments to open, and a query with no time bound at all is the
187 * one most likely to be refused. */
188 4 static int po_expr_time_bounded(const po_expr *e) {
189 4 ▶ if (!e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return 0; 0 4 4
190 4 ▶ if (e->kind == PO_E_CMP)
Branch: 1 branch
BranchTrueFalseTotal
if (e->kind == PO_E_CMP) 2 2 4
191 2 ▶ return (e->field_len == 1 && e->field[0] == 't')
Branch: 1 branch
BranchTrueFalseTotal
return (e->field_len == 1 && e->field[0] == 't') 0 2 2
192 4 ▶ || (e->field_len == 4 && memcmp(e->field, "time", 4) == 0);
Branch: 3 branches
BranchTrueFalseTotal
|| (e->field_len == 4 && memcmp(e->field, "time", 4) == 0); 2 0 2
|| (e->field_len == 4 && memcmp(e->field, "time", 4) == 0); 0 0 0
|| (e->field_len == 4 && memcmp(e->field, "time", 4) == 0); 0 0 0
193 2 ▶ if (e->kind == PO_E_AND)
Branch: 1 branch
BranchTrueFalseTotal
if (e->kind == PO_E_AND) 1 1 2
194 1 ▶ return po_expr_time_bounded(e->a) || po_expr_time_bounded(e->b);
Branch: 2 branches
BranchTrueFalseTotal
return po_expr_time_bounded(e->a) || po_expr_time_bounded(e->b); 0 1 1
return po_expr_time_bounded(e->a) || po_expr_time_bounded(e->b); 0 0 0
195 /* An OR does not BOUND anything: `t > x or service = "y"` still admits
196 * every row outside the range. Only a conjunction narrows. */
197 1 return 0;
198 }
199  
200 /* Does it constrain anything selective at all? */
201 5 static int po_expr_selective(const po_expr *e) {
202 5 ▶ if (!e) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!e) return 0; 0 5 5
203 5 ▶ if (e->kind == PO_E_CMP) return e->op == PO_OP_EQ;
Branch: 1 branch
BranchTrueFalseTotal
if (e->kind == PO_E_CMP) return e->op == PO_OP_EQ; 3 2 5
204 2 ▶ if (e->kind == PO_E_AND)
Branch: 1 branch
BranchTrueFalseTotal
if (e->kind == PO_E_AND) 1 1 2
205 1 ▶ return po_expr_selective(e->a) || po_expr_selective(e->b);
Branch: 2 branches
BranchTrueFalseTotal
return po_expr_selective(e->a) || po_expr_selective(e->b); 1 0 1
return po_expr_selective(e->a) || po_expr_selective(e->b); 1 0 1
206 1 return 0;
207 }
208  
209 #endif /* PO_EXPR_H */
« xs/segment.xs ↑ Summary include/punk_observe/po_pb_read.h »