Skip to content

Instantly share code, notes, and snippets.

@spetrunia
Created March 12, 2024 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spetrunia/4e8d8bdded2f5c698bf3ff385bb272cd to your computer and use it in GitHub Desktop.
Save spetrunia/4e8d8bdded2f5c698bf3ff385bb272cd to your computer and use it in GitHub Desktop.
diff --git a/sql/field.h b/sql/field.h
index d5c1b049ff1..b766665e002 100644
--- a/sql/field.h
+++ b/sql/field.h
@@ -1636,9 +1636,12 @@ class Field: public Value_source
{ return max_length;}
virtual bool is_packable() const { return false; }
- uint offset(const uchar *record) const
+ uint offset() const
{
- return (uint) (ptr - record);
+ ptrdiff_t diff= table->s->field[field_index]->ptr -
+ table->s->default_values;
+ DBUG_ASSERT(diff >= 0 && diff < (ptrdiff_t)table->s->reclength);
+ return (uint)diff;
}
void copy_from_tmp(int offset);
uint fill_cache_field(struct st_cache_field *copy);
diff --git a/sql/item_sum.cc b/sql/item_sum.cc
index bcaf229dd15..03413450962 100644
--- a/sql/item_sum.cc
+++ b/sql/item_sum.cc
@@ -3585,8 +3585,7 @@ int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
if (!field)
continue;
- uint offset= (field->offset(field->table->record[0]) -
- field->table->s->null_bytes);
+ uint offset= (field->offset() - field->table->s->null_bytes);
int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
if (res)
return res;
@@ -3643,8 +3642,7 @@ int group_concat_key_cmp_with_distinct_with_nulls(void* arg,
if (field->is_null_in_record((uchar*)key2_arg))
return 1;
- uint offset= (field->offset(field->table->record[0]) -
- field->table->s->null_bytes);
+ uint offset= (field->offset() - field->table->s->null_bytes);
int res= field->cmp(key1 + offset, key2 + offset);
if (res)
return res;
@@ -3693,8 +3691,7 @@ int group_concat_key_cmp_with_order(void* arg, const void* key1,
if (!field)
continue;
- uint offset= (field->offset(field->table->record[0]) -
- field->table->s->null_bytes);
+ uint offset= (field->offset() - field->table->s->null_bytes);
int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
if (res)
return ((*order_item)->direction == ORDER::ORDER_ASC) ? res : -res;
@@ -3759,8 +3756,7 @@ int group_concat_key_cmp_with_order_with_nulls(void *arg, const void *key1_arg,
if (field->is_null_in_record((uchar*)key2_arg))
return ((*order_item)->direction == ORDER::ORDER_ASC) ? 1 : -1;
- uint offset= (field->offset(field->table->record[0]) -
- field->table->s->null_bytes);
+ uint offset= (field->offset() - field->table->s->null_bytes);
int res= field->cmp((uchar*)key1 + offset, (uchar*)key2 + offset);
if (res)
return ((*order_item)->direction == ORDER::ORDER_ASC) ? res : -res;
@@ -3866,8 +3862,7 @@ int dump_leaf_key(void* key_arg, element_count count __attribute__((unused)),
Field *field= (*arg)->get_tmp_table_field();
if (field)
{
- uint offset= (field->offset(field->table->record[0]) -
- table->s->null_bytes);
+ uint offset= (field->offset() - table->s->null_bytes);
DBUG_ASSERT(offset < table->s->reclength);
res= item->get_str_from_field(*arg, field, &tmp, key,
offset + item->get_null_bytes());
diff --git a/sql/key.cc b/sql/key.cc
index 93b172c3e22..fddd2028aaf 100644
--- a/sql/key.cc
+++ b/sql/key.cc
@@ -49,21 +49,19 @@
key_length is set to length of key before (not including) field
*/
+//psergey-KABOOM!
int find_ref_key(KEY *key, uint key_count, uchar *record, Field *field,
uint *key_length, uint *keypart)
{
int i;
KEY *key_info;
- uint fieldpos;
- fieldpos= field->offset(record);
-
- /* Test if some key starts as fieldpos */
+ /* Test if some key starts with field */
for (i= 0, key_info= key ;
i < (int) key_count ;
i++, key_info++)
{
- if (key_info->key_part[0].offset == fieldpos &&
+ if (key_info->key_part[0].fieldnr == field->field_index + 1 &&
key_info->key_part[0].field->type() != MYSQL_TYPE_BIT)
{ /* Found key. Calc keylength */
*key_length= *keypart= 0;
@@ -71,7 +69,7 @@ int find_ref_key(KEY *key, uint key_count, uchar *record, Field *field,
}
}
- /* Test if some key contains fieldpos */
+ /* Test if some key contains field */
for (i= 0, key_info= key;
i < (int) key_count ;
i++, key_info++)
@@ -83,7 +81,7 @@ int find_ref_key(KEY *key, uint key_count, uchar *record, Field *field,
j < key_info->user_defined_key_parts ;
j++, key_part++)
{
- if (key_part->offset == fieldpos &&
+ if (key_part->fieldnr == field->field_index + 1 &&
key_part->field->type() != MYSQL_TYPE_BIT)
{
*keypart= j;
diff --git a/sql/opt_subselect.cc b/sql/opt_subselect.cc
index 6d9b40cc063..867b0470387 100644
--- a/sql/opt_subselect.cc
+++ b/sql/opt_subselect.cc
@@ -4987,7 +4987,7 @@ SJ_TMP_TABLE::create_sj_weedout_tmp_table(THD *thd)
{
key_part_info->null_bit=0;
key_part_info->field= field;
- key_part_info->offset= field->offset(table->record[0]);
+ key_part_info->offset= field->offset();
key_part_info->length= (uint16) field->key_length();
key_part_info->type= (uint8) field->key_type();
key_part_info->key_type = FIELDFLAG_BINARY;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index 6f2797b6b39..0e9c96b8e45 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -21777,7 +21777,7 @@ bool Create_tmp_table::finalize(THD *thd,
m_key_part_info->fieldnr= field->field_index + 1;
if (cur_group == m_group)
field->key_start.set_bit(0);
- m_key_part_info->offset= field->offset(table->record[0]);
+ m_key_part_info->offset= field->ptr - table->record[0];
m_key_part_info->length= (uint16) field->key_length();
m_key_part_info->type= (uint8) field->key_type();
m_key_part_info->key_type =
@@ -21941,8 +21941,11 @@ bool Create_tmp_table::finalize(THD *thd,
m_key_part_info->null_bit= (*reg_field)->null_bit;
m_key_part_info->null_offset= (uint) ((*reg_field)->null_ptr -
(uchar*) table->record[0]);
-
- m_key_part_info->offset= (*reg_field)->offset(table->record[0]);
+ /*
+ Can't call reg_field->offset(), because temporary tables don't
+ have table->s->field set.
+ */
+ m_key_part_info->offset= (*reg_field)->ptr - table->record[0];
m_key_part_info->length= (uint16) (*reg_field)->pack_length();
m_key_part_info->fieldnr= (*reg_field)->field_index + 1;
/* TODO:
diff --git a/sql/table.cc b/sql/table.cc
index ca4b4e40014..a7a38181473 100644
--- a/sql/table.cc
+++ b/sql/table.cc
@@ -4999,7 +4999,7 @@ static field_index_t find_field(Field **fields, uchar *record, uint start,
pos= 0;
for (field= fields, i=1 ; *field ; i++,field++)
{
- if ((*field)->offset(record) == start)
+ if ((*field)->offset() == start)
{
if ((*field)->key_length() == length)
return (i);
@@ -8373,7 +8373,7 @@ void TABLE::create_key_part_by_field(KEY_PART_INFO *key_part_info,
(uchar*) record[0]);
key_part_info->field= field;
key_part_info->fieldnr= fieldnr;
- key_part_info->offset= field->offset(record[0]);
+ key_part_info->offset= field->ptr - record[0];
/*
field->key_length() accounts for the raw length of the field, excluding
any metadata such as length of field or the NULL flag.
diff --git a/storage/archive/ha_archive.cc b/storage/archive/ha_archive.cc
index 2a8deb431b1..b5934cf317f 100644
--- a/storage/archive/ha_archive.cc
+++ b/storage/archive/ha_archive.cc
@@ -953,7 +953,7 @@ unsigned int ha_archive::pack_row(const uchar *record, azio_stream *writer)
for (Field **field=table->field ; *field ; field++)
{
if (!((*field)->is_null(rec_offset)))
- ptr= (*field)->pack(ptr, record + (*field)->offset(record));
+ ptr= (*field)->pack(ptr, record + (*field)->offset());
}
int4store(record_buffer->buffer, (int)(ptr - record_buffer->buffer -
@@ -1046,7 +1046,7 @@ int ha_archive::write_row(const uchar *buf)
while (!(get_row(&archive, read_buf)))
{
- if (!memcmp(read_buf + mfield->offset(record),
+ if (!memcmp(read_buf + mfield->offset(),
table->next_number_field->ptr,
mfield->max_display_length()))
{
@@ -1336,7 +1336,7 @@ int ha_archive::unpack_row(azio_stream *file_to_read, uchar *record)
{
if (!((*field)->is_null_in_record(record)))
{
- if (!(ptr= (*field)->unpack(record + (*field)->offset(table->record[0]),
+ if (!(ptr= (*field)->unpack(record + (*field)->offset(),
ptr, end)))
DBUG_RETURN(HA_ERR_WRONG_IN_RECORD);
}
@@ -1623,7 +1623,7 @@ int ha_archive::optimize(THD* thd, HA_CHECK_OPT* check_opt)
Field *field= table->found_next_number_field;
ulonglong auto_value=
(ulonglong) field->val_int(table->record[0] +
- field->offset(table->record[0]));
+ field->offset());
if (share->archive_write.auto_increment < auto_value)
stats.auto_increment_value=
(share->archive_write.auto_increment= auto_value) + 1;
diff --git a/storage/federated/ha_federated.cc b/storage/federated/ha_federated.cc
index 7fc88ba0ab1..f88107ea697 100644
--- a/storage/federated/ha_federated.cc
+++ b/storage/federated/ha_federated.cc
@@ -2175,7 +2175,6 @@ int ha_federated::update_row(const uchar *old_data, const uchar *new_data)
String where_string(where_buffer,
sizeof(where_buffer),
&my_charset_bin);
- uchar *record= table->record[0];
DBUG_ENTER("ha_federated::update_row");
/*
set string lengths to 0 to avoid misc chars in string
@@ -2242,7 +2241,7 @@ int ha_federated::update_row(const uchar *old_data, const uchar *new_data)
bool needs_quote= (*field)->str_needs_quotes();
where_string.append(STRING_WITH_LEN(" = "));
(*field)->val_str(&field_value,
- (old_data + (*field)->offset(record)));
+ (old_data + (*field)->offset()));
if (needs_quote)
where_string.append(value_quote_char);
field_value.print(&where_string);
diff --git a/storage/federatedx/ha_federatedx.cc b/storage/federatedx/ha_federatedx.cc
index e4c5bf8d13d..fe9996d56f0 100644
--- a/storage/federatedx/ha_federatedx.cc
+++ b/storage/federatedx/ha_federatedx.cc
@@ -2370,7 +2370,6 @@ int ha_federatedx::update_row(const uchar *old_data, const uchar *new_data)
String where_string(where_buffer,
sizeof(where_buffer),
&my_charset_bin);
- uchar *record= table->record[0];
int error;
DBUG_ENTER("ha_federatedx::update_row");
/*
@@ -2440,7 +2439,7 @@ int ha_federatedx::update_row(const uchar *old_data, const uchar *new_data)
bool needs_quote= (*field)->str_needs_quotes();
where_string.append(STRING_WITH_LEN(" = "));
(*field)->val_str(&field_value,
- (old_data + (*field)->offset(record)));
+ (old_data + (*field)->offset()));
if (needs_quote)
where_string.append(value_quote_char);
field_value.print(&where_string);
diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc
index 1d8e1c1bf1c..6416b9a6145 100644
--- a/storage/innobase/handler/ha_innodb.cc
+++ b/storage/innobase/handler/ha_innodb.cc
@@ -817,8 +817,7 @@ get_field_offset(
const TABLE* table,
const Field* field)
{
-// return field->offset(table->record[0]);
- return field->offset(field->record_ptr());
+ return field->offset();
}
diff --git a/storage/maria/ha_maria.cc b/storage/maria/ha_maria.cc
index 2e93d32fe93..ff546a2658f 100644
--- a/storage/maria/ha_maria.cc
+++ b/storage/maria/ha_maria.cc
@@ -516,7 +516,6 @@ static int table2maria(TABLE *table_arg, data_file_type row_type,
{
uint i, j, recpos, minpos, fieldpos, temp_length, length;
enum ha_base_keytype type= HA_KEYTYPE_BINARY;
- uchar *record;
KEY *pos;
MARIA_KEYDEF *keydef;
MARIA_COLUMNDEF *recinfo, *recinfo_pos;
@@ -626,7 +625,6 @@ static int table2maria(TABLE *table_arg, data_file_type row_type,
}
if (table_arg->found_next_number_field)
keydef[share->next_number_index].flag|= HA_AUTO_KEY;
- record= table_arg->record[0];
recpos= 0;
recinfo_pos= recinfo;
create_info->null_bytes= table_arg->s->null_bytes;
@@ -639,7 +637,7 @@ static int table2maria(TABLE *table_arg, data_file_type row_type,
for (field= table_arg->field; *field; field++)
{
- if ((fieldpos= (*field)->offset(record)) >= recpos &&
+ if ((fieldpos= (*field)->offset()) >= recpos &&
fieldpos <= minpos)
{
/* skip null fields */
diff --git a/storage/myisam/ha_myisam.cc b/storage/myisam/ha_myisam.cc
index e050e46c8db..a094d591b31 100644
--- a/storage/myisam/ha_myisam.cc
+++ b/storage/myisam/ha_myisam.cc
@@ -269,7 +269,6 @@ int table2myisam(TABLE *table_arg, MI_KEYDEF **keydef_out,
{
uint i, j, recpos, minpos, fieldpos, temp_length, length;
enum ha_base_keytype type= HA_KEYTYPE_BINARY;
- uchar *record;
KEY *pos;
MI_KEYDEF *keydef;
MI_COLUMNDEF *recinfo, *recinfo_pos;
@@ -361,7 +360,6 @@ int table2myisam(TABLE *table_arg, MI_KEYDEF **keydef_out,
}
if (table_arg->found_next_number_field)
keydef[share->next_number_index].flag|= HA_AUTO_KEY;
- record= table_arg->record[0];
recpos= 0;
recinfo_pos= recinfo;
while (recpos < (uint) share->stored_rec_length)
@@ -372,7 +370,7 @@ int table2myisam(TABLE *table_arg, MI_KEYDEF **keydef_out,
for (field= table_arg->field; *field; field++)
{
- if ((fieldpos= (*field)->offset(record)) >= recpos &&
+ if ((fieldpos= (*field)->offset()) >= recpos &&
fieldpos <= minpos)
{
/* skip null fields */
@@ -1020,7 +1018,7 @@ void ha_myisam::setup_vcols_for_repair(HA_CHECK *param)
{
if (!(*vf)->stored_in_db())
{
- uint vf_end= ((*vf)->offset(table->record[0]) +
+ uint vf_end= ((*vf)->offset() +
(*vf)->pack_length_in_rec());
set_if_bigger(new_vreclength, vf_end);
indexed_vcols|= ((*vf)->flags & PART_KEY_FLAG) != 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment