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