1
/* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software Foundation,
14
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
19
#include "rpl_record.h"
20
#include "rpl_slave.h" // Need to pull in slave_print_msg
21
#include "rpl_utility.h"
28
Pack a record of data for a table into a format suitable for
29
transfer via the binary log.
31
The format for a row in transfer with N fields is the following:
34
One null bit for every column *regardless of whether it can be
35
null or not*. This simplifies the decoding. Observe that the
36
number of null bits is equal to the number of set bits in the
37
@c cols bitmap. The number of null bytes is the smallest number
38
of bytes necessary to store the null bits.
43
Each field is stored in packed format.
46
@param table Table describing the format of the record
48
@param cols Bitmap with a set bit for each column that should
51
@param row_data Pointer to memory where row will be written
53
@param record Pointer to record that should be packed. It is
54
assumed that the pointer refers to either @c
55
record[0] or @c record[1], but no such check is
56
made since the code does not rely on that.
58
@return The number of bytes written at @c row_data.
60
#if !defined(MYSQL_CLIENT)
62
pack_row(TABLE *table, MY_BITMAP const* cols,
63
uchar *row_data, const uchar *record)
65
Field **p_field= table->field, *field;
66
int const null_byte_count= (bitmap_bits_set(cols) + 7) / 8;
67
uchar *pack_ptr = row_data + null_byte_count;
68
uchar *null_ptr = row_data;
69
my_ptrdiff_t const rec_offset= record - table->record[0];
70
my_ptrdiff_t const def_offset= table->s->default_values - table->record[0];
72
DBUG_ENTER("pack_row");
75
We write the null bits and the packed records using one pass
76
through all the fields. The null bytes are written little-endian,
77
i.e., the first fields are in the first byte.
79
unsigned int null_bits= (1U << 8) - 1;
80
// Mask to mask out the correct but among the null bits
81
unsigned int null_mask= 1U;
82
DBUG_PRINT("debug", ("null ptr: 0x%lx; row start: %p; null bytes: %d",
83
(ulong) null_ptr, row_data, null_byte_count));
84
DBUG_DUMP("cols", (uchar*) cols->bitmap, cols->last_word_ptr - cols->bitmap + 1);
85
for ( ; (field= *p_field) ; p_field++)
87
if (bitmap_is_set(cols, p_field - table->field))
90
if (field->is_null(rec_offset))
92
DBUG_PRINT("debug", ("Is NULL; null_mask: 0x%x; null_bits: 0x%x",
93
null_mask, null_bits));
95
null_bits |= null_mask;
100
null_bits &= ~null_mask;
103
We only store the data of the field if it is non-null
105
For big-endian machines, we have to make sure that the
106
length is stored in little-endian format, since this is the
107
format used for the binlog.
110
const uchar *old_pack_ptr= pack_ptr;
112
pack_ptr= field->pack(pack_ptr, field->ptr + offset,
113
field->max_data_length(), TRUE);
114
DBUG_PRINT("debug", ("field: %s; real_type: %d, pack_ptr: 0x%lx;"
115
" pack_ptr':0x%lx; bytes: %d",
116
field->field_name, field->real_type(),
117
(ulong) old_pack_ptr, (ulong) pack_ptr,
118
(int) (pack_ptr - old_pack_ptr)));
119
DBUG_DUMP("packed_data", old_pack_ptr, pack_ptr - old_pack_ptr);
123
if ((null_mask & 0xFF) == 0)
125
DBUG_ASSERT(null_ptr < row_data + null_byte_count);
127
*null_ptr++ = null_bits;
128
null_bits= (1U << 8) - 1;
134
DBUG_PRINT("debug", ("Skipped"));
140
Write the last (partial) byte, if there is one
142
if ((null_mask & 0xFF) > 1)
144
DBUG_ASSERT(null_ptr < row_data + null_byte_count);
145
*null_ptr++ = null_bits;
149
The null pointer should now point to the first byte of the
150
packed data. If it doesn't, something is very wrong.
152
DBUG_ASSERT(null_ptr == row_data + null_byte_count);
153
DBUG_DUMP("row_data", row_data, pack_ptr - row_data);
154
DBUG_RETURN(static_cast<size_t>(pack_ptr - row_data));
160
Unpack a row into @c table->record[0].
162
The function will always unpack into the @c table->record[0]
163
record. This is because there are too many dependencies on where
164
the various member functions of Field and subclasses expect to
167
The row is assumed to only consist of the fields for which the
168
corresponding bit in bitset @c cols is set; the other parts of the
169
record are left alone.
171
At most @c colcnt columns are read: if the table is larger than
172
that, the remaining fields are not filled in.
174
@note The relay log information can be NULL, which means that no
175
checking or comparison with the source table is done, simply
176
because it is not used. This feature is used by MySQL Backup to
177
unpack a row from from the backup image, but can be used for other
180
@param rli Relay log info, which can be NULL
181
@param table Table to unpack into
182
@param colcnt Number of columns to read from record
185
@param cols Pointer to bitset describing columns to fill in
186
@param row_end Pointer to variable that will hold the value of the
187
one-after-end position for the row
188
@param master_reclength
189
Pointer to variable that will be set to the length of the
190
record on the master side
194
@retval HA_ERR_GENERIC
195
A generic, internal, error caused the unpacking to fail.
197
#if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION)
199
unpack_row(Relay_log_info const *rli,
200
TABLE *table, uint const colcnt,
201
uchar const *const row_data, MY_BITMAP const *cols,
202
uchar const **const row_end, ulong *const master_reclength)
204
DBUG_ENTER("unpack_row");
205
DBUG_ASSERT(row_data);
207
size_t const master_null_byte_count= (bitmap_bits_set(cols) + 7) / 8;
210
uchar const *null_ptr= row_data;
211
uchar const *pack_ptr= row_data + master_null_byte_count;
213
if (bitmap_is_clear_all(cols))
216
There was no data sent from the master, so there is
220
*master_reclength= 0;
225
Field **const begin_ptr = table->field;
227
Field **const end_ptr= begin_ptr + colcnt;
229
DBUG_ASSERT(null_ptr < row_data + master_null_byte_count);
231
// Mask to mask out the correct bit among the null bits
232
unsigned int null_mask= 1U;
233
// The "current" null bits
234
unsigned int null_bits= *null_ptr++;
236
table_def *tabledef= NULL;
237
TABLE *conv_table= NULL;
238
bool table_found= rli && rli->get_table_data(table, &tabledef, &conv_table);
239
DBUG_PRINT("debug", ("Table data: table_found: %d, tabldef: %p, conv_table: %p",
240
table_found, tabledef, conv_table));
241
DBUG_ASSERT(table_found);
244
If rli is NULL it means that there is no source table and that the
245
row shall just be unpacked without doing any checks. This feature
246
is used by MySQL Backup, but can be used for other purposes as
249
if (rli && !table_found)
250
DBUG_RETURN(HA_ERR_GENERIC);
252
for (field_ptr= begin_ptr ; field_ptr < end_ptr && *field_ptr ; ++field_ptr)
255
If there is a conversion table, we pick up the field pointer to
256
the conversion table. If the conversion table or the field
257
pointer is NULL, no conversions are necessary.
260
conv_table ? conv_table->field[field_ptr - begin_ptr] : NULL;
262
conv_field ? conv_field : *field_ptr;
263
DBUG_PRINT("debug", ("Conversion %srequired for field '%s' (#%ld)",
264
conv_field ? "" : "not ",
265
(*field_ptr)->field_name,
266
(long) (field_ptr - begin_ptr)));
267
DBUG_ASSERT(f != NULL);
269
DBUG_PRINT("debug", ("field: %s; null mask: 0x%x; null bits: 0x%lx;"
270
" row start: %p; null bytes: %ld",
271
f->field_name, null_mask, (ulong) null_bits,
272
pack_ptr, (ulong) master_null_byte_count));
275
No need to bother about columns that does not exist: they have
276
gotten default values when being emptied above.
278
if (bitmap_is_set(cols, field_ptr - begin_ptr))
280
if ((null_mask & 0xFF) == 0)
282
DBUG_ASSERT(null_ptr < row_data + master_null_byte_count);
284
null_bits= *null_ptr++;
287
DBUG_ASSERT(null_mask & 0xFF); // One of the 8 LSB should be set
289
/* Field...::unpack() cannot return 0 */
290
DBUG_ASSERT(pack_ptr != NULL);
292
if (null_bits & null_mask)
296
DBUG_PRINT("debug", ("Was NULL; null mask: 0x%x; null bits: 0x%x",
297
null_mask, null_bits));
299
Calling reset just in case one is unpacking on top a
302
This could probably go into set_null() but doing so,
303
(i) triggers assertion in other parts of the code at
304
the moment; (ii) it would make us reset the field,
305
always when setting null, which right now doesn't seem
306
needed anywhere else except here.
308
TODO: maybe in the future we should consider moving
309
the reset to make it part of set_null. But then
310
the assertions triggered need to be
319
push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
320
ER_BAD_NULL_ERROR, ER(ER_BAD_NULL_ERROR),
329
We only unpack the field if it was non-null.
330
Use the master's size information if available else call
331
normal unpack operation.
333
uint16 const metadata= tabledef->field_metadata(i);
335
uchar const *const old_pack_ptr= pack_ptr;
337
pack_ptr= f->unpack(f->ptr, pack_ptr, metadata, TRUE);
338
DBUG_PRINT("debug", ("Unpacked; metadata: 0x%x;"
339
" pack_ptr: 0x%lx; pack_ptr': 0x%lx; bytes: %d",
340
metadata, (ulong) old_pack_ptr, (ulong) pack_ptr,
341
(int) (pack_ptr - old_pack_ptr)));
344
The raw size of the field, as calculated in calc_field_size,
345
should match the one reported by Field_*::unpack unless it is
346
a old decimal data type which is unsupported datatype in
349
DBUG_ASSERT(tabledef->type(i) == MYSQL_TYPE_DECIMAL ||
350
tabledef->calc_field_size(i, (uchar *) old_pack_ptr) ==
351
(uint32) (pack_ptr - old_pack_ptr));
355
If conv_field is set, then we are doing a conversion. In this
356
case, we have unpacked the master data to the conversion
357
table, so we need to copy the value stored in the conversion
358
table into the final table and do the conversion at the same time.
364
char source_buf[MAX_FIELD_WIDTH];
365
char value_buf[MAX_FIELD_WIDTH];
366
String source_type(source_buf, sizeof(source_buf), system_charset_info);
367
String value_string(value_buf, sizeof(value_buf), system_charset_info);
368
conv_field->sql_type(source_type);
369
conv_field->val_str(&value_string);
370
DBUG_PRINT("debug", ("Copying field '%s' of type '%s' with value '%s'",
371
(*field_ptr)->field_name,
372
source_type.c_ptr_safe(), value_string.c_ptr_safe()));
374
copy.set(*field_ptr, f, TRUE);
375
(*copy.do_copy)(©);
377
char target_buf[MAX_FIELD_WIDTH];
378
String target_type(target_buf, sizeof(target_buf), system_charset_info);
379
(*field_ptr)->sql_type(target_type);
380
(*field_ptr)->val_str(&value_string);
381
DBUG_PRINT("debug", ("Value of field '%s' of type '%s' is now '%s'",
382
(*field_ptr)->field_name,
383
target_type.c_ptr_safe(), value_string.c_ptr_safe()));
392
DBUG_PRINT("debug", ("Non-existent: skipped"));
399
throw away master's extra fields
401
uint max_cols= min<ulong>(tabledef->size(), cols->n_bits);
402
for (; i < max_cols; i++)
404
if (bitmap_is_set(cols, i))
406
if ((null_mask & 0xFF) == 0)
408
DBUG_ASSERT(null_ptr < row_data + master_null_byte_count);
410
null_bits= *null_ptr++;
412
DBUG_ASSERT(null_mask & 0xFF); // One of the 8 LSB should be set
414
if (!((null_bits & null_mask) && tabledef->maybe_null(i))) {
415
uint32 len= tabledef->calc_field_size(i, (uchar *) pack_ptr);
416
DBUG_DUMP("field_data", pack_ptr, len);
424
We should now have read all the null bytes, otherwise something is
427
DBUG_ASSERT(null_ptr == row_data + master_null_byte_count);
429
DBUG_DUMP("row_data", row_data, pack_ptr - row_data);
432
if (master_reclength)
435
*master_reclength = (*field_ptr)->ptr - table->record[0];
437
*master_reclength = table->s->reclength;
444
Fills @c table->record[0] with default values.
446
First @c restore_record() is called to restore the default values for
447
record concerning the given table. Then, if @c check is true,
448
a check is performed to see if fields are have default value or can
449
be NULL. Otherwise error is reported.
451
@param table Table whose record[0] buffer is prepared.
452
@param check Specifies if lack of default error needs checking.
454
@returns 0 on success or a handler level error code
456
int prepare_record(TABLE *const table, const MY_BITMAP *cols, const bool check)
458
DBUG_ENTER("prepare_record");
460
restore_record(table, s->default_values);
466
For fields the extra fields on the slave, we check if they have a default.
467
The check follows the same rules as the INSERT query without specifying an
468
explicit value for a field not having the explicit default
469
(@c check_that_all_fields_are_given_values()).
472
DBUG_PRINT_BITSET("debug", "cols: %s", cols);
473
for (Field **field_ptr= table->field; *field_ptr; ++field_ptr)
475
if ((uint) (field_ptr - table->field) >= cols->n_bits ||
476
!bitmap_is_set(cols, field_ptr - table->field))
478
Field *const f= *field_ptr;
479
if ((f->flags & NO_DEFAULT_VALUE_FLAG) &&
480
(f->real_type() != MYSQL_TYPE_ENUM))
483
push_warning_printf(current_thd,
484
Sql_condition::WARN_LEVEL_WARN,
485
ER_NO_DEFAULT_FOR_FIELD,
486
ER(ER_NO_DEFAULT_FOR_FIELD),
495
#endif // HAVE_REPLICATION