~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
612.2.4 by Monty Taylor
Moved some defines to config.h. Stopped including config.h directly anywhere.
20
#include <drizzled/global.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
21
#include <drizzled/sj_tmp_table.h>
22
#include <drizzled/session.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
23
#include <drizzled/field/varstring.h>
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
24
25
/*
26
  Create a temporary table to weed out duplicate rowid combinations
27
28
  SYNOPSIS
29
398.1.6 by Monty Taylor
Removed __alpha__ references.
30
  create_duplicate_weedout_tmp_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
31
  session
398.1.6 by Monty Taylor
Removed __alpha__ references.
32
  uniq_tuple_length_arg
33
  SJ_TMP_TABLE
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
34
35
  DESCRIPTION
398.1.6 by Monty Taylor
Removed __alpha__ references.
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().
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
52
53
  RETURN
398.1.6 by Monty Taylor
Removed __alpha__ references.
54
  created table
55
  NULL on error
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
56
*/
57
520.1.22 by Brian Aker
Second pass of thd cleanup
58
Table *create_duplicate_weedout_tmp_table(Session *session,
438.1.13 by Brian Aker
uint cleanup.
59
                                          uint32_t uniq_tuple_length_arg,
398.1.6 by Monty Taylor
Removed __alpha__ references.
60
                                          SJ_TMP_TABLE *sjtbl)
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
61
{
62
  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
63
  Table *table;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
64
  TABLE_SHARE *share;
438.1.13 by Brian Aker
uint cleanup.
65
  uint32_t  temp_pool_slot=MY_BIT_NONE;
398.1.6 by Monty Taylor
Removed __alpha__ references.
66
  char  *tmpname,path[FN_REFLEN];
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
67
  Field **reg_field;
68
  KEY_PART_INFO *key_part_info;
69
  KEY *keyinfo;
481 by Brian Aker
Remove all of uchar.
70
  unsigned char *group_buff;
71
  unsigned char *bitmaps;
438.1.13 by Brian Aker
uint cleanup.
72
  uint32_t *blob_field;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
73
  MI_COLUMNDEF *recinfo, *start_recinfo;
74
  bool using_unique_constraint=false;
75
  Field *field, *key_field;
438.1.13 by Brian Aker
uint cleanup.
76
  uint32_t blob_count, null_pack_length, null_count;
481 by Brian Aker
Remove all of uchar.
77
  unsigned char *null_flags;
78
  unsigned char *pos;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
79
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
80
  /*
81
    STEP 1: Get temporary table name
82
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
83
  statistic_increment(session->status_var.created_tmp_tables, &LOCK_status);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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
584.2.1 by Stewart Smith
convert tmp_file_prefix to TMP_FILE_PREFIX to indicate it's a macro
88
    sprintf(path, "%s_%lx_%i", TMP_FILE_PREFIX,
606 by Brian Aker
Remove dead lex structure and clean up use of pid_t
89
	    (unsigned long)current_pid, temp_pool_slot);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
90
  else
91
  {
92
    /* if we run out of slots or we are not using tempool */
606 by Brian Aker
Remove dead lex structure and clean up use of pid_t
93
    sprintf(path,"%s%lx_%"PRIx64"_%x", TMP_FILE_PREFIX, (unsigned long)current_pid,
520.1.22 by Brian Aker
Second pass of thd cleanup
94
            session->thread_id, session->tmp_table++);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
95
  }
575.4.3 by ysano
Rename mysql to drizzle.
96
  fn_format(path, path, drizzle_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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),
438.1.13 by Brian Aker
uint cleanup.
108
                        &blob_field, sizeof(uint32_t)*2,
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
109
                        &keyinfo, sizeof(*keyinfo),
110
                        &key_part_info, sizeof(*key_part_info) * 2,
111
                        &start_recinfo,
112
                        sizeof(*recinfo)*(1*2+4),
438.1.13 by Brian Aker
uint cleanup.
113
                        &tmpname, (uint32_t) strlen(path)+1,
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
114
                        &group_buff, (!using_unique_constraint ?
115
                                      uniq_tuple_length_arg : 0),
116
                        &bitmaps, bitmap_buffer_size(1)*2,
461 by Monty Taylor
Removed NullS. bu-bye.
117
                        NULL))
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
118
  {
119
    if (temp_pool_slot != MY_BIT_NONE)
120
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
121
    return(NULL);
122
  }
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
123
  strcpy(tmpname,path);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
124
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
125
  /* STEP 4: Create Table description */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
126
  memset(table, 0, sizeof(*table));
127
  memset(reg_field, 0, sizeof(Field*)*2);
128
129
  table->mem_root= own_root;
520.1.22 by Brian Aker
Second pass of thd cleanup
130
  mem_root_save= session->mem_root;
131
  session->mem_root= &table->mem_root;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
132
133
  table->field=reg_field;
134
  table->alias= "weedout-tmp";
398.1.6 by Monty Taylor
Removed __alpha__ references.
135
  table->reginfo.lock_type=TL_WRITE;  /* Will be updated */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
520.1.22 by Brian Aker
Second pass of thd cleanup
140
  table->in_use= session;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
141
  table->quick_keys.init();
142
  table->covering_keys.init();
143
  table->keys_in_use_for_query.init();
144
145
  table->s= share;
520.1.22 by Brian Aker
Second pass of thd cleanup
146
  init_tmp_table_share(session, share, "", 0, tmpname, tmpname);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
398.1.6 by Monty Taylor
Removed __alpha__ references.
175
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
176
    field->field_index= 0;
398.1.6 by Monty Taylor
Removed __alpha__ references.
177
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
178
    *(reg_field++)= field;
179
    *blob_field= 0;
180
    *reg_field= 0;
181
182
    share->fields= 1;
183
    share->blob_fields= 0;
184
  }
185
438.1.13 by Brian Aker
uint cleanup.
186
  uint32_t reclength= field->pack_length();
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
187
  if (using_unique_constraint)
398.1.6 by Monty Taylor
Removed __alpha__ references.
188
  {
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
398.1.6 by Monty Taylor
Removed __alpha__ references.
204
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
205
  null_pack_length= 1;
206
  reclength += null_pack_length;
207
208
  share->reclength= reclength;
209
  {
438.1.13 by Brian Aker
uint cleanup.
210
    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.
211
    share->rec_buff_length= alloc_length;
481 by Brian Aker
Remove all of uchar.
212
    if (!(table->record[0]= (unsigned char*)
398.1.6 by Monty Taylor
Removed __alpha__ references.
213
          alloc_root(&table->mem_root, alloc_length*3)))
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
481 by Brian Aker
Remove all of uchar.
221
  null_flags=(unsigned char*) table->record[0];
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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++;
398.1.6 by Monty Taylor
Removed __alpha__ references.
229
    memset(null_flags, 255, null_pack_length);  // Set null fields
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
230
481 by Brian Aker
Remove all of uchar.
231
    table->null_flags= (unsigned char*) table->record[0];
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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;
438.1.13 by Brian Aker
uint cleanup.
239
    uint32_t length;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
240
    memset(recinfo, 0, sizeof(*recinfo));
481 by Brian Aker
Remove all of uchar.
241
    field->move_field(pos,(unsigned char*) 0,0);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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
520.1.22 by Brian Aker
Second pass of thd cleanup
267
  if (session->variables.tmp_table_size == ~ (uint64_t) 0)    // No limit
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
268
    share->max_rows= ~(ha_rows) 0;
269
  else
270
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
520.1.22 by Brian Aker
Second pass of thd cleanup
271
                                 cmin(session->variables.tmp_table_size,
272
                                      session->variables.max_heap_table_size) :
273
                                 session->variables.tmp_table_size) /
398.1.6 by Monty Taylor
Removed __alpha__ references.
274
                                share->reclength);
275
  set_if_bigger(share->max_rows,1);    // For dummy start options
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
300
        if (!(key_field= field->new_key_field(session->mem_root, table,
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
301
                                              group_buff,
302
                                              field->null_ptr,
303
                                              field->null_bit)))
398.1.6 by Monty Taylor
Removed __alpha__ references.
304
          goto err;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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
520.1.22 by Brian Aker
Second pass of thd cleanup
311
  if (session->is_fatal_error)        // If end of memory
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
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
520.1.22 by Brian Aker
Second pass of thd cleanup
325
  session->mem_root= mem_root_save;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
326
  return(table);
327
328
err:
520.1.22 by Brian Aker
Second pass of thd cleanup
329
  session->mem_root= mem_root_save;
330
  table->free_tmp_table(session);                    /* purecov: inspected */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
331
  if (temp_pool_slot != MY_BIT_NONE)
332
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
398.1.6 by Monty Taylor
Removed __alpha__ references.
333
  return(NULL);        /* purecov: inspected */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
334
}