diff options
author | unknown <monty@mishka.local> | 2004-05-16 14:48:32 +0300 |
---|---|---|
committer | unknown <monty@mishka.local> | 2004-05-16 14:48:32 +0300 |
commit | 70f79563d9fa8ef3cdfef01b8eee95ea6e927147 (patch) | |
tree | 318557f2fbc33b547d96f62b2544f2e9f4174082 /sql | |
parent | c2ce702b3bfff6faa25c6e2b145e41394d544d8d (diff) | |
download | mariadb-git-70f79563d9fa8ef3cdfef01b8eee95ea6e927147.tar.gz |
key_cmp -> key_cmp_if_same
New records_in_range() interface (similar to read_range())
Macros for faster bitmap handling
Simplify read_range() code (#WL1786)
New general key_cmp() function to compare keys
heap/hp_hash.c:
New records_in_range() interface
include/heap.h:
New records_in_range() interface
include/my_base.h:
Moved 'key_range' here so that all table handlers can use it
include/my_bitmap.h:
Make some bitmap functions inline for faster usage in one thread
include/myisam.h:
New records_in_range() interface
include/myisammrg.h:
New records_in_range() interface
myisam/mi_range.c:
New records_in_range() interface
myisam/mi_test2.c:
New records_in_range() interface
myisam/rt_test.c:
New records_in_range() interface
Indentation fixes
myisam/sp_test.c:
New records_in_range() interface
Indentation fixes
myisammrg/myrg_range.c:
New records_in_range() interface
mysys/my_bitmap.c:
Make some bitmap functions inline for faster usage in one thread
sql/examples/ha_example.cc:
New records_in_range() interface
sql/field.cc:
Fixed indentation
sql/ha_berkeley.cc:
New records_in_range() interface
sql/ha_berkeley.h:
New records_in_range() interface
sql/ha_heap.cc:
New records_in_range() interface
sql/ha_heap.h:
New records_in_range() interface
sql/ha_innodb.cc:
New records_in_range() interface
sql/ha_innodb.h:
New records_in_range() interface
sql/ha_isam.cc:
New records_in_range() interface
sql/ha_isam.h:
New records_in_range() interface
sql/ha_myisam.cc:
New records_in_range() interface
sql/ha_myisam.h:
New records_in_range() interface
sql/ha_myisammrg.cc:
New records_in_range() interface
sql/ha_myisammrg.h:
New records_in_range() interface
sql/ha_ndbcluster.cc:
New records_in_range() interface
sql/ha_ndbcluster.h:
New records_in_range() interface
sql/handler.cc:
Simplify read_range() interface:
- Add 'eq_range' to read_range_first
- Remove 'eq_range' parameer from read_range_next()
- Trust values from index_next_same()
- Simplfy compare_key() by moving key_comparision to key.cc (as this code can be reused from other places)
sql/handler.h:
Move key_range to my_base.h to be used by external table handlers
Simplify read_range() interface
New records_in_range() interface
sql/key.cc:
Rename key_cmp() to key_cmp_if_same() to make it more descriptive
Add new key_cmp() function usable from range and handler code.
sql/mysql_priv.h:
Prototypes for new functions
sql/opt_range.cc:
New records_in_range() interface
Simplify cmp_prev()
(We can in 5.0 simplify cmp_next() the same way)
sql/opt_range.h:
Added key_part_info to QUICK_SELECT to be able to use key_cmp() in get_next()
sql/opt_sum.cc:
key_cmp -> key_cmp_if_same
sql/sql_acl.cc:
key_cmp -> key_cmp_if_same
sql/sql_select.cc:
key_cmp -> key_cmp_if_same
Diffstat (limited to 'sql')
-rw-r--r-- | sql/examples/ha_example.cc | 11 | ||||
-rw-r--r-- | sql/field.cc | 9 | ||||
-rw-r--r-- | sql/ha_berkeley.cc | 29 | ||||
-rw-r--r-- | sql/ha_berkeley.h | 7 | ||||
-rw-r--r-- | sql/ha_heap.cc | 34 | ||||
-rw-r--r-- | sql/ha_heap.h | 5 | ||||
-rw-r--r-- | sql/ha_innodb.cc | 41 | ||||
-rw-r--r-- | sql/ha_innodb.h | 6 | ||||
-rw-r--r-- | sql/ha_isam.cc | 23 | ||||
-rw-r--r-- | sql/ha_isam.h | 6 | ||||
-rw-r--r-- | sql/ha_myisam.cc | 26 | ||||
-rw-r--r-- | sql/ha_myisam.h | 6 | ||||
-rw-r--r-- | sql/ha_myisammrg.cc | 16 | ||||
-rw-r--r-- | sql/ha_myisammrg.h | 6 | ||||
-rw-r--r-- | sql/ha_ndbcluster.cc | 25 | ||||
-rw-r--r-- | sql/ha_ndbcluster.h | 7 | ||||
-rw-r--r-- | sql/handler.cc | 66 | ||||
-rw-r--r-- | sql/handler.h | 25 | ||||
-rw-r--r-- | sql/key.cc | 76 | ||||
-rw-r--r-- | sql/mysql_priv.h | 5 | ||||
-rw-r--r-- | sql/opt_range.cc | 81 | ||||
-rw-r--r-- | sql/opt_range.h | 7 | ||||
-rw-r--r-- | sql/opt_sum.cc | 2 | ||||
-rw-r--r-- | sql/sql_acl.cc | 4 | ||||
-rw-r--r-- | sql/sql_select.cc | 4 |
25 files changed, 244 insertions, 283 deletions
diff --git a/sql/examples/ha_example.cc b/sql/examples/ha_example.cc index b8ae5967475..2d17caf1a83 100644 --- a/sql/examples/ha_example.cc +++ b/sql/examples/ha_example.cc @@ -625,14 +625,11 @@ int ha_example::rename_table(const char * from, const char * to) Called from opt_range.cc by check_quick_keys(). */ -ha_rows ha_example::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows ha_example::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { - DBUG_ENTER("ha_example::records_in_range "); - DBUG_RETURN(records); + DBUG_ENTER("ha_example::records_in_range"); + DBUG_RETURN(10); // low number to force index usage } diff --git a/sql/field.cc b/sql/field.cc index edaa29dbaa0..1ad7e039363 100644 --- a/sql/field.cc +++ b/sql/field.cc @@ -4252,12 +4252,13 @@ int Field_string::cmp(const char *a_ptr, const char *b_ptr) like in latin_de 'ae' and 0xe4 */ return field_charset->coll->strnncollsp(field_charset, - (const uchar*) a_ptr, field_length, - (const uchar*) b_ptr, field_length); + (const uchar*) a_ptr, field_length, + (const uchar*) b_ptr, + field_length); } return field_charset->coll->strnncoll(field_charset, - (const uchar*) a_ptr, field_length, - (const uchar*) b_ptr, field_length); + (const uchar*) a_ptr, field_length, + (const uchar*) b_ptr, field_length); } void Field_string::sort_string(char *to,uint length) diff --git a/sql/ha_berkeley.cc b/sql/ha_berkeley.cc index 593b9575fb6..4dde893116f 100644 --- a/sql/ha_berkeley.cc +++ b/sql/ha_berkeley.cc @@ -1542,7 +1542,7 @@ int ha_berkeley::index_next_same(byte * buf, const byte *key, uint keylen) { error=read_row(cursor->c_get(cursor, &last_key, &row, DB_NEXT), (char*) buf, active_index, &row, &last_key, 1); - if (!error && ::key_cmp(table, key, active_index, keylen)) + if (!error && ::key_cmp_if_same(table, key, active_index, keylen)) error=HA_ERR_END_OF_FILE; } DBUG_RETURN(error); @@ -1987,11 +1987,8 @@ double ha_berkeley::scan_time() return rows2double(records/3); } -ha_rows ha_berkeley::records_in_range(int keynr, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows ha_berkeley::records_in_range(uint keynr, key_range *start_key, + key_range *end_key) { DBT key; DB_KEY_RANGE start_range, end_range; @@ -2000,25 +1997,27 @@ ha_rows ha_berkeley::records_in_range(int keynr, DBUG_ENTER("records_in_range"); if ((start_key && kfile->key_range(kfile,transaction, - pack_key(&key, keynr, key_buff, start_key, - start_key_len), - &start_range,0)) || + pack_key(&key, keynr, key_buff, + start_key->key, + start_key->length), + &start_range,0)) || (end_key && kfile->key_range(kfile,transaction, - pack_key(&key, keynr, key_buff, end_key, - end_key_len), + pack_key(&key, keynr, key_buff, + end_key->key, + end_key->length), &end_range,0))) DBUG_RETURN(HA_BERKELEY_RANGE_COUNT); // Better than returning an error /* purecov: inspected */ if (!start_key) - start_pos=0.0; - else if (start_search_flag == HA_READ_KEY_EXACT) + start_pos= 0.0; + else if (start_key->flag == HA_READ_KEY_EXACT) start_pos=start_range.less; else start_pos=start_range.less+start_range.equal; if (!end_key) - end_pos=1.0; - else if (end_search_flag == HA_READ_BEFORE_KEY) + end_pos= 1.0; + else if (end_key->flag == HA_READ_BEFORE_KEY) end_pos=end_range.less; else end_pos=end_range.less+end_range.equal; diff --git a/sql/ha_berkeley.h b/sql/ha_berkeley.h index 88af2326eac..4bc8e3a5777 100644 --- a/sql/ha_berkeley.h +++ b/sql/ha_berkeley.h @@ -143,12 +143,7 @@ class ha_berkeley: public handler int optimize(THD* thd, HA_CHECK_OPT* check_opt); int check(THD* thd, HA_CHECK_OPT* check_opt); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); - + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); int create(const char *name, register TABLE *form, HA_CREATE_INFO *create_info); int delete_table(const char *name); diff --git a/sql/ha_heap.cc b/sql/ha_heap.cc index 12b922e6fc0..9915dc90dc1 100644 --- a/sql/ha_heap.cc +++ b/sql/ha_heap.cc @@ -363,28 +363,20 @@ int ha_heap::rename_table(const char * from, const char * to) return heap_rename(from,to); } -ha_rows ha_heap::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) + +ha_rows ha_heap::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { - KEY *pos=table->key_info+inx; - if (pos->algorithm == HA_KEY_ALG_BTREE) - { - return hp_rb_records_in_range(file, inx, start_key, start_key_len, - start_search_flag, end_key, end_key_len, - end_search_flag); - } - else - { - if (start_key_len != end_key_len || - start_key_len != pos->key_length || - start_search_flag != HA_READ_KEY_EXACT || - end_search_flag != HA_READ_AFTER_KEY) - return HA_POS_ERROR; // Can't only use exact keys - return 10; // Good guess - } + KEY *key=table->key_info+inx; + if (key->algorithm == HA_KEY_ALG_BTREE) + return hp_rb_records_in_range(file, inx, min_key, max_key); + + if (min_key->length != max_key->length || + min_key->length != key->key_length || + min_key->flag != HA_READ_KEY_EXACT || + max_key->flag != HA_READ_AFTER_KEY) + return HA_POS_ERROR; // Can't only use exact keys + return 10; // Good guess } diff --git a/sql/ha_heap.h b/sql/ha_heap.h index 2f849d2574b..f55eda91149 100644 --- a/sql/ha_heap.h +++ b/sql/ha_heap.h @@ -86,10 +86,7 @@ class ha_heap: public handler int disable_indexes(uint mode); int enable_indexes(uint mode); int indexes_are_disabled(void); - ha_rows records_in_range(int inx, const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); int delete_table(const char *from); int rename_table(const char * from, const char * to); int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); diff --git a/sql/ha_innodb.cc b/sql/ha_innodb.cc index 201ec8f183f..ff491a95043 100644 --- a/sql/ha_innodb.cc +++ b/sql/ha_innodb.cc @@ -3925,18 +3925,11 @@ ha_innobase::records_in_range( /*==========================*/ /* out: estimated number of rows */ - int keynr, /* in: index number */ - const mysql_byte* start_key, /* in: start key value of the - range, may also be empty */ - uint start_key_len, /* in: start key val len, may - also be 0 */ - enum ha_rkey_function start_search_flag,/* in: start search condition - e.g., 'greater than' */ - const mysql_byte* end_key, /* in: range end key val, may - also be empty */ - uint end_key_len, /* in: range end key val len, - may also be 0 */ - enum ha_rkey_function end_search_flag)/* in: range end search cond */ + uint keynr, /* in: index number */ + key_range *min_key, /* in: start key value of the + range, may also be 0 */ + key_range *max_key) /* in: range end key val, may + also be 0 */ { row_prebuilt_t* prebuilt = (row_prebuilt_t*) innobase_prebuilt; KEY* key; @@ -3957,12 +3950,6 @@ ha_innobase::records_in_range( DBUG_ENTER("records_in_range"); - /* We do not know if MySQL can call this function before calling - external_lock(). To be safe, update the thd of the current table - handle. */ - - update_thd(current_thd); - prebuilt->trx->op_info = (char*)"estimating records in index range"; /* In case MySQL calls this in the middle of a SELECT query, release @@ -3986,17 +3973,21 @@ ha_innobase::records_in_range( range_start, (byte*) key_val_buff, (ulint)upd_and_key_val_buff_len, index, - (byte*) start_key, - (ulint) start_key_len); + (byte*) (min_key ? min_key->key : + (const mysql_byte*) 0), + (ulint) (min_key ? min_key->length : 0)); row_sel_convert_mysql_key_to_innobase( range_end, (byte*) key_val_buff2, buff2_len, index, - (byte*) end_key, - (ulint) end_key_len); - - mode1 = convert_search_mode_to_innobase(start_search_flag); - mode2 = convert_search_mode_to_innobase(end_search_flag); + (byte*) (max_key ? max_key->key : + (const mysql_byte*) 0), + (ulint) (max_key ? max_key->length : 0)); + + mode1 = convert_search_mode_to_innobase(min_key ? min_key->flag : + HA_READ_KEY_EXACT); + mode2 = convert_search_mode_to_innobase(max_key ? max_key->flag : + HA_READ_KEY_EXACT); n_rows = btr_estimate_n_rows_in_range(index, range_start, mode1, range_end, mode2); diff --git a/sql/ha_innodb.h b/sql/ha_innodb.h index 83a3edc4126..c585fd9c463 100644 --- a/sql/ha_innodb.h +++ b/sql/ha_innodb.h @@ -166,11 +166,7 @@ class ha_innobase: public handler int start_stmt(THD *thd); void position(byte *record); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); ha_rows estimate_number_of_rows(); int create(const char *name, register TABLE *form, diff --git a/sql/ha_isam.cc b/sql/ha_isam.cc index 09c2aeceafc..85ab25a31d9 100644 --- a/sql/ha_isam.cc +++ b/sql/ha_isam.cc @@ -382,18 +382,21 @@ int ha_isam::create(const char *name, register TABLE *form, } +static key_range no_range= { (byte*) 0, 0, HA_READ_KEY_EXACT }; -ha_rows ha_isam::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows ha_isam::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { + /* ISAM checks if 'key' pointer <> 0 to know if there is no range */ + if (!min_key) + min_key= &no_range; + if (!max_key) + max_key= &no_range; return (ha_rows) nisam_records_in_range(file, - inx, - start_key,start_key_len, - start_search_flag, - end_key,end_key_len, - end_search_flag); + (int) inx, + min_key->key, min_key->length, + min_key->flag, + max_key->key, max_key->length, + max_key->flag); } #endif /* HAVE_ISAM */ diff --git a/sql/ha_isam.h b/sql/ha_isam.h index 2c8ec274145..8a887ababde 100644 --- a/sql/ha_isam.h +++ b/sql/ha_isam.h @@ -70,11 +70,7 @@ class ha_isam: public handler void info(uint); int extra(enum ha_extra_function operation); int external_lock(THD *thd, int lock_type); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, diff --git a/sql/ha_myisam.cc b/sql/ha_myisam.cc index 1b24633953d..feac5c51dce 100644 --- a/sql/ha_myisam.cc +++ b/sql/ha_myisam.cc @@ -1515,19 +1515,15 @@ longlong ha_myisam::get_auto_increment() SYNOPSIS records_in_range() inx Index to use - start_key Start of range. Null pointer if from first key - start_key_len Length of start key - start_search_flag Flag if start key should be included or not - end_key End of range. Null pointer if to last key - end_key_len Length of end key - end_search_flag Flag if start key should be included or not + min_key Start of range. Null pointer if from first key + max_key End of range. Null pointer if to last key NOTES - start_search_flag can have one of the following values: + min_key.flag can have one of the following values: HA_READ_KEY_EXACT Include the key in the range HA_READ_AFTER_KEY Don't include key in range - end_search_flag can have one of the following values: + max_key.flag can have one of the following values: HA_READ_BEFORE_KEY Don't include key in range HA_READ_AFTER_KEY Include all 'end_key' values in the range @@ -1538,18 +1534,10 @@ longlong ha_myisam::get_auto_increment() the range. */ -ha_rows ha_myisam::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) +ha_rows ha_myisam::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { - return (ha_rows) mi_records_in_range(file, - inx, - start_key,start_key_len, - start_search_flag, - end_key,end_key_len, - end_search_flag); + return (ha_rows) mi_records_in_range(file, (int) inx, min_key, max_key); } diff --git a/sql/ha_myisam.h b/sql/ha_myisam.h index 206a1c62a2f..77887220903 100644 --- a/sql/ha_myisam.h +++ b/sql/ha_myisam.h @@ -110,11 +110,7 @@ class ha_myisam: public handler int indexes_are_disabled(void); void start_bulk_insert(ha_rows rows); int end_bulk_insert(); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); void update_create_info(HA_CREATE_INFO *create_info); int create(const char *name, TABLE *form, HA_CREATE_INFO *create_info); THR_LOCK_DATA **store_lock(THD *thd, THR_LOCK_DATA **to, diff --git a/sql/ha_myisammrg.cc b/sql/ha_myisammrg.cc index 041d9eaead3..9aa6d039efb 100644 --- a/sql/ha_myisammrg.cc +++ b/sql/ha_myisammrg.cc @@ -199,20 +199,14 @@ void ha_myisammrg::position(const byte *record) ha_store_ptr(ref, ref_length, (my_off_t) position); } -ha_rows ha_myisammrg::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) + +ha_rows ha_myisammrg::records_in_range(uint inx, key_range *min_key, + key_range *max_key) { - return (ha_rows) myrg_records_in_range(file, - inx, - start_key,start_key_len, - start_search_flag, - end_key,end_key_len, - end_search_flag); + return (ha_rows) myrg_records_in_range(file, (int) inx, min_key, max_key); } + void ha_myisammrg::info(uint flag) { MYMERGE_INFO info; diff --git a/sql/ha_myisammrg.h b/sql/ha_myisammrg.h index c0f81a77a1e..fd36c78202d 100644 --- a/sql/ha_myisammrg.h +++ b/sql/ha_myisammrg.h @@ -70,11 +70,7 @@ class ha_myisammrg: public handler int rnd_next(byte *buf); int rnd_pos(byte * buf, byte *pos); void position(const byte *record); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); my_off_t row_position() { return myrg_position(file); } void info(uint); int extra(enum ha_extra_function operation); diff --git a/sql/ha_ndbcluster.cc b/sql/ha_ndbcluster.cc index 3bc322878d1..21056ef4a8f 100644 --- a/sql/ha_ndbcluster.cc +++ b/sql/ha_ndbcluster.cc @@ -2748,22 +2748,19 @@ void ha_ndbcluster::set_dbname(const char *path_name) ha_rows -ha_ndbcluster::records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) -{ - ha_rows records= 10; - KEY* key_info= table->key_info + inx; +ha_ndbcluster::records_in_range(uint inx, key_range *min_key, + key_range *max_key) +{ + ha_rows records= 10; /* Good guess when you don't know anything */ + KEY *key_info= table->key_info + inx; uint key_length= key_info->key_length; DBUG_ENTER("records_in_range"); - DBUG_PRINT("enter", ("inx: %d", inx)); - DBUG_PRINT("enter", ("start_key: %x, start_key_len: %d", start_key, start_key_len)); - DBUG_PRINT("enter", ("start_search_flag: %d", start_search_flag)); - DBUG_PRINT("enter", ("end_key: %x, end_key_len: %d", end_key, end_key_len)); - DBUG_PRINT("enter", ("end_search_flag: %d", end_search_flag)); + DBUG_PRINT("enter", ("inx: %u", inx)); + DBUG_DUMP("start_key", min_key->key, min_key->length); + DBUG_DUMP("end_key", max_key->key, max_key->length); + DBUG_PRINT("enter", ("start_search_flag: %u end_search_flag: %u", + min_key->flag, max_key->flag)); /* Check that start_key_len is equal to @@ -2772,7 +2769,7 @@ ha_ndbcluster::records_in_range(int inx, */ NDB_INDEX_TYPE idx_type= get_index_type(inx); if ((idx_type == UNIQUE_INDEX || idx_type == PRIMARY_KEY_INDEX) && - start_key_len < key_length) + min_key->length < key_length) { DBUG_PRINT("warning", ("Tried to use index which required" "full key length: %d, HA_POS_ERROR", diff --git a/sql/ha_ndbcluster.h b/sql/ha_ndbcluster.h index bd601f39fc4..029ebc11e46 100644 --- a/sql/ha_ndbcluster.h +++ b/sql/ha_ndbcluster.h @@ -121,12 +121,7 @@ class ha_ndbcluster: public handler } double scan_time(); - ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag); - + ha_rows records_in_range(uint inx, key_range *min_key, key_range *max_key); static Ndb* seize_ndb(); static void release_ndb(Ndb* ndb); diff --git a/sql/handler.cc b/sql/handler.cc index 7374242ebf8..f8eca1b93ef 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -1184,7 +1184,7 @@ int handler::index_next_same(byte *buf, const byte *key, uint keylen) int error; if (!(error=index_next(buf))) { - if (key_cmp(table, key, active_index, keylen)) + if (key_cmp_if_same(table, key, active_index, keylen)) { table->status=STATUS_NOT_FOUND; error=HA_ERR_END_OF_FILE; @@ -1388,6 +1388,7 @@ int ha_discover(const char* dbname, const char* name, read_range_first() start_key Start key. Is 0 if no min range end_key End key. Is 0 if no max range + eq_range_arg Set to 1 if start_key == end_key sorted Set to 1 if result should be sorted per key NOTES @@ -1401,11 +1402,12 @@ int ha_discover(const char* dbname, const char* name, int handler::read_range_first(const key_range *start_key, const key_range *end_key, - bool sorted) + bool eq_range_arg, bool sorted) { int result; DBUG_ENTER("handler::read_range_first"); + eq_range= eq_range_arg; end_range= 0; if (end_key) { @@ -1416,7 +1418,6 @@ int handler::read_range_first(const key_range *start_key, } range_key_part= table->key_info[active_index].key_part; - if (!start_key) // Read first record result= index_first(table->record[0]); else @@ -1438,7 +1439,6 @@ int handler::read_range_first(const key_range *start_key, SYNOPSIS read_range_next() - eq_range Set to 1 if start_key == end_key NOTES Record is read into table->record[0] @@ -1449,17 +1449,19 @@ int handler::read_range_first(const key_range *start_key, # Error code */ -int handler::read_range_next(bool eq_range) +int handler::read_range_next() { int result; DBUG_ENTER("handler::read_range_next"); if (eq_range) - result= index_next_same(table->record[0], - end_range->key, - end_range->length); - else - result= index_next(table->record[0]); + { + /* We trust that index_next_same always gives a row in range */ + DBUG_RETURN(index_next_same(table->record[0], + end_range->key, + end_range->length)); + } + result= index_next(table->record[0]); if (result) DBUG_RETURN(result); DBUG_RETURN(compare_key(end_range) <= 0 ? 0 : HA_ERR_END_OF_FILE); @@ -1467,16 +1469,18 @@ int handler::read_range_next(bool eq_range) /* - Compare if found key is over max-value + Compare if found key (in row) is over max-value SYNOPSIS compare_key - range key to compare to row + range range to compare to row. May be 0 for no range NOTES - For this to work, the row must be stored in table->record[0] + See key.cc::key_cmp() for details RETURN + The return value is SIGN(key_in_row - range_key): + 0 Key is equal to range or 'range' == 0 (no range) -1 Key is less than range 1 Key is larger than range @@ -1484,37 +1488,11 @@ int handler::read_range_next(bool eq_range) int handler::compare_key(key_range *range) { - KEY_PART_INFO *key_part= range_key_part; - uint store_length; - + int cmp; if (!range) return 0; // No max range - - for (const char *key= (const char*) range->key, *end=key+range->length; - key < end; - key+= store_length, key_part++) - { - int cmp; - store_length= key_part->store_length; - if (key_part->null_bit) - { - if (*key) - { - if (!key_part->field->is_null()) - return 1; - continue; - } - else if (key_part->field->is_null()) - return 0; - key++; // Skip null byte - store_length--; - } - if ((cmp=key_part->field->key_cmp((byte*) key, key_part->length)) < 0) - return -1; - if (cmp > 0) - return 1; - } - return key_compare_result_on_equal; + cmp= key_cmp(range_key_part, range->key, range->length); + if (!cmp) + cmp= key_compare_result_on_equal; + return cmp; } - - diff --git a/sql/handler.h b/sql/handler.h index 4f721a01412..bbeefa7c916 100644 --- a/sql/handler.h +++ b/sql/handler.h @@ -234,14 +234,6 @@ typedef struct st_ha_check_opt } HA_CHECK_OPT; -typedef struct st_key_range -{ - const byte *key; - uint length; - enum ha_rkey_function flag; -} key_range; - - class handler :public Sql_alloc { protected: @@ -268,6 +260,7 @@ public: key_range save_end_range, *end_range; KEY_PART_INFO *range_key_part; int key_compare_result_on_equal; + bool eq_range; uint errkey; /* Last dup key */ uint sortkey, key_used_on_scan; @@ -330,13 +323,14 @@ public: return (my_errno=HA_ERR_WRONG_COMMAND); } virtual int read_range_first(const key_range *start_key, - const key_range *end_key, - bool sorted); - virtual int read_range_next(bool eq_range); + const key_range *end_key, + bool eq_range, bool sorted); + virtual int read_range_next(); int compare_key(key_range *range); virtual int ft_init() { return -1; } - virtual FT_INFO *ft_init_ext(uint flags,uint inx,const byte *key, uint keylen) + virtual FT_INFO *ft_init_ext(uint flags,uint inx,const byte *key, + uint keylen) { return NULL; } virtual int ft_read(byte *buf) { return -1; } virtual int rnd_init(bool scan=1)=0; @@ -345,11 +339,8 @@ public: virtual int rnd_pos(byte * buf, byte *pos)=0; virtual int read_first_row(byte *buf, uint primary_key); virtual int restart_rnd_next(byte *buf, byte *pos); - virtual ha_rows records_in_range(int inx, - const byte *start_key,uint start_key_len, - enum ha_rkey_function start_search_flag, - const byte *end_key,uint end_key_len, - enum ha_rkey_function end_search_flag) + virtual ha_rows records_in_range(uint inx, key_range *min_key, + key_range *max_key) { return (ha_rows) 10; } virtual void position(const byte *record)=0; virtual my_off_t row_position() { return HA_OFFSET_ERROR; } diff --git a/sql/key.cc b/sql/key.cc index a2c3b2c8989..9425a368669 100644 --- a/sql/key.cc +++ b/sql/key.cc @@ -156,9 +156,28 @@ void key_restore(TABLE *table,byte *key,uint idx,uint key_length) } /* key_restore */ - /* Compare if a key has changed */ +/* + Compare if a key has changed + + SYNOPSIS + key_cmp_if_same() + table TABLE + key key to compare to row + idx Index used + key_length Length of key + + NOTES + In theory we could just call field->cmp() for all field types, + but as we are only interested if a key has changed (not if the key is + larger or smaller than the previous value) we can do things a bit + faster by using memcmp() instead. + + RETURN + 0 If key is equal + 1 Key has changed +*/ -int key_cmp(TABLE *table,const byte *key,uint idx,uint key_length) +bool key_cmp_if_same(TABLE *table,const byte *key,uint idx,uint key_length) { uint length; KEY_PART_INFO *key_part; @@ -281,3 +300,56 @@ bool check_if_key_used(TABLE *table, uint idx, List<Item> &fields) return check_if_key_used(table, table->primary_key, fields); return 0; } + + +/* + Compare key in row to a given key + + SYNOPSIS + key_cmp() + key_part Key part handler + key Key to compare to value in table->record[0] + key_length length of 'key' + + RETURN + The return value is SIGN(key_in_row - range_key): + + 0 Key is equal to range or 'range' == 0 (no range) + -1 Key is less than range + 1 Key is larger than range +*/ + +int key_cmp(KEY_PART_INFO *key_part, const byte *key, uint key_length) +{ + uint store_length; + + for (const byte *end=key + key_length; + key < end; + key+= store_length, key_part++) + { + int cmp; + store_length= key_part->store_length; + if (key_part->null_bit) + { + /* This key part allows null values; NULL is lower than everything */ + register bool field_is_null= key_part->field->is_null(); + if (*key) // If range key is null + { + /* the range is expecting a null value */ + if (!field_is_null) + return 1; // Found key is > range + /* null -- exact match, go to next key part */ + continue; + } + else if (field_is_null) + return -1; // NULL is less than any value + key++; // Skip null byte + store_length--; + } + if ((cmp=key_part->field->key_cmp((byte*) key, key_part->length)) < 0) + return -1; + if (cmp > 0) + return 1; + } + return 0; // Keys are equal +} diff --git a/sql/mysql_priv.h b/sql/mysql_priv.h index 8cae95e5070..14bd16332ef 100644 --- a/sql/mysql_priv.h +++ b/sql/mysql_priv.h @@ -754,11 +754,12 @@ void mysql_print_status(THD *thd); int find_ref_key(TABLE *form,Field *field, uint *offset); void key_copy(byte *key,TABLE *form,uint index,uint key_length); void key_restore(TABLE *form,byte *key,uint index,uint key_length); -int key_cmp(TABLE *form,const byte *key,uint index,uint key_length); +bool key_cmp_if_same(TABLE *form,const byte *key,uint index,uint key_length); void key_unpack(String *to,TABLE *form,uint index); bool check_if_key_used(TABLE *table, uint idx, List<Item> &fields); -bool init_errmessage(void); +int key_cmp(KEY_PART_INFO *key_part, const byte *key, uint key_length); +bool init_errmessage(void); void sql_perror(const char *message); void sql_print_error(const char *format,...) __attribute__ ((format (printf, 1, 2))); diff --git a/sql/opt_range.cc b/sql/opt_range.cc index 1e7bf90738c..3cc64c35223 100644 --- a/sql/opt_range.cc +++ b/sql/opt_range.cc @@ -1800,8 +1800,8 @@ SEL_ARG * SEL_ARG::insert(SEL_ARG *key) { SEL_ARG *element,**par,*last_element; - LINT_INIT(par); LINT_INIT(last_element); + for (element= this; element != &null_element ; ) { last_element=element; @@ -2296,26 +2296,32 @@ check_quick_keys(PARAM *param,uint idx,SEL_ARG *key_tree, { if (tmp_min_flag & GEOM_FLAG) { - tmp= param->table->file-> - records_in_range((int) keynr, (byte*)(param->min_key), - min_key_length, - (ha_rkey_function)(tmp_min_flag ^ GEOM_FLAG), - (byte *)NullS, 0, HA_READ_KEY_EXACT); + key_range min_range; + min_range.key= (byte*) param->min_key; + min_range.length= min_key_length; + /* In this case tmp_min_flag contains the handler-read-function */ + min_range.flag= (ha_rkey_function) (tmp_min_flag ^ GEOM_FLAG); + + tmp= param->table->file->records_in_range(keynr, &min_range, + (key_range*) 0); } else { - tmp=param->table->file-> - records_in_range((int) keynr, - (byte*) (!min_key_length ? NullS : - param->min_key), - min_key_length, - tmp_min_flag & NEAR_MIN ? - HA_READ_AFTER_KEY : HA_READ_KEY_EXACT, - (byte*) (!max_key_length ? NullS : - param->max_key), - max_key_length, - (tmp_max_flag & NEAR_MAX ? - HA_READ_BEFORE_KEY : HA_READ_AFTER_KEY)); + key_range min_range, max_range; + + min_range.key= (byte*) param->min_key; + min_range.length= min_key_length; + min_range.flag= (tmp_min_flag & NEAR_MIN ? HA_READ_AFTER_KEY : + HA_READ_KEY_EXACT); + max_range.key= param->max_key; + max_range.length= max_key_length; + max_range.flag= (tmp_max_flag & NEAR_MAX ? + HA_READ_BEFORE_KEY : HA_READ_AFTER_KEY); + tmp=param->table->file->records_in_range(keynr, + (min_key_length ? &min_range : + (key_range*) 0), + (max_key_length ? &max_range : + (key_range*) 0)); } } end: @@ -2604,7 +2610,7 @@ int QUICK_SELECT::get_next() if (range) { // Already read through key - result= file->read_range_next(test(range->flag & EQ_RANGE)); + result= file->read_range_next(); if (result != HA_ERR_END_OF_FILE) DBUG_RETURN(result); } @@ -2628,6 +2634,7 @@ int QUICK_SELECT::get_next() result= file->read_range_first(range->min_length ? &start_key : 0, range->max_length ? &end_key : 0, + test(range->flag & EQ_RANGE), sorted); if (range->flag == (UNIQUE_RANGE | EQ_RANGE)) range=0; // Stop searching @@ -2815,40 +2822,14 @@ int QUICK_SELECT_DESC::get_next() int QUICK_SELECT_DESC::cmp_prev(QUICK_RANGE *range_arg) { + int cmp; if (range_arg->flag & NO_MIN_RANGE) return 0; /* key can't be to small */ - KEY_PART *key_part = key_parts; - uint store_length; - - for (char *key = range_arg->min_key, *end = key + range_arg->min_length; - key < end; - key += store_length, key_part++) - { - int cmp; - store_length= key_part->store_length; - if (key_part->null_bit) - { - // this key part allows null values; NULL is lower than everything else - if (*key) - { - // the range is expecting a null value - if (!key_part->field->is_null()) - return 0; // not null -- still inside the range - continue; // null -- exact match, go to next key part - } - else if (key_part->field->is_null()) - return 1; // null -- outside the range - key++; - store_length--; - } - if ((cmp = key_part->field->key_cmp((byte*) key, - key_part->length)) > 0) - return 0; - if (cmp < 0) - return 1; - } - return (range_arg->flag & NEAR_MIN) ? 1 : 0; // Exact match + cmp= key_cmp(key_part_info, range_arg->min_key, range_arg->min_length); + if (cmp > 0 || cmp == 0 && !(range_arg->flag & NEAR_MIN)) + return 0; + return 1; // outside of range } diff --git a/sql/opt_range.h b/sql/opt_range.h index 2df9d93e1ef..2072ded15d1 100644 --- a/sql/opt_range.h +++ b/sql/opt_range.h @@ -80,13 +80,18 @@ public: MEM_ROOT alloc; KEY_PART *key_parts; + KEY_PART_INFO *key_part_info; ha_rows records; double read_time; QUICK_SELECT(THD *thd, TABLE *table,uint index_arg,bool no_alloc=0); virtual ~QUICK_SELECT(); void reset(void) { next=0; it.rewind(); } - int init() { return error=file->index_init(index); } + int init() + { + key_part_info= head->key_info[index].key_part; + return error=file->index_init(index); + } virtual int get_next(); virtual bool reverse_sorted() { return 0; } bool unique_key_range(); diff --git a/sql/opt_sum.cc b/sql/opt_sum.cc index 4fdcd093132..8c1cd9ce1cb 100644 --- a/sql/opt_sum.cc +++ b/sql/opt_sum.cc @@ -716,7 +716,7 @@ static bool find_key_for_maxmin(bool max_fl, TABLE_REF *ref, static int reckey_in_range(bool max_fl, TABLE_REF *ref, Field* field, COND *cond, uint range_fl, uint prefix_len) { - if (key_cmp(field->table, ref->key_buff, ref->key, prefix_len)) + if (key_cmp_if_same(field->table, ref->key_buff, ref->key, prefix_len)) return 1; if (!cond || (range_fl & (max_fl ? NO_MIN_RANGE : NO_MAX_RANGE))) return 0; diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index 9c7fe3e2993..447a5d9ae80 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -1820,7 +1820,7 @@ GRANT_TABLE::GRANT_TABLE(TABLE *form, TABLE *col_privs) } my_hash_insert(&hash_columns, (byte *) mem_check); } while (!col_privs->file->index_next(col_privs->record[0]) && - !key_cmp(col_privs,key,0,key_len)); + !key_cmp_if_same(col_privs,key,0,key_len)); } } @@ -2043,7 +2043,7 @@ static int replace_column_table(GRANT_TABLE *g_t, } } } while (!table->file->index_next(table->record[0]) && - !key_cmp(table,key,0,key_length)); + !key_cmp_if_same(table,key,0,key_length)); } end: diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 5db4a0042ea..5e2525f046a 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -6162,8 +6162,8 @@ join_read_prev_same(READ_RECORD *info) if ((error=table->file->index_prev(table->record[0]))) return report_error(table, error); - if (key_cmp(table, tab->ref.key_buff, tab->ref.key, - tab->ref.key_length)) + if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key, + tab->ref.key_length)) { table->status=STATUS_NOT_FOUND; error= -1; |