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