Debian Codegraph @

storage/innobase/eval/eval0eval.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1997, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file eval/eval0eval.cc
29 SQL evaluator: evaluates simple data structures, like expressions, in
30 a query graph
31
32 Created 12/29/1997 Heikki Tuuri
33 *******************************************************/
34
35 #include "eval0eval.h"
36
37 #include "data0data.h"
38
39 #include "rem0cmp.h"
40 #include "row0sel.h"
41
42 /** Dummy address used when we should allocate a buffer of size 0 in
43 eval_node_alloc_val_buf */
44
45 static byte eval_dummy;
46
47 /*************************************************************************
48 Gets the like node from the node */
49 static inline que_node_t *que_node_get_like_node(
50 /* out: next node in a list of nodes */
51 que_node_t *node) /* in: node in a list */
52 {
53 return (((sym_node_t *)node)->like_node);
54 }
55
56 /** Allocate a buffer from global dynamic memory for a value of a que_node.
57 NOTE that this memory must be explicitly freed when the query graph is
58 freed. If the node already has an allocated buffer, that buffer is freed
59 here. NOTE that this is the only function where dynamic memory should be
60 allocated for a query node val field.
61 @return pointer to allocated buffer */
62 byte *eval_node_alloc_val_buf(
63 que_node_t *node, /*!< in: query graph node; sets the val field
64 data field to point to the new buffer, and
65 len field equal to size */
66 ulint size) /*!< in: buffer size */
67 {
68 dfield_t *dfield;
69 byte *data;
70
71 ut_ad(que_node_get_type(node) == QUE_NODE_SYMBOL ||
72 que_node_get_type(node) == QUE_NODE_FUNC);
73
74 dfield = que_node_get_val(node);
75
76 data = static_cast<byte *>(dfield_get_data(dfield));
77
78 if (data != &eval_dummy) {
79 ut::free(data);
80 }
81
82 if (size == 0) {
83 data = &eval_dummy;
84 } else {
85 data =
86 static_cast<byte *>(ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, size));
87 }
88
89 que_node_set_val_buf_size(node, size);
90
91 dfield_set_data(dfield, data, size);
92
93 return (data);
94 }
95
96 /** Free the buffer from global dynamic memory for a value of a que_node,
97 if it has been allocated in the above function. The freeing for pushed
98 column values is done in sel_col_prefetch_buf_free. */
99 void eval_node_free_val_buf(que_node_t *node) /*!< in: query graph node */
100 {
101 dfield_t *dfield;
102 byte *data;
103
104 ut_ad(que_node_get_type(node) == QUE_NODE_SYMBOL ||
105 que_node_get_type(node) == QUE_NODE_FUNC);
106
107 dfield = que_node_get_val(node);
108
109 data = static_cast<byte *>(dfield_get_data(dfield));
110
111 if (que_node_get_val_buf_size(node) > 0) {
112 ut_a(data);
113
114 ut::free(data);
115 }
116 }
117
118 /*********************************************************************
119 Evaluates a LIKE comparison node.
120 @return the result of the comparison */
121 static inline bool eval_cmp_like(que_node_t *arg1, /* !< in: left operand */
122 que_node_t *arg2) /* !< in: right operand */
123 {
124 ib_like_t op;
125 que_node_t *arg3;
126 que_node_t *arg4;
127 const dfield_t *dfield;
128
129 arg3 = que_node_get_like_node(arg2);
130
131 /* Get the comparison type operator */
132 ut_a(arg3);
133
134 dfield = que_node_get_val(arg3);
135 ut_ad(dtype_get_mtype(dfield_get_type(dfield)) == DATA_INT);
136 op = static_cast<ib_like_t>(
137 mach_read_from_4(static_cast<const byte *>(dfield_get_data(dfield))));
138
139 switch (op) {
140 case IB_LIKE_PREFIX:
141 arg4 = que_node_get_next(arg3);
142 return (cmp_dfield_dfield_eq_prefix(que_node_get_val(arg1),
143 que_node_get_val(arg4)));
144 case IB_LIKE_EXACT:
145 return (!cmp_dfield_dfield(que_node_get_val(arg1), que_node_get_val(arg2),
146 true));
147 }
148
149 ut_error;
150 }
151
152 /*********************************************************************
153 Evaluates a comparison node.
154 @return the result of the comparison */
155 bool eval_cmp(func_node_t *cmp_node) /*!< in: comparison node */
156 {
157 que_node_t *arg1;
158 que_node_t *arg2;
159 int res;
160 bool val = false;
161
162 ut_ad(que_node_get_type(cmp_node) == QUE_NODE_FUNC);
163
164 arg1 = cmp_node->args;
165 arg2 = que_node_get_next(arg1);
166
167 switch (cmp_node->func) {
168 case '<':
169 case '=':
170 case '>':
171 case PARS_LE_TOKEN:
172 case PARS_NE_TOKEN:
173 case PARS_GE_TOKEN:
174 res = cmp_dfield_dfield(que_node_get_val(arg1), que_node_get_val(arg2),
175 true);
176
177 switch (cmp_node->func) {
178 case '<':
179 val = (res < 0);
180 break;
181 case '=':
182 val = (res == 0);
183 break;
184 case '>':
185 val = (res > 0);
186 break;
187 case PARS_LE_TOKEN:
188 val = (res <= 0);
189 break;
190 case PARS_NE_TOKEN:
191 val = (res != 0);
192 break;
193 case PARS_GE_TOKEN:
194 val = (res >= 0);
195 break;
196 }
197 break;
198 default:
199 val = eval_cmp_like(arg1, arg2);
200 break;
201 }
202
203 eval_node_set_bool_val(cmp_node, val);
204
205 return (val);
206 }
207
208 /** Evaluates a logical operation node. */
209 static inline void eval_logical(
210 func_node_t *logical_node) /*!< in: logical operation node */
211 {
212 que_node_t *arg1;
213 que_node_t *arg2;
214 bool val2 = false;
215 bool val = false;
216 int func;
217
218 ut_ad(que_node_get_type(logical_node) == QUE_NODE_FUNC);
219
220 arg1 = logical_node->args;
221 arg2 = que_node_get_next(arg1); /* arg2 is NULL if func is 'NOT' */
222
223 auto val1 = eval_node_get_bool_val(arg1);
224
225 if (arg2) {
226 val2 = eval_node_get_bool_val(arg2);
227 }
228
229 func = logical_node->func;
230
231 if (func == PARS_AND_TOKEN) {
232 val = val1 & val2;
233 } else if (func == PARS_OR_TOKEN) {
234 val = val1 | val2;
235 } else if (func == PARS_NOT_TOKEN) {
236 val = !val1;
237 } else {
238 ut_error;
239 }
240
241 eval_node_set_bool_val(logical_node, val);
242 }
243
244 /** Evaluates an arithmetic operation node. */
245 static inline void eval_arith(
246 func_node_t *arith_node) /*!< in: arithmetic operation node */
247 {
248 que_node_t *arg1;
249 que_node_t *arg2;
250 lint val1;
251 lint val2 = 0; /* remove warning */
252 lint val;
253 int func;
254
255 ut_ad(que_node_get_type(arith_node) == QUE_NODE_FUNC);
256
257 arg1 = arith_node->args;
258 arg2 = que_node_get_next(arg1); /* arg2 is NULL if func is unary '-' */
259
260 val1 = eval_node_get_int_val(arg1);
261
262 if (arg2) {
263 val2 = eval_node_get_int_val(arg2);
264 }
265
266 func = arith_node->func;
267
268 if (func == '+') {
269 val = val1 + val2;
270 } else if ((func == '-') && arg2) {
271 val = val1 - val2;
272 } else if (func == '-') {
273 val = -val1;
274 } else if (func == '*') {
275 val = val1 * val2;
276 } else {
277 ut_ad(func == '/');
278 val = val1 / val2;
279 }
280
281 eval_node_set_int_val(arith_node, val);
282 }
283
284 /** Evaluates an aggregate operation node. */
285 static inline void eval_aggregate(
286 func_node_t *node) /*!< in: aggregate operation node */
287 {
288 que_node_t *arg;
289 lint val;
290 lint arg_val;
291 int func;
292
293 ut_ad(que_node_get_type(node) == QUE_NODE_FUNC);
294
295 val = eval_node_get_int_val(node);
296
297 func = node->func;
298
299 if (func == PARS_COUNT_TOKEN) {
300 val = val + 1;
301 } else {
302 ut_ad(func == PARS_SUM_TOKEN);
303
304 arg = node->args;
305 arg_val = eval_node_get_int_val(arg);
306
307 val = val + arg_val;
308 }
309
310 eval_node_set_int_val(node, val);
311 }
312
313 /** Evaluates a notfound-function node. */
314 static inline void eval_notfound(
315 func_node_t *func_node) /*!< in: function node */
316 {
317 sym_node_t *cursor;
318 sel_node_t *sel_node;
319 bool bool_val;
320
321 ut_ad(func_node->func == PARS_NOTFOUND_TOKEN);
322
323 cursor = static_cast<sym_node_t *>(func_node->args);
324
325 ut_ad(que_node_get_type(cursor) == QUE_NODE_SYMBOL);
326
327 if (cursor->token_type == SYM_LIT) {
328 ut_ad(ut_memcmp(dfield_get_data(que_node_get_val(cursor)), "SQL", 3) == 0);
329
330 sel_node = cursor->sym_table->query_graph->last_sel_node;
331 } else {
332 sel_node = cursor->alias->cursor_def;
333 }
334
335 if (sel_node->state == SEL_NODE_NO_MORE_ROWS) {
336 bool_val = true;
337 } else {
338 bool_val = false;
339 }
340
341 eval_node_set_bool_val(func_node, bool_val);
342 }
343
344 /** Evaluates a substr-function node. */
345 static inline void eval_substr(func_node_t *func_node) /*!< in: function node */
346 {
347 que_node_t *arg1;
348 que_node_t *arg2;
349 que_node_t *arg3;
350 dfield_t *dfield;
351 byte *str1;
352 ulint len1;
353 ulint len2;
354
355 arg1 = func_node->args;
356 arg2 = que_node_get_next(arg1);
357
358 ut_ad(func_node->func == PARS_SUBSTR_TOKEN);
359
360 arg3 = que_node_get_next(arg2);
361
362 str1 = static_cast<byte *>(dfield_get_data(que_node_get_val(arg1)));
363
364 len1 = (ulint)eval_node_get_int_val(arg2);
365 len2 = (ulint)eval_node_get_int_val(arg3);
366
367 dfield = que_node_get_val(func_node);
368
369 dfield_set_data(dfield, str1 + len1, len2);
370 }
371
372 /** Evaluates an instr-function node. */
373 static void eval_instr(func_node_t *func_node) /*!< in: function node */
374 {
375 que_node_t *arg1;
376 que_node_t *arg2;
377 dfield_t *dfield1;
378 dfield_t *dfield2;
379 lint int_val;
380 byte *str1;
381 byte *str2;
382 byte match_char;
383 ulint len1;
384 ulint len2;
385 ulint i;
386 ulint j;
387
388 arg1 = func_node->args;
389 arg2 = que_node_get_next(arg1);
390
391 dfield1 = que_node_get_val(arg1);
392 dfield2 = que_node_get_val(arg2);
393
394 str1 = static_cast<byte *>(dfield_get_data(dfield1));
395 str2 = static_cast<byte *>(dfield_get_data(dfield2));
396
397 len1 = dfield_get_len(dfield1);
398 len2 = dfield_get_len(dfield2);
399
400 if (len2 == 0) {
401 ut_error;
402 }
403
404 match_char = str2[0];
405
406 for (i = 0; i < len1; i++) {
407 /* In this outer loop, the number of matched characters is 0 */
408
409 if (str1[i] == match_char) {
410 if (i + len2 > len1) {
411 break;
412 }
413
414 for (j = 1;; j++) {
415 /* We have already matched j characters */
416
417 if (j == len2) {
418 int_val = i + 1;
419
420 goto match_found;
421 }
422
423 if (str1[i + j] != str2[j]) {
424 break;
425 }
426 }
427 }
428 }
429
430 int_val = 0;
431
432 match_found:
433 eval_node_set_int_val(func_node, int_val);
434 }
435
436 /** Evaluates a predefined function node. */
437 static void eval_concat(func_node_t *func_node) /*!< in: function node */
438 {
439 que_node_t *arg;
440 dfield_t *dfield;
441 byte *data;
442 ulint len;
443 ulint len1;
444
445 arg = func_node->args;
446 len = 0;
447
448 while (arg) {
449 len1 = dfield_get_len(que_node_get_val(arg));
450
451 len += len1;
452
453 arg = que_node_get_next(arg);
454 }
455
456 data = eval_node_ensure_val_buf(func_node, len);
457
458 arg = func_node->args;
459 len = 0;
460
461 while (arg) {
462 dfield = que_node_get_val(arg);
463 len1 = dfield_get_len(dfield);
464
465 ut_memcpy(data + len, dfield_get_data(dfield), len1);
466
467 len += len1;
468
469 arg = que_node_get_next(arg);
470 }
471 }
472
473 /** Evaluates a predefined function node. If the first argument is an integer,
474 this function looks at the second argument which is the integer length in
475 bytes, and converts the integer to a VARCHAR.
476 If the first argument is of some other type, this function converts it to
477 BINARY. */
478 static inline void eval_to_binary(
479 func_node_t *func_node) /*!< in: function node */
480 {
481 que_node_t *arg1;
482 que_node_t *arg2;
483 dfield_t *dfield;
484 byte *str1;
485 ulint len;
486 ulint len1;
487
488 arg1 = func_node->args;
489
490 str1 = static_cast<byte *>(dfield_get_data(que_node_get_val(arg1)));
491
492 if (dtype_get_mtype(que_node_get_data_type(arg1)) != DATA_INT) {
493 len = dfield_get_len(que_node_get_val(arg1));
494
495 dfield = que_node_get_val(func_node);
496
497 dfield_set_data(dfield, str1, len);
498
499 return;
500 }
501
502 arg2 = que_node_get_next(arg1);
503
504 len1 = (ulint)eval_node_get_int_val(arg2);
505
506 if (len1 > 4) {
507 ut_error;
508 }
509
510 dfield = que_node_get_val(func_node);
511
512 dfield_set_data(dfield, str1 + (4 - len1), len1);
513 }
514
515 /** Evaluate the predefined LENGTH function. */
516 static inline void eval_length(func_node_t *func_node) /*!< in: function node */
517 {
518 ut_ad(func_node->func == PARS_LENGTH_TOKEN);
519 eval_node_set_int_val(
520 func_node, (lint)dfield_get_len(que_node_get_val(func_node->args)));
521 }
522
523 /** Evaluates a function node. */
524 void eval_func(func_node_t *func_node) /*!< in: function node */
525 {
526 que_node_t *arg;
527 ulint fclass;
528 ulint func;
529
530 ut_ad(que_node_get_type(func_node) == QUE_NODE_FUNC);
531
532 fclass = func_node->fclass;
533 func = func_node->func;
534
535 arg = func_node->args;
536
537 /* Evaluate first the argument list */
538 while (arg) {
539 eval_exp(arg);
540
541 /* The functions are not defined for SQL null argument
542 values, except for eval_cmp and notfound */
543
544 if (dfield_is_null(que_node_get_val(arg)) && (fclass != PARS_FUNC_CMP) &&
545 (func != PARS_NOTFOUND_TOKEN)) {
546 ut_error;
547 }
548
549 arg = que_node_get_next(arg);
550 }
551
552 switch (fclass) {
553 case PARS_FUNC_CMP:
554 eval_cmp(func_node);
555 return;
556 case PARS_FUNC_ARITH:
557 eval_arith(func_node);
558 return;
559 case PARS_FUNC_AGGREGATE:
560 eval_aggregate(func_node);
561 return;
562 case PARS_FUNC_PREDEFINED:
563 switch (func) {
564 case PARS_NOTFOUND_TOKEN:
565 eval_notfound(func_node);
566 return;
567 case PARS_SUBSTR_TOKEN:
568 eval_substr(func_node);
569 return;
570 case PARS_INSTR_TOKEN:
571 eval_instr(func_node);
572 return;
573 case PARS_CONCAT_TOKEN:
574 eval_concat(func_node);
575 return;
576 case PARS_TO_BINARY_TOKEN:
577 eval_to_binary(func_node);
578 return;
579 case PARS_LENGTH_TOKEN:
580 eval_length(func_node);
581 return;
582 }
583 break;
584 case PARS_FUNC_LOGICAL:
585 eval_logical(func_node);
586 return;
587 }
588
589 ut_error;
590 }