1
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "sj_tmp_table.h"
23
Create a temporary table to weed out duplicate rowid combinations
27
create_duplicate_weedout_tmp_table()
33
Create a temporary table to weed out duplicate rowid combinations. The
34
table has a single column that is a concatenation of all rowids in the
37
Depending on the needed length, there are two cases:
39
1. When the length of the column < max_key_length:
41
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
43
2. Otherwise (not a valid SQL syntax but internally supported):
45
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
47
The code in this function was produced by extraction of relevant parts
48
from create_tmp_table().
55
Table *create_duplicate_weedout_tmp_table(THD *thd,
56
uint32_t uniq_tuple_length_arg,
59
MEM_ROOT *mem_root_save, own_root;
62
uint32_t temp_pool_slot=MY_BIT_NONE;
63
char *tmpname,path[FN_REFLEN];
65
KEY_PART_INFO *key_part_info;
67
unsigned char *group_buff;
68
unsigned char *bitmaps;
70
MI_COLUMNDEF *recinfo, *start_recinfo;
71
bool using_unique_constraint=false;
72
Field *field, *key_field;
73
uint32_t blob_count, null_pack_length, null_count;
74
unsigned char *null_flags;
78
STEP 1: Get temporary table name
80
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
81
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
82
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
84
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
85
sprintf(path, "%s_%lx_%i", tmp_file_prefix,
86
current_pid, temp_pool_slot);
89
/* if we run out of slots or we are not using tempool */
90
sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
91
thd->thread_id, thd->tmp_table++);
93
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
95
/* STEP 2: Figure if we'll be using a key or blob+constraint */
96
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
97
using_unique_constraint= true;
99
/* STEP 3: Allocate memory for temptable description */
100
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
101
if (!multi_alloc_root(&own_root,
102
&table, sizeof(*table),
103
&share, sizeof(*share),
104
®_field, sizeof(Field*) * (1+1),
105
&blob_field, sizeof(uint32_t)*2,
106
&keyinfo, sizeof(*keyinfo),
107
&key_part_info, sizeof(*key_part_info) * 2,
109
sizeof(*recinfo)*(1*2+4),
110
&tmpname, (uint32_t) strlen(path)+1,
111
&group_buff, (!using_unique_constraint ?
112
uniq_tuple_length_arg : 0),
113
&bitmaps, bitmap_buffer_size(1)*2,
116
if (temp_pool_slot != MY_BIT_NONE)
117
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
120
my_stpcpy(tmpname,path);
122
/* STEP 4: Create Table description */
123
memset(table, 0, sizeof(*table));
124
memset(reg_field, 0, sizeof(Field*)*2);
126
table->mem_root= own_root;
127
mem_root_save= thd->mem_root;
128
thd->mem_root= &table->mem_root;
130
table->field=reg_field;
131
table->alias= "weedout-tmp";
132
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
133
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
135
table->temp_pool_slot = temp_pool_slot;
136
table->copy_blobs= 1;
138
table->quick_keys.init();
139
table->covering_keys.init();
140
table->keys_in_use_for_query.init();
143
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
144
share->blob_field= blob_field;
145
share->blob_ptr_size= portable_sizeof_char_ptr;
146
share->db_low_byte_first=1; // True for HEAP and MyISAM
147
share->table_charset= NULL;
148
share->primary_key= MAX_KEY; // Indicate no primary key
149
share->keys_for_keyread.init();
150
share->keys_in_use.init();
154
/* Create the field */
157
For the sake of uniformity, always use Field_varstring.
159
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
164
field->key_start.init(0);
165
field->part_of_key.init(0);
166
field->part_of_sortkey.init(0);
167
field->unireg_check= Field::NONE;
168
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
169
field->reset_fields();
171
field->orig_table= NULL;
173
field->field_index= 0;
175
*(reg_field++)= field;
180
share->blob_fields= 0;
183
uint32_t reclength= field->pack_length();
184
if (using_unique_constraint)
186
share->db_plugin= ha_lock_engine(0, myisam_hton);
187
table->file= get_new_handler(share, &table->mem_root,
189
assert(uniq_tuple_length_arg <= table->file->max_key_length());
193
share->db_plugin= ha_lock_engine(0, heap_hton);
194
table->file= get_new_handler(share, &table->mem_root,
203
reclength += null_pack_length;
205
share->reclength= reclength;
207
uint32_t alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
208
share->rec_buff_length= alloc_length;
209
if (!(table->record[0]= (unsigned char*)
210
alloc_root(&table->mem_root, alloc_length*3)))
212
table->record[1]= table->record[0]+alloc_length;
213
share->default_values= table->record[1]+alloc_length;
215
table->setup_tmp_table_column_bitmaps(bitmaps);
217
recinfo= start_recinfo;
218
null_flags=(unsigned char*) table->record[0];
219
pos=table->record[0]+ null_pack_length;
220
if (null_pack_length)
222
memset(recinfo, 0, sizeof(*recinfo));
223
recinfo->type=FIELD_NORMAL;
224
recinfo->length=null_pack_length;
226
memset(null_flags, 255, null_pack_length); // Set null fields
228
table->null_flags= (unsigned char*) table->record[0];
229
share->null_fields= null_count;
230
share->null_bytes= null_pack_length;
235
//Field *field= *reg_field;
237
memset(recinfo, 0, sizeof(*recinfo));
238
field->move_field(pos,(unsigned char*) 0,0);
242
Test if there is a default field value. The test for ->ptr is to skip
243
'offset' fields generated by initalize_tables
245
// Initialize the table field:
246
memset(field->ptr, 0, field->pack_length());
248
length=field->pack_length();
251
/* Make entry for create table */
252
recinfo->length=length;
253
if (field->flags & BLOB_FLAG)
254
recinfo->type= (int) FIELD_BLOB;
256
recinfo->type=FIELD_NORMAL;
258
field->table_name= &table->alias;
261
//param->recinfo=recinfo;
262
//store_record(table,s->default_values); // Make empty default record
264
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
265
share->max_rows= ~(ha_rows) 0;
267
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
268
cmin(thd->variables.tmp_table_size,
269
thd->variables.max_heap_table_size) :
270
thd->variables.tmp_table_size) /
272
set_if_bigger(share->max_rows,1); // For dummy start options
275
//// keyinfo= param->keyinfo;
279
share->uniques= test(using_unique_constraint);
280
table->key_info=keyinfo;
281
keyinfo->key_part=key_part_info;
282
keyinfo->flags=HA_NOSAME;
283
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
284
keyinfo->key_length=0;
285
keyinfo->rec_per_key=0;
286
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
287
keyinfo->name= (char*) "weedout_key";
289
key_part_info->null_bit=0;
290
key_part_info->field= field;
291
key_part_info->offset= field->offset(table->record[0]);
292
key_part_info->length= (uint16_t) field->key_length();
293
key_part_info->type= (uint8_t) field->key_type();
294
key_part_info->key_type = FIELDFLAG_BINARY;
295
if (!using_unique_constraint)
297
if (!(key_field= field->new_key_field(thd->mem_root, table,
302
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
304
keyinfo->key_length+= key_part_info->length;
308
if (thd->is_fatal_error) // If end of memory
310
share->db_record_offset= 1;
311
if (share->db_type() == myisam_hton)
314
if (table->create_myisam_tmp_table(keyinfo, start_recinfo, &recinfo, 0))
317
sjtbl->start_recinfo= start_recinfo;
318
sjtbl->recinfo= recinfo;
319
if (table->open_tmp_table())
322
thd->mem_root= mem_root_save;
326
thd->mem_root= mem_root_save;
327
table->free_tmp_table(thd); /* purecov: inspected */
328
if (temp_pool_slot != MY_BIT_NONE)
329
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
330
return(NULL); /* purecov: inspected */