You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1299 lines
35 KiB

26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
22 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
WL#3817: Simplify string / memory area types and make things more consistent (first part) The following type conversions was done: - Changed byte to uchar - Changed gptr to uchar* - Change my_string to char * - Change my_size_t to size_t - Change size_s to size_t Removed declaration of byte, gptr, my_string, my_size_t and size_s. Following function parameter changes was done: - All string functions in mysys/strings was changed to use size_t instead of uint for string lengths. - All read()/write() functions changed to use size_t (including vio). - All protocoll functions changed to use size_t instead of uint - Functions that used a pointer to a string length was changed to use size_t* - Changed malloc(), free() and related functions from using gptr to use void * as this requires fewer casts in the code and is more in line with how the standard functions work. - Added extra length argument to dirname_part() to return the length of the created string. - Changed (at least) following functions to take uchar* as argument: - db_dump() - my_net_write() - net_write_command() - net_store_data() - DBUG_DUMP() - decimal2bin() & bin2decimal() - Changed my_compress() and my_uncompress() to use size_t. Changed one argument to my_uncompress() from a pointer to a value as we only return one value (makes function easier to use). - Changed type of 'pack_data' argument to packfrm() to avoid casts. - Changed in readfrm() and writefrom(), ha_discover and handler::discover() the type for argument 'frmdata' to uchar** to avoid casts. - Changed most Field functions to use uchar* instead of char* (reduced a lot of casts). - Changed field->val_xxx(xxx, new_ptr) to take const pointers. Other changes: - Removed a lot of not needed casts - Added a few new cast required by other changes - Added some cast to my_multi_malloc() arguments for safety (as string lengths needs to be uint, not size_t). - Fixed all calls to hash-get-key functions to use size_t*. (Needed to be done explicitely as this conflict was often hided by casting the function to hash_get_key). - Changed some buffers to memory regions to uchar* to avoid casts. - Changed some string lengths from uint to size_t. - Changed field->ptr to be uchar* instead of char*. This allowed us to get rid of a lot of casts. - Some changes from true -> TRUE, false -> FALSE, unsigned char -> uchar - Include zlib.h in some files as we needed declaration of crc32() - Changed MY_FILE_ERROR to be (size_t) -1. - Changed many variables to hold the result of my_read() / my_write() to be size_t. This was needed to properly detect errors (which are returned as (size_t) -1). - Removed some very old VMS code - Changed packfrm()/unpackfrm() to not be depending on uint size (portability fix) - Removed windows specific code to restore cursor position as this causes slowdown on windows and we should not mix read() and pread() calls anyway as this is not thread safe. Updated function comment to reflect this. Changed function that depended on original behavior of my_pwrite() to itself restore the cursor position (one such case). - Added some missing checking of return value of malloc(). - Changed definition of MOD_PAD_CHAR_TO_FULL_LENGTH to avoid 'long' overflow. - Changed type of table_def::m_size from my_size_t to ulong to reflect that m_size is the number of elements in the array, not a string/memory length. - Moved THD::max_row_length() to table.cc (as it's not depending on THD). Inlined max_row_length_blob() into this function. - More function comments - Fixed some compiler warnings when compiled without partitions. - Removed setting of LEX_STRING() arguments in declaration (portability fix). - Some trivial indentation/variable name changes. - Some trivial code simplifications: - Replaced some calls to alloc_root + memcpy to use strmake_root()/strdup_root(). - Changed some calls from memdup() to strmake() (Safety fix) - Simpler loops in client-simple.c
19 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
26 years ago
21 years ago
  1. /*
  2. Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
  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.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  13. */
  14. /* Analyse database */
  15. /* TODO: - Check if any character fields can be of any date type
  16. ** (date, datetime, year, time, timestamp, newdate)
  17. ** - Check if any number field should be a timestamp
  18. ** - type set is out of optimization yet
  19. */
  20. #ifdef USE_PRAGMA_IMPLEMENTATION
  21. #pragma implementation // gcc: Class implementation
  22. #endif
  23. #define MYSQL_LEX 1
  24. #include "mysql_priv.h"
  25. #include "procedure.h"
  26. #include "sql_analyse.h"
  27. #include <m_ctype.h>
  28. #define MAX_TREEMEM 8192
  29. #define MAX_TREE_ELEMENTS 256
  30. int sortcmp2(void* cmp_arg __attribute__((unused)),
  31. const String *a,const String *b)
  32. {
  33. return sortcmp(a,b,a->charset());
  34. }
  35. int compare_double2(void* cmp_arg __attribute__((unused)),
  36. const double *s, const double *t)
  37. {
  38. return compare_double(s,t);
  39. }
  40. int compare_longlong2(void* cmp_arg __attribute__((unused)),
  41. const longlong *s, const longlong *t)
  42. {
  43. return compare_longlong(s,t);
  44. }
  45. int compare_ulonglong2(void* cmp_arg __attribute__((unused)),
  46. const ulonglong *s, const ulonglong *t)
  47. {
  48. return compare_ulonglong(s,t);
  49. }
  50. int compare_decimal2(int* len, const char *s, const char *t)
  51. {
  52. return memcmp(s, t, *len);
  53. }
  54. Procedure *
  55. proc_analyse_init(THD *thd, ORDER *param, select_result *result,
  56. List<Item> &field_list)
  57. {
  58. char *proc_name = (*param->item)->name;
  59. analyse *pc = new analyse(result);
  60. field_info **f_info;
  61. DBUG_ENTER("proc_analyse_init");
  62. if (!pc)
  63. DBUG_RETURN(0);
  64. if (!(param = param->next))
  65. {
  66. pc->max_tree_elements = MAX_TREE_ELEMENTS;
  67. pc->max_treemem = MAX_TREEMEM;
  68. }
  69. else if (param->next)
  70. {
  71. // first parameter
  72. if (!(*param->item)->fixed && (*param->item)->fix_fields(thd, param->item))
  73. {
  74. DBUG_PRINT("info", ("fix_fields() for the first parameter failed"));
  75. goto err;
  76. }
  77. if ((*param->item)->type() != Item::INT_ITEM ||
  78. (*param->item)->val_real() < 0)
  79. {
  80. my_error(ER_WRONG_PARAMETERS_TO_PROCEDURE, MYF(0), proc_name);
  81. goto err;
  82. }
  83. pc->max_tree_elements = (uint) (*param->item)->val_int();
  84. param = param->next;
  85. if (param->next) // no third parameter possible
  86. {
  87. my_error(ER_WRONG_PARAMCOUNT_TO_PROCEDURE, MYF(0), proc_name);
  88. goto err;
  89. }
  90. // second parameter
  91. if (!(*param->item)->fixed && (*param->item)->fix_fields(thd, param->item))
  92. {
  93. DBUG_PRINT("info", ("fix_fields() for the second parameter failed"));
  94. goto err;
  95. }
  96. if ((*param->item)->type() != Item::INT_ITEM ||
  97. (*param->item)->val_real() < 0)
  98. {
  99. my_error(ER_WRONG_PARAMETERS_TO_PROCEDURE, MYF(0), proc_name);
  100. goto err;
  101. }
  102. pc->max_treemem = (uint) (*param->item)->val_int();
  103. }
  104. else if ((*param->item)->type() != Item::INT_ITEM ||
  105. (*param->item)->val_real() < 0)
  106. {
  107. my_error(ER_WRONG_PARAMETERS_TO_PROCEDURE, MYF(0), proc_name);
  108. goto err;
  109. }
  110. // if only one parameter was given, it will be the value of max_tree_elements
  111. else
  112. {
  113. pc->max_tree_elements = (uint) (*param->item)->val_int();
  114. pc->max_treemem = MAX_TREEMEM;
  115. }
  116. if (!(pc->f_info=
  117. (field_info**)sql_alloc(sizeof(field_info*)*field_list.elements)))
  118. goto err;
  119. pc->f_end = pc->f_info + field_list.elements;
  120. pc->fields = field_list;
  121. {
  122. List_iterator_fast<Item> it(pc->fields);
  123. f_info = pc->f_info;
  124. Item *item;
  125. while ((item = it++))
  126. {
  127. field_info *new_field;
  128. switch (item->result_type()) {
  129. case INT_RESULT:
  130. // Check if fieldtype is ulonglong
  131. if (item->type() == Item::FIELD_ITEM &&
  132. ((Item_field*) item)->field->type() == MYSQL_TYPE_LONGLONG &&
  133. ((Field_longlong*) ((Item_field*) item)->field)->unsigned_flag)
  134. new_field= new field_ulonglong(item, pc);
  135. else
  136. new_field= new field_longlong(item, pc);
  137. break;
  138. case REAL_RESULT:
  139. new_field= new field_real(item, pc);
  140. break;
  141. case DECIMAL_RESULT:
  142. new_field= new field_decimal(item, pc);
  143. break;
  144. case STRING_RESULT:
  145. new_field= new field_str(item, pc);
  146. break;
  147. default:
  148. goto err;
  149. }
  150. *f_info++= new_field;
  151. }
  152. }
  153. DBUG_RETURN(pc);
  154. err:
  155. delete pc;
  156. DBUG_RETURN(0);
  157. }
  158. /*
  159. Return 1 if number, else return 0
  160. store info about found number in info
  161. NOTE:It is expected, that elements of 'info' are all zero!
  162. */
  163. bool test_if_number(NUM_INFO *info, const char *str, uint str_len)
  164. {
  165. const char *begin, *end= str + str_len;
  166. DBUG_ENTER("test_if_number");
  167. /*
  168. MySQL removes any endspaces of a string, so we must take care only of
  169. spaces in front of a string
  170. */
  171. for (; str != end && my_isspace(system_charset_info, *str); str++) ;
  172. if (str == end)
  173. DBUG_RETURN(0);
  174. if (*str == '-')
  175. {
  176. info->negative = 1;
  177. if (++str == end || *str == '0') // converting -0 to a number
  178. DBUG_RETURN(0); // might lose information
  179. }
  180. else
  181. info->negative = 0;
  182. begin = str;
  183. for (; str != end && my_isdigit(system_charset_info,*str); str++)
  184. {
  185. if (!info->integers && *str == '0' && (str + 1) != end &&
  186. my_isdigit(system_charset_info,*(str + 1)))
  187. info->zerofill = 1; // could be a postnumber for example
  188. info->integers++;
  189. }
  190. if (str == end && info->integers)
  191. {
  192. char *endpos= (char*) end;
  193. int error;
  194. info->ullval= (ulonglong) my_strtoll10(begin, &endpos, &error);
  195. if (info->integers == 1)
  196. DBUG_RETURN(0); // single number can't be zerofill
  197. info->maybe_zerofill = 1;
  198. DBUG_RETURN(1); // a zerofill number, or an integer
  199. }
  200. if (*str == '.' || *str == 'e' || *str == 'E')
  201. {
  202. if (info->zerofill) // can't be zerofill anymore
  203. DBUG_RETURN(0);
  204. if ((str + 1) == end) // number was something like '123[.eE]'
  205. {
  206. char *endpos= (char*) str;
  207. int error;
  208. info->ullval= (ulonglong) my_strtoll10(begin, &endpos, &error);
  209. DBUG_RETURN(1);
  210. }
  211. if (*str == 'e' || *str == 'E') // number may be something like '1e+50'
  212. {
  213. str++;
  214. if (*str != '-' && *str != '+')
  215. DBUG_RETURN(0);
  216. for (str++; str != end && my_isdigit(system_charset_info,*str); str++) ;
  217. if (str == end)
  218. {
  219. info->is_float = 1; // we can't use variable decimals here
  220. DBUG_RETURN(1);
  221. }
  222. DBUG_RETURN(0);
  223. }
  224. for (str++; *(end - 1) == '0'; end--) ; // jump over zeros at the end
  225. if (str == end) // number was something like '123.000'
  226. {
  227. char *endpos= (char*) str;
  228. int error;
  229. info->ullval= (ulonglong) my_strtoll10(begin, &endpos, &error);
  230. DBUG_RETURN(1);
  231. }
  232. for (; str != end && my_isdigit(system_charset_info,*str); str++)
  233. info->decimals++;
  234. if (str == end)
  235. {
  236. info->dval = my_atof(begin);
  237. DBUG_RETURN(1);
  238. }
  239. }
  240. DBUG_RETURN(0);
  241. }
  242. /*
  243. Stores the biggest and the smallest value from current 'info'
  244. to ev_num_info
  245. If info contains an ulonglong number, which is bigger than
  246. biggest positive number able to be stored in a longlong variable
  247. and is marked as negative, function will return 0, else 1.
  248. */
  249. bool get_ev_num_info(EV_NUM_INFO *ev_info, NUM_INFO *info, const char *num)
  250. {
  251. if (info->negative)
  252. {
  253. if (((longlong) info->ullval) < 0)
  254. return 0; // Impossible to store as a negative number
  255. ev_info->llval = -(longlong) max((ulonglong) -ev_info->llval,
  256. info->ullval);
  257. ev_info->min_dval = (double) -max(-ev_info->min_dval, info->dval);
  258. }
  259. else // ulonglong is as big as bigint in MySQL
  260. {
  261. if ((check_ulonglong(num, info->integers) == DECIMAL_NUM))
  262. return 0;
  263. ev_info->ullval = (ulonglong) max(ev_info->ullval, info->ullval);
  264. ev_info->max_dval = (double) max(ev_info->max_dval, info->dval);
  265. }
  266. return 1;
  267. } // get_ev_num_info
  268. void free_string(String *s)
  269. {
  270. s->free();
  271. }
  272. void field_str::add()
  273. {
  274. char buff[MAX_FIELD_WIDTH], *ptr;
  275. String s(buff, sizeof(buff),&my_charset_bin), *res;
  276. ulong length;
  277. if (!(res = item->val_str(&s)))
  278. {
  279. nulls++;
  280. return;
  281. }
  282. if (!(length = res->length()))
  283. empty++;
  284. else
  285. {
  286. ptr = (char*) res->ptr();
  287. if (*(ptr + (length - 1)) == ' ')
  288. must_be_blob = 1;
  289. }
  290. if (can_be_still_num)
  291. {
  292. bzero((char*) &num_info, sizeof(num_info));
  293. if (!test_if_number(&num_info, res->ptr(), (uint) length))
  294. can_be_still_num = 0;
  295. if (!found)
  296. {
  297. bzero((char*) &ev_num_info, sizeof(ev_num_info));
  298. was_zero_fill = num_info.zerofill;
  299. }
  300. else if (num_info.zerofill != was_zero_fill && !was_maybe_zerofill)
  301. can_be_still_num = 0; // one more check needed, when length is counted
  302. if (can_be_still_num)
  303. can_be_still_num = get_ev_num_info(&ev_num_info, &num_info, res->ptr());
  304. was_maybe_zerofill = num_info.maybe_zerofill;
  305. }
  306. /* Update min and max arguments */
  307. if (!found)
  308. {
  309. found = 1;
  310. min_arg.copy(*res);
  311. max_arg.copy(*res);
  312. min_length = max_length = length; sum=length;
  313. }
  314. else if (length)
  315. {
  316. sum += length;
  317. if (length < min_length)
  318. min_length = length;
  319. if (length > max_length)
  320. max_length = length;
  321. if (sortcmp(res, &min_arg,item->collation.collation) < 0)
  322. min_arg.copy(*res);
  323. if (sortcmp(res, &max_arg,item->collation.collation) > 0)
  324. max_arg.copy(*res);
  325. }
  326. if (room_in_tree)
  327. {
  328. if (res != &s)
  329. s.copy(*res);
  330. if (!tree_search(&tree, (void*) &s, tree.custom_arg)) // If not in tree
  331. {
  332. s.copy(); // slow, when SAFE_MALLOC is in use
  333. if (!tree_insert(&tree, (void*) &s, 0, tree.custom_arg))
  334. {
  335. room_in_tree = 0; // Remove tree, out of RAM ?
  336. delete_tree(&tree);
  337. }
  338. else
  339. {
  340. bzero((char*) &s, sizeof(s)); // Let tree handle free of this
  341. if ((treemem += length) > pc->max_treemem)
  342. {
  343. room_in_tree = 0; // Remove tree, too big tree
  344. delete_tree(&tree);
  345. }
  346. }
  347. }
  348. }
  349. if ((num_info.zerofill && (max_length != min_length)) ||
  350. (was_zero_fill && (max_length != min_length)))
  351. can_be_still_num = 0; // zerofilled numbers must be of same length
  352. } // field_str::add
  353. void field_real::add()
  354. {
  355. char buff[MAX_FIELD_WIDTH], *ptr, *end;
  356. double num= item->val_real();
  357. uint length, zero_count, decs;
  358. TREE_ELEMENT *element;
  359. if (item->null_value)
  360. {
  361. nulls++;
  362. return;
  363. }
  364. if (num == 0.0)
  365. empty++;
  366. if ((decs = decimals()) == NOT_FIXED_DEC)
  367. {
  368. length= sprintf(buff, "%g", num);
  369. if (rint(num) != num)
  370. max_notzero_dec_len = 1;
  371. }
  372. else
  373. {
  374. #ifdef HAVE_SNPRINTF
  375. buff[sizeof(buff)-1]=0; // Safety
  376. snprintf(buff, sizeof(buff)-1, "%-.*f", (int) decs, num);
  377. length = (uint) strlen(buff);
  378. #else
  379. length= sprintf(buff, "%-.*f", (int) decs, num);
  380. #endif
  381. // We never need to check further than this
  382. end = buff + length - 1 - decs + max_notzero_dec_len;
  383. zero_count = 0;
  384. for (ptr = buff + length - 1; ptr > end && *ptr == '0'; ptr--)
  385. zero_count++;
  386. if ((decs - zero_count > max_notzero_dec_len))
  387. max_notzero_dec_len = decs - zero_count;
  388. }
  389. if (room_in_tree)
  390. {
  391. if (!(element = tree_insert(&tree, (void*) &num, 0, tree.custom_arg)))
  392. {
  393. room_in_tree = 0; // Remove tree, out of RAM ?
  394. delete_tree(&tree);
  395. }
  396. /*
  397. if element->count == 1, this element can be found only once from tree
  398. if element->count == 2, or more, this element is already in tree
  399. */
  400. else if (element->count == 1 && (tree_elements++) >= pc->max_tree_elements)
  401. {
  402. room_in_tree = 0; // Remove tree, too many elements
  403. delete_tree(&tree);
  404. }
  405. }
  406. if (!found)
  407. {
  408. found = 1;
  409. min_arg = max_arg = sum = num;
  410. sum_sqr = num * num;
  411. min_length = max_length = length;
  412. }
  413. else if (num != 0.0)
  414. {
  415. sum += num;
  416. sum_sqr += num * num;
  417. if (length < min_length)
  418. min_length = length;
  419. if (length > max_length)
  420. max_length = length;
  421. if (compare_double(&num, &min_arg) < 0)
  422. min_arg = num;
  423. if (compare_double(&num, &max_arg) > 0)
  424. max_arg = num;
  425. }
  426. } // field_real::add
  427. void field_decimal::add()
  428. {
  429. /*TODO - remove rounding stuff after decimal_div returns proper frac */
  430. my_decimal dec_buf, *dec= item->val_decimal(&dec_buf);
  431. my_decimal rounded;
  432. uint length;
  433. TREE_ELEMENT *element;
  434. if (item->null_value)
  435. {
  436. nulls++;
  437. return;
  438. }
  439. my_decimal_round(E_DEC_FATAL_ERROR, dec, item->decimals, FALSE,&rounded);
  440. dec= &rounded;
  441. length= my_decimal_string_length(dec);
  442. if (decimal_is_zero(dec))
  443. empty++;
  444. if (room_in_tree)
  445. {
  446. uchar buf[DECIMAL_MAX_FIELD_SIZE];
  447. my_decimal2binary(E_DEC_FATAL_ERROR, dec, buf,
  448. item->max_length, item->decimals);
  449. if (!(element = tree_insert(&tree, (void*)buf, 0, tree.custom_arg)))
  450. {
  451. room_in_tree = 0; // Remove tree, out of RAM ?
  452. delete_tree(&tree);
  453. }
  454. /*
  455. if element->count == 1, this element can be found only once from tree
  456. if element->count == 2, or more, this element is already in tree
  457. */
  458. else if (element->count == 1 && (tree_elements++) >= pc->max_tree_elements)
  459. {
  460. room_in_tree = 0; // Remove tree, too many elements
  461. delete_tree(&tree);
  462. }
  463. }
  464. if (!found)
  465. {
  466. found = 1;
  467. min_arg = max_arg = sum[0] = *dec;
  468. min_arg.fix_buffer_pointer();
  469. max_arg.fix_buffer_pointer();
  470. sum[0].fix_buffer_pointer();
  471. my_decimal_mul(E_DEC_FATAL_ERROR, sum_sqr, dec, dec);
  472. cur_sum= 0;
  473. min_length = max_length = length;
  474. }
  475. else if (!decimal_is_zero(dec))
  476. {
  477. int next_cur_sum= cur_sum ^ 1;
  478. my_decimal sqr_buf;
  479. my_decimal_add(E_DEC_FATAL_ERROR, sum+next_cur_sum, sum+cur_sum, dec);
  480. my_decimal_mul(E_DEC_FATAL_ERROR, &sqr_buf, dec, dec);
  481. my_decimal_add(E_DEC_FATAL_ERROR,
  482. sum_sqr+next_cur_sum, sum_sqr+cur_sum, &sqr_buf);
  483. cur_sum= next_cur_sum;
  484. if (length < min_length)
  485. min_length = length;
  486. if (length > max_length)
  487. max_length = length;
  488. if (my_decimal_cmp(dec, &min_arg) < 0)
  489. {
  490. min_arg= *dec;
  491. min_arg.fix_buffer_pointer();
  492. }
  493. if (my_decimal_cmp(dec, &max_arg) > 0)
  494. {
  495. max_arg= *dec;
  496. max_arg.fix_buffer_pointer();
  497. }
  498. }
  499. }
  500. void field_longlong::add()
  501. {
  502. char buff[MAX_FIELD_WIDTH];
  503. longlong num = item->val_int();
  504. uint length = (uint) (longlong10_to_str(num, buff, -10) - buff);
  505. TREE_ELEMENT *element;
  506. if (item->null_value)
  507. {
  508. nulls++;
  509. return;
  510. }
  511. if (num == 0)
  512. empty++;
  513. if (room_in_tree)
  514. {
  515. if (!(element = tree_insert(&tree, (void*) &num, 0, tree.custom_arg)))
  516. {
  517. room_in_tree = 0; // Remove tree, out of RAM ?
  518. delete_tree(&tree);
  519. }
  520. /*
  521. if element->count == 1, this element can be found only once from tree
  522. if element->count == 2, or more, this element is already in tree
  523. */
  524. else if (element->count == 1 && (tree_elements++) >= pc->max_tree_elements)
  525. {
  526. room_in_tree = 0; // Remove tree, too many elements
  527. delete_tree(&tree);
  528. }
  529. }
  530. if (!found)
  531. {
  532. found = 1;
  533. min_arg = max_arg = sum = num;
  534. sum_sqr = num * num;
  535. min_length = max_length = length;
  536. }
  537. else if (num != 0)
  538. {
  539. sum += num;
  540. sum_sqr += num * num;
  541. if (length < min_length)
  542. min_length = length;
  543. if (length > max_length)
  544. max_length = length;
  545. if (compare_longlong(&num, &min_arg) < 0)
  546. min_arg = num;
  547. if (compare_longlong(&num, &max_arg) > 0)
  548. max_arg = num;
  549. }
  550. } // field_longlong::add
  551. void field_ulonglong::add()
  552. {
  553. char buff[MAX_FIELD_WIDTH];
  554. longlong num = item->val_int();
  555. uint length = (uint) (longlong10_to_str(num, buff, 10) - buff);
  556. TREE_ELEMENT *element;
  557. if (item->null_value)
  558. {
  559. nulls++;
  560. return;
  561. }
  562. if (num == 0)
  563. empty++;
  564. if (room_in_tree)
  565. {
  566. if (!(element = tree_insert(&tree, (void*) &num, 0, tree.custom_arg)))
  567. {
  568. room_in_tree = 0; // Remove tree, out of RAM ?
  569. delete_tree(&tree);
  570. }
  571. /*
  572. if element->count == 1, this element can be found only once from tree
  573. if element->count == 2, or more, this element is already in tree
  574. */
  575. else if (element->count == 1 && (tree_elements++) >= pc->max_tree_elements)
  576. {
  577. room_in_tree = 0; // Remove tree, too many elements
  578. delete_tree(&tree);
  579. }
  580. }
  581. if (!found)
  582. {
  583. found = 1;
  584. min_arg = max_arg = sum = num;
  585. sum_sqr = num * num;
  586. min_length = max_length = length;
  587. }
  588. else if (num != 0)
  589. {
  590. sum += num;
  591. sum_sqr += num * num;
  592. if (length < min_length)
  593. min_length = length;
  594. if (length > max_length)
  595. max_length = length;
  596. if (compare_ulonglong((ulonglong*) &num, &min_arg) < 0)
  597. min_arg = num;
  598. if (compare_ulonglong((ulonglong*) &num, &max_arg) > 0)
  599. max_arg = num;
  600. }
  601. } // field_ulonglong::add
  602. int analyse::send_row(List<Item> & /* field_list */)
  603. {
  604. field_info **f = f_info;
  605. rows++;
  606. for (;f != f_end; f++)
  607. {
  608. (*f)->add();
  609. }
  610. return 0;
  611. } // analyse::send_row
  612. int analyse::end_of_records()
  613. {
  614. field_info **f = f_info;
  615. char buff[MAX_FIELD_WIDTH];
  616. String *res, s_min(buff, sizeof(buff),&my_charset_bin),
  617. s_max(buff, sizeof(buff),&my_charset_bin),
  618. ans(buff, sizeof(buff),&my_charset_bin);
  619. for (; f != f_end; f++)
  620. {
  621. func_items[0]->set((*f)->item->full_name());
  622. if (!(*f)->found)
  623. {
  624. func_items[1]->null_value = 1;
  625. func_items[2]->null_value = 1;
  626. }
  627. else
  628. {
  629. func_items[1]->null_value = 0;
  630. res = (*f)->get_min_arg(&s_min);
  631. func_items[1]->set(res->ptr(), res->length(), res->charset());
  632. func_items[2]->null_value = 0;
  633. res = (*f)->get_max_arg(&s_max);
  634. func_items[2]->set(res->ptr(), res->length(), res->charset());
  635. }
  636. func_items[3]->set((longlong) (*f)->min_length);
  637. func_items[4]->set((longlong) (*f)->max_length);
  638. func_items[5]->set((longlong) (*f)->empty);
  639. func_items[6]->set((longlong) (*f)->nulls);
  640. res = (*f)->avg(&s_max, rows);
  641. func_items[7]->set(res->ptr(), res->length(), res->charset());
  642. func_items[8]->null_value = 0;
  643. res = (*f)->std(&s_max, rows);
  644. if (!res)
  645. func_items[8]->null_value = 1;
  646. else
  647. func_items[8]->set(res->ptr(), res->length(), res->charset());
  648. /*
  649. count the dots, quotas, etc. in (ENUM("a","b","c"...))
  650. If tree has been removed, don't suggest ENUM.
  651. treemem is used to measure the size of tree for strings,
  652. tree_elements is used to count the elements
  653. max_treemem tells how long the string starting from ENUM("... and
  654. ending to ..") shall at maximum be. If case is about numbers,
  655. max_tree_elements will tell the length of the above, now
  656. every number is considered as length 1
  657. */
  658. if (((*f)->treemem || (*f)->tree_elements) &&
  659. (*f)->tree.elements_in_tree &&
  660. (((*f)->treemem ? max_treemem : max_tree_elements) >
  661. (((*f)->treemem ? (*f)->treemem : (*f)->tree_elements) +
  662. ((*f)->tree.elements_in_tree * 3 - 1 + 6))))
  663. {
  664. char tmp[331]; //331, because one double prec. num. can be this long
  665. String tmp_str(tmp, sizeof(tmp),&my_charset_bin);
  666. TREE_INFO tree_info;
  667. tree_info.str = &tmp_str;
  668. tree_info.found = 0;
  669. tree_info.item = (*f)->item;
  670. tmp_str.set(STRING_WITH_LEN("ENUM("),&my_charset_bin);
  671. tree_walk(&(*f)->tree, (*f)->collect_enum(), (char*) &tree_info,
  672. left_root_right);
  673. tmp_str.append(')');
  674. if (!(*f)->nulls)
  675. tmp_str.append(STRING_WITH_LEN(" NOT NULL"));
  676. output_str_length = tmp_str.length();
  677. func_items[9]->set(tmp_str.ptr(), tmp_str.length(), tmp_str.charset());
  678. if (result->send_data(result_fields))
  679. return -1;
  680. continue;
  681. }
  682. ans.length(0);
  683. if (!(*f)->treemem && !(*f)->tree_elements)
  684. ans.append(STRING_WITH_LEN("CHAR(0)"));
  685. else if ((*f)->item->type() == Item::FIELD_ITEM)
  686. {
  687. switch (((Item_field*) (*f)->item)->field->real_type())
  688. {
  689. case MYSQL_TYPE_TIMESTAMP:
  690. ans.append(STRING_WITH_LEN("TIMESTAMP"));
  691. break;
  692. case MYSQL_TYPE_DATETIME:
  693. ans.append(STRING_WITH_LEN("DATETIME"));
  694. break;
  695. case MYSQL_TYPE_DATE:
  696. case MYSQL_TYPE_NEWDATE:
  697. ans.append(STRING_WITH_LEN("DATE"));
  698. break;
  699. case MYSQL_TYPE_SET:
  700. ans.append(STRING_WITH_LEN("SET"));
  701. break;
  702. case MYSQL_TYPE_YEAR:
  703. ans.append(STRING_WITH_LEN("YEAR"));
  704. break;
  705. case MYSQL_TYPE_TIME:
  706. ans.append(STRING_WITH_LEN("TIME"));
  707. break;
  708. case MYSQL_TYPE_DECIMAL:
  709. ans.append(STRING_WITH_LEN("DECIMAL"));
  710. // if item is FIELD_ITEM, it _must_be_ Field_num in this case
  711. if (((Field_num*) ((Item_field*) (*f)->item)->field)->zerofill)
  712. ans.append(STRING_WITH_LEN(" ZEROFILL"));
  713. break;
  714. default:
  715. (*f)->get_opt_type(&ans, rows);
  716. break;
  717. }
  718. }
  719. if (!(*f)->nulls)
  720. ans.append(STRING_WITH_LEN(" NOT NULL"));
  721. func_items[9]->set(ans.ptr(), ans.length(), ans.charset());
  722. if (result->send_data(result_fields))
  723. return -1;
  724. }
  725. return 0;
  726. } // analyse::end_of_records
  727. void field_str::get_opt_type(String *answer, ha_rows total_rows)
  728. {
  729. char buff[MAX_FIELD_WIDTH];
  730. if (can_be_still_num)
  731. {
  732. if (num_info.is_float)
  733. sprintf(buff, "DOUBLE"); // number was like 1e+50... TODO:
  734. else if (num_info.decimals) // DOUBLE(%d,%d) sometime
  735. {
  736. if (num_info.dval > -FLT_MAX && num_info.dval < FLT_MAX)
  737. sprintf(buff, "FLOAT(%d,%d)", (num_info.integers + num_info.decimals), num_info.decimals);
  738. else
  739. sprintf(buff, "DOUBLE(%d,%d)", (num_info.integers + num_info.decimals), num_info.decimals);
  740. }
  741. else if (ev_num_info.llval >= -128 &&
  742. ev_num_info.ullval <=
  743. (ulonglong) (ev_num_info.llval >= 0 ? 255 : 127))
  744. sprintf(buff, "TINYINT(%d)", num_info.integers);
  745. else if (ev_num_info.llval >= INT_MIN16 &&
  746. ev_num_info.ullval <= (ulonglong) (ev_num_info.llval >= 0 ?
  747. UINT_MAX16 : INT_MAX16))
  748. sprintf(buff, "SMALLINT(%d)", num_info.integers);
  749. else if (ev_num_info.llval >= INT_MIN24 &&
  750. ev_num_info.ullval <= (ulonglong) (ev_num_info.llval >= 0 ?
  751. UINT_MAX24 : INT_MAX24))
  752. sprintf(buff, "MEDIUMINT(%d)", num_info.integers);
  753. else if (ev_num_info.llval >= INT_MIN32 &&
  754. ev_num_info.ullval <= (ulonglong) (ev_num_info.llval >= 0 ?
  755. UINT_MAX32 : INT_MAX32))
  756. sprintf(buff, "INT(%d)", num_info.integers);
  757. else
  758. sprintf(buff, "BIGINT(%d)", num_info.integers);
  759. answer->append(buff, (uint) strlen(buff));
  760. if (ev_num_info.llval >= 0 && ev_num_info.min_dval >= 0)
  761. answer->append(STRING_WITH_LEN(" UNSIGNED"));
  762. if (num_info.zerofill)
  763. answer->append(STRING_WITH_LEN(" ZEROFILL"));
  764. }
  765. else if (max_length < 256)
  766. {
  767. if (must_be_blob)
  768. {
  769. if (item->collation.collation == &my_charset_bin)
  770. answer->append(STRING_WITH_LEN("TINYBLOB"));
  771. else
  772. answer->append(STRING_WITH_LEN("TINYTEXT"));
  773. }
  774. else if ((max_length * (total_rows - nulls)) < (sum + total_rows))
  775. {
  776. sprintf(buff, "CHAR(%d)", (int) max_length);
  777. answer->append(buff, (uint) strlen(buff));
  778. }
  779. else
  780. {
  781. sprintf(buff, "VARCHAR(%d)", (int) max_length);
  782. answer->append(buff, (uint) strlen(buff));
  783. }
  784. }
  785. else if (max_length < (1L << 16))
  786. {
  787. if (item->collation.collation == &my_charset_bin)
  788. answer->append(STRING_WITH_LEN("BLOB"));
  789. else
  790. answer->append(STRING_WITH_LEN("TEXT"));
  791. }
  792. else if (max_length < (1L << 24))
  793. {
  794. if (item->collation.collation == &my_charset_bin)
  795. answer->append(STRING_WITH_LEN("MEDIUMBLOB"));
  796. else
  797. answer->append(STRING_WITH_LEN("MEDIUMTEXT"));
  798. }
  799. else
  800. {
  801. if (item->collation.collation == &my_charset_bin)
  802. answer->append(STRING_WITH_LEN("LONGBLOB"));
  803. else
  804. answer->append(STRING_WITH_LEN("LONGTEXT"));
  805. }
  806. } // field_str::get_opt_type
  807. void field_real::get_opt_type(String *answer,
  808. ha_rows total_rows __attribute__((unused)))
  809. {
  810. char buff[MAX_FIELD_WIDTH];
  811. if (!max_notzero_dec_len)
  812. {
  813. int len= (int) max_length - ((item->decimals == NOT_FIXED_DEC) ?
  814. 0 : (item->decimals + 1));
  815. if (min_arg >= -128 && max_arg <= (min_arg >= 0 ? 255 : 127))
  816. sprintf(buff, "TINYINT(%d)", len);
  817. else if (min_arg >= INT_MIN16 && max_arg <= (min_arg >= 0 ?
  818. UINT_MAX16 : INT_MAX16))
  819. sprintf(buff, "SMALLINT(%d)", len);
  820. else if (min_arg >= INT_MIN24 && max_arg <= (min_arg >= 0 ?
  821. UINT_MAX24 : INT_MAX24))
  822. sprintf(buff, "MEDIUMINT(%d)", len);
  823. else if (min_arg >= INT_MIN32 && max_arg <= (min_arg >= 0 ?
  824. UINT_MAX32 : INT_MAX32))
  825. sprintf(buff, "INT(%d)", len);
  826. else
  827. sprintf(buff, "BIGINT(%d)", len);
  828. answer->append(buff, (uint) strlen(buff));
  829. if (min_arg >= 0)
  830. answer->append(STRING_WITH_LEN(" UNSIGNED"));
  831. }
  832. else if (item->decimals == NOT_FIXED_DEC)
  833. {
  834. if (min_arg >= -FLT_MAX && max_arg <= FLT_MAX)
  835. answer->append(STRING_WITH_LEN("FLOAT"));
  836. else
  837. answer->append(STRING_WITH_LEN("DOUBLE"));
  838. }
  839. else
  840. {
  841. if (min_arg >= -FLT_MAX && max_arg <= FLT_MAX)
  842. sprintf(buff, "FLOAT(%d,%d)", (int) max_length - (item->decimals + 1) + max_notzero_dec_len,
  843. max_notzero_dec_len);
  844. else
  845. sprintf(buff, "DOUBLE(%d,%d)", (int) max_length - (item->decimals + 1) + max_notzero_dec_len,
  846. max_notzero_dec_len);
  847. answer->append(buff, (uint) strlen(buff));
  848. }
  849. // if item is FIELD_ITEM, it _must_be_ Field_num in this class
  850. if (item->type() == Item::FIELD_ITEM &&
  851. // a single number shouldn't be zerofill
  852. (max_length - (item->decimals + 1)) != 1 &&
  853. ((Field_num*) ((Item_field*) item)->field)->zerofill)
  854. answer->append(STRING_WITH_LEN(" ZEROFILL"));
  855. } // field_real::get_opt_type
  856. void field_longlong::get_opt_type(String *answer,
  857. ha_rows total_rows __attribute__((unused)))
  858. {
  859. char buff[MAX_FIELD_WIDTH];
  860. if (min_arg >= -128 && max_arg <= (min_arg >= 0 ? 255 : 127))
  861. sprintf(buff, "TINYINT(%d)", (int) max_length);
  862. else if (min_arg >= INT_MIN16 && max_arg <= (min_arg >= 0 ?
  863. UINT_MAX16 : INT_MAX16))
  864. sprintf(buff, "SMALLINT(%d)", (int) max_length);
  865. else if (min_arg >= INT_MIN24 && max_arg <= (min_arg >= 0 ?
  866. UINT_MAX24 : INT_MAX24))
  867. sprintf(buff, "MEDIUMINT(%d)", (int) max_length);
  868. else if (min_arg >= INT_MIN32 && max_arg <= (min_arg >= 0 ?
  869. UINT_MAX32 : INT_MAX32))
  870. sprintf(buff, "INT(%d)", (int) max_length);
  871. else
  872. sprintf(buff, "BIGINT(%d)", (int) max_length);
  873. answer->append(buff, (uint) strlen(buff));
  874. if (min_arg >= 0)
  875. answer->append(STRING_WITH_LEN(" UNSIGNED"));
  876. // if item is FIELD_ITEM, it _must_be_ Field_num in this class
  877. if ((item->type() == Item::FIELD_ITEM) &&
  878. // a single number shouldn't be zerofill
  879. max_length != 1 &&
  880. ((Field_num*) ((Item_field*) item)->field)->zerofill)
  881. answer->append(STRING_WITH_LEN(" ZEROFILL"));
  882. } // field_longlong::get_opt_type
  883. void field_ulonglong::get_opt_type(String *answer,
  884. ha_rows total_rows __attribute__((unused)))
  885. {
  886. char buff[MAX_FIELD_WIDTH];
  887. if (max_arg < 256)
  888. sprintf(buff, "TINYINT(%d) UNSIGNED", (int) max_length);
  889. else if (max_arg <= ((2 * INT_MAX16) + 1))
  890. sprintf(buff, "SMALLINT(%d) UNSIGNED", (int) max_length);
  891. else if (max_arg <= ((2 * INT_MAX24) + 1))
  892. sprintf(buff, "MEDIUMINT(%d) UNSIGNED", (int) max_length);
  893. else if (max_arg < (((ulonglong) 1) << 32))
  894. sprintf(buff, "INT(%d) UNSIGNED", (int) max_length);
  895. else
  896. sprintf(buff, "BIGINT(%d) UNSIGNED", (int) max_length);
  897. // if item is FIELD_ITEM, it _must_be_ Field_num in this class
  898. answer->append(buff, (uint) strlen(buff));
  899. if (item->type() == Item::FIELD_ITEM &&
  900. // a single number shouldn't be zerofill
  901. max_length != 1 &&
  902. ((Field_num*) ((Item_field*) item)->field)->zerofill)
  903. answer->append(STRING_WITH_LEN(" ZEROFILL"));
  904. } //field_ulonglong::get_opt_type
  905. void field_decimal::get_opt_type(String *answer,
  906. ha_rows total_rows __attribute__((unused)))
  907. {
  908. my_decimal zero;
  909. char buff[MAX_FIELD_WIDTH];
  910. uint length;
  911. my_decimal_set_zero(&zero);
  912. my_bool is_unsigned= (my_decimal_cmp(&zero, &min_arg) >= 0);
  913. length= my_snprintf(buff, sizeof(buff), "DECIMAL(%d, %d)",
  914. (int) (max_length - (item->decimals ? 1 : 0)),
  915. item->decimals);
  916. if (is_unsigned)
  917. length= (uint) (strmov(buff+length, " UNSIGNED")- buff);
  918. answer->append(buff, length);
  919. }
  920. String *field_decimal::get_min_arg(String *str)
  921. {
  922. my_decimal2string(E_DEC_FATAL_ERROR, &min_arg, 0, 0, '0', str);
  923. return str;
  924. }
  925. String *field_decimal::get_max_arg(String *str)
  926. {
  927. my_decimal2string(E_DEC_FATAL_ERROR, &max_arg, 0, 0, '0', str);
  928. return str;
  929. }
  930. String *field_decimal::avg(String *s, ha_rows rows)
  931. {
  932. if (!(rows - nulls))
  933. {
  934. s->set_real((double) 0.0, 1,my_thd_charset);
  935. return s;
  936. }
  937. my_decimal num, avg_val, rounded_avg;
  938. int prec_increment= current_thd->variables.div_precincrement;
  939. int2my_decimal(E_DEC_FATAL_ERROR, rows - nulls, FALSE, &num);
  940. my_decimal_div(E_DEC_FATAL_ERROR, &avg_val, sum+cur_sum, &num, prec_increment);
  941. /* TODO remove this after decimal_div returns proper frac */
  942. my_decimal_round(E_DEC_FATAL_ERROR, &avg_val,
  943. min(sum[cur_sum].frac + prec_increment, DECIMAL_MAX_SCALE),
  944. FALSE,&rounded_avg);
  945. my_decimal2string(E_DEC_FATAL_ERROR, &rounded_avg, 0, 0, '0', s);
  946. return s;
  947. }
  948. String *field_decimal::std(String *s, ha_rows rows)
  949. {
  950. if (!(rows - nulls))
  951. {
  952. s->set_real((double) 0.0, 1,my_thd_charset);
  953. return s;
  954. }
  955. my_decimal num, tmp, sum2, sum2d;
  956. double std_sqr;
  957. int prec_increment= current_thd->variables.div_precincrement;
  958. int2my_decimal(E_DEC_FATAL_ERROR, rows - nulls, FALSE, &num);
  959. my_decimal_mul(E_DEC_FATAL_ERROR, &sum2, sum+cur_sum, sum+cur_sum);
  960. my_decimal_div(E_DEC_FATAL_ERROR, &tmp, &sum2, &num, prec_increment);
  961. my_decimal_sub(E_DEC_FATAL_ERROR, &sum2, sum_sqr+cur_sum, &tmp);
  962. my_decimal_div(E_DEC_FATAL_ERROR, &tmp, &sum2, &num, prec_increment);
  963. my_decimal2double(E_DEC_FATAL_ERROR, &tmp, &std_sqr);
  964. s->set_real(((double) std_sqr <= 0.0 ? 0.0 : sqrt(std_sqr)),
  965. min(item->decimals + prec_increment, NOT_FIXED_DEC), my_thd_charset);
  966. return s;
  967. }
  968. int collect_string(String *element,
  969. element_count count __attribute__((unused)),
  970. TREE_INFO *info)
  971. {
  972. if (info->found)
  973. info->str->append(',');
  974. else
  975. info->found = 1;
  976. info->str->append('\'');
  977. if (append_escaped(info->str, element))
  978. return 1;
  979. info->str->append('\'');
  980. return 0;
  981. } // collect_string
  982. int collect_real(double *element, element_count count __attribute__((unused)),
  983. TREE_INFO *info)
  984. {
  985. char buff[MAX_FIELD_WIDTH];
  986. String s(buff, sizeof(buff),current_thd->charset());
  987. if (info->found)
  988. info->str->append(',');
  989. else
  990. info->found = 1;
  991. info->str->append('\'');
  992. s.set_real(*element, info->item->decimals, current_thd->charset());
  993. info->str->append(s);
  994. info->str->append('\'');
  995. return 0;
  996. } // collect_real
  997. int collect_decimal(uchar *element, element_count count,
  998. TREE_INFO *info)
  999. {
  1000. char buff[DECIMAL_MAX_STR_LENGTH];
  1001. String s(buff, sizeof(buff),&my_charset_bin);
  1002. if (info->found)
  1003. info->str->append(',');
  1004. else
  1005. info->found = 1;
  1006. my_decimal dec;
  1007. binary2my_decimal(E_DEC_FATAL_ERROR, element, &dec,
  1008. info->item->max_length, info->item->decimals);
  1009. info->str->append('\'');
  1010. my_decimal2string(E_DEC_FATAL_ERROR, &dec, 0, 0, '0', &s);
  1011. info->str->append(s);
  1012. info->str->append('\'');
  1013. return 0;
  1014. }
  1015. int collect_longlong(longlong *element,
  1016. element_count count __attribute__((unused)),
  1017. TREE_INFO *info)
  1018. {
  1019. char buff[MAX_FIELD_WIDTH];
  1020. String s(buff, sizeof(buff),&my_charset_bin);
  1021. if (info->found)
  1022. info->str->append(',');
  1023. else
  1024. info->found = 1;
  1025. info->str->append('\'');
  1026. s.set(*element, current_thd->charset());
  1027. info->str->append(s);
  1028. info->str->append('\'');
  1029. return 0;
  1030. } // collect_longlong
  1031. int collect_ulonglong(ulonglong *element,
  1032. element_count count __attribute__((unused)),
  1033. TREE_INFO *info)
  1034. {
  1035. char buff[MAX_FIELD_WIDTH];
  1036. String s(buff, sizeof(buff),&my_charset_bin);
  1037. if (info->found)
  1038. info->str->append(',');
  1039. else
  1040. info->found = 1;
  1041. info->str->append('\'');
  1042. s.set(*element, current_thd->charset());
  1043. info->str->append(s);
  1044. info->str->append('\'');
  1045. return 0;
  1046. } // collect_ulonglong
  1047. bool analyse::change_columns(List<Item> &field_list)
  1048. {
  1049. field_list.empty();
  1050. func_items[0] = new Item_proc_string("Field_name", 255);
  1051. func_items[1] = new Item_proc_string("Min_value", 255);
  1052. func_items[1]->maybe_null = 1;
  1053. func_items[2] = new Item_proc_string("Max_value", 255);
  1054. func_items[2]->maybe_null = 1;
  1055. func_items[3] = new Item_proc_int("Min_length");
  1056. func_items[4] = new Item_proc_int("Max_length");
  1057. func_items[5] = new Item_proc_int("Empties_or_zeros");
  1058. func_items[6] = new Item_proc_int("Nulls");
  1059. func_items[7] = new Item_proc_string("Avg_value_or_avg_length", 255);
  1060. func_items[8] = new Item_proc_string("Std", 255);
  1061. func_items[8]->maybe_null = 1;
  1062. func_items[9] = new Item_proc_string("Optimal_fieldtype",
  1063. max(64, output_str_length));
  1064. for (uint i = 0; i < array_elements(func_items); i++)
  1065. field_list.push_back(func_items[i]);
  1066. result_fields = field_list;
  1067. return 0;
  1068. } // analyse::change_columns
  1069. int compare_double(const double *s, const double *t)
  1070. {
  1071. return ((*s < *t) ? -1 : *s > *t ? 1 : 0);
  1072. } /* compare_double */
  1073. int compare_longlong(const longlong *s, const longlong *t)
  1074. {
  1075. return ((*s < *t) ? -1 : *s > *t ? 1 : 0);
  1076. } /* compare_longlong */
  1077. int compare_ulonglong(const ulonglong *s, const ulonglong *t)
  1078. {
  1079. return ((*s < *t) ? -1 : *s > *t ? 1 : 0);
  1080. } /* compare_ulonglong */
  1081. uint check_ulonglong(const char *str, uint length)
  1082. {
  1083. const char *long_str = "2147483647", *ulonglong_str = "18446744073709551615";
  1084. const uint long_len = 10, ulonglong_len = 20;
  1085. while (*str == '0' && length)
  1086. {
  1087. str++; length--;
  1088. }
  1089. if (length < long_len)
  1090. return NUM;
  1091. uint smaller, bigger;
  1092. const char *cmp;
  1093. if (length == long_len)
  1094. {
  1095. cmp = long_str;
  1096. smaller = NUM;
  1097. bigger = LONG_NUM;
  1098. }
  1099. else if (length > ulonglong_len)
  1100. return DECIMAL_NUM;
  1101. else
  1102. {
  1103. cmp = ulonglong_str;
  1104. smaller = LONG_NUM;
  1105. bigger = DECIMAL_NUM;
  1106. }
  1107. while (*cmp && *cmp++ == *str++) ;
  1108. return ((uchar) str[-1] <= (uchar) cmp[-1]) ? smaller : bigger;
  1109. } /* check_ulonlong */
  1110. /*
  1111. Quote special characters in a string.
  1112. SYNOPSIS
  1113. append_escaped(to_str, from_str)
  1114. to_str (in) A pointer to a String.
  1115. from_str (to) A pointer to an allocated string
  1116. DESCRIPTION
  1117. append_escaped() takes a String type variable, where it appends
  1118. escaped the second argument. Only characters that require escaping
  1119. will be escaped.
  1120. RETURN VALUES
  1121. 0 Success
  1122. 1 Out of memory
  1123. */
  1124. bool append_escaped(String *to_str, String *from_str)
  1125. {
  1126. char *from, *end, c;
  1127. if (to_str->realloc(to_str->length() + from_str->length()))
  1128. return 1;
  1129. from= (char*) from_str->ptr();
  1130. end= from + from_str->length();
  1131. for (; from < end; from++)
  1132. {
  1133. c= *from;
  1134. switch (c) {
  1135. case '\0':
  1136. c= '0';
  1137. break;
  1138. case '\032':
  1139. c= 'Z';
  1140. break;
  1141. case '\\':
  1142. case '\'':
  1143. break;
  1144. default:
  1145. goto normal_character;
  1146. }
  1147. if (to_str->append('\\'))
  1148. return 1;
  1149. normal_character:
  1150. if (to_str->append(c))
  1151. return 1;
  1152. }
  1153. return 0;
  1154. }