| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2012, Facebook Inc. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 8 |
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, but WITHOUT |
| 19 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 21 |
for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License along with |
| 24 |
this program; if not, write to the Free Software Foundation, Inc., |
| 25 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
|
| 27 |
*****************************************************************************/ |
| 28 |
|
| 29 |
/** @file dict/dict.cc |
| 30 |
Data dictionary system |
| 31 |
|
| 32 |
Created 1/8/1996 Heikki Tuuri |
| 33 |
***********************************************************************/ |
| 34 |
|
| 35 |
/** NOTE: The functions in this file should only use functions from |
| 36 |
other files in library. The code in this file is used to make a library for |
| 37 |
external tools. */ |
| 38 |
|
| 39 |
#include <stddef.h> |
| 40 |
|
| 41 |
#include "dict0dict.h" |
| 42 |
#include "dict0mem.h" |
| 43 |
|
| 44 |
/** Adds a column to index. |
| 45 |
@param[in,out] index index |
| 46 |
@param[in] table table |
| 47 |
@param[in] col column |
| 48 |
@param[in] prefix_len column prefix length |
| 49 |
@param[in] is_ascending true=ASC, false=DESC */ |
| 50 |
void dict_index_add_col(dict_index_t *index, const dict_table_t *table, |
| 51 |
dict_col_t *col, ulint prefix_len, bool is_ascending) { |
| 52 |
dict_field_t *field; |
| 53 |
const char *col_name = nullptr; |
| 54 |
|
| 55 |
#ifndef UNIV_LIBRARY |
| 56 |
if (col->is_virtual()) { |
| 57 |
#ifndef UNIV_HOTBACKUP |
| 58 |
dict_v_col_t *v_col = reinterpret_cast<dict_v_col_t *>(col); |
| 59 |
|
| 60 |
/* When v_col->v_indexes==NULL, |
| 61 |
ha_innobase::commit_inplace_alter_table(commit=true) |
| 62 |
will evict and reload the table definition, and |
| 63 |
v_col->v_indexes will not be NULL for the new table. */ |
| 64 |
if (v_col->v_indexes != nullptr) { |
| 65 |
/* Register the index with the virtual column index |
| 66 |
list */ |
| 67 |
struct dict_v_idx_t new_idx = {index, index->n_def}; |
| 68 |
|
| 69 |
v_col->v_indexes->push_back(new_idx); |
| 70 |
} |
| 71 |
|
| 72 |
col_name = dict_table_get_v_col_name_mysql(table, dict_col_get_no(col)); |
| 73 |
#else /* !UNIV_HOTBACKUP */ |
| 74 |
/* PRELIMINARY TEMPORARY WORKAROUND: is this ever used? */ |
| 75 |
bool not_hotbackup = false; |
| 76 |
ut_a(not_hotbackup); |
| 77 |
#endif /* !UNIV_HOTBACKUP */ |
| 78 |
} else |
| 79 |
#endif /* !UNIV_LIBRARY */ |
| 80 |
{ |
| 81 |
col_name = table->get_col_name(dict_col_get_no(col)); |
| 82 |
} |
| 83 |
|
| 84 |
index->add_field(col_name, prefix_len, is_ascending); |
| 85 |
|
| 86 |
field = index->get_field(index->n_def - 1); |
| 87 |
|
| 88 |
field->col = col; |
| 89 |
/* DATA_POINT is a special type, whose fixed_len should be: |
| 90 |
1) DATA_MBR_LEN, when it's indexed in R-TREE. In this case, |
| 91 |
it must be the first col to be added. |
| 92 |
2) DATA_POINT_LEN(be equal to fixed size of column), when it's |
| 93 |
indexed in B-TREE, |
| 94 |
3) DATA_POINT_LEN, if a POINT col is the PRIMARY KEY, and we are |
| 95 |
adding the PK col to other B-TREE/R-TREE. */ |
| 96 |
/* TODO: We suppose the dimension is 2 now. */ |
| 97 |
/* NOTE: This function, for historical reasons, computes field->fixed_len in a |
| 98 |
slightly suboptimal way. For example: |
| 99 |
|
| 100 |
- A spatial index always has MTR in the first field, even for |
| 101 |
DATA_GEOMETRY not just for DATA_POINT, and so always has DATA_MBR_LEN. Yet, |
| 102 |
we only set it to DATA_MBR_LEN for DATA_POINT, and for DATA_GEOMETRY set it |
| 103 |
to 0 returned by col->get_fixed_size(..). |
| 104 |
- We always store the first dict_index_get_n_unique_in_tree() fields |
| 105 |
inline (see dtuple_convert_big_rec()) to facilitate efficient navigation. |
| 106 |
Yet, we assume that whenever fixed_len > DICT_MAX_FIXED_COL_LEN we may |
| 107 |
store it off-page. |
| 108 |
|
| 109 |
Alas, we can never change how dict_index_add_col() decides if fixed_len should |
| 110 |
be 0 or not, without corrupting indexes created before upgrade. It is used to |
| 111 |
determine for which fields the COMPACT format needs to store extra byte(s) for |
| 112 |
length. Thus changing the logic here, will cause parser to interpret existing |
| 113 |
index in a wrong way, and produce new records incompatible with the old ones. |
| 114 |
Instead of modifying this function, consider improving get_field_max_size(..) |
| 115 |
and dict_index_node_ptr_max_size(..) to provide better estimates. */ |
| 116 |
if (dict_index_is_spatial(index) && DATA_POINT_MTYPE(col->mtype) && |
| 117 |
index->n_def == 1) { |
| 118 |
field->fixed_len = DATA_MBR_LEN; |
| 119 |
} else { |
| 120 |
field->fixed_len = static_cast<unsigned int>( |
| 121 |
col->get_fixed_size(dict_table_is_comp(table))); |
| 122 |
} |
| 123 |
|
| 124 |
if (prefix_len && field->fixed_len > prefix_len) { |
| 125 |
field->fixed_len = (unsigned int)prefix_len; |
| 126 |
} |
| 127 |
|
| 128 |
/* Long fixed-length fields that need external storage are treated as |
| 129 |
variable-length fields, so that the extern flag can be embedded in |
| 130 |
the length word. */ |
| 131 |
|
| 132 |
if (field->fixed_len > DICT_MAX_FIXED_COL_LEN) { |
| 133 |
field->fixed_len = 0; |
| 134 |
} |
| 135 |
|
| 136 |
/* The comparison limit above must be constant. If it were |
| 137 |
changed, the disk format of some fixed-length columns would |
| 138 |
change, which would be a disaster. */ |
| 139 |
static_assert(DICT_MAX_FIXED_COL_LEN == 768, "DICT_MAX_FIXED_COL_LEN != 768"); |
| 140 |
|
| 141 |
/* Skip INSTANT DROP column */ |
| 142 |
if (col->is_nullable() && !col->is_instant_dropped()) { |
| 143 |
index->n_nullable++; |
| 144 |
} |
| 145 |
} |