17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#include "sj_tmp_table.h"
20
#include <drizzled/global.h>
21
#include <drizzled/sj_tmp_table.h>
22
#include <drizzled/session.h>
23
#include <drizzled/field/varstring.h>
23
26
Create a temporary table to weed out duplicate rowid combinations
55
Table *create_duplicate_weedout_tmp_table(THD *thd,
58
Table *create_duplicate_weedout_tmp_table(Session *session,
56
59
uint32_t uniq_tuple_length_arg,
57
60
SJ_TMP_TABLE *sjtbl)
59
62
MEM_ROOT *mem_root_save, own_root;
62
uint32_t temp_pool_slot=MY_BIT_NONE;
63
65
char *tmpname,path[FN_REFLEN];
65
67
KEY_PART_INFO *key_part_info;
73
75
uint32_t blob_count, null_pack_length, null_count;
74
76
unsigned char *null_flags;
75
77
unsigned char *pos;
78
80
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);
82
statistic_increment(session->status_var.created_tmp_tables, &LOCK_status);
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);
84
/* if we run out of slots or we are not using tempool */
85
sprintf(path,"%s%lx_%"PRIx64"_%x", TMP_FILE_PREFIX, (unsigned long)current_pid,
86
session->thread_id, session->tmp_table++);
87
fn_format(path, path, drizzle_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
95
89
/* STEP 2: Figure if we'll be using a key or blob+constraint */
96
90
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
113
107
&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);
112
strcpy(tmpname,path);
122
114
/* STEP 4: Create Table description */
123
115
memset(table, 0, sizeof(*table));
124
116
memset(reg_field, 0, sizeof(Field*)*2);
126
118
table->mem_root= own_root;
127
mem_root_save= thd->mem_root;
128
thd->mem_root= &table->mem_root;
119
mem_root_save= session->mem_root;
120
session->mem_root= &table->mem_root;
130
122
table->field=reg_field;
131
123
table->alias= "weedout-tmp";
132
124
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
133
125
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
135
table->temp_pool_slot = temp_pool_slot;
136
127
table->copy_blobs= 1;
138
table->quick_keys.init();
139
table->covering_keys.init();
140
table->keys_in_use_for_query.init();
128
table->in_use= session;
129
table->quick_keys.reset();
130
table->covering_keys.reset();
131
table->keys_in_use_for_query.reset();
143
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
134
share->init(tmpname, tmpname);
144
135
share->blob_field= blob_field;
145
136
share->blob_ptr_size= portable_sizeof_char_ptr;
146
137
share->db_low_byte_first=1; // True for HEAP and MyISAM
147
138
share->table_charset= NULL;
148
139
share->primary_key= MAX_KEY; // Indicate no primary key
149
share->keys_for_keyread.init();
150
share->keys_in_use.init();
140
share->keys_for_keyread.reset();
141
share->keys_in_use.reset();
163
154
field->table= table;
164
field->key_start.init(0);
165
field->part_of_key.init(0);
166
field->part_of_sortkey.init(0);
155
field->key_start.reset();
156
field->part_of_key.reset();
157
field->part_of_sortkey.reset();
167
158
field->unireg_check= Field::NONE;
168
159
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
169
160
field->reset_fields();
183
174
uint32_t reclength= field->pack_length();
184
175
if (using_unique_constraint)
186
share->db_plugin= ha_lock_engine(0, myisam_hton);
177
share->storage_engine= myisam_engine;
187
178
table->file= get_new_handler(share, &table->mem_root,
188
179
share->db_type());
189
180
assert(uniq_tuple_length_arg <= table->file->max_key_length());
193
share->db_plugin= ha_lock_engine(0, heap_hton);
184
share->storage_engine= heap_engine;
194
185
table->file= get_new_handler(share, &table->mem_root,
195
186
share->db_type());
261
252
//param->recinfo=recinfo;
262
//store_record(table,s->default_values); // Make empty default record
253
//table->storeRecordAsDefault(); // Make empty default record
264
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
255
if (session->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
265
256
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) /
258
share->max_rows= (ha_rows) (((share->db_type() == heap_engine) ?
259
cmin(session->variables.tmp_table_size,
260
session->variables.max_heap_table_size) :
261
session->variables.tmp_table_size) /
271
262
share->reclength);
272
set_if_bigger(share->max_rows,1); // For dummy start options
263
set_if_bigger(share->max_rows,(ha_rows)1); // For dummy start options
275
266
//// keyinfo= param->keyinfo;
294
285
key_part_info->key_type = FIELDFLAG_BINARY;
295
286
if (!using_unique_constraint)
297
if (!(key_field= field->new_key_field(thd->mem_root, table,
288
if (!(key_field= field->new_key_field(session->mem_root, table,
300
291
field->null_bit)))
308
if (thd->is_fatal_error) // If end of memory
299
if (session->is_fatal_error) // If end of memory
310
301
share->db_record_offset= 1;
311
if (share->db_type() == myisam_hton)
302
if (share->db_type() == myisam_engine)
314
305
if (table->create_myisam_tmp_table(keyinfo, start_recinfo, &recinfo, 0))
319
310
if (table->open_tmp_table())
322
thd->mem_root= mem_root_save;
313
session->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 */
317
session->mem_root= mem_root_save;
318
table->free_tmp_table(session); /* purecov: inspected */
319
return NULL; /* purecov: inspected */