1 #ifndef ITEM_SUM_INCLUDED 2 #define ITEM_SUM_INCLUDED 3 4 /* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved. reserved. 5 reserved. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License, version 2.0, 9 as published by the Free Software Foundation. 10 11 This program is also distributed with certain software (including 12 but not limited to OpenSSL) that is licensed under separate terms, 13 as designated in a particular file or component or in included license 14 documentation. The authors of MySQL hereby grant you an additional 15 permission to link the program and your derivative works with the 16 separately licensed software that they have included with MySQL. 17 18 This program is distributed in the hope that it will be useful, 19 but WITHOUT ANY WARRANTY; without even the implied warranty of 20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 GNU General Public License, version 2.0, for more details. 22 23 You should have received a copy of the GNU General Public License 24 along with this program; if not, write to the Free Software 25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ 26 27 28 /* classes for sum functions */ 29 30 #include <my_tree.h> 31 #include "sql_udf.h" /* udf_handler */ 32 33 class Item_sum; 34 class Aggregator_distinct; 35 class Aggregator_simple; 36 37 /** 38 The abstract base class for the Aggregator_* classes. 39 It implements the data collection functions (setup/add/clear) 40 as either pass-through to the real functionality or 41 as collectors into an Unique (for distinct) structure. 42 43 Note that update_field/reset_field are not in that 44 class, because they're simply not called when 45 GROUP BY/DISTINCT can be handled with help of index on grouped 46 fields (quick_group = 0); 47 */ 48 49 class Aggregator : public Sql_alloc 50 { 51 friend class Item_sum; 52 friend class Item_sum_sum; 53 friend class Item_sum_count; 54 friend class Item_sum_avg; 55 56 /* 57 All members are protected as this class is not usable outside of an 58 Item_sum descendant. 59 */ 60 protected: 61 /* the aggregate function class to act on */ 62 Item_sum *item_sum; 63 64 public: Aggregator(Item_sum * arg)65 Aggregator (Item_sum *arg): item_sum(arg) {} ~Aggregator()66 virtual ~Aggregator () {} /* Keep gcc happy */ 67 68 enum Aggregator_type { SIMPLE_AGGREGATOR, DISTINCT_AGGREGATOR }; 69 virtual Aggregator_type Aggrtype() = 0; 70 71 /** 72 Called before adding the first row. 73 Allocates and sets up the internal aggregation structures used, 74 e.g. the Unique instance used to calculate distinct. 75 */ 76 virtual bool setup(THD *) = 0; 77 78 /** 79 Called when we need to wipe out all the data from the aggregator : 80 all the values acumulated and all the state. 81 Cleans up the internal structures and resets them to their initial state. 82 */ 83 virtual void clear() = 0; 84 85 /** 86 Called when there's a new value to be aggregated. 87 Updates the internal state of the aggregator to reflect the new value. 88 */ 89 virtual bool add() = 0; 90 91 /** 92 Called when there are no more data and the final value is to be retrieved. 93 Finalises the state of the aggregator, so the final result can be retrieved. 94 */ 95 virtual void endup() = 0; 96 97 /** Decimal value of being-aggregated argument */ 98 virtual my_decimal *arg_val_decimal(my_decimal * value) = 0; 99 /** Floating point value of being-aggregated argument */ 100 virtual double arg_val_real() = 0; 101 /** 102 NULLness of being-aggregated argument. 103 104 @param use_null_value Optimization: to determine if the argument is NULL 105 we must, in the general case, call is_null() on it, which itself might 106 call val_*() on it, which might be costly. If you just have called 107 arg_val*(), you can pass use_null_value=true; this way, arg_is_null() 108 might avoid is_null() and instead do a cheap read of the Item's null_value 109 (updated by arg_val*()). 110 */ 111 virtual bool arg_is_null(bool use_null_value) = 0; 112 }; 113 114 115 class st_select_lex; 116 117 /** 118 Class Item_sum is the base class used for special expressions that SQL calls 119 'set functions'. These expressions are formed with the help of aggregate 120 functions such as SUM, MAX, GROUP_CONCAT etc. 121 122 GENERAL NOTES 123 124 A set function cannot be used in certain positions where expressions are 125 accepted. There are some quite explicable restrictions for the usage of 126 set functions. 127 128 In the query: 129 SELECT AVG(b) FROM t1 WHERE SUM(b) > 20 GROUP by a 130 the usage of the set function AVG(b) is legal, while the usage of SUM(b) 131 is illegal. A WHERE condition must contain expressions that can be 132 evaluated for each row of the table. Yet the expression SUM(b) can be 133 evaluated only for each group of rows with the same value of column a. 134 In the query: 135 SELECT AVG(b) FROM t1 WHERE c > 30 GROUP BY a HAVING SUM(b) > 20 136 both set function expressions AVG(b) and SUM(b) are legal. 137 138 We can say that in a query without nested selects an occurrence of a 139 set function in an expression of the SELECT list or/and in the HAVING 140 clause is legal, while in the WHERE clause it's illegal. 141 142 The general rule to detect whether a set function is legal in a query with 143 nested subqueries is much more complicated. 144 145 Consider the the following query: 146 SELECT t1.a FROM t1 GROUP BY t1.a 147 HAVING t1.a > ALL (SELECT t2.c FROM t2 WHERE SUM(t1.b) < t2.c). 148 The set function SUM(b) is used here in the WHERE clause of the subquery. 149 Nevertheless it is legal since it is under the HAVING clause of the query 150 to which this function relates. The expression SUM(t1.b) is evaluated 151 for each group defined in the main query, not for groups of the subquery. 152 153 The problem of finding the query where to aggregate a particular 154 set function is not so simple as it seems to be. 155 156 In the query: 157 SELECT t1.a FROM t1 GROUP BY t1.a 158 HAVING t1.a > ALL(SELECT t2.c FROM t2 GROUP BY t2.c 159 HAVING SUM(t1.a) < t2.c) 160 the set function can be evaluated for both outer and inner selects. 161 If we evaluate SUM(t1.a) for the outer query then we get the value of t1.a 162 multiplied by the cardinality of a group in table t1. In this case 163 in each correlated subquery SUM(t1.a) is used as a constant. But we also 164 can evaluate SUM(t1.a) for the inner query. In this case t1.a will be a 165 constant for each correlated subquery and summation is performed 166 for each group of table t2. 167 (Here it makes sense to remind that the query 168 SELECT c FROM t GROUP BY a HAVING SUM(1) < a 169 is quite legal in our SQL). 170 171 So depending on what query we assign the set function to we 172 can get different result sets. 173 174 The general rule to detect the query where a set function is to be 175 evaluated can be formulated as follows. 176 Consider a set function S(E) where E is an expression with occurrences 177 of column references C1, ..., CN. Resolve these column references against 178 subqueries that contain the set function S(E). Let Q be the innermost 179 subquery of those subqueries. (It should be noted here that S(E) 180 in no way can be evaluated in the subquery embedding the subquery Q, 181 otherwise S(E) would refer to at least one unbound column reference) 182 If S(E) is used in a construct of Q where set functions are allowed then 183 we evaluate S(E) in Q. 184 Otherwise we look for a innermost subquery containing S(E) of those where 185 usage of S(E) is allowed. 186 187 Let's demonstrate how this rule is applied to the following queries. 188 189 1. SELECT t1.a FROM t1 GROUP BY t1.a 190 HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b 191 HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c 192 HAVING SUM(t1.a+t2.b) < t3.c)) 193 For this query the set function SUM(t1.a+t2.b) depends on t1.a and t2.b 194 with t1.a defined in the outermost query, and t2.b defined for its 195 subquery. The set function is in the HAVING clause of the subquery and can 196 be evaluated in this subquery. 197 198 2. SELECT t1.a FROM t1 GROUP BY t1.a 199 HAVING t1.a > ALL(SELECT t2.b FROM t2 200 WHERE t2.b > ALL (SELECT t3.c FROM t3 GROUP BY t3.c 201 HAVING SUM(t1.a+t2.b) < t3.c)) 202 Here the set function SUM(t1.a+t2.b)is in the WHERE clause of the second 203 subquery - the most upper subquery where t1.a and t2.b are defined. 204 If we evaluate the function in this subquery we violate the context rules. 205 So we evaluate the function in the third subquery (over table t3) where it 206 is used under the HAVING clause. 207 208 3. SELECT t1.a FROM t1 GROUP BY t1.a 209 HAVING t1.a > ALL(SELECT t2.b FROM t2 210 WHERE t2.b > ALL (SELECT t3.c FROM t3 211 WHERE SUM(t1.a+t2.b) < t3.c)) 212 In this query evaluation of SUM(t1.a+t2.b) is not legal neither in the second 213 nor in the third subqueries. So this query is invalid. 214 215 Mostly set functions cannot be nested. In the query 216 SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20 217 the expression SUM(b) is not acceptable, though it is under a HAVING clause. 218 Yet it is acceptable in the query: 219 SELECT t.1 FROM t1 GROUP BY t1.a HAVING SUM(t1.b) > 20. 220 221 An argument of a set function does not have to be a reference to a table 222 column as we saw it in examples above. This can be a more complex expression 223 SELECT t1.a FROM t1 GROUP BY t1.a HAVING SUM(t1.b+1) > 20. 224 The expression SUM(t1.b+1) has a very clear semantics in this context: 225 we sum up the values of t1.b+1 where t1.b varies for all values within a 226 group of rows that contain the same t1.a value. 227 228 A set function for an outer query yields a constant within a subquery. So 229 the semantics of the query 230 SELECT t1.a FROM t1 GROUP BY t1.a 231 HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 232 HAVING AVG(t2.c+SUM(t1.b)) > 20) 233 is still clear. For a group of the rows with the same t1.a values we 234 calculate the value of SUM(t1.b). This value 's' is substituted in the 235 the subquery: 236 SELECT t2.c FROM t2 GROUP BY t2.c HAVING AVG(t2.c+s) 237 than returns some result set. 238 239 By the same reason the following query with a subquery 240 SELECT t1.a FROM t1 GROUP BY t1.a 241 HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c 242 HAVING AVG(SUM(t1.b)) > 20) 243 is also acceptable. 244 245 IMPLEMENTATION NOTES 246 247 Three methods were added to the class to check the constraints specified 248 in the previous section. These methods utilize several new members. 249 250 The field 'nest_level' contains the number of the level for the subquery 251 containing the set function. The main SELECT is of level 0, its subqueries 252 are of levels 1, the subqueries of the latter are of level 2 and so on. 253 254 The field 'aggr_level' is to contain the nest level of the subquery 255 where the set function is aggregated. 256 257 The field 'max_arg_level' is for the maximun of the nest levels of the 258 unbound column references occurred in the set function. A column reference 259 is unbound within a set function if it is not bound by any subquery 260 used as a subexpression in this function. A column reference is bound by 261 a subquery if it is a reference to the column by which the aggregation 262 of some set function that is used in the subquery is calculated. 263 For the set function used in the query 264 SELECT t1.a FROM t1 GROUP BY t1.a 265 HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b 266 HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c 267 HAVING SUM(t1.a+t2.b) < t3.c)) 268 the value of max_arg_level is equal to 1 since t1.a is bound in the main 269 query, and t2.b is bound by the first subquery whose nest level is 1. 270 Obviously a set function cannot be aggregated in the subquery whose 271 nest level is less than max_arg_level. (Yet it can be aggregated in the 272 subqueries whose nest level is greater than max_arg_level.) 273 In the query 274 SELECT t.a FROM t1 HAVING AVG(t1.a+(SELECT MIN(t2.c) FROM t2)) 275 the value of the max_arg_level for the AVG set function is 0 since 276 the reference t2.c is bound in the subquery. 277 278 The field 'max_sum_func_level' is to contain the maximum of the 279 nest levels of the set functions that are used as subexpressions of 280 the arguments of the given set function, but not aggregated in any 281 subquery within this set function. A nested set function s1 can be 282 used within set function s0 only if s1.max_sum_func_level < 283 s0.max_sum_func_level. Set function s1 is considered as nested 284 for set function s0 if s1 is not calculated in any subquery 285 within s0. 286 287 A set function that is used as a subexpression in an argument of another 288 set function refers to the latter via the field 'in_sum_func'. 289 290 The condition imposed on the usage of set functions are checked when 291 we traverse query subexpressions with the help of the recursive method 292 fix_fields. When we apply this method to an object of the class 293 Item_sum, first, on the descent, we call the method init_sum_func_check 294 that initialize members used at checking. Then, on the ascent, we 295 call the method check_sum_func that validates the set function usage 296 and reports an error if it is illegal. 297 The method register_sum_func serves to link the items for the set functions 298 that are aggregated in the embedding (sub)queries. Circular chains of such 299 functions are attached to the corresponding st_select_lex structures 300 through the field inner_sum_func_list. 301 302 Exploiting the fact that the members mentioned above are used in one 303 recursive function we could have allocated them on the thread stack. 304 Yet we don't do it now. 305 306 We assume that the nesting level of subquries does not exceed 127. 307 TODO: to catch queries where the limit is exceeded to make the 308 code clean here. 309 310 */ 311 312 class Item_sum :public Item_result_field 313 { 314 friend class Aggregator_distinct; 315 friend class Aggregator_simple; 316 317 protected: 318 /** 319 Aggregator class instance. Not set initially. Allocated only after 320 it is determined if the incoming data are already distinct. 321 */ 322 Aggregator *aggr; 323 324 private: 325 /** 326 Used in making ROLLUP. Set for the ROLLUP copies of the original 327 Item_sum and passed to create_tmp_field() to cause it to work 328 over the temp table buffer that is referenced by 329 Item_result_field::result_field. 330 */ 331 bool force_copy_fields; 332 333 /** 334 Indicates how the aggregate function was specified by the parser : 335 1 if it was written as AGGREGATE(DISTINCT), 336 0 if it was AGGREGATE() 337 */ 338 bool with_distinct; 339 340 public: 341 has_force_copy_fields()342 bool has_force_copy_fields() const { return force_copy_fields; } has_with_distinct()343 bool has_with_distinct() const { return with_distinct; } 344 345 enum Sumfunctype 346 { COUNT_FUNC, COUNT_DISTINCT_FUNC, SUM_FUNC, SUM_DISTINCT_FUNC, AVG_FUNC, 347 AVG_DISTINCT_FUNC, MIN_FUNC, MAX_FUNC, STD_FUNC, 348 VARIANCE_FUNC, SUM_BIT_FUNC, UDF_SUM_FUNC, GROUP_CONCAT_FUNC 349 }; 350 351 Item **ref_by; /* pointer to a ref to the object used to register it */ 352 Item_sum *next; /* next in the circular chain of registered objects */ 353 Item_sum *in_sum_func; /* embedding set function if any */ 354 st_select_lex * aggr_sel; /* select where the function is aggregated */ 355 int8 nest_level; /* number of the nesting level of the set function */ 356 int8 aggr_level; /* nesting level of the aggregating subquery */ 357 int8 max_arg_level; /* max level of unbound column references */ 358 int8 max_sum_func_level;/* max level of aggregation for embedded functions */ 359 bool quick_group; /* If incremental update of fields */ 360 /* 361 This list is used by the check for mixing non aggregated fields and 362 sum functions in the ONLY_FULL_GROUP_BY_MODE. We save all outer fields 363 directly or indirectly used under this function it as it's unclear 364 at the moment of fixing outer field whether it's aggregated or not. 365 */ 366 List<Item_field> outer_fields; 367 368 protected: 369 uint arg_count; 370 Item **args, *tmp_args[2]; 371 table_map used_tables_cache; 372 bool forced_const; 373 static ulonglong ram_limitation(THD *thd); 374 375 public: 376 377 void mark_as_sum_func(); Item_sum()378 Item_sum() :next(NULL), quick_group(1), arg_count(0), forced_const(FALSE) 379 { 380 mark_as_sum_func(); 381 init_aggregator(); 382 } Item_sum(Item * a)383 Item_sum(Item *a) :next(NULL), quick_group(1), arg_count(1), args(tmp_args), 384 forced_const(FALSE) 385 { 386 args[0]=a; 387 mark_as_sum_func(); 388 init_aggregator(); 389 } Item_sum(Item * a,Item * b)390 Item_sum( Item *a, Item *b ) :next(NULL), quick_group(1), arg_count(2), args(tmp_args), 391 forced_const(FALSE) 392 { 393 args[0]=a; args[1]=b; 394 mark_as_sum_func(); 395 init_aggregator(); 396 } 397 Item_sum(List<Item> &list); 398 //Copy constructor, need to perform subselects with temporary tables 399 Item_sum(THD *thd, Item_sum *item); type()400 enum Type type() const { return SUM_FUNC_ITEM; } 401 virtual enum Sumfunctype sum_func () const=0; 402 /** 403 Resets the aggregate value to its default and aggregates the current 404 value of its attribute(s). 405 */ reset_and_add()406 inline bool reset_and_add() 407 { 408 aggregator_clear(); 409 return aggregator_add(); 410 }; 411 412 /* 413 Called when new group is started and results are being saved in 414 a temporary table. Similarly to reset_and_add() it resets the 415 value to its default and aggregates the value of its 416 attribute(s), but must also store it in result_field. 417 This set of methods (result_item(), reset_field, update_field()) of 418 Item_sum is used only if quick_group is not null. Otherwise 419 copy_or_same() is used to obtain a copy of this item. 420 */ 421 virtual void reset_field()=0; 422 /* 423 Called for each new value in the group, when temporary table is in use. 424 Similar to add(), but uses temporary table field to obtain current value, 425 Updated value is then saved in the field. 426 */ 427 virtual void update_field()=0; keep_field_type(void)428 virtual bool keep_field_type(void) const { return 0; } fix_length_and_dec()429 virtual void fix_length_and_dec() { maybe_null=1; null_value=1; } result_item(Field * field)430 virtual Item *result_item(Field *field) 431 { return new Item_field(field); } used_tables()432 table_map used_tables() const { return used_tables_cache; } 433 void update_used_tables (); is_null()434 bool is_null() { return null_value; } make_const()435 void make_const () 436 { 437 used_tables_cache= 0; 438 forced_const= TRUE; 439 } const_item()440 virtual bool const_item() const { return forced_const; } const_during_execution()441 virtual bool const_during_execution() const { return false; } 442 virtual void print(String *str, enum_query_type query_type); 443 void fix_num_length_and_dec(); 444 445 /** 446 Mark an aggregate as having no rows. 447 448 This function is called by the execution engine to assign 'NO ROWS 449 FOUND' value to an aggregate item, when the underlying result set 450 has no rows. Such value, in a general case, may be different from 451 the default value of the item after 'clear()': e.g. a numeric item 452 may be initialized to 0 by clear() and to NULL by 453 no_rows_in_result(). 454 */ no_rows_in_result()455 virtual void no_rows_in_result() 456 { 457 set_aggregator(with_distinct ? 458 Aggregator::DISTINCT_AGGREGATOR : 459 Aggregator::SIMPLE_AGGREGATOR); 460 aggregator_clear(); 461 } make_unique()462 virtual void make_unique() { force_copy_fields= TRUE; } 463 Item *get_tmp_table_item(THD *thd); 464 virtual Field *create_tmp_field(bool group, TABLE *table); 465 bool walk(Item_processor processor, bool walk_subquery, uchar *argument); 466 virtual bool clean_up_after_removal(uchar *arg); 467 bool init_sum_func_check(THD *thd); 468 bool check_sum_func(THD *thd, Item **ref); 469 bool register_sum_func(THD *thd, Item **ref); depended_from()470 st_select_lex *depended_from() 471 { return (nest_level == aggr_level ? 0 : aggr_sel); } 472 get_arg(uint i)473 Item *get_arg(uint i) { return args[i]; } 474 Item *set_arg(uint i, THD *thd, Item *new_val); get_arg_count()475 uint get_arg_count() const { return arg_count; } 476 477 /* Initialization of distinct related members */ init_aggregator()478 void init_aggregator() 479 { 480 aggr= NULL; 481 with_distinct= FALSE; 482 force_copy_fields= FALSE; 483 } 484 485 /** 486 Called to initialize the aggregator. 487 */ 488 aggregator_setup(THD * thd)489 inline bool aggregator_setup(THD *thd) { return aggr->setup(thd); }; 490 491 /** 492 Called to cleanup the aggregator. 493 */ 494 aggregator_clear()495 inline void aggregator_clear() { aggr->clear(); } 496 497 /** 498 Called to add value to the aggregator. 499 */ 500 aggregator_add()501 inline bool aggregator_add() { return aggr->add(); }; 502 503 /* stores the declared DISTINCT flag (from the parser) */ set_distinct(bool distinct)504 void set_distinct(bool distinct) 505 { 506 with_distinct= distinct; 507 quick_group= with_distinct ? 0 : 1; 508 } 509 510 /* 511 Set the type of aggregation : DISTINCT or not. 512 513 May be called multiple times. 514 */ 515 516 int set_aggregator(Aggregator::Aggregator_type aggregator); 517 518 virtual void clear()= 0; 519 virtual bool add()= 0; setup(THD * thd)520 virtual bool setup(THD *thd) { return false; } 521 522 virtual void cleanup(); 523 }; 524 525 526 class Unique; 527 528 529 /** 530 The distinct aggregator. 531 Implements AGGFN (DISTINCT ..) 532 Collects all the data into an Unique (similarly to what Item_sum_distinct 533 does currently) and then (if applicable) iterates over the list of 534 unique values and pumps them back into its object 535 */ 536 537 class Aggregator_distinct : public Aggregator 538 { 539 friend class Item_sum_sum; 540 541 /* 542 flag to prevent consecutive runs of endup(). Normally in endup there are 543 expensive calculations (like walking the distinct tree for example) 544 which we must do only once if there are no data changes. 545 We can re-use the data for the second and subsequent val_xxx() calls. 546 endup_done set to TRUE also means that the calculated values for 547 the aggregate functions are correct and don't need recalculation. 548 */ 549 bool endup_done; 550 551 /* 552 Used depending on the type of the aggregate function and the presence of 553 blob columns in it: 554 - For COUNT(DISTINCT) and no blob fields this points to a real temporary 555 table. It's used as a hash table. 556 - For AVG/SUM(DISTINCT) or COUNT(DISTINCT) with blob fields only the 557 in-memory data structure of a temporary table is constructed. 558 It's used by the Field classes to transform data into row format. 559 */ 560 TABLE *table; 561 562 /* 563 An array of field lengths on row allocated and used only for 564 COUNT(DISTINCT) with multiple columns and no blobs. Used in 565 Aggregator_distinct::composite_key_cmp (called from Unique to compare 566 nodes 567 */ 568 uint32 *field_lengths; 569 570 /* 571 Used in conjunction with 'table' to support the access to Field classes 572 for COUNT(DISTINCT). Needed by copy_fields()/copy_funcs(). 573 */ 574 TMP_TABLE_PARAM *tmp_table_param; 575 576 /* 577 If there are no blobs in the COUNT(DISTINCT) arguments, we can use a tree, 578 which is faster than heap table. In that case, we still use the table 579 to help get things set up, but we insert nothing in it. 580 For AVG/SUM(DISTINCT) we always use this tree (as it takes a single 581 argument) to get the distinct rows. 582 */ 583 Unique *tree; 584 585 /* 586 The length of the temp table row. Must be a member of the class as it 587 gets passed down to simple_raw_key_cmp () as a compare function argument 588 to Unique. simple_raw_key_cmp () is used as a fast comparison function 589 when the entire row can be binary compared. 590 */ 591 uint tree_key_length; 592 593 enum Const_distinct{ 594 NOT_CONST= 0, 595 /** 596 Set to true if the result is known to be always NULL. 597 If set deactivates creation and usage of the temporary table (in the 598 'table' member) and the Unique instance (in the 'tree' member) as well as 599 the calculation of the final value on the first call to 600 Item_[sum|avg|count]::val_xxx(). 601 */ 602 CONST_NULL, 603 /** 604 Set to true if count distinct is on only const items. Distinct on a const 605 value will always be the constant itself. And count distinct of the same 606 would always be 1. Similar to CONST_NULL, it avoids creation of temporary 607 table and the Unique instance. 608 */ 609 CONST_NOT_NULL 610 } const_distinct; 611 612 /** 613 When feeding back the data in endup() from Unique/temp table back to 614 Item_sum::add() methods we must read the data from Unique (and not 615 recalculate the functions that are given as arguments to the aggregate 616 function. 617 This flag is to tell the arg_*() methods to take the data from the Unique 618 instead of calling the relevant val_..() method. 619 */ 620 bool use_distinct_values; 621 622 public: Aggregator_distinct(Item_sum * sum)623 Aggregator_distinct (Item_sum *sum) : 624 Aggregator(sum), table(NULL), tmp_table_param(NULL), tree(NULL), 625 const_distinct(NOT_CONST), use_distinct_values(false) {} 626 virtual ~Aggregator_distinct (); Aggrtype()627 Aggregator_type Aggrtype() { return DISTINCT_AGGREGATOR; } 628 629 bool setup(THD *); 630 void clear(); 631 bool add(); 632 void endup(); 633 virtual my_decimal *arg_val_decimal(my_decimal * value); 634 virtual double arg_val_real(); 635 virtual bool arg_is_null(bool use_null_value); 636 637 bool unique_walk_function(void *element); 638 static int composite_key_cmp(void* arg, uchar* key1, uchar* key2); 639 }; 640 641 642 /** 643 The pass-through aggregator. 644 Implements AGGFN (DISTINCT ..) by knowing it gets distinct data on input. 645 So it just pumps them back to the Item_sum descendant class. 646 */ 647 class Aggregator_simple : public Aggregator 648 { 649 public: 650 Aggregator_simple(Item_sum * sum)651 Aggregator_simple (Item_sum *sum) : 652 Aggregator(sum) {} Aggrtype()653 Aggregator_type Aggrtype() { return Aggregator::SIMPLE_AGGREGATOR; } 654 setup(THD * thd)655 bool setup(THD * thd) { return item_sum->setup(thd); } clear()656 void clear() { item_sum->clear(); } add()657 bool add() { return item_sum->add(); } endup()658 void endup() {}; 659 virtual my_decimal *arg_val_decimal(my_decimal * value); 660 virtual double arg_val_real(); 661 virtual bool arg_is_null(bool use_null_value); 662 }; 663 664 665 class Item_sum_num :public Item_sum 666 { 667 protected: 668 /* 669 val_xxx() functions may be called several times during the execution of a 670 query. Derived classes that require extensive calculation in val_xxx() 671 maintain cache of aggregate value. This variable governs the validity of 672 that cache. 673 */ 674 bool is_evaluated; 675 public: Item_sum_num()676 Item_sum_num() :Item_sum(),is_evaluated(FALSE) {} Item_sum_num(Item * item_par)677 Item_sum_num(Item *item_par) 678 :Item_sum(item_par), is_evaluated(FALSE) {} Item_sum_num(Item * a,Item * b)679 Item_sum_num(Item *a, Item* b) :Item_sum(a,b),is_evaluated(FALSE) {} Item_sum_num(List<Item> & list)680 Item_sum_num(List<Item> &list) 681 :Item_sum(list), is_evaluated(FALSE) {} Item_sum_num(THD * thd,Item_sum_num * item)682 Item_sum_num(THD *thd, Item_sum_num *item) 683 :Item_sum(thd, item),is_evaluated(item->is_evaluated) {} 684 bool fix_fields(THD *, Item **); val_int()685 longlong val_int() 686 { 687 DBUG_ASSERT(fixed == 1); 688 return (longlong) rint(val_real()); /* Real as default */ 689 } 690 String *val_str(String*str); 691 my_decimal *val_decimal(my_decimal *); get_date(MYSQL_TIME * ltime,uint fuzzydate)692 bool get_date(MYSQL_TIME *ltime, uint fuzzydate) 693 { 694 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */ 695 } get_time(MYSQL_TIME * ltime)696 bool get_time(MYSQL_TIME *ltime) 697 { 698 return get_time_from_numeric(ltime); /* Decimal or real */ 699 } 700 void reset_field(); 701 }; 702 703 704 class Item_sum_int :public Item_sum_num 705 { 706 public: Item_sum_int(Item * item_par)707 Item_sum_int(Item *item_par) :Item_sum_num(item_par) {} Item_sum_int(List<Item> & list)708 Item_sum_int(List<Item> &list) :Item_sum_num(list) {} Item_sum_int(THD * thd,Item_sum_int * item)709 Item_sum_int(THD *thd, Item_sum_int *item) :Item_sum_num(thd, item) {} val_real()710 double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); } 711 String *val_str(String*str); 712 my_decimal *val_decimal(my_decimal *); get_date(MYSQL_TIME * ltime,uint fuzzydate)713 bool get_date(MYSQL_TIME *ltime, uint fuzzydate) 714 { 715 return get_date_from_int(ltime, fuzzydate); 716 } get_time(MYSQL_TIME * ltime)717 bool get_time(MYSQL_TIME *ltime) 718 { 719 return get_time_from_int(ltime); 720 } result_type()721 enum Item_result result_type () const { return INT_RESULT; } fix_length_and_dec()722 void fix_length_and_dec() 723 { decimals=0; max_length=21; maybe_null=null_value=0; } 724 }; 725 726 727 class Item_sum_sum :public Item_sum_num 728 { 729 protected: 730 Item_result hybrid_type; 731 double sum; 732 my_decimal dec_buffs[2]; 733 uint curr_dec_buff; 734 void fix_length_and_dec(); 735 736 public: Item_sum_sum(Item * item_par,bool distinct)737 Item_sum_sum(Item *item_par, bool distinct) :Item_sum_num(item_par) 738 { 739 set_distinct(distinct); 740 } 741 Item_sum_sum(THD *thd, Item_sum_sum *item); sum_func()742 enum Sumfunctype sum_func () const 743 { 744 return has_with_distinct() ? SUM_DISTINCT_FUNC : SUM_FUNC; 745 } 746 void clear(); 747 bool add(); 748 double val_real(); 749 longlong val_int(); 750 String *val_str(String*str); 751 my_decimal *val_decimal(my_decimal *); result_type()752 enum Item_result result_type () const { return hybrid_type; } 753 void reset_field(); 754 void update_field(); no_rows_in_result()755 void no_rows_in_result() {} func_name()756 const char *func_name() const 757 { 758 return has_with_distinct() ? "sum(distinct " : "sum("; 759 } 760 Item *copy_or_same(THD* thd); 761 }; 762 763 764 class Item_sum_count :public Item_sum_int 765 { 766 longlong count; 767 768 friend class Aggregator_distinct; 769 770 void clear(); 771 bool add(); 772 void cleanup(); 773 774 public: Item_sum_count(Item * item_par)775 Item_sum_count(Item *item_par) 776 :Item_sum_int(item_par),count(0) 777 {} 778 779 /** 780 Constructs an instance for COUNT(DISTINCT) 781 782 @param list a list of the arguments to the aggregate function 783 784 This constructor is called by the parser only for COUNT (DISTINCT). 785 */ 786 Item_sum_count(List<Item> & list)787 Item_sum_count(List<Item> &list) 788 :Item_sum_int(list),count(0) 789 { 790 set_distinct(TRUE); 791 } Item_sum_count(THD * thd,Item_sum_count * item)792 Item_sum_count(THD *thd, Item_sum_count *item) 793 :Item_sum_int(thd, item), count(item->count) 794 {} sum_func()795 enum Sumfunctype sum_func () const 796 { 797 return has_with_distinct() ? COUNT_DISTINCT_FUNC : COUNT_FUNC; 798 } no_rows_in_result()799 void no_rows_in_result() { count=0; } make_const(longlong count_arg)800 void make_const(longlong count_arg) 801 { 802 count=count_arg; 803 Item_sum::make_const(); 804 } 805 longlong val_int(); 806 void reset_field(); 807 void update_field(); func_name()808 const char *func_name() const 809 { 810 return has_with_distinct() ? "count(distinct " : "count("; 811 } 812 Item *copy_or_same(THD* thd); 813 }; 814 815 816 /* Item to get the value of a stored sum function */ 817 818 class Item_sum_avg; 819 820 /** 821 Common abstract class for: 822 Item_avg_field 823 Item_variance_field 824 */ 825 class Item_sum_num_field: public Item_result_field 826 { 827 protected: 828 Field *field; 829 Item_result hybrid_type; 830 public: val_int()831 longlong val_int() 832 { 833 /* can't be fix_fields()ed */ 834 return (longlong) rint(val_real()); 835 } get_date(MYSQL_TIME * ltime,uint fuzzydate)836 bool get_date(MYSQL_TIME *ltime, uint fuzzydate) 837 { 838 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */ 839 } get_time(MYSQL_TIME * ltime)840 bool get_time(MYSQL_TIME *ltime) 841 { 842 return get_time_from_numeric(ltime); /* Decimal or real */ 843 } field_type()844 enum_field_types field_type() const 845 { 846 return hybrid_type == DECIMAL_RESULT ? 847 MYSQL_TYPE_NEWDECIMAL : MYSQL_TYPE_DOUBLE; 848 } result_type()849 enum Item_result result_type () const { return hybrid_type; } is_null()850 bool is_null() { update_null_value(); return null_value; } 851 }; 852 853 854 class Item_avg_field :public Item_sum_num_field 855 { 856 public: 857 uint f_precision, f_scale, dec_bin_size; 858 uint prec_increment; 859 Item_avg_field(Item_result res_type, Item_sum_avg *item); type()860 enum Type type() const { return FIELD_AVG_ITEM; } 861 double val_real(); 862 my_decimal *val_decimal(my_decimal *); 863 String *val_str(String*); fix_length_and_dec()864 void fix_length_and_dec() {} func_name()865 const char *func_name() const { DBUG_ASSERT(0); return "avg_field"; } 866 }; 867 868 869 class Item_sum_avg :public Item_sum_sum 870 { 871 public: 872 ulonglong count; 873 uint prec_increment; 874 uint f_precision, f_scale, dec_bin_size; Item_sum_avg(Item * item_par,bool distinct)875 Item_sum_avg(Item *item_par, bool distinct) 876 :Item_sum_sum(item_par, distinct), count(0) 877 {} Item_sum_avg(THD * thd,Item_sum_avg * item)878 Item_sum_avg(THD *thd, Item_sum_avg *item) 879 :Item_sum_sum(thd, item), count(item->count), 880 prec_increment(item->prec_increment) {} 881 882 void fix_length_and_dec(); sum_func()883 enum Sumfunctype sum_func () const 884 { 885 return has_with_distinct() ? AVG_DISTINCT_FUNC : AVG_FUNC; 886 } 887 void clear(); 888 bool add(); 889 double val_real(); 890 // In SPs we might force the "wrong" type with select into a declare variable val_int()891 longlong val_int() { return (longlong) rint(val_real()); } 892 my_decimal *val_decimal(my_decimal *); 893 String *val_str(String *str); 894 void reset_field(); 895 void update_field(); result_item(Field * field)896 Item *result_item(Field *field) 897 { return new Item_avg_field(hybrid_type, this); } no_rows_in_result()898 void no_rows_in_result() {} func_name()899 const char *func_name() const 900 { 901 return has_with_distinct() ? "avg(distinct " : "avg("; 902 } 903 Item *copy_or_same(THD* thd); 904 Field *create_tmp_field(bool group, TABLE *table); cleanup()905 void cleanup() 906 { 907 count= 0; 908 Item_sum_sum::cleanup(); 909 } 910 }; 911 912 class Item_sum_variance; 913 914 class Item_variance_field :public Item_sum_num_field 915 { 916 protected: 917 uint f_precision0, f_scale0; 918 uint f_precision1, f_scale1; 919 uint dec_bin_size0, dec_bin_size1; 920 uint sample; 921 uint prec_increment; 922 public: 923 Item_variance_field(Item_sum_variance *item); type()924 enum Type type() const {return FIELD_VARIANCE_ITEM; } 925 double val_real(); val_str(String * str)926 String *val_str(String *str) 927 { return val_string_from_real(str); } val_decimal(my_decimal * dec_buf)928 my_decimal *val_decimal(my_decimal *dec_buf) 929 { return val_decimal_from_real(dec_buf); } fix_length_and_dec()930 void fix_length_and_dec() {} func_name()931 const char *func_name() const { DBUG_ASSERT(0); return "variance_field"; } 932 }; 933 934 935 /* 936 variance(a) = 937 938 = sum (ai - avg(a))^2 / count(a) ) 939 = sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a) 940 = (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) = 941 = (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) = 942 = (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2 )/count(a) = 943 = (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a) )/count(a) = 944 = (sum(ai^2) - sum(a)^2/count(a))/count(a) 945 946 But, this falls prey to catastrophic cancellation. Instead, use the recurrence formulas 947 948 M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline 949 S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline 950 for 2 <= k <= n newline 951 ital variance = S_{n} / (n-1) 952 953 */ 954 955 class Item_sum_variance : public Item_sum_num 956 { 957 void fix_length_and_dec(); 958 959 public: 960 Item_result hybrid_type; 961 int cur_dec; 962 double recurrence_m, recurrence_s; /* Used in recurrence relation. */ 963 ulonglong count; 964 uint f_precision0, f_scale0; 965 uint f_precision1, f_scale1; 966 uint dec_bin_size0, dec_bin_size1; 967 uint sample; 968 uint prec_increment; 969 Item_sum_variance(Item * item_par,uint sample_arg)970 Item_sum_variance(Item *item_par, uint sample_arg) :Item_sum_num(item_par), 971 hybrid_type(REAL_RESULT), count(0), sample(sample_arg) 972 {} 973 Item_sum_variance(THD *thd, Item_sum_variance *item); sum_func()974 enum Sumfunctype sum_func () const { return VARIANCE_FUNC; } 975 void clear(); 976 bool add(); 977 double val_real(); 978 my_decimal *val_decimal(my_decimal *); 979 void reset_field(); 980 void update_field(); result_item(Field * field)981 Item *result_item(Field *field) 982 { return new Item_variance_field(this); } no_rows_in_result()983 void no_rows_in_result() {} func_name()984 const char *func_name() const 985 { return sample ? "var_samp(" : "variance("; } 986 Item *copy_or_same(THD* thd); 987 Field *create_tmp_field(bool group, TABLE *table); result_type()988 enum Item_result result_type () const { return REAL_RESULT; } cleanup()989 void cleanup() 990 { 991 count= 0; 992 Item_sum_num::cleanup(); 993 } 994 }; 995 996 class Item_sum_std; 997 998 class Item_std_field :public Item_variance_field 999 { 1000 public: 1001 Item_std_field(Item_sum_std *item); type()1002 enum Type type() const { return FIELD_STD_ITEM; } 1003 double val_real(); 1004 my_decimal *val_decimal(my_decimal *); result_type()1005 enum Item_result result_type () const { return REAL_RESULT; } field_type()1006 enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;} func_name()1007 const char *func_name() const { DBUG_ASSERT(0); return "std_field"; } 1008 }; 1009 1010 /* 1011 standard_deviation(a) = sqrt(variance(a)) 1012 */ 1013 1014 class Item_sum_std :public Item_sum_variance 1015 { 1016 public: Item_sum_std(Item * item_par,uint sample_arg)1017 Item_sum_std(Item *item_par, uint sample_arg) 1018 :Item_sum_variance(item_par, sample_arg) {} Item_sum_std(THD * thd,Item_sum_std * item)1019 Item_sum_std(THD *thd, Item_sum_std *item) 1020 :Item_sum_variance(thd, item) 1021 {} sum_func()1022 enum Sumfunctype sum_func () const { return STD_FUNC; } 1023 double val_real(); result_item(Field * field)1024 Item *result_item(Field *field) 1025 { return new Item_std_field(this); } func_name()1026 const char *func_name() const { return "std("; } 1027 Item *copy_or_same(THD* thd); result_type()1028 enum Item_result result_type () const { return REAL_RESULT; } field_type()1029 enum_field_types field_type() const { return MYSQL_TYPE_DOUBLE;} 1030 }; 1031 1032 // This class is a string or number function depending on num_func 1033 class Arg_comparator; 1034 class Item_cache; 1035 class Item_sum_hybrid :public Item_sum 1036 { 1037 protected: 1038 Item_cache *value, *arg_cache; 1039 Arg_comparator *cmp; 1040 Item_result hybrid_type; 1041 enum_field_types hybrid_field_type; 1042 int cmp_sign; 1043 bool was_values; // Set if we have found at least one row (for max/min only) 1044 1045 public: Item_sum_hybrid(Item * item_par,int sign)1046 Item_sum_hybrid(Item *item_par,int sign) 1047 :Item_sum(item_par), value(0), arg_cache(0), cmp(0), 1048 hybrid_type(INT_RESULT), hybrid_field_type(MYSQL_TYPE_LONGLONG), 1049 cmp_sign(sign), was_values(TRUE) 1050 { collation.set(&my_charset_bin); } Item_sum_hybrid(THD * thd,Item_sum_hybrid * item)1051 Item_sum_hybrid(THD *thd, Item_sum_hybrid *item) 1052 :Item_sum(thd, item), value(item->value), arg_cache(0), 1053 hybrid_type(item->hybrid_type), hybrid_field_type(item->hybrid_field_type), 1054 cmp_sign(item->cmp_sign), was_values(item->was_values) 1055 { } 1056 bool fix_fields(THD *, Item **); 1057 void setup_hybrid(Item *item, Item *value_arg); 1058 void clear(); 1059 double val_real(); 1060 longlong val_int(); 1061 longlong val_time_temporal(); 1062 longlong val_date_temporal(); 1063 my_decimal *val_decimal(my_decimal *); 1064 bool get_date(MYSQL_TIME *ltime, uint fuzzydate); 1065 bool get_time(MYSQL_TIME *ltime); 1066 void reset_field(); 1067 String *val_str(String *); keep_field_type(void)1068 bool keep_field_type(void) const { return 1; } result_type()1069 enum Item_result result_type () const { return hybrid_type; } field_type()1070 enum enum_field_types field_type() const { return hybrid_field_type; } 1071 void update_field(); 1072 void min_max_update_str_field(); 1073 void min_max_update_temporal_field(); 1074 void min_max_update_real_field(); 1075 void min_max_update_int_field(); 1076 void min_max_update_decimal_field(); 1077 void cleanup(); any_value()1078 bool any_value() { return was_values; } 1079 void no_rows_in_result(); 1080 Field *create_tmp_field(bool group, TABLE *table); 1081 }; 1082 1083 1084 class Item_sum_min :public Item_sum_hybrid 1085 { 1086 public: Item_sum_min(Item * item_par)1087 Item_sum_min(Item *item_par) :Item_sum_hybrid(item_par,1) {} Item_sum_min(THD * thd,Item_sum_min * item)1088 Item_sum_min(THD *thd, Item_sum_min *item) :Item_sum_hybrid(thd, item) {} sum_func()1089 enum Sumfunctype sum_func () const {return MIN_FUNC;} 1090 1091 bool add(); func_name()1092 const char *func_name() const { return "min("; } 1093 Item *copy_or_same(THD* thd); 1094 }; 1095 1096 1097 class Item_sum_max :public Item_sum_hybrid 1098 { 1099 public: Item_sum_max(Item * item_par)1100 Item_sum_max(Item *item_par) :Item_sum_hybrid(item_par,-1) {} Item_sum_max(THD * thd,Item_sum_max * item)1101 Item_sum_max(THD *thd, Item_sum_max *item) :Item_sum_hybrid(thd, item) {} sum_func()1102 enum Sumfunctype sum_func () const {return MAX_FUNC;} 1103 1104 bool add(); func_name()1105 const char *func_name() const { return "max("; } 1106 Item *copy_or_same(THD* thd); 1107 }; 1108 1109 1110 class Item_sum_bit :public Item_sum_int 1111 { 1112 protected: 1113 ulonglong reset_bits,bits; 1114 1115 public: Item_sum_bit(Item * item_par,ulonglong reset_arg)1116 Item_sum_bit(Item *item_par,ulonglong reset_arg) 1117 :Item_sum_int(item_par),reset_bits(reset_arg),bits(reset_arg) {} Item_sum_bit(THD * thd,Item_sum_bit * item)1118 Item_sum_bit(THD *thd, Item_sum_bit *item): 1119 Item_sum_int(thd, item), reset_bits(item->reset_bits), bits(item->bits) {} sum_func()1120 enum Sumfunctype sum_func () const {return SUM_BIT_FUNC;} 1121 void clear(); 1122 longlong val_int(); 1123 void reset_field(); 1124 void update_field(); fix_length_and_dec()1125 void fix_length_and_dec() 1126 { decimals= 0; max_length=21; unsigned_flag= 1; maybe_null= null_value= 0; } cleanup()1127 void cleanup() 1128 { 1129 bits= reset_bits; 1130 Item_sum_int::cleanup(); 1131 } 1132 }; 1133 1134 1135 class Item_sum_or :public Item_sum_bit 1136 { 1137 public: Item_sum_or(Item * item_par)1138 Item_sum_or(Item *item_par) :Item_sum_bit(item_par,LL(0)) {} Item_sum_or(THD * thd,Item_sum_or * item)1139 Item_sum_or(THD *thd, Item_sum_or *item) :Item_sum_bit(thd, item) {} 1140 bool add(); func_name()1141 const char *func_name() const { return "bit_or("; } 1142 Item *copy_or_same(THD* thd); 1143 }; 1144 1145 1146 class Item_sum_and :public Item_sum_bit 1147 { 1148 public: Item_sum_and(Item * item_par)1149 Item_sum_and(Item *item_par) :Item_sum_bit(item_par, ULONGLONG_MAX) {} Item_sum_and(THD * thd,Item_sum_and * item)1150 Item_sum_and(THD *thd, Item_sum_and *item) :Item_sum_bit(thd, item) {} 1151 bool add(); func_name()1152 const char *func_name() const { return "bit_and("; } 1153 Item *copy_or_same(THD* thd); 1154 }; 1155 1156 class Item_sum_xor :public Item_sum_bit 1157 { 1158 public: Item_sum_xor(Item * item_par)1159 Item_sum_xor(Item *item_par) :Item_sum_bit(item_par,LL(0)) {} Item_sum_xor(THD * thd,Item_sum_xor * item)1160 Item_sum_xor(THD *thd, Item_sum_xor *item) :Item_sum_bit(thd, item) {} 1161 bool add(); func_name()1162 const char *func_name() const { return "bit_xor("; } 1163 Item *copy_or_same(THD* thd); 1164 }; 1165 1166 1167 /* 1168 User defined aggregates 1169 */ 1170 1171 #ifdef HAVE_DLOPEN 1172 1173 class Item_udf_sum : public Item_sum 1174 { 1175 protected: 1176 udf_handler udf; 1177 1178 public: Item_udf_sum(udf_func * udf_arg)1179 Item_udf_sum(udf_func *udf_arg) 1180 :Item_sum(), udf(udf_arg) 1181 { quick_group=0; } Item_udf_sum(udf_func * udf_arg,List<Item> & list)1182 Item_udf_sum(udf_func *udf_arg, List<Item> &list) 1183 :Item_sum(list), udf(udf_arg) 1184 { quick_group=0;} Item_udf_sum(THD * thd,Item_udf_sum * item)1185 Item_udf_sum(THD *thd, Item_udf_sum *item) 1186 :Item_sum(thd, item), udf(item->udf) 1187 { udf.not_original= TRUE; } func_name()1188 const char *func_name() const { return udf.name(); } fix_fields(THD * thd,Item ** ref)1189 bool fix_fields(THD *thd, Item **ref) 1190 { 1191 DBUG_ASSERT(fixed == 0); 1192 1193 if (init_sum_func_check(thd)) 1194 return TRUE; 1195 1196 fixed= 1; 1197 if (udf.fix_fields(thd, this, this->arg_count, this->args)) 1198 return TRUE; 1199 1200 return check_sum_func(thd, ref); 1201 } sum_func()1202 enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; } have_field_update(void)1203 virtual bool have_field_update(void) const { return 0; } 1204 1205 void clear(); 1206 bool add(); reset_field()1207 void reset_field() {}; update_field()1208 void update_field() {}; 1209 void cleanup(); 1210 virtual void print(String *str, enum_query_type query_type); 1211 }; 1212 1213 1214 class Item_sum_udf_float :public Item_udf_sum 1215 { 1216 public: Item_sum_udf_float(udf_func * udf_arg)1217 Item_sum_udf_float(udf_func *udf_arg) 1218 :Item_udf_sum(udf_arg) {} Item_sum_udf_float(udf_func * udf_arg,List<Item> & list)1219 Item_sum_udf_float(udf_func *udf_arg, List<Item> &list) 1220 :Item_udf_sum(udf_arg, list) {} Item_sum_udf_float(THD * thd,Item_sum_udf_float * item)1221 Item_sum_udf_float(THD *thd, Item_sum_udf_float *item) 1222 :Item_udf_sum(thd, item) {} val_int()1223 longlong val_int() 1224 { 1225 DBUG_ASSERT(fixed == 1); 1226 return (longlong) rint(Item_sum_udf_float::val_real()); 1227 } 1228 double val_real(); 1229 String *val_str(String*str); 1230 my_decimal *val_decimal(my_decimal *); get_date(MYSQL_TIME * ltime,uint fuzzydate)1231 bool get_date(MYSQL_TIME *ltime, uint fuzzydate) 1232 { 1233 return get_date_from_real(ltime, fuzzydate); 1234 } get_time(MYSQL_TIME * ltime)1235 bool get_time(MYSQL_TIME *ltime) 1236 { 1237 return get_time_from_real(ltime); 1238 } fix_length_and_dec()1239 void fix_length_and_dec() { fix_num_length_and_dec(); } 1240 Item *copy_or_same(THD* thd); 1241 }; 1242 1243 1244 class Item_sum_udf_int :public Item_udf_sum 1245 { 1246 public: Item_sum_udf_int(udf_func * udf_arg)1247 Item_sum_udf_int(udf_func *udf_arg) 1248 :Item_udf_sum(udf_arg) {} Item_sum_udf_int(udf_func * udf_arg,List<Item> & list)1249 Item_sum_udf_int(udf_func *udf_arg, List<Item> &list) 1250 :Item_udf_sum(udf_arg, list) {} Item_sum_udf_int(THD * thd,Item_sum_udf_int * item)1251 Item_sum_udf_int(THD *thd, Item_sum_udf_int *item) 1252 :Item_udf_sum(thd, item) {} 1253 longlong val_int(); val_real()1254 double val_real() 1255 { DBUG_ASSERT(fixed == 1); return (double) Item_sum_udf_int::val_int(); } 1256 String *val_str(String*str); 1257 my_decimal *val_decimal(my_decimal *); get_date(MYSQL_TIME * ltime,uint fuzzydate)1258 bool get_date(MYSQL_TIME *ltime, uint fuzzydate) 1259 { 1260 return get_date_from_int(ltime, fuzzydate); 1261 } get_time(MYSQL_TIME * ltime)1262 bool get_time(MYSQL_TIME *ltime) 1263 { 1264 return get_time_from_int(ltime); 1265 } result_type()1266 enum Item_result result_type () const { return INT_RESULT; } fix_length_and_dec()1267 void fix_length_and_dec() { decimals=0; max_length=21; } 1268 Item *copy_or_same(THD* thd); 1269 }; 1270 1271 1272 class Item_sum_udf_str :public Item_udf_sum 1273 { 1274 public: Item_sum_udf_str(udf_func * udf_arg)1275 Item_sum_udf_str(udf_func *udf_arg) 1276 :Item_udf_sum(udf_arg) {} Item_sum_udf_str(udf_func * udf_arg,List<Item> & list)1277 Item_sum_udf_str(udf_func *udf_arg, List<Item> &list) 1278 :Item_udf_sum(udf_arg,list) {} Item_sum_udf_str(THD * thd,Item_sum_udf_str * item)1279 Item_sum_udf_str(THD *thd, Item_sum_udf_str *item) 1280 :Item_udf_sum(thd, item) {} 1281 String *val_str(String *); val_real()1282 double val_real() 1283 { 1284 int err_not_used; 1285 char *end_not_used; 1286 String *res; 1287 res=val_str(&str_value); 1288 return res ? my_strntod(res->charset(),(char*) res->ptr(),res->length(), 1289 &end_not_used, &err_not_used) : 0.0; 1290 } val_int()1291 longlong val_int() 1292 { 1293 int err_not_used; 1294 char *end; 1295 String *res; 1296 const CHARSET_INFO *cs; 1297 1298 if (!(res= val_str(&str_value))) 1299 return 0; /* Null value */ 1300 cs= res->charset(); 1301 end= (char*) res->ptr()+res->length(); 1302 return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used); 1303 } 1304 my_decimal *val_decimal(my_decimal *dec); get_date(MYSQL_TIME * ltime,uint fuzzydate)1305 bool get_date(MYSQL_TIME *ltime, uint fuzzydate) 1306 { 1307 return get_date_from_string(ltime, fuzzydate); 1308 } get_time(MYSQL_TIME * ltime)1309 bool get_time(MYSQL_TIME *ltime) 1310 { 1311 return get_time_from_string(ltime); 1312 } result_type()1313 enum Item_result result_type () const { return STRING_RESULT; } 1314 void fix_length_and_dec(); 1315 Item *copy_or_same(THD* thd); 1316 }; 1317 1318 1319 class Item_sum_udf_decimal :public Item_udf_sum 1320 { 1321 public: Item_sum_udf_decimal(udf_func * udf_arg)1322 Item_sum_udf_decimal(udf_func *udf_arg) 1323 :Item_udf_sum(udf_arg) {} Item_sum_udf_decimal(udf_func * udf_arg,List<Item> & list)1324 Item_sum_udf_decimal(udf_func *udf_arg, List<Item> &list) 1325 :Item_udf_sum(udf_arg, list) {} Item_sum_udf_decimal(THD * thd,Item_sum_udf_decimal * item)1326 Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item) 1327 :Item_udf_sum(thd, item) {} 1328 String *val_str(String *); 1329 double val_real(); 1330 longlong val_int(); 1331 my_decimal *val_decimal(my_decimal *); get_date(MYSQL_TIME * ltime,uint fuzzydate)1332 bool get_date(MYSQL_TIME *ltime, uint fuzzydate) 1333 { 1334 return get_date_from_decimal(ltime, fuzzydate); 1335 } get_time(MYSQL_TIME * ltime)1336 bool get_time(MYSQL_TIME *ltime) 1337 { 1338 return get_time_from_decimal(ltime); 1339 } result_type()1340 enum Item_result result_type () const { return DECIMAL_RESULT; } fix_length_and_dec()1341 void fix_length_and_dec() { fix_num_length_and_dec(); } 1342 Item *copy_or_same(THD* thd); 1343 }; 1344 1345 #else /* Dummy functions to get sql_yacc.cc compiled */ 1346 1347 class Item_sum_udf_float :public Item_sum_num 1348 { 1349 public: Item_sum_udf_float(udf_func * udf_arg)1350 Item_sum_udf_float(udf_func *udf_arg) 1351 :Item_sum_num() {} Item_sum_udf_float(udf_func * udf_arg,List<Item> & list)1352 Item_sum_udf_float(udf_func *udf_arg, List<Item> &list) :Item_sum_num() {} Item_sum_udf_float(THD * thd,Item_sum_udf_float * item)1353 Item_sum_udf_float(THD *thd, Item_sum_udf_float *item) 1354 :Item_sum_num(thd, item) {} sum_func()1355 enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; } val_real()1356 double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; } clear()1357 void clear() {} add()1358 bool add() { return 0; } update_field()1359 void update_field() {} 1360 }; 1361 1362 1363 class Item_sum_udf_int :public Item_sum_num 1364 { 1365 public: Item_sum_udf_int(udf_func * udf_arg)1366 Item_sum_udf_int(udf_func *udf_arg) 1367 :Item_sum_num() {} Item_sum_udf_int(udf_func * udf_arg,List<Item> & list)1368 Item_sum_udf_int(udf_func *udf_arg, List<Item> &list) :Item_sum_num() {} Item_sum_udf_int(THD * thd,Item_sum_udf_int * item)1369 Item_sum_udf_int(THD *thd, Item_sum_udf_int *item) 1370 :Item_sum_num(thd, item) {} sum_func()1371 enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; } val_int()1372 longlong val_int() { DBUG_ASSERT(fixed == 1); return 0; } val_real()1373 double val_real() { DBUG_ASSERT(fixed == 1); return 0; } clear()1374 void clear() {} add()1375 bool add() { return 0; } update_field()1376 void update_field() {} 1377 }; 1378 1379 1380 class Item_sum_udf_decimal :public Item_sum_num 1381 { 1382 public: Item_sum_udf_decimal(udf_func * udf_arg)1383 Item_sum_udf_decimal(udf_func *udf_arg) 1384 :Item_sum_num() {} Item_sum_udf_decimal(udf_func * udf_arg,List<Item> & list)1385 Item_sum_udf_decimal(udf_func *udf_arg, List<Item> &list) 1386 :Item_sum_num() {} Item_sum_udf_decimal(THD * thd,Item_sum_udf_float * item)1387 Item_sum_udf_decimal(THD *thd, Item_sum_udf_float *item) 1388 :Item_sum_num(thd, item) {} sum_func()1389 enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; } val_real()1390 double val_real() { DBUG_ASSERT(fixed == 1); return 0.0; } val_decimal(my_decimal *)1391 my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed == 1); return 0; } clear()1392 void clear() {} add()1393 bool add() { return 0; } update_field()1394 void update_field() {} 1395 }; 1396 1397 1398 class Item_sum_udf_str :public Item_sum_num 1399 { 1400 public: Item_sum_udf_str(udf_func * udf_arg)1401 Item_sum_udf_str(udf_func *udf_arg) 1402 :Item_sum_num() {} Item_sum_udf_str(udf_func * udf_arg,List<Item> & list)1403 Item_sum_udf_str(udf_func *udf_arg, List<Item> &list) 1404 :Item_sum_num() {} Item_sum_udf_str(THD * thd,Item_sum_udf_str * item)1405 Item_sum_udf_str(THD *thd, Item_sum_udf_str *item) 1406 :Item_sum_num(thd, item) {} val_str(String *)1407 String *val_str(String *) 1408 { DBUG_ASSERT(fixed == 1); null_value=1; return 0; } val_real()1409 double val_real() { DBUG_ASSERT(fixed == 1); null_value=1; return 0.0; } val_int()1410 longlong val_int() { DBUG_ASSERT(fixed == 1); null_value=1; return 0; } result_type()1411 enum Item_result result_type () const { return STRING_RESULT; } fix_length_and_dec()1412 void fix_length_and_dec() { maybe_null=1; max_length=0; } sum_func()1413 enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; } clear()1414 void clear() {} add()1415 bool add() { return 0; } update_field()1416 void update_field() {} 1417 }; 1418 1419 #endif /* HAVE_DLOPEN */ 1420 1421 C_MODE_START 1422 int group_concat_key_cmp_with_distinct(const void* arg, const void* key1, 1423 const void* key2); 1424 int group_concat_key_cmp_with_order(const void* arg, const void* key1, 1425 const void* key2); 1426 int dump_leaf_key(void* key_arg, 1427 element_count count MY_ATTRIBUTE((unused)), 1428 void* item_arg); 1429 C_MODE_END 1430 1431 class Item_func_group_concat : public Item_sum 1432 { 1433 TMP_TABLE_PARAM *tmp_table_param; 1434 String result; 1435 String *separator; 1436 TREE tree_base; 1437 TREE *tree; 1438 1439 /** 1440 If DISTINCT is used with this GROUP_CONCAT, this member is used to filter 1441 out duplicates. 1442 @see Item_func_group_concat::setup 1443 @see Item_func_group_concat::add 1444 @see Item_func_group_concat::clear 1445 */ 1446 Unique *unique_filter; 1447 TABLE *table; 1448 ORDER **order; 1449 Name_resolution_context *context; 1450 /** The number of ORDER BY items. */ 1451 uint arg_count_order; 1452 /** The number of selected items, aka the expr list. */ 1453 uint arg_count_field; 1454 uint row_count; 1455 bool distinct; 1456 bool warning_for_row; 1457 bool always_null; 1458 bool force_copy_fields; 1459 bool no_appended; 1460 /* 1461 Following is 0 normal object and pointer to original one for copy 1462 (to correctly free resources) 1463 */ 1464 Item_func_group_concat *original; 1465 1466 friend int group_concat_key_cmp_with_distinct(const void* arg, 1467 const void* key1, 1468 const void* key2); 1469 friend int group_concat_key_cmp_with_order(const void* arg, 1470 const void* key1, 1471 const void* key2); 1472 friend int dump_leaf_key(void* key_arg, 1473 element_count count MY_ATTRIBUTE((unused)), 1474 void* item_arg); 1475 1476 public: 1477 Item_func_group_concat(Name_resolution_context *context_arg, 1478 bool is_distinct, List<Item> *is_select, 1479 const SQL_I_List<ORDER> &is_order, String *is_separator); 1480 1481 Item_func_group_concat(THD *thd, Item_func_group_concat *item); 1482 ~Item_func_group_concat(); 1483 void cleanup(); 1484 sum_func()1485 enum Sumfunctype sum_func () const {return GROUP_CONCAT_FUNC;} func_name()1486 const char *func_name() const { return "group_concat"; } result_type()1487 virtual Item_result result_type () const { return STRING_RESULT; } 1488 virtual Field *make_string_field(TABLE *table); field_type()1489 enum_field_types field_type() const 1490 { 1491 if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB ) 1492 return MYSQL_TYPE_BLOB; 1493 else 1494 return MYSQL_TYPE_VARCHAR; 1495 } 1496 void clear(); 1497 bool add(); reset_field()1498 void reset_field() { DBUG_ASSERT(0); } // not used update_field()1499 void update_field() { DBUG_ASSERT(0); } // not used 1500 bool fix_fields(THD *,Item **); 1501 bool setup(THD *thd); 1502 void make_unique(); val_real()1503 double val_real() 1504 { 1505 String *res; res=val_str(&str_value); 1506 return res ? my_atof(res->c_ptr()) : 0.0; 1507 } val_int()1508 longlong val_int() 1509 { 1510 String *res; 1511 char *end_ptr; 1512 int error; 1513 if (!(res= val_str(&str_value))) 1514 return (longlong) 0; 1515 end_ptr= (char*) res->ptr()+ res->length(); 1516 return my_strtoll10(res->ptr(), &end_ptr, &error); 1517 } val_decimal(my_decimal * decimal_value)1518 my_decimal *val_decimal(my_decimal *decimal_value) 1519 { 1520 return val_decimal_from_string(decimal_value); 1521 } get_date(MYSQL_TIME * ltime,uint fuzzydate)1522 bool get_date(MYSQL_TIME *ltime, uint fuzzydate) 1523 { 1524 return get_date_from_string(ltime, fuzzydate); 1525 } get_time(MYSQL_TIME * ltime)1526 bool get_time(MYSQL_TIME *ltime) 1527 { 1528 return get_time_from_string(ltime); 1529 } 1530 String* val_str(String* str); 1531 Item *copy_or_same(THD* thd); no_rows_in_result()1532 void no_rows_in_result() {} 1533 virtual void print(String *str, enum_query_type query_type); change_context_processor(uchar * cntx)1534 virtual bool change_context_processor(uchar *cntx) 1535 { context= (Name_resolution_context *)cntx; return FALSE; } 1536 }; 1537 1538 #endif /* ITEM_SUM_INCLUDED */ 1539