~drizzle-trunk/drizzle/development

1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008-2009 Sun Microsystems
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
30
#include "config.h"
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
31
#include "drizzled/sql_select.h" /* include join.h */
32
#include "drizzled/field/blob.h"
33
1067.4.7 by Nathan Williams
The remaining files using cmax have been converted to std::max.
34
#include <algorithm>
35
36
using namespace std;
37
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
38
namespace drizzled
39
{
40
1539.1.6 by Brian Aker
Update for Join structure changes.
41
static uint32_t used_blob_length(CacheField **ptr);
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
42
1539.1.6 by Brian Aker
Update for Join structure changes.
43
static uint32_t used_blob_length(CacheField **ptr)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
44
{
45
  uint32_t length,blob_length;
46
  for (length=0 ; *ptr ; ptr++)
47
  {
48
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
49
    length+=blob_length;
50
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
51
  }
52
  return length;
53
}
54
55
/*****************************************************************************
56
  Fill join cache with packed records
57
  Records are stored in tab->cache.buffer and last record in
58
  last record is stored with pointers to blobs to support very big
59
  records
60
******************************************************************************/
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
61
int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
62
{
63
  unsigned int length, blobs;
64
  size_t size;
1539.1.6 by Brian Aker
Update for Join structure changes.
65
  CacheField *copy,**blob_ptr;
66
  JoinCache  *cache;
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
67
  JoinTable *join_tab;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
68
69
  cache= &tables[table_count].cache;
70
  cache->fields=blobs=0;
71
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
72
  join_tab= tables;
1539.1.6 by Brian Aker
Update for Join structure changes.
73
  for (unsigned int i= 0; i < table_count ; i++, join_tab++)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
74
  {
75
    if (!join_tab->used_fieldlength)		/* Not calced yet */
76
      calc_used_field_length(session, join_tab);
77
    cache->fields+=join_tab->used_fields;
78
    blobs+=join_tab->used_blobs;
79
80
    /* SemiJoinDuplicateElimination: reserve space for rowid */
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
81
    if (join_tab->rowid_keep_flags & JoinTable::KEEP_ROWID)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
82
    {
83
      cache->fields++;
1208.3.2 by brian
Update for Cursor renaming.
84
      join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
85
    }
86
  }
1539.1.6 by Brian Aker
Update for Join structure changes.
87
  if (!(cache->field=(CacheField*)
88
        memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
89
  {
971.6.11 by Eric Day
Removed purecov messages.
90
    free((unsigned char*) cache->buff);
91
    cache->buff=0;
92
    return(1);
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
93
  }
94
  copy=cache->field;
1539.1.6 by Brian Aker
Update for Join structure changes.
95
  blob_ptr=cache->blob_ptr=(CacheField**)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
96
    (cache->field+cache->fields+table_count*2);
97
98
  length=0;
1539.1.6 by Brian Aker
Update for Join structure changes.
99
  for (unsigned int i= 0 ; i < table_count ; i++)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
100
  {
101
    uint32_t null_fields=0, used_fields;
102
    Field **f_ptr,*field;
1578.2.16 by Brian Aker
Merge in change to getTable() to private the field objects.
103
    for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
104
    {
105
      field= *f_ptr;
106
      if (field->isReadSet())
107
      {
1039.2.4 by Jay Pipes
Tiny indentation cleanup.
108
        used_fields--;
109
        length+=field->fill_cache_field(copy);
110
        if (copy->blob_field)
111
          (*blob_ptr++)=copy;
112
        if (field->maybe_null())
113
          null_fields++;
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
114
        copy->get_rowid= NULL;
1039.2.4 by Jay Pipes
Tiny indentation cleanup.
115
        copy++;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
116
      }
117
    }
118
    /* Copy null bits from table */
119
    if (null_fields && tables[i].table->getNullFields())
120
    {						/* must copy null bits */
121
      copy->str= tables[i].table->null_flags;
1532.1.15 by Brian Aker
Partial encapsulation of TableShare from Table.
122
      copy->length= tables[i].table->getShare()->null_bytes;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
123
      copy->strip=0;
124
      copy->blob_field=0;
125
      copy->get_rowid= NULL;
126
      length+=copy->length;
127
      copy++;
128
      cache->fields++;
129
    }
130
    /* If outer join table, copy null_row flag */
131
    if (tables[i].table->maybe_null)
132
    {
133
      copy->str= (unsigned char*) &tables[i].table->null_row;
134
      copy->length=sizeof(tables[i].table->null_row);
135
      copy->strip=0;
136
      copy->blob_field=0;
137
      copy->get_rowid= NULL;
138
      length+=copy->length;
139
      copy++;
140
      cache->fields++;
141
    }
142
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
143
    if (tables[i].rowid_keep_flags & JoinTable::KEEP_ROWID)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
144
    {
1208.3.2 by brian
Update for Cursor renaming.
145
      copy->str= tables[i].table->cursor->ref;
146
      copy->length= tables[i].table->cursor->ref_length;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
147
      copy->strip=0;
148
      copy->blob_field=0;
149
      copy->get_rowid= NULL;
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
150
      if (tables[i].rowid_keep_flags & JoinTable::CALL_POSITION)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
151
      {
152
        /* We will need to call h->position(): */
153
        copy->get_rowid= tables[i].table;
154
        /* And those after us won't have to: */
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
155
        tables[i].rowid_keep_flags&=  ~((int)JoinTable::CALL_POSITION);
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
156
      }
157
      copy++;
158
    }
159
  }
160
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
161
  cache->length= length+blobs*sizeof(char*);
162
  cache->blobs= blobs;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
163
  *blob_ptr= NULL;					/* End sequentel */
1067.4.7 by Nathan Williams
The remaining files using cmax have been converted to std::max.
164
  size= max((size_t) session->variables.join_buff_size, (size_t)cache->length);
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
165
  if (!(cache->buff= (unsigned char*) malloc(size)))
971.6.11 by Eric Day
Removed purecov messages.
166
    return 1;
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
167
  cache->end= cache->buff+size;
1539.1.7 by Brian Aker
JoinCache rename.
168
  cache->reset_cache_write();
169
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
170
  return 0;
171
}
172
1539.1.7 by Brian Aker
JoinCache rename.
173
bool JoinCache::store_record_in_cache()
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
174
{
1539.1.7 by Brian Aker
JoinCache rename.
175
  JoinCache *cache= this;
176
  unsigned char *local_pos;
1539.1.6 by Brian Aker
Update for Join structure changes.
177
  CacheField *copy,*end_field;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
178
  bool last_record;
179
1539.1.7 by Brian Aker
JoinCache rename.
180
  local_pos= cache->pos;
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
181
  end_field= cache->field+cache->fields;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
182
1539.1.7 by Brian Aker
JoinCache rename.
183
  {
184
    uint32_t local_length;
185
186
    local_length= cache->length;
187
    if (cache->blobs)
188
    {
189
      local_length+= used_blob_length(cache->blob_ptr);
190
    }
191
192
    if ((last_record= (local_length + cache->length > (size_t) (cache->end - local_pos))))
193
    {
194
      cache->ptr_record= cache->records;
195
    }
196
  }
197
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
198
  /*
199
    There is room in cache. Put record there
200
  */
201
  cache->records++;
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
202
  for (copy= cache->field; copy < end_field; copy++)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
203
  {
204
    if (copy->blob_field)
205
    {
206
      if (last_record)
207
      {
1539.1.7 by Brian Aker
JoinCache rename.
208
        copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
209
        local_pos+= copy->length+sizeof(char*);
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
210
      }
211
      else
212
      {
1539.1.7 by Brian Aker
JoinCache rename.
213
        copy->blob_field->get_image(local_pos, copy->length, // blob length
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
214
				    copy->blob_field->charset());
1539.1.7 by Brian Aker
JoinCache rename.
215
        memcpy(local_pos + copy->length,copy->str,copy->blob_length);  // Blob data
216
        local_pos+= copy->length+copy->blob_length;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
217
      }
218
    }
219
    else
220
    {
221
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
222
      if (copy->get_rowid)
1672.3.6 by Brian Aker
First pass in encapsulating row
223
        copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
224
225
      if (copy->strip)
226
      {
1539.1.7 by Brian Aker
JoinCache rename.
227
        unsigned char *str, *local_end;
228
        for (str= copy->str,local_end= str+copy->length; local_end > str && local_end[-1] == ' '; local_end--) {}
229
230
        uint32_t local_length= (uint32_t) (local_end - str);
231
        memcpy(local_pos+2, str, local_length);
232
        int2store(local_pos, local_length);
233
        local_pos+= local_length+2;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
234
      }
235
      else
236
      {
1539.1.7 by Brian Aker
JoinCache rename.
237
        memcpy(local_pos, copy->str, copy->length);
238
        local_pos+= copy->length;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
239
      }
240
    }
241
  }
1539.1.7 by Brian Aker
JoinCache rename.
242
  cache->pos= local_pos;
243
  return last_record || (size_t) (cache->end - local_pos) < cache->length;
244
}
245
246
void JoinCache::reset_cache_read()
247
{
248
  record_nr= 0;
249
  pos= buff;
250
}
251
252
void JoinCache::reset_cache_write()
253
{
254
  reset_cache_read();
255
  records= 0;
256
  ptr_record= UINT32_MAX;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
257
}
258
259
/**
260
  @} (end of group Query_Optimizer)
261
*/
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
262
263
} /* namespace drizzled */