1 /* Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software Foundation,
21 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
22
23 #include "sql_priv.h"
24 #include "unireg.h"
25 #include "rpl_rli.h"
26 #include "rpl_record.h"
27 #include "rpl_slave.h" // Need to pull in slave_print_msg
28 #include "rpl_utility.h"
29 #include "rpl_rli.h"
30
31 using std::min;
32 using std::max;
33
34 /**
35 Pack a record of data for a table into a format suitable for
36 transfer via the binary log.
37
38 The format for a row in transfer with N fields is the following:
39
40 ceil(N/8) null bytes:
41 One null bit for every column *regardless of whether it can be
42 null or not*. This simplifies the decoding. Observe that the
43 number of null bits is equal to the number of set bits in the
44 @c cols bitmap. The number of null bytes is the smallest number
45 of bytes necessary to store the null bits.
46
47 Padding bits are 1.
48
49 N packets:
50 Each field is stored in packed format.
51
52
53 @param table Table describing the format of the record
54
55 @param cols Bitmap with a set bit for each column that should
56 be stored in the row
57
58 @param row_data Pointer to memory where row will be written
59
60 @param record Pointer to record that should be packed. It is
61 assumed that the pointer refers to either @c
62 record[0] or @c record[1], but no such check is
63 made since the code does not rely on that.
64
65 @return The number of bytes written at @c row_data.
66 */
67 #if !defined(MYSQL_CLIENT)
68 size_t
pack_row(TABLE * table,MY_BITMAP const * cols,uchar * row_data,const uchar * record)69 pack_row(TABLE *table, MY_BITMAP const* cols,
70 uchar *row_data, const uchar *record)
71 {
72 Field **p_field= table->field, *field;
73 int const null_byte_count= (bitmap_bits_set(cols) + 7) / 8;
74 uchar *pack_ptr = row_data + null_byte_count;
75 uchar *null_ptr = row_data;
76 my_ptrdiff_t const rec_offset= record - table->record[0];
77 my_ptrdiff_t const def_offset= table->s->default_values - table->record[0];
78
79 DBUG_ENTER("pack_row");
80
81 /*
82 We write the null bits and the packed records using one pass
83 through all the fields. The null bytes are written little-endian,
84 i.e., the first fields are in the first byte.
85 */
86 unsigned int null_bits= (1U << 8) - 1;
87 // Mask to mask out the correct but among the null bits
88 unsigned int null_mask= 1U;
89 DBUG_PRINT("debug", ("null ptr: 0x%lx; row start: %p; null bytes: %d",
90 (ulong) null_ptr, row_data, null_byte_count));
91 DBUG_DUMP("cols", (uchar*) cols->bitmap, cols->last_word_ptr - cols->bitmap + 1);
92 for ( ; (field= *p_field) ; p_field++)
93 {
94 if (bitmap_is_set(cols, p_field - table->field))
95 {
96 my_ptrdiff_t offset;
97 if (field->is_null(rec_offset))
98 {
99 DBUG_PRINT("debug", ("Is NULL; null_mask: 0x%x; null_bits: 0x%x",
100 null_mask, null_bits));
101 offset= def_offset;
102 null_bits |= null_mask;
103 }
104 else
105 {
106 offset= rec_offset;
107 null_bits &= ~null_mask;
108
109 /*
110 We only store the data of the field if it is non-null
111
112 For big-endian machines, we have to make sure that the
113 length is stored in little-endian format, since this is the
114 format used for the binlog.
115 */
116 #ifndef DBUG_OFF
117 const uchar *old_pack_ptr= pack_ptr;
118 #endif
119 pack_ptr= field->pack(pack_ptr, field->ptr + offset,
120 field->max_data_length(), TRUE);
121 DBUG_PRINT("debug", ("field: %s; real_type: %d, pack_ptr: 0x%lx;"
122 " pack_ptr':0x%lx; bytes: %d",
123 field->field_name, field->real_type(),
124 (ulong) old_pack_ptr, (ulong) pack_ptr,
125 (int) (pack_ptr - old_pack_ptr)));
126 DBUG_DUMP("packed_data", old_pack_ptr, pack_ptr - old_pack_ptr);
127 }
128
129 null_mask <<= 1;
130 if ((null_mask & 0xFF) == 0)
131 {
132 DBUG_ASSERT(null_ptr < row_data + null_byte_count);
133 null_mask = 1U;
134 *null_ptr++ = null_bits;
135 null_bits= (1U << 8) - 1;
136 }
137 }
138 #ifndef DBUG_OFF
139 else
140 {
141 DBUG_PRINT("debug", ("Skipped"));
142 }
143 #endif
144 }
145
146 /*
147 Write the last (partial) byte, if there is one
148 */
149 if ((null_mask & 0xFF) > 1)
150 {
151 DBUG_ASSERT(null_ptr < row_data + null_byte_count);
152 *null_ptr++ = null_bits;
153 }
154
155 /*
156 The null pointer should now point to the first byte of the
157 packed data. If it doesn't, something is very wrong.
158 */
159 DBUG_ASSERT(null_ptr == row_data + null_byte_count);
160 DBUG_DUMP("row_data", row_data, pack_ptr - row_data);
161 DBUG_RETURN(static_cast<size_t>(pack_ptr - row_data));
162 }
163 #endif
164
165
166 /**
167 Unpack a row into @c table->record[0].
168
169 The function will always unpack into the @c table->record[0]
170 record. This is because there are too many dependencies on where
171 the various member functions of Field and subclasses expect to
172 write.
173
174 The row is assumed to only consist of the fields for which the
175 corresponding bit in bitset @c cols is set; the other parts of the
176 record are left alone.
177
178 At most @c colcnt columns are read: if the table is larger than
179 that, the remaining fields are not filled in.
180
181 @note The relay log information can be NULL, which means that no
182 checking or comparison with the source table is done, simply
183 because it is not used. This feature is used by MySQL Backup to
184 unpack a row from from the backup image, but can be used for other
185 purposes as well.
186
187 @param rli Relay log info, which can be NULL
188 @param table Table to unpack into
189 @param colcnt Number of columns to read from record
190 @param row_data
191 Packed row data
192 @param cols Pointer to bitset describing columns to fill in
193 @param curr_row_end
194 Pointer to variable that will hold the value of the
195 one-after-end position for the current row
196 @param master_reclength
197 Pointer to variable that will be set to the length of the
198 record on the master side
199 @param row_end
200 Pointer to variable that will hold the value of the
201 end position for the data in the row event
202
203 @retval 0 No error
204
205 @retval HA_ERR_GENERIC
206 A generic, internal, error caused the unpacking to fail.
207 */
208 #if !defined(MYSQL_CLIENT) && defined(HAVE_REPLICATION)
209 int
unpack_row(Relay_log_info const * rli,TABLE * table,uint const colcnt,uchar const * const row_data,MY_BITMAP const * cols,uchar const ** const current_row_end,ulong * const master_reclength,uchar const * const row_end)210 unpack_row(Relay_log_info const *rli,
211 TABLE *table, uint const colcnt,
212 uchar const *const row_data, MY_BITMAP const *cols,
213 uchar const **const current_row_end, ulong *const master_reclength,
214 uchar const *const row_end)
215 {
216 DBUG_ENTER("unpack_row");
217 DBUG_ASSERT(row_data);
218 DBUG_ASSERT(table);
219 size_t const master_null_byte_count= (bitmap_bits_set(cols) + 7) / 8;
220 int error= 0;
221
222 uchar const *null_ptr= row_data;
223 uchar const *pack_ptr= row_data + master_null_byte_count;
224
225 if (bitmap_is_clear_all(cols))
226 {
227 /**
228 There was no data sent from the master, so there is
229 nothing to unpack.
230 */
231 *current_row_end= pack_ptr;
232 *master_reclength= 0;
233 DBUG_RETURN(error);
234 }
235
236
237 Field **const begin_ptr = table->field;
238 Field **field_ptr;
239 Field **const end_ptr= begin_ptr + colcnt;
240
241 DBUG_ASSERT(null_ptr < row_data + master_null_byte_count);
242
243 // Mask to mask out the correct bit among the null bits
244 unsigned int null_mask= 1U;
245 // The "current" null bits
246 unsigned int null_bits= *null_ptr++;
247 uint i= 0;
248 table_def *tabledef= NULL;
249 TABLE *conv_table= NULL;
250 bool table_found= rli && rli->get_table_data(table, &tabledef, &conv_table);
251 DBUG_PRINT("debug", ("Table data: table_found: %d, tabldef: %p, conv_table: %p",
252 table_found, tabledef, conv_table));
253 DBUG_ASSERT(table_found);
254
255 /*
256 If rli is NULL it means that there is no source table and that the
257 row shall just be unpacked without doing any checks. This feature
258 is used by MySQL Backup, but can be used for other purposes as
259 well.
260 */
261 if (rli && !table_found)
262 DBUG_RETURN(HA_ERR_GENERIC);
263
264 for (field_ptr= begin_ptr ; field_ptr < end_ptr && *field_ptr ; ++field_ptr)
265 {
266 /*
267 If there is a conversion table, we pick up the field pointer to
268 the conversion table. If the conversion table or the field
269 pointer is NULL, no conversions are necessary.
270 */
271 Field *conv_field=
272 conv_table ? conv_table->field[field_ptr - begin_ptr] : NULL;
273 Field *const f=
274 conv_field ? conv_field : *field_ptr;
275 DBUG_PRINT("debug", ("Conversion %srequired for field '%s' (#%ld)",
276 conv_field ? "" : "not ",
277 (*field_ptr)->field_name,
278 (long) (field_ptr - begin_ptr)));
279 DBUG_ASSERT(f != NULL);
280
281 DBUG_PRINT("debug", ("field: %s; null mask: 0x%x; null bits: 0x%lx;"
282 " row start: %p; null bytes: %ld",
283 f->field_name, null_mask, (ulong) null_bits,
284 pack_ptr, (ulong) master_null_byte_count));
285
286 /*
287 No need to bother about columns that does not exist: they have
288 gotten default values when being emptied above.
289 */
290 if (bitmap_is_set(cols, field_ptr - begin_ptr))
291 {
292 if ((null_mask & 0xFF) == 0)
293 {
294 DBUG_ASSERT(null_ptr < row_data + master_null_byte_count);
295 null_mask= 1U;
296 null_bits= *null_ptr++;
297 }
298
299 DBUG_ASSERT(null_mask & 0xFF); // One of the 8 LSB should be set
300
301 /* Field...::unpack() cannot return 0 */
302 DBUG_ASSERT(pack_ptr != NULL);
303
304 if (null_bits & null_mask)
305 {
306 if (f->maybe_null())
307 {
308 DBUG_PRINT("debug", ("Was NULL; null mask: 0x%x; null bits: 0x%x",
309 null_mask, null_bits));
310 /**
311 Calling reset just in case one is unpacking on top a
312 record with data.
313
314 This could probably go into set_null() but doing so,
315 (i) triggers assertion in other parts of the code at
316 the moment; (ii) it would make us reset the field,
317 always when setting null, which right now doesn't seem
318 needed anywhere else except here.
319
320 TODO: maybe in the future we should consider moving
321 the reset to make it part of set_null. But then
322 the assertions triggered need to be
323 addressed/revisited.
324 */
325 f->reset();
326 f->set_null();
327 }
328 else
329 {
330 f->set_default();
331 push_warning_printf(current_thd, Sql_condition::WARN_LEVEL_WARN,
332 ER_BAD_NULL_ERROR, ER(ER_BAD_NULL_ERROR),
333 f->field_name);
334 }
335 }
336 else
337 {
338 f->set_notnull();
339
340 /*
341 We only unpack the field if it was non-null.
342 Use the master's size information if available else call
343 normal unpack operation.
344 */
345 uint16 const metadata= tabledef->field_metadata(i);
346 #ifndef DBUG_OFF
347 uchar const *const old_pack_ptr= pack_ptr;
348 #endif
349 uint32 len= tabledef->calc_field_size(i, (uchar *) pack_ptr);
350 if ( pack_ptr + len > row_end )
351 {
352 pack_ptr+= len;
353 my_error(ER_SLAVE_CORRUPT_EVENT, MYF(0));
354 DBUG_RETURN(ER_SLAVE_CORRUPT_EVENT);
355 }
356 pack_ptr= f->unpack(f->ptr, pack_ptr, metadata, TRUE);
357 DBUG_PRINT("debug", ("Unpacked; metadata: 0x%x;"
358 " pack_ptr: 0x%lx; pack_ptr': 0x%lx; bytes: %d",
359 metadata, (ulong) old_pack_ptr, (ulong) pack_ptr,
360 (int) (pack_ptr - old_pack_ptr)));
361
362 /*
363 The raw size of the field, as calculated in calc_field_size,
364 should match the one reported by Field_*::unpack unless it is
365 a old decimal data type which is unsupported datatype in
366 RBR mode.
367 */
368 DBUG_ASSERT(tabledef->type(i) == MYSQL_TYPE_DECIMAL ||
369 tabledef->calc_field_size(i, (uchar *) old_pack_ptr) ==
370 (uint32) (pack_ptr - old_pack_ptr));
371 }
372
373 /*
374 If conv_field is set, then we are doing a conversion. In this
375 case, we have unpacked the master data to the conversion
376 table, so we need to copy the value stored in the conversion
377 table into the final table and do the conversion at the same time.
378 */
379 if (conv_field)
380 {
381 Copy_field copy;
382 #ifndef DBUG_OFF
383 char source_buf[MAX_FIELD_WIDTH];
384 char value_buf[MAX_FIELD_WIDTH];
385 String source_type(source_buf, sizeof(source_buf), system_charset_info);
386 String value_string(value_buf, sizeof(value_buf), system_charset_info);
387 conv_field->sql_type(source_type);
388 conv_field->val_str(&value_string);
389 DBUG_PRINT("debug", ("Copying field '%s' of type '%s' with value '%s'",
390 (*field_ptr)->field_name,
391 source_type.c_ptr_safe(), value_string.c_ptr_safe()));
392 #endif
393 copy.set(*field_ptr, f, TRUE);
394 (*copy.do_copy)(©);
395 #ifndef DBUG_OFF
396 char target_buf[MAX_FIELD_WIDTH];
397 String target_type(target_buf, sizeof(target_buf), system_charset_info);
398 (*field_ptr)->sql_type(target_type);
399 (*field_ptr)->val_str(&value_string);
400 DBUG_PRINT("debug", ("Value of field '%s' of type '%s' is now '%s'",
401 (*field_ptr)->field_name,
402 target_type.c_ptr_safe(), value_string.c_ptr_safe()));
403 #endif
404 }
405
406 null_mask <<= 1;
407 }
408 #ifndef DBUG_OFF
409 else
410 {
411 DBUG_PRINT("debug", ("Non-existent: skipped"));
412 }
413 #endif
414 i++;
415 }
416
417 /*
418 throw away master's extra fields
419 */
420 uint max_cols= min<ulong>(tabledef->size(), cols->n_bits);
421 for (; i < max_cols; i++)
422 {
423 if (bitmap_is_set(cols, i))
424 {
425 if ((null_mask & 0xFF) == 0)
426 {
427 DBUG_ASSERT(null_ptr < row_data + master_null_byte_count);
428 null_mask= 1U;
429 null_bits= *null_ptr++;
430 }
431 DBUG_ASSERT(null_mask & 0xFF); // One of the 8 LSB should be set
432
433 if (!((null_bits & null_mask) && tabledef->maybe_null(i))) {
434 uint32 len= tabledef->calc_field_size(i, (uchar *) pack_ptr);
435 DBUG_DUMP("field_data", pack_ptr, len);
436 pack_ptr+= len;
437 if ( pack_ptr > row_end )
438 {
439 my_error(ER_SLAVE_CORRUPT_EVENT, MYF(0));
440 DBUG_RETURN(ER_SLAVE_CORRUPT_EVENT);
441 }
442 }
443 null_mask <<= 1;
444 }
445 }
446
447 /*
448 We should now have read all the null bytes, otherwise something is
449 really wrong.
450 */
451 DBUG_ASSERT(null_ptr == row_data + master_null_byte_count);
452
453 DBUG_DUMP("row_data", row_data, pack_ptr - row_data);
454
455 *current_row_end = pack_ptr;
456 if (master_reclength)
457 {
458 if (*field_ptr)
459 *master_reclength = (*field_ptr)->ptr - table->record[0];
460 else
461 *master_reclength = table->s->reclength;
462 }
463
464 DBUG_RETURN(error);
465 }
466
467 /**
468 Fills @c table->record[0] with default values.
469
470 First @c restore_record() is called to restore the default values for
471 record concerning the given table. Then, if @c check is true,
472 a check is performed to see if fields are have default value or can
473 be NULL. Otherwise error is reported.
474
475 @param table Table whose record[0] buffer is prepared.
476 @param check Specifies if lack of default error needs checking.
477
478 @returns 0 on success or a handler level error code
479 */
prepare_record(TABLE * const table,const MY_BITMAP * cols,const bool check)480 int prepare_record(TABLE *const table, const MY_BITMAP *cols, const bool check)
481 {
482 DBUG_ENTER("prepare_record");
483
484 restore_record(table, s->default_values);
485
486 if (!check)
487 DBUG_RETURN(0);
488
489 /*
490 For fields the extra fields on the slave, we check if they have a default.
491 The check follows the same rules as the INSERT query without specifying an
492 explicit value for a field not having the explicit default
493 (@c check_that_all_fields_are_given_values()).
494 */
495
496 DBUG_PRINT_BITSET("debug", "cols: %s", cols);
497 /**
498 Save a reference to the original write set bitmaps.
499 We will need this to restore the bitmaps at the end.
500 */
501 MY_BITMAP *old_write_set= table->write_set;
502 /**
503 Just to be sure that tmp_set is currently not in use as
504 the read_set already.
505 */
506 DBUG_ASSERT(table->write_set != &table->tmp_set);
507 /* set the temporary write_set */
508 table->column_bitmaps_set_no_signal(table->read_set,
509 &table->tmp_set);
510 /**
511 Set table->write_set bits for all the columns as they
512 will be checked in set_default() function.
513 */
514 bitmap_set_all(table->write_set);
515
516 for (Field **field_ptr= table->field; *field_ptr; ++field_ptr)
517 {
518 uint field_index= (uint) (field_ptr - table->field);
519 if (field_index >= cols->n_bits || !bitmap_is_set(cols, field_index))
520 {
521 Field *const f= *field_ptr;
522 if ((f->flags & NO_DEFAULT_VALUE_FLAG) &&
523 (f->real_type() != MYSQL_TYPE_ENUM))
524 {
525 f->set_default();
526 push_warning_printf(current_thd,
527 Sql_condition::WARN_LEVEL_WARN,
528 ER_NO_DEFAULT_FOR_FIELD,
529 ER(ER_NO_DEFAULT_FOR_FIELD),
530 f->field_name);
531 }
532 else if (f->has_insert_default_function())
533 {
534 f->set_default();
535 }
536 }
537 }
538
539 /* set the write_set back to original*/
540 table->column_bitmaps_set_no_signal(table->read_set,
541 old_write_set);
542
543 DBUG_RETURN(0);
544 }
545
546 #endif // HAVE_REPLICATION
547