1 #ifndef SQL_ANALYSE_INCLUDED
2 #define SQL_ANALYSE_INCLUDED
22 #include "procedure.h"
24 #define my_thd_charset default_charset_info
31 bool negative, is_float, zerofill, maybe_zerofill;
42 double max_dval, min_dval;
52 uint check_ulonglong(
const char *str, uint length);
54 bool test_if_number(
NUM_INFO *info,
const char *str, uint str_len);
55 int compare_double(
const double *s,
const double *t);
56 int compare_double2(
void* cmp_arg __attribute__((unused)),
57 const double *s,
const double *t);
58 int compare_longlong(
const longlong *s,
const longlong *t);
59 int compare_longlong2(
void* cmp_arg __attribute__((unused)),
60 const longlong *s,
const longlong *t);
61 int compare_ulonglong(
const ulonglong *s,
const ulonglong *t);
62 int compare_ulonglong2(
void* cmp_arg __attribute__((unused)),
63 const ulonglong *s,
const ulonglong *t);
64 int compare_decimal2(
int* len,
const char *s,
const char *t);
71 ulong treemem, tree_elements, empty, nulls, min_length, max_length;
80 : treemem(0), tree_elements(0), empty(0),
81 nulls(0), min_length(0), max_length(0), room_in_tree(1),
82 found(0),item(a), pc(b) {};
85 virtual void add() = 0;
86 virtual void get_opt_type(
String*, ha_rows) = 0;
91 virtual tree_walk_action collect_enum() = 0;
92 virtual uint decimals() {
return 0; }
97 int collect_string(
String *element, element_count count,
100 int sortcmp2(
void* cmp_arg __attribute__((unused)),
107 bool must_be_blob, was_zero_fill, was_maybe_zerofill,
114 min_arg(
"",default_charset_info),
115 max_arg(
"",default_charset_info), sum(0),
116 must_be_blob(0), was_zero_fill(0),
117 was_maybe_zerofill(0), can_be_still_num(1)
118 { init_tree(&tree, 0, 0,
sizeof(
String), (qsort_cmp2) sortcmp2,
119 0, (tree_element_free) free_string, NULL); };
122 void get_opt_type(
String*, ha_rows);
123 String *get_min_arg(
String *not_used __attribute__((unused)))
125 String *get_max_arg(
String *not_used __attribute__((unused)))
130 s->set_real((
double) 0.0, 1,my_thd_charset);
132 s->set_real((ulonglong2double(sum) / ulonglong2double(rows - nulls)),
133 DEC_IN_AVG,my_thd_charset);
136 friend int collect_string(
String *element, element_count count,
138 tree_walk_action collect_enum()
139 {
return (tree_walk_action) collect_string; }
141 ha_rows rows __attribute__((unused)))
146 int collect_decimal(uchar *element, element_count count,
158 bin_size= my_decimal_get_binary_size(a->max_length, a->decimals);
159 init_tree(&tree, 0, 0, bin_size, (qsort_cmp2)compare_decimal2,
160 0, 0, (
void *)&bin_size);
164 void get_opt_type(
String*, ha_rows);
168 friend int collect_decimal(uchar *element, element_count count,
170 tree_walk_action collect_enum()
171 {
return (tree_walk_action) collect_decimal; }
176 int collect_real(
double *element, element_count count,
TREE_INFO *info);
180 double min_arg, max_arg;
182 uint max_notzero_dec_len;
186 min_arg(0), max_arg(0), sum(0), sum_sqr(0), max_notzero_dec_len(0)
187 { init_tree(&tree, 0, 0,
sizeof(
double),
188 (qsort_cmp2) compare_double2, 0, NULL, NULL); }
191 void get_opt_type(
String*, ha_rows);
194 s->set_real(min_arg, item->decimals, my_thd_charset);
199 s->set_real(max_arg, item->decimals, my_thd_charset);
205 s->set_real((
double) 0.0, 1,my_thd_charset);
207 s->set_real(((
double)sum / (
double) (rows - nulls)), item->decimals,my_thd_charset);
212 double tmp = ulonglong2double(rows);
214 s->set_real((
double) 0.0, 1,my_thd_charset);
217 double tmp2 = ((sum_sqr - sum * sum / (tmp - nulls)) /
219 s->set_real(((
double) tmp2 <= 0.0 ? 0.0 : sqrt(tmp2)), item->decimals,my_thd_charset);
223 uint decimals() {
return item->decimals; }
224 friend int collect_real(
double *element, element_count count,
226 tree_walk_action collect_enum()
227 {
return (tree_walk_action) collect_real;}
230 int collect_longlong(longlong *element, element_count count,
235 longlong min_arg, max_arg;
236 longlong sum, sum_sqr;
240 min_arg(0), max_arg(0), sum(0), sum_sqr(0)
241 { init_tree(&tree, 0, 0,
sizeof(longlong),
242 (qsort_cmp2) compare_longlong2, 0, NULL, NULL); }
245 void get_opt_type(
String*, ha_rows);
246 String *get_min_arg(
String *s) { s->set(min_arg,my_thd_charset);
return s; }
247 String *get_max_arg(
String *s) { s->set(max_arg,my_thd_charset);
return s; }
251 s->set_real((
double) 0.0, 1,my_thd_charset);
253 s->set_real(((
double) sum / (
double) (rows - nulls)), DEC_IN_AVG,my_thd_charset);
258 double tmp = ulonglong2double(rows);
260 s->set_real((
double) 0.0, 1,my_thd_charset);
263 double tmp2 = ((sum_sqr - sum * sum / (tmp - nulls)) /
265 s->set_real(((
double) tmp2 <= 0.0 ? 0.0 : sqrt(tmp2)), DEC_IN_AVG,my_thd_charset);
269 friend int collect_longlong(longlong *element, element_count count,
271 tree_walk_action collect_enum()
272 {
return (tree_walk_action) collect_longlong;}
275 int collect_ulonglong(ulonglong *element, element_count count,
280 ulonglong min_arg, max_arg;
281 ulonglong sum, sum_sqr;
285 min_arg(0), max_arg(0), sum(0),sum_sqr(0)
286 { init_tree(&tree, 0, 0,
sizeof(ulonglong),
287 (qsort_cmp2) compare_ulonglong2, 0, NULL, NULL); }
289 void get_opt_type(
String*, ha_rows);
290 String *get_min_arg(
String *s) { s->set(min_arg,my_thd_charset);
return s; }
291 String *get_max_arg(
String *s) { s->set(max_arg,my_thd_charset);
return s; }
295 s->set_real((
double) 0.0, 1,my_thd_charset);
297 s->set_real((ulonglong2double(sum) / ulonglong2double(rows - nulls)),
298 DEC_IN_AVG,my_thd_charset);
303 double tmp = ulonglong2double(rows);
305 s->set_real((
double) 0.0, 1,my_thd_charset);
308 double tmp2 = ((ulonglong2double(sum_sqr) -
309 ulonglong2double(sum * sum) / (tmp - nulls)) /
311 s->set_real(((
double) tmp2 <= 0.0 ? 0.0 : sqrt(tmp2)), DEC_IN_AVG,my_thd_charset);
315 friend int collect_ulonglong(ulonglong *element, element_count count,
317 tree_walk_action collect_enum()
318 {
return (tree_walk_action) collect_ulonglong; }
328 select_result *result;
335 uint output_str_length;
338 const uint max_tree_elements;
339 const uint max_treemem;
342 select_analyse(select_result *result,
const Proc_analyse_params *params)
343 : result(result), f_info(NULL), f_end(NULL), rows(0), output_str_length(0),
344 max_tree_elements(params->max_tree_elements),
345 max_treemem(params->max_treemem)
350 virtual void cleanup();
352 {
return array_elements(func_items); }
353 virtual int prepare(
List<Item> &list, SELECT_LEX_UNIT *u)
354 {
return result->prepare(list, u); }
355 virtual bool send_result_set_metadata(
List<Item> &fields, uint flag);
357 virtual bool send_eof();
358 virtual void abort_result_set();
362 bool change_columns();