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