~ubuntu-branches/ubuntu/quantal/drizzle/quantal

1 by Monty Taylor
Import upstream version 2010.03.1347
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1.2.4 by Monty Taylor
Import upstream version 2010.12.06
4
 *  Copyright (C) 2008-2009 Sun Microsystems, Inc.
1 by Monty Taylor
Import upstream version 2010.03.1347
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; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
21
/**
22
 * @file
23
 *
24
 * Implementation of the JOIN cache
25
 * 
26
 * @defgroup Query_Optimizer  Query Optimizer
27
 * @{
28
 */
29
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
30
#include <config.h>
1.2.8 by Monty Taylor
Import upstream version 2011.02.10
31
1.2.9 by Monty Taylor
Import upstream version 2011.03.11
32
#include <drizzled/sql_select.h> /* include join.h */
33
#include <drizzled/field/blob.h>
34
#include <drizzled/drizzled.h>
35
#include <drizzled/internal/my_sys.h>
1.2.8 by Monty Taylor
Import upstream version 2011.02.10
36
#include <drizzled/table.h>
37
#include <drizzled/session.h>
1 by Monty Taylor
Import upstream version 2010.03.1347
38
39
#include <algorithm>
40
41
using namespace std;
42
43
namespace drizzled
44
{
45
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
46
static uint32_t used_blob_length(CacheField **ptr);
1 by Monty Taylor
Import upstream version 2010.03.1347
47
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
48
static uint32_t used_blob_length(CacheField **ptr)
1 by Monty Taylor
Import upstream version 2010.03.1347
49
{
50
  uint32_t length,blob_length;
51
  for (length=0 ; *ptr ; ptr++)
52
  {
53
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
54
    length+=blob_length;
55
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
56
  }
57
  return length;
58
}
59
60
/*****************************************************************************
61
  Fill join cache with packed records
62
  Records are stored in tab->cache.buffer and last record in
63
  last record is stored with pointers to blobs to support very big
64
  records
65
******************************************************************************/
66
int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
67
{
68
  unsigned int length, blobs;
69
  size_t size;
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
70
  CacheField *copy,**blob_ptr;
71
  JoinCache  *cache;
1 by Monty Taylor
Import upstream version 2010.03.1347
72
  JoinTable *join_tab;
73
74
  cache= &tables[table_count].cache;
75
  cache->fields=blobs=0;
76
77
  join_tab= tables;
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
78
  for (unsigned int i= 0; i < table_count ; i++, join_tab++)
1 by Monty Taylor
Import upstream version 2010.03.1347
79
  {
80
    if (!join_tab->used_fieldlength)		/* Not calced yet */
81
      calc_used_field_length(session, join_tab);
82
    cache->fields+=join_tab->used_fields;
83
    blobs+=join_tab->used_blobs;
84
85
    /* SemiJoinDuplicateElimination: reserve space for rowid */
86
    if (join_tab->rowid_keep_flags & JoinTable::KEEP_ROWID)
87
    {
88
      cache->fields++;
89
      join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
90
    }
91
  }
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
92
  if (!(cache->field=(CacheField*)
93
        memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
1 by Monty Taylor
Import upstream version 2010.03.1347
94
  {
1.2.1 by Monty Taylor
Import upstream version 2010.11.03
95
    size= cache->end - cache->buff;
96
    global_join_buffer.sub(size);
1 by Monty Taylor
Import upstream version 2010.03.1347
97
    free((unsigned char*) cache->buff);
98
    cache->buff=0;
99
    return(1);
100
  }
101
  copy=cache->field;
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
102
  blob_ptr=cache->blob_ptr=(CacheField**)
1 by Monty Taylor
Import upstream version 2010.03.1347
103
    (cache->field+cache->fields+table_count*2);
104
105
  length=0;
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
106
  for (unsigned int i= 0 ; i < table_count ; i++)
1 by Monty Taylor
Import upstream version 2010.03.1347
107
  {
108
    uint32_t null_fields=0, used_fields;
109
    Field **f_ptr,*field;
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
110
    for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
1 by Monty Taylor
Import upstream version 2010.03.1347
111
    {
112
      field= *f_ptr;
113
      if (field->isReadSet())
114
      {
115
        used_fields--;
116
        length+=field->fill_cache_field(copy);
117
        if (copy->blob_field)
118
          (*blob_ptr++)=copy;
119
        if (field->maybe_null())
120
          null_fields++;
121
        copy->get_rowid= NULL;
122
        copy++;
123
      }
124
    }
125
    /* Copy null bits from table */
126
    if (null_fields && tables[i].table->getNullFields())
127
    {						/* must copy null bits */
128
      copy->str= tables[i].table->null_flags;
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
129
      copy->length= tables[i].table->getShare()->null_bytes;
1 by Monty Taylor
Import upstream version 2010.03.1347
130
      copy->strip=0;
131
      copy->blob_field=0;
132
      copy->get_rowid= NULL;
133
      length+=copy->length;
134
      copy++;
135
      cache->fields++;
136
    }
137
    /* If outer join table, copy null_row flag */
138
    if (tables[i].table->maybe_null)
139
    {
140
      copy->str= (unsigned char*) &tables[i].table->null_row;
141
      copy->length=sizeof(tables[i].table->null_row);
142
      copy->strip=0;
143
      copy->blob_field=0;
144
      copy->get_rowid= NULL;
145
      length+=copy->length;
146
      copy++;
147
      cache->fields++;
148
    }
149
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
150
    if (tables[i].rowid_keep_flags & JoinTable::KEEP_ROWID)
151
    {
152
      copy->str= tables[i].table->cursor->ref;
153
      copy->length= tables[i].table->cursor->ref_length;
154
      copy->strip=0;
155
      copy->blob_field=0;
156
      copy->get_rowid= NULL;
157
      if (tables[i].rowid_keep_flags & JoinTable::CALL_POSITION)
158
      {
159
        /* We will need to call h->position(): */
160
        copy->get_rowid= tables[i].table;
161
        /* And those after us won't have to: */
162
        tables[i].rowid_keep_flags&=  ~((int)JoinTable::CALL_POSITION);
163
      }
164
      copy++;
165
    }
166
  }
167
168
  cache->length= length+blobs*sizeof(char*);
169
  cache->blobs= blobs;
170
  *blob_ptr= NULL;					/* End sequentel */
171
  size= max((size_t) session->variables.join_buff_size, (size_t)cache->length);
1.2.1 by Monty Taylor
Import upstream version 2010.11.03
172
  if (not global_join_buffer.add(size))
173
  {
174
    my_error(ER_OUT_OF_GLOBAL_JOINMEMORY, MYF(ME_ERROR+ME_WAITTANG));
175
    return 1;
176
  }
1 by Monty Taylor
Import upstream version 2010.03.1347
177
  if (!(cache->buff= (unsigned char*) malloc(size)))
178
    return 1;
179
  cache->end= cache->buff+size;
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
180
  cache->reset_cache_write();
181
1 by Monty Taylor
Import upstream version 2010.03.1347
182
  return 0;
183
}
184
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
185
bool JoinCache::store_record_in_cache()
1 by Monty Taylor
Import upstream version 2010.03.1347
186
{
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
187
  JoinCache *cache= this;
188
  unsigned char *local_pos;
189
  CacheField *copy,*end_field;
1 by Monty Taylor
Import upstream version 2010.03.1347
190
  bool last_record;
191
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
192
  local_pos= cache->pos;
1 by Monty Taylor
Import upstream version 2010.03.1347
193
  end_field= cache->field+cache->fields;
194
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
195
  {
196
    uint32_t local_length;
197
198
    local_length= cache->length;
199
    if (cache->blobs)
200
    {
201
      local_length+= used_blob_length(cache->blob_ptr);
202
    }
203
204
    if ((last_record= (local_length + cache->length > (size_t) (cache->end - local_pos))))
205
    {
206
      cache->ptr_record= cache->records;
207
    }
208
  }
209
1 by Monty Taylor
Import upstream version 2010.03.1347
210
  /*
211
    There is room in cache. Put record there
212
  */
213
  cache->records++;
214
  for (copy= cache->field; copy < end_field; copy++)
215
  {
216
    if (copy->blob_field)
217
    {
218
      if (last_record)
219
      {
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
220
        copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
221
        local_pos+= copy->length+sizeof(char*);
1 by Monty Taylor
Import upstream version 2010.03.1347
222
      }
223
      else
224
      {
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
225
        copy->blob_field->get_image(local_pos, copy->length, // blob length
1 by Monty Taylor
Import upstream version 2010.03.1347
226
				    copy->blob_field->charset());
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
227
        memcpy(local_pos + copy->length,copy->str,copy->blob_length);  // Blob data
228
        local_pos+= copy->length+copy->blob_length;
1 by Monty Taylor
Import upstream version 2010.03.1347
229
      }
230
    }
231
    else
232
    {
233
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
234
      if (copy->get_rowid)
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
235
        copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
1 by Monty Taylor
Import upstream version 2010.03.1347
236
237
      if (copy->strip)
238
      {
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
239
        unsigned char *str, *local_end;
240
        for (str= copy->str,local_end= str+copy->length; local_end > str && local_end[-1] == ' '; local_end--) {}
241
242
        uint32_t local_length= (uint32_t) (local_end - str);
243
        memcpy(local_pos+2, str, local_length);
244
        int2store(local_pos, local_length);
245
        local_pos+= local_length+2;
1 by Monty Taylor
Import upstream version 2010.03.1347
246
      }
247
      else
248
      {
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
249
        memcpy(local_pos, copy->str, copy->length);
250
        local_pos+= copy->length;
1 by Monty Taylor
Import upstream version 2010.03.1347
251
      }
252
    }
253
  }
1.1.1 by Monty Taylor
Import upstream version 2010.09.1802
254
  cache->pos= local_pos;
255
  return last_record || (size_t) (cache->end - local_pos) < cache->length;
256
}
257
258
void JoinCache::reset_cache_read()
259
{
260
  record_nr= 0;
261
  pos= buff;
262
}
263
264
void JoinCache::reset_cache_write()
265
{
266
  reset_cache_read();
267
  records= 0;
268
  ptr_record= UINT32_MAX;
1 by Monty Taylor
Import upstream version 2010.03.1347
269
}
270
271
/**
272
  @} (end of group Query_Optimizer)
273
*/
274
275
} /* namespace drizzled */