sql_analyse.h 10.6 KB
Newer Older
1 2 3
#ifndef SQL_ANALYSE_INCLUDED
#define SQL_ANALYSE_INCLUDED

unknown's avatar
unknown committed
4
/* Copyright (C) 2000-2003, 2005 MySQL AB
unknown's avatar
unknown committed
5

unknown's avatar
unknown committed
6 7
   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
unknown's avatar
unknown committed
8
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
9

unknown's avatar
unknown committed
10 11 12 13
   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.
unknown's avatar
unknown committed
14

unknown's avatar
unknown committed
15 16 17 18 19 20 21
   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 */


/* Analyse database */

22
#ifdef USE_PRAGMA_INTERFACE
unknown's avatar
unknown committed
23 24 25
#pragma interface				/* gcc class implementation */
#endif

unknown's avatar
unknown committed
26 27
#define my_thd_charset	default_charset_info

unknown's avatar
unknown committed
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
#define DEC_IN_AVG 4

typedef struct st_number_info
{
  // if zerofill is true, the number must be zerofill, or string
  bool	    negative, is_float, zerofill, maybe_zerofill;
  int8	    integers;
  int8	    decimals;
  double    dval;
  ulonglong ullval;
} NUM_INFO;

typedef struct st_extreme_value_number_info
{
  ulonglong ullval;
  longlong  llval;
  double    max_dval, min_dval;
} EV_NUM_INFO;

typedef struct st_tree_info
{
  bool	 found;
  String *str;
  Item	 *item;
} TREE_INFO;

uint check_ulonglong(const char *str, uint length);
bool get_ev_num_info(EV_NUM_INFO *ev_info, NUM_INFO *info, const char *num);
unknown's avatar
unknown committed
56
bool test_if_number(NUM_INFO *info, const char *str, uint str_len);
unknown's avatar
unknown committed
57
int compare_double(const double *s, const double *t);
58 59
int compare_double2(void* cmp_arg __attribute__((unused)),
		    const double *s, const double *t);
unknown's avatar
unknown committed
60
int compare_longlong(const longlong *s, const longlong *t);
61 62
int compare_longlong2(void* cmp_arg __attribute__((unused)),
		      const longlong *s, const longlong *t);
unknown's avatar
unknown committed
63
int compare_ulonglong(const ulonglong *s, const ulonglong *t);
64 65
int compare_ulonglong2(void* cmp_arg __attribute__((unused)),
		       const ulonglong *s, const ulonglong *t);
unknown's avatar
unknown committed
66
int compare_decimal2(int* len, const char *s, const char *t);
unknown's avatar
unknown committed
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
Procedure *proc_analyse_init(THD *thd, ORDER *param, select_result *result,
			     List<Item> &field_list);
void free_string(String*);
class analyse;

class field_info :public Sql_alloc
{
protected:
  ulong   treemem, tree_elements, empty, nulls, min_length, max_length;
  uint	  room_in_tree;
  my_bool found;
  TREE	  tree;
  Item	  *item;
  analyse *pc;

public:
  field_info(Item* a, analyse* b) : treemem(0), tree_elements(0), empty(0),
    nulls(0), min_length(0), max_length(0), room_in_tree(1),
    found(0),item(a), pc(b) {};

  virtual ~field_info() { delete_tree(&tree); }
  virtual void	 add() = 0;
  virtual void	 get_opt_type(String*, ha_rows) = 0;
  virtual String *get_min_arg(String *) = 0;
  virtual String *get_max_arg(String *) = 0;
  virtual String *avg(String*, ha_rows) = 0;
  virtual String *std(String*, ha_rows) = 0;
  virtual tree_walk_action collect_enum() = 0;
  virtual uint decimals() { return 0; }
  friend  class analyse;
};


100 101 102
int collect_string(String *element, element_count count,
		   TREE_INFO *info);

103 104 105
int sortcmp2(void* cmp_arg __attribute__((unused)),
	     const String *a,const String *b);

unknown's avatar
unknown committed
106 107 108 109 110 111 112 113 114 115
class field_str :public field_info
{
  String      min_arg, max_arg;
  ulonglong   sum;
  bool	      must_be_blob, was_zero_fill, was_maybe_zerofill,
	      can_be_still_num;
  NUM_INFO    num_info;
  EV_NUM_INFO ev_num_info;

public:
116 117 118
  field_str(Item* a, analyse* b) :field_info(a,b), 
    min_arg("",default_charset_info),
    max_arg("",default_charset_info), sum(0),
unknown's avatar
unknown committed
119 120
    must_be_blob(0), was_zero_fill(0),
    was_maybe_zerofill(0), can_be_still_num(1)
121
    { init_tree(&tree, 0, 0, sizeof(String), (qsort_cmp2) sortcmp2,
unknown's avatar
unknown committed
122
		0, (tree_element_free) free_string, NULL); };
unknown's avatar
unknown committed
123 124 125 126 127 128 129 130 131 132

  void	 add();
  void	 get_opt_type(String*, ha_rows);
  String *get_min_arg(String *not_used __attribute__((unused)))
  { return &min_arg; }
  String *get_max_arg(String *not_used __attribute__((unused)))
  { return &max_arg; }
  String *avg(String *s, ha_rows rows)
  {
    if (!(rows - nulls))
133
      s->set_real((double) 0.0, 1,my_thd_charset);
unknown's avatar
unknown committed
134
    else
135
      s->set_real((ulonglong2double(sum) / ulonglong2double(rows - nulls)),
136
	     DEC_IN_AVG,my_thd_charset);
unknown's avatar
unknown committed
137 138 139 140 141 142 143 144 145 146 147
    return s;
  }
  friend int collect_string(String *element, element_count count,
			    TREE_INFO *info);
  tree_walk_action collect_enum()
  { return (tree_walk_action) collect_string; }
  String *std(String *s __attribute__((unused)),
	      ha_rows rows __attribute__((unused)))
  { return (String*) 0; }
};

148

149
int collect_decimal(uchar *element, element_count count,
unknown's avatar
unknown committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
                    TREE_INFO *info);

class field_decimal :public field_info
{
  my_decimal min_arg, max_arg;
  my_decimal sum[2], sum_sqr[2];
  int cur_sum;
  int bin_size;
public:
  field_decimal(Item* a, analyse* b) :field_info(a,b)
  {
    bin_size= my_decimal_get_binary_size(a->max_length, a->decimals);
    init_tree(&tree, 0, 0, bin_size, (qsort_cmp2)compare_decimal2,
              0, 0, (void *)&bin_size);
  };

  void	 add();
  void	 get_opt_type(String*, ha_rows);
  String *get_min_arg(String *);
  String *get_max_arg(String *);
  String *avg(String *s, ha_rows rows);
171
  friend int collect_decimal(uchar *element, element_count count,
unknown's avatar
unknown committed
172 173 174 175 176 177 178
                             TREE_INFO *info);
  tree_walk_action collect_enum()
  { return (tree_walk_action) collect_decimal; }
  String *std(String *s, ha_rows rows);
};


179 180
int collect_real(double *element, element_count count, TREE_INFO *info);

unknown's avatar
unknown committed
181 182 183 184 185 186 187 188 189
class field_real: public field_info
{
  double min_arg, max_arg;
  double sum, sum_sqr;
  uint	 max_notzero_dec_len;

public:
  field_real(Item* a, analyse* b) :field_info(a,b),
    min_arg(0), max_arg(0),  sum(0), sum_sqr(0), max_notzero_dec_len(0)
unknown's avatar
unknown committed
190 191
    { init_tree(&tree, 0, 0, sizeof(double),
		(qsort_cmp2) compare_double2, 0, NULL, NULL); }
unknown's avatar
unknown committed
192 193 194

  void	 add();
  void	 get_opt_type(String*, ha_rows);
195 196 197 198
  String *get_min_arg(String *s)
  {
    s->set_real(min_arg, item->decimals, my_thd_charset);
    return s;
199
  }
200 201 202 203
  String *get_max_arg(String *s)
  {
    s->set_real(max_arg, item->decimals, my_thd_charset);
    return s;
204
  }
unknown's avatar
unknown committed
205 206 207
  String *avg(String *s, ha_rows rows)
  {
    if (!(rows - nulls))
208
      s->set_real((double) 0.0, 1,my_thd_charset);
unknown's avatar
unknown committed
209
    else
210
      s->set_real(((double)sum / (double) (rows - nulls)), item->decimals,my_thd_charset);
unknown's avatar
unknown committed
211 212 213 214 215 216
    return s;
  }
  String *std(String *s, ha_rows rows)
  {
    double tmp = ulonglong2double(rows);
    if (!(tmp - nulls))
217
      s->set_real((double) 0.0, 1,my_thd_charset);
unknown's avatar
unknown committed
218 219 220 221
    else
    {
      double tmp2 = ((sum_sqr - sum * sum / (tmp - nulls)) /
		     (tmp - nulls));
222
      s->set_real(((double) tmp2 <= 0.0 ? 0.0 : sqrt(tmp2)), item->decimals,my_thd_charset);
unknown's avatar
unknown committed
223 224 225 226 227 228 229 230 231 232
    }
    return s;
  }
  uint	 decimals() { return item->decimals; }
  friend int collect_real(double *element, element_count count,
			  TREE_INFO *info);
  tree_walk_action collect_enum()
  { return (tree_walk_action) collect_real;}
};

233 234 235
int collect_longlong(longlong *element, element_count count,
		     TREE_INFO *info);

unknown's avatar
unknown committed
236 237 238 239 240 241 242 243
class field_longlong: public field_info
{
  longlong min_arg, max_arg;
  longlong sum, sum_sqr;

public:
  field_longlong(Item* a, analyse* b) :field_info(a,b), 
    min_arg(0), max_arg(0), sum(0), sum_sqr(0)
unknown's avatar
unknown committed
244 245
    { init_tree(&tree, 0, 0, sizeof(longlong),
		(qsort_cmp2) compare_longlong2, 0, NULL, NULL); }
unknown's avatar
unknown committed
246 247 248

  void	 add();
  void	 get_opt_type(String*, ha_rows);
249 250
  String *get_min_arg(String *s) { s->set(min_arg,my_thd_charset); return s; }
  String *get_max_arg(String *s) { s->set(max_arg,my_thd_charset); return s; }
unknown's avatar
unknown committed
251 252 253
  String *avg(String *s, ha_rows rows)
  {
    if (!(rows - nulls))
254
      s->set_real((double) 0.0, 1,my_thd_charset);
unknown's avatar
unknown committed
255
    else
256
      s->set_real(((double) sum / (double) (rows - nulls)), DEC_IN_AVG,my_thd_charset);
unknown's avatar
unknown committed
257 258 259 260 261 262
    return s;
  }
  String *std(String *s, ha_rows rows)
  {
    double tmp = ulonglong2double(rows);
    if (!(tmp - nulls))
263
      s->set_real((double) 0.0, 1,my_thd_charset);
unknown's avatar
unknown committed
264 265 266 267
    else
    {
      double tmp2 = ((sum_sqr - sum * sum / (tmp - nulls)) /
		    (tmp - nulls));
268
      s->set_real(((double) tmp2 <= 0.0 ? 0.0 : sqrt(tmp2)), DEC_IN_AVG,my_thd_charset);
unknown's avatar
unknown committed
269 270 271 272 273 274 275 276 277
    }
    return s;
  }
  friend int collect_longlong(longlong *element, element_count count,
			      TREE_INFO *info);
  tree_walk_action collect_enum()
  { return (tree_walk_action) collect_longlong;}
};

278 279
int collect_ulonglong(ulonglong *element, element_count count,
		      TREE_INFO *info);
unknown's avatar
unknown committed
280 281 282 283 284 285 286 287 288

class field_ulonglong: public field_info
{
  ulonglong min_arg, max_arg;
  ulonglong sum, sum_sqr;

public:
  field_ulonglong(Item* a, analyse * b) :field_info(a,b),
    min_arg(0), max_arg(0), sum(0),sum_sqr(0)
unknown's avatar
unknown committed
289 290
    { init_tree(&tree, 0, 0, sizeof(ulonglong),
		(qsort_cmp2) compare_ulonglong2, 0, NULL, NULL); }
unknown's avatar
unknown committed
291 292
  void	 add();
  void	 get_opt_type(String*, ha_rows);
293 294
  String *get_min_arg(String *s) { s->set(min_arg,my_thd_charset); return s; }
  String *get_max_arg(String *s) { s->set(max_arg,my_thd_charset); return s; }
unknown's avatar
unknown committed
295 296 297
  String *avg(String *s, ha_rows rows)
  {
    if (!(rows - nulls))
298
      s->set_real((double) 0.0, 1,my_thd_charset);
unknown's avatar
unknown committed
299
    else
300
      s->set_real((ulonglong2double(sum) / ulonglong2double(rows - nulls)),
301
	     DEC_IN_AVG,my_thd_charset);
unknown's avatar
unknown committed
302 303 304 305 306 307
    return s;
  }
  String *std(String *s, ha_rows rows)
  {
    double tmp = ulonglong2double(rows);
    if (!(tmp - nulls))
308
      s->set_real((double) 0.0, 1,my_thd_charset);
unknown's avatar
unknown committed
309 310 311 312 313
    else
    {
      double tmp2 = ((ulonglong2double(sum_sqr) - 
		     ulonglong2double(sum * sum) / (tmp - nulls)) /
		     (tmp - nulls));
314
      s->set_real(((double) tmp2 <= 0.0 ? 0.0 : sqrt(tmp2)), DEC_IN_AVG,my_thd_charset);
unknown's avatar
unknown committed
315 316 317 318 319 320 321 322 323 324
    }
    return s;
  }
  friend int collect_ulonglong(ulonglong *element, element_count count,
			       TREE_INFO *info);
  tree_walk_action collect_enum()
  { return (tree_walk_action) collect_ulonglong; }
};


325 326 327 328
Procedure *proc_analyse_init(THD *thd, ORDER *param,
			     select_result *result,
			     List<Item> &field_list);

unknown's avatar
unknown committed
329 330 331 332 333 334 335 336 337 338 339 340
class analyse: public Procedure
{
protected:
  Item_proc    *func_items[10];
  List<Item>   fields, result_fields;
  field_info   **f_info, **f_end;
  ha_rows      rows;
  uint	       output_str_length;

public:
  uint max_tree_elements, max_treemem;

341 342
  analyse(select_result *res) :Procedure(res, PROC_NO_SORT), f_info(0),
    rows(0), output_str_length(0) {}
unknown's avatar
unknown committed
343 344

  ~analyse()
345 346
  {
    if (f_info)
unknown's avatar
unknown committed
347
    {
348 349
      for (field_info **f=f_info; f != f_end; f++)
	delete (*f);
unknown's avatar
unknown committed
350
    }
unknown's avatar
unknown committed
351 352 353
  }
  virtual void add() {}
  virtual bool change_columns(List<Item> &fields);
unknown's avatar
unknown committed
354
  virtual int  send_row(List<Item> &field_list);
unknown's avatar
unknown committed
355
  virtual void end_group(void) {}
356
  virtual int end_of_records(void);
unknown's avatar
unknown committed
357 358 359 360
  friend Procedure *proc_analyse_init(THD *thd, ORDER *param,
				      select_result *result,
				      List<Item> &field_list);
};
361 362

#endif /* SQL_ANALYSE_INCLUDED */