~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sj_tmp_table.cc

Removed dead variable, sorted authors file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
 
 *
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.
9
 
 *
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.
14
 
 *
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
18
 
 */
19
 
 
20
 
#include <drizzled/global.h>
21
 
#include <drizzled/sj_tmp_table.h>
22
 
#include <drizzled/session.h>
23
 
#include <drizzled/field/varstring.h>
24
 
 
25
 
/*
26
 
  Create a temporary table to weed out duplicate rowid combinations
27
 
 
28
 
  SYNOPSIS
29
 
 
30
 
  create_duplicate_weedout_tmp_table()
31
 
  session
32
 
  uniq_tuple_length_arg
33
 
  SJ_TMP_TABLE
34
 
 
35
 
  DESCRIPTION
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
38
 
  combination.
39
 
 
40
 
  Depending on the needed length, there are two cases:
41
 
 
42
 
  1. When the length of the column < max_key_length:
43
 
 
44
 
  CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
45
 
 
46
 
  2. Otherwise (not a valid SQL syntax but internally supported):
47
 
 
48
 
  CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
49
 
 
50
 
  The code in this function was produced by extraction of relevant parts
51
 
  from create_tmp_table().
52
 
 
53
 
  RETURN
54
 
  created table
55
 
  NULL on error
56
 
*/
57
 
 
58
 
Table *create_duplicate_weedout_tmp_table(Session *session,
59
 
                                          uint32_t uniq_tuple_length_arg,
60
 
                                          SJ_TMP_TABLE *sjtbl)
61
 
{
62
 
  MEM_ROOT *mem_root_save, own_root;
63
 
  Table *table;
64
 
  TABLE_SHARE *share;
65
 
  uint32_t  temp_pool_slot=MY_BIT_NONE;
66
 
  char  *tmpname,path[FN_REFLEN];
67
 
  Field **reg_field;
68
 
  KEY_PART_INFO *key_part_info;
69
 
  KEY *keyinfo;
70
 
  unsigned char *group_buff;
71
 
  unsigned char *bitmaps;
72
 
  uint32_t *blob_field;
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;
78
 
  unsigned char *pos;
79
 
 
80
 
  /*
81
 
    STEP 1: Get temporary table name
82
 
  */
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 = bitmap_lock_set_next(&temp_pool);
86
 
 
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);
90
 
  else
91
 
  {
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++);
95
 
  }
96
 
  fn_format(path, path, drizzle_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
97
 
 
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;
101
 
 
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
 
                        &reg_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,
111
 
                        &start_recinfo,
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,
117
 
                        NULL))
118
 
  {
119
 
    if (temp_pool_slot != MY_BIT_NONE)
120
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
121
 
    return(NULL);
122
 
  }
123
 
  strcpy(tmpname,path);
124
 
 
125
 
  /* STEP 4: Create Table description */
126
 
  memset(table, 0, sizeof(*table));
127
 
  memset(reg_field, 0, sizeof(Field*)*2);
128
 
 
129
 
  table->mem_root= own_root;
130
 
  mem_root_save= session->mem_root;
131
 
  session->mem_root= &table->mem_root;
132
 
 
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;
137
 
  table->map=1;
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();
144
 
 
145
 
  table->s= share;
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();
154
 
 
155
 
  blob_count= 0;
156
 
 
157
 
  /* Create the field */
158
 
  {
159
 
    /*
160
 
      For the sake of uniformity, always use Field_varstring.
161
 
    */
162
 
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
163
 
                               &my_charset_bin);
164
 
    if (!field)
165
 
      return(0);
166
 
    field->table= table;
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();
173
 
    field->init(table);
174
 
    field->orig_table= NULL;
175
 
 
176
 
    field->field_index= 0;
177
 
 
178
 
    *(reg_field++)= field;
179
 
    *blob_field= 0;
180
 
    *reg_field= 0;
181
 
 
182
 
    share->fields= 1;
183
 
    share->blob_fields= 0;
184
 
  }
185
 
 
186
 
  uint32_t reclength= field->pack_length();
187
 
  if (using_unique_constraint)
188
 
  {
189
 
    share->db_plugin= ha_lock_engine(0, myisam_hton);
190
 
    table->file= get_new_handler(share, &table->mem_root,
191
 
                                 share->db_type());
192
 
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
193
 
  }
194
 
  else
195
 
  {
196
 
    share->db_plugin= ha_lock_engine(0, heap_hton);
197
 
    table->file= get_new_handler(share, &table->mem_root,
198
 
                                 share->db_type());
199
 
  }
200
 
  if (!table->file)
201
 
    goto err;
202
 
 
203
 
  null_count=1;
204
 
 
205
 
  null_pack_length= 1;
206
 
  reclength += null_pack_length;
207
 
 
208
 
  share->reclength= reclength;
209
 
  {
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)))
214
 
      goto err;
215
 
    table->record[1]= table->record[0]+alloc_length;
216
 
    share->default_values= table->record[1]+alloc_length;
217
 
  }
218
 
  table->setup_tmp_table_column_bitmaps(bitmaps);
219
 
 
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)
224
 
  {
225
 
    memset(recinfo, 0, sizeof(*recinfo));
226
 
    recinfo->type=FIELD_NORMAL;
227
 
    recinfo->length=null_pack_length;
228
 
    recinfo++;
229
 
    memset(null_flags, 255, null_pack_length);  // Set null fields
230
 
 
231
 
    table->null_flags= (unsigned char*) table->record[0];
232
 
    share->null_fields= null_count;
233
 
    share->null_bytes= null_pack_length;
234
 
  }
235
 
  null_count=1;
236
 
 
237
 
  {
238
 
    //Field *field= *reg_field;
239
 
    uint32_t length;
240
 
    memset(recinfo, 0, sizeof(*recinfo));
241
 
    field->move_field(pos,(unsigned char*) 0,0);
242
 
 
243
 
    field->reset();
244
 
    /*
245
 
      Test if there is a default field value. The test for ->ptr is to skip
246
 
      'offset' fields generated by initalize_tables
247
 
    */
248
 
    // Initialize the table field:
249
 
    memset(field->ptr, 0, field->pack_length());
250
 
 
251
 
    length=field->pack_length();
252
 
    pos+= length;
253
 
 
254
 
    /* Make entry for create table */
255
 
    recinfo->length=length;
256
 
    if (field->flags & BLOB_FLAG)
257
 
      recinfo->type= (int) FIELD_BLOB;
258
 
    else
259
 
      recinfo->type=FIELD_NORMAL;
260
 
 
261
 
    field->table_name= &table->alias;
262
 
  }
263
 
 
264
 
  //param->recinfo=recinfo;
265
 
  //store_record(table,s->default_values);        // Make empty default record
266
 
 
267
 
  if (session->variables.tmp_table_size == ~ (uint64_t) 0)    // No limit
268
 
    share->max_rows= ~(ha_rows) 0;
269
 
  else
270
 
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
271
 
                                 cmin(session->variables.tmp_table_size,
272
 
                                      session->variables.max_heap_table_size) :
273
 
                                 session->variables.tmp_table_size) /
274
 
                                share->reclength);
275
 
  set_if_bigger(share->max_rows,1);    // For dummy start options
276
 
 
277
 
 
278
 
  //// keyinfo= param->keyinfo;
279
 
  if (true)
280
 
  {
281
 
    share->keys=1;
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";
291
 
    {
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)
299
 
      {
300
 
        if (!(key_field= field->new_key_field(session->mem_root, table,
301
 
                                              group_buff,
302
 
                                              field->null_ptr,
303
 
                                              field->null_bit)))
304
 
          goto err;
305
 
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
306
 
      }
307
 
      keyinfo->key_length+=  key_part_info->length;
308
 
    }
309
 
  }
310
 
 
311
 
  if (session->is_fatal_error)        // If end of memory
312
 
    goto err;
313
 
  share->db_record_offset= 1;
314
 
  if (share->db_type() == myisam_hton)
315
 
  {
316
 
    recinfo++;
317
 
    if (table->create_myisam_tmp_table(keyinfo, start_recinfo, &recinfo, 0))
318
 
      goto err;
319
 
  }
320
 
  sjtbl->start_recinfo= start_recinfo;
321
 
  sjtbl->recinfo=       recinfo;
322
 
  if (table->open_tmp_table())
323
 
    goto err;
324
 
 
325
 
  session->mem_root= mem_root_save;
326
 
  return(table);
327
 
 
328
 
err:
329
 
  session->mem_root= mem_root_save;
330
 
  table->free_tmp_table(session);                    /* purecov: inspected */
331
 
  if (temp_pool_slot != MY_BIT_NONE)
332
 
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
333
 
  return(NULL);        /* purecov: inspected */
334
 
}