Code

store: Don't report an error in scan() if the store is still empty.
[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, int oper, int context)
44 {
45         sdb_strbuf_sprintf(errbuf, "Cannot use %s %s in %s context",
46                         MATCHER_SYM(op), SDB_STORE_TYPE_TO_NAME(oper),
47                         SDB_STORE_TYPE_TO_NAME(context));
48 } /* iter_error */
50 static void
51 iter_array_error(sdb_strbuf_t *errbuf, int op,
52                 int array_type, int cmp, int value_type)
53 {
54         sdb_strbuf_sprintf(errbuf, "Invalid array iterator %s %s %s %s",
55                         MATCHER_SYM(op), SDB_TYPE_TO_STRING(array_type),
56                         MATCHER_SYM(cmp), SDB_TYPE_TO_STRING(value_type));
57         if ((array_type & 0xff) != value_type)
58                 sdb_strbuf_append(errbuf, " (type mismatch)");
59         else
60                 sdb_strbuf_append(errbuf, " (invalid operator)");
61 } /* iter_array_error */
63 static void
64 cmp_error(sdb_strbuf_t *errbuf, int op, int left, int right)
65 {
66         sdb_strbuf_sprintf(errbuf, "Invalid operator %s for types %s and %s",
67                         MATCHER_SYM(op), SDB_TYPE_TO_STRING(left),
68                         SDB_TYPE_TO_STRING(right));
69 } /* cmp_error */
71 static int
72 analyze_expr(int context, sdb_store_expr_t *e, sdb_strbuf_t *errbuf)
73 {
74         if (! e)
75                 return 0;
77         if ((e->type < TYPED_EXPR) || (SDB_DATA_CONCAT < e->type)) {
78                 sdb_strbuf_sprintf(errbuf, "Invalid expression of type %d", e->type);
79                 return -1;
80         }
82         switch (e->type) {
83                 case TYPED_EXPR:
84                         if (analyze_expr((int)e->data.data.integer, e->left, errbuf))
85                                 return -1;
86                         if (context == (int)e->data.data.integer)
87                                 return 0;
88                         if ((e->data.data.integer == SDB_HOST) &&
89                                         ((context == SDB_SERVICE) || (context == SDB_METRIC)))
90                                 return 0;
91                         sdb_strbuf_sprintf(errbuf, "Invalid expression %s.%s "
92                                         "in %s context",
93                                         SDB_STORE_TYPE_TO_NAME(e->data.data.integer),
94                                         EXPR_TO_STRING(e->left), SDB_STORE_TYPE_TO_NAME(context));
95                         return -1;
97                 case ATTR_VALUE:
98                 case FIELD_VALUE:
99                 case 0:
100                         break;
102                 default:
103                         if (analyze_expr(context, e->left, errbuf))
104                                 return -1;
105                         if (analyze_expr(context, e->right, errbuf))
106                                 return -1;
107                         break;
108         }
109         return 0;
110 } /* analyze_expr */
112 static int
113 analyze_matcher(int context, sdb_store_matcher_t *m, sdb_strbuf_t *errbuf)
115         if (! m)
116                 return 0;
118         switch (m->type) {
119                 case MATCHER_OR:
120                 case MATCHER_AND:
121                         assert(OP_M(m)->left && OP_M(m)->right);
122                         if (analyze_matcher(context, OP_M(m)->left, errbuf))
123                                 return -1;
124                         if (analyze_matcher(context, OP_M(m)->right, errbuf))
125                                 return -1;
126                         break;
128                 case MATCHER_NOT:
129                         assert(UOP_M(m)->op);
130                         if (analyze_matcher(context, UOP_M(m)->op, errbuf))
131                                 return -1;
132                         break;
134                 case MATCHER_ANY:
135                 case MATCHER_ALL:
136                         assert(ITER_M(m)->m);
137                         if ((context != SDB_HOST)
138                                         && (context != SDB_SERVICE)
139                                         && (context != SDB_METRIC)) {
140                                 iter_error(errbuf, m->type, ITER_M(m)->type, context);
141                                 return -1;
142                         }
143                         if (ITER_M(m)->type == context) {
144                                 iter_error(errbuf, m->type, ITER_M(m)->type, context);
145                                 return -1;
146                         }
147                         if ((ITER_M(m)->type != SDB_SERVICE)
148                                         && (ITER_M(m)->type != SDB_METRIC)
149                                         && (ITER_M(m)->type != SDB_ATTRIBUTE)
150                                         && (ITER_M(m)->type != SDB_FIELD_BACKEND)) {
151                                 iter_error(errbuf, m->type, ITER_M(m)->type, context);
152                                 return -1;
153                         }
154                         if ((context == SDB_SERVICE)
155                                         && (ITER_M(m)->type == SDB_METRIC)) {
156                                 iter_error(errbuf, m->type, ITER_M(m)->type, context);
157                                 return -1;
158                         }
159                         else if ((context == SDB_METRIC)
160                                         && (ITER_M(m)->type == SDB_SERVICE)) {
161                                 iter_error(errbuf, m->type, ITER_M(m)->type, context);
162                                 return -1;
163                         }
164                         if (ITER_M(m)->type == SDB_FIELD_BACKEND) {
165                                 /* array iterators only support simple comparison atm */
166                                 if ((ITER_M(m)->m->type != MATCHER_LT)
167                                                 && (ITER_M(m)->m->type != MATCHER_LE)
168                                                 && (ITER_M(m)->m->type != MATCHER_EQ)
169                                                 && (ITER_M(m)->m->type != MATCHER_NE)
170                                                 && (ITER_M(m)->m->type != MATCHER_GE)
171                                                 && (ITER_M(m)->m->type != MATCHER_GT)
172                                                 && (ITER_M(m)->m->type != MATCHER_REGEX)
173                                                 && (ITER_M(m)->m->type != MATCHER_NREGEX)) {
174                                         iter_array_error(errbuf, m->type,
175                                                         CMP_M(ITER_M(m)->m)->left->data_type,
176                                                         ITER_M(m)->m->type,
177                                                         CMP_M(ITER_M(m)->m)->right->data_type);
178                                         return -1;
179                                 }
180                                 if (CMP_M(ITER_M(m)->m)->right->data_type < 0)
181                                         return 0; /* skip further type checks */
182                                 if (CMP_M(ITER_M(m)->m)->right->data_type & SDB_TYPE_ARRAY) {
183                                         iter_array_error(errbuf, m->type,
184                                                         CMP_M(ITER_M(m)->m)->left->data_type,
185                                                         ITER_M(m)->m->type,
186                                                         CMP_M(ITER_M(m)->m)->right->data_type);
187                                         return -1;
188                                 }
189                                 if ((CMP_M(ITER_M(m)->m)->left->data_type & 0xff)
190                                                 != CMP_M(ITER_M(m)->m)->right->data_type) {
191                                         iter_array_error(errbuf, m->type,
192                                                         CMP_M(ITER_M(m)->m)->left->data_type,
193                                                         ITER_M(m)->m->type,
194                                                         CMP_M(ITER_M(m)->m)->right->data_type);
195                                         return -1;
196                                 }
197                         }
198                         else if (analyze_matcher(ITER_M(m)->type, ITER_M(m)->m, errbuf))
199                                 return -1;
200                         break;
202                 case MATCHER_LT:
203                 case MATCHER_LE:
204                 case MATCHER_EQ:
205                 case MATCHER_NE:
206                 case MATCHER_GE:
207                 case MATCHER_GT:
208                         assert(CMP_M(m)->left && CMP_M(m)->right);
209                         if (analyze_expr(context, CMP_M(m)->left, errbuf))
210                                 return -1;
211                         if (analyze_expr(context, CMP_M(m)->right, errbuf))
212                                 return -1;
214                         if ((CMP_M(m)->left->data_type > 0)
215                                         && (CMP_M(m)->right->data_type > 0)
216                                         && (CMP_M(m)->left->data_type == CMP_M(m)->right->data_type))
217                                 return 0;
218                         if ((CMP_M(m)->left->data_type > 0)
219                                         && (CMP_M(m)->left->data_type & SDB_TYPE_ARRAY)) {
220                                 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
221                                                 CMP_M(m)->right->data_type);
222                                 return -1;
223                         }
224                         if ((CMP_M(m)->right->data_type > 0)
225                                         && (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY)) {
226                                 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
227                                                 CMP_M(m)->right->data_type);
228                                 return -1;
229                         }
230                         break;
232                 case MATCHER_IN:
233                         if (analyze_expr(context, CMP_M(m)->left, errbuf))
234                                 return -1;
235                         if (analyze_expr(context, CMP_M(m)->right, errbuf))
236                                 return -1;
238                         if ((CMP_M(m)->left->data_type > 0)
239                                         && (CMP_M(m)->left->data_type & SDB_TYPE_ARRAY)) {
240                                 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
241                                                 CMP_M(m)->right->data_type);
242                                 return -1;
243                         }
244                         if ((CMP_M(m)->right->data_type > 0)
245                                         && (! (CMP_M(m)->right->data_type & SDB_TYPE_ARRAY))) {
246                                 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
247                                                 CMP_M(m)->right->data_type);
248                                 return -1;
249                         }
250                         break;
252                 case MATCHER_REGEX:
253                 case MATCHER_NREGEX:
254                         if (analyze_expr(context, CMP_M(m)->left, errbuf))
255                                 return -1;
256                         if (analyze_expr(context, CMP_M(m)->right, errbuf))
257                                 return -1;
259                         /* all types are supported for the left operand */
260                         if ((CMP_M(m)->right->data_type > 0)
261                                         && (CMP_M(m)->right->data_type != SDB_TYPE_REGEX)
262                                         && (CMP_M(m)->right->data_type != SDB_TYPE_STRING)) {
263                                 cmp_error(errbuf, m->type, CMP_M(m)->left->data_type,
264                                                 CMP_M(m)->right->data_type);
265                                 return -1;
266                         }
267                         break;
269                 case MATCHER_ISNULL:
270                 case MATCHER_ISNNULL:
271                         if (analyze_expr(context, ISNULL_M(m)->expr, errbuf))
272                                 return -1;
273                         break;
275                 default:
276                         sdb_strbuf_sprintf(errbuf, "Unknown matcher type %d", m->type);
277                         return -1;
278         }
279         return 0;
280 } /* analyze_matcher */
282 /*
283  * public API
284  */
286 int
287 sdb_fe_analyze(sdb_conn_node_t *node, sdb_strbuf_t *errbuf)
289         sdb_store_matcher_t *m = NULL, *filter = NULL;
290         int context = -1;
291         int status = 0;
293         if (! node)
294                 return -1;
296         /* For now, this function checks basic matcher attributes only;
297          * later, this may be turned into one of multiple AST visitors. */
298         if (node->cmd == SDB_CONNECTION_FETCH) {
299                 conn_fetch_t *fetch = CONN_FETCH(node);
300                 if ((fetch->type == SDB_HOST) && fetch->name) {
301                         sdb_strbuf_sprintf(errbuf, "Unexpected STRING '%s'", fetch->name);
302                         return -1;
303                 }
304                 if ((fetch->type != SDB_HOST) && (! fetch->name)) {
305                         sdb_strbuf_sprintf(errbuf, "Missing %s name",
306                                         SDB_STORE_TYPE_TO_NAME(fetch->type));
307                         return -1;
308                 }
309                 if (fetch->filter)
310                         filter = fetch->filter->matcher;
311                 context = fetch->type;
312         }
313         else if (node->cmd == SDB_CONNECTION_LIST) {
314                 if (CONN_LIST(node)->filter)
315                         filter = CONN_LIST(node)->filter->matcher;
316                 context = CONN_LIST(node)->type;
317         }
318         else if (node->cmd == SDB_CONNECTION_LOOKUP) {
319                 if (CONN_LOOKUP(node)->matcher)
320                         m = CONN_LOOKUP(node)->matcher->matcher;
321                 if (CONN_LOOKUP(node)->filter)
322                         filter = CONN_LOOKUP(node)->filter->matcher;
323                 context = CONN_LOOKUP(node)->type;
324         }
325         else if ((node->cmd == SDB_CONNECTION_STORE_HOST)
326                         || (node->cmd == SDB_CONNECTION_STORE_SERVICE)
327                         || (node->cmd == SDB_CONNECTION_STORE_METRIC)
328                         || (node->cmd == SDB_CONNECTION_STORE_ATTRIBUTE)) {
329                 return 0;
330         }
331         else if (node->cmd == SDB_CONNECTION_TIMESERIES) {
332                 return 0;
333         }
334         else {
335                 sdb_strbuf_sprintf(errbuf,
336                                 "Don't know how to analyze command %#x", node->cmd);
337                 return -1;
338         }
340         if (analyze_matcher(context, m, errbuf))
341                 status = -1;
342         if (analyze_matcher(-1, filter, errbuf))
343                 status = -1;
344         return status;
345 } /* sdb_fe_analyze */
347 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */