Commit bba73a09 authored by unknown's avatar unknown

Bug #12796: Record lost in HEAP table

Two handler objects were present, one was used for an insert and the other for a select
The state of the statistics was local to the handler object and thus the other handler
object didn't notice the insert.
Fix included:
1) Add a new variable key_stat_version added to whenever statistics was considered in need
of update (previously key_stats_ok= FALSE in those places)
2) Add a new handler variable key_stat_version assigned whenever key_stats_ok= TRUE was set
previously
3) Fix records_in_range to return records if records <= 1
4) Fix records_in_range to add 2 to rec_per_key to ensure we don't specify 0 or 1 when it isn't
and thus invoking incorrect optimisations.
5) Fix unique key handling for HEAP table in records_in_range

parent 0e878d7e
...@@ -104,6 +104,7 @@ int heap_create(const char *name, uint keys, HP_KEYDEF *keydef, ...@@ -104,6 +104,7 @@ int heap_create(const char *name, uint keys, HP_KEYDEF *keydef,
DBUG_RETURN(1); DBUG_RETURN(1);
} }
share->keydef= (HP_KEYDEF*) (share + 1); share->keydef= (HP_KEYDEF*) (share + 1);
share->key_stat_version= 1;
keyseg= (HA_KEYSEG*) (share->keydef + keys); keyseg= (HA_KEYSEG*) (share->keydef + keys);
init_block(&share->block, reclength + 1, min_records, max_records); init_block(&share->block, reclength + 1, min_records, max_records);
/* Fix keys */ /* Fix keys */
......
...@@ -136,6 +136,7 @@ typedef struct st_heap_share ...@@ -136,6 +136,7 @@ typedef struct st_heap_share
HP_KEYDEF *keydef; HP_KEYDEF *keydef;
ulong min_records,max_records; /* Params to open */ ulong min_records,max_records; /* Params to open */
ulong data_length,index_length,max_table_size; ulong data_length,index_length,max_table_size;
uint key_stat_version; /* version to indicate insert/delete */
uint records; /* records */ uint records; /* records */
uint blength; /* records rounded up to 2^n */ uint blength; /* records rounded up to 2^n */
uint deleted; /* Deleted records in database */ uint deleted; /* Deleted records in database */
......
...@@ -182,7 +182,7 @@ SELECT * FROM t1 WHERE a=NULL; ...@@ -182,7 +182,7 @@ SELECT * FROM t1 WHERE a=NULL;
a b a b
explain SELECT * FROM t1 WHERE a IS NULL; explain SELECT * FROM t1 WHERE a IS NULL;
id select_type table type possible_keys key key_len ref rows Extra id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 ref a a 5 const 1 Using where 1 SIMPLE t1 ref a a 5 const 2 Using where
SELECT * FROM t1 WHERE a<=>NULL; SELECT * FROM t1 WHERE a<=>NULL;
a b a b
NULL 99 NULL 99
...@@ -296,3 +296,13 @@ insert into t1 values ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd ...@@ -296,3 +296,13 @@ insert into t1 values ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd
insert into t1 values ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); insert into t1 values ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
ERROR 23000: Duplicate entry 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl' for key 1 ERROR 23000: Duplicate entry 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijkl' for key 1
drop table t1; drop table t1;
CREATE TABLE t1 (a int, key(a)) engine=heap;
insert delayed into t1 values (0);
delete from t1;
select * from t1;
a
insert delayed into t1 values (0), (1);
select * from t1 where a = 0;
a
0
drop table t1;
...@@ -234,4 +234,15 @@ insert into t1 values ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd ...@@ -234,4 +234,15 @@ insert into t1 values ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd
insert into t1 values ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"); insert into t1 values ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
drop table t1; drop table t1;
#
# Bug 12796: Record doesn't show when selecting through index
#
CREATE TABLE t1 (a int, key(a)) engine=heap;
insert delayed into t1 values (0);
delete from t1;
select * from t1;
insert delayed into t1 values (0), (1);
select * from t1 where a = 0;
drop table t1;
# End of 4.1 tests # End of 4.1 tests
#
# Test of heap tables.
#
--disable_warnings
drop table if exists t1;
--enable_warnings
#
# Bug 12796: Record doesn't show when selecting through index
#
CREATE TABLE t1 (a int, key(a)) engine=heap;
insert delayed into t1 values (0);
delete from t1;
select * from t1;
insert delayed into t1 values (0), (1);
select * from t1 where a = 0;
drop table t1;
# End of 4.1 tests
...@@ -68,7 +68,7 @@ int ha_heap::open(const char *name, int mode, uint test_if_locked) ...@@ -68,7 +68,7 @@ int ha_heap::open(const char *name, int mode, uint test_if_locked)
ha_heap::info(), which is always called before key statistics are ha_heap::info(), which is always called before key statistics are
used. used.
*/ */
key_stats_ok= FALSE; key_stat_version= file->s->key_stat_version-1;
} }
return (file ? 0 : 1); return (file ? 0 : 1);
} }
...@@ -113,15 +113,22 @@ void ha_heap::update_key_stats() ...@@ -113,15 +113,22 @@ void ha_heap::update_key_stats()
if (!key->rec_per_key) if (!key->rec_per_key)
continue; continue;
if (key->algorithm != HA_KEY_ALG_BTREE) if (key->algorithm != HA_KEY_ALG_BTREE)
{
if (key->flags & HA_NOSAME)
key->rec_per_key[key->key_parts-1]= 1;
else
{ {
ha_rows hash_buckets= file->s->keydef[i].hash_buckets; ha_rows hash_buckets= file->s->keydef[i].hash_buckets;
key->rec_per_key[key->key_parts-1]= uint no_records= hash_buckets ? file->s->records/hash_buckets : 2;
hash_buckets ? file->s->records/hash_buckets : 0; if (no_records < 2)
no_records= 2;
key->rec_per_key[key->key_parts-1]= no_records;
}
} }
} }
records_changed= 0; records_changed= 0;
/* At the end of update_key_stats() we can proudly claim they are OK. */ /* At the end of update_key_stats() we can proudly claim they are OK. */
key_stats_ok= TRUE; key_stat_version= file->s->key_stat_version;
} }
int ha_heap::write_row(byte * buf) int ha_heap::write_row(byte * buf)
...@@ -135,7 +142,7 @@ int ha_heap::write_row(byte * buf) ...@@ -135,7 +142,7 @@ int ha_heap::write_row(byte * buf)
res= heap_write(file,buf); res= heap_write(file,buf);
if (!res && ++records_changed*HEAP_STATS_UPDATE_THRESHOLD > if (!res && ++records_changed*HEAP_STATS_UPDATE_THRESHOLD >
file->s->records) file->s->records)
key_stats_ok= FALSE; file->s->key_stat_version++;
return res; return res;
} }
...@@ -148,7 +155,7 @@ int ha_heap::update_row(const byte * old_data, byte * new_data) ...@@ -148,7 +155,7 @@ int ha_heap::update_row(const byte * old_data, byte * new_data)
res= heap_update(file,old_data,new_data); res= heap_update(file,old_data,new_data);
if (!res && ++records_changed*HEAP_STATS_UPDATE_THRESHOLD > if (!res && ++records_changed*HEAP_STATS_UPDATE_THRESHOLD >
file->s->records) file->s->records)
key_stats_ok= FALSE; file->s->key_stat_version++;
return res; return res;
} }
...@@ -159,7 +166,7 @@ int ha_heap::delete_row(const byte * buf) ...@@ -159,7 +166,7 @@ int ha_heap::delete_row(const byte * buf)
res= heap_delete(file,buf); res= heap_delete(file,buf);
if (!res && table->tmp_table == NO_TMP_TABLE && if (!res && table->tmp_table == NO_TMP_TABLE &&
++records_changed*HEAP_STATS_UPDATE_THRESHOLD > file->s->records) ++records_changed*HEAP_STATS_UPDATE_THRESHOLD > file->s->records)
key_stats_ok= FALSE; file->s->key_stat_version++;
return res; return res;
} }
...@@ -277,7 +284,7 @@ void ha_heap::info(uint flag) ...@@ -277,7 +284,7 @@ void ha_heap::info(uint flag)
have to update the key statistics. Hoping that a table lock is now have to update the key statistics. Hoping that a table lock is now
in place. in place.
*/ */
if (! key_stats_ok) if (key_stat_version != file->s->key_stat_version)
update_key_stats(); update_key_stats();
} }
...@@ -290,7 +297,7 @@ int ha_heap::delete_all_rows() ...@@ -290,7 +297,7 @@ int ha_heap::delete_all_rows()
{ {
heap_clear(file); heap_clear(file);
if (table->tmp_table == NO_TMP_TABLE) if (table->tmp_table == NO_TMP_TABLE)
key_stats_ok= FALSE; file->s->key_stat_version++;
return 0; return 0;
} }
...@@ -450,11 +457,16 @@ ha_rows ha_heap::records_in_range(uint inx, key_range *min_key, ...@@ -450,11 +457,16 @@ ha_rows ha_heap::records_in_range(uint inx, key_range *min_key,
max_key->flag != HA_READ_AFTER_KEY) max_key->flag != HA_READ_AFTER_KEY)
return HA_POS_ERROR; // Can only use exact keys return HA_POS_ERROR; // Can only use exact keys
else else
{
if (records <= 1)
return records;
else
{ {
/* Assert that info() did run. We need current statistics here. */ /* Assert that info() did run. We need current statistics here. */
DBUG_ASSERT(key_stats_ok); DBUG_ASSERT(key_stat_version == file->s->key_stat_version);
return key->rec_per_key[key->key_parts-1]; return key->rec_per_key[key->key_parts-1];
} }
}
} }
......
...@@ -29,10 +29,10 @@ class ha_heap: public handler ...@@ -29,10 +29,10 @@ class ha_heap: public handler
key_map btree_keys; key_map btree_keys;
/* number of records changed since last statistics update */ /* number of records changed since last statistics update */
uint records_changed; uint records_changed;
bool key_stats_ok; uint key_stat_version;
public: public:
ha_heap(TABLE *table): handler(table), file(0), records_changed(0), ha_heap(TABLE *table): handler(table), file(0), records_changed(0),
key_stats_ok(0) {} key_stat_version(0) {}
~ha_heap() {} ~ha_heap() {}
const char *table_type() const { return "HEAP"; } const char *table_type() const { return "HEAP"; }
const char *index_type(uint inx) const char *index_type(uint inx)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment