1
/*****************************************************************************
3
Copyright (c) 1996, 2009, Innobase Oy. All Rights Reserved.
5
This program is free software; you can redistribute it and/or modify it under
6
the terms of the GNU General Public License as published by the Free Software
7
Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful, but WITHOUT
10
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License along with
14
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
Place, Suite 330, Boston, MA 02111-1307 USA
17
*****************************************************************************/
19
/**************************************************//**
20
@file include/data0type.h
23
Created 1/16/1996 Heikki Tuuri
24
*******************************************************/
31
extern ulint data_mysql_default_charset_coll;
32
#define DATA_MYSQL_LATIN1_SWEDISH_CHARSET_COLL 8
33
#define DATA_MYSQL_BINARY_CHARSET_COLL 63
35
/* SQL data type struct */
36
typedef struct dtype_struct dtype_t;
38
/*-------------------------------------------*/
39
/* The 'MAIN TYPE' of a column */
40
#define DATA_VARCHAR 1 /* character varying of the
41
latin1_swedish_ci charset-collation; note
42
that the MySQL format for this, DATA_BINARY,
43
DATA_VARMYSQL, is also affected by whether the
44
'precise type' contains
45
DATA_MYSQL_TRUE_VARCHAR */
46
#define DATA_CHAR 2 /* fixed length character of the
47
latin1_swedish_ci charset-collation */
48
#define DATA_FIXBINARY 3 /* binary string of fixed length */
49
#define DATA_BINARY 4 /* binary string */
50
#define DATA_BLOB 5 /* binary large object, or a TEXT type;
51
if prtype & DATA_BINARY_TYPE == 0, then this is
52
actually a TEXT column (or a BLOB created
53
with < 4.0.14; since column prefix indexes
54
came only in 4.0.14, the missing flag in BLOBs
55
created before that does not cause any harm) */
56
#define DATA_INT 6 /* integer: can be any size 1 - 8 bytes */
57
#define DATA_SYS_CHILD 7 /* address of the child page in node pointer */
58
#define DATA_SYS 8 /* system column */
60
/* Data types >= DATA_FLOAT must be compared using the whole field, not as
64
#define DATA_DOUBLE 10
65
#define DATA_DECIMAL 11 /* decimal number stored as an ASCII string */
66
#define DATA_VARMYSQL 12 /* any charset varying length char */
67
#define DATA_MYSQL 13 /* any charset fixed length char */
68
/* NOTE that 4.1.1 used DATA_MYSQL and
69
DATA_VARMYSQL for all character sets, and the
70
charset-collation for tables created with it
71
can also be latin1_swedish_ci */
72
#define DATA_MTYPE_MAX 63 /* dtype_store_for_order_and_null_size()
73
requires the values are <= 63 */
74
/*-------------------------------------------*/
75
/* The 'PRECISE TYPE' of a column */
77
Tables created by a MySQL user have the following convention:
79
- In the least significant byte in the precise type we store the MySQL type
80
code (not applicable for system columns).
82
- In the second least significant byte we OR flags DATA_NOT_NULL,
83
DATA_UNSIGNED, DATA_BINARY_TYPE.
85
- In the third least significant byte of the precise type of string types we
86
store the MySQL charset-collation code. In DATA_BLOB columns created with
87
< 4.0.14 we do not actually know if it is a BLOB or a TEXT column. Since there
88
are no indexes on prefixes of BLOB or TEXT columns in < 4.0.14, this is no
91
Note that versions < 4.1.2 or < 5.0.1 did not store the charset code to the
92
precise type, since the charset was always the default charset of the MySQL
93
installation. If the stored charset code is 0 in the system table SYS_COLUMNS
94
of InnoDB, that means that the default charset of this MySQL installation
97
When loading a table definition from the system tables to the InnoDB data
98
dictionary cache in main memory, InnoDB versions >= 4.1.2 and >= 5.0.1 check
99
if the stored charset-collation is 0, and if that is the case and the type is
100
a non-binary string, replace that 0 by the default charset-collation code of
101
this MySQL installation. In short, in old tables, the charset-collation code
102
in the system tables on disk can be 0, but in in-memory data structures
103
(dtype_t), the charset-collation code is always != 0 for non-binary string
106
In new tables, in binary string types, the charset-collation code is the
107
MySQL code for the 'binary charset', that is, != 0.
109
For binary string types and for DATA_CHAR, DATA_VARCHAR, and for those
110
DATA_BLOB which are binary or have the charset-collation latin1_swedish_ci,
111
InnoDB performs all comparisons internally, without resorting to the MySQL
112
comparison functions. This is to save CPU time.
114
InnoDB's own internal system tables have different precise types for their
115
columns, and for them the precise type is usually not used at all.
118
#define DATA_ENGLISH 4 /* English language character string: this
119
is a relic from pre-MySQL time and only used
120
for InnoDB's own system tables */
121
#define DATA_ERROR 111 /* another relic from pre-MySQL time */
123
#define DATA_MYSQL_TYPE_MASK 255 /* AND with this mask to extract the MySQL
124
type from the precise type */
125
#define DATA_MYSQL_TRUE_VARCHAR 15 /* MySQL type code for the >= 5.0.3
126
format true VARCHAR */
128
/* Precise data types for system columns and the length of those columns;
129
NOTE: the values must run from 0 up in the order given! All codes must
131
#define DATA_ROW_ID 0 /* row id: a dulint */
132
#define DATA_ROW_ID_LEN 6 /* stored length for row id */
134
#define DATA_TRX_ID 1 /* transaction id: 6 bytes */
135
#define DATA_TRX_ID_LEN 6
137
#define DATA_ROLL_PTR 2 /* rollback data pointer: 7 bytes */
138
#define DATA_ROLL_PTR_LEN 7
140
#define DATA_N_SYS_COLS 3 /* number of system columns defined above */
142
#define DATA_SYS_PRTYPE_MASK 0xF /* mask to extract the above from prtype */
144
/* Flags ORed to the precise data type */
145
#define DATA_NOT_NULL 256 /* this is ORed to the precise type when
146
the column is declared as NOT NULL */
147
#define DATA_UNSIGNED 512 /* this id ORed to the precise type when
148
we have an unsigned integer type */
149
#define DATA_BINARY_TYPE 1024 /* if the data type is a binary character
150
string, this is ORed to the precise type:
151
this only holds for tables created with
153
/* #define DATA_NONLATIN1 2048 This is a relic from < 4.1.2 and < 5.0.1.
154
In earlier versions this was set for some
157
#define DATA_LONG_TRUE_VARCHAR 4096 /* this is ORed to the precise data
158
type when the column is true VARCHAR where
159
MySQL uses 2 bytes to store the data len;
160
for shorter VARCHARs MySQL uses only 1 byte */
161
/*-------------------------------------------*/
163
/* This many bytes we need to store the type information affecting the
164
alphabetical order for a single field and decide the storage size of an
166
#define DATA_ORDER_NULL_TYPE_BUF_SIZE 4
167
/* In the >= 4.1.x storage format we add 2 bytes more so that we can also
168
store the charset-collation number; one byte is left unused, though */
169
#define DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE 6
171
#ifndef UNIV_HOTBACKUP
172
/*********************************************************************//**
173
Gets the MySQL type code from a dtype.
174
@return MySQL type code; this is NOT an InnoDB type code! */
177
dtype_get_mysql_type(
178
/*=================*/
179
const dtype_t* type); /*!< in: type struct */
180
/*********************************************************************//**
181
Determine how many bytes the first n characters of the given string occupy.
182
If the string is shorter than n characters, returns the number of bytes
183
the characters in the string occupy.
184
@return length of the prefix, in bytes */
187
dtype_get_at_most_n_mbchars(
188
/*========================*/
189
ulint prtype, /*!< in: precise type */
190
ulint mbminlen, /*!< in: minimum length of a
191
multi-byte character */
192
ulint mbmaxlen, /*!< in: maximum length of a
193
multi-byte character */
194
ulint prefix_len, /*!< in: length of the requested
195
prefix, in characters, multiplied by
196
dtype_get_mbmaxlen(dtype) */
197
ulint data_len, /*!< in: length of str (in bytes) */
198
const char* str); /*!< in: the string whose prefix
199
length is being determined */
200
#endif /* !UNIV_HOTBACKUP */
201
/*********************************************************************//**
202
Checks if a data main type is a string type. Also a BLOB is considered a
204
@return TRUE if string type */
207
dtype_is_string_type(
208
/*=================*/
209
ulint mtype); /*!< in: InnoDB main data type code: DATA_CHAR, ... */
210
/*********************************************************************//**
211
Checks if a type is a binary string type. Note that for tables created with
212
< 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column. For
213
those DATA_BLOB columns this function currently returns FALSE.
214
@return TRUE if binary string type */
217
dtype_is_binary_string_type(
218
/*========================*/
219
ulint mtype, /*!< in: main data type */
220
ulint prtype);/*!< in: precise type */
221
/*********************************************************************//**
222
Checks if a type is a non-binary string type. That is, dtype_is_string_type is
223
TRUE and dtype_is_binary_string_type is FALSE. Note that for tables created
224
with < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column.
225
For those DATA_BLOB columns this function currently returns TRUE.
226
@return TRUE if non-binary string type */
229
dtype_is_non_binary_string_type(
230
/*============================*/
231
ulint mtype, /*!< in: main data type */
232
ulint prtype);/*!< in: precise type */
233
/*********************************************************************//**
234
Sets a data type structure. */
239
dtype_t* type, /*!< in: type struct to init */
240
ulint mtype, /*!< in: main data type */
241
ulint prtype, /*!< in: precise type */
242
ulint len); /*!< in: precision of type */
243
/*********************************************************************//**
244
Copies a data type structure. */
249
dtype_t* type1, /*!< in: type struct to copy to */
250
const dtype_t* type2); /*!< in: type struct to copy from */
251
/*********************************************************************//**
252
Gets the SQL main data type.
253
@return SQL main data type */
258
const dtype_t* type); /*!< in: data type */
259
/*********************************************************************//**
260
Gets the precise data type.
261
@return precise data type */
266
const dtype_t* type); /*!< in: data type */
267
#ifndef UNIV_HOTBACKUP
268
/*********************************************************************//**
269
Compute the mbminlen and mbmaxlen members of a data type structure. */
274
ulint mtype, /*!< in: main type */
275
ulint prtype, /*!< in: precise type (and collation) */
276
ulint* mbminlen, /*!< out: minimum length of a
277
multi-byte character */
278
ulint* mbmaxlen); /*!< out: maximum length of a
279
multi-byte character */
280
/*********************************************************************//**
281
Gets the MySQL charset-collation code for MySQL string types.
282
@return MySQL charset-collation code */
285
dtype_get_charset_coll(
286
/*===================*/
287
ulint prtype);/*!< in: precise data type */
288
/*********************************************************************//**
289
Forms a precise type from the < 4.1.2 format precise type plus the
290
charset-collation code.
291
@return precise type, including the charset-collation code */
296
ulint old_prtype, /*!< in: the MySQL type code and the flags
297
DATA_BINARY_TYPE etc. */
298
ulint charset_coll); /*!< in: MySQL charset-collation code */
299
/*********************************************************************//**
300
Determines if a MySQL string type is a subset of UTF-8. This function
301
may return false negatives, in case further character-set collation
302
codes are introduced in MySQL later.
303
@return TRUE if a subset of UTF-8 */
308
ulint prtype);/*!< in: precise data type */
309
#endif /* !UNIV_HOTBACKUP */
310
/*********************************************************************//**
311
Gets the type length.
312
@return fixed length of the type, in bytes, or 0 if variable-length */
317
const dtype_t* type); /*!< in: data type */
318
#ifndef UNIV_HOTBACKUP
319
/*********************************************************************//**
320
Gets the minimum length of a character, in bytes.
321
@return minimum length of a char, in bytes, or 0 if this is not a
327
const dtype_t* type); /*!< in: type */
328
/*********************************************************************//**
329
Gets the maximum length of a character, in bytes.
330
@return maximum length of a char, in bytes, or 0 if this is not a
336
const dtype_t* type); /*!< in: type */
337
/*********************************************************************//**
338
Gets the padding character code for the type.
339
@return padding character code, or ULINT_UNDEFINED if no padding specified */
344
ulint mtype, /*!< in: main type */
345
ulint prtype); /*!< in: precise type */
346
#endif /* !UNIV_HOTBACKUP */
347
/***********************************************************************//**
348
Returns the size of a fixed size data type, 0 if not a fixed size type.
349
@return fixed size, or 0 */
352
dtype_get_fixed_size_low(
353
/*=====================*/
354
ulint mtype, /*!< in: main type */
355
ulint prtype, /*!< in: precise type */
356
ulint len, /*!< in: length */
357
ulint mbminlen, /*!< in: minimum length of a multibyte char */
358
ulint mbmaxlen, /*!< in: maximum length of a multibyte char */
359
ulint comp); /*!< in: nonzero=ROW_FORMAT=COMPACT */
360
#ifndef UNIV_HOTBACKUP
361
/***********************************************************************//**
362
Returns the minimum size of a data type.
363
@return minimum size */
366
dtype_get_min_size_low(
367
/*===================*/
368
ulint mtype, /*!< in: main type */
369
ulint prtype, /*!< in: precise type */
370
ulint len, /*!< in: length */
371
ulint mbminlen, /*!< in: minimum length of a multibyte char */
372
ulint mbmaxlen); /*!< in: maximum length of a multibyte char */
373
/***********************************************************************//**
374
Returns the maximum size of a data type. Note: types in system tables may be
375
incomplete and return incorrect information.
376
@return maximum size */
379
dtype_get_max_size_low(
380
/*===================*/
381
ulint mtype, /*!< in: main type */
382
ulint len); /*!< in: length */
383
#endif /* !UNIV_HOTBACKUP */
384
/***********************************************************************//**
385
Returns the ROW_FORMAT=REDUNDANT stored SQL NULL size of a type.
386
For fixed length types it is the fixed length of the type, otherwise 0.
387
@return SQL null storage size in ROW_FORMAT=REDUNDANT */
390
dtype_get_sql_null_size(
391
/*====================*/
392
const dtype_t* type, /*!< in: type */
393
ulint comp); /*!< in: nonzero=ROW_FORMAT=COMPACT */
394
#ifndef UNIV_HOTBACKUP
395
/**********************************************************************//**
396
Reads to a type the stored information which determines its alphabetical
397
ordering and the storage size of an SQL NULL value. */
400
dtype_read_for_order_and_null_size(
401
/*===============================*/
402
dtype_t* type, /*!< in: type struct */
403
const byte* buf); /*!< in: buffer for the stored order info */
404
/**********************************************************************//**
405
Stores for a type the information which determines its alphabetical ordering
406
and the storage size of an SQL NULL value. This is the >= 4.1.x storage
410
dtype_new_store_for_order_and_null_size(
411
/*====================================*/
412
byte* buf, /*!< in: buffer for
413
DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE
414
bytes where we store the info */
415
const dtype_t* type, /*!< in: type struct */
416
ulint prefix_len);/*!< in: prefix length to
417
replace type->len, or 0 */
418
/**********************************************************************//**
419
Reads to a type the stored information which determines its alphabetical
420
ordering and the storage size of an SQL NULL value. This is the 4.1.x storage
424
dtype_new_read_for_order_and_null_size(
425
/*===================================*/
426
dtype_t* type, /*!< in: type struct */
427
const byte* buf); /*!< in: buffer for stored type order info */
428
#endif /* !UNIV_HOTBACKUP */
430
/*********************************************************************//**
431
Validates a data type structure.
432
@return TRUE if ok */
437
const dtype_t* type); /*!< in: type struct to validate */
438
/*********************************************************************//**
439
Prints a data type structure. */
444
const dtype_t* type); /*!< in: type */
446
/* Structure for an SQL data type.
447
If you add fields to this structure, be sure to initialize them everywhere.
448
This structure is initialized in the following functions:
450
dtype_read_for_order_and_null_size()
451
dtype_new_read_for_order_and_null_size()
452
sym_tab_add_null_lit() */
455
unsigned mtype:8; /*!< main data type */
456
unsigned prtype:24; /*!< precise type; MySQL data
457
type, charset code, flags to
458
indicate nullability,
459
signedness, whether this is a
460
binary string, whether this is
461
a true VARCHAR where MySQL
462
uses 2 bytes to store the length */
464
/* the remaining fields do not affect alphabetical ordering: */
466
unsigned len:16; /*!< length; for MySQL data this
467
is field->pack_length(),
468
except that for a >= 5.0.3
469
type true VARCHAR this is the
470
maximum byte length of the
471
string data (in addition to
472
the string, MySQL uses 1 or 2
473
bytes to store the string length) */
474
#ifndef UNIV_HOTBACKUP
475
unsigned mbminlen:2; /*!< minimum length of a
476
character, in bytes */
477
unsigned mbmaxlen:3; /*!< maximum length of a
478
character, in bytes */
479
#endif /* !UNIV_HOTBACKUP */
483
#include "data0type.ic"