1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
OLAP implementation by Sinisa Milivojevic <sinisa@mysql.com>
19
Inspired by code submitted by Srilakshmi <lakshmi@gdit.iiit.net>
21
The ROLLUP code in this file has to be complitely rewritten as it's
22
not good enough to satisfy the goals of MySQL.
24
In 4.1 we will replace this with a working, superior implementation
28
#ifdef DISABLED_UNTIL_REWRITTEN_IN_4_1
29
#include <drizzled/server_includes.h>
30
#include <drizzled/sql_select.h>
33
/****************************************************************************
34
Functions that recursively actually creates new SELECT's
35
Returns 0 if OK, 1 if error, -1 if error already printed to client
36
****************************************************************************/
39
static int make_new_olap_select(LEX *lex, Select_Lex *select_lex, List<Item> new_fields)
41
Session *session=current_session;
42
Item *item, *new_item;
43
Item_null *constant= new Item_null("ALL");
45
Select_Lex *new_select = (Select_Lex *) session->memdup((char*) select_lex, sizeof(*select_lex));
48
lex->last_selects->next=new_select;
49
new_select->linkage=OLAP_TYPE;
50
new_select->olap=NON_EXISTING_ONE;
51
new_select->group_list.elements=0;
52
new_select->group_list.first=(unsigned char *)0;
53
new_select->group_list.next=(unsigned char **)&new_select->group_list.first;
56
List_iterator<Item> list_it(select_lex->item_list);
57
List_iterator<Item> new_it(new_fields);
59
while ((item=list_it++))
62
if (item->type()==Item::FIELD_ITEM)
64
Item_field *iif = (Item_field *)item;
66
while ((new_item=new_it++))
68
if (new_item->type()==Item::FIELD_ITEM &&
69
!strcmp(((Item_field*)new_item)->table_name,iif->table_name) &&
70
!strcmp(((Item_field*)new_item)->field_name,iif->field_name))
73
((Item_field*)new_item)->db_name=iif->db_name;
74
Item_field *new_one=new Item_field(&select_lex->context,
75
iif->db_name, iif->table_name, iif->field_name);
76
privlist.push_back(new_one);
77
if (add_to_list(new_select->group_list,new_one,1))
85
if (item->type() == Item::FIELD_ITEM)
86
privlist.push_back(constant);
88
privlist.push_back((Item*)session->memdup((char *)item,item->size_of()));
91
new_select->item_list=privlist;
93
lex->last_selects = new_select;
97
/****************************************************************************
98
Functions that recursively creates combinations of queries for OLAP
99
Returns 0 if OK, 1 if error, -1 if error already printed to client
100
****************************************************************************/
102
static int olap_combos(List<Item> old_fields, List<Item> new_fields, Item *item, LEX *lex,
103
Select_Lex *select_lex, int position, int selection, int num_fields,
107
if (position == num_new_fields)
110
new_fields.push_front(item);
111
sl_return = make_new_olap_select(lex, select_lex, new_fields);
116
new_fields.push_front(item);
117
while ((num_fields - num_new_fields >= selection - position) && !sl_return)
119
item = old_fields.pop();
120
sl_return = olap_combos(old_fields, new_fields, item, lex, select_lex, position+1, ++selection, num_fields, num_new_fields);
127
/****************************************************************************
128
Top level function for converting OLAP clauses to multiple selects
129
This is also a place where clauses treatment depends on OLAP type
130
Returns 0 if OK, 1 if error, -1 if error already printed to client
131
****************************************************************************/
133
int handle_olaps(LEX *lex, Select_Lex *select_lex)
135
List<Item> item_list_copy, new_item_list;
136
item_list_copy.empty();
137
new_item_list.empty();
138
int count=select_lex->group_list.elements;
142
lex->last_selects=select_lex;
144
for (ORDER *order=(ORDER *)select_lex->group_list.first ; order ; order=order->next)
145
item_list_copy.push_back(*(order->item));
147
List<Item> all_fields(select_lex->item_list);
150
if (setup_tables(lex->session, &select_lex->context, &select_lex->top_join_list,
151
(TableList *)select_lex->table_list.first
152
&select_lex->leaf_tables, false) ||
153
setup_fields(lex->session, 0, select_lex->item_list, MARK_COLUMNS_READ,
155
setup_fields(lex->session, 0, item_list_copy, MARK_COLUMNS_READ,
159
if (select_lex->olap == CUBE_TYPE)
161
for ( int i=count-1; i>=0 && !sl_return; i--)
162
sl_return=olap_combos(item_list_copy, new_item_list, (Item *)0, lex, select_lex, 0, 0, count, i);
164
else if (select_lex->olap == ROLLUP_TYPE)
166
for ( int i=count-1; i>=0 && !sl_return; i--)
169
item_list_copy.pop();
170
List_iterator<Item> it(item_list_copy);
171
new_item_list.empty();
172
while ((item = it++))
173
new_item_list.push_front(item);
174
sl_return=make_new_olap_select(lex, select_lex, new_item_list);
178
sl_return=1; // impossible
182
#endif /* DISABLED_UNTIL_REWRITTEN_IN_4_1 */