~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;
1000.1.3 by Brian Aker
Renamed TABLE_SHARE to TableShare
64
  TableShare *share;
398.1.6 by Monty Taylor
Removed __alpha__ references.
65
  char  *tmpname,path[FN_REFLEN];
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
66
  Field **reg_field;
67
  KEY_PART_INFO *key_part_info;
68
  KEY *keyinfo;
481 by Brian Aker
Remove all of uchar.
69
  unsigned char *group_buff;
1005.2.4 by Monty Taylor
Patch cleaning.
70
  unsigned char *bitmaps;
438.1.13 by Brian Aker
uint cleanup.
71
  uint32_t *blob_field;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
72
  MI_COLUMNDEF *recinfo, *start_recinfo;
73
  bool using_unique_constraint=false;
74
  Field *field, *key_field;
438.1.13 by Brian Aker
uint cleanup.
75
  uint32_t blob_count, null_pack_length, null_count;
481 by Brian Aker
Remove all of uchar.
76
  unsigned char *null_flags;
77
  unsigned char *pos;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
78
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
79
  /*
80
    STEP 1: Get temporary table name
81
  */
520.1.22 by Brian Aker
Second pass of thd cleanup
82
  statistic_increment(session->status_var.created_tmp_tables, &LOCK_status);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
83
1014.2.12 by Monty Taylor
Removed the thread-safe crap in MY_BITMAP. Also remove the temp-pool option for
84
  /* if we run out of slots or we are not using tempool */
85
  sprintf(path,"%s%lx_%"PRIx64"_%x", TMP_FILE_PREFIX, (unsigned long)current_pid,
86
          session->thread_id, session->tmp_table++);
575.4.3 by ysano
Rename mysql to drizzle.
87
  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.
88
89
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
90
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
91
    using_unique_constraint= true;
92
93
  /* STEP 3: Allocate memory for temptable description */
94
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
95
  if (!multi_alloc_root(&own_root,
96
                        &table, sizeof(*table),
97
                        &share, sizeof(*share),
98
                        &reg_field, sizeof(Field*) * (1+1),
438.1.13 by Brian Aker
uint cleanup.
99
                        &blob_field, sizeof(uint32_t)*2,
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
100
                        &keyinfo, sizeof(*keyinfo),
101
                        &key_part_info, sizeof(*key_part_info) * 2,
102
                        &start_recinfo,
103
                        sizeof(*recinfo)*(1*2+4),
438.1.13 by Brian Aker
uint cleanup.
104
                        &tmpname, (uint32_t) strlen(path)+1,
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
105
                        &group_buff, (!using_unique_constraint ?
106
                                      uniq_tuple_length_arg : 0),
1005.2.1 by Monty Taylor
Reverted a crap-ton of padraig's work.
107
                        &bitmaps, bitmap_buffer_size(1)*2,
461 by Monty Taylor
Removed NullS. bu-bye.
108
                        NULL))
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
109
  {
1019.1.6 by Brian Aker
A number of random cleanups.
110
    return NULL;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
111
  }
641.4.1 by Toru Maesaka
First pass of replacing MySQL's my_stpcpy() with appropriate libc calls
112
  strcpy(tmpname,path);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
113
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
114
  /* STEP 4: Create Table description */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
115
  memset(table, 0, sizeof(*table));
116
  memset(reg_field, 0, sizeof(Field*)*2);
117
118
  table->mem_root= own_root;
520.1.22 by Brian Aker
Second pass of thd cleanup
119
  mem_root_save= session->mem_root;
120
  session->mem_root= &table->mem_root;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
121
122
  table->field=reg_field;
123
  table->alias= "weedout-tmp";
398.1.6 by Monty Taylor
Removed __alpha__ references.
124
  table->reginfo.lock_type=TL_WRITE;  /* Will be updated */
784.1.6 by Stewart Smith
merge
125
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
126
  table->map=1;
127
  table->copy_blobs= 1;
520.1.22 by Brian Aker
Second pass of thd cleanup
128
  table->in_use= session;
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
129
  table->quick_keys.reset();
130
  table->covering_keys.reset();
131
  table->keys_in_use_for_query.reset();
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
132
133
  table->s= share;
1000.1.5 by Brian Aker
More refactoring back to TableShare object.
134
  share->init(tmpname, tmpname);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
135
  share->blob_field= blob_field;
136
  share->blob_ptr_size= portable_sizeof_char_ptr;
137
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
138
  share->table_charset= NULL;
139
  share->primary_key= MAX_KEY;               // Indicate no primary key
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
140
  share->keys_for_keyread.reset();
141
  share->keys_in_use.reset();
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
142
143
  blob_count= 0;
144
145
  /* Create the field */
146
  {
147
    /*
148
      For the sake of uniformity, always use Field_varstring.
149
    */
150
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
151
                               &my_charset_bin);
152
    if (!field)
153
      return(0);
154
    field->table= table;
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
155
    field->key_start.reset();
156
    field->part_of_key.reset();
157
    field->part_of_sortkey.reset();
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
158
    field->unireg_check= Field::NONE;
159
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
160
    field->reset_fields();
161
    field->init(table);
162
    field->orig_table= NULL;
398.1.6 by Monty Taylor
Removed __alpha__ references.
163
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
164
    field->field_index= 0;
398.1.6 by Monty Taylor
Removed __alpha__ references.
165
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
166
    *(reg_field++)= field;
167
    *blob_field= 0;
168
    *reg_field= 0;
169
170
    share->fields= 1;
171
    share->blob_fields= 0;
172
  }
173
438.1.13 by Brian Aker
uint cleanup.
174
  uint32_t reclength= field->pack_length();
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
175
  if (using_unique_constraint)
398.1.6 by Monty Taylor
Removed __alpha__ references.
176
  {
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
177
    share->storage_engine= myisam_engine;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
178
    table->file= get_new_handler(share, &table->mem_root,
179
                                 share->db_type());
180
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
181
  }
182
  else
183
  {
971.1.21 by Monty Taylor
Store StorageEngine in system variables, rather than storage engine plugin.
184
    share->storage_engine= heap_engine;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
185
    table->file= get_new_handler(share, &table->mem_root,
186
                                 share->db_type());
187
  }
188
  if (!table->file)
189
    goto err;
190
191
  null_count=1;
398.1.6 by Monty Taylor
Removed __alpha__ references.
192
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
193
  null_pack_length= 1;
194
  reclength += null_pack_length;
195
196
  share->reclength= reclength;
197
  {
438.1.13 by Brian Aker
uint cleanup.
198
    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.
199
    share->rec_buff_length= alloc_length;
481 by Brian Aker
Remove all of uchar.
200
    if (!(table->record[0]= (unsigned char*)
398.1.6 by Monty Taylor
Removed __alpha__ references.
201
          alloc_root(&table->mem_root, alloc_length*3)))
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
202
      goto err;
203
    table->record[1]= table->record[0]+alloc_length;
204
    share->default_values= table->record[1]+alloc_length;
205
  }
1005.2.3 by Monty Taylor
Further reversion of P.
206
  table->setup_tmp_table_column_bitmaps(bitmaps);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
207
208
  recinfo= start_recinfo;
481 by Brian Aker
Remove all of uchar.
209
  null_flags=(unsigned char*) table->record[0];
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
210
  pos=table->record[0]+ null_pack_length;
211
  if (null_pack_length)
212
  {
213
    memset(recinfo, 0, sizeof(*recinfo));
214
    recinfo->type=FIELD_NORMAL;
215
    recinfo->length=null_pack_length;
216
    recinfo++;
398.1.6 by Monty Taylor
Removed __alpha__ references.
217
    memset(null_flags, 255, null_pack_length);  // Set null fields
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
218
481 by Brian Aker
Remove all of uchar.
219
    table->null_flags= (unsigned char*) table->record[0];
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
220
    share->null_fields= null_count;
221
    share->null_bytes= null_pack_length;
222
  }
223
  null_count=1;
224
225
  {
226
    //Field *field= *reg_field;
438.1.13 by Brian Aker
uint cleanup.
227
    uint32_t length;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
228
    memset(recinfo, 0, sizeof(*recinfo));
481 by Brian Aker
Remove all of uchar.
229
    field->move_field(pos,(unsigned char*) 0,0);
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
230
231
    field->reset();
232
    /*
233
      Test if there is a default field value. The test for ->ptr is to skip
234
      'offset' fields generated by initalize_tables
235
    */
236
    // Initialize the table field:
237
    memset(field->ptr, 0, field->pack_length());
238
239
    length=field->pack_length();
240
    pos+= length;
241
242
    /* Make entry for create table */
243
    recinfo->length=length;
244
    if (field->flags & BLOB_FLAG)
245
      recinfo->type= (int) FIELD_BLOB;
246
    else
247
      recinfo->type=FIELD_NORMAL;
248
249
    field->table_name= &table->alias;
250
  }
251
252
  //param->recinfo=recinfo;
997.5.1 by chris
Replace macros around unireg.h, store_record,restore_record,cmp_record,empty_record
253
  //table->storeRecordAsDefault();        // Make empty default record
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
254
520.1.22 by Brian Aker
Second pass of thd cleanup
255
  if (session->variables.tmp_table_size == ~ (uint64_t) 0)    // No limit
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
256
    share->max_rows= ~(ha_rows) 0;
257
  else
960.2.25 by Monty Taylor
First step of hton rename.
258
    share->max_rows= (ha_rows) (((share->db_type() == heap_engine) ?
520.1.22 by Brian Aker
Second pass of thd cleanup
259
                                 cmin(session->variables.tmp_table_size,
260
                                      session->variables.max_heap_table_size) :
261
                                 session->variables.tmp_table_size) /
398.1.6 by Monty Taylor
Removed __alpha__ references.
262
                                share->reclength);
937.2.6 by Stewart Smith
make set_if_bigger typesafe for C and C++. Fix up everywhere.
263
  set_if_bigger(share->max_rows,(ha_rows)1);    // For dummy start options
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
264
265
266
  //// keyinfo= param->keyinfo;
267
  if (true)
268
  {
269
    share->keys=1;
270
    share->uniques= test(using_unique_constraint);
271
    table->key_info=keyinfo;
272
    keyinfo->key_part=key_part_info;
273
    keyinfo->flags=HA_NOSAME;
274
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
275
    keyinfo->key_length=0;
276
    keyinfo->rec_per_key=0;
277
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
278
    keyinfo->name= (char*) "weedout_key";
279
    {
280
      key_part_info->null_bit=0;
281
      key_part_info->field=  field;
282
      key_part_info->offset= field->offset(table->record[0]);
283
      key_part_info->length= (uint16_t) field->key_length();
284
      key_part_info->type=   (uint8_t) field->key_type();
285
      key_part_info->key_type = FIELDFLAG_BINARY;
286
      if (!using_unique_constraint)
287
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
288
        if (!(key_field= field->new_key_field(session->mem_root, table,
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
289
                                              group_buff,
290
                                              field->null_ptr,
291
                                              field->null_bit)))
398.1.6 by Monty Taylor
Removed __alpha__ references.
292
          goto err;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
293
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
294
      }
295
      keyinfo->key_length+=  key_part_info->length;
296
    }
297
  }
298
520.1.22 by Brian Aker
Second pass of thd cleanup
299
  if (session->is_fatal_error)        // If end of memory
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
300
    goto err;
301
  share->db_record_offset= 1;
960.2.25 by Monty Taylor
First step of hton rename.
302
  if (share->db_type() == myisam_engine)
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
303
  {
304
    recinfo++;
305
    if (table->create_myisam_tmp_table(keyinfo, start_recinfo, &recinfo, 0))
306
      goto err;
307
  }
308
  sjtbl->start_recinfo= start_recinfo;
309
  sjtbl->recinfo=       recinfo;
310
  if (table->open_tmp_table())
311
    goto err;
312
520.1.22 by Brian Aker
Second pass of thd cleanup
313
  session->mem_root= mem_root_save;
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
314
  return(table);
315
316
err:
520.1.22 by Brian Aker
Second pass of thd cleanup
317
  session->mem_root= mem_root_save;
318
  table->free_tmp_table(session);                    /* purecov: inspected */
1019.1.6 by Brian Aker
A number of random cleanups.
319
  return NULL;        /* purecov: inspected */
327.1.2 by Brian Aker
Commiting next pass of Table class cleanup.
320
}