54 int find_ref_key(
KEY *key, uint key_count, uchar *
record,
Field *field,
55 uint *key_length, uint *keypart)
61 fieldpos= field->offset(record);
64 for (i= 0, key_info= key ;
68 if (key_info->key_part[0].offset == fieldpos)
70 *key_length= *keypart= 0;
76 for (i= 0, key_info= key;
83 for (j=0, key_part=key_info->key_part ;
84 j < key_info->user_defined_key_parts ;
87 if (key_part->offset == fieldpos)
92 *key_length+= key_part->store_length;
114 void key_copy(uchar *to_key, uchar *from_record,
KEY *key_info,
122 for (key_part= key_info->key_part; (
int) key_length > 0; key_part++)
124 if (key_part->null_bit)
126 *to_key++=
test(from_record[key_part->null_offset] &
130 if (key_part->key_part_flag & HA_BLOB_PART ||
131 key_part->key_part_flag & HA_VAR_LENGTH_PART)
133 key_length-= HA_KEY_BLOB_LENGTH;
134 length= min<uint>(key_length, key_part->length);
135 key_part->field->get_key_image(to_key, length, Field::itRAW);
136 to_key+= HA_KEY_BLOB_LENGTH;
140 length= min<uint>(key_length, key_part->length);
141 Field *field= key_part->field;
143 uint bytes= field->get_key_image(to_key, length, Field::itRAW);
145 cs->cset->fill(cs, (
char*) to_key + bytes, length - bytes,
' ');
163 void key_zero_nulls(uchar *
tuple,
KEY *key_info)
167 for (; key_part != key_part_end; key_part++)
169 if (key_part->null_bit && *tuple)
170 memset(tuple+1, 0, key_part->store_length-1);
171 tuple+= key_part->store_length;
188 void key_restore(uchar *to_record, uchar *from_key,
KEY *key_info,
198 for (key_part= key_info->key_part ; (
int) key_length > 0 ; key_part++)
200 uchar used_uneven_bits= 0;
201 if (key_part->null_bit)
204 to_record[key_part->null_offset]|= key_part->null_bit;
206 to_record[key_part->null_offset]&= ~key_part->null_bit;
209 if (key_part->type == HA_KEYTYPE_BIT)
214 uchar bits= *(from_key + key_part->length -
215 field->pack_length_in_rec() - 1);
216 set_rec_bits(bits, to_record + key_part->null_offset +
217 (key_part->null_bit == 128),
218 field->bit_ofs, field->bit_len);
223 if (key_part->key_part_flag & HA_BLOB_PART)
232 uint blob_length= uint2korr(from_key);
234 from_key+= HA_KEY_BLOB_LENGTH;
235 key_length-= HA_KEY_BLOB_LENGTH;
236 field->set_ptr_offset(to_record - field->table->record[0],
237 (ulong) blob_length, from_key);
238 length= key_part->length;
240 else if (key_part->key_part_flag & HA_VAR_LENGTH_PART)
242 Field *field= key_part->field;
243 my_bitmap_map *old_map;
244 my_ptrdiff_t ptrdiff= to_record - field->table->record[0];
245 field->move_field_offset(ptrdiff);
246 key_length-= HA_KEY_BLOB_LENGTH;
247 length= min<uint>(key_length, key_part->length);
248 old_map= dbug_tmp_use_all_columns(field->table, field->table->write_set);
249 field->set_key_image(from_key, length);
250 dbug_tmp_restore_column_map(field->table->write_set, old_map);
251 from_key+= HA_KEY_BLOB_LENGTH;
252 field->move_field_offset(-ptrdiff);
256 length= min<uint>(key_length, key_part->length);
258 memcpy(to_record + key_part->offset, from_key + used_uneven_bits
259 , (
size_t) length - used_uneven_bits);
287 bool key_cmp_if_same(
TABLE *
table,
const uchar *key,uint idx,uint key_length)
291 const uchar *key_end= key + key_length;;
293 for (key_part=table->key_info[idx].key_part;
295 key_part++, key+= store_length)
298 store_length= key_part->store_length;
300 if (key_part->null_bit)
302 if (*key !=
test(table->record[0][key_part->null_offset] &
310 if (key_part->key_part_flag & (HA_BLOB_PART | HA_VAR_LENGTH_PART |
313 if (key_part->field->key_cmp(key, key_part->length))
317 length= min((uint) (key_end-key), store_length);
318 if (!(key_part->key_type & (FIELDFLAG_NUMBER+FIELDFLAG_BINARY+
322 uint char_length= key_part->length / cs->mbmaxlen;
323 const uchar *pos= table->record[0] + key_part->offset;
324 if (length > char_length)
326 char_length= my_charpos(cs, pos, pos + length, char_length);
327 set_if_smaller(char_length, length);
329 if (cs->coll->strnncollsp(cs,
330 (
const uchar*) key, length,
331 (
const uchar*) pos, char_length, 0))
335 if (memcmp(key,table->record[0]+key_part->offset,length))
353 void field_unpack(
String *
to,
Field *field,
const uchar *
rec, uint max_length,
357 DBUG_ENTER(
"field_unpack");
359 max_length= field->pack_length();
362 if (field->is_null())
364 to->append(STRING_WITH_LEN(
"NULL"));
368 field->val_str(&tmp);
373 if (field->binary() && field->type() == MYSQL_TYPE_STRING && tmp.length())
375 const char *tmp_end= tmp.ptr() + tmp.length();
376 while (tmp_end > tmp.ptr() && !*--tmp_end) ;
377 tmp.length(tmp_end - tmp.ptr() + 1);
379 if (cs->mbmaxlen > 1 && prefix_key)
388 uint charpos, char_length= max_length / cs->mbmaxlen;
389 if ((charpos= my_charpos(cs, tmp.ptr(),
390 tmp.ptr() + tmp.length(),
391 char_length)) < tmp.length())
394 if (max_length < field->pack_length())
395 tmp.length(min(tmp.length(),max_length));
397 to->append(err.ptr());
400 to->append(STRING_WITH_LEN(
"???"));
421 my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
422 DBUG_ENTER(
"key_unpack");
427 key_part < key_part_end;
432 if (key_part->null_bit)
434 if (table->record[0][key_part->null_offset] & key_part->null_bit)
436 to->append(STRING_WITH_LEN(
"NULL"));
440 field_unpack(to, key_part->field, table->record[0], key_part->length,
441 test(key_part->key_part_flag & HA_PART_KEY_SEG));
443 dbug_tmp_restore_column_map(table->read_set, old_map);
468 bitmap_clear_all(&table->tmp_set);
469 table->mark_columns_used_by_index_no_reset(idx, &table->tmp_set);
470 if (bitmap_is_overlapping(&table->tmp_set, fields))
477 if (idx != table->s->primary_key && table->s->primary_key < MAX_KEY &&
479 return is_key_used(table, table->s->primary_key, fields);
498 int key_cmp(
KEY_PART_INFO *key_part,
const uchar *key, uint key_length)
502 for (
const uchar *end=key + key_length;
504 key+= store_length, key_part++)
507 store_length= key_part->store_length;
508 if (key_part->null_bit)
511 register bool field_is_null= key_part->field->is_null();
520 else if (field_is_null)
525 if ((cmp=key_part->field->key_cmp(key, key_part->length)) < 0)
566 int key_rec_cmp(
void *key_p, uchar *first_rec, uchar *second_rec)
569 KEY *key_info= *(key++);
570 uint key_parts, key_part_num;
572 uchar *rec0= key_part->field->ptr - key_part->offset;
573 my_ptrdiff_t first_diff= first_rec - rec0, sec_diff= second_rec - rec0;
576 DBUG_ENTER(
"key_rec_cmp");
579 DBUG_ASSERT(bitmap_is_set(key_info->table->read_set,
580 key_info->key_part->field->field_index));
585 key_part= key_info->key_part;
591 field= key_part->field;
594 if (!bitmap_is_set(field->table->read_set, field->field_index))
597 if (key_part->null_bit)
600 bool first_is_null= field->is_real_null(first_diff);
601 bool sec_is_null= field->is_real_null(sec_diff);
618 else if (!sec_is_null)
632 if ((result= field->cmp_max(field->ptr+first_diff, field->ptr+sec_diff,
638 }
while (key_part_num < key_parts);