1
/* Copyright (C) 2000-2006 MySQL AB
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
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
/* Functions to handle keys and fields in forms */
19
#include <drizzled/server_includes.h>
22
Search after a key that starts with 'field'
26
key First key to check
27
key_count How many keys to check
28
record Start of record
29
field Field to search after
30
key_length On partial match, contains length of fields before
32
keypart key part # of a field
35
Used when calculating key for NEXT_NUMBER
38
If no key starts with field test if field is part of some key. If we find
39
one, then return first key and set key_length to the number of bytes
43
-1 field is not part of the key
44
# Key part for key matching key.
45
key_length is set to length of key before (not including) field
48
int find_ref_key(KEY *key, uint32_t key_count, unsigned char *record, Field *field,
49
uint32_t *key_length, uint32_t *keypart)
52
register KEY *key_info;
55
fieldpos= field->offset(record);
57
/* Test if some key starts as fieldpos */
58
for (i= 0, key_info= key ;
62
if (key_info->key_part[0].offset == fieldpos)
63
{ /* Found key. Calc keylength */
64
*key_length= *keypart= 0;
65
return i; /* Use this key */
69
/* Test if some key contains fieldpos */
70
for (i= 0, key_info= key;
75
KEY_PART_INFO *key_part;
77
for (j=0, key_part=key_info->key_part ;
78
j < key_info->key_parts ;
81
if (key_part->offset == fieldpos)
84
return i; /* Use this key */
86
*key_length+= key_part->store_length;
89
return(-1); /* No key is ok */
94
Copy part of a record that forms a key or key prefix to a buffer.
96
The function takes a complete table record (as e.g. retrieved by
97
handler::index_read()), and a description of an index on the same table,
98
and extracts the first key_length bytes of the record which are part of a
99
key into to_key. If length == 0 then copy all bytes from the record that
102
@param to_key buffer that will be used as a key
103
@param from_record full record to be copied from
104
@param key_info descriptor of the index
105
@param key_length specifies length of all keyparts that will be copied
108
void key_copy(unsigned char *to_key, unsigned char *from_record, KEY *key_info,
109
unsigned int key_length)
112
KEY_PART_INFO *key_part;
115
key_length= key_info->key_length;
116
for (key_part= key_info->key_part; (int) key_length > 0; key_part++)
118
if (key_part->null_bit)
120
*to_key++= test(from_record[key_part->null_offset] &
124
if (key_part->key_part_flag & HA_BLOB_PART ||
125
key_part->key_part_flag & HA_VAR_LENGTH_PART)
127
key_length-= HA_KEY_BLOB_LENGTH;
128
length= cmin((uint16_t)key_length, key_part->length);
129
key_part->field->get_key_image(to_key, length, Field::itRAW);
130
to_key+= HA_KEY_BLOB_LENGTH;
134
length= cmin((uint16_t)key_length, key_part->length);
135
Field *field= key_part->field;
136
const CHARSET_INFO * const cs= field->charset();
137
uint32_t bytes= field->get_key_image(to_key, length, Field::itRAW);
139
cs->cset->fill(cs, (char*) to_key + bytes, length - bytes, ' ');
148
Zero the null components of key tuple.
151
void key_zero_nulls(unsigned char *tuple, KEY *key_info)
153
KEY_PART_INFO *key_part= key_info->key_part;
154
KEY_PART_INFO *key_part_end= key_part + key_info->key_parts;
155
for (; key_part != key_part_end; key_part++)
157
if (key_part->null_bit && *tuple)
158
memset(tuple+1, 0, key_part->store_length-1);
159
tuple+= key_part->store_length;
165
Restore a key from some buffer to record.
167
This function converts a key into record format. It can be used in cases
168
when we want to return a key as a result row.
170
@param to_record record buffer where the key will be restored to
171
@param from_key buffer that contains a key
172
@param key_info descriptor of the index
173
@param key_length specifies length of all keyparts that will be restored
176
void key_restore(unsigned char *to_record, unsigned char *from_key, KEY *key_info,
180
KEY_PART_INFO *key_part;
184
key_length= key_info->key_length;
186
for (key_part= key_info->key_part ; (int) key_length > 0 ; key_part++)
188
unsigned char used_uneven_bits= 0;
189
if (key_part->null_bit)
192
to_record[key_part->null_offset]|= key_part->null_bit;
194
to_record[key_part->null_offset]&= ~key_part->null_bit;
197
if (key_part->key_part_flag & HA_BLOB_PART)
200
This in fact never happens, as we have only partial BLOB
201
keys yet anyway, so it's difficult to find any sence to
202
restore the part of a record.
203
Maybe this branch is to be removed, but now we
204
have to ignore GCov compaining.
206
uint32_t blob_length= uint2korr(from_key);
207
Field_blob *field= (Field_blob*) key_part->field;
208
from_key+= HA_KEY_BLOB_LENGTH;
209
key_length-= HA_KEY_BLOB_LENGTH;
210
field->set_ptr_offset(to_record - field->table->record[0],
211
(ulong) blob_length, from_key);
212
length= key_part->length;
214
else if (key_part->key_part_flag & HA_VAR_LENGTH_PART)
216
Field *field= key_part->field;
217
my_ptrdiff_t ptrdiff= to_record - field->table->record[0];
218
field->move_field_offset(ptrdiff);
219
key_length-= HA_KEY_BLOB_LENGTH;
220
length= cmin(key_length, key_part->length);
221
field->set_key_image(from_key, length);
222
from_key+= HA_KEY_BLOB_LENGTH;
223
field->move_field_offset(-ptrdiff);
227
length= cmin(key_length, key_part->length);
228
/* skip the byte with 'uneven' bits, if used */
229
memcpy(to_record + key_part->offset, from_key + used_uneven_bits
230
, (size_t) length - used_uneven_bits);
239
Compare if a key has changed.
242
@param key key to compare to row
243
@param idx Index used
244
@param key_length Length of key
247
In theory we could just call field->cmp() for all field types,
248
but as we are only interested if a key has changed (not if the key is
249
larger or smaller than the previous value) we can do things a bit
250
faster by using memcmp() instead.
258
bool key_cmp_if_same(Table *table,const unsigned char *key,uint32_t idx,uint32_t key_length)
260
uint32_t store_length;
261
KEY_PART_INFO *key_part;
262
const unsigned char *key_end= key + key_length;;
264
for (key_part=table->key_info[idx].key_part;
266
key_part++, key+= store_length)
269
store_length= key_part->store_length;
271
if (key_part->null_bit)
273
if (*key != test(table->record[0][key_part->null_offset] &
281
if (key_part->key_part_flag & (HA_BLOB_PART | HA_VAR_LENGTH_PART |
284
if (key_part->field->key_cmp(key, key_part->length))
288
length= cmin((uint) (key_end-key), store_length);
289
if (!(key_part->key_type & (FIELDFLAG_NUMBER+FIELDFLAG_BINARY+
292
const CHARSET_INFO * const cs= key_part->field->charset();
293
uint32_t char_length= key_part->length / cs->mbmaxlen;
294
const unsigned char *pos= table->record[0] + key_part->offset;
295
if (length > char_length)
297
char_length= my_charpos(cs, pos, pos + length, char_length);
298
set_if_smaller(char_length, length);
300
if (cs->coll->strnncollsp(cs,
301
(const unsigned char*) key, length,
302
(const unsigned char*) pos, char_length, 0))
306
if (memcmp(key,table->record[0]+key_part->offset,length))
313
unpack key-fields from record to some buffer.
315
This is used mainly to get a good error message. We temporary
316
change the column bitmap so that all columns are readable.
319
to Store value here in an easy to read form
326
void key_unpack(String *to,Table *table,uint32_t idx)
328
KEY_PART_INFO *key_part,*key_part_end;
333
for (key_part=table->key_info[idx].key_part,key_part_end=key_part+
334
table->key_info[idx].key_parts ;
335
key_part < key_part_end;
340
if (key_part->null_bit)
342
if (table->record[0][key_part->null_offset] & key_part->null_bit)
344
to->append(STRING_WITH_LEN("NULL"));
348
if ((field=key_part->field))
350
const CHARSET_INFO * const cs= field->charset();
351
field->val_str(&tmp);
352
if (cs->mbmaxlen > 1 &&
353
table->field[key_part->fieldnr - 1]->field_length !=
357
Prefix key, multi-byte charset.
358
For the columns of type CHAR(N), the above val_str()
359
call will return exactly "key_part->length" bytes,
360
which can break a multi-byte characters in the middle.
361
Align, returning not more than "char_length" characters.
363
uint32_t charpos, char_length= key_part->length / cs->mbmaxlen;
364
if ((charpos= my_charpos(cs, tmp.ptr(),
365
tmp.ptr() + tmp.length(),
366
char_length)) < key_part->length)
370
if (key_part->length < field->pack_length())
371
tmp.length(cmin(tmp.length(),(uint32_t)key_part->length));
375
to->append(STRING_WITH_LEN("???"));
383
Check if key uses field that is marked in passed field bitmap.
387
table Table object with which keys and fields are associated.
388
idx Key to be checked.
389
fields Bitmap of fields to be checked.
392
This function uses Table::tmp_set bitmap so the caller should care
393
about saving/restoring its state if it also uses this bitmap.
396
TRUE Key uses field from bitmap
400
bool is_key_used(Table *table, uint32_t idx, const MY_BITMAP *fields)
402
bitmap_clear_all(&table->tmp_set);
403
table->mark_columns_used_by_index_no_reset(idx, &table->tmp_set);
404
if (bitmap_is_overlapping(&table->tmp_set, fields))
408
If table handler has primary key as part of the index, check that primary
411
if (idx != table->s->primary_key && table->s->primary_key < MAX_KEY &&
412
(table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX))
413
return is_key_used(table, table->s->primary_key, fields);
419
Compare key in row to a given key.
421
@param key_part Key part handler
422
@param key Key to compare to value in table->record[0]
423
@param key_length length of 'key'
426
The return value is SIGN(key_in_row - range_key):
427
- 0 Key is equal to range or 'range' == 0 (no range)
428
- -1 Key is less than range
429
- 1 Key is larger than range
432
int key_cmp(KEY_PART_INFO *key_part, const unsigned char *key, uint32_t key_length)
434
uint32_t store_length;
436
for (const unsigned char *end=key + key_length;
438
key+= store_length, key_part++)
441
store_length= key_part->store_length;
442
if (key_part->null_bit)
444
/* This key part allows null values; NULL is lower than everything */
445
register bool field_is_null= key_part->field->is_null();
446
if (*key) // If range key is null
448
/* the range is expecting a null value */
450
return 1; // Found key is > range
451
/* null -- exact match, go to next key part */
454
else if (field_is_null)
455
return -1; // NULL is less than any value
456
key++; // Skip null byte
459
if ((cmp=key_part->field->key_cmp(key, key_part->length)) < 0)
464
return 0; // Keys are equal
469
Compare two records in index order
472
key Index information
473
rec0 Pointer to table->record[0]
474
first_rec Pointer to record compare with
475
second_rec Pointer to record compare against first_rec
478
This method is set-up such that it can be called directly from the
479
priority queue and it is attempted to be optimised as much as possible
480
since this will be called O(N * log N) times while performing a merge
481
sort in various places in the code.
483
We retrieve the pointer to table->record[0] using the fact that key_parts
484
have an offset making it possible to calculate the start of the record.
485
We need to get the diff to the compared record since none of the records
486
being compared are stored in table->record[0].
488
We first check for NULL values, if there are no NULL values we use
489
a compare method that gets two field pointers and a max length
490
and return the result of the comparison.
493
int key_rec_cmp(void *key, unsigned char *first_rec, unsigned char *second_rec)
495
KEY *key_info= (KEY*)key;
496
uint32_t key_parts= key_info->key_parts, i= 0;
497
KEY_PART_INFO *key_part= key_info->key_part;
498
unsigned char *rec0= key_part->field->ptr - key_part->offset;
499
my_ptrdiff_t first_diff= first_rec - rec0, sec_diff= second_rec - rec0;
504
Field *field= key_part->field;
506
if (key_part->null_bit)
508
/* The key_part can contain NULL values */
509
bool first_is_null= field->is_null_in_record_with_offset(first_diff);
510
bool sec_is_null= field->is_null_in_record_with_offset(sec_diff);
512
NULL is smaller then everything so if first is NULL and the other
513
not then we know that we should return -1 and for the opposite
514
we should return +1. If both are NULL then we call it equality
515
although it is a strange form of equality, we have equally little
516
information of the real value.
521
; /* Fall through, no NULL fields */
527
else if (!sec_is_null)
532
goto next_loop; /* Both were NULL */
535
No null values in the fields
536
We use the virtual method cmp_max with a max length parameter.
537
For most field types this translates into a cmp without
538
max length. The exceptions are the BLOB and VARCHAR field types
539
that take the max length into account.
541
result= field->cmp_max(field->ptr+first_diff, field->ptr+sec_diff,
545
} while (!result && ++i < key_parts);