Code

analyzer: attributes and backends are always iterable, even in filters.
[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                         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                                 return 0;
100                         sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s "
101                                         "in %s context",
102                                         SDB_STORE_TYPE_TO_NAME(e->data.data.integer),
103                                         EXPR_TO_STRING(e->left), SDB_STORE_TYPE_TO_NAME(context));
104                         return -1;
106                 case ATTR_VALUE:
107                 case FIELD_VALUE:
108                 case 0:
109                         break;
111                 default:
112                         if (analyze_expr(context, e->left, errbuf))
113                                 return -1;
114                         if (analyze_expr(context, e->right, errbuf))
115                                 return -1;
117                         if ((e->left->data_type > 0) && (e->right->data_type > 0)) {
118                                 if (sdb_data_expr_type(e->type, e->left->data_type,
119                                                         e->right->data_type) < 0) {
120                                         op_error(errbuf, e->type, e->left->data_type,
121                                                         e->right->data_type);
122                                         return -1;
123                                 }
124                         }
125                         break;
126         }
127         return 0;
128 } /* analyze_expr */
130 static int
131 analyze_matcher(int context, int parent_type,
132                 sdb_store_matcher_t *m, sdb_strbuf_t *errbuf)
134         if (! m)
135                 return 0;
137         switch (m->type) {
138                 case MATCHER_OR:
139                 case MATCHER_AND:
140                         assert(OP_M(m)->left && OP_M(m)->right);
141                         if (analyze_matcher(context, m->type, OP_M(m)->left, errbuf))
142                                 return -1;
143                         if (analyze_matcher(context, m->type, OP_M(m)->right, errbuf))
144                                 return -1;
145                         break;
147                 case MATCHER_NOT:
148                         assert(UOP_M(m)->op);
149                         if (analyze_matcher(context, m->type, UOP_M(m)->op, errbuf))
150                                 return -1;
151                         break;
153                 case MATCHER_ANY:
154                 case MATCHER_ALL:
155                 {
156                         int child_context = -1;
157                         int left_type = -1;
158                         int type = -1;
160                         assert(ITER_M(m)->m);
162                         if ((ITER_M(m)->iter->type == TYPED_EXPR)
163                                         || (ITER_M(m)->iter->type == FIELD_VALUE))
164                                 type = ITER_M(m)->iter->data.data.integer;
166                         if (context == -1) { /* inside a filter */
167                                 /* attributes are always iterable */
168                                 if ((ITER_M(m)->iter->type == TYPED_EXPR)
169                                                 && (type != SDB_ATTRIBUTE)) {
170                                         iter_error(errbuf, m->type, ITER_M(m)->iter, context);
171                                         return -1;
172                                 }
173                                 /* backends are always iterable */
174                                 if ((ITER_M(m)->iter->type == FIELD_VALUE)
175                                                 && (! (type != SDB_FIELD_BACKEND))) {
176                                         iter_error(errbuf, m->type, ITER_M(m)->iter, context);
177                                         return -1;
178                                 }
179                         }
180                         else if (! sdb_store_expr_iterable(ITER_M(m)->iter, context)) {
181                                 iter_error(errbuf, m->type, ITER_M(m)->iter, context);
182                                 return -1;
183                         }
185                         if (ITER_M(m)->iter->type == TYPED_EXPR) {
186                                 child_context = type;
187                                 left_type = ITER_M(m)->iter->data_type;
188                         }
189                         else if (ITER_M(m)->iter->type == FIELD_VALUE) {
190                                 child_context = context;
191                                 /* element type of the field */
192                                 left_type = ITER_M(m)->iter->data_type & 0xff;
193                         }
194                         else if (! ITER_M(m)->iter->type) {
195                                 child_context = context;
196                                 /* elements of the array constant */
197                                 left_type = ITER_M(m)->iter->data.type & 0xff;
198                         }
199                         else {
200                                 iter_error(errbuf, m->type, ITER_M(m)->iter, context);
201                                 return -1;
202                         }
204                         /* any ary operator will do but these are the once
205                          * we currently support */
206                         if ((ITER_M(m)->m->type != MATCHER_LT)
207                                         && (ITER_M(m)->m->type != MATCHER_LE)
208                                         && (ITER_M(m)->m->type != MATCHER_EQ)
209                                         && (ITER_M(m)->m->type != MATCHER_NE)
210                                         && (ITER_M(m)->m->type != MATCHER_GE)
211                                         && (ITER_M(m)->m->type != MATCHER_GT)
212                                         && (ITER_M(m)->m->type != MATCHER_REGEX)
213                                         && (ITER_M(m)->m->type != MATCHER_NREGEX)) {
214                                 iter_op_error(errbuf, m->type,
215                                                 left_type, ITER_M(m)->m->type,
216                                                 CMP_M(ITER_M(m)->m)->right->data_type);
217                                 return -1;
218                         }
219                         if ((left_type >= 0)
220                                         && (CMP_M(ITER_M(m)->m)->right->data_type >= 0)) {
221                                 if (left_type != CMP_M(ITER_M(m)->m)->right->data_type) {
222                                         iter_op_error(errbuf, m->type,
223                                                         left_type, ITER_M(m)->m->type,
224                                                         CMP_M(ITER_M(m)->m)->right->data_type);
225                                         return -1;
226                                 }
227                         }
228                         if (child_context <= 0) {
229                                 sdb_strbuf_sprintf(errbuf, "Unable to determine the context "
230                                                 "(object type) of iterator %s %s %s %s",
231                                                 MATCHER_SYM(m->type), SDB_TYPE_TO_STRING(left_type),
232                                                 MATCHER_SYM(ITER_M(m)->m->type),
233                                                 SDB_TYPE_TO_STRING(CMP_M(ITER_M(m)->m)->right->data_type));
234                         }
235                         if (analyze_matcher(child_context, m->type, ITER_M(m)->m, errbuf))
236                                 return -1;
237                         break;
238                 }
240                 case MATCHER_LT:
241                 case MATCHER_LE:
242                 case MATCHER_EQ:
243                 case MATCHER_NE:
244                 case MATCHER_GE:
245                 case MATCHER_GT:
246                 {
247                         int left_type = -1;
249                         assert(CMP_M(m)->right);
250                         if ((parent_type == MATCHER_ALL)
251                                         || (parent_type == MATCHER_ANY)) {
252                                 assert(! CMP_M(m)->left);
253                         }
254                         else {
255                                 assert(CMP_M(m)->left);
256                                 left_type = CMP_M(m)->left->data_type;
257                         }
259                         if (analyze_expr(context, CMP_M(m)->left, errbuf))
260                                 return -1;
261                         if (analyze_expr(context, CMP_M(m)->right, errbuf))
262                                 return -1;
264                         if ((left_type > 0) && (CMP_M(m)->right->data_type > 0)) {
265                                 if (left_type == CMP_M(m)->right->data_type)
266                                         return 0;
267                                 cmp_error(errbuf, m->type, left_type,
268                                                 CMP_M(m)->right->data_type);
269                                 return -1;
270                         }
271                         if ((left_type > 0) && (left_type & SDB_TYPE_ARRAY)) {
272                                 cmp_error(errbuf, m->type, left_type,
273                                                 CMP_M(m)->right->data_type);
274                                 return -1;
275                         }
276                         if ((CMP_M(m)->right->data_type > 0)
277                                         && (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY)) {
278                                 cmp_error(errbuf, m->type, left_type,
279                                                 CMP_M(m)->right->data_type);
280                                 return -1;
281                         }
282                         break;
283                 }
285                 case MATCHER_IN:
286                 case MATCHER_NIN:
287                         if (analyze_expr(context, CMP_M(m)->left, errbuf))
288                                 return -1;
289                         if (analyze_expr(context, CMP_M(m)->right, errbuf))
290                                 return -1;
292                         /* the left operand may be a scalar or an array but the element
293                          * type has to match */
294                         if ((CMP_M(m)->right->data_type > 0)
295                                         && (! (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY))) {
296                                 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
297                                                 CMP_M(m)->right->data_type);
298                                 return -1;
299                         }
300                         if ((CMP_M(m)->left->data_type > 0)
301                                         && (CMP_M(m)->right->data_type > 0)) {
302                                 if ((CMP_M(m)->left->data_type & 0xff)
303                                                 != (CMP_M(m)->right->data_type & 0xff)) {
304                                         cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
305                                                         CMP_M(m)->right->data_type);
306                                         return -1;
307                                 }
308                         }
309                         break;
311                 case MATCHER_REGEX:
312                 case MATCHER_NREGEX:
313                         if (analyze_expr(context, CMP_M(m)->left, errbuf))
314                                 return -1;
315                         if (analyze_expr(context, CMP_M(m)->right, errbuf))
316                                 return -1;
318                         /* all types are supported for the left operand */
319                         if ((CMP_M(m)->right->data_type > 0)
320                                         && (CMP_M(m)->right->data_type != SDB_TYPE_REGEX)
321                                         && (CMP_M(m)->right->data_type != SDB_TYPE_STRING)) {
322                                 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
323                                                 CMP_M(m)->right->data_type);
324                                 return -1;
325                         }
326                         break;
328                 case MATCHER_ISNULL:
329                 case MATCHER_ISNNULL:
330                         if (analyze_expr(context, ISNULL_M(m)->expr, errbuf))
331                                 return -1;
332                         break;
334                 default:
335                         sdb_strbuf_sprintf(errbuf, "Unknown matcher type %d", m->type);
336                         return -1;
337         }
338         return 0;
339 } /* analyze_matcher */
341 /*
342  * public API
343  */
345 int
346 sdb_fe_analyze(sdb_conn_node_t *node, sdb_strbuf_t *errbuf)
348         sdb_store_matcher_t *m = NULL, *filter = NULL;
349         int context = -1;
350         int status = 0;
352         if (! node)
353                 return -1;
355         /* For now, this function checks basic matcher attributes only;
356          * later, this may be turned into one of multiple AST visitors. */
357         if (node->cmd == SDB_CONNECTION_FETCH) {
358                 conn_fetch_t *fetch = CONN_FETCH(node);
359                 if ((fetch->type == SDB_HOST) && fetch->name) {
360                         sdb_strbuf_sprintf(errbuf, "Unexpected STRING '%s'", fetch->name);
361                         return -1;
362                 }
363                 if ((fetch->type != SDB_HOST) && (! fetch->name)) {
364                         sdb_strbuf_sprintf(errbuf, "Missing %s name",
365                                         SDB_STORE_TYPE_TO_NAME(fetch->type));
366                         return -1;
367                 }
368                 if (fetch->filter)
369                         filter = fetch->filter->matcher;
370                 context = fetch->type;
371         }
372         else if (node->cmd == SDB_CONNECTION_LIST) {
373                 if (CONN_LIST(node)->filter)
374                         filter = CONN_LIST(node)->filter->matcher;
375                 context = CONN_LIST(node)->type;
376         }
377         else if (node->cmd == SDB_CONNECTION_LOOKUP) {
378                 if (CONN_LOOKUP(node)->matcher)
379                         m = CONN_LOOKUP(node)->matcher->matcher;
380                 if (CONN_LOOKUP(node)->filter)
381                         filter = CONN_LOOKUP(node)->filter->matcher;
382                 context = CONN_LOOKUP(node)->type;
383         }
384         else if ((node->cmd == SDB_CONNECTION_STORE_HOST)
385                         || (node->cmd == SDB_CONNECTION_STORE_SERVICE)
386                         || (node->cmd == SDB_CONNECTION_STORE_METRIC)
387                         || (node->cmd == SDB_CONNECTION_STORE_ATTRIBUTE)) {
388                 return 0;
389         }
390         else if (node->cmd == SDB_CONNECTION_TIMESERIES) {
391                 return 0;
392         }
393         else {
394                 sdb_strbuf_sprintf(errbuf,
395                                 "Don't know how to analyze command %#x", node->cmd);
396                 return -1;
397         }
399         if (context <= 0) {
400                 sdb_strbuf_sprintf(errbuf, "Unable to determine the context "
401                                 "(object type) for command %#x", node->cmd);
402                 return -1;
403         }
404         if (analyze_matcher(context, -1, m, errbuf))
405                 status = -1;
406         if (analyze_matcher(-1, -1, filter, errbuf))
407                 status = -1;
408         return status;
409 } /* sdb_fe_analyze */
411 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */