~drizzle-trunk/drizzle/development

398.1.6 by Monty Taylor
Removed __alpha__ references.
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
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
20
#include "sj_tmp_table.h"
21
22
/*
23
  Create a temporary table to weed out duplicate rowid combinations
24
25
  SYNOPSIS
26
398.1.6 by Monty Taylor
Removed __alpha__ references.
27
  create_duplicate_weedout_tmp_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
28
  session
398.1.6 by Monty Taylor
Removed __alpha__ references.
29
  uniq_tuple_length_arg
30
  SJ_TMP_TABLE
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
31
32
  DESCRIPTION
398.1.6 by Monty Taylor
Removed __alpha__ references.
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().
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
49
50
  RETURN
398.1.6 by Monty Taylor
Removed __alpha__ references.
51
  created table
52
  NULL on error
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
53
*/
54
520.1.22 by Brian Aker
Second pass of thd cleanup
55
Table *create_duplicate_weedout_tmp_table(Session *session,
438.1.13 by Brian Aker
uint cleanup.
56
                                          uint32_t uniq_tuple_length_arg,
398.1.6 by Monty Taylor
Removed __alpha__ references.
57
                                          SJ_TMP_TABLE *sjtbl)
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
58
{
59
  MEM_ROOT *mem_root_save, own_root;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
60
  Table *table;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
61
  TABLE_SHARE *share;
438.1.13 by Brian Aker
uint cleanup.
62
  uint32_t  temp_pool_slot=MY_BIT_NONE;
398.1.6 by Monty Taylor
Removed __alpha__ references.
63
  char  *tmpname,path[FN_REFLEN];
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
64
  Field **reg_field;
65
  KEY_PART_INFO *key_part_info;
66
  KEY *keyinfo;
481 by Brian Aker
Remove all of uchar.
67
  unsigned char *group_buff;
68
  unsigned char *bitmaps;
438.1.13 by Brian Aker
uint cleanup.
69
  uint32_t *blob_field;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
70
  MI_COLUMNDEF *recinfo, *start_recinfo;
71
  bool using_unique_constraint=false;
72
  Field *field, *key_field;
438.1.13 by Brian Aker
uint cleanup.
73
  uint32_t blob_count, null_pack_length, null_count;
481 by Brian Aker
Remove all of uchar.
74
  unsigned char *null_flags;
75
  unsigned char *pos;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
76
  
77
  /*
78
    STEP 1: Get temporary table name
79
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
80
  statistic_increment(session->status_var.created_tmp_tables, &LOCK_status);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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 */
555 by Monty
Fixed 32-bit issues.
90
    sprintf(path,"%s%lx_%"PRIx64"_%x", tmp_file_prefix,current_pid,
520.1.22 by Brian Aker
Second pass of thd cleanup
91
            session->thread_id, session->tmp_table++);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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),
438.1.13 by Brian Aker
uint cleanup.
105
                        &blob_field, sizeof(uint32_t)*2,
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
106
                        &keyinfo, sizeof(*keyinfo),
107
                        &key_part_info, sizeof(*key_part_info) * 2,
108
                        &start_recinfo,
109
                        sizeof(*recinfo)*(1*2+4),
438.1.13 by Brian Aker
uint cleanup.
110
                        &tmpname, (uint32_t) strlen(path)+1,
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
111
                        &group_buff, (!using_unique_constraint ?
112
                                      uniq_tuple_length_arg : 0),
113
                        &bitmaps, bitmap_buffer_size(1)*2,
461 by Monty Taylor
Removed NullS. bu-bye.
114
                        NULL))
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
115
  {
116
    if (temp_pool_slot != MY_BIT_NONE)
117
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
118
    return(NULL);
119
  }
411.1.1 by Brian Aker
Work on removing GNU specific calls.
120
  my_stpcpy(tmpname,path);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
121
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
122
  /* STEP 4: Create Table description */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
123
  memset(table, 0, sizeof(*table));
124
  memset(reg_field, 0, sizeof(Field*)*2);
125
126
  table->mem_root= own_root;
520.1.22 by Brian Aker
Second pass of thd cleanup
127
  mem_root_save= session->mem_root;
128
  session->mem_root= &table->mem_root;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
129
130
  table->field=reg_field;
131
  table->alias= "weedout-tmp";
398.1.6 by Monty Taylor
Removed __alpha__ references.
132
  table->reginfo.lock_type=TL_WRITE;  /* Will be updated */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
520.1.22 by Brian Aker
Second pass of thd cleanup
137
  table->in_use= session;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
138
  table->quick_keys.init();
139
  table->covering_keys.init();
140
  table->keys_in_use_for_query.init();
141
142
  table->s= share;
520.1.22 by Brian Aker
Second pass of thd cleanup
143
  init_tmp_table_share(session, share, "", 0, tmpname, tmpname);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
398.1.6 by Monty Taylor
Removed __alpha__ references.
172
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
173
    field->field_index= 0;
398.1.6 by Monty Taylor
Removed __alpha__ references.
174
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
175
    *(reg_field++)= field;
176
    *blob_field= 0;
177
    *reg_field= 0;
178
179
    share->fields= 1;
180
    share->blob_fields= 0;
181
  }
182
438.1.13 by Brian Aker
uint cleanup.
183
  uint32_t reclength= field->pack_length();
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
184
  if (using_unique_constraint)
398.1.6 by Monty Taylor
Removed __alpha__ references.
185
  {
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
398.1.6 by Monty Taylor
Removed __alpha__ references.
201
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
202
  null_pack_length= 1;
203
  reclength += null_pack_length;
204
205
  share->reclength= reclength;
206
  {
438.1.13 by Brian Aker
uint cleanup.
207
    uint32_t alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
208
    share->rec_buff_length= alloc_length;
481 by Brian Aker
Remove all of uchar.
209
    if (!(table->record[0]= (unsigned char*)
398.1.6 by Monty Taylor
Removed __alpha__ references.
210
          alloc_root(&table->mem_root, alloc_length*3)))
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
481 by Brian Aker
Remove all of uchar.
218
  null_flags=(unsigned char*) table->record[0];
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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++;
398.1.6 by Monty Taylor
Removed __alpha__ references.
226
    memset(null_flags, 255, null_pack_length);  // Set null fields
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
227
481 by Brian Aker
Remove all of uchar.
228
    table->null_flags= (unsigned char*) table->record[0];
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
438.1.13 by Brian Aker
uint cleanup.
236
    uint32_t length;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
237
    memset(recinfo, 0, sizeof(*recinfo));
481 by Brian Aker
Remove all of uchar.
238
    field->move_field(pos,(unsigned char*) 0,0);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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
520.1.22 by Brian Aker
Second pass of thd cleanup
264
  if (session->variables.tmp_table_size == ~ (uint64_t) 0)    // No limit
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
265
    share->max_rows= ~(ha_rows) 0;
266
  else
267
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
520.1.22 by Brian Aker
Second pass of thd cleanup
268
                                 cmin(session->variables.tmp_table_size,
269
                                      session->variables.max_heap_table_size) :
270
                                 session->variables.tmp_table_size) /
398.1.6 by Monty Taylor
Removed __alpha__ references.
271
                                share->reclength);
272
  set_if_bigger(share->max_rows,1);    // For dummy start options
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
297
        if (!(key_field= field->new_key_field(session->mem_root, table,
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
298
                                              group_buff,
299
                                              field->null_ptr,
300
                                              field->null_bit)))
398.1.6 by Monty Taylor
Removed __alpha__ references.
301
          goto err;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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
520.1.22 by Brian Aker
Second pass of thd cleanup
308
  if (session->is_fatal_error)        // If end of memory
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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
520.1.22 by Brian Aker
Second pass of thd cleanup
322
  session->mem_root= mem_root_save;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
323
  return(table);
324
325
err:
520.1.22 by Brian Aker
Second pass of thd cleanup
326
  session->mem_root= mem_root_save;
327
  table->free_tmp_table(session);                    /* purecov: inspected */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
328
  if (temp_pool_slot != MY_BIT_NONE)
329
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
398.1.6 by Monty Taylor
Removed __alpha__ references.
330
  return(NULL);        /* purecov: inspected */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
331
}