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 <drizzled/global.h>
21
#include <drizzled/sj_tmp_table.h>
22
#include <drizzled/session.h>
23
#include <drizzled/field/varstring.h>
26
Create a temporary table to weed out duplicate rowid combinations
30
create_duplicate_weedout_tmp_table()
36
Create a temporary table to weed out duplicate rowid combinations. The
37
table has a single column that is a concatenation of all rowids in the
40
Depending on the needed length, there are two cases:
42
1. When the length of the column < max_key_length:
44
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
46
2. Otherwise (not a valid SQL syntax but internally supported):
48
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
50
The code in this function was produced by extraction of relevant parts
51
from create_tmp_table().
58
Table *create_duplicate_weedout_tmp_table(Session *session,
59
uint32_t uniq_tuple_length_arg,
62
MEM_ROOT *mem_root_save, own_root;
65
uint32_t temp_pool_slot= MY_BIT_NONE;
66
char *tmpname,path[FN_REFLEN];
68
KEY_PART_INFO *key_part_info;
70
unsigned char *group_buff;
71
unsigned char *bitmaps;
73
MI_COLUMNDEF *recinfo, *start_recinfo;
74
bool using_unique_constraint=false;
75
Field *field, *key_field;
76
uint32_t blob_count, null_pack_length, null_count;
77
unsigned char *null_flags;
81
STEP 1: Get temporary table name
83
statistic_increment(session->status_var.created_tmp_tables, &LOCK_status);
84
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
85
temp_pool_slot = setNextBit(temp_pool);
87
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
88
sprintf(path, "%s_%lx_%i", TMP_FILE_PREFIX,
89
(unsigned long)current_pid, temp_pool_slot);
92
/* if we run out of slots or we are not using tempool */
93
sprintf(path,"%s%lx_%"PRIx64"_%x", TMP_FILE_PREFIX, (unsigned long)current_pid,
94
session->thread_id, session->tmp_table++);
96
fn_format(path, path, drizzle_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
98
/* STEP 2: Figure if we'll be using a key or blob+constraint */
99
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
100
using_unique_constraint= true;
102
/* STEP 3: Allocate memory for temptable description */
103
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
104
if (!multi_alloc_root(&own_root,
105
&table, sizeof(*table),
106
&share, sizeof(*share),
107
®_field, sizeof(Field*) * (1+1),
108
&blob_field, sizeof(uint32_t)*2,
109
&keyinfo, sizeof(*keyinfo),
110
&key_part_info, sizeof(*key_part_info) * 2,
112
sizeof(*recinfo)*(1*2+4),
113
&tmpname, (uint32_t) strlen(path)+1,
114
&group_buff, (!using_unique_constraint ?
115
uniq_tuple_length_arg : 0),
116
&bitmaps, bitmap_buffer_size(1)*2,
119
if (temp_pool_slot != MY_BIT_NONE)
120
temp_pool.reset(temp_pool_slot);
123
strcpy(tmpname,path);
125
/* STEP 4: Create Table description */
126
memset(table, 0, sizeof(*table));
127
memset(reg_field, 0, sizeof(Field*)*2);
129
table->mem_root= own_root;
130
mem_root_save= session->mem_root;
131
session->mem_root= &table->mem_root;
133
table->field=reg_field;
134
table->alias= "weedout-tmp";
135
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
136
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
138
table->temp_pool_slot = temp_pool_slot;
139
table->copy_blobs= 1;
140
table->in_use= session;
141
table->quick_keys.init();
142
table->covering_keys.init();
143
table->keys_in_use_for_query.init();
146
init_tmp_table_share(session, share, "", 0, tmpname, tmpname);
147
share->blob_field= blob_field;
148
share->blob_ptr_size= portable_sizeof_char_ptr;
149
share->db_low_byte_first=1; // True for HEAP and MyISAM
150
share->table_charset= NULL;
151
share->primary_key= MAX_KEY; // Indicate no primary key
152
share->keys_for_keyread.init();
153
share->keys_in_use.init();
157
/* Create the field */
160
For the sake of uniformity, always use Field_varstring.
162
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
167
field->key_start.init(0);
168
field->part_of_key.init(0);
169
field->part_of_sortkey.init(0);
170
field->unireg_check= Field::NONE;
171
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
172
field->reset_fields();
174
field->orig_table= NULL;
176
field->field_index= 0;
178
*(reg_field++)= field;
183
share->blob_fields= 0;
186
uint32_t reclength= field->pack_length();
187
if (using_unique_constraint)
189
share->storage_engine= myisam_engine;
190
table->file= get_new_handler(share, &table->mem_root,
192
assert(uniq_tuple_length_arg <= table->file->max_key_length());
196
share->storage_engine= heap_engine;
197
table->file= get_new_handler(share, &table->mem_root,
206
reclength += null_pack_length;
208
share->reclength= reclength;
210
uint32_t alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
211
share->rec_buff_length= alloc_length;
212
if (!(table->record[0]= (unsigned char*)
213
alloc_root(&table->mem_root, alloc_length*3)))
215
table->record[1]= table->record[0]+alloc_length;
216
share->default_values= table->record[1]+alloc_length;
218
table->setup_tmp_table_column_bitmaps();
220
recinfo= start_recinfo;
221
null_flags=(unsigned char*) table->record[0];
222
pos=table->record[0]+ null_pack_length;
223
if (null_pack_length)
225
memset(recinfo, 0, sizeof(*recinfo));
226
recinfo->type=FIELD_NORMAL;
227
recinfo->length=null_pack_length;
229
memset(null_flags, 255, null_pack_length); // Set null fields
231
table->null_flags= (unsigned char*) table->record[0];
232
share->null_fields= null_count;
233
share->null_bytes= null_pack_length;
238
//Field *field= *reg_field;
240
memset(recinfo, 0, sizeof(*recinfo));
241
field->move_field(pos,(unsigned char*) 0,0);
245
Test if there is a default field value. The test for ->ptr is to skip
246
'offset' fields generated by initalize_tables
248
// Initialize the table field:
249
memset(field->ptr, 0, field->pack_length());
251
length=field->pack_length();
254
/* Make entry for create table */
255
recinfo->length=length;
256
if (field->flags & BLOB_FLAG)
257
recinfo->type= (int) FIELD_BLOB;
259
recinfo->type=FIELD_NORMAL;
261
field->table_name= &table->alias;
264
//param->recinfo=recinfo;
265
//store_record(table,s->default_values); // Make empty default record
267
if (session->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
268
share->max_rows= ~(ha_rows) 0;
270
share->max_rows= (ha_rows) (((share->db_type() == heap_engine) ?
271
cmin(session->variables.tmp_table_size,
272
session->variables.max_heap_table_size) :
273
session->variables.tmp_table_size) /
275
set_if_bigger(share->max_rows,(ha_rows)1); // For dummy start options
278
//// keyinfo= param->keyinfo;
282
share->uniques= test(using_unique_constraint);
283
table->key_info=keyinfo;
284
keyinfo->key_part=key_part_info;
285
keyinfo->flags=HA_NOSAME;
286
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
287
keyinfo->key_length=0;
288
keyinfo->rec_per_key=0;
289
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
290
keyinfo->name= (char*) "weedout_key";
292
key_part_info->null_bit=0;
293
key_part_info->field= field;
294
key_part_info->offset= field->offset(table->record[0]);
295
key_part_info->length= (uint16_t) field->key_length();
296
key_part_info->type= (uint8_t) field->key_type();
297
key_part_info->key_type = FIELDFLAG_BINARY;
298
if (!using_unique_constraint)
300
if (!(key_field= field->new_key_field(session->mem_root, table,
305
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
307
keyinfo->key_length+= key_part_info->length;
311
if (session->is_fatal_error) // If end of memory
313
share->db_record_offset= 1;
314
if (share->db_type() == myisam_engine)
317
if (table->create_myisam_tmp_table(keyinfo, start_recinfo, &recinfo, 0))
320
sjtbl->start_recinfo= start_recinfo;
321
sjtbl->recinfo= recinfo;
322
if (table->open_tmp_table())
325
session->mem_root= mem_root_save;
329
session->mem_root= mem_root_save;
330
table->free_tmp_table(session); /* purecov: inspected */
331
if (temp_pool_slot != MY_BIT_NONE)
332
temp_pool.reset(temp_pool_slot);
333
return(NULL); /* purecov: inspected */