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