Debian Codegraph @

storage/innobase/include/data0type.ic

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 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/data0type.ic
29 Data types
30
31 Created 1/16/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include "ha_prototypes.h"
35 #include "mach0data.h"
36
37 /** Gets the MySQL charset-collation code for MySQL string types.
38 @return MySQL charset-collation code */
39 static inline ulint dtype_get_charset_coll(
40 ulint prtype) /*!< in: precise data type */
41 {
42 return ((prtype >> 16) & CHAR_COLL_MASK);
43 }
44
45 #ifndef UNIV_HOTBACKUP
46 /** Determines if a MySQL string type is a subset of UTF-8. This function
47 may return false negatives, in case further character-set collation
48 codes are introduced in MySQL later.
49 @return true if a subset of UTF-8 */
50 static inline bool dtype_is_utf8(ulint prtype) /*!< in: precise data type */
51 {
52 /* These codes have been copied from strings/ctype-extra.cc
53 and strings/ctype-utf8.cc. */
54 switch (dtype_get_charset_coll(prtype)) {
55 case 11: /* ascii_general_ci */
56 case 65: /* ascii_bin */
57 case 33: /* utf8mb3_general_ci */
58 case 83: /* utf8mb3_bin */
59 return true;
60 }
61
62 return false;
63 }
64
65 /** Gets the MySQL type code from a dtype.
66 @return MySQL type code; this is NOT an InnoDB type code! */
67 static inline ulint dtype_get_mysql_type(
68 const dtype_t *type) /*!< in: type struct */
69 {
70 return (type->prtype & 0xFFUL);
71 }
72
73 /** Compute the mbminlen and mbmaxlen members of a data type structure. */
74 static inline void dtype_get_mblen(
75 ulint mtype, /*!< in: main type */
76 ulint prtype, /*!< in: precise type (and collation) */
77 ulint *mbminlen, /*!< out: minimum length of a
78 multi-byte character */
79 ulint *mbmaxlen) /*!< out: maximum length of a
80 multi-byte character */
81 {
82 if (dtype_is_string_type(mtype)) {
83 innobase_get_cset_width(dtype_get_charset_coll(prtype), mbminlen, mbmaxlen);
84 ut_ad(*mbminlen <= *mbmaxlen);
85 ut_ad(*mbminlen < DATA_MBMAX);
86 ut_ad(*mbmaxlen < DATA_MBMAX);
87 } else {
88 *mbminlen = *mbmaxlen = 0;
89 }
90 }
91
92 /** Sets the minimum and maximum length of a character, in bytes. */
93 static inline void dtype_set_mbminmaxlen(
94 dtype_t *type, /*!< in/out: type */
95 ulint mbminlen, /*!< in: minimum length of a char,
96 in bytes, or 0 if this is not
97 a character type */
98 ulint mbmaxlen) /*!< in: maximum length of a char,
99 in bytes, or 0 if this is not
100 a character type */
101 {
102 ut_ad(mbminlen < DATA_MBMAX);
103 ut_ad(mbmaxlen < DATA_MBMAX);
104 ut_ad(mbminlen <= mbmaxlen);
105
106 type->mbminmaxlen = DATA_MBMINMAXLEN(mbminlen, mbmaxlen);
107 }
108
109 /** Compute the mbminlen and mbmaxlen members of a data type structure. */
110 static inline void dtype_set_mblen(dtype_t *type) /*!< in/out: type */
111 {
112 ulint mbminlen;
113 ulint mbmaxlen;
114
115 dtype_get_mblen(type->mtype, type->prtype, &mbminlen, &mbmaxlen);
116 dtype_set_mbminmaxlen(type, mbminlen, mbmaxlen);
117
118 ut_ad(dtype_validate(type));
119 }
120 #else /* !UNIV_HOTBACKUP */
121 #define dtype_set_mblen(type) (void)0
122 #endif /* !UNIV_HOTBACKUP */
123
124 /** Sets a data type structure. */
125 static inline void dtype_set(dtype_t *type, /*!< in: type struct to init */
126 ulint mtype, /*!< in: main data type */
127 ulint prtype, /*!< in: precise type */
128 ulint len) /*!< in: precision of type */
129 {
130 ut_ad(type);
131 ut_ad(mtype <= DATA_MTYPE_MAX);
132
133 type->mtype = mtype;
134 type->prtype = prtype;
135 type->len = len;
136
137 dtype_set_mblen(type);
138 }
139
140 /** Copies a data type structure. */
141 static inline void dtype_copy(
142 dtype_t *type1, /*!< in: type struct to copy to */
143 const dtype_t *type2) /*!< in: type struct to copy from */
144 {
145 *type1 = *type2;
146
147 ut_ad(dtype_validate(type1));
148 }
149
150 /** Gets the SQL main data type.
151 @return SQL main data type */
152 static inline ulint dtype_get_mtype(const dtype_t *type) /*!< in: data type */
153 {
154 ut_ad(type);
155
156 return (type->mtype);
157 }
158
159 /** Gets the precise data type.
160 @return precise data type */
161 static inline ulint dtype_get_prtype(const dtype_t *type) /*!< in: data type */
162 {
163 ut_ad(type);
164
165 return (type->prtype);
166 }
167
168 /** Gets the type length.
169 @return fixed length of the type, in bytes, or 0 if variable-length */
170 static inline ulint dtype_get_len(const dtype_t *type) /*!< in: data type */
171 {
172 ut_ad(type);
173
174 return (type->len);
175 }
176
177 #ifndef UNIV_HOTBACKUP
178 /** Gets the minimum length of a character, in bytes.
179 @return minimum length of a char, in bytes, or 0 if this is not a
180 character type */
181 static inline ulint dtype_get_mbminlen(const dtype_t *type) /*!< in: type */
182 {
183 ut_ad(type);
184 return (DATA_MBMINLEN(type->mbminmaxlen));
185 }
186 /** Gets the maximum length of a character, in bytes.
187 @return maximum length of a char, in bytes, or 0 if this is not a
188 character type */
189 static inline ulint dtype_get_mbmaxlen(const dtype_t *type) /*!< in: type */
190 {
191 ut_ad(type);
192 return (DATA_MBMAXLEN(type->mbminmaxlen));
193 }
194
195 /** Stores for a type the information which determines its alphabetical ordering
196 and the storage size of an SQL NULL value. This is the >= 4.1.x storage
197 format. */
198 static inline void dtype_new_store_for_order_and_null_size(
199 byte *buf, /*!< in: buffer for
200 DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE
201 bytes where we store the info */
202 const dtype_t *type, /*!< in: type struct */
203 ulint prefix_len) /*!< in: prefix length to
204 replace type->len, or 0 */
205 {
206 static_assert(DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE == 6,
207 "6 != DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE");
208 ulint len;
209
210 ut_ad(type);
211 ut_ad(type->mtype >= DATA_VARCHAR);
212 ut_ad(type->mtype <= DATA_MTYPE_MAX);
213
214 buf[0] = (byte)(type->mtype & 0xFFUL);
215
216 if (type->prtype & DATA_BINARY_TYPE) {
217 buf[0] |= 128;
218 }
219
220 /* In versions < 4.1.2 we had: if (type->prtype & DATA_NONLATIN1) {
221 buf[0] |= 64;
222 }
223 */
224 if (type->prtype & DATA_MULTI_VALUE) {
225 buf[0] |= 64;
226 }
227
228 buf[1] = (byte)(type->prtype & 0xFFUL);
229
230 len = prefix_len ? prefix_len : type->len;
231
232 mach_write_to_2(buf + 2, len & 0xFFFFUL);
233
234 ut_ad(dtype_get_charset_coll(type->prtype) <= MAX_CHAR_COLL_NUM);
235 mach_write_to_2(buf + 4, dtype_get_charset_coll(type->prtype));
236
237 if (type->prtype & DATA_NOT_NULL) {
238 buf[4] |= 128;
239 }
240 }
241
242 /** Reads to a type the stored information which determines its alphabetical
243 ordering and the storage size of an SQL NULL value. This is the < 4.1.x
244 storage format. */
245 static inline void dtype_read_for_order_and_null_size(
246 dtype_t *type, /*!< in: type struct */
247 const byte *buf) /*!< in: buffer for stored type order info */
248 {
249 static_assert(DATA_ORDER_NULL_TYPE_BUF_SIZE == 4,
250 "4 != DATA_ORDER_NULL_TYPE_BUF_SIZE");
251
252 type->mtype = buf[0] & 63;
253 type->prtype = buf[1];
254
255 if (buf[0] & 128) {
256 type->prtype |= DATA_BINARY_TYPE;
257 }
258
259 type->len = mach_read_from_2(buf + 2);
260
261 type->prtype =
262 dtype_form_prtype(type->prtype, data_mysql_default_charset_coll);
263 dtype_set_mblen(type);
264 }
265
266 /** Reads to a type the stored information which determines its alphabetical
267 ordering and the storage size of an SQL NULL value. This is the >= 4.1.x
268 storage format. */
269 static inline void dtype_new_read_for_order_and_null_size(
270 dtype_t *type, /*!< in: type struct */
271 const byte *buf) /*!< in: buffer for stored type order info */
272 {
273 ulint charset_coll;
274
275 static_assert(DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE == 6,
276 "6 != DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE");
277
278 type->mtype = buf[0] & 63;
279 type->prtype = buf[1];
280
281 if (buf[0] & 128) {
282 type->prtype |= DATA_BINARY_TYPE;
283 }
284
285 if (buf[0] & 64) {
286 type->prtype |= DATA_MULTI_VALUE;
287 }
288
289 if (buf[4] & 128) {
290 type->prtype |= DATA_NOT_NULL;
291 }
292
293 type->len = mach_read_from_2(buf + 2);
294
295 charset_coll = mach_read_from_2(buf + 4) & CHAR_COLL_MASK;
296
297 if (dtype_is_string_type(type->mtype)) {
298 ut_a(charset_coll <= MAX_CHAR_COLL_NUM);
299
300 if (charset_coll == 0) {
301 /* This insert buffer record was inserted with MySQL
302 version < 4.1.2, and the charset-collation code was not
303 explicitly stored to dtype->prtype at that time. It
304 must be the default charset-collation of this MySQL
305 installation. */
306
307 charset_coll = data_mysql_default_charset_coll;
308 }
309
310 type->prtype = dtype_form_prtype(type->prtype, charset_coll);
311 }
312 dtype_set_mblen(type);
313 }
314
315 /** Returns the type's SQL name (e.g. BIGINT UNSIGNED) from mtype,prtype,len
316 @return the SQL type name */
317 static inline char *dtype_sql_name(
318 unsigned mtype, /*!< in: mtype */
319 unsigned prtype, /*!< in: prtype */
320 unsigned len, /*!< in: len */
321 char *name, /*!< out: SQL name */
322 unsigned name_sz) /*!< in: size of the name buffer */
323 {
324 #define APPEND_UNSIGNED() \
325 do { \
326 if (prtype & DATA_UNSIGNED) { \
327 snprintf(name + strlen(name), name_sz - strlen(name), " UNSIGNED"); \
328 } \
329 } while (0)
330
331 snprintf(name, name_sz, "UNKNOWN");
332
333 switch (mtype) {
334 case DATA_INT:
335 switch (len) {
336 case 1:
337 snprintf(name, name_sz, "TINYINT");
338 break;
339 case 2:
340 snprintf(name, name_sz, "SMALLINT");
341 break;
342 case 3:
343 snprintf(name, name_sz, "MEDIUMINT");
344 break;
345 case 4:
346 snprintf(name, name_sz, "INT");
347 break;
348 case 8:
349 snprintf(name, name_sz, "BIGINT");
350 break;
351 }
352 APPEND_UNSIGNED();
353 break;
354 case DATA_FLOAT:
355 snprintf(name, name_sz, "FLOAT");
356 APPEND_UNSIGNED();
357 break;
358 case DATA_DOUBLE:
359 snprintf(name, name_sz, "DOUBLE");
360 APPEND_UNSIGNED();
361 break;
362 case DATA_FIXBINARY:
363 snprintf(name, name_sz, "BINARY(%u)", len);
364 break;
365 case DATA_CHAR:
366 case DATA_MYSQL:
367 snprintf(name, name_sz, "CHAR(%u)", len);
368 break;
369 case DATA_VARCHAR:
370 case DATA_VARMYSQL:
371 snprintf(name, name_sz, "VARCHAR(%u)", len);
372 break;
373 case DATA_BINARY:
374 snprintf(name, name_sz, "VARBINARY(%u)", len);
375 break;
376 case DATA_GEOMETRY:
377 snprintf(name, name_sz, "GEOMETRY");
378 break;
379 case DATA_BLOB:
380 switch (len) {
381 case 9:
382 snprintf(name, name_sz, "TINYBLOB");
383 break;
384 case 10:
385 snprintf(name, name_sz, "BLOB");
386 break;
387 case 11:
388 snprintf(name, name_sz, "MEDIUMBLOB");
389 break;
390 case 12:
391 snprintf(name, name_sz, "LONGBLOB");
392 break;
393 }
394 }
395
396 if (prtype & DATA_NOT_NULL) {
397 snprintf(name + strlen(name), name_sz - strlen(name), " NOT NULL");
398 }
399
400 return (name);
401 }
402
403 #endif /* !UNIV_HOTBACKUP */
404
405 /** Returns the size of a fixed size data type, 0 if not a fixed size type.
406 @return fixed size, or 0 */
407 static inline ulint dtype_get_fixed_size_low(
408 ulint mtype, /*!< in: main type */
409 ulint prtype, /*!< in: precise type */
410 ulint len, /*!< in: length */
411 ulint mbminmaxlen, /*!< in: minimum and maximum length of
412 a multibyte character, in bytes */
413 bool comp) /*!< in: nonzero=ROW_FORMAT=COMPACT */
414 {
415 switch (mtype) {
416 case DATA_SYS:
417 #ifdef UNIV_DEBUG
418 switch (prtype & DATA_MYSQL_TYPE_MASK) {
419 case DATA_ROW_ID:
420 ut_ad(len == DATA_ROW_ID_LEN);
421 break;
422 case DATA_TRX_ID:
423 ut_ad(len == DATA_TRX_ID_LEN);
424 break;
425 case DATA_ROLL_PTR:
426 ut_ad(len == DATA_ROLL_PTR_LEN);
427 break;
428 default:
429 ut_d(ut_error);
430 ut_o(return (0));
431 }
432 [[fallthrough]];
433 #endif /* UNIV_DEBUG */
434 case DATA_CHAR:
435 case DATA_FIXBINARY:
436 case DATA_INT:
437 case DATA_FLOAT:
438 case DATA_DOUBLE:
439 case DATA_POINT:
440 return (len);
441 case DATA_MYSQL:
442 #ifndef UNIV_LIBRARY
443 if (prtype & DATA_BINARY_TYPE) {
444 return (len);
445 } else if (!comp) {
446 return (len);
447 } else {
448 #ifndef UNIV_HOTBACKUP
449 #ifdef UNIV_DEBUG
450 ulint i_mbminlen, i_mbmaxlen;
451
452 innobase_get_cset_width(dtype_get_charset_coll(prtype), &i_mbminlen,
453 &i_mbmaxlen);
454
455 ut_ad(DATA_MBMINMAXLEN(i_mbminlen, i_mbmaxlen) == mbminmaxlen);
456 #endif /* UNIV_DEBUG */
457 #endif /* !UNIV_HOTBACKUP */
458 if (DATA_MBMINLEN(mbminmaxlen) == DATA_MBMAXLEN(mbminmaxlen)) {
459 return (len);
460 }
461 }
462 #else /* !UNIV_LIBRARY */
463 return (len);
464 #endif /* !UNIV_LIBRARY */
465 /* fall through for variable-length charsets */
466 [[fallthrough]];
467 case DATA_VARCHAR:
468 case DATA_BINARY:
469 case DATA_DECIMAL:
470 case DATA_VARMYSQL:
471 case DATA_VAR_POINT:
472 case DATA_GEOMETRY:
473 case DATA_BLOB:
474 return (0);
475 default:
476 ut_error;
477 }
478 }
479
480 /** Returns the minimum size of a data type.
481 @return minimum size */
482 static inline ulint dtype_get_min_size_low(
483 ulint mtype, /*!< in: main type */
484 ulint prtype, /*!< in: precise type */
485 ulint len, /*!< in: length */
486 ulint mbminmaxlen) /*!< in: minimum and maximum length of a
487 multi-byte character */
488 {
489 switch (mtype) {
490 case DATA_SYS:
491 #ifdef UNIV_DEBUG
492 switch (prtype & DATA_MYSQL_TYPE_MASK) {
493 case DATA_ROW_ID:
494 ut_ad(len == DATA_ROW_ID_LEN);
495 break;
496 case DATA_TRX_ID:
497 ut_ad(len == DATA_TRX_ID_LEN);
498 break;
499 case DATA_ROLL_PTR:
500 ut_ad(len == DATA_ROLL_PTR_LEN);
501 break;
502 default:
503 ut_d(ut_error);
504 ut_o(return (0));
505 }
506 #endif /* UNIV_DEBUG */
507 [[fallthrough]];
508 case DATA_CHAR:
509 case DATA_FIXBINARY:
510 case DATA_INT:
511 case DATA_FLOAT:
512 case DATA_DOUBLE:
513 case DATA_POINT:
514 return (len);
515 case DATA_MYSQL:
516 if (prtype & DATA_BINARY_TYPE) {
517 return (len);
518 } else {
519 ulint mbminlen = DATA_MBMINLEN(mbminmaxlen);
520 ulint mbmaxlen = DATA_MBMAXLEN(mbminmaxlen);
521
522 if (mbminlen == mbmaxlen) {
523 return (len);
524 }
525
526 /* this is a variable-length character set */
527 ut_a(mbminlen > 0);
528 ut_a(mbmaxlen > mbminlen);
529 ut_a(len % mbmaxlen == 0);
530 return (len * mbminlen / mbmaxlen);
531 }
532 case DATA_VARCHAR:
533 case DATA_BINARY:
534 case DATA_DECIMAL:
535 case DATA_VARMYSQL:
536 case DATA_VAR_POINT:
537 case DATA_GEOMETRY:
538 case DATA_BLOB:
539 return (0);
540 default:
541 ut_error;
542 }
543 }
544
545 /** Returns the maximum size of a data type. Note: types in system tables may
546 be incomplete and return incorrect information.
547 @param[in] mtype main type
548 @param[in] len length
549 @return maximum size */
550 static inline ulint dtype_get_max_size_low(ulint mtype, /*!< in: main type */
551 ulint len) /*!< in: length */
552 {
553 switch (mtype) {
554 case DATA_SYS:
555 case DATA_CHAR:
556 case DATA_FIXBINARY:
557 case DATA_INT:
558 case DATA_FLOAT:
559 case DATA_DOUBLE:
560 case DATA_MYSQL:
561 case DATA_VARCHAR:
562 case DATA_BINARY:
563 case DATA_DECIMAL:
564 case DATA_VARMYSQL:
565 case DATA_POINT:
566 return (len);
567 case DATA_VAR_POINT:
568 case DATA_GEOMETRY:
569 case DATA_BLOB:
570 break;
571 default:
572 ut_error;
573 }
574 return (ULINT_MAX);
575 }
576
577 /** Returns the ROW_FORMAT=REDUNDANT stored SQL NULL size of a type.
578 For fixed length types it is the fixed length of the type, otherwise 0.
579 @return SQL null storage size in ROW_FORMAT=REDUNDANT */
580 static inline ulint dtype_get_sql_null_size(
581 const dtype_t *type, /*!< in: type */
582 bool comp) /*!< in: nonzero=ROW_FORMAT=COMPACT */
583 {
584 #ifndef UNIV_HOTBACKUP
585 return (dtype_get_fixed_size_low(type->mtype, type->prtype, type->len,
586 type->mbminmaxlen, comp));
587 #else /* !UNIV_HOTBACKUP */
588 return (dtype_get_fixed_size_low(type->mtype, type->prtype, type->len, 0, 0));
589 #endif /* !UNIV_HOTBACKUP */
590 }