1
#include "sj_tmp_table.h"
4
Create a temporary table to weed out duplicate rowid combinations
8
create_duplicate_weedout_tmp_table()
14
Create a temporary table to weed out duplicate rowid combinations. The
15
table has a single column that is a concatenation of all rowids in the
18
Depending on the needed length, there are two cases:
20
1. When the length of the column < max_key_length:
22
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
24
2. Otherwise (not a valid SQL syntax but internally supported):
26
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
28
The code in this function was produced by extraction of relevant parts
29
from create_tmp_table().
36
TABLE *create_duplicate_weedout_tmp_table(THD *thd,
37
uint uniq_tuple_length_arg,
40
MEM_ROOT *mem_root_save, own_root;
43
uint temp_pool_slot=MY_BIT_NONE;
44
char *tmpname,path[FN_REFLEN];
46
KEY_PART_INFO *key_part_info;
51
MI_COLUMNDEF *recinfo, *start_recinfo;
52
bool using_unique_constraint=false;
53
Field *field, *key_field;
54
uint blob_count, null_pack_length, null_count;
59
STEP 1: Get temporary table name
61
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
62
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
63
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
65
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
66
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
67
current_pid, temp_pool_slot);
70
/* if we run out of slots or we are not using tempool */
71
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
72
thd->thread_id, thd->tmp_table++);
74
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
76
/* STEP 2: Figure if we'll be using a key or blob+constraint */
77
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
78
using_unique_constraint= true;
80
/* STEP 3: Allocate memory for temptable description */
81
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
82
if (!multi_alloc_root(&own_root,
83
&table, sizeof(*table),
84
&share, sizeof(*share),
85
®_field, sizeof(Field*) * (1+1),
86
&blob_field, sizeof(uint)*2,
87
&keyinfo, sizeof(*keyinfo),
88
&key_part_info, sizeof(*key_part_info) * 2,
90
sizeof(*recinfo)*(1*2+4),
91
&tmpname, (uint) strlen(path)+1,
92
&group_buff, (!using_unique_constraint ?
93
uniq_tuple_length_arg : 0),
94
&bitmaps, bitmap_buffer_size(1)*2,
97
if (temp_pool_slot != MY_BIT_NONE)
98
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
101
stpcpy(tmpname,path);
104
/* STEP 4: Create TABLE description */
105
memset(table, 0, sizeof(*table));
106
memset(reg_field, 0, sizeof(Field*)*2);
108
table->mem_root= own_root;
109
mem_root_save= thd->mem_root;
110
thd->mem_root= &table->mem_root;
112
table->field=reg_field;
113
table->alias= "weedout-tmp";
114
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
115
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
117
table->temp_pool_slot = temp_pool_slot;
118
table->copy_blobs= 1;
120
table->quick_keys.init();
121
table->covering_keys.init();
122
table->keys_in_use_for_query.init();
125
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
126
share->blob_field= blob_field;
127
share->blob_ptr_size= portable_sizeof_char_ptr;
128
share->db_low_byte_first=1; // True for HEAP and MyISAM
129
share->table_charset= NULL;
130
share->primary_key= MAX_KEY; // Indicate no primary key
131
share->keys_for_keyread.init();
132
share->keys_in_use.init();
136
/* Create the field */
139
For the sake of uniformity, always use Field_varstring.
141
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
146
field->key_start.init(0);
147
field->part_of_key.init(0);
148
field->part_of_sortkey.init(0);
149
field->unireg_check= Field::NONE;
150
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
151
field->reset_fields();
153
field->orig_table= NULL;
155
field->field_index= 0;
157
*(reg_field++)= field;
162
share->blob_fields= 0;
165
uint reclength= field->pack_length();
166
if (using_unique_constraint)
168
share->db_plugin= ha_lock_engine(0, myisam_hton);
169
table->file= get_new_handler(share, &table->mem_root,
171
assert(uniq_tuple_length_arg <= table->file->max_key_length());
175
share->db_plugin= ha_lock_engine(0, heap_hton);
176
table->file= get_new_handler(share, &table->mem_root,
185
reclength += null_pack_length;
187
share->reclength= reclength;
189
uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
190
share->rec_buff_length= alloc_length;
191
if (!(table->record[0]= (uchar*)
192
alloc_root(&table->mem_root, alloc_length*3)))
194
table->record[1]= table->record[0]+alloc_length;
195
share->default_values= table->record[1]+alloc_length;
197
table->setup_tmp_table_column_bitmaps(bitmaps);
199
recinfo= start_recinfo;
200
null_flags=(uchar*) table->record[0];
201
pos=table->record[0]+ null_pack_length;
202
if (null_pack_length)
204
memset(recinfo, 0, sizeof(*recinfo));
205
recinfo->type=FIELD_NORMAL;
206
recinfo->length=null_pack_length;
208
memset(null_flags, 255, null_pack_length); // Set null fields
210
table->null_flags= (uchar*) table->record[0];
211
share->null_fields= null_count;
212
share->null_bytes= null_pack_length;
217
//Field *field= *reg_field;
219
memset(recinfo, 0, sizeof(*recinfo));
220
field->move_field(pos,(uchar*) 0,0);
224
Test if there is a default field value. The test for ->ptr is to skip
225
'offset' fields generated by initalize_tables
227
// Initialize the table field:
228
memset(field->ptr, 0, field->pack_length());
230
length=field->pack_length();
233
/* Make entry for create table */
234
recinfo->length=length;
235
if (field->flags & BLOB_FLAG)
236
recinfo->type= (int) FIELD_BLOB;
238
recinfo->type=FIELD_NORMAL;
240
field->table_name= &table->alias;
243
//param->recinfo=recinfo;
244
//store_record(table,s->default_values); // Make empty default record
246
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
247
share->max_rows= ~(ha_rows) 0;
249
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
250
min(thd->variables.tmp_table_size,
251
thd->variables.max_heap_table_size) :
252
thd->variables.tmp_table_size) /
254
set_if_bigger(share->max_rows,1); // For dummy start options
257
//// keyinfo= param->keyinfo;
261
share->uniques= test(using_unique_constraint);
262
table->key_info=keyinfo;
263
keyinfo->key_part=key_part_info;
264
keyinfo->flags=HA_NOSAME;
265
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
266
keyinfo->key_length=0;
267
keyinfo->rec_per_key=0;
268
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
269
keyinfo->name= (char*) "weedout_key";
271
key_part_info->null_bit=0;
272
key_part_info->field= field;
273
key_part_info->offset= field->offset(table->record[0]);
274
key_part_info->length= (uint16_t) field->key_length();
275
key_part_info->type= (uint8_t) field->key_type();
276
key_part_info->key_type = FIELDFLAG_BINARY;
277
if (!using_unique_constraint)
279
if (!(key_field= field->new_key_field(thd->mem_root, table,
284
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
286
keyinfo->key_length+= key_part_info->length;
290
if (thd->is_fatal_error) // If end of memory
292
share->db_record_offset= 1;
293
if (share->db_type() == myisam_hton)
296
if (table->create_myisam_tmp_table(keyinfo, start_recinfo, &recinfo, 0))
299
sjtbl->start_recinfo= start_recinfo;
300
sjtbl->recinfo= recinfo;
301
if (table->open_tmp_table())
304
thd->mem_root= mem_root_save;
308
thd->mem_root= mem_root_save;
309
table->free_tmp_table(thd); /* purecov: inspected */
310
if (temp_pool_slot != MY_BIT_NONE)
311
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
312
return(NULL); /* purecov: inspected */