~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
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
41
static uint32_t used_blob_length(CACHE_FIELD **ptr);
42
43
static uint32_t used_blob_length(CACHE_FIELD **ptr)
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
  register unsigned int i;
64
  unsigned int length, blobs;
65
  size_t size;
66
  CACHE_FIELD *copy,**blob_ptr;
67
  JOIN_CACHE  *cache;
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
68
  JoinTable *join_tab;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
69
70
  cache= &tables[table_count].cache;
71
  cache->fields=blobs=0;
72
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
73
  join_tab= tables;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
74
  for (i=0 ; i < table_count ; i++,join_tab++)
75
  {
76
    if (!join_tab->used_fieldlength)		/* Not calced yet */
77
      calc_used_field_length(session, join_tab);
78
    cache->fields+=join_tab->used_fields;
79
    blobs+=join_tab->used_blobs;
80
81
    /* SemiJoinDuplicateElimination: reserve space for rowid */
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
82
    if (join_tab->rowid_keep_flags & JoinTable::KEEP_ROWID)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
83
    {
84
      cache->fields++;
1208.3.2 by brian
Update for Cursor renaming.
85
      join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
86
    }
87
  }
88
  if (!(cache->field=(CACHE_FIELD*)
1253.1.6 by Monty Taylor
Moved mem_root functions into drizzled::memory:: namespace.
89
	memory::sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
90
91
		  sizeof(CACHE_FIELD*))))
92
  {
971.6.11 by Eric Day
Removed purecov messages.
93
    free((unsigned char*) cache->buff);
94
    cache->buff=0;
95
    return(1);
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
96
  }
97
  copy=cache->field;
98
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
99
    (cache->field+cache->fields+table_count*2);
100
101
  length=0;
102
  for (i=0 ; i < table_count ; i++)
103
  {
104
    uint32_t null_fields=0, used_fields;
105
    Field **f_ptr,*field;
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
106
    for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
107
    {
108
      field= *f_ptr;
109
      if (field->isReadSet())
110
      {
1039.2.4 by Jay Pipes
Tiny indentation cleanup.
111
        used_fields--;
112
        length+=field->fill_cache_field(copy);
113
        if (copy->blob_field)
114
          (*blob_ptr++)=copy;
115
        if (field->maybe_null())
116
          null_fields++;
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
117
        copy->get_rowid= NULL;
1039.2.4 by Jay Pipes
Tiny indentation cleanup.
118
        copy++;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
119
      }
120
    }
121
    /* Copy null bits from table */
122
    if (null_fields && tables[i].table->getNullFields())
123
    {						/* must copy null bits */
124
      copy->str= tables[i].table->null_flags;
125
      copy->length= tables[i].table->s->null_bytes;
126
      copy->strip=0;
127
      copy->blob_field=0;
128
      copy->get_rowid= NULL;
129
      length+=copy->length;
130
      copy++;
131
      cache->fields++;
132
    }
133
    /* If outer join table, copy null_row flag */
134
    if (tables[i].table->maybe_null)
135
    {
136
      copy->str= (unsigned char*) &tables[i].table->null_row;
137
      copy->length=sizeof(tables[i].table->null_row);
138
      copy->strip=0;
139
      copy->blob_field=0;
140
      copy->get_rowid= NULL;
141
      length+=copy->length;
142
      copy++;
143
      cache->fields++;
144
    }
145
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
146
    if (tables[i].rowid_keep_flags & JoinTable::KEEP_ROWID)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
147
    {
1208.3.2 by brian
Update for Cursor renaming.
148
      copy->str= tables[i].table->cursor->ref;
149
      copy->length= tables[i].table->cursor->ref_length;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
150
      copy->strip=0;
151
      copy->blob_field=0;
152
      copy->get_rowid= NULL;
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
153
      if (tables[i].rowid_keep_flags & JoinTable::CALL_POSITION)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
154
      {
155
        /* We will need to call h->position(): */
156
        copy->get_rowid= tables[i].table;
157
        /* And those after us won't have to: */
1089.1.1 by Brian Aker
Remove of JOIN_TAB to JoinTable
158
        tables[i].rowid_keep_flags&=  ~((int)JoinTable::CALL_POSITION);
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
159
      }
160
      copy++;
161
    }
162
  }
163
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
164
  cache->length= length+blobs*sizeof(char*);
165
  cache->blobs= blobs;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
166
  *blob_ptr= NULL;					/* End sequentel */
1067.4.7 by Nathan Williams
The remaining files using cmax have been converted to std::max.
167
  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.
168
  if (!(cache->buff= (unsigned char*) malloc(size)))
971.6.11 by Eric Day
Removed purecov messages.
169
    return 1;
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
170
  cache->end= cache->buff+size;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
171
  reset_cache_write(cache);
172
  return 0;
173
}
174
175
bool store_record_in_cache(JOIN_CACHE *cache)
176
{
177
  uint32_t length;
178
  unsigned char *pos;
179
  CACHE_FIELD *copy,*end_field;
180
  bool last_record;
181
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
182
  pos= cache->pos;
183
  end_field= cache->field+cache->fields;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
184
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
185
  length= cache->length;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
186
  if (cache->blobs)
187
    length+= used_blob_length(cache->blob_ptr);
188
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
189
    cache->ptr_record= cache->records;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
190
  /*
191
    There is room in cache. Put record there
192
  */
193
  cache->records++;
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
194
  for (copy= cache->field; copy < end_field; copy++)
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
195
  {
196
    if (copy->blob_field)
197
    {
198
      if (last_record)
199
      {
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
200
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
201
        pos+= copy->length+sizeof(char*);
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
202
      }
203
      else
204
      {
205
        copy->blob_field->get_image(pos, copy->length, // blob length
206
				    copy->blob_field->charset());
207
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
208
        pos+= copy->length+copy->blob_length;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
209
      }
210
    }
211
    else
212
    {
213
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
214
      if (copy->get_rowid)
1208.3.2 by brian
Update for Cursor renaming.
215
        copy->get_rowid->cursor->position(copy->get_rowid->record[0]);
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
216
217
      if (copy->strip)
218
      {
219
        unsigned char *str,*end;
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
220
        for (str= copy->str,end= str+copy->length; end > str && end[-1] == ' '; end--)
221
        {}
222
        length= (uint32_t) (end-str);
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
223
        memcpy(pos+2, str, length);
224
        int2store(pos, length);
225
        pos+= length+2;
226
      }
227
      else
228
      {
229
        memcpy(pos,copy->str,copy->length);
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
230
        pos+= copy->length;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
231
      }
232
    }
233
  }
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
234
  cache->pos= pos;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
235
  return last_record || (size_t) (cache->end - pos) < cache->length;
236
}
237
238
void reset_cache_read(JOIN_CACHE *cache)
239
{
1039.2.7 by Jay Pipes
Yet more style and indentation cleanups.
240
  cache->record_nr= 0;
241
  cache->pos= cache->buff;
1039.2.3 by Jay Pipes
Phase 3 of refactoring JOIN
242
}
243
244
void reset_cache_write(JOIN_CACHE *cache)
245
{
246
  reset_cache_read(cache);
247
  cache->records= 0;
248
  cache->ptr_record= UINT32_MAX;
249
}
250
251
/**
252
  @} (end of group Query_Optimizer)
253
*/
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
254
255
} /* namespace drizzled */