sql_olap.cc 5.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


18
/* OLAP implementation by Sinisa Milivojevic <sinisa@mysql.com> */
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181

#ifdef __GNUC__
#pragma implementation				// gcc: Class implementation
#endif

#include "mysql_priv.h"
#include "sql_select.h"


/****************************************************************************
  Functions that recursively actually creates new SELECT's
  Returns 0 if OK, 1 if error, -1 if error already printed to client
****************************************************************************/


static int make_new_olap_select(LEX *lex, SELECT_LEX *select_lex, List<Item> new_fields)
{
  THD	*thd=current_thd;
  Item *item, *new_item;
  Item_string *constant= new Item_string("ALL",3);

  SELECT_LEX *new_select = (SELECT_LEX *) thd->memdup((char*) select_lex, sizeof(*select_lex));
  if (!new_select)
    return 1;
  lex->last_selects->next=new_select;
  new_select->linkage=OLAP_TYPE;
  new_select->olap=NON_EXISTING_ONE;
  new_select->group_list.elements=0;
  new_select->group_list.first=(byte *)0;
  new_select->group_list.next=(byte **)&new_select->group_list.first;
  List<Item> privlist;
  
  List_iterator<Item> list_it(select_lex->item_list);
  List_iterator<Item> new_it(new_fields);
    
  while((item=list_it++))
  {
    bool not_found=true;
    if (item->type()==Item::FIELD_ITEM)
    {
      Item_field *iif = (Item_field *)item;
      new_it.rewind();
      while ((new_item=new_it++))
      {
	if (new_item->type()==Item::FIELD_ITEM && 
	    !strcmp(((Item_field*)new_item)->table_name,iif->table_name) &&
	    !strcmp(((Item_field*)new_item)->field_name,iif->field_name))
	{
	  not_found=false;
	  ((Item_field*)new_item)->db_name=iif->db_name;
	  Item_field *new_one=new Item_field(iif->db_name, iif->table_name, iif->field_name);
	  privlist.push_back(new_one);
	  if (add_to_list(new_select->group_list,new_one,1))
	    return 1;
	  break;
	}
      }
    }
    if (not_found)
    {
      if (item->type() == Item::FIELD_ITEM)
	privlist.push_back(constant);
      else
	privlist.push_back((Item*)thd->memdup((char *)item,item->size_of()));
    }
  }
  new_select->item_list=privlist;

  lex->last_selects = new_select;
  return 0;
}

/****************************************************************************
  Functions that recursively creates combinations of queries for OLAP
  Returns 0 if OK, 1 if error, -1 if error already printed to client
****************************************************************************/

static int  olap_combos(List<Item> old_fields, List<Item> new_fields, Item *item, LEX *lex, 
			      SELECT_LEX *select_lex, int position, int selection, int num_fields, 
			      int num_new_fields)
{
  int sl_return = 0;
  if(position == num_new_fields)
  {
    if(item)
      new_fields.push_front(item);
    sl_return = make_new_olap_select(lex, select_lex, new_fields);
  }
  else
  {
    if(item)
      new_fields.push_front(item);
    while ((num_fields - num_new_fields >= selection - position) && !sl_return)
    {
      item = old_fields.pop();
      sl_return = olap_combos(old_fields, new_fields, item, lex, select_lex, position+1, ++selection, num_fields, num_new_fields);
    }
  }
  return sl_return;
}


/****************************************************************************
  Top level function for converting OLAP clauses to multiple selects
  This is also a place where clauses treatment depends on OLAP type 
  Returns 0 if OK, 1 if error, -1 if error already printed to client
****************************************************************************/

int handle_olaps(LEX *lex, SELECT_LEX *select_lex)
{
  List<Item> item_list_copy, new_item_list;
  item_list_copy.empty();
  new_item_list.empty();
  int count=select_lex->group_list.elements;
  int sl_return=0;

// a fix for UNION's
  for (TABLE_LIST *cursor= (TABLE_LIST *)select_lex->table_list.first;
       cursor;
       cursor=cursor->next)
  {
    if (cursor->do_redirect)
    {
      cursor->table= ((TABLE_LIST*) cursor->table)->table;
      cursor->do_redirect=false;
    }
  }

  lex->last_selects=select_lex;

  for (ORDER *order=(ORDER *)select_lex->group_list.first ; order ; order=order->next)
    item_list_copy.push_back(*(order->item));

  List<Item>	all_fields(select_lex->item_list);


  if (setup_tables((TABLE_LIST *)select_lex->table_list.first) ||
      setup_fields(lex->thd,(TABLE_LIST *)select_lex->table_list.first,select_lex->item_list,1,&all_fields,1) ||
      setup_fields(lex->thd,(TABLE_LIST *)select_lex->table_list.first,item_list_copy,1,&all_fields,1))
    return -1;

  if (select_lex->olap == CUBE_TYPE)
  {
    for( int i=count-1; i>=0 && !sl_return; i--)
      sl_return=olap_combos(item_list_copy, new_item_list, (Item *)0, lex, select_lex, 0, 0, count, i);
  }
  else if (select_lex->olap == ROLLUP_TYPE)
  {
    for( int i=count-1; i>=0 && !sl_return; i--)
    {
      Item *item;
      item_list_copy.pop();
      List_iterator<Item> it(item_list_copy);
      new_item_list.empty();
      while ((item = it++))
	new_item_list.push_front(item);
      sl_return=make_new_olap_select(lex, select_lex, new_item_list);
    }
  }
  else
    sl_return=1; // impossible
  return sl_return;
}