| 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 include/eval0eval.ic |
| 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 "pars0grm.h" |
| 36 |
#include "que0que.h" |
| 37 |
#include "rem0cmp.h" |
| 38 |
|
| 39 |
/** Evaluates a function node. */ |
| 40 |
void eval_func(func_node_t *func_node); /*!< in: function node */ |
| 41 |
/** Allocate a buffer from global dynamic memory for a value of a que_node. |
| 42 |
NOTE that this memory must be explicitly freed when the query graph is |
| 43 |
freed. If the node already has allocated buffer, that buffer is freed |
| 44 |
here. NOTE that this is the only function where dynamic memory should be |
| 45 |
allocated for a query node val field. |
| 46 |
@return pointer to allocated buffer */ |
| 47 |
byte *eval_node_alloc_val_buf( |
| 48 |
que_node_t *node, /*!< in: query graph node; sets the val field |
| 49 |
data field to point to the new buffer, and |
| 50 |
len field equal to size */ |
| 51 |
ulint size); /*!< in: buffer size */ |
| 52 |
|
| 53 |
/** Allocates a new buffer if needed. |
| 54 |
@return pointer to buffer */ |
| 55 |
static inline byte *eval_node_ensure_val_buf( |
| 56 |
que_node_t *node, /*!< in: query graph node; sets the val field |
| 57 |
data field to point to the new buffer, and |
| 58 |
len field equal to size */ |
| 59 |
ulint size) /*!< in: buffer size */ |
| 60 |
{ |
| 61 |
dfield_t *dfield; |
| 62 |
byte *data; |
| 63 |
|
| 64 |
dfield = que_node_get_val(node); |
| 65 |
dfield_set_len(dfield, size); |
| 66 |
|
| 67 |
data = static_cast<byte *>(dfield_get_data(dfield)); |
| 68 |
|
| 69 |
if (!data || que_node_get_val_buf_size(node) < size) { |
| 70 |
data = eval_node_alloc_val_buf(node, size); |
| 71 |
} |
| 72 |
|
| 73 |
return (data); |
| 74 |
} |
| 75 |
|
| 76 |
/** Evaluates a symbol table symbol. */ |
| 77 |
static inline void eval_sym(sym_node_t *sym_node) /*!< in: symbol table node */ |
| 78 |
{ |
| 79 |
ut_ad(que_node_get_type(sym_node) == QUE_NODE_SYMBOL); |
| 80 |
|
| 81 |
if (sym_node->indirection) { |
| 82 |
/* The symbol table node is an alias for a variable or a |
| 83 |
column */ |
| 84 |
|
| 85 |
dfield_copy_data(que_node_get_val(sym_node), |
| 86 |
que_node_get_val(sym_node->indirection)); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
/** Evaluates an expression. */ |
| 91 |
static inline void eval_exp(que_node_t *exp_node) /*!< in: expression */ |
| 92 |
{ |
| 93 |
if (que_node_get_type(exp_node) == QUE_NODE_SYMBOL) { |
| 94 |
eval_sym((sym_node_t *)exp_node); |
| 95 |
|
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
eval_func(static_cast<func_node_t *>(exp_node)); |
| 100 |
} |
| 101 |
|
| 102 |
/** Sets an integer value as the value of an expression node. */ |
| 103 |
static inline void eval_node_set_int_val( |
| 104 |
que_node_t *node, /*!< in: expression node */ |
| 105 |
lint val) /*!< in: value to set */ |
| 106 |
{ |
| 107 |
dfield_t *dfield; |
| 108 |
byte *data; |
| 109 |
|
| 110 |
dfield = que_node_get_val(node); |
| 111 |
|
| 112 |
data = static_cast<byte *>(dfield_get_data(dfield)); |
| 113 |
|
| 114 |
if (data == nullptr) { |
| 115 |
data = eval_node_alloc_val_buf(node, 4); |
| 116 |
} |
| 117 |
|
| 118 |
ut_ad(dfield_get_len(dfield) == 4); |
| 119 |
|
| 120 |
mach_write_to_4(data, (ulint)val); |
| 121 |
} |
| 122 |
|
| 123 |
/** Gets an integer non-SQL null value from an expression node. |
| 124 |
@return integer value */ |
| 125 |
static inline lint eval_node_get_int_val( |
| 126 |
que_node_t *node) /*!< in: expression node */ |
| 127 |
{ |
| 128 |
const byte *ptr; |
| 129 |
dfield_t *dfield; |
| 130 |
|
| 131 |
dfield = que_node_get_val(node); |
| 132 |
ptr = static_cast<byte *>(dfield_get_data(dfield)); |
| 133 |
|
| 134 |
ut_ad(dfield_get_len(dfield) == 4); |
| 135 |
|
| 136 |
return (mach_read_from_4(ptr)); |
| 137 |
} |
| 138 |
|
| 139 |
/** Gets a boolean value from a query node. |
| 140 |
@return boolean value */ |
| 141 |
static inline bool eval_node_get_bool_val( |
| 142 |
que_node_t *node) /*!< in: query graph node */ |
| 143 |
{ |
| 144 |
dfield_t *dfield; |
| 145 |
byte *data; |
| 146 |
|
| 147 |
dfield = que_node_get_val(node); |
| 148 |
|
| 149 |
data = static_cast<byte *>(dfield_get_data(dfield)); |
| 150 |
|
| 151 |
ut_ad(data != nullptr); |
| 152 |
|
| 153 |
return (mach_read_from_1(data)); |
| 154 |
} |
| 155 |
|
| 156 |
/** Sets a boolean value as the value of a function node. */ |
| 157 |
static inline void eval_node_set_bool_val( |
| 158 |
func_node_t *func_node, /*!< in: function node */ |
| 159 |
bool val) /*!< in: value to set */ |
| 160 |
{ |
| 161 |
dfield_t *dfield; |
| 162 |
byte *data; |
| 163 |
|
| 164 |
dfield = que_node_get_val(func_node); |
| 165 |
|
| 166 |
data = static_cast<byte *>(dfield_get_data(dfield)); |
| 167 |
|
| 168 |
if (data == nullptr) { |
| 169 |
/* Allocate 1 byte to hold the value */ |
| 170 |
|
| 171 |
data = eval_node_alloc_val_buf(func_node, 1); |
| 172 |
} |
| 173 |
|
| 174 |
ut_ad(dfield_get_len(dfield) == 1); |
| 175 |
|
| 176 |
mach_write_to_1(data, val); |
| 177 |
} |
| 178 |
|
| 179 |
/** Copies a binary string value as the value of a query graph node. Allocates a |
| 180 |
new buffer if necessary. */ |
| 181 |
static inline void eval_node_copy_and_alloc_val( |
| 182 |
que_node_t *node, /*!< in: query graph node */ |
| 183 |
const byte *str, /*!< in: binary string */ |
| 184 |
ulint len) /*!< in: string length or UNIV_SQL_NULL */ |
| 185 |
{ |
| 186 |
byte *data; |
| 187 |
|
| 188 |
if (len == UNIV_SQL_NULL) { |
| 189 |
dfield_set_len(que_node_get_val(node), len); |
| 190 |
|
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
data = eval_node_ensure_val_buf(node, len); |
| 195 |
|
| 196 |
ut_memcpy(data, str, len); |
| 197 |
} |
| 198 |
|
| 199 |
/** Copies a query node value to another node. */ |
| 200 |
static inline void eval_node_copy_val( |
| 201 |
que_node_t *node1, /*!< in: node to copy to */ |
| 202 |
que_node_t *node2) /*!< in: node to copy from */ |
| 203 |
{ |
| 204 |
dfield_t *dfield2; |
| 205 |
|
| 206 |
dfield2 = que_node_get_val(node2); |
| 207 |
|
| 208 |
eval_node_copy_and_alloc_val(node1, |
| 209 |
static_cast<byte *>(dfield_get_data(dfield2)), |
| 210 |
dfield_get_len(dfield2)); |
| 211 |
} |