~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sj_tmp_table.cc

  • Committer: Monty Taylor
  • Date: 2008-07-05 18:10:38 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: monty@inaugust.com-20080705181038-0ih0nnamu5qrut0y
Fixed prototypes. Cleaned define a little bit.

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 "sj_tmp_table.h"
21
 
 
22
 
/*
23
 
  Create a temporary table to weed out duplicate rowid combinations
24
 
 
25
 
  SYNOPSIS
26
 
 
27
 
  create_duplicate_weedout_tmp_table()
28
 
  thd
29
 
  uniq_tuple_length_arg
30
 
  SJ_TMP_TABLE
31
 
 
32
 
  DESCRIPTION
33
 
  Create a temporary table to weed out duplicate rowid combinations. The
34
 
  table has a single column that is a concatenation of all rowids in the
35
 
  combination.
36
 
 
37
 
  Depending on the needed length, there are two cases:
38
 
 
39
 
  1. When the length of the column < max_key_length:
40
 
 
41
 
  CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
42
 
 
43
 
  2. Otherwise (not a valid SQL syntax but internally supported):
44
 
 
45
 
  CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
46
 
 
47
 
  The code in this function was produced by extraction of relevant parts
48
 
  from create_tmp_table().
49
 
 
50
 
  RETURN
51
 
  created table
52
 
  NULL on error
53
 
*/
54
 
 
55
 
Table *create_duplicate_weedout_tmp_table(THD *thd,
56
 
                                          uint32_t uniq_tuple_length_arg,
57
 
                                          SJ_TMP_TABLE *sjtbl)
58
 
{
59
 
  MEM_ROOT *mem_root_save, own_root;
60
 
  Table *table;
61
 
  TABLE_SHARE *share;
62
 
  uint32_t  temp_pool_slot=MY_BIT_NONE;
63
 
  char  *tmpname,path[FN_REFLEN];
64
 
  Field **reg_field;
65
 
  KEY_PART_INFO *key_part_info;
66
 
  KEY *keyinfo;
67
 
  unsigned char *group_buff;
68
 
  unsigned char *bitmaps;
69
 
  uint32_t *blob_field;
70
 
  MI_COLUMNDEF *recinfo, *start_recinfo;
71
 
  bool using_unique_constraint=false;
72
 
  Field *field, *key_field;
73
 
  uint32_t blob_count, null_pack_length, null_count;
74
 
  unsigned char *null_flags;
75
 
  unsigned char *pos;
76
 
  
77
 
  /*
78
 
    STEP 1: Get temporary table name
79
 
  */
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);
83
 
 
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);
87
 
  else
88
 
  {
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++);
92
 
  }
93
 
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
94
 
 
95
 
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
96
 
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
97
 
    using_unique_constraint= true;
98
 
 
99
 
  /* STEP 3: Allocate memory for temptable description */
100
 
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
101
 
  if (!multi_alloc_root(&own_root,
102
 
                        &table, sizeof(*table),
103
 
                        &share, sizeof(*share),
104
 
                        &reg_field, sizeof(Field*) * (1+1),
105
 
                        &blob_field, sizeof(uint32_t)*2,
106
 
                        &keyinfo, sizeof(*keyinfo),
107
 
                        &key_part_info, sizeof(*key_part_info) * 2,
108
 
                        &start_recinfo,
109
 
                        sizeof(*recinfo)*(1*2+4),
110
 
                        &tmpname, (uint32_t) strlen(path)+1,
111
 
                        &group_buff, (!using_unique_constraint ?
112
 
                                      uniq_tuple_length_arg : 0),
113
 
                        &bitmaps, bitmap_buffer_size(1)*2,
114
 
                        NULL))
115
 
  {
116
 
    if (temp_pool_slot != MY_BIT_NONE)
117
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
118
 
    return(NULL);
119
 
  }
120
 
  my_stpcpy(tmpname,path);
121
 
 
122
 
  /* STEP 4: Create Table description */
123
 
  memset(table, 0, sizeof(*table));
124
 
  memset(reg_field, 0, sizeof(Field*)*2);
125
 
 
126
 
  table->mem_root= own_root;
127
 
  mem_root_save= thd->mem_root;
128
 
  thd->mem_root= &table->mem_root;
129
 
 
130
 
  table->field=reg_field;
131
 
  table->alias= "weedout-tmp";
132
 
  table->reginfo.lock_type=TL_WRITE;  /* Will be updated */
133
 
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
134
 
  table->map=1;
135
 
  table->temp_pool_slot = temp_pool_slot;
136
 
  table->copy_blobs= 1;
137
 
  table->in_use= thd;
138
 
  table->quick_keys.init();
139
 
  table->covering_keys.init();
140
 
  table->keys_in_use_for_query.init();
141
 
 
142
 
  table->s= share;
143
 
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
144
 
  share->blob_field= blob_field;
145
 
  share->blob_ptr_size= portable_sizeof_char_ptr;
146
 
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
147
 
  share->table_charset= NULL;
148
 
  share->primary_key= MAX_KEY;               // Indicate no primary key
149
 
  share->keys_for_keyread.init();
150
 
  share->keys_in_use.init();
151
 
 
152
 
  blob_count= 0;
153
 
 
154
 
  /* Create the field */
155
 
  {
156
 
    /*
157
 
      For the sake of uniformity, always use Field_varstring.
158
 
    */
159
 
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
160
 
                               &my_charset_bin);
161
 
    if (!field)
162
 
      return(0);
163
 
    field->table= table;
164
 
    field->key_start.init(0);
165
 
    field->part_of_key.init(0);
166
 
    field->part_of_sortkey.init(0);
167
 
    field->unireg_check= Field::NONE;
168
 
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
169
 
    field->reset_fields();
170
 
    field->init(table);
171
 
    field->orig_table= NULL;
172
 
 
173
 
    field->field_index= 0;
174
 
 
175
 
    *(reg_field++)= field;
176
 
    *blob_field= 0;
177
 
    *reg_field= 0;
178
 
 
179
 
    share->fields= 1;
180
 
    share->blob_fields= 0;
181
 
  }
182
 
 
183
 
  uint32_t reclength= field->pack_length();
184
 
  if (using_unique_constraint)
185
 
  {
186
 
    share->db_plugin= ha_lock_engine(0, myisam_hton);
187
 
    table->file= get_new_handler(share, &table->mem_root,
188
 
                                 share->db_type());
189
 
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
190
 
  }
191
 
  else
192
 
  {
193
 
    share->db_plugin= ha_lock_engine(0, heap_hton);
194
 
    table->file= get_new_handler(share, &table->mem_root,
195
 
                                 share->db_type());
196
 
  }
197
 
  if (!table->file)
198
 
    goto err;
199
 
 
200
 
  null_count=1;
201
 
 
202
 
  null_pack_length= 1;
203
 
  reclength += null_pack_length;
204
 
 
205
 
  share->reclength= reclength;
206
 
  {
207
 
    uint32_t alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
208
 
    share->rec_buff_length= alloc_length;
209
 
    if (!(table->record[0]= (unsigned char*)
210
 
          alloc_root(&table->mem_root, alloc_length*3)))
211
 
      goto err;
212
 
    table->record[1]= table->record[0]+alloc_length;
213
 
    share->default_values= table->record[1]+alloc_length;
214
 
  }
215
 
  table->setup_tmp_table_column_bitmaps(bitmaps);
216
 
 
217
 
  recinfo= start_recinfo;
218
 
  null_flags=(unsigned char*) table->record[0];
219
 
  pos=table->record[0]+ null_pack_length;
220
 
  if (null_pack_length)
221
 
  {
222
 
    memset(recinfo, 0, sizeof(*recinfo));
223
 
    recinfo->type=FIELD_NORMAL;
224
 
    recinfo->length=null_pack_length;
225
 
    recinfo++;
226
 
    memset(null_flags, 255, null_pack_length);  // Set null fields
227
 
 
228
 
    table->null_flags= (unsigned char*) table->record[0];
229
 
    share->null_fields= null_count;
230
 
    share->null_bytes= null_pack_length;
231
 
  }
232
 
  null_count=1;
233
 
 
234
 
  {
235
 
    //Field *field= *reg_field;
236
 
    uint32_t length;
237
 
    memset(recinfo, 0, sizeof(*recinfo));
238
 
    field->move_field(pos,(unsigned char*) 0,0);
239
 
 
240
 
    field->reset();
241
 
    /*
242
 
      Test if there is a default field value. The test for ->ptr is to skip
243
 
      'offset' fields generated by initalize_tables
244
 
    */
245
 
    // Initialize the table field:
246
 
    memset(field->ptr, 0, field->pack_length());
247
 
 
248
 
    length=field->pack_length();
249
 
    pos+= length;
250
 
 
251
 
    /* Make entry for create table */
252
 
    recinfo->length=length;
253
 
    if (field->flags & BLOB_FLAG)
254
 
      recinfo->type= (int) FIELD_BLOB;
255
 
    else
256
 
      recinfo->type=FIELD_NORMAL;
257
 
 
258
 
    field->table_name= &table->alias;
259
 
  }
260
 
 
261
 
  //param->recinfo=recinfo;
262
 
  //store_record(table,s->default_values);        // Make empty default record
263
 
 
264
 
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)    // No limit
265
 
    share->max_rows= ~(ha_rows) 0;
266
 
  else
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) /
271
 
                                share->reclength);
272
 
  set_if_bigger(share->max_rows,1);    // For dummy start options
273
 
 
274
 
 
275
 
  //// keyinfo= param->keyinfo;
276
 
  if (true)
277
 
  {
278
 
    share->keys=1;
279
 
    share->uniques= test(using_unique_constraint);
280
 
    table->key_info=keyinfo;
281
 
    keyinfo->key_part=key_part_info;
282
 
    keyinfo->flags=HA_NOSAME;
283
 
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
284
 
    keyinfo->key_length=0;
285
 
    keyinfo->rec_per_key=0;
286
 
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
287
 
    keyinfo->name= (char*) "weedout_key";
288
 
    {
289
 
      key_part_info->null_bit=0;
290
 
      key_part_info->field=  field;
291
 
      key_part_info->offset= field->offset(table->record[0]);
292
 
      key_part_info->length= (uint16_t) field->key_length();
293
 
      key_part_info->type=   (uint8_t) field->key_type();
294
 
      key_part_info->key_type = FIELDFLAG_BINARY;
295
 
      if (!using_unique_constraint)
296
 
      {
297
 
        if (!(key_field= field->new_key_field(thd->mem_root, table,
298
 
                                              group_buff,
299
 
                                              field->null_ptr,
300
 
                                              field->null_bit)))
301
 
          goto err;
302
 
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
303
 
      }
304
 
      keyinfo->key_length+=  key_part_info->length;
305
 
    }
306
 
  }
307
 
 
308
 
  if (thd->is_fatal_error)        // If end of memory
309
 
    goto err;
310
 
  share->db_record_offset= 1;
311
 
  if (share->db_type() == myisam_hton)
312
 
  {
313
 
    recinfo++;
314
 
    if (table->create_myisam_tmp_table(keyinfo, start_recinfo, &recinfo, 0))
315
 
      goto err;
316
 
  }
317
 
  sjtbl->start_recinfo= start_recinfo;
318
 
  sjtbl->recinfo=       recinfo;
319
 
  if (table->open_tmp_table())
320
 
    goto err;
321
 
 
322
 
  thd->mem_root= mem_root_save;
323
 
  return(table);
324
 
 
325
 
err:
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 */
331
 
}