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)
73
76
uint32_t blob_count, null_pack_length, null_count;
74
77
unsigned char *null_flags;
75
78
unsigned char *pos;
78
81
STEP 1: Get temporary table name
80
statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
83
statistic_increment(session->status_var.created_tmp_tables, &LOCK_status);
81
84
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
82
85
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
84
87
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);
88
sprintf(path, "%s_%lx_%i", TMP_FILE_PREFIX,
89
(unsigned long)current_pid, temp_pool_slot);
89
92
/* 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
sprintf(path,"%s%lx_%"PRIx64"_%x", TMP_FILE_PREFIX, (unsigned long)current_pid,
94
session->thread_id, session->tmp_table++);
93
fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
96
fn_format(path, path, drizzle_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
95
98
/* STEP 2: Figure if we'll be using a key or blob+constraint */
96
99
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
117
120
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
120
my_stpcpy(tmpname,path);
123
strcpy(tmpname,path);
122
125
/* STEP 4: Create Table description */
123
126
memset(table, 0, sizeof(*table));
124
127
memset(reg_field, 0, sizeof(Field*)*2);
126
129
table->mem_root= own_root;
127
mem_root_save= thd->mem_root;
128
thd->mem_root= &table->mem_root;
130
mem_root_save= session->mem_root;
131
session->mem_root= &table->mem_root;
130
133
table->field=reg_field;
131
134
table->alias= "weedout-tmp";
135
138
table->temp_pool_slot = temp_pool_slot;
136
139
table->copy_blobs= 1;
140
table->in_use= session;
138
141
table->quick_keys.init();
139
142
table->covering_keys.init();
140
143
table->keys_in_use_for_query.init();
143
init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
146
init_tmp_table_share(session, share, "", 0, tmpname, tmpname);
144
147
share->blob_field= blob_field;
145
148
share->blob_ptr_size= portable_sizeof_char_ptr;
146
149
share->db_low_byte_first=1; // True for HEAP and MyISAM
183
186
uint32_t reclength= field->pack_length();
184
187
if (using_unique_constraint)
186
share->db_plugin= ha_lock_engine(0, myisam_hton);
189
share->db_plugin= ha_lock_engine(0, myisam_engine);
187
190
table->file= get_new_handler(share, &table->mem_root,
188
191
share->db_type());
189
192
assert(uniq_tuple_length_arg <= table->file->max_key_length());
193
share->db_plugin= ha_lock_engine(0, heap_hton);
196
share->db_plugin= ha_lock_engine(0, heap_engine);
194
197
table->file= get_new_handler(share, &table->mem_root,
195
198
share->db_type());
261
264
//param->recinfo=recinfo;
262
265
//store_record(table,s->default_values); // Make empty default record
264
if (thd->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
267
if (session->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
265
268
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) /
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) /
271
274
share->reclength);
272
set_if_bigger(share->max_rows,1); // For dummy start options
275
set_if_bigger(share->max_rows,(ha_rows)1); // For dummy start options
275
278
//// keyinfo= param->keyinfo;
294
297
key_part_info->key_type = FIELDFLAG_BINARY;
295
298
if (!using_unique_constraint)
297
if (!(key_field= field->new_key_field(thd->mem_root, table,
300
if (!(key_field= field->new_key_field(session->mem_root, table,
300
303
field->null_bit)))
308
if (thd->is_fatal_error) // If end of memory
311
if (session->is_fatal_error) // If end of memory
310
313
share->db_record_offset= 1;
311
if (share->db_type() == myisam_hton)
314
if (share->db_type() == myisam_engine)
314
317
if (table->create_myisam_tmp_table(keyinfo, start_recinfo, &recinfo, 0))
319
322
if (table->open_tmp_table())
322
thd->mem_root= mem_root_save;
325
session->mem_root= mem_root_save;
326
thd->mem_root= mem_root_save;
327
table->free_tmp_table(thd); /* purecov: inspected */
329
session->mem_root= mem_root_save;
330
table->free_tmp_table(session); /* purecov: inspected */
328
331
if (temp_pool_slot != MY_BIT_NONE)
329
332
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
330
333
return(NULL); /* purecov: inspected */