Code

01e1ad357adec6502fc7efc74768bee7cdf03969
[sysdb.git] / src / frontend / analyzer.c
1 /*
2  * SysDB - src/frontend/analyzer.c
3  * Copyright (C) 2014 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 "core/store-private.h"
31 #include "frontend/connection-private.h"
32 #include "frontend/parser.h"
33 #include "utils/error.h"
34 #include "utils/strbuf.h"
36 #include <assert.h>
38 /*
39  * private helper functions
40  */
42 static void
43 iter_error(sdb_strbuf_t *errbuf, int op, sdb_store_expr_t *iter, int context)
44 {
45         sdb_strbuf_sprintf(errbuf, "Invalid %s iterator: %s %s "
46                         "not iterable in %s context", MATCHER_SYM(op),
47                         EXPR_TO_STRING(iter), SDB_STORE_TYPE_TO_NAME(iter->data_type),
48                         context == -1 ? "generic" : SDB_STORE_TYPE_TO_NAME(context));
49 } /* iter_error */
51 static void
52 iter_op_error(sdb_strbuf_t *errbuf, int op,
53                 int iter_type, int cmp, int value_type)
54 {
55         sdb_strbuf_sprintf(errbuf, "Invalid iterator %s %s %s %s",
56                         MATCHER_SYM(op), SDB_TYPE_TO_STRING(iter_type),
57                         MATCHER_SYM(cmp), SDB_TYPE_TO_STRING(value_type));
58         if ((iter_type & 0xff) != value_type)
59                 sdb_strbuf_append(errbuf, " (type mismatch)");
60         else
61                 sdb_strbuf_append(errbuf, " (invalid operator)");
62 } /* iter_op_error */
64 static void
65 cmp_error(sdb_strbuf_t *errbuf, int op, int left, int right)
66 {
67         sdb_strbuf_sprintf(errbuf, "Invalid operator %s for types %s and %s",
68                         MATCHER_SYM(op), SDB_TYPE_TO_STRING(left),
69                         SDB_TYPE_TO_STRING(right));
70 } /* cmp_error */
72 static void
73 op_error(sdb_strbuf_t *errbuf, int op, int left, int right)
74 {
75         sdb_strbuf_sprintf(errbuf, "Invalid operator %s for types %s and %s",
76                         SDB_DATA_OP_TO_STRING(op), SDB_TYPE_TO_STRING(left),
77                         SDB_TYPE_TO_STRING(right));
78 } /* cmp_error */
80 static int
81 analyze_expr(int context, sdb_store_expr_t *e, sdb_strbuf_t *errbuf)
82 {
83         if (! e)
84                 return 0;
86         if ((e->type < TYPED_EXPR) || (SDB_DATA_CONCAT < e->type)) {
87                 sdb_strbuf_sprintf(errbuf, "Invalid expression of type %d", e->type);
88                 return -1;
89         }
91         switch (e->type) {
92                 case TYPED_EXPR:
93                         if (analyze_expr((int)e->data.data.integer, e->left, errbuf))
94                                 return -1;
95                         if (context == (int)e->data.data.integer)
96                                 return 0;
97                         if ((e->data.data.integer == SDB_HOST) &&
98                                         ((context == SDB_SERVICE) || (context == SDB_METRIC)
99                                                 || (context < 0)))
100                                 return 0;
101                         sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s "
102                                         "in %s context",
103                                         SDB_STORE_TYPE_TO_NAME(e->data.data.integer),
104                                         EXPR_TO_STRING(e->left),
105                                         context == -1 ? "generic" : SDB_STORE_TYPE_TO_NAME(context));
106                         return -1;
108                 case ATTR_VALUE:
109                 case 0:
110                         break;
112                 case FIELD_VALUE:
113                         if ((e->data.data.integer == SDB_FIELD_VALUE)
114                                         && (context != SDB_ATTRIBUTE)) {
115                                 sdb_strbuf_sprintf(errbuf, "Invalid expression %s.value "
116                                                 "(only attributes have a value)",
117                                                 SDB_STORE_TYPE_TO_NAME(context));
118                                 return -1;
119                         }
120                         break;
122                 default:
123                         if (analyze_expr(context, e->left, errbuf))
124                                 return -1;
125                         if (analyze_expr(context, e->right, errbuf))
126                                 return -1;
128                         if ((e->left->data_type > 0) && (e->right->data_type > 0)) {
129                                 if (sdb_data_expr_type(e->type, e->left->data_type,
130                                                         e->right->data_type) < 0) {
131                                         op_error(errbuf, e->type, e->left->data_type,
132                                                         e->right->data_type);
133                                         return -1;
134                                 }
135                         }
136                         break;
137         }
138         return 0;
139 } /* analyze_expr */
141 static int
142 analyze_matcher(int context, int parent_type,
143                 sdb_store_matcher_t *m, sdb_strbuf_t *errbuf)
145         if (! m)
146                 return 0;
148         switch (m->type) {
149                 case MATCHER_OR:
150                 case MATCHER_AND:
151                         assert(OP_M(m)->left && OP_M(m)->right);
152                         if (analyze_matcher(context, m->type, OP_M(m)->left, errbuf))
153                                 return -1;
154                         if (analyze_matcher(context, m->type, OP_M(m)->right, errbuf))
155                                 return -1;
156                         break;
158                 case MATCHER_NOT:
159                         assert(UOP_M(m)->op);
160                         if (analyze_matcher(context, m->type, UOP_M(m)->op, errbuf))
161                                 return -1;
162                         break;
164                 case MATCHER_ANY:
165                 case MATCHER_ALL:
166                 {
167                         int child_context = -1;
168                         int left_type = -1;
169                         int type = -1;
171                         assert(ITER_M(m)->m);
173                         if ((ITER_M(m)->iter->type == TYPED_EXPR)
174                                         || (ITER_M(m)->iter->type == FIELD_VALUE))
175                                 type = (int)ITER_M(m)->iter->data.data.integer;
177                         if (context == -1) { /* inside a filter */
178                                 /* attributes are always iterable */
179                                 if ((ITER_M(m)->iter->type == TYPED_EXPR)
180                                                 && (type != SDB_ATTRIBUTE)) {
181                                         iter_error(errbuf, m->type, ITER_M(m)->iter, context);
182                                         return -1;
183                                 }
184                                 /* backends are always iterable */
185                                 if ((ITER_M(m)->iter->type == FIELD_VALUE)
186                                                 && (! (type != SDB_FIELD_BACKEND))) {
187                                         iter_error(errbuf, m->type, ITER_M(m)->iter, context);
188                                         return -1;
189                                 }
190                         }
191                         else if (! sdb_store_expr_iterable(ITER_M(m)->iter, context)) {
192                                 iter_error(errbuf, m->type, ITER_M(m)->iter, context);
193                                 return -1;
194                         }
196                         if (ITER_M(m)->iter->type == TYPED_EXPR) {
197                                 child_context = type;
198                                 left_type = ITER_M(m)->iter->data_type;
199                         }
200                         else if (ITER_M(m)->iter->type == FIELD_VALUE) {
201                                 child_context = context;
202                                 /* element type of the field */
203                                 left_type = ITER_M(m)->iter->data_type & 0xff;
204                         }
205                         else if (! ITER_M(m)->iter->type) {
206                                 child_context = context;
207                                 /* elements of the array constant */
208                                 left_type = ITER_M(m)->iter->data.type & 0xff;
209                         }
210                         else {
211                                 iter_error(errbuf, m->type, ITER_M(m)->iter, context);
212                                 return -1;
213                         }
215                         /* any ary operator will do but these are the once
216                          * we currently support */
217                         if ((ITER_M(m)->m->type != MATCHER_LT)
218                                         && (ITER_M(m)->m->type != MATCHER_LE)
219                                         && (ITER_M(m)->m->type != MATCHER_EQ)
220                                         && (ITER_M(m)->m->type != MATCHER_NE)
221                                         && (ITER_M(m)->m->type != MATCHER_GE)
222                                         && (ITER_M(m)->m->type != MATCHER_GT)
223                                         && (ITER_M(m)->m->type != MATCHER_REGEX)
224                                         && (ITER_M(m)->m->type != MATCHER_NREGEX)) {
225                                 iter_op_error(errbuf, m->type,
226                                                 left_type, ITER_M(m)->m->type,
227                                                 CMP_M(ITER_M(m)->m)->right->data_type);
228                                 return -1;
229                         }
230                         if ((left_type >= 0)
231                                         && (CMP_M(ITER_M(m)->m)->right->data_type >= 0)) {
232                                 if (left_type != CMP_M(ITER_M(m)->m)->right->data_type) {
233                                         iter_op_error(errbuf, m->type,
234                                                         left_type, ITER_M(m)->m->type,
235                                                         CMP_M(ITER_M(m)->m)->right->data_type);
236                                         return -1;
237                                 }
238                         }
239                         if (child_context <= 0) {
240                                 sdb_strbuf_sprintf(errbuf, "Unable to determine the context "
241                                                 "(object type) of iterator %s %s %s %s",
242                                                 MATCHER_SYM(m->type), SDB_TYPE_TO_STRING(left_type),
243                                                 MATCHER_SYM(ITER_M(m)->m->type),
244                                                 SDB_TYPE_TO_STRING(CMP_M(ITER_M(m)->m)->right->data_type));
245                         }
246                         if (analyze_matcher(child_context, m->type, ITER_M(m)->m, errbuf))
247                                 return -1;
248                         break;
249                 }
251                 case MATCHER_LT:
252                 case MATCHER_LE:
253                 case MATCHER_EQ:
254                 case MATCHER_NE:
255                 case MATCHER_GE:
256                 case MATCHER_GT:
257                 {
258                         int left_type = -1;
260                         assert(CMP_M(m)->right);
261                         if ((parent_type == MATCHER_ALL)
262                                         || (parent_type == MATCHER_ANY)) {
263                                 assert(! CMP_M(m)->left);
264                         }
265                         else {
266                                 assert(CMP_M(m)->left);
267                                 left_type = CMP_M(m)->left->data_type;
268                         }
270                         if (analyze_expr(context, CMP_M(m)->left, errbuf))
271                                 return -1;
272                         if (analyze_expr(context, CMP_M(m)->right, errbuf))
273                                 return -1;
275                         if ((left_type > 0) && (CMP_M(m)->right->data_type > 0)) {
276                                 if (left_type == CMP_M(m)->right->data_type)
277                                         return 0;
278                                 cmp_error(errbuf, m->type, left_type,
279                                                 CMP_M(m)->right->data_type);
280                                 return -1;
281                         }
282                         if ((left_type > 0) && (left_type & SDB_TYPE_ARRAY)) {
283                                 cmp_error(errbuf, m->type, left_type,
284                                                 CMP_M(m)->right->data_type);
285                                 return -1;
286                         }
287                         if ((CMP_M(m)->right->data_type > 0)
288                                         && (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY)) {
289                                 cmp_error(errbuf, m->type, left_type,
290                                                 CMP_M(m)->right->data_type);
291                                 return -1;
292                         }
293                         break;
294                 }
296                 case MATCHER_IN:
297                 case MATCHER_NIN:
298                         if (analyze_expr(context, CMP_M(m)->left, errbuf))
299                                 return -1;
300                         if (analyze_expr(context, CMP_M(m)->right, errbuf))
301                                 return -1;
303                         /* the left operand may be a scalar or an array but the element
304                          * type has to match */
305                         if ((CMP_M(m)->right->data_type > 0)
306                                         && (! (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY))) {
307                                 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
308                                                 CMP_M(m)->right->data_type);
309                                 return -1;
310                         }
311                         if ((CMP_M(m)->left->data_type > 0)
312                                         && (CMP_M(m)->right->data_type > 0)) {
313                                 if ((CMP_M(m)->left->data_type & 0xff)
314                                                 != (CMP_M(m)->right->data_type & 0xff)) {
315                                         cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
316                                                         CMP_M(m)->right->data_type);
317                                         return -1;
318                                 }
319                         }
320                         break;
322                 case MATCHER_REGEX:
323                 case MATCHER_NREGEX:
324                         if (analyze_expr(context, CMP_M(m)->left, errbuf))
325                                 return -1;
326                         if (analyze_expr(context, CMP_M(m)->right, errbuf))
327                                 return -1;
329                         /* all types are supported for the left operand */
330                         if ((CMP_M(m)->right->data_type > 0)
331                                         && (CMP_M(m)->right->data_type != SDB_TYPE_REGEX)
332                                         && (CMP_M(m)->right->data_type != SDB_TYPE_STRING)) {
333                                 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
334                                                 CMP_M(m)->right->data_type);
335                                 return -1;
336                         }
337                         break;
339                 case MATCHER_ISNULL:
340                 case MATCHER_ISNNULL:
341                         if (analyze_expr(context, ISNULL_M(m)->expr, errbuf))
342                                 return -1;
343                         break;
345                 default:
346                         sdb_strbuf_sprintf(errbuf, "Unknown matcher type %d", m->type);
347                         return -1;
348         }
349         return 0;
350 } /* analyze_matcher */
352 /*
353  * public API
354  */
356 int
357 sdb_fe_analyze(sdb_conn_node_t *node, sdb_strbuf_t *errbuf)
359         sdb_store_matcher_t *m = NULL, *filter = NULL;
360         int context = -1;
361         int status = 0;
363         if (! node)
364                 return -1;
366         /* For now, this function checks basic matcher attributes only;
367          * later, this may be turned into one of multiple AST visitors. */
368         if (node->cmd == SDB_CONNECTION_FETCH) {
369                 conn_fetch_t *fetch = CONN_FETCH(node);
370                 if ((fetch->type == SDB_HOST) && fetch->name) {
371                         sdb_strbuf_sprintf(errbuf, "Unexpected STRING '%s'", fetch->name);
372                         return -1;
373                 }
374                 if ((fetch->type != SDB_HOST) && (! fetch->name)) {
375                         sdb_strbuf_sprintf(errbuf, "Missing %s name",
376                                         SDB_STORE_TYPE_TO_NAME(fetch->type));
377                         return -1;
378                 }
379                 if (fetch->filter)
380                         filter = fetch->filter->matcher;
381                 context = fetch->type;
382         }
383         else if (node->cmd == SDB_CONNECTION_LIST) {
384                 if (CONN_LIST(node)->filter)
385                         filter = CONN_LIST(node)->filter->matcher;
386                 context = CONN_LIST(node)->type;
387         }
388         else if (node->cmd == SDB_CONNECTION_LOOKUP) {
389                 if (CONN_LOOKUP(node)->matcher)
390                         m = CONN_LOOKUP(node)->matcher->matcher;
391                 if (CONN_LOOKUP(node)->filter)
392                         filter = CONN_LOOKUP(node)->filter->matcher;
393                 context = CONN_LOOKUP(node)->type;
394         }
395         else if ((node->cmd == SDB_CONNECTION_STORE_HOST)
396                         || (node->cmd == SDB_CONNECTION_STORE_SERVICE)
397                         || (node->cmd == SDB_CONNECTION_STORE_METRIC)
398                         || (node->cmd == SDB_CONNECTION_STORE_ATTRIBUTE)) {
399                 return 0;
400         }
401         else if (node->cmd == SDB_CONNECTION_TIMESERIES) {
402                 return 0;
403         }
404         else {
405                 sdb_strbuf_sprintf(errbuf,
406                                 "Don't know how to analyze %s command (id=%#x)",
407                                 SDB_CONN_MSGTYPE_TO_STRING(node->cmd), node->cmd);
408                 return -1;
409         }
411         if (context <= 0) {
412                 sdb_strbuf_sprintf(errbuf, "Unable to determine the context "
413                                 "(object type) for %s command (id=%#x)",
414                                 SDB_CONN_MSGTYPE_TO_STRING(node->cmd), node->cmd);
415                 return -1;
416         }
417         if (analyze_matcher(context, -1, m, errbuf))
418                 status = -1;
419         if (analyze_matcher(-1, -1, filter, errbuf))
420                 status = -1;
421         return status;
422 } /* sdb_fe_analyze */
424 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */