Commit 4e138572 authored by unknown's avatar unknown
Browse files

Bug#20086: Can't get data from key partitioned tables with VARCHAR key

The problem appeared because the same values produced different hash
during INSERT and SELECT for VARCHAR data type.
Fix:
VARCHAR required special treatment to avoid hashing of length bytes
(leftmost one or two bytes) as well as trailing bytes beyond real length,
which could contain garbage. Fix is done by introducing hash() - new method
in the Field class.


mysql-test/r/partition_innodb.result:
  Adding test case
mysql-test/r/partition_pruning.result:
  Fixing test results (results differ due to changes in hash function)
mysql-test/t/partition_innodb.test:
  Adding test case
sql/field.cc:
  Adding generic hash() method, and a special
  method for VARCHAR.
sql/field.h:
  Adding prototypes for new methods
sql/key.cc:
  Mark columns for write before executinf of set_key_image().
  Thanks for Mikael for suggesting this fix.
sql/sql_partition.cc:
  Removing old hash code.
  Using new methid field->hash() instead.
parent 9addb8fd
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -92,3 +92,18 @@ DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t0_aux;
DROP TABLE IF EXISTS t0_definition;
DROP TABLE IF EXISTS t0_template;
create table t1 (id varchar(64) primary key) engine=innodb
partition by key(id) partitions 5;
insert into t1 values ('a');
insert into t1 values ('aa');
insert into t1 values ('aaa');
select * from t1 where id = 'a';
id
a
select * from t1 where id = 'aa';
id
aa
select * from t1 where id = 'aaa';
id
aaa
drop table t1;
+1 −1
Original line number Diff line number Diff line
@@ -31,7 +31,7 @@ id select_type table partitions type possible_keys key key_len ref rows Extra
1	SIMPLE	t2	p0,p1	ALL	NULL	NULL	NULL	NULL	3	Using where
explain partitions select * from t2 where a=1 and b=1;
id	select_type	table	partitions	type	possible_keys	key	key_len	ref	rows	Extra
1	SIMPLE	t2	p0	ALL	NULL	NULL	NULL	NULL	3	Using where
1	SIMPLE	t2	p0	ALL	NULL	NULL	NULL	NULL	2	Using where
create table t3 (
a int
)
+12 −0
Original line number Diff line number Diff line
@@ -66,3 +66,15 @@ DROP TABLE IF EXISTS t0_definition;
DROP TABLE IF EXISTS t0_template;
--enable_warnings

#
# Bug#20086: Can't get data from key partitioned tables with VARCHAR key
#
create table t1 (id varchar(64) primary key) engine=innodb
partition by key(id) partitions 5;
insert into t1 values ('a');
insert into t1 values ('aa');
insert into t1 values ('aaa');
select * from t1 where id = 'a';
select * from t1 where id = 'aa';
select * from t1 where id = 'aaa';
drop table t1;
+30 −0
Original line number Diff line number Diff line
@@ -1243,6 +1243,21 @@ uint Field::offset()
}


void Field::hash(ulong *nr, ulong *nr2)
{
  if (is_null())
  {
    *nr^= (*nr << 1) | 1;
  }
  else
  {
    uint len= pack_length();
    CHARSET_INFO *cs= charset();
    cs->coll->hash_sort(cs, (uchar*) ptr, len, nr, nr2);
  }
}


void Field::copy_from_tmp(int row_offset)
{
  memcpy(ptr,ptr+row_offset,pack_length());
@@ -6923,6 +6938,21 @@ uint Field_varstring::is_equal(create_field *new_field)
}


void Field_varstring::hash(ulong *nr, ulong *nr2)
{
  if (is_null())
  {
    *nr^= (*nr << 1) | 1;
  }
  else
  {
    uint len=  length_bytes == 1 ? (uint) (uchar) *ptr : uint2korr(ptr);
    CHARSET_INFO *cs= charset();
    cs->coll->hash_sort(cs, (uchar*) ptr + length_bytes, len, nr, nr2);
  }
}


/****************************************************************************
** blob type
** A blob is saved as a length and a pointer. The length is stored in the
+3 −0
Original line number Diff line number Diff line
@@ -351,6 +351,8 @@ class Field
    return field_length / charset()->mbmaxlen;
  }

  /* Hash value */
  virtual void hash(ulong *nr, ulong *nr2);
  friend bool reopen_table(THD *,struct st_table *,bool);
  friend int cre_myisam(my_string name, register TABLE *form, uint options,
			ulonglong auto_increment_value);
@@ -1120,6 +1122,7 @@ class Field_varstring :public Field_longstr {
                       char *new_ptr, uchar *new_null_ptr,
                       uint new_null_bit);
  uint is_equal(create_field *new_field);
  void hash(ulong *nr, ulong *nr2);
};


Loading