1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <config.h>
#include <drizzled/sj_tmp_table.h>
#include <drizzled/session.h>
#include <drizzled/field/varstring.h>
/*
Create a temporary table to weed out duplicate rowid combinations
SYNOPSIS
create_duplicate_weedout_tmp_table()
session
uniq_tuple_length_arg
SJ_TMP_TABLE
DESCRIPTION
Create a temporary table to weed out duplicate rowid combinations. The
table has a single column that is a concatenation of all rowids in the
combination.
Depending on the needed length, there are two cases:
1. When the length of the column < max_key_length:
CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
2. Otherwise (not a valid SQL syntax but internally supported):
CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
The code in this function was produced by extraction of relevant parts
from create_tmp_table().
RETURN
created table
NULL on error
*/
Table *create_duplicate_weedout_tmp_table(Session *session,
uint32_t uniq_tuple_length_arg,
SJ_TMP_TABLE *sjtbl)
{
MEM_ROOT *mem_root_save, own_root;
Table *table;
TABLE_SHARE *share;
uint32_t temp_pool_slot=MY_BIT_NONE;
char *tmpname,path[FN_REFLEN];
Field **reg_field;
KEY_PART_INFO *key_part_info;
KEY *keyinfo;
unsigned char *group_buff;
unsigned char *bitmaps;
uint32_t *blob_field;
MI_COLUMNDEF *recinfo, *start_recinfo;
bool using_unique_constraint=false;
Field *field, *key_field;
uint32_t blob_count, null_pack_length, null_count;
unsigned char *null_flags;
unsigned char *pos;
/*
STEP 1: Get temporary table name
*/
statistic_increment(session->status_var.created_tmp_tables, &LOCK_status);
if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
temp_pool_slot = bitmap_lock_set_next(&temp_pool);
if (temp_pool_slot != MY_BIT_NONE) // we got a slot
sprintf(path, "%s_%lx_%i", TMP_FILE_PREFIX,
(unsigned long)current_pid, temp_pool_slot);
else
{
/* if we run out of slots or we are not using tempool */
sprintf(path,"%s%lx_%"PRIx64"_%x", TMP_FILE_PREFIX, (unsigned long)current_pid,
session->thread_id, session->tmp_table++);
}
fn_format(path, path, drizzle_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
/* STEP 2: Figure if we'll be using a key or blob+constraint */
if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
using_unique_constraint= true;
/* STEP 3: Allocate memory for temptable description */
init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
if (!multi_alloc_root(&own_root,
&table, sizeof(*table),
&share, sizeof(*share),
®_field, sizeof(Field*) * (1+1),
&blob_field, sizeof(uint32_t)*2,
&keyinfo, sizeof(*keyinfo),
&key_part_info, sizeof(*key_part_info) * 2,
&start_recinfo,
sizeof(*recinfo)*(1*2+4),
&tmpname, (uint32_t) strlen(path)+1,
&group_buff, (!using_unique_constraint ?
uniq_tuple_length_arg : 0),
&bitmaps, bitmap_buffer_size(1)*2,
NULL))
{
if (temp_pool_slot != MY_BIT_NONE)
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
return(NULL);
}
my_stpcpy(tmpname,path);
/* STEP 4: Create Table description */
memset(table, 0, sizeof(*table));
memset(reg_field, 0, sizeof(Field*)*2);
table->mem_root= own_root;
mem_root_save= session->mem_root;
session->mem_root= &table->mem_root;
table->field=reg_field;
table->alias= "weedout-tmp";
table->reginfo.lock_type=TL_WRITE; /* Will be updated */
table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
table->map=1;
table->temp_pool_slot = temp_pool_slot;
table->copy_blobs= 1;
table->in_use= session;
table->quick_keys.init();
table->covering_keys.init();
table->keys_in_use_for_query.init();
table->s= share;
init_tmp_table_share(session, share, "", 0, tmpname, tmpname);
share->blob_field= blob_field;
share->blob_ptr_size= portable_sizeof_char_ptr;
share->db_low_byte_first=1; // True for HEAP and MyISAM
share->table_charset= NULL;
share->primary_key= MAX_KEY; // Indicate no primary key
share->keys_for_keyread.init();
share->keys_in_use.init();
blob_count= 0;
/* Create the field */
{
/*
For the sake of uniformity, always use Field_varstring.
*/
field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
&my_charset_bin);
if (!field)
return(0);
field->table= table;
field->key_start.init(0);
field->part_of_key.init(0);
field->part_of_sortkey.init(0);
field->unireg_check= Field::NONE;
field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
field->reset_fields();
field->init(table);
field->orig_table= NULL;
field->field_index= 0;
*(reg_field++)= field;
*blob_field= 0;
*reg_field= 0;
share->fields= 1;
share->blob_fields= 0;
}
uint32_t reclength= field->pack_length();
if (using_unique_constraint)
{
share->db_plugin= ha_lock_engine(0, myisam_hton);
table->file= get_new_handler(share, &table->mem_root,
share->db_type());
assert(uniq_tuple_length_arg <= table->file->max_key_length());
}
else
{
share->db_plugin= ha_lock_engine(0, heap_hton);
table->file= get_new_handler(share, &table->mem_root,
share->db_type());
}
if (!table->file)
goto err;
null_count=1;
null_pack_length= 1;
reclength += null_pack_length;
share->reclength= reclength;
{
uint32_t alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
share->rec_buff_length= alloc_length;
if (!(table->record[0]= (unsigned char*)
alloc_root(&table->mem_root, alloc_length*3)))
goto err;
table->record[1]= table->record[0]+alloc_length;
share->default_values= table->record[1]+alloc_length;
}
table->setup_tmp_table_column_bitmaps(bitmaps);
recinfo= start_recinfo;
null_flags=(unsigned char*) table->record[0];
pos=table->record[0]+ null_pack_length;
if (null_pack_length)
{
memset(recinfo, 0, sizeof(*recinfo));
recinfo->type=FIELD_NORMAL;
recinfo->length=null_pack_length;
recinfo++;
memset(null_flags, 255, null_pack_length); // Set null fields
table->null_flags= (unsigned char*) table->record[0];
share->null_fields= null_count;
share->null_bytes= null_pack_length;
}
null_count=1;
{
//Field *field= *reg_field;
uint32_t length;
memset(recinfo, 0, sizeof(*recinfo));
field->move_field(pos,(unsigned char*) 0,0);
field->reset();
/*
Test if there is a default field value. The test for ->ptr is to skip
'offset' fields generated by initalize_tables
*/
// Initialize the table field:
memset(field->ptr, 0, field->pack_length());
length=field->pack_length();
pos+= length;
/* Make entry for create table */
recinfo->length=length;
if (field->flags & BLOB_FLAG)
recinfo->type= (int) FIELD_BLOB;
else
recinfo->type=FIELD_NORMAL;
field->table_name= &table->alias;
}
//param->recinfo=recinfo;
//store_record(table,s->default_values); // Make empty default record
if (session->variables.tmp_table_size == ~ (uint64_t) 0) // No limit
share->max_rows= ~(ha_rows) 0;
else
share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
cmin(session->variables.tmp_table_size,
session->variables.max_heap_table_size) :
session->variables.tmp_table_size) /
share->reclength);
set_if_bigger(share->max_rows,1); // For dummy start options
//// keyinfo= param->keyinfo;
if (true)
{
share->keys=1;
share->uniques= test(using_unique_constraint);
table->key_info=keyinfo;
keyinfo->key_part=key_part_info;
keyinfo->flags=HA_NOSAME;
keyinfo->usable_key_parts= keyinfo->key_parts= 1;
keyinfo->key_length=0;
keyinfo->rec_per_key=0;
keyinfo->algorithm= HA_KEY_ALG_UNDEF;
keyinfo->name= (char*) "weedout_key";
{
key_part_info->null_bit=0;
key_part_info->field= field;
key_part_info->offset= field->offset(table->record[0]);
key_part_info->length= (uint16_t) field->key_length();
key_part_info->type= (uint8_t) field->key_type();
key_part_info->key_type = FIELDFLAG_BINARY;
if (!using_unique_constraint)
{
if (!(key_field= field->new_key_field(session->mem_root, table,
group_buff,
field->null_ptr,
field->null_bit)))
goto err;
key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
}
keyinfo->key_length+= key_part_info->length;
}
}
if (session->is_fatal_error) // If end of memory
goto err;
share->db_record_offset= 1;
if (share->db_type() == myisam_hton)
{
recinfo++;
if (table->create_myisam_tmp_table(keyinfo, start_recinfo, &recinfo, 0))
goto err;
}
sjtbl->start_recinfo= start_recinfo;
sjtbl->recinfo= recinfo;
if (table->open_tmp_table())
goto err;
session->mem_root= mem_root_save;
return(table);
err:
session->mem_root= mem_root_save;
table->free_tmp_table(session); /* purecov: inspected */
if (temp_pool_slot != MY_BIT_NONE)
bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
return(NULL); /* purecov: inspected */
}
|