2f79fc394c79dcf2f2fb65087942935db5efd59e
1 /*
2 * SysDB - src/parser/analyzer.c
3 * Copyright (C) 2014-2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
28 #include "sysdb.h"
30 #include "parser/ast.h"
31 #include "parser/parser.h"
32 #include "utils/error.h"
33 #include "utils/strbuf.h"
35 #include <assert.h>
36 #include <stdarg.h>
38 #define VALID_OBJ_TYPE(t) ((SDB_HOST <= (t)) && ((t) <= SDB_METRIC))
40 #define FILTER_CONTEXT -1
41 #define UNSPEC_CONTEXT -2
43 static int
44 analyze_node(int context, sdb_ast_node_t *node, sdb_strbuf_t *errbuf);
46 /*
47 * error reporting
48 */
50 static void
51 op_error(sdb_strbuf_t *errbuf, sdb_ast_op_t *op, const char *reason)
52 {
53 sdb_strbuf_sprintf(errbuf, "Invalid operation %s %s %s (%s)",
54 SDB_TYPE_TO_STRING(op->left->data_type),
55 SDB_AST_OP_TO_STRING(op->kind),
56 SDB_TYPE_TO_STRING(op->right->data_type),
57 reason);
58 } /* op_error */
60 static void
61 __attribute__((format(printf, 3, 4)))
62 iter_error(sdb_strbuf_t *errbuf, sdb_ast_iter_t *iter, const char *reason, ...)
63 {
64 char r[1024];
65 va_list ap;
67 va_start(ap, reason);
68 vsnprintf(r, sizeof(r), reason, ap);
69 va_end(ap);
71 assert((iter->expr->type == SDB_AST_TYPE_OPERATOR)
72 && (! SDB_AST_OP(iter->expr)->left));
73 sdb_strbuf_sprintf(errbuf, "Invalid iterator %s %s %s %s (%s)",
74 SDB_AST_OP_TO_STRING(iter->kind),
75 SDB_TYPE_TO_STRING(iter->iter->data_type),
76 SDB_AST_OP_TO_STRING(SDB_AST_OP(iter->expr)->kind),
77 SDB_TYPE_TO_STRING(SDB_AST_OP(iter->expr)->right->data_type),
78 r);
79 } /* iter_error */
81 /*
82 * expression nodes
83 */
85 static int
86 analyze_logical(int context, sdb_ast_op_t *op, sdb_strbuf_t *errbuf)
87 {
88 switch (op->kind) {
89 case SDB_AST_OR:
90 case SDB_AST_AND:
91 if (! SDB_AST_IS_LOGICAL(op->left)) {
92 sdb_strbuf_sprintf(errbuf, "Invalid left operand (%s) "
93 "in %s expression", SDB_AST_TYPE_TO_STRING(op->left),
94 SDB_AST_OP_TO_STRING(op->kind));
95 return -1;
96 }
97 if (analyze_node(context, op->left, errbuf))
98 return -1;
99 /* fallthrough */
100 case SDB_AST_NOT:
101 if (! SDB_AST_IS_LOGICAL(op->right)) {
102 sdb_strbuf_sprintf(errbuf, "Invalid right operand (%s) "
103 "in %s expression", SDB_AST_TYPE_TO_STRING(op->right),
104 SDB_AST_OP_TO_STRING(op->kind));
105 return -1;
106 }
107 if (analyze_node(context, op->right, errbuf))
108 return -1;
109 break;
111 case SDB_AST_LT:
112 case SDB_AST_LE:
113 case SDB_AST_EQ:
114 case SDB_AST_NE:
115 case SDB_AST_GE:
116 case SDB_AST_GT:
117 {
118 if (analyze_node(context, op->left, errbuf))
119 return -1;
120 if (analyze_node(context, op->right, errbuf))
121 return -1;
123 if ((op->left->data_type > 0) && (op->right->data_type > 0)) {
124 if (op->left->data_type == op->right->data_type)
125 return 0;
126 op_error(errbuf, op, "type mismatch");
127 return -1;
128 }
129 if ((op->left->data_type > 0) && (op->left->data_type & SDB_TYPE_ARRAY)) {
130 op_error(errbuf, op, "array not allowed");
131 return -1;
132 }
133 if ((op->right->data_type > 0) && (op->right->data_type & SDB_TYPE_ARRAY)) {
134 op_error(errbuf, op, "array not allowed");
135 return -1;
136 }
137 break;
138 }
140 case SDB_AST_REGEX:
141 case SDB_AST_NREGEX:
142 if (analyze_node(context, op->left, errbuf))
143 return -1;
144 if (analyze_node(context, op->right, errbuf))
145 return -1;
147 /* all types are supported for the left operand
148 * TODO: introduce a cast operator if it's not a string */
149 if ((op->right->data_type > 0)
150 && (op->right->data_type != SDB_TYPE_REGEX)
151 && (op->right->data_type != SDB_TYPE_STRING)) {
152 op_error(errbuf, op, "invalid regex");
153 return -1;
154 }
155 break;
157 case SDB_AST_ISNULL:
158 if (analyze_node(context, op->right, errbuf))
159 return -1;
160 break;
162 case SDB_AST_IN:
163 if (analyze_node(context, op->left, errbuf))
164 return -1;
165 if (analyze_node(context, op->right, errbuf))
166 return -1;
168 if ((op->right->data_type > 0) && (! (op->right->data_type & SDB_TYPE_ARRAY))) {
169 op_error(errbuf, op, "array expected");
170 return -1;
171 }
172 /* the left operand may be a scalar or an array but the element
173 * type has to match */
174 if ((op->left->data_type > 0) && (op->right->data_type > 0)
175 && ((op->left->data_type & 0xff) != (op->right->data_type & 0xff))) {
176 op_error(errbuf, op, "type mismatch");
177 return -1;
178 }
179 break;
181 default:
182 sdb_strbuf_sprintf(errbuf, "Unknown operand type %d", op->kind);
183 return -1;
184 }
185 return 0;
186 } /* analyze_logical */
188 static int
189 analyze_arith(int context, sdb_ast_op_t *op, sdb_strbuf_t *errbuf)
190 {
191 if (analyze_node(context, op->left, errbuf))
192 return -1;
193 if (analyze_node(context, op->right, errbuf))
194 return -1;
195 SDB_AST_NODE(op)->data_type = sdb_data_expr_type(SDB_AST_OP_TO_DATA_OP(op->kind),
196 op->left->data_type, op->right->data_type);
198 if ((op->left->data_type > 0) && (op->right->data_type > 0)
199 && (SDB_AST_NODE(op)->data_type <= 0)) {
200 op_error(errbuf, op, "type mismatch");
201 return -1;
202 }
204 /* TODO: replace constant arithmetic operations with a constant value */
205 return 0;
206 } /* analyze_arith */
208 static int
209 analyze_iter(int context, sdb_ast_iter_t *iter, sdb_strbuf_t *errbuf)
210 {
211 sdb_ast_const_t c = SDB_AST_CONST_INIT;
212 int iter_context = context;
213 int status;
215 if (iter->iter->type == SDB_AST_TYPE_TYPED)
216 iter_context = SDB_AST_TYPED(iter->iter)->type;
218 if (analyze_node(context, iter->iter, errbuf))
219 return -1;
220 if (iter->iter->data_type > 0)
221 c.value.type = iter->iter->data_type & 0xff;
222 else
223 c.value.type = -1;
225 /* TODO: support other setups as well */
226 assert((iter->expr->type == SDB_AST_TYPE_OPERATOR)
227 && (! SDB_AST_OP(iter->expr)->left));
228 SDB_AST_OP(iter->expr)->left = SDB_AST_NODE(&c);
229 status = analyze_node(iter_context, iter->expr, errbuf);
230 SDB_AST_OP(iter->expr)->left = NULL;
231 if (status)
232 return -1;
234 if (iter->iter->type == SDB_AST_TYPE_TYPED) {
235 int iter_type = SDB_AST_TYPED(iter->iter)->type;
237 if (iter_type == SDB_ATTRIBUTE) {
238 /* attributes are always iterable */
239 }
240 else if ((context != SDB_HOST) && (context != SDB_SERVICE)
241 && (context != SDB_METRIC) && (context != UNSPEC_CONTEXT)) {
242 iter_error(errbuf, iter, "%s not iterable in %s context",
243 SDB_STORE_TYPE_TO_NAME(iter_type),
244 SDB_STORE_TYPE_TO_NAME(context));
245 return -1;
246 }
248 if ((context == iter_type)
249 || ((iter_type != SDB_SERVICE)
250 && (iter_type != SDB_METRIC)
251 && (iter_type != SDB_ATTRIBUTE))
252 || ((context == SDB_SERVICE)
253 && (iter_type == SDB_METRIC))
254 || ((context == SDB_METRIC)
255 && (iter_type == SDB_SERVICE))) {
256 iter_error(errbuf, iter, "%s not iterable in %s context",
257 SDB_STORE_TYPE_TO_NAME(iter_type),
258 SDB_STORE_TYPE_TO_NAME(context));
259 return -1;
260 }
261 }
262 else if (iter->iter->type == SDB_AST_TYPE_VALUE) {
263 int iter_type = SDB_AST_VALUE(iter->iter)->type;
265 if (iter_type == SDB_FIELD_BACKEND) {
266 /* backends are always iterable */
267 }
268 else if ((context != SDB_HOST) && (context != SDB_SERVICE)
269 && (context != SDB_METRIC) && (context != SDB_ATTRIBUTE)
270 && (context != UNSPEC_CONTEXT)) {
271 iter_error(errbuf, iter, "%s not iterable in %s context",
272 (iter_type == SDB_ATTRIBUTE)
273 ? "attribute"
274 : SDB_FIELD_TO_NAME(iter_type),
275 SDB_STORE_TYPE_TO_NAME(context));
276 return -1;
277 }
278 }
279 else if (iter->iter->type == SDB_AST_TYPE_CONST) {
280 if (! (SDB_AST_CONST(iter->iter)->value.type & SDB_TYPE_ARRAY)) {
281 iter_error(errbuf, iter, "%s not iterable",
282 SDB_TYPE_TO_STRING(SDB_AST_CONST(iter->iter)->value.type));
283 return -1;
284 }
285 }
286 return 0;
287 } /* analyze_iter */
289 static int
290 analyze_const(int __attribute__((unused)) context, sdb_ast_const_t *c,
291 sdb_strbuf_t __attribute__((unused)) *errbuf)
292 {
293 SDB_AST_NODE(c)->data_type = c->value.type;
294 return 0;
295 } /* analyze_const */
297 static int
298 analyze_value(int __attribute__((unused)) context, sdb_ast_value_t *v,
299 sdb_strbuf_t __attribute__((unused)) *errbuf)
300 {
301 if (v->type != SDB_ATTRIBUTE)
302 SDB_AST_NODE(v)->data_type = SDB_FIELD_TYPE(v->type);
303 return 0;
304 } /* analyze_value */
306 static int
307 analyze_typed(int __attribute__((unused)) context, sdb_ast_typed_t *t,
308 sdb_strbuf_t *errbuf)
309 {
310 if (analyze_node(t->type, t->expr, errbuf))
311 return -1;
312 SDB_AST_NODE(t)->data_type = t->expr->data_type;
313 return 0;
314 } /* analyze_typed */
316 static int
317 analyze_node(int context, sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
318 {
319 if (! node) {
320 sdb_strbuf_sprintf(errbuf, "Empty AST node");
321 return -1;
322 }
324 /* unknown by default */
325 node->data_type = -1;
327 if ((node->type == SDB_AST_TYPE_OPERATOR)
328 && (SDB_AST_IS_LOGICAL(node)))
329 return analyze_logical(context, SDB_AST_OP(node), errbuf);
330 else if ((node->type == SDB_AST_TYPE_OPERATOR)
331 && (SDB_AST_IS_ARITHMETIC(node)))
332 return analyze_arith(context, SDB_AST_OP(node), errbuf);
333 else if (node->type == SDB_AST_TYPE_ITERATOR)
334 return analyze_iter(context, SDB_AST_ITER(node), errbuf);
335 else if (node->type == SDB_AST_TYPE_CONST)
336 return analyze_const(context, SDB_AST_CONST(node), errbuf);
337 else if (node->type == SDB_AST_TYPE_VALUE)
338 return analyze_value(context, SDB_AST_VALUE(node), errbuf);
339 else if (node->type == SDB_AST_TYPE_TYPED)
340 return analyze_typed(context, SDB_AST_TYPED(node), errbuf);
342 sdb_strbuf_sprintf(errbuf, "Invalid expression node "
343 "of type %#x", node->type);
344 return -1;
345 } /* analyze_node */
347 /*
348 * top level / command nodes
349 */
351 static int
352 analyze_fetch(sdb_ast_fetch_t *fetch, sdb_strbuf_t *errbuf)
353 {
354 if (! VALID_OBJ_TYPE(fetch->obj_type)) {
355 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
356 "in FETCH command", fetch->obj_type);
357 return -1;
358 }
359 if (! fetch->name) {
360 sdb_strbuf_sprintf(errbuf, "Missing object name in "
361 "FETCH %s command", SDB_STORE_TYPE_TO_NAME(fetch->obj_type));
362 return -1;
363 }
365 if ((fetch->obj_type == SDB_HOST) && fetch->hostname) {
366 sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
367 "in FETCH HOST command", fetch->hostname);
368 return -1;
369 }
370 else if ((fetch->obj_type != SDB_HOST) && (! fetch->hostname)) {
371 sdb_strbuf_sprintf(errbuf, "Missing parent hostname for '%s' "
372 "in FETCH %s command", fetch->name,
373 SDB_STORE_TYPE_TO_NAME(fetch->obj_type));
374 return -1;
375 }
377 if (fetch->filter)
378 return analyze_node(FILTER_CONTEXT, fetch->filter, errbuf);
379 return 0;
380 } /* analyze_fetch */
382 static int
383 analyze_list(sdb_ast_list_t *list, sdb_strbuf_t *errbuf)
384 {
385 if (! VALID_OBJ_TYPE(list->obj_type)) {
386 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
387 "in LIST command", list->obj_type);
388 return -1;
389 }
390 if (list->filter)
391 return analyze_node(FILTER_CONTEXT, list->filter, errbuf);
392 return 0;
393 } /* analyze_list */
395 static int
396 analyze_lookup(sdb_ast_lookup_t *lookup, sdb_strbuf_t *errbuf)
397 {
398 if (! VALID_OBJ_TYPE(lookup->obj_type)) {
399 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
400 "in LOOKUP command", lookup->obj_type);
401 return -1;
402 }
403 if (lookup->matcher)
404 if (analyze_node(lookup->obj_type, lookup->matcher, errbuf))
405 return -1;
406 if (lookup->filter)
407 return analyze_node(FILTER_CONTEXT, lookup->filter, errbuf);
408 return 0;
409 } /* analyze_lookup */
411 static int
412 analyze_store(sdb_ast_store_t *st, sdb_strbuf_t *errbuf)
413 {
414 if ((st->obj_type != SDB_ATTRIBUTE)
415 && (! VALID_OBJ_TYPE(st->obj_type))) {
416 sdb_strbuf_sprintf(errbuf, "Invalid object type %#x "
417 "in STORE command", st->obj_type);
418 return -1;
419 }
420 if (! st->name) {
421 sdb_strbuf_sprintf(errbuf, "Missing object name in "
422 "STORE %s command", SDB_STORE_TYPE_TO_NAME(st->obj_type));
423 return -1;
424 }
426 if ((st->obj_type == SDB_HOST) && st->hostname) {
427 sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
428 "in STORE HOST command", st->hostname);
429 return -1;
430 }
431 else if ((st->obj_type != SDB_HOST) && (! st->hostname)) {
432 sdb_strbuf_sprintf(errbuf, "Missing parent hostname for '%s' "
433 "in STORE %s command", st->name,
434 SDB_STORE_TYPE_TO_NAME(st->obj_type));
435 return -1;
436 }
438 if (st->obj_type == SDB_ATTRIBUTE) {
439 if ((st->parent_type <= 0) && st->parent) {
440 sdb_strbuf_sprintf(errbuf, "Unexpected parent hostname '%s' "
441 "in STORE %s command", st->parent,
442 SDB_STORE_TYPE_TO_NAME(st->obj_type));
443 return -1;
444 }
445 else if (st->parent_type > 0) {
446 if (! VALID_OBJ_TYPE(st->parent_type)) {
447 sdb_strbuf_sprintf(errbuf, "Invalid parent type %#x "
448 "in STORE %s command", st->parent_type,
449 SDB_STORE_TYPE_TO_NAME(st->obj_type));
450 return -1;
451 }
452 if (! st->parent) {
453 sdb_strbuf_sprintf(errbuf, "Missing %s parent name "
454 "in STORE %s command",
455 SDB_STORE_TYPE_TO_NAME(st->parent_type),
456 SDB_STORE_TYPE_TO_NAME(st->obj_type));
457 return -1;
458 }
459 }
460 }
461 else if ((st->parent_type > 0) || st->parent) {
462 sdb_strbuf_sprintf(errbuf, "Unexpected %s parent name '%s' "
463 "in STORE %s command",
464 SDB_STORE_TYPE_TO_NAME(st->parent_type),
465 st->parent ? st->parent : "<unknown>",
466 SDB_STORE_TYPE_TO_NAME(st->obj_type));
467 return -1;
468 }
470 if (st->obj_type == SDB_METRIC) {
471 if ((! st->store_type) != (! st->store_id)) {
472 sdb_strbuf_sprintf(errbuf, "Incomplete metric store %s %s "
473 "in STORE METRIC command",
474 st->store_type ? st->store_type : "<unknown>",
475 st->store_id ? st->store_id : "<unknown>");
476 return -1;
477 }
478 }
479 else if (st->store_type || st->store_id) {
480 sdb_strbuf_sprintf(errbuf, "Unexpected metric store %s %s "
481 "in STORE %s command",
482 st->store_type ? st->store_type : "<unknown>",
483 st->store_id ? st->store_id : "<unknown>",
484 SDB_STORE_TYPE_TO_NAME(st->obj_type));
485 return -1;
486 }
488 if ((! (st->obj_type == SDB_ATTRIBUTE))
489 && (st->value.type != SDB_TYPE_NULL)) {
490 char v_str[sdb_data_format(&st->value, NULL, 0, SDB_DOUBLE_QUOTED) + 1];
491 sdb_data_format(&st->value, v_str, sizeof(v_str), SDB_DOUBLE_QUOTED);
492 sdb_strbuf_sprintf(errbuf, "Unexpected value %s in STORE %s command",
493 v_str, SDB_STORE_TYPE_TO_NAME(st->obj_type));
494 return -1;
495 }
496 return 0;
497 } /* analyze_store */
499 static int
500 analyze_timeseries(sdb_ast_timeseries_t *ts, sdb_strbuf_t *errbuf)
501 {
502 if (! ts->hostname) {
503 sdb_strbuf_sprintf(errbuf, "Missing hostname in STORE command");
504 return -1;
505 }
506 if (! ts->metric) {
507 sdb_strbuf_sprintf(errbuf, "Missing metric name in STORE command");
508 return -1;
509 }
510 if (ts->end <= ts->start) {
511 char start_str[64], end_str[64];
512 sdb_strftime(start_str, sizeof(start_str), "%F %T Tz", ts->start);
513 sdb_strftime(end_str, sizeof(end_str), "%F %T Tz", ts->end);
514 sdb_strbuf_sprintf(errbuf, "Start time (%s) greater than "
515 "end time (%s) in STORE command", start_str, end_str);
516 return -1;
517 }
518 return 0;
519 } /* analyze_timeseries */
521 /*
522 * public API
523 */
525 int
526 sdb_parser_analyze(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
527 {
528 if (! node) {
529 sdb_strbuf_sprintf(errbuf, "Empty AST node");
530 return -1;
531 }
533 /* top-level nodes don't have a type */
534 node->data_type = -1;
536 if (node->type == SDB_AST_TYPE_FETCH)
537 return analyze_fetch(SDB_AST_FETCH(node), errbuf);
538 else if (node->type == SDB_AST_TYPE_LIST)
539 return analyze_list(SDB_AST_LIST(node), errbuf);
540 else if (node->type == SDB_AST_TYPE_LOOKUP)
541 return analyze_lookup(SDB_AST_LOOKUP(node), errbuf);
542 else if (node->type == SDB_AST_TYPE_STORE)
543 return analyze_store(SDB_AST_STORE(node), errbuf);
544 else if (node->type == SDB_AST_TYPE_TIMESERIES)
545 return analyze_timeseries(SDB_AST_TIMESERIES(node), errbuf);
547 sdb_strbuf_sprintf(errbuf, "Invalid top-level AST node "
548 "of type %#x", node->type);
549 return -1;
550 } /* sdb_parser_analyze */
552 int
553 sdb_parser_analyze_conditional(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
554 {
555 if (! node) {
556 sdb_strbuf_sprintf(errbuf, "Empty conditional node");
557 return -1;
558 }
559 if (! SDB_AST_IS_LOGICAL(node)) {
560 sdb_strbuf_sprintf(errbuf, "Not a conditional node (got %s)",
561 SDB_AST_TYPE_TO_STRING(node));
562 return -1;
563 }
564 return analyze_node(UNSPEC_CONTEXT, node, errbuf);
565 } /* sdb_parser_analyze_conditional */
567 int
568 sdb_parser_analyze_arith(sdb_ast_node_t *node, sdb_strbuf_t *errbuf)
569 {
570 if (! node) {
571 sdb_strbuf_sprintf(errbuf, "Empty arithmetic node");
572 return -1;
573 }
574 if (! SDB_AST_IS_ARITHMETIC(node)) {
575 sdb_strbuf_sprintf(errbuf, "Not an arithmetic node (got %s)",
576 SDB_AST_TYPE_TO_STRING(node));
577 return -1;
578 }
579 return analyze_node(UNSPEC_CONTEXT, node, errbuf);
580 } /* sdb_parser_analyze_arith */
582 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */