1 /*****************************************************************************
2 
3 Copyright (c) 1996, 2012, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2.0,
7 as published by the Free Software Foundation.
8 
9 This program is also distributed 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 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License, version 2.0, for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24 
25 *****************************************************************************/
26 
27 /**************************************************//**
28 @file include/data0type.h
29 Data types
30 
31 Created 1/16/1996 Heikki Tuuri
32 *******************************************************/
33 
34 #ifndef data0type_h
35 #define data0type_h
36 
37 #include "univ.i"
38 
39 extern ulint	data_mysql_default_charset_coll;
40 #define DATA_MYSQL_LATIN1_SWEDISH_CHARSET_COLL 8
41 #define DATA_MYSQL_BINARY_CHARSET_COLL 63
42 
43 /* SQL data type struct */
44 struct dtype_t;
45 
46 /* SQL Like operator comparison types */
47 enum ib_like_t {
48 	IB_LIKE_EXACT,                  /* e.g.  STRING */
49 	IB_LIKE_PREFIX,                 /* e.g., STRING% */
50 	IB_LIKE_SUFFIX,                 /* e.g., %STRING */
51 	IB_LIKE_SUBSTR,                 /* e.g., %STRING% */
52 	IB_LIKE_REGEXP                  /* Future */
53 };
54 
55 /*-------------------------------------------*/
56 /* The 'MAIN TYPE' of a column */
57 #define DATA_MISSING	0	/* missing column */
58 #define	DATA_VARCHAR	1	/* character varying of the
59 				latin1_swedish_ci charset-collation; note
60 				that the MySQL format for this, DATA_BINARY,
61 				DATA_VARMYSQL, is also affected by whether the
62 				'precise type' contains
63 				DATA_MYSQL_TRUE_VARCHAR */
64 #define DATA_CHAR	2	/* fixed length character of the
65 				latin1_swedish_ci charset-collation */
66 #define DATA_FIXBINARY	3	/* binary string of fixed length */
67 #define DATA_BINARY	4	/* binary string */
68 #define DATA_BLOB	5	/* binary large object, or a TEXT type;
69 				if prtype & DATA_BINARY_TYPE == 0, then this is
70 				actually a TEXT column (or a BLOB created
71 				with < 4.0.14; since column prefix indexes
72 				came only in 4.0.14, the missing flag in BLOBs
73 				created before that does not cause any harm) */
74 #define	DATA_INT	6	/* integer: can be any size 1 - 8 bytes */
75 #define	DATA_SYS_CHILD	7	/* address of the child page in node pointer */
76 #define	DATA_SYS	8	/* system column */
77 
78 /* Data types >= DATA_FLOAT must be compared using the whole field, not as
79 binary strings */
80 
81 #define DATA_FLOAT	9
82 #define DATA_DOUBLE	10
83 #define DATA_DECIMAL	11	/* decimal number stored as an ASCII string */
84 #define	DATA_VARMYSQL	12	/* any charset varying length char */
85 #define	DATA_MYSQL	13	/* any charset fixed length char */
86 				/* NOTE that 4.1.1 used DATA_MYSQL and
87 				DATA_VARMYSQL for all character sets, and the
88 				charset-collation for tables created with it
89 				can also be latin1_swedish_ci */
90 #define DATA_MTYPE_MAX	63	/* dtype_store_for_order_and_null_size()
91 				requires the values are <= 63 */
92 /*-------------------------------------------*/
93 /* The 'PRECISE TYPE' of a column */
94 /*
95 Tables created by a MySQL user have the following convention:
96 
97 - In the least significant byte in the precise type we store the MySQL type
98 code (not applicable for system columns).
99 
100 - In the second least significant byte we OR flags DATA_NOT_NULL,
101 DATA_UNSIGNED, DATA_BINARY_TYPE.
102 
103 - In the third least significant byte of the precise type of string types we
104 store the MySQL charset-collation code. In DATA_BLOB columns created with
105 < 4.0.14 we do not actually know if it is a BLOB or a TEXT column. Since there
106 are no indexes on prefixes of BLOB or TEXT columns in < 4.0.14, this is no
107 problem, though.
108 
109 Note that versions < 4.1.2 or < 5.0.1 did not store the charset code to the
110 precise type, since the charset was always the default charset of the MySQL
111 installation. If the stored charset code is 0 in the system table SYS_COLUMNS
112 of InnoDB, that means that the default charset of this MySQL installation
113 should be used.
114 
115 When loading a table definition from the system tables to the InnoDB data
116 dictionary cache in main memory, InnoDB versions >= 4.1.2 and >= 5.0.1 check
117 if the stored charset-collation is 0, and if that is the case and the type is
118 a non-binary string, replace that 0 by the default charset-collation code of
119 this MySQL installation. In short, in old tables, the charset-collation code
120 in the system tables on disk can be 0, but in in-memory data structures
121 (dtype_t), the charset-collation code is always != 0 for non-binary string
122 types.
123 
124 In new tables, in binary string types, the charset-collation code is the
125 MySQL code for the 'binary charset', that is, != 0.
126 
127 For binary string types and for DATA_CHAR, DATA_VARCHAR, and for those
128 DATA_BLOB which are binary or have the charset-collation latin1_swedish_ci,
129 InnoDB performs all comparisons internally, without resorting to the MySQL
130 comparison functions. This is to save CPU time.
131 
132 InnoDB's own internal system tables have different precise types for their
133 columns, and for them the precise type is usually not used at all.
134 */
135 
136 #define DATA_ENGLISH	4	/* English language character string: this
137 				is a relic from pre-MySQL time and only used
138 				for InnoDB's own system tables */
139 #define DATA_ERROR	111	/* another relic from pre-MySQL time */
140 
141 #define DATA_MYSQL_TYPE_MASK 255 /* AND with this mask to extract the MySQL
142 				 type from the precise type */
143 #define DATA_MYSQL_TRUE_VARCHAR 15 /* MySQL type code for the >= 5.0.3
144 				   format true VARCHAR */
145 
146 /* Precise data types for system columns and the length of those columns;
147 NOTE: the values must run from 0 up in the order given! All codes must
148 be less than 256 */
149 #define	DATA_ROW_ID	0	/* row id: a 48-bit integer */
150 #define DATA_ROW_ID_LEN	6	/* stored length for row id */
151 
152 #define DATA_TRX_ID	1	/* transaction id: 6 bytes */
153 #define DATA_TRX_ID_LEN	6
154 
155 #define	DATA_ROLL_PTR	2	/* rollback data pointer: 7 bytes */
156 #define DATA_ROLL_PTR_LEN 7
157 
158 #define	DATA_N_SYS_COLS 3	/* number of system columns defined above */
159 
160 #define DATA_FTS_DOC_ID	3	/* Used as FTS DOC ID column */
161 
162 #define DATA_SYS_PRTYPE_MASK 0xF /* mask to extract the above from prtype */
163 
164 /* Flags ORed to the precise data type */
165 #define DATA_NOT_NULL	256	/* this is ORed to the precise type when
166 				the column is declared as NOT NULL */
167 #define DATA_UNSIGNED	512	/* this id ORed to the precise type when
168 				we have an unsigned integer type */
169 #define	DATA_BINARY_TYPE 1024	/* if the data type is a binary character
170 				string, this is ORed to the precise type:
171 				this only holds for tables created with
172 				>= MySQL-4.0.14 */
173 /* #define	DATA_NONLATIN1	2048 This is a relic from < 4.1.2 and < 5.0.1.
174 				In earlier versions this was set for some
175 				BLOB columns.
176 */
177 #define	DATA_LONG_TRUE_VARCHAR 4096	/* this is ORed to the precise data
178 				type when the column is true VARCHAR where
179 				MySQL uses 2 bytes to store the data len;
180 				for shorter VARCHARs MySQL uses only 1 byte */
181 /*-------------------------------------------*/
182 
183 /* This many bytes we need to store the type information affecting the
184 alphabetical order for a single field and decide the storage size of an
185 SQL null*/
186 #define DATA_ORDER_NULL_TYPE_BUF_SIZE		4
187 /* In the >= 4.1.x storage format we add 2 bytes more so that we can also
188 store the charset-collation number; one byte is left unused, though */
189 #define DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE	6
190 
191 /* Maximum multi-byte character length in bytes, plus 1 */
192 #define DATA_MBMAX	5
193 
194 /* Pack mbminlen, mbmaxlen to mbminmaxlen. */
195 #define DATA_MBMINMAXLEN(mbminlen, mbmaxlen)	\
196 	((mbmaxlen) * DATA_MBMAX + (mbminlen))
197 /* Get mbminlen from mbminmaxlen. Cast the result of UNIV_EXPECT to ulint
198 because in GCC it returns a long. */
199 #define DATA_MBMINLEN(mbminmaxlen) ((ulint) \
200                                     UNIV_EXPECT(((mbminmaxlen) % DATA_MBMAX), \
201                                                 1))
202 /* Get mbmaxlen from mbminmaxlen. */
203 #define DATA_MBMAXLEN(mbminmaxlen) ((ulint) ((mbminmaxlen) / DATA_MBMAX))
204 
205 /* We now support 15 bits (up to 32767) collation number */
206 #define MAX_CHAR_COLL_NUM	32767
207 
208 /* Mask to get the Charset Collation number (0x7fff) */
209 #define CHAR_COLL_MASK		MAX_CHAR_COLL_NUM
210 
211 #ifndef UNIV_HOTBACKUP
212 /*********************************************************************//**
213 Gets the MySQL type code from a dtype.
214 @return	MySQL type code; this is NOT an InnoDB type code! */
215 UNIV_INLINE
216 ulint
217 dtype_get_mysql_type(
218 /*=================*/
219 	const dtype_t*	type);	/*!< in: type struct */
220 /*********************************************************************//**
221 Determine how many bytes the first n characters of the given string occupy.
222 If the string is shorter than n characters, returns the number of bytes
223 the characters in the string occupy.
224 @return	length of the prefix, in bytes */
225 UNIV_INTERN
226 ulint
227 dtype_get_at_most_n_mbchars(
228 /*========================*/
229 	ulint		prtype,		/*!< in: precise type */
230 	ulint		mbminmaxlen,	/*!< in: minimum and maximum length of
231 					a multi-byte character */
232 	ulint		prefix_len,	/*!< in: length of the requested
233 					prefix, in characters, multiplied by
234 					dtype_get_mbmaxlen(dtype) */
235 	ulint		data_len,	/*!< in: length of str (in bytes) */
236 	const char*	str);		/*!< in: the string whose prefix
237 					length is being determined */
238 #endif /* !UNIV_HOTBACKUP */
239 /*********************************************************************//**
240 Checks if a data main type is a string type. Also a BLOB is considered a
241 string type.
242 @return	TRUE if string type */
243 UNIV_INTERN
244 ibool
245 dtype_is_string_type(
246 /*=================*/
247 	ulint	mtype);	/*!< in: InnoDB main data type code: DATA_CHAR, ... */
248 /*********************************************************************//**
249 Checks if a type is a binary string type. Note that for tables created with
250 < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column. For
251 those DATA_BLOB columns this function currently returns FALSE.
252 @return	TRUE if binary string type */
253 UNIV_INTERN
254 ibool
255 dtype_is_binary_string_type(
256 /*========================*/
257 	ulint	mtype,	/*!< in: main data type */
258 	ulint	prtype);/*!< in: precise type */
259 /*********************************************************************//**
260 Checks if a type is a non-binary string type. That is, dtype_is_string_type is
261 TRUE and dtype_is_binary_string_type is FALSE. Note that for tables created
262 with < 4.0.14, we do not know if a DATA_BLOB column is a BLOB or a TEXT column.
263 For those DATA_BLOB columns this function currently returns TRUE.
264 @return	TRUE if non-binary string type */
265 UNIV_INTERN
266 ibool
267 dtype_is_non_binary_string_type(
268 /*============================*/
269 	ulint	mtype,	/*!< in: main data type */
270 	ulint	prtype);/*!< in: precise type */
271 /*********************************************************************//**
272 Sets a data type structure. */
273 UNIV_INLINE
274 void
275 dtype_set(
276 /*======*/
277 	dtype_t*	type,	/*!< in: type struct to init */
278 	ulint		mtype,	/*!< in: main data type */
279 	ulint		prtype,	/*!< in: precise type */
280 	ulint		len);	/*!< in: precision of type */
281 /*********************************************************************//**
282 Copies a data type structure. */
283 UNIV_INLINE
284 void
285 dtype_copy(
286 /*=======*/
287 	dtype_t*	type1,	/*!< in: type struct to copy to */
288 	const dtype_t*	type2);	/*!< in: type struct to copy from */
289 /*********************************************************************//**
290 Gets the SQL main data type.
291 @return	SQL main data type */
292 UNIV_INLINE
293 ulint
294 dtype_get_mtype(
295 /*============*/
296 	const dtype_t*	type);	/*!< in: data type */
297 /*********************************************************************//**
298 Gets the precise data type.
299 @return	precise data type */
300 UNIV_INLINE
301 ulint
302 dtype_get_prtype(
303 /*=============*/
304 	const dtype_t*	type);	/*!< in: data type */
305 #ifndef UNIV_HOTBACKUP
306 /*********************************************************************//**
307 Compute the mbminlen and mbmaxlen members of a data type structure. */
308 UNIV_INLINE
309 void
310 dtype_get_mblen(
311 /*============*/
312 	ulint	mtype,		/*!< in: main type */
313 	ulint	prtype,		/*!< in: precise type (and collation) */
314 	ulint*	mbminlen,	/*!< out: minimum length of a
315 				multi-byte character */
316 	ulint*	mbmaxlen);	/*!< out: maximum length of a
317 				multi-byte character */
318 /*********************************************************************//**
319 Gets the MySQL charset-collation code for MySQL string types.
320 @return	MySQL charset-collation code */
321 UNIV_INLINE
322 ulint
323 dtype_get_charset_coll(
324 /*===================*/
325 	ulint	prtype);/*!< in: precise data type */
326 /*********************************************************************//**
327 Forms a precise type from the < 4.1.2 format precise type plus the
328 charset-collation code.
329 @return precise type, including the charset-collation code */
330 UNIV_INTERN
331 ulint
332 dtype_form_prtype(
333 /*==============*/
334 	ulint	old_prtype,	/*!< in: the MySQL type code and the flags
335 				DATA_BINARY_TYPE etc. */
336 	ulint	charset_coll);	/*!< in: MySQL charset-collation code */
337 /*********************************************************************//**
338 Determines if a MySQL string type is a subset of UTF-8.  This function
339 may return false negatives, in case further character-set collation
340 codes are introduced in MySQL later.
341 @return	TRUE if a subset of UTF-8 */
342 UNIV_INLINE
343 ibool
344 dtype_is_utf8(
345 /*==========*/
346 	ulint	prtype);/*!< in: precise data type */
347 #endif /* !UNIV_HOTBACKUP */
348 /*********************************************************************//**
349 Gets the type length.
350 @return	fixed length of the type, in bytes, or 0 if variable-length */
351 UNIV_INLINE
352 ulint
353 dtype_get_len(
354 /*==========*/
355 	const dtype_t*	type);	/*!< in: data type */
356 #ifndef UNIV_HOTBACKUP
357 /*********************************************************************//**
358 Gets the minimum length of a character, in bytes.
359 @return minimum length of a char, in bytes, or 0 if this is not a
360 character type */
361 UNIV_INLINE
362 ulint
363 dtype_get_mbminlen(
364 /*===============*/
365 	const dtype_t*	type);	/*!< in: type */
366 /*********************************************************************//**
367 Gets the maximum length of a character, in bytes.
368 @return maximum length of a char, in bytes, or 0 if this is not a
369 character type */
370 UNIV_INLINE
371 ulint
372 dtype_get_mbmaxlen(
373 /*===============*/
374 	const dtype_t*	type);	/*!< in: type */
375 /*********************************************************************//**
376 Sets the minimum and maximum length of a character, in bytes. */
377 UNIV_INLINE
378 void
379 dtype_set_mbminmaxlen(
380 /*==================*/
381 	dtype_t*	type,		/*!< in/out: type */
382 	ulint		mbminlen,	/*!< in: minimum length of a char,
383 					in bytes, or 0 if this is not
384 					a character type */
385 	ulint		mbmaxlen);	/*!< in: maximum length of a char,
386 					in bytes, or 0 if this is not
387 					a character type */
388 /*********************************************************************//**
389 Gets the padding character code for the type.
390 @return	padding character code, or ULINT_UNDEFINED if no padding specified */
391 UNIV_INLINE
392 ulint
393 dtype_get_pad_char(
394 /*===============*/
395 	ulint	mtype,		/*!< in: main type */
396 	ulint	prtype);	/*!< in: precise type */
397 #endif /* !UNIV_HOTBACKUP */
398 /***********************************************************************//**
399 Returns the size of a fixed size data type, 0 if not a fixed size type.
400 @return	fixed size, or 0 */
401 UNIV_INLINE
402 ulint
403 dtype_get_fixed_size_low(
404 /*=====================*/
405 	ulint	mtype,		/*!< in: main type */
406 	ulint	prtype,		/*!< in: precise type */
407 	ulint	len,		/*!< in: length */
408 	ulint	mbminmaxlen,	/*!< in: minimum and maximum length of a
409 				multibyte character, in bytes */
410 	ulint	comp);		/*!< in: nonzero=ROW_FORMAT=COMPACT  */
411 #ifndef UNIV_HOTBACKUP
412 /***********************************************************************//**
413 Returns the minimum size of a data type.
414 @return	minimum size */
415 UNIV_INLINE
416 ulint
417 dtype_get_min_size_low(
418 /*===================*/
419 	ulint	mtype,		/*!< in: main type */
420 	ulint	prtype,		/*!< in: precise type */
421 	ulint	len,		/*!< in: length */
422 	ulint	mbminmaxlen);	/*!< in: minimum and maximum length of a
423 				multibyte character */
424 /***********************************************************************//**
425 Returns the maximum size of a data type. Note: types in system tables may be
426 incomplete and return incorrect information.
427 @return	maximum size */
428 UNIV_INLINE
429 ulint
430 dtype_get_max_size_low(
431 /*===================*/
432 	ulint	mtype,		/*!< in: main type */
433 	ulint	len);		/*!< in: length */
434 #endif /* !UNIV_HOTBACKUP */
435 /***********************************************************************//**
436 Returns the ROW_FORMAT=REDUNDANT stored SQL NULL size of a type.
437 For fixed length types it is the fixed length of the type, otherwise 0.
438 @return	SQL null storage size in ROW_FORMAT=REDUNDANT */
439 UNIV_INLINE
440 ulint
441 dtype_get_sql_null_size(
442 /*====================*/
443 	const dtype_t*	type,	/*!< in: type */
444 	ulint		comp);	/*!< in: nonzero=ROW_FORMAT=COMPACT  */
445 #ifndef UNIV_HOTBACKUP
446 /**********************************************************************//**
447 Reads to a type the stored information which determines its alphabetical
448 ordering and the storage size of an SQL NULL value. */
449 UNIV_INLINE
450 void
451 dtype_read_for_order_and_null_size(
452 /*===============================*/
453 	dtype_t*	type,	/*!< in: type struct */
454 	const byte*	buf);	/*!< in: buffer for the stored order info */
455 /**********************************************************************//**
456 Stores for a type the information which determines its alphabetical ordering
457 and the storage size of an SQL NULL value. This is the >= 4.1.x storage
458 format. */
459 UNIV_INLINE
460 void
461 dtype_new_store_for_order_and_null_size(
462 /*====================================*/
463 	byte*		buf,	/*!< in: buffer for
464 				DATA_NEW_ORDER_NULL_TYPE_BUF_SIZE
465 				bytes where we store the info */
466 	const dtype_t*	type,	/*!< in: type struct */
467 	ulint		prefix_len);/*!< in: prefix length to
468 				replace type->len, or 0 */
469 /**********************************************************************//**
470 Reads to a type the stored information which determines its alphabetical
471 ordering and the storage size of an SQL NULL value. This is the 4.1.x storage
472 format. */
473 UNIV_INLINE
474 void
475 dtype_new_read_for_order_and_null_size(
476 /*===================================*/
477 	dtype_t*	type,	/*!< in: type struct */
478 	const byte*	buf);	/*!< in: buffer for stored type order info */
479 
480 /*********************************************************************//**
481 Returns the type's SQL name (e.g. BIGINT UNSIGNED) from mtype,prtype,len
482 @return the SQL type name */
483 UNIV_INLINE
484 char*
485 dtype_sql_name(
486 /*===========*/
487 	unsigned	mtype,	/*!< in: mtype */
488 	unsigned	prtype,	/*!< in: prtype */
489 	unsigned	len,	/*!< in: len */
490 	char*		name,	/*!< out: SQL name */
491 	unsigned	name_sz);/*!< in: size of the name buffer */
492 
493 #endif /* !UNIV_HOTBACKUP */
494 
495 /*********************************************************************//**
496 Validates a data type structure.
497 @return	TRUE if ok */
498 UNIV_INTERN
499 ibool
500 dtype_validate(
501 /*===========*/
502 	const dtype_t*	type);	/*!< in: type struct to validate */
503 /*********************************************************************//**
504 Prints a data type structure. */
505 UNIV_INTERN
506 void
507 dtype_print(
508 /*========*/
509 	const dtype_t*	type);	/*!< in: type */
510 
511 /* Structure for an SQL data type.
512 If you add fields to this structure, be sure to initialize them everywhere.
513 This structure is initialized in the following functions:
514 dtype_set()
515 dtype_read_for_order_and_null_size()
516 dtype_new_read_for_order_and_null_size()
517 sym_tab_add_null_lit() */
518 
519 struct dtype_t{
520 	unsigned	prtype:32;	/*!< precise type; MySQL data
521 					type, charset code, flags to
522 					indicate nullability,
523 					signedness, whether this is a
524 					binary string, whether this is
525 					a true VARCHAR where MySQL
526 					uses 2 bytes to store the length */
527 	unsigned	mtype:8;	/*!< main data type */
528 
529 	/* the remaining fields do not affect alphabetical ordering: */
530 
531 	unsigned	len:16;		/*!< length; for MySQL data this
532 					is field->pack_length(),
533 					except that for a >= 5.0.3
534 					type true VARCHAR this is the
535 					maximum byte length of the
536 					string data (in addition to
537 					the string, MySQL uses 1 or 2
538 					bytes to store the string length) */
539 #ifndef UNIV_HOTBACKUP
540 	unsigned	mbminmaxlen:5;	/*!< minimum and maximum length of a
541 					character, in bytes;
542 					DATA_MBMINMAXLEN(mbminlen,mbmaxlen);
543 					mbminlen=DATA_MBMINLEN(mbminmaxlen);
544 					mbmaxlen=DATA_MBMINLEN(mbminmaxlen) */
545 #endif /* !UNIV_HOTBACKUP */
546 };
547 
548 #ifndef UNIV_NONINL
549 #include "data0type.ic"
550 #endif
551 
552 #endif
553