~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sj_tmp_table.cc

  • Committer: Brian Aker
  • Date: 2008-08-15 21:14:46 UTC
  • mto: This revision was merged to the branch mainline in revision 346.
  • Revision ID: brian@tangent.org-20080815211446-aqtozo1hoe8mb2az
Commiting next pass of Table class cleanup.

Show diffs side-by-side

added added

removed removed

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