MySQL 8.4.0
Source Code Documentation
create_field.h
Go to the documentation of this file.
1#ifndef SQL_CREATE_FIELD_INCLUDED
2#define SQL_CREATE_FIELD_INCLUDED
3
4/* Copyright (c) 2018, 2024, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License, version 2.0, for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
26
27#include <optional>
28
29#include "field_types.h"
30#include "lex_string.h"
31#include "my_alloc.h"
32#include "my_base.h"
34#include "sql/dd/types/column.h"
35#include "sql/field.h"
36#include "sql/gis/srid.h"
37#include "sql/sql_list.h"
38#include "typelib.h"
39
40class Item;
41class String;
42class Value_generator;
43
44/// Create_field is a description a field/column that may or may not exists in
45/// a table.
46///
47/// The main usage of Create_field is to contain the description of a column
48/// given by the user (usually given with CREATE TABLE). It is also used to
49/// describe changes to be carried out on a column (usually given with ALTER
50/// TABLE ... CHANGE COLUMN).
52 public:
53 /// @returns the maximum display width of this column, in number of.
54 /// code points. See m_max_display_width_in_codepoints for an
55 /// explanation of "display width" and "code point".
57
58 /// @returns the maximum display width of this column, in number of bytes. For
59 /// numeric types, temporal types, YEAR and BIT, this method returns
60 /// the same as max_display_width_in_codepoints(). For character
61 /// types (CHAR, VARCHAR, TEXT etc) the returned value depends on
62 /// max_display_width_in_codepoints() and the character set of this
63 /// column.
64 size_t max_display_width_in_bytes() const;
65
66 /// @returns the pack length for this column, which is the number of bytes
67 /// needed to store this column in memory. Note that blob returns
68 /// a length variable + the size of a pointer to an external memory
69 /// location where the actual data is stored. So LONGBLOB would
70 /// return 4 bytes for the length variable + 8 bytes for the pointer
71 /// to the data (12 bytes in total).
72 /// @param dont_override Don't use pack_length_override even if non-zero
73 /// Used by multi-valued index, where pack_length
74 /// and key_length aren't the same.
75 size_t pack_length(bool dont_override = false) const;
76
77 /// @returns the key length for this column.
78 size_t key_length() const;
79
80 /// @retval true if the maximum column length was given explicitly by the
81 /// user.
82 /// @retval false if the user didn't specify any maximum length.
84
85 /// Set the maximum display width based on another Create_field.
87 const Create_field &create_field) {
90 }
91
93
94 const char *field_name;
95 /**
96 Name of column modified by ALTER TABLE's CHANGE/MODIFY COLUMN clauses,
97 NULL for columns added.
98 */
99 const char *change;
100 const char *after{nullptr}; // Put column after this one
101 LEX_CSTRING comment; // Comment for field
102
103 /**
104 The declared default value, if any, otherwise NULL. Note that this member
105 is NULL if the default is a function. If the column definition has a
106 function declared as the default, the information is found in
107 Create_field::auto_flags.
108
109 @see Create_field::auto_flags
110 */
114 uint flags{0};
115 /**
116 Bitmap of flags indicating if field value should be auto-generated
117 by default and/or on update, and in which way.
118
119 @sa Field::enum_auto_flags for possible options.
120 */
122 TYPELIB *interval; // Which interval to use
123 // Used only for UCS2 intervals
126 bool is_explicit_collation; // User exeplicitly provided charset ?
128 Field *field; // For alter table
129
130 uint offset;
131
132 /**
133 Indicate whether column is nullable, zerofill or unsigned.
134
135 Initialized based on flags and other members at prepare_create_field()/
136 init_for_tmp_table() stage.
137 */
141
142 /**
143 Indicates that storage engine doesn't support optimized BIT field
144 storage.
145
146 @note We also use safe/non-optimized version of BIT field for
147 special cases like virtual temporary tables.
148
149 Initialized at mysql_prepare_create_table()/sp_prepare_create_field()/
150 init_for_tmp_table() stage.
151 */
153
154 /**
155 Row based replication code sometimes needs to create ENUM and SET
156 fields with pack length which doesn't correspond to number of
157 elements in interval TYPELIB.
158
159 When this member is non-zero ENUM/SET field to be created will use
160 its value as pack length instead of one calculated from number
161 elements in its interval.
162
163 Initialized at prepare_create_field()/init_for_tmp_table() stage.
164 */
166
167 /* Generated column expression information */
169 /*
170 Indication that the field is phycically stored in tables
171 rather than just generated on SQL queries.
172 As of now, false can only be set for virtual generated columns.
173 */
175
176 /// Holds the expression to be used to generate default values.
178 std::optional<gis::srid_t> m_srid;
179
180 // Whether the field is actually an array of the field's type;
181 bool is_array{false};
182
185
187 : after(nullptr),
189 geom_type(Field::GEOM_GEOMETRY),
190 is_nullable(false),
191 is_zerofill(false),
192 is_unsigned(false),
193 /*
194 Initialize treat_bit_as_char for all field types even if
195 it is only used for MYSQL_TYPE_BIT. This avoids bogus
196 valgrind warnings in optimized builds.
197 */
198 treat_bit_as_char(false),
200 stored_in_db(false),
202 Create_field(Field *field, Field *orig_field);
203
204 /* Used to make a clone of this object for ALTER/CREATE TABLE */
206 return new (mem_root) Create_field(*this);
207 }
208 bool is_gcol() const { return gcol_info; }
209 bool is_virtual_gcol() const {
211 }
212
213 /* Init for a tmp table field. To be extended if need be. */
214 void init_for_tmp_table(enum_field_types sql_type_arg, uint32 max_length,
217 const char *field_name = "");
218
219 bool init(THD *thd, const char *field_name, enum_field_types type,
220 const char *length, const char *decimals, uint type_modifier,
221 Item *default_value, Item *on_update_value,
222 const LEX_CSTRING *comment, const char *change,
224 bool has_explicit_collation, uint uint_geom_type,
225 Value_generator *gcol_info, Value_generator *default_val_expr,
226 std::optional<gis::srid_t> srid,
228
231 }
232
235 }
236
237 private:
238 /// The maximum display width of this column.
239 ///
240 /// The "display width" is the number of code points that is needed to print
241 /// out the string representation of a value. It can be given by the user
242 /// both explicitly and implicitly. If a user creates a table with the columns
243 /// "a VARCHAR(3), b INT(3)", both columns are given an explicit display width
244 /// of 3 code points. But if a user creates a table with the columns
245 /// "a INT, b TINYINT UNSIGNED", the first column has an implicit display
246 /// width of 11 (-2147483648 is the longest value for a signed int) and the
247 /// second column has an implicit display width of 3 (255 is the longest value
248 /// for an unsigned tinyint).
249 /// This is related to storage size for some types (VARCHAR, BLOB etc), but
250 /// not for all types (an INT is four bytes regardless of the display width).
251 ///
252 /// A "code point" is basically a numeric value. For instance, ASCII
253 /// compromises of 128 code points (0x00 to 0x7F), while unicode contains way
254 /// more. In most cases a code point represents a single graphical unit (aka
255 /// grapheme), but not always. For instance, É may consists of two code points
256 /// where one is the letter E and the other one is the quotation mark above
257 /// the letter.
259
260 /// Whether or not the display width was given explicitly by the user.
262};
263
264/// @returns whether or not this field is a hidden column that represents a
265/// functional index.
266bool is_field_for_functional_index(const Create_field *create_field);
267
268/**
269 @retval true If this column is hidden either in the storage engine
270 or SQL layer. Either way, it is completely hidden from
271 the user.
272 @retval false Otherwise.
273*/
274bool is_hidden_by_system(const Create_field *create_field);
275
276/**
277 @retval true If this column is hidden by the user.
278 @retval false otherwise.
279*/
280bool is_hidden_by_user(const Create_field *create_field);
281#endif
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
Create_field is a description a field/column that may or may not exists in a table.
Definition: create_field.h:51
const char * change
Name of column modified by ALTER TABLE's CHANGE/MODIFY COLUMN clauses, NULL for columns added.
Definition: create_field.h:99
bool treat_bit_as_char
Indicates that storage engine doesn't support optimized BIT field storage.
Definition: create_field.h:152
bool stored_in_db
Definition: create_field.h:174
size_t max_display_width_in_codepoints() const
Definition: create_field.cc:638
TYPELIB * interval
Definition: create_field.h:122
ha_storage_media field_storage_type() const
Definition: create_field.h:229
uint decimals
Definition: create_field.h:113
Value_generator * gcol_info
Definition: create_field.h:168
Value_generator * m_default_val_expr
Holds the expression to be used to generate default values.
Definition: create_field.h:177
bool is_zerofill
Definition: create_field.h:139
dd::Column::enum_hidden_type hidden
Definition: create_field.h:92
uint pack_length_override
Row based replication code sometimes needs to create ENUM and SET fields with pack length which doesn...
Definition: create_field.h:165
uint flags
Definition: create_field.h:114
bool is_explicit_collation
Definition: create_field.h:126
size_t key_length() const
Definition: create_field.cc:758
const CHARSET_INFO * charset
Definition: create_field.h:125
const char * field_name
Definition: create_field.h:94
bool is_virtual_gcol() const
Definition: create_field.h:209
List< String > interval_list
Definition: create_field.h:124
LEX_CSTRING m_engine_attribute
Definition: create_field.h:183
bool is_unsigned
Definition: create_field.h:140
bool explicit_display_width() const
Definition: create_field.h:83
Create_field()
Definition: create_field.h:186
uint offset
Definition: create_field.h:130
bool is_gcol() const
Definition: create_field.h:208
const char * after
Definition: create_field.h:100
void init_for_tmp_table(enum_field_types sql_type_arg, uint32 max_length, uint32 decimals, bool is_nullable, bool is_unsigned, uint pack_length_override, const char *field_name="")
Init for a tmp table field.
Definition: create_field.cc:581
Field::geometry_type geom_type
Definition: create_field.h:127
uchar auto_flags
Bitmap of flags indicating if field value should be auto-generated by default and/or on update,...
Definition: create_field.h:121
size_t pack_length(bool dont_override=false) const
Definition: create_field.cc:726
column_format_type column_format() const
Definition: create_field.h:233
Create_field * clone(MEM_ROOT *mem_root) const
Definition: create_field.h:205
size_t max_display_width_in_bytes() const
Definition: create_field.cc:685
bool init(THD *thd, const char *field_name, enum_field_types type, const char *length, const char *decimals, uint type_modifier, Item *default_value, Item *on_update_value, const LEX_CSTRING *comment, const char *change, List< String > *interval_list, const CHARSET_INFO *cs, bool has_explicit_collation, uint uint_geom_type, Value_generator *gcol_info, Value_generator *default_val_expr, std::optional< gis::srid_t > srid, dd::Column::enum_hidden_type hidden, bool is_array=false)
Initialize a column definition object.
Definition: create_field.cc:197
bool is_array
Definition: create_field.h:181
bool m_explicit_display_width
Whether or not the display width was given explicitly by the user.
Definition: create_field.h:261
size_t m_max_display_width_in_codepoints
The maximum display width of this column.
Definition: create_field.h:258
Field * field
Definition: create_field.h:128
bool is_nullable
Indicate whether column is nullable, zerofill or unsigned.
Definition: create_field.h:138
Item * constant_default
The declared default value, if any, otherwise NULL.
Definition: create_field.h:111
LEX_CSTRING comment
Definition: create_field.h:101
std::optional< gis::srid_t > m_srid
Definition: create_field.h:178
enum_field_types sql_type
Definition: create_field.h:112
LEX_CSTRING m_secondary_engine_attribute
Definition: create_field.h:184
void set_max_display_width_from_create_field(const Create_field &create_field)
Set the maximum display width based on another Create_field.
Definition: create_field.h:86
Definition: field.h:575
@ NONE
Definition: field.h:711
geometry_type
Definition: field.h:718
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:934
Definition: sql_list.h:467
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Used for storing information associated with generated column, default values generated from expressi...
Definition: field.h:483
bool get_field_stored() const
Definition: field.h:535
enum_hidden_type
Definition: column.h:95
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
bool is_field_for_functional_index(const Create_field *create_field)
Definition: create_field.cc:788
bool is_hidden_by_user(const Create_field *create_field)
Definition: create_field.cc:797
bool is_hidden_by_system(const Create_field *create_field)
Definition: create_field.cc:792
column_format_type
Definition: field.h:190
This file contains the field type.
enum_field_types
Column types for MySQL Note: Keep include/mysql/components/services/bits/stored_program_bits....
Definition: field_types.h:55
#define FIELD_FLAGS_COLUMN_FORMAT
Field column format, bit 24-25.
Definition: mysql_com.h:187
#define FIELD_FLAGS_STORAGE_MEDIA
Field storage media, bit 22-23.
Definition: mysql_com.h:185
constexpr const LEX_CSTRING EMPTY_CSTR
Definition: lex_string.h:48
A better implementation of the UNIX ctype(3) library.
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
This file includes constants used by all storage engines.
ha_storage_media
Definition: my_base.h:116
unsigned char uchar
Definition: my_inttypes.h:52
uint32_t uint32
Definition: my_inttypes.h:67
Definition: commit_order_queue.h:34
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
required string type
Definition: replication_group_member_actions.proto:34
Definition: m_ctype.h:423
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: mysql_lex_string.h:40
Definition: typelib.h:35