item_sum.h 69.1 KB
Newer Older
1 2
#ifndef ITEM_SUM_INCLUDED
#define ITEM_SUM_INCLUDED
Sergei Golubchik's avatar
Sergei Golubchik committed
3
/* Copyright (c) 2000, 2013 Oracle and/or its affiliates.
4
   Copyright (c) 2008, 2020, MariaDB Corporation.
unknown's avatar
unknown committed
5

unknown's avatar
unknown committed
6 7
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
unknown's avatar
unknown committed
8
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
9

unknown's avatar
unknown committed
10 11 12 13
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
unknown's avatar
unknown committed
14

unknown's avatar
unknown committed
15 16
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
Vicențiu Ciorbaru's avatar
Vicențiu Ciorbaru committed
17
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA */
unknown's avatar
unknown committed
18 19 20 21


/* classes for sum functions */

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

26
#include <my_tree.h>
27
#include "sql_udf.h"                            /* udf_handler */
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
class Item_sum;
class Aggregator_distinct;
class Aggregator_simple;

/**
  The abstract base class for the Aggregator_* classes.
  It implements the data collection functions (setup/add/clear)
  as either pass-through to the real functionality or
  as collectors into an Unique (for distinct) structure.

  Note that update_field/reset_field are not in that
  class, because they're simply not called when
  GROUP BY/DISTINCT can be handled with help of index on grouped 
  fields (quick_group = 0);
*/

class Aggregator : public Sql_alloc
{
  friend class Item_sum;
  friend class Item_sum_sum;
  friend class Item_sum_count;
  friend class Item_sum_avg;

  /* 
    All members are protected as this class is not usable outside of an 
    Item_sum descendant.
  */
protected:
  /* the aggregate function class to act on */
  Item_sum *item_sum;

public:
61
  Aggregator (Item_sum *arg): item_sum(arg) {}
62 63
  virtual ~Aggregator () {}                   /* Keep gcc happy */

Michael Widenius's avatar
Michael Widenius committed
64
  enum Aggregator_type { SIMPLE_AGGREGATOR, DISTINCT_AGGREGATOR };
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  virtual Aggregator_type Aggrtype() = 0;

  /**
    Called before adding the first row. 
    Allocates and sets up the internal aggregation structures used, 
    e.g. the Unique instance used to calculate distinct.
  */
  virtual bool setup(THD *) = 0;

  /**
    Called when we need to wipe out all the data from the aggregator :
    all the values acumulated and all the state.
    Cleans up the internal structures and resets them to their initial state.
  */
  virtual void clear() = 0;

  /**
    Called when there's a new value to be aggregated.
    Updates the internal state of the aggregator to reflect the new value.
  */
  virtual bool add() = 0;

  /**
    Called when there are no more data and the final value is to be retrieved.
    Finalises the state of the aggregator, so the final result can be retrieved.
  */
  virtual void endup() = 0;

93 94 95 96 97
  /** Decimal value of being-aggregated argument */
  virtual my_decimal *arg_val_decimal(my_decimal * value) = 0;
  /** Floating point value of being-aggregated argument */
  virtual double arg_val_real() = 0;
  /**
98 99 100 101 102 103 104 105
    NULLness of being-aggregated argument.

    @param use_null_value Optimization: to determine if the argument is NULL
    we must, in the general case, call is_null() on it, which itself might
    call val_*() on it, which might be costly. If you just have called
    arg_val*(), you can pass use_null_value=true; this way, arg_is_null()
    might avoid is_null() and instead do a cheap read of the Item's null_value
    (updated by arg_val*()).
106
  */
107
  virtual bool arg_is_null(bool use_null_value) = 0;
108 109 110 111
};


class st_select_lex;
112
class Window_spec;
113 114

/**
unknown's avatar
unknown committed
115 116 117 118 119 120
  Class Item_sum is the base class used for special expressions that SQL calls
  'set functions'. These expressions are formed with the help of aggregate
  functions such as SUM, MAX, GROUP_CONCAT etc.

 GENERAL NOTES

unknown's avatar
unknown committed
121
  A set function cannot be used in certain positions where expressions are
unknown's avatar
unknown committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  accepted. There are some quite explicable restrictions for the usage of 
  set functions.

  In the query:
    SELECT AVG(b) FROM t1 WHERE SUM(b) > 20 GROUP by a
  the usage of the set function AVG(b) is legal, while the usage of SUM(b)
  is illegal. A WHERE condition must contain expressions that can be 
  evaluated for each row of the table. Yet the expression SUM(b) can be
  evaluated only for each group of rows with the same value of column a.
  In the query:
    SELECT AVG(b) FROM t1 WHERE c > 30 GROUP BY a HAVING SUM(b) > 20
  both set function expressions AVG(b) and SUM(b) are legal.

  We can say that in a query without nested selects an occurrence of a
  set function in an expression of the SELECT list or/and in the HAVING
  clause is legal, while in the WHERE clause it's illegal.

  The general rule to detect whether a set function is legal in a query with
  nested subqueries is much more complicated.

  Consider the the following query:
    SELECT t1.a FROM t1 GROUP BY t1.a
      HAVING t1.a > ALL (SELECT t2.c FROM t2 WHERE SUM(t1.b) < t2.c).
  The set function SUM(b) is used here in the WHERE clause of the subquery.
  Nevertheless it is legal since it is under the HAVING clause of the query
  to which this function relates. The expression SUM(t1.b) is evaluated
  for each group defined in the main query, not for groups of the subquery.

  The problem of finding the query where to aggregate a particular
unknown's avatar
unknown committed
151
  set function is not so simple as it seems to be.
unknown's avatar
unknown committed
152 153 154 155 156 157

  In the query: 
    SELECT t1.a FROM t1 GROUP BY t1.a
     HAVING t1.a > ALL(SELECT t2.c FROM t2 GROUP BY t2.c
                         HAVING SUM(t1.a) < t2.c)
  the set function can be evaluated for both outer and inner selects.
unknown's avatar
unknown committed
158
  If we evaluate SUM(t1.a) for the outer query then we get the value of t1.a
unknown's avatar
unknown committed
159 160
  multiplied by the cardinality of a group in table t1. In this case 
  in each correlated subquery SUM(t1.a) is used as a constant. But we also
unknown's avatar
unknown committed
161
  can evaluate SUM(t1.a) for the inner query. In this case t1.a will be a
unknown's avatar
unknown committed
162 163 164 165 166 167
  constant for each correlated subquery and summation is performed
  for each group of table t2.
  (Here it makes sense to remind that the query
    SELECT c FROM t GROUP BY a HAVING SUM(1) < a 
  is quite legal in our SQL).

unknown's avatar
unknown committed
168
  So depending on what query we assign the set function to we
unknown's avatar
unknown committed
169 170 171 172 173
  can get different result sets.

  The general rule to detect the query where a set function is to be
  evaluated can be formulated as follows.
  Consider a set function S(E) where E is an expression with occurrences
unknown's avatar
unknown committed
174 175 176 177 178 179
  of column references C1, ..., CN. Resolve these column references against
  subqueries that contain the set function S(E). Let Q be the innermost
  subquery of those subqueries. (It should be noted here that S(E)
  in no way can be evaluated in the subquery embedding the subquery Q,
  otherwise S(E) would refer to at least one unbound column reference)
  If S(E) is used in a construct of Q where set functions are allowed then
unknown's avatar
unknown committed
180
  we evaluate S(E) in Q.
unknown's avatar
unknown committed
181
  Otherwise we look for a innermost subquery containing S(E) of those where
unknown's avatar
unknown committed
182 183 184 185 186 187 188 189 190
  usage of S(E) is allowed.

  Let's demonstrate how this rule is applied to the following queries.

  1. SELECT t1.a FROM t1 GROUP BY t1.a
       HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
                           HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
                                                HAVING SUM(t1.a+t2.b) < t3.c))
  For this query the set function SUM(t1.a+t2.b) depends on t1.a and t2.b
unknown's avatar
unknown committed
191
  with t1.a defined in the outermost query, and t2.b defined for its
unknown's avatar
unknown committed
192 193 194 195 196 197 198 199 200 201
  subquery. The set function is in the HAVING clause of the subquery and can
  be evaluated in this subquery.

  2. SELECT t1.a FROM t1 GROUP BY t1.a
       HAVING t1.a > ALL(SELECT t2.b FROM t2
                           WHERE t2.b > ALL (SELECT t3.c FROM t3 GROUP BY t3.c
                                               HAVING SUM(t1.a+t2.b) < t3.c))
  Here the set function SUM(t1.a+t2.b)is in the WHERE clause of the second
  subquery - the most upper subquery where t1.a and t2.b are defined.
  If we evaluate the function in this subquery we violate the context rules.
unknown's avatar
unknown committed
202 203
  So we evaluate the function in the third subquery (over table t3) where it
  is used under the HAVING clause.
unknown's avatar
unknown committed
204 205 206 207 208 209 210 211 212 213 214

  3. SELECT t1.a FROM t1 GROUP BY t1.a
       HAVING t1.a > ALL(SELECT t2.b FROM t2
                           WHERE t2.b > ALL (SELECT t3.c FROM t3 
                                               WHERE SUM(t1.a+t2.b) < t3.c))
  In this query evaluation of SUM(t1.a+t2.b) is not legal neither in the second
  nor in the third subqueries. So this query is invalid.

  Mostly set functions cannot be nested. In the query
    SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20
  the expression SUM(b) is not acceptable, though it is under a HAVING clause.
unknown's avatar
unknown committed
215
  Yet it is acceptable in the query:
unknown's avatar
unknown committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229
    SELECT t.1 FROM t1 GROUP BY t1.a HAVING SUM(t1.b) > 20.

  An argument of a set function does not have to be a reference to a table
  column as we saw it in examples above. This can be a more complex expression
    SELECT t1.a FROM t1 GROUP BY t1.a HAVING SUM(t1.b+1) > 20.
  The expression SUM(t1.b+1) has a very clear semantics in this context:
  we sum up the values of t1.b+1 where t1.b varies for all values within a
  group of rows that contain the same t1.a value.

  A set function for an outer query yields a constant within a subquery. So
  the semantics of the query
    SELECT t1.a FROM t1 GROUP BY t1.a
      HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
                        HAVING AVG(t2.c+SUM(t1.b)) > 20)
unknown's avatar
unknown committed
230
  is still clear. For a group of the rows with the same t1.a values we
unknown's avatar
unknown committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244
  calculate the value of SUM(t1.b). This value 's' is substituted in the
  the subquery:
    SELECT t2.c FROM t2 GROUP BY t2.c HAVING AVG(t2.c+s)
  than returns some result set.

  By the same reason the following query with a subquery 
    SELECT t1.a FROM t1 GROUP BY t1.a
      HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
                        HAVING AVG(SUM(t1.b)) > 20)
  is also acceptable.

 IMPLEMENTATION NOTES

  Three methods were added to the class to check the constraints specified
unknown's avatar
unknown committed
245
  in the previous section. These methods utilize several new members.
unknown's avatar
unknown committed
246 247 248

  The field 'nest_level' contains the number of the level for the subquery
  containing the set function. The main SELECT is of level 0, its subqueries
unknown's avatar
unknown committed
249
  are of levels 1, the subqueries of the latter are of level 2 and so on.
unknown's avatar
unknown committed
250 251 252 253

  The field 'aggr_level' is to contain the nest level of the subquery
  where the set function is aggregated.

Ian Gilfillan's avatar
Ian Gilfillan committed
254
  The field 'max_arg_level' is for the maximum of the nest levels of the
unknown's avatar
unknown committed
255 256 257 258 259 260 261 262 263 264 265
  unbound column references occurred in the set function. A column reference
  is unbound  within a set function if it is not bound by any subquery
  used as a subexpression in this function. A column reference is bound by
  a subquery if it is a reference to the column by which the aggregation
  of some set function that is used in the subquery is calculated.
  For the set function used in the query
    SELECT t1.a FROM t1 GROUP BY t1.a
      HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
                          HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
                                              HAVING SUM(t1.a+t2.b) < t3.c))
  the value of max_arg_level is equal to 1 since t1.a is bound in the main
unknown's avatar
unknown committed
266
  query, and t2.b is bound by the first subquery whose nest level is 1.
unknown's avatar
unknown committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  Obviously a set function cannot be aggregated in the subquery whose
  nest level is less than max_arg_level. (Yet it can be aggregated in the
  subqueries whose nest level is greater than max_arg_level.)
  In the query
    SELECT t.a FROM t1 HAVING AVG(t1.a+(SELECT MIN(t2.c) FROM t2))
  the value of the max_arg_level for the AVG set function is 0 since
  the reference t2.c is bound in the subquery.

  The field 'max_sum_func_level' is to contain the maximum of the
  nest levels of the set functions that are used as subexpressions of
  the arguments of the given set function, but not aggregated in any
  subquery within this set function. A nested set function s1 can be
  used within set function s0 only if s1.max_sum_func_level <
  s0.max_sum_func_level. Set function s1 is considered as nested
  for set function s0 if s1 is not calculated in any subquery
  within s0.

unknown's avatar
unknown committed
284 285
  A set function that is used as a subexpression in an argument of another
  set function refers to the latter via the field 'in_sum_func'.
unknown's avatar
unknown committed
286 287

  The condition imposed on the usage of set functions are checked when
unknown's avatar
unknown committed
288
  we traverse query subexpressions with the help of the recursive method
unknown's avatar
unknown committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
  fix_fields. When we apply this method to an object of the class
  Item_sum, first, on the descent, we call the method init_sum_func_check
  that initialize members used at checking. Then, on the ascent, we
  call the method check_sum_func that validates the set function usage
  and reports an error if it is illegal.
  The method register_sum_func serves to link the items for the set functions
  that are aggregated in the embedding (sub)queries. Circular chains of such
  functions are attached to the corresponding st_select_lex structures
  through the field inner_sum_func_list.

  Exploiting the fact that the members mentioned above are used in one
  recursive function we could have allocated them on the thread stack.
  Yet we don't do it now.
  
  We assume that the nesting level of subquries does not exceed 127.
  TODO: to catch queries where the limit is exceeded to make the
  code clean here.  
306 307 308 309 310 311 312

  @note
  The implementation takes into account the used strategy:
  - Items resolved at optimization phase return 0 from Item_sum::used_tables().
  - Items that depend on the number of join output records, but not columns of
  any particular table (like COUNT(*)), returm 0 from Item_sum::used_tables(),
  but still return false from Item_sum::const_item().
313
*/
unknown's avatar
unknown committed
314

315
class Item_sum :public Item_func_or_sum
unknown's avatar
unknown committed
316
{
317
  friend class Aggregator_distinct;
318
  friend class Aggregator_simple;
319

320
protected:
321 322 323 324 325 326
  /**
    Aggregator class instance. Not set initially. Allocated only after
    it is determined if the incoming data are already distinct.
  */
  Aggregator *aggr;

327
private:
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
  /**
    Used in making ROLLUP. Set for the ROLLUP copies of the original
    Item_sum and passed to create_tmp_field() to cause it to work
    over the temp table buffer that is referenced by
    Item_result_field::result_field.
  */
  bool force_copy_fields;

  /**
    Indicates how the aggregate function was specified by the parser :
    1 if it was written as AGGREGATE(DISTINCT),
    0 if it was AGGREGATE()
  */
  bool with_distinct;

Igor Babaev's avatar
Igor Babaev committed
343 344 345
  /* TRUE if this is aggregate function of a window function */
  bool window_func_sum_expr_flag;

unknown's avatar
unknown committed
346
public:
347 348 349 350

  bool has_force_copy_fields() const { return force_copy_fields; }
  bool has_with_distinct()     const { return with_distinct; }

351
  enum Sumfunctype
unknown's avatar
unknown committed
352
  { COUNT_FUNC, COUNT_DISTINCT_FUNC, SUM_FUNC, SUM_DISTINCT_FUNC, AVG_FUNC,
353
    AVG_DISTINCT_FUNC, MIN_FUNC, MAX_FUNC, STD_FUNC,
354 355
    VARIANCE_FUNC, SUM_BIT_FUNC, UDF_SUM_FUNC, GROUP_CONCAT_FUNC,
    ROW_NUMBER_FUNC, RANK_FUNC, DENSE_RANK_FUNC, PERCENT_RANK_FUNC,
356
    CUME_DIST_FUNC, NTILE_FUNC, FIRST_VALUE_FUNC, LAST_VALUE_FUNC,
357
    NTH_VALUE_FUNC, LEAD_FUNC, LAG_FUNC, PERCENTILE_CONT_FUNC,
358 359
    PERCENTILE_DISC_FUNC, SP_AGGREGATE_FUNC, JSON_ARRAYAGG_FUNC,
    JSON_OBJECTAGG_FUNC
360
  };
unknown's avatar
unknown committed
361

unknown's avatar
unknown committed
362 363 364
  Item **ref_by; /* pointer to a ref to the object used to register it */
  Item_sum *next; /* next in the circular chain of registered objects  */
  Item_sum *in_sum_func;  /* embedding set function if any */ 
365
  st_select_lex * aggr_sel; /* select where the function is aggregated       */ 
unknown's avatar
unknown committed
366 367 368 369
  int8 nest_level;        /* number of the nesting level of the set function */
  int8 aggr_level;        /* nesting level of the aggregating subquery       */
  int8 max_arg_level;     /* max level of unbound column references          */
  int8 max_sum_func_level;/* max level of aggregation for embedded functions */
unknown's avatar
unknown committed
370
  bool quick_group;			/* If incremental update of fields */
371 372 373 374 375 376 377
  /*
    This list is used by the check for mixing non aggregated fields and
    sum functions in the ONLY_FULL_GROUP_BY_MODE. We save all outer fields
    directly or indirectly used under this function it as it's unclear
    at the moment of fixing outer field whether it's aggregated or not.
  */
  List<Item_field> outer_fields;
unknown's avatar
unknown committed
378

unknown's avatar
unknown committed
379
protected:  
380 381 382 383 384 385
  /* 
    Copy of the arguments list to hold the original set of arguments.
    Used in EXPLAIN EXTENDED instead of the current argument list because 
    the current argument list can be altered by usage of temporary tables.
  */
  Item **orig_args, *tmp_orig_args[2];
Sergey Petrunia's avatar
Sergey Petrunia committed
386
  
387
  static size_t ram_limitation(THD *thd);
388 389 390
public:
  // Methods used by ColumnStore
  Item **get_orig_args() const { return orig_args; }
unknown's avatar
unknown committed
391 392
public:  

393
  void mark_as_sum_func();
394
  Item_sum(THD *thd): Item_func_or_sum(thd), quick_group(1)
395 396
  {
    mark_as_sum_func();
397
    init_aggregator();
398
  }
399
  Item_sum(THD *thd, Item *a): Item_func_or_sum(thd, a), quick_group(1),
400
    orig_args(tmp_orig_args)
unknown's avatar
unknown committed
401
  {
402
    mark_as_sum_func();
403
    init_aggregator();
unknown's avatar
unknown committed
404
  }
405
  Item_sum(THD *thd, Item *a, Item *b): Item_func_or_sum(thd, a, b),
406
    quick_group(1), orig_args(tmp_orig_args)
unknown's avatar
unknown committed
407
  {
408
    mark_as_sum_func();
409
    init_aggregator();
unknown's avatar
unknown committed
410
  }
411
  Item_sum(THD *thd, List<Item> &list);
412
  //Copy constructor, need to perform subselects with temporary tables
413
  Item_sum(THD *thd, Item_sum *item);
414
  enum Type type() const override { return SUM_FUNC_ITEM; }
unknown's avatar
unknown committed
415
  virtual enum Sumfunctype sum_func () const=0;
Igor Babaev's avatar
Igor Babaev committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
  bool is_aggr_sum_func()
  {
    switch (sum_func()) {
    case COUNT_FUNC:
    case COUNT_DISTINCT_FUNC:
    case SUM_FUNC:
    case SUM_DISTINCT_FUNC:
    case AVG_FUNC:
    case AVG_DISTINCT_FUNC:
    case MIN_FUNC:
    case MAX_FUNC:
    case STD_FUNC:
    case VARIANCE_FUNC:
    case SUM_BIT_FUNC:
    case UDF_SUM_FUNC:
    case GROUP_CONCAT_FUNC:
432
    case JSON_ARRAYAGG_FUNC:
Igor Babaev's avatar
Igor Babaev committed
433 434 435 436 437
      return true;
    default:
      return false;
    }
  }
438 439 440
  /**
    Resets the aggregate value to its default and aggregates the current
    value of its attribute(s).
Igor Babaev's avatar
Igor Babaev committed
441
  */
442 443
  inline bool reset_and_add() 
  { 
Michael Widenius's avatar
Michael Widenius committed
444 445
    aggregator_clear();
    return aggregator_add();
446
  };
447

unknown's avatar
unknown committed
448 449
  /*
    Called when new group is started and results are being saved in
450 451 452 453
    a temporary table. Similarly to reset_and_add() it resets the 
    value to its default and aggregates the value of its 
    attribute(s), but must also store it in result_field. 
    This set of methods (result_item(), reset_field, update_field()) of
unknown's avatar
unknown committed
454 455
    Item_sum is used only if quick_group is not null. Otherwise
    copy_or_same() is used to obtain a copy of this item.
unknown's avatar
unknown committed
456
  */
unknown's avatar
unknown committed
457
  virtual void reset_field()=0;
unknown's avatar
unknown committed
458 459 460 461 462
  /*
    Called for each new value in the group, when temporary table is in use.
    Similar to add(), but uses temporary table field to obtain current value,
    Updated value is then saved in the field.
  */
unknown's avatar
unknown committed
463
  virtual void update_field()=0;
464
  bool fix_length_and_dec() override
465
  {
466
    set_maybe_null();
467 468 469
    null_value=1;
    return FALSE;
  }
Monty's avatar
Monty committed
470 471
  virtual Item *result_item(THD *thd, Field *field);

472
  void update_used_tables() override;
473
  COND *build_equal_items(THD *thd, COND_EQUAL *inherited,
474
                          bool link_item_fields,
475
                          COND_EQUAL **cond_equal_ref) override
476 477 478 479 480 481 482
  {
    /*
      Item_sum (and derivants) of the original WHERE/HAVING clauses
      should already be replaced to Item_aggregate_ref by the time when
      build_equal_items() is called. See Item::split_sum_func2().
    */
    DBUG_ASSERT(0);
483 484
    return Item::build_equal_items(thd, inherited, link_item_fields,
                                   cond_equal_ref);
485
  }
486
  bool is_null() override { return null_value; }
487 488 489 490 491 492
  /**
    make_const()
    Called if we've managed to calculate the value of this Item in
    opt_sum_query(), hence it can be considered constant at all subsequent
    steps.
  */
unknown's avatar
unknown committed
493 494
  void make_const () 
  { 
Michael Widenius's avatar
Michael Widenius committed
495
    used_tables_cache= 0;
496
    const_item_cache= true;
unknown's avatar
unknown committed
497
  }
Marko Mäkelä's avatar
Marko Mäkelä committed
498
  void reset_forced_const() { const_item_cache= false; }
499 500
  bool const_during_execution() const override { return false; }
  void print(String *str, enum_query_type query_type) override;
unknown's avatar
unknown committed
501
  void fix_num_length_and_dec();
502

503 504 505
  /**
    Mark an aggregate as having no rows.

506 507 508 509 510 511 512
    This function is called by the execution engine to assign 'NO ROWS
    FOUND' value to an aggregate item, when the underlying result set
    has no rows. Such value, in a general case, may be different from
    the default value of the item after 'clear()': e.g. a numeric item
    may be initialized to 0 by clear() and to NULL by
    no_rows_in_result().
  */
513
  void no_rows_in_result() override
514
  {
515
    set_aggregator(current_thd, with_distinct ?
516 517
                   Aggregator::DISTINCT_AGGREGATOR :
                   Aggregator::SIMPLE_AGGREGATOR);
518
    aggregator_clear();
519 520
  }
  virtual void make_unique() { force_copy_fields= TRUE; }
521
  Item *get_tmp_table_item(THD *thd) override;
522 523
  virtual Field *create_tmp_field(MEM_ROOT *root, bool group, TABLE *table);
  Field *create_tmp_field_ex(MEM_ROOT *root, TABLE *table, Tmp_field_src *src,
524
                             const Tmp_field_param *param) override
525
  {
526
    return create_tmp_field(root, param->group(), table);
527
  }
528
  bool collect_outer_ref_processor(void *param) override;
unknown's avatar
unknown committed
529 530 531
  bool init_sum_func_check(THD *thd);
  bool check_sum_func(THD *thd, Item **ref);
  bool register_sum_func(THD *thd, Item **ref);
532 533
  st_select_lex *depended_from() 
    { return (nest_level == aggr_level ? 0 : aggr_sel); }
534

535
  Item *get_arg(uint i) const { return args[i]; }
536
  Item *set_arg(uint i, THD *thd, Item *new_val);
537
  uint get_arg_count() const { return arg_count; }
538
  virtual Item **get_args() { return fixed() ? orig_args : args; }
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

  /* Initialization of distinct related members */
  void init_aggregator()
  {
    aggr= NULL;
    with_distinct= FALSE;
    force_copy_fields= FALSE;
  }

  /**
    Called to initialize the aggregator.
  */

  inline bool aggregator_setup(THD *thd) { return aggr->setup(thd); };

  /**
    Called to cleanup the aggregator.
  */

  inline void aggregator_clear() { aggr->clear(); }

  /**
    Called to add value to the aggregator.
  */

  inline bool aggregator_add() { return aggr->add(); };

  /* stores the declared DISTINCT flag (from the parser) */
  void set_distinct(bool distinct)
  {
    with_distinct= distinct;
    quick_group= with_distinct ? 0 : 1;
  }

573
  /*
574 575
    Set the type of aggregation : DISTINCT or not.

576
    May be called multiple times.
577 578
  */

579
  int set_aggregator(THD *thd, Aggregator::Aggregator_type aggregator);
580

581 582
  virtual void clear()= 0;
  virtual bool add()= 0;
583
  virtual bool setup(THD *thd) { return false; }
584 585

  virtual bool supports_removal() const { return false; }
586
  virtual void remove() { DBUG_ASSERT(0); }
587

588 589
  void cleanup() override;
  bool check_vcol_func_processor(void *arg) override;
590
  virtual void setup_window_func(THD *thd, Window_spec *window_spec) {}
Igor Babaev's avatar
Igor Babaev committed
591
  void mark_as_window_func_sum_expr() { window_func_sum_expr_flag= true; }
Igor Babaev's avatar
Igor Babaev committed
592 593
  bool is_window_func_sum_expr() { return window_func_sum_expr_flag; }
  virtual void setup_caches(THD *thd) {};
594
  virtual void set_partition_row_count(ulonglong count) { DBUG_ASSERT(0); }
595 596 597 598 599 600 601 602 603
};


class Unique;


/**
 The distinct aggregator. 
 Implements AGGFN (DISTINCT ..)
604 605 606
 Collects all the data into an Unique (similarly to what Item_sum
 does currently when with_distinct=true) and then (if applicable) iterates over
 the list of unique values and pumps them back into its object
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
*/

class Aggregator_distinct : public Aggregator
{
  friend class Item_sum_sum;

  /* 
    flag to prevent consecutive runs of endup(). Normally in endup there are 
    expensive calculations (like walking the distinct tree for example) 
    which we must do only once if there are no data changes.
    We can re-use the data for the second and subsequent val_xxx() calls.
    endup_done set to TRUE also means that the calculated values for
    the aggregate functions are correct and don't need recalculation.
  */
  bool endup_done;

  /*
    Used depending on the type of the aggregate function and the presence of
    blob columns in it:
    - For COUNT(DISTINCT) and no blob fields this points to a real temporary
      table. It's used as a hash table.
    - For AVG/SUM(DISTINCT) or COUNT(DISTINCT) with blob fields only the
      in-memory data structure of a temporary table is constructed.
      It's used by the Field classes to transform data into row format.
  */
  TABLE *table;
  
  /*
    An array of field lengths on row allocated and used only for 
    COUNT(DISTINCT) with multiple columns and no blobs. Used in 
    Aggregator_distinct::composite_key_cmp (called from Unique to compare 
    nodes
  */
  uint32 *field_lengths;

  /*
643
    Used in conjunction with 'table' to support the access to Field classes 
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
    for COUNT(DISTINCT). Needed by copy_fields()/copy_funcs().
  */
  TMP_TABLE_PARAM *tmp_table_param;
  
  /*
    If there are no blobs in the COUNT(DISTINCT) arguments, we can use a tree,
    which is faster than heap table. In that case, we still use the table
    to help get things set up, but we insert nothing in it. 
    For AVG/SUM(DISTINCT) we always use this tree (as it takes a single 
    argument) to get the distinct rows.
  */
  Unique *tree;

  /* 
    The length of the temp table row. Must be a member of the class as it
    gets passed down to simple_raw_key_cmp () as a compare function argument
    to Unique. simple_raw_key_cmp () is used as a fast comparison function 
    when the entire row can be binary compared.
  */  
  uint tree_key_length;

  /* 
    Set to true if the result is known to be always NULL.
    If set deactivates creation and usage of the temporary table (in the 
    'table' member) and the Unique instance (in the 'tree' member) as well as 
    the calculation of the final value on the first call to 
    Item_[sum|avg|count]::val_xxx(). 
  */
  bool always_null;

674 675 676 677 678 679 680 681 682 683
  /**
    When feeding back the data in endup() from Unique/temp table back to
    Item_sum::add() methods we must read the data from Unique (and not
    recalculate the functions that are given as arguments to the aggregate
    function.
    This flag is to tell the arg_*() methods to take the data from the Unique
    instead of calling the relevant val_..() method.
  */
  bool use_distinct_values;

684 685 686
public:
  Aggregator_distinct (Item_sum *sum) :
    Aggregator(sum), table(NULL), tmp_table_param(NULL), tree(NULL),
687
    always_null(false), use_distinct_values(false) {}
688 689 690 691 692 693 694
  virtual ~Aggregator_distinct ();
  Aggregator_type Aggrtype() { return DISTINCT_AGGREGATOR; }

  bool setup(THD *);
  void clear(); 
  bool add();
  void endup();
695 696
  virtual my_decimal *arg_val_decimal(my_decimal * value);
  virtual double arg_val_real();
697
  virtual bool arg_is_null(bool use_null_value);
698 699

  bool unique_walk_function(void *element);
700
  bool unique_walk_function_for_count(void *element);
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
  static int composite_key_cmp(void* arg, uchar* key1, uchar* key2);
};


/**
  The pass-through aggregator. 
  Implements AGGFN (DISTINCT ..) by knowing it gets distinct data on input. 
  So it just pumps them back to the Item_sum descendant class.
*/
class Aggregator_simple : public Aggregator
{
public:

  Aggregator_simple (Item_sum *sum) :
    Aggregator(sum) {}
  Aggregator_type Aggrtype() { return Aggregator::SIMPLE_AGGREGATOR; }

  bool setup(THD * thd) { return item_sum->setup(thd); }
  void clear() { item_sum->clear(); }
  bool add() { return item_sum->add(); }
  void endup() {};
722 723
  virtual my_decimal *arg_val_decimal(my_decimal * value);
  virtual double arg_val_real();
724
  virtual bool arg_is_null(bool use_null_value);
unknown's avatar
unknown committed
725 726 727 728 729 730
};


class Item_sum_num :public Item_sum
{
public:
731
  Item_sum_num(THD *thd): Item_sum(thd) {}
732
  Item_sum_num(THD *thd, Item *item_par):
733
    Item_sum(thd, item_par) {}
734
  Item_sum_num(THD *thd, Item *a, Item* b):
735
    Item_sum(thd, a, b) {}
736
  Item_sum_num(THD *thd, List<Item> &list):
737
    Item_sum(thd, list) {}
738
  Item_sum_num(THD *thd, Item_sum_num *item):
739
    Item_sum(thd, item) {}
unknown's avatar
unknown committed
740
  bool fix_fields(THD *, Item **);
741 742 743 744 745 746 747 748 749 750
};


class Item_sum_double :public Item_sum_num
{
public:
  Item_sum_double(THD *thd): Item_sum_num(thd) {}
  Item_sum_double(THD *thd, Item *item_par): Item_sum_num(thd, item_par) {}
  Item_sum_double(THD *thd, List<Item> &list): Item_sum_num(thd, list) {}
  Item_sum_double(THD *thd, Item_sum_double *item) :Item_sum_num(thd, item) {}
751
  longlong val_int() override
752 753 754
  {
    return val_int_from_real();
  }
755
  String *val_str(String*str) override
756 757 758
  {
    return val_string_from_real(str);
  }
759
  my_decimal *val_decimal(my_decimal *to) override
760 761 762
  {
    return val_decimal_from_real(to);
  }
763
  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
764
  {
765
    return get_date_from_real(thd, ltime, fuzzydate);
766
  }
767 768
  const Type_handler *type_handler() const override
  { return &type_handler_double; }
unknown's avatar
unknown committed
769 770 771 772 773 774
};


class Item_sum_int :public Item_sum_num
{
public:
775
  Item_sum_int(THD *thd): Item_sum_num(thd) {}
776 777
  Item_sum_int(THD *thd, Item *item_par): Item_sum_num(thd, item_par) {}
  Item_sum_int(THD *thd, List<Item> &list): Item_sum_num(thd, list) {}
778
  Item_sum_int(THD *thd, Item_sum_int *item) :Item_sum_num(thd, item) {}
779 780 781 782
  double val_real() override { DBUG_ASSERT(fixed()); return (double) val_int(); }
  String *val_str(String*str) override;
  my_decimal *val_decimal(my_decimal *) override;
  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
783
  {
784
    return get_date_from_int(thd, ltime, fuzzydate);
785
  }
786
  bool fix_length_and_dec() override
787 788 789
  {
    decimals=0;
    max_length=21;
790
    base_flags&= ~item_base_t::MAYBE_NULL;
791 792
    null_value=0;
    return FALSE; }
unknown's avatar
unknown committed
793 794 795
};


796
class Item_sum_sum :public Item_sum_num,
797
                   public Type_handler_hybrid_field_type 
unknown's avatar
unknown committed
798
{
unknown's avatar
unknown committed
799
protected:
Monty's avatar
Monty committed
800 801 802 803
  bool direct_added;
  bool direct_reseted_field;
  bool direct_sum_is_null;
  double direct_sum_real;
unknown's avatar
unknown committed
804
  double sum;
Monty's avatar
Monty committed
805
  my_decimal direct_sum_decimal;
unknown's avatar
unknown committed
806 807
  my_decimal dec_buffs[2];
  uint curr_dec_buff;
808
  bool fix_length_and_dec() override;
unknown's avatar
unknown committed
809

unknown's avatar
unknown committed
810
public:
811
  Item_sum_sum(THD *thd, Item *item_par, bool distinct):
Monty's avatar
Monty committed
812 813
    Item_sum_num(thd, item_par), direct_added(FALSE),
    direct_reseted_field(FALSE)
814 815 816
  {
    set_distinct(distinct);
  }
unknown's avatar
unknown committed
817
  Item_sum_sum(THD *thd, Item_sum_sum *item);
818
  enum Sumfunctype sum_func() const override
819
  { 
820
    return has_with_distinct() ? SUM_DISTINCT_FUNC : SUM_FUNC; 
821
  }
822
  void cleanup() override;
Monty's avatar
Monty committed
823 824
  void direct_add(my_decimal *add_sum_decimal);
  void direct_add(double add_sum_real, bool add_sum_is_null);
825 826 827 828 829 830 831
  void clear() override;
  bool add() override;
  double val_real() override;
  longlong val_int() override;
  String *val_str(String*str) override;
  my_decimal *val_decimal(my_decimal *) override;
  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
832
  {
833
    return type_handler()->Item_get_date_with_warn(thd, this, ltime, fuzzydate);
834
  }
835
  const Type_handler *type_handler() const override
836
  { return Type_handler_hybrid_field_type::type_handler(); }
837 838
  void fix_length_and_dec_double();
  void fix_length_and_dec_decimal();
839 840 841
  void reset_field() override;
  void update_field() override;
  void no_rows_in_result() override {}
Monty's avatar
Monty committed
842
  LEX_CSTRING func_name_cstring() const override
843
  { 
Monty's avatar
Monty committed
844 845 846
    static LEX_CSTRING name_distinct= { STRING_WITH_LEN("sum(distinct ")};
    static LEX_CSTRING name_normal=   { STRING_WITH_LEN("sum(") };
    return has_with_distinct() ? name_distinct : name_normal;
847
  }
848 849 850
  Item *copy_or_same(THD* thd) override;
  void remove() override;
  Item *get_copy(THD *thd) override
851
  { return get_item_copy<Item_sum_sum>(thd, this); }
852

853
  bool supports_removal() const override
854 855 856 857
  {
    return true;
  }

858 859
private:
  void add_helper(bool perform_removal);
860
  ulonglong count;
unknown's avatar
unknown committed
861 862 863
};


864
class Item_sum_count :public Item_sum_int
unknown's avatar
unknown committed
865
{
Monty's avatar
Monty committed
866 867 868
  bool direct_counted;
  bool direct_reseted_field;
  longlong direct_count;
869 870 871
  longlong count;

  friend class Aggregator_distinct;
unknown's avatar
unknown committed
872

873 874 875 876
  void clear() override;
  bool add() override;
  void cleanup() override;
  void remove() override;
unknown's avatar
unknown committed
877

Monty's avatar
Monty committed
878
public:
879
  Item_sum_count(THD *thd, Item *item_par):
Monty's avatar
Monty committed
880 881
    Item_sum_int(thd, item_par), direct_counted(FALSE),
    direct_reseted_field(FALSE), count(0)
unknown's avatar
unknown committed
882
  {}
883 884 885 886 887 888 889 890 891

  /**
    Constructs an instance for COUNT(DISTINCT)

    @param list  a list of the arguments to the aggregate function

    This constructor is called by the parser only for COUNT (DISTINCT).
  */

892
  Item_sum_count(THD *thd, List<Item> &list):
Monty's avatar
Monty committed
893 894
    Item_sum_int(thd, list), direct_counted(FALSE),
    direct_reseted_field(FALSE), count(0)
895 896 897
  {
    set_distinct(TRUE);
  }
898
  Item_sum_count(THD *thd, Item_sum_count *item):
Monty's avatar
Monty committed
899 900
    Item_sum_int(thd, item), direct_counted(FALSE),
    direct_reseted_field(FALSE), count(item->count)
901
  {}
902
  enum Sumfunctype sum_func () const override
903
  { 
904
    return has_with_distinct() ? COUNT_DISTINCT_FUNC : COUNT_FUNC; 
905
  }
906
  void no_rows_in_result() override { count=0; }
unknown's avatar
unknown committed
907 908 909 910 911
  void make_const(longlong count_arg) 
  { 
    count=count_arg;
    Item_sum::make_const();
  }
912 913 914 915 916
  const Type_handler *type_handler() const override
  { return &type_handler_slonglong; }
  longlong val_int() override;
  void reset_field() override;
  void update_field() override;
Monty's avatar
Monty committed
917
  void direct_add(longlong add_count);
Monty's avatar
Monty committed
918
  LEX_CSTRING func_name_cstring() const override
919
  { 
Monty's avatar
Monty committed
920 921 922
    static LEX_CSTRING name_distinct= { STRING_WITH_LEN("count(distinct ")};
    static LEX_CSTRING name_normal=   { STRING_WITH_LEN("count(") };
    return has_with_distinct() ? name_distinct : name_normal;
923
  }
924 925
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
926
  { return get_item_copy<Item_sum_count>(thd, this); }
927

928
  bool supports_removal() const override
929 930 931
  {
    return true;
  }
unknown's avatar
unknown committed
932 933 934
};


unknown's avatar
unknown committed
935
class Item_sum_avg :public Item_sum_sum
unknown's avatar
unknown committed
936
{
unknown's avatar
unknown committed
937
public:
938 939
  // TODO-cvicentiu given that Item_sum_sum now uses a counter of its own, in
  // order to implement remove(), it is possible to remove this member.
unknown's avatar
unknown committed
940
  ulonglong count;
unknown's avatar
unknown committed
941 942
  uint prec_increment;
  uint f_precision, f_scale, dec_bin_size;
unknown's avatar
unknown committed
943

944 945
  Item_sum_avg(THD *thd, Item *item_par, bool distinct):
    Item_sum_sum(thd, item_par, distinct), count(0)
946
  {}
947
  Item_sum_avg(THD *thd, Item_sum_avg *item)
unknown's avatar
unknown committed
948 949 950
    :Item_sum_sum(thd, item), count(item->count),
    prec_increment(item->prec_increment) {}

951 952
  void fix_length_and_dec_double();
  void fix_length_and_dec_decimal();
953 954
  bool fix_length_and_dec() override;
  enum Sumfunctype sum_func () const override
955
  {
956
    return has_with_distinct() ? AVG_DISTINCT_FUNC : AVG_FUNC;
957
  }
958 959 960 961
  void clear() override;
  bool add() override;
  void remove() override;
  double val_real() override;
unknown's avatar
unknown committed
962
  // In SPs we might force the "wrong" type with select into a declare variable
963 964 965 966 967 968 969
  longlong val_int() override { return val_int_from_real(); }
  my_decimal *val_decimal(my_decimal *) override;
  String *val_str(String *str) override;
  void reset_field() override;
  void update_field() override;
  Item *result_item(THD *thd, Field *field) override;
  void no_rows_in_result() override {}
Monty's avatar
Monty committed
970
  LEX_CSTRING func_name_cstring() const override
971
  { 
Monty's avatar
Monty committed
972 973 974
    static LEX_CSTRING name_distinct= { STRING_WITH_LEN("avg(distinct ")};
    static LEX_CSTRING name_normal=   { STRING_WITH_LEN("avg(") };
    return has_with_distinct() ? name_distinct : name_normal;
975
  }
976 977 978
  Item *copy_or_same(THD* thd) override;
  Field *create_tmp_field(MEM_ROOT *root, bool group, TABLE *table) override;
  void cleanup() override
979
  {
980 981
    count= 0;
    Item_sum_sum::cleanup();
982
  }
983
  Item *get_copy(THD *thd) override
984
  { return get_item_copy<Item_sum_avg>(thd, this); }
985

986
  bool supports_removal() const override
987 988 989
  {
    return true;
  }
unknown's avatar
unknown committed
990 991
};

unknown's avatar
unknown committed
992

unknown's avatar
unknown committed
993 994 995 996 997 998 999 1000 1001 1002
/*
  variance(a) =

  =  sum (ai - avg(a))^2 / count(a) )
  =  sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
  =  (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) = 
  =  (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) = 
  =  (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2 )/count(a) = 
  =  (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a) )/count(a) = 
  =  (sum(ai^2) - sum(a)^2/count(a))/count(a)
1003 1004 1005 1006 1007 1008 1009 1010

But, this falls prey to catastrophic cancellation.  Instead, use the recurrence formulas

  M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline 
  S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
  for 2 <= k <= n newline
  ital variance = S_{n} / (n-1)

1011
*/
unknown's avatar
unknown committed
1012

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
class Stddev
{
  double m_m;
  double m_s;
  ulonglong m_count;
public:
  Stddev() :m_m(0), m_s(0), m_count(0) { }
  Stddev(double nr) :m_m(nr), m_s(0.0), m_count(1) { }
  Stddev(const uchar *);
  void to_binary(uchar *) const;
  void recurrence_next(double nr);
  double result(bool is_simple_variance);
  ulonglong count() const { return m_count; }
  static uint32 binary_size()
  {
    return (uint32) (sizeof(double) * 2 + sizeof(ulonglong));
  };
};



1034
class Item_sum_variance :public Item_sum_double
unknown's avatar
unknown committed
1035
{
1036
  Stddev m_stddev;
1037
  bool fix_length_and_dec() override;
unknown's avatar
unknown committed
1038 1039 1040 1041 1042

public:
  uint sample;
  uint prec_increment;

1043
  Item_sum_variance(THD *thd, Item *item_par, uint sample_arg):
1044
    Item_sum_double(thd, item_par),
1045
    sample(sample_arg)
unknown's avatar
unknown committed
1046 1047
    {}
  Item_sum_variance(THD *thd, Item_sum_variance *item);
1048
  Sumfunctype sum_func () const override { return VARIANCE_FUNC; }
1049 1050
  void fix_length_and_dec_double();
  void fix_length_and_dec_decimal();
1051 1052
  void clear() override final;
  bool add() override final;
1053
  double val_real() override;
1054 1055
  void reset_field() override final;
  void update_field() override final;
1056
  Item *result_item(THD *thd, Field *field) override;
1057
  void no_rows_in_result() override final {}
Monty's avatar
Monty committed
1058 1059 1060 1061 1062 1063
  LEX_CSTRING func_name_cstring() const override
  {
    static LEX_CSTRING name_sample= { STRING_WITH_LEN("var_samp(")};
    static LEX_CSTRING name_normal=   { STRING_WITH_LEN("variance(") };
    return sample ? name_sample : name_normal;
  }
1064 1065 1066 1067
  Item *copy_or_same(THD* thd) override;
  Field *create_tmp_field(MEM_ROOT *root, bool group, TABLE *table) override
    final;
  void cleanup() override final
1068
  {
1069
    m_stddev= Stddev();
1070
    Item_sum_double::cleanup();
1071
  }
1072
  Item *get_copy(THD *thd) override
1073
  { return get_item_copy<Item_sum_variance>(thd, this); }
unknown's avatar
unknown committed
1074 1075
};

1076 1077 1078 1079
/*
   standard_deviation(a) = sqrt(variance(a))
*/

1080
class Item_sum_std final :public Item_sum_variance
1081 1082
{
  public:
1083 1084
  Item_sum_std(THD *thd, Item *item_par, uint sample_arg):
    Item_sum_variance(thd, item_par, sample_arg) {}
1085
  Item_sum_std(THD *thd, Item_sum_std *item)
1086 1087
    :Item_sum_variance(thd, item)
    {}
1088 1089 1090
  enum Sumfunctype sum_func () const override final { return STD_FUNC; }
  double val_real() override final;
  Item *result_item(THD *thd, Field *field) override final;
Monty's avatar
Monty committed
1091 1092 1093 1094 1095
  LEX_CSTRING func_name_cstring() const override
  {
    static LEX_CSTRING sum_name= {STRING_WITH_LEN("std(") };
    return sum_name;
  }
1096 1097
  Item *copy_or_same(THD* thd) override final;
  Item *get_copy(THD *thd) override final
1098
  { return get_item_copy<Item_sum_std>(thd, this); }
1099
};
unknown's avatar
unknown committed
1100

1101

1102
class Item_sum_hybrid : public Item_sum,
1103 1104 1105 1106 1107
                       public Type_handler_hybrid_field_type
{
public:
  Item_sum_hybrid(THD *thd, Item *item_par):
    Item_sum(thd, item_par),
1108
    Type_handler_hybrid_field_type(&type_handler_slonglong)
1109 1110 1111
  { collation.set(&my_charset_bin); }
  Item_sum_hybrid(THD *thd, Item *a, Item *b):
    Item_sum(thd, a, b),
1112
    Type_handler_hybrid_field_type(&type_handler_slonglong)
1113 1114 1115 1116 1117
  { collation.set(&my_charset_bin); }
  Item_sum_hybrid(THD *thd, Item_sum_hybrid *item)
    :Item_sum(thd, item),
    Type_handler_hybrid_field_type(item)
  { }
1118
  const Type_handler *type_handler() const override
1119
  { return Type_handler_hybrid_field_type::type_handler(); }
1120 1121 1122
  bool fix_length_and_dec_generic();
  bool fix_length_and_dec_numeric(const Type_handler *h);
  bool fix_length_and_dec_string();
1123 1124 1125
};


unknown's avatar
unknown committed
1126
// This class is a string or number function depending on num_func
1127 1128
class Arg_comparator;
class Item_cache;
1129
class Item_sum_min_max :public Item_sum_hybrid
unknown's avatar
unknown committed
1130
{
unknown's avatar
unknown committed
1131
protected:
Monty's avatar
Monty committed
1132 1133
  bool direct_added;
  Item *direct_item;
1134
  Item_cache *value, *arg_cache;
1135
  Arg_comparator *cmp;
unknown's avatar
unknown committed
1136
  int cmp_sign;
unknown's avatar
unknown committed
1137
  bool was_values;  // Set if we have found at least one row (for max/min only)
1138
  bool was_null_value;
unknown's avatar
unknown committed
1139

1140 1141 1142
public:
  Item_sum_min_max(THD *thd, Item *item_par,int sign):
    Item_sum_hybrid(thd, item_par),
Monty's avatar
Monty committed
1143
    direct_added(FALSE), value(0), arg_cache(0), cmp(0),
unknown's avatar
unknown committed
1144
    cmp_sign(sign), was_values(TRUE)
unknown's avatar
unknown committed
1145
  { collation.set(&my_charset_bin); }
1146 1147
  Item_sum_min_max(THD *thd, Item_sum_min_max *item)
    :Item_sum_hybrid(thd, item),
Monty's avatar
Monty committed
1148
    direct_added(FALSE), value(item->value), arg_cache(0),
1149
    cmp_sign(item->cmp_sign), was_values(item->was_values)
1150
  { }
1151 1152
  bool fix_fields(THD *, Item **) override;
  bool fix_length_and_dec() override;
1153
  void setup_hybrid(THD *thd, Item *item, Item *value_arg);
1154
  void clear() override;
Monty's avatar
Monty committed
1155
  void direct_add(Item *item);
1156 1157 1158 1159 1160 1161 1162 1163
  double val_real() override;
  longlong val_int() override;
  my_decimal *val_decimal(my_decimal *) override;
  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override;
  void reset_field() override;
  String *val_str(String *) override;
  bool val_native(THD *thd, Native *) override;
  const Type_handler *real_type_handler() const override
1164 1165 1166
  {
    return get_arg(0)->real_type_handler();
  }
1167 1168
  const TYPELIB *get_typelib() const  override { return args[0]->get_typelib(); }
  void update_field() override;
unknown's avatar
unknown committed
1169 1170 1171
  void min_max_update_str_field();
  void min_max_update_real_field();
  void min_max_update_int_field();
unknown's avatar
unknown committed
1172
  void min_max_update_decimal_field();
1173
  void min_max_update_native_field();
1174
  void cleanup() override;
1175
  bool any_value() { return was_values; }
1176 1177 1178 1179 1180
  void no_rows_in_result() override;
  void restore_to_before_no_rows_in_result() override;
  Field *create_tmp_field(MEM_ROOT *root, bool group, TABLE *table) override;
  void setup_caches(THD *thd) override
  { setup_hybrid(thd, arguments()[0], NULL); }
unknown's avatar
unknown committed
1181 1182 1183
};


1184
class Item_sum_min final :public Item_sum_min_max
unknown's avatar
unknown committed
1185 1186
{
public:
1187 1188
  Item_sum_min(THD *thd, Item *item_par): Item_sum_min_max(thd, item_par, 1) {}
  Item_sum_min(THD *thd, Item_sum_min *item) :Item_sum_min_max(thd, item) {}
1189
  enum Sumfunctype sum_func () const override {return MIN_FUNC;}
unknown's avatar
unknown committed
1190

1191
  bool add() override;
Monty's avatar
Monty committed
1192 1193 1194 1195 1196
  LEX_CSTRING func_name_cstring() const override
  {
    static LEX_CSTRING sum_name= {STRING_WITH_LEN("min(") };
    return sum_name;
  }
1197 1198
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
1199
  { return get_item_copy<Item_sum_min>(thd, this); }
unknown's avatar
unknown committed
1200 1201 1202
};


1203
class Item_sum_max final :public Item_sum_min_max
unknown's avatar
unknown committed
1204 1205
{
public:
1206 1207
  Item_sum_max(THD *thd, Item *item_par): Item_sum_min_max(thd, item_par, -1) {}
  Item_sum_max(THD *thd, Item_sum_max *item) :Item_sum_min_max(thd, item) {}
1208
  enum Sumfunctype sum_func () const  override {return MAX_FUNC;}
unknown's avatar
unknown committed
1209

1210
  bool add() override;
Monty's avatar
Monty committed
1211 1212 1213 1214 1215
  LEX_CSTRING func_name_cstring() const override
  {
    static LEX_CSTRING sum_name= {STRING_WITH_LEN("max(") };
    return sum_name;
  }
1216 1217
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
1218
  { return get_item_copy<Item_sum_max>(thd, this); }
unknown's avatar
unknown committed
1219 1220 1221 1222 1223
};


class Item_sum_bit :public Item_sum_int
{
1224
public:
1225
  Item_sum_bit(THD *thd, Item *item_par, ulonglong reset_arg):
1226 1227
    Item_sum_int(thd, item_par), reset_bits(reset_arg), bits(reset_arg),
    as_window_function(FALSE), num_values_added(0) {}
1228
  Item_sum_bit(THD *thd, Item_sum_bit *item):
1229 1230 1231 1232 1233 1234 1235
    Item_sum_int(thd, item), reset_bits(item->reset_bits), bits(item->bits),
    as_window_function(item->as_window_function),
    num_values_added(item->num_values_added)
  {
    if (as_window_function)
      memcpy(bit_counters, item->bit_counters, sizeof(bit_counters));
  }
1236 1237 1238 1239 1240 1241 1242 1243
  enum Sumfunctype sum_func () const override { return SUM_BIT_FUNC;}
  void clear() override;
  longlong val_int() override;
  void reset_field() override;
  void update_field() override;
  const Type_handler *type_handler() const override
  { return &type_handler_ulonglong; }
  bool fix_length_and_dec() override
1244
  {
Monty's avatar
Monty committed
1245
    if (args[0]->check_type_can_return_int(func_name_cstring()))
1246
      return true;
1247
    decimals= 0; max_length=21; unsigned_flag= 1;
1248
    base_flags&= ~item_base_t::MAYBE_NULL;
1249
    null_value= 0;
1250 1251
    return FALSE;
  }
1252
  void cleanup() override
1253
  {
1254
    bits= reset_bits;
1255 1256
    if (as_window_function)
      clear_as_window();
1257 1258
    Item_sum_int::cleanup();
  }
1259 1260
  void setup_window_func(THD *thd __attribute__((unused)),
                         Window_spec *window_spec __attribute__((unused)))
1261
    override
1262 1263 1264 1265
  {
    as_window_function= TRUE;
    clear_as_window();
  }
1266
  void remove() override
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
  {
    if (as_window_function)
    {
      remove_as_window(args[0]->val_int());
      return;
    }
    // Unless we're counting bits, we can not remove anything.
    DBUG_ASSERT(0);
  }

1277
  bool supports_removal() const override
1278 1279 1280 1281
  {
    return true;
  }

1282
protected:
1283
  enum bit_counters { NUM_BIT_COUNTERS= 64 };
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
  ulonglong reset_bits,bits;
  /*
    Marks whether the function is to be computed as a window function.
  */
  bool as_window_function;
  // When used as an aggregate window function, we need to store
  // this additional information.
  ulonglong num_values_added;
  ulonglong bit_counters[NUM_BIT_COUNTERS];
  bool add_as_window(ulonglong value);
  bool remove_as_window(ulonglong value);
  bool clear_as_window();
  virtual void set_bits_from_counters()= 0;
unknown's avatar
unknown committed
1297 1298 1299
};


1300
class Item_sum_or final :public Item_sum_bit
unknown's avatar
unknown committed
1301
{
unknown's avatar
unknown committed
1302
public:
1303
  Item_sum_or(THD *thd, Item *item_par): Item_sum_bit(thd, item_par, 0) {}
1304
  Item_sum_or(THD *thd, Item_sum_or *item) :Item_sum_bit(thd, item) {}
1305
  bool add() override;
Monty's avatar
Monty committed
1306 1307 1308 1309 1310
  LEX_CSTRING func_name_cstring() const override
  {
    static LEX_CSTRING sum_name= {STRING_WITH_LEN("bit_or(") };
    return sum_name;
  }
1311 1312
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
1313
  { return get_item_copy<Item_sum_or>(thd, this); }
1314 1315

private:
1316
  void set_bits_from_counters() override;
unknown's avatar
unknown committed
1317 1318 1319
};


1320
class Item_sum_and final :public Item_sum_bit
unknown's avatar
unknown committed
1321
{
1322
public:
1323 1324
  Item_sum_and(THD *thd, Item *item_par):
    Item_sum_bit(thd, item_par, ULONGLONG_MAX) {}
1325
  Item_sum_and(THD *thd, Item_sum_and *item) :Item_sum_bit(thd, item) {}
1326
  bool add() override;
Monty's avatar
Monty committed
1327 1328 1329 1330 1331
  LEX_CSTRING func_name_cstring() const override
  {
    static LEX_CSTRING sum_min_name= {STRING_WITH_LEN("bit_and(") };
    return sum_min_name;
  }
1332 1333
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
1334
  { return get_item_copy<Item_sum_and>(thd, this); }
1335 1336

private:
1337
  void set_bits_from_counters() override;
unknown's avatar
unknown committed
1338 1339
};

1340
class Item_sum_xor final :public Item_sum_bit
1341
{
1342
public:
1343
  Item_sum_xor(THD *thd, Item *item_par): Item_sum_bit(thd, item_par, 0) {}
1344
  Item_sum_xor(THD *thd, Item_sum_xor *item) :Item_sum_bit(thd, item) {}
1345
  bool add() override;
Monty's avatar
Monty committed
1346 1347 1348 1349 1350
  LEX_CSTRING func_name_cstring() const override
  {
    static LEX_CSTRING sum_min_name= {STRING_WITH_LEN("bit_xor(") };
    return sum_min_name;
  }
1351 1352
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
1353
  { return get_item_copy<Item_sum_xor>(thd, this); }
1354 1355

private:
1356
  void set_bits_from_counters() override;
1357 1358
};

1359 1360 1361 1362 1363 1364 1365 1366 1367
class sp_head;
class sp_name;
class Query_arena;
struct st_sp_security_context;

/*
  Item_sum_sp handles STORED AGGREGATE FUNCTIONS

  Each Item_sum_sp represents a custom aggregate function. Inside the
1368
  function's body, we require at least one occurrence of FETCH GROUP NEXT ROW
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
  instruction. This cursor is what makes custom stored aggregates possible.

  During computation the function's add method is called. This in turn performs
  an execution of the function. The function will execute from the current
  function context (and instruction), if one exists, or from the start if not.
  See Item_sp for more details.

  Upon encounter of FETCH GROUP NEXT ROW instruction, the function will pause
  execution. We assume that the user has performed the necessary additions for
  a row, between two encounters of FETCH GROUP NEXT ROW.

  Example:
  create aggregate function f1(x INT) returns int
  begin
    declare continue handler for not found return s;
    declare s int default 0
    loop
      fetch group next row;
      set s = s + x;
    end loop;
  end

  The function will always stop after an encounter of FETCH GROUP NEXT ROW,
  except (!) on first encounter, as the value for the first row in the
  group is already set in the argument x. This behaviour is done so when
  a user writes a function, he should "logically" include FETCH GROUP NEXT ROW
  before any "add" instructions in the stored function. This means however that
1396
  internally, the first occurrence doesn't stop the function. See the
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410
  implementation of FETCH GROUP NEXT ROW for details as to how it happens.

  Either way, one should assume that after calling "Item_sum_sp::add()" that
  the values for that particular row have been added to the aggregation.

  To produce values for val_xxx methods we need an extra syntactic construct.
  We require a continue handler when "no more rows are available". val_xxx
  methods force a function return by executing the function again, while
  setting a server flag that no more rows have been found. This implies
  that val_xxx methods should only be called once per group however.

  Example:
  DECLARE CONTINUE HANDLER FOR NOT FOUND RETURN ret_val;
*/
1411
class Item_sum_sp :public Item_sum,
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
                   public Item_sp
{
 private:
  bool execute();

public:
  Item_sum_sp(THD *thd, Name_resolution_context *context_arg, sp_name *name,
              sp_head *sp);

  Item_sum_sp(THD *thd, Name_resolution_context *context_arg, sp_name *name,
              sp_head *sp, List<Item> &list);
1423
  Item_sum_sp(THD *thd, Item_sum_sp *item);
1424

1425
  enum Sumfunctype sum_func () const override
1426 1427 1428
  {
    return SP_AGGREGATE_FUNC;
  }
1429
  Field *create_field_for_create_select(MEM_ROOT *root, TABLE *table) override
1430
  {
1431
    return create_table_field_from_handler(root, table);
1432
  }
1433 1434
  bool fix_length_and_dec() override;
  bool fix_fields(THD *thd, Item **ref) override;
Monty's avatar
Monty committed
1435
  LEX_CSTRING func_name_cstring() const override;
1436 1437
  const Type_handler *type_handler() const override;
  bool add() override;
1438 1439

  /* val_xx functions */
1440
  longlong val_int() override
1441 1442 1443 1444 1445 1446
  {
    if(execute())
      return 0;
    return sp_result_field->val_int();
  }

1447
  double val_real() override
1448 1449 1450 1451 1452 1453
  {
    if(execute())
      return 0.0;
    return sp_result_field->val_real();
  }

1454
  my_decimal *val_decimal(my_decimal *dec_buf) override
1455 1456 1457 1458 1459 1460
  {
    if(execute())
      return NULL;
    return sp_result_field->val_decimal(dec_buf);
  }

1461
  bool val_native(THD *thd, Native *to) override
1462
  {
1463
    return (null_value= execute()) || sp_result_field->val_native(to);
1464 1465
  }

1466
  String *val_str(String *str) override
1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
  {
    String buf;
    char buff[20];
    buf.set(buff, 20, str->charset());
    buf.length(0);
    if (execute())
      return NULL;
    /*
      result_field will set buf pointing to internal buffer
      of the resul_field. Due to this it will change any time
      when SP is executed. In order to prevent occasional
      corruption of returned value, we make here a copy.
    */
    sp_result_field->val_str(&buf);
    str->copy(buf);
    return str;
  }
1484 1485 1486 1487 1488
  void reset_field() override{DBUG_ASSERT(0);}
  void update_field() override{DBUG_ASSERT(0);}
  void clear() override;
  void cleanup() override;
  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
1489 1490 1491
  {
    return execute() || sp_result_field->get_date(ltime, fuzzydate);
  }
1492 1493 1494 1495
  inline Field *get_sp_result_field()
  {
    return sp_result_field;
  }
1496
  Item *get_copy(THD *thd) override
1497
  { return get_item_copy<Item_sum_sp>(thd, this); }
1498
  Item *copy_or_same(THD *thd) override;
1499
};
1500

1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511
/* Items to get the value of a stored sum function */

class Item_sum_field :public Item
{
protected:
  Field *field;
public:
  Item_sum_field(THD *thd, Item_sum *item)
    :Item(thd), field(item->result_field)
  {
    name= item->name;
1512
    set_maybe_null();
1513 1514 1515 1516
    decimals= item->decimals;
    max_length= item->max_length;
    unsigned_flag= item->unsigned_flag;
  }
1517
  table_map used_tables() const override { return (table_map) 1L; }
1518
  Field *create_tmp_field_ex(MEM_ROOT *root, TABLE *table, Tmp_field_src *src,
1519
                             const Tmp_field_param *param) override
1520
  {
1521
    return create_tmp_field_ex_simple(root, table, src, param);
1522
  }
1523 1524
  void save_in_result_field(bool no_conversions) override { DBUG_ASSERT(0); }
  bool check_vcol_func_processor(void *arg) override
1525
  {
1526
    return mark_unsupported_function(name.str, arg, VCOL_IMPOSSIBLE);
1527
  }
1528
  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
1529
  {
1530
    return type_handler()->Item_get_date_with_warn(thd, this, ltime, fuzzydate);
1531
  }
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
};


class Item_avg_field :public Item_sum_field
{
protected:
  uint prec_increment;
public:
  Item_avg_field(THD *thd, Item_sum_avg *item)
   :Item_sum_field(thd, item), prec_increment(item->prec_increment)
  { }
1543 1544
  enum Type type() const override { return FIELD_AVG_ITEM; }
  bool is_null() override { update_null_value(); return null_value; }
1545 1546 1547 1548 1549 1550 1551 1552 1553
};


class Item_avg_field_double :public Item_avg_field
{
public:
  Item_avg_field_double(THD *thd, Item_sum_avg *item)
   :Item_avg_field(thd, item)
  { }
1554 1555 1556 1557 1558 1559 1560 1561 1562
  const Type_handler *type_handler() const override
  { return &type_handler_double; }
  longlong val_int() override { return val_int_from_real(); }
  my_decimal *val_decimal(my_decimal *dec) override
  { return val_decimal_from_real(dec); }
  String *val_str(String *str) override
  { return val_string_from_real(str); }
  double val_real() override;
  Item *get_copy(THD *thd) override
1563
  { return get_item_copy<Item_avg_field_double>(thd, this); }
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
};


class Item_avg_field_decimal :public Item_avg_field
{
  uint f_precision, f_scale, dec_bin_size;
public:
  Item_avg_field_decimal(THD *thd, Item_sum_avg *item)
   :Item_avg_field(thd, item),
    f_precision(item->f_precision),
    f_scale(item->f_scale),
    dec_bin_size(item->dec_bin_size)
  { }
1577 1578 1579
  const Type_handler *type_handler() const override
  { return &type_handler_newdecimal; }
  double val_real() override
1580 1581 1582
  {
    return VDec(this).to_double();
  }
1583
  longlong val_int() override
1584 1585 1586
  {
    return VDec(this).to_longlong(unsigned_flag);
  }
1587
  String *val_str(String *str) override
1588 1589 1590
  {
    return VDec(this).to_string_round(str, decimals);
  }
1591 1592
  my_decimal *val_decimal(my_decimal *) override;
  Item *get_copy(THD *thd) override
1593
  { return get_item_copy<Item_avg_field_decimal>(thd, this); }
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
};


class Item_variance_field :public Item_sum_field
{
  uint sample;
public:
  Item_variance_field(THD *thd, Item_sum_variance *item)
   :Item_sum_field(thd, item), sample(item->sample)
  { }
1604 1605 1606 1607
  enum Type type() const override {return FIELD_VARIANCE_ITEM; }
  double val_real() override;
  longlong val_int() override { return val_int_from_real(); }
  String *val_str(String *str) override
1608
  { return val_string_from_real(str); }
1609
  my_decimal *val_decimal(my_decimal *dec_buf) override
1610
  { return val_decimal_from_real(dec_buf); }
1611 1612 1613 1614
  bool is_null() override { update_null_value(); return null_value; }
  const Type_handler *type_handler() const override
  { return &type_handler_double; }
  Item *get_copy(THD *thd) override
1615
  { return get_item_copy<Item_variance_field>(thd, this); }
1616 1617 1618 1619 1620 1621 1622 1623 1624
};


class Item_std_field :public Item_variance_field
{
public:
  Item_std_field(THD *thd, Item_sum_std *item)
   :Item_variance_field(thd, item)
  { }
1625 1626 1627
  enum Type type() const override { return FIELD_STD_ITEM; }
  double val_real() override;
  Item *get_copy(THD *thd) override
1628
  { return get_item_copy<Item_std_field>(thd, this); }
1629 1630 1631
};


unknown's avatar
unknown committed
1632
/*
unknown's avatar
unknown committed
1633
  User defined aggregates
unknown's avatar
unknown committed
1634
*/
unknown's avatar
unknown committed
1635

unknown's avatar
unknown committed
1636 1637 1638 1639 1640 1641 1642 1643
#ifdef HAVE_DLOPEN

class Item_udf_sum : public Item_sum
{
protected:
  udf_handler udf;

public:
1644 1645
  Item_udf_sum(THD *thd, udf_func *udf_arg):
    Item_sum(thd), udf(udf_arg)
unknown's avatar
unknown committed
1646
  { quick_group=0; }
1647 1648
  Item_udf_sum(THD *thd, udf_func *udf_arg, List<Item> &list):
    Item_sum(thd, list), udf(udf_arg)
unknown's avatar
unknown committed
1649
  { quick_group=0;}
1650
  Item_udf_sum(THD *thd, Item_udf_sum *item)
unknown's avatar
unknown committed
1651 1652
    :Item_sum(thd, item), udf(item->udf)
  { udf.not_original= TRUE; }
Monty's avatar
Monty committed
1653 1654 1655 1656 1657
  LEX_CSTRING func_name_cstring() const override
  {
    const char *tmp= udf.name();
    return {tmp, strlen(tmp) };
  }
1658
  bool fix_fields(THD *thd, Item **ref) override
unknown's avatar
unknown committed
1659
  {
1660
    DBUG_ASSERT(fixed() == 0);
1661 1662 1663 1664

    if (init_sum_func_check(thd))
      return TRUE;

1665
    base_flags|= item_base_t::FIXED;
1666 1667 1668 1669 1670 1671
    /*
      We set const_item_cache to false in constructors.
      It can be later changed to "true", in a Item_sum::make_const() call.
      No make_const() calls should have happened so far.
    */
    DBUG_ASSERT(!const_item_cache);
1672 1673
    if (udf.fix_fields(thd, this, this->arg_count, this->args))
      return TRUE;
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
    /**
      The above call for udf.fix_fields() updates
      the Used_tables_and_const_cache part of "this" as if it was a regular
      non-aggregate UDF function and can change both const_item_cache and
      used_tables_cache members.
      - The used_tables_cache will be re-calculated in update_used_tables()
        which is called from check_sum_func() below. So we don't care about
        its current value.
      - The const_item_cache must stay "false" until a Item_sum::make_const()
        call happens, if ever. So we need to reset const_item_cache back to
        "false" here.
    */
    const_item_cache= false;
1687
    memcpy (orig_args, args, sizeof (Item *) * arg_count);
1688
    return check_sum_func(thd, ref);
unknown's avatar
unknown committed
1689
  }
1690
  enum Sumfunctype sum_func () const override { return UDF_SUM_FUNC; }
unknown's avatar
unknown committed
1691 1692
  virtual bool have_field_update(void) const { return 0; }

1693 1694 1695 1696 1697 1698 1699 1700 1701
  void clear() override;
  bool add() override;
  bool supports_removal() const override;
  void remove() override;
  void reset_field() override {};
  void update_field() override {}
  void cleanup() override;
  void print(String *str, enum_query_type query_type) override;
  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
1702
  {
1703
    return type_handler()->Item_get_date_with_warn(thd, this, ltime, fuzzydate);
1704
  }
unknown's avatar
unknown committed
1705 1706 1707 1708 1709 1710
};


class Item_sum_udf_float :public Item_udf_sum
{
 public:
1711 1712 1713 1714
  Item_sum_udf_float(THD *thd, udf_func *udf_arg):
    Item_udf_sum(thd, udf_arg) {}
  Item_sum_udf_float(THD *thd, udf_func *udf_arg, List<Item> &list):
    Item_udf_sum(thd, udf_arg, list) {}
1715
  Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
1716
    :Item_udf_sum(thd, item) {}
1717 1718 1719 1720 1721 1722 1723 1724 1725 1726
  longlong val_int() override { return val_int_from_real(); }
  double val_real() override;
  String *val_str(String*str) override;
  my_decimal *val_decimal(my_decimal *) override;
  const Type_handler *type_handler() const override
  { return &type_handler_double; }
  bool fix_length_and_dec() override
  { fix_num_length_and_dec(); return FALSE; }
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
1727
  { return get_item_copy<Item_sum_udf_float>(thd, this); }
unknown's avatar
unknown committed
1728 1729 1730 1731 1732 1733
};


class Item_sum_udf_int :public Item_udf_sum
{
public:
1734 1735 1736 1737
  Item_sum_udf_int(THD *thd, udf_func *udf_arg):
    Item_udf_sum(thd, udf_arg) {}
  Item_sum_udf_int(THD *thd, udf_func *udf_arg, List<Item> &list):
    Item_udf_sum(thd, udf_arg, list) {}
1738
  Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
1739
    :Item_udf_sum(thd, item) {}
1740 1741
  longlong val_int() override;
  double val_real() override
1742
  { DBUG_ASSERT(fixed()); return (double) Item_sum_udf_int::val_int(); }
1743 1744 1745
  String *val_str(String*str) override;
  my_decimal *val_decimal(my_decimal *) override;
  const Type_handler *type_handler() const override
1746
  {
1747 1748 1749
    if (unsigned_flag)
      return &type_handler_ulonglong;
    return &type_handler_slonglong;
1750
  }
1751 1752 1753
  bool fix_length_and_dec() override { decimals=0; max_length=21; return FALSE; }
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
1754
  { return get_item_copy<Item_sum_udf_int>(thd, this); }
unknown's avatar
unknown committed
1755 1756 1757 1758 1759 1760
};


class Item_sum_udf_str :public Item_udf_sum
{
public:
1761 1762 1763 1764
  Item_sum_udf_str(THD *thd, udf_func *udf_arg):
    Item_udf_sum(thd, udf_arg) {}
  Item_sum_udf_str(THD *thd, udf_func *udf_arg, List<Item> &list):
    Item_udf_sum(thd, udf_arg, list) {}
1765
  Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
1766
    :Item_udf_sum(thd, item) {}
1767 1768
  String *val_str(String *) override;
  double val_real() override
unknown's avatar
unknown committed
1769
  {
unknown's avatar
unknown committed
1770
    int err_not_used;
unknown's avatar
unknown committed
1771 1772 1773
    char *end_not_used;
    String *res;
    res=val_str(&str_value);
1774 1775
    return res ? res->charset()->strntod((char*) res->ptr(),res->length(),
			                 &end_not_used, &err_not_used) : 0.0;
unknown's avatar
unknown committed
1776
  }
1777
  longlong val_int() override
unknown's avatar
unknown committed
1778
  {
unknown's avatar
unknown committed
1779 1780 1781 1782 1783 1784 1785 1786 1787
    int err_not_used;
    char *end;
    String *res;
    CHARSET_INFO *cs;

    if (!(res= val_str(&str_value)))
      return 0;                                 /* Null value */
    cs= res->charset();
    end= (char*) res->ptr()+res->length();
1788
    return cs->strtoll10(res->ptr(), &end, &err_not_used);
unknown's avatar
unknown committed
1789
  }
1790 1791 1792 1793 1794 1795
  my_decimal *val_decimal(my_decimal *dec) override;
  const Type_handler *type_handler() const override
  { return string_type_handler(); }
  bool fix_length_and_dec() override;
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
1796
  { return get_item_copy<Item_sum_udf_str>(thd, this); }
unknown's avatar
unknown committed
1797 1798
};

unknown's avatar
unknown committed
1799 1800 1801 1802

class Item_sum_udf_decimal :public Item_udf_sum
{
public:
1803 1804 1805 1806
  Item_sum_udf_decimal(THD *thd, udf_func *udf_arg):
    Item_udf_sum(thd, udf_arg) {}
  Item_sum_udf_decimal(THD *thd, udf_func *udf_arg, List<Item> &list):
    Item_udf_sum(thd, udf_arg, list) {}
unknown's avatar
unknown committed
1807 1808
  Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
    :Item_udf_sum(thd, item) {}
1809
  String *val_str(String *str) override
1810 1811 1812
  {
    return VDec(this).to_string_round(str, decimals);
  }
1813
  double val_real() override
1814 1815 1816
  {
    return VDec(this).to_double();
  }
1817
  longlong val_int() override
1818 1819 1820
  {
    return VDec(this).to_longlong(unsigned_flag);
  }
1821 1822 1823 1824 1825 1826 1827
  my_decimal *val_decimal(my_decimal *) override;
  const Type_handler *type_handler() const override
  { return &type_handler_newdecimal; }
  bool fix_length_and_dec() override
  { fix_num_length_and_dec(); return FALSE; }
  Item *copy_or_same(THD* thd) override;
  Item *get_copy(THD *thd) override
1828
  { return get_item_copy<Item_sum_udf_decimal>(thd, this); }
unknown's avatar
unknown committed
1829 1830
};

unknown's avatar
unknown committed
1831 1832
#else /* Dummy functions to get sql_yacc.cc compiled */

1833
class Item_sum_udf_float :public Item_sum_double
unknown's avatar
unknown committed
1834 1835
{
 public:
1836
  Item_sum_udf_float(THD *thd, udf_func *udf_arg):
1837
    Item_sum_double(thd) {}
1838
  Item_sum_udf_float(THD *thd, udf_func *udf_arg, List<Item> &list):
1839
    Item_sum_double(thd) {}
1840
  Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
1841
    :Item_sum_double(thd, item) {}
unknown's avatar
unknown committed
1842
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
1843
  double val_real() { DBUG_ASSERT(fixed()); return 0.0; }
unknown's avatar
unknown committed
1844
  void clear() {}
unknown's avatar
unknown committed
1845
  bool add() { return 0; }  
1846
  void reset_field() { DBUG_ASSERT(0); };
unknown's avatar
unknown committed
1847
  void update_field() {}
unknown's avatar
unknown committed
1848 1849 1850
};


1851
class Item_sum_udf_int :public Item_sum_double
unknown's avatar
unknown committed
1852 1853
{
public:
1854
  Item_sum_udf_int(THD *thd, udf_func *udf_arg):
1855
    Item_sum_double(thd) {}
1856
  Item_sum_udf_int(THD *thd, udf_func *udf_arg, List<Item> &list):
1857
    Item_sum_double(thd) {}
1858
  Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
1859
    :Item_sum_double(thd, item) {}
unknown's avatar
unknown committed
1860
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
1861 1862
  longlong val_int() { DBUG_ASSERT(fixed()); return 0; }
  double val_real() { DBUG_ASSERT(fixed()); return 0; }
1863
  void clear() {}
unknown's avatar
unknown committed
1864
  bool add() { return 0; }  
1865
  void reset_field() { DBUG_ASSERT(0); };
unknown's avatar
unknown committed
1866
  void update_field() {}
unknown's avatar
unknown committed
1867 1868 1869
};


1870
class Item_sum_udf_decimal :public Item_sum_double
unknown's avatar
unknown committed
1871 1872
{
 public:
1873
  Item_sum_udf_decimal(THD *thd, udf_func *udf_arg):
1874
    Item_sum_double(thd) {}
1875
  Item_sum_udf_decimal(THD *thd, udf_func *udf_arg, List<Item> &list):
1876
    Item_sum_double(thd) {}
unknown's avatar
unknown committed
1877
  Item_sum_udf_decimal(THD *thd, Item_sum_udf_float *item)
1878
    :Item_sum_double(thd, item) {}
unknown's avatar
unknown committed
1879
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
1880 1881
  double val_real() { DBUG_ASSERT(fixed()); return 0.0; }
  my_decimal *val_decimal(my_decimal *) { DBUG_ASSERT(fixed()); return 0; }
unknown's avatar
unknown committed
1882 1883
  void clear() {}
  bool add() { return 0; }
1884
  void reset_field() { DBUG_ASSERT(0); };
unknown's avatar
unknown committed
1885 1886 1887 1888
  void update_field() {}
};


1889
class Item_sum_udf_str :public Item_sum_double
unknown's avatar
unknown committed
1890 1891
{
public:
1892
  Item_sum_udf_str(THD *thd, udf_func *udf_arg):
1893
    Item_sum_double(thd) {}
1894
  Item_sum_udf_str(THD *thd, udf_func *udf_arg, List<Item> &list):
1895
    Item_sum_double(thd) {}
1896
  Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
1897
    :Item_sum_double(thd, item) {}
1898
  String *val_str(String *)
1899 1900 1901
    { DBUG_ASSERT(fixed()); null_value=1; return 0; }
  double val_real() { DBUG_ASSERT(fixed()); null_value=1; return 0.0; }
  longlong val_int() { DBUG_ASSERT(fixed()); null_value=1; return 0; }
1902 1903
  bool fix_length_and_dec() override
  { base_flags|= item_base_t::MAYBE_NULL; max_length=0; return FALSE; }
unknown's avatar
unknown committed
1904
  enum Sumfunctype sum_func () const { return UDF_SUM_FUNC; }
1905
  void clear() {}
unknown's avatar
unknown committed
1906
  bool add() { return 0; }  
1907
  void reset_field() { DBUG_ASSERT(0); };
unknown's avatar
unknown committed
1908
  void update_field() {}
unknown's avatar
unknown committed
1909 1910 1911
};

#endif /* HAVE_DLOPEN */
1912

1913 1914 1915
C_MODE_START
int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
                                       const void* key2);
1916 1917
int group_concat_key_cmp_with_distinct_with_nulls(void* arg, const void* key1,
                                                  const void* key2);
1918 1919
int group_concat_key_cmp_with_order(void* arg, const void* key1,
                                    const void* key2);
1920 1921
int group_concat_key_cmp_with_order_with_nulls(void *arg, const void *key1,
                                               const void *key2);
1922 1923 1924 1925 1926
int dump_leaf_key(void* key_arg,
                  element_count count __attribute__((unused)),
                  void* item_arg);
C_MODE_END

1927 1928
class Item_func_group_concat : public Item_sum
{
1929
protected:
1930
  TMP_TABLE_PARAM *tmp_table_param;
unknown's avatar
unknown committed
1931 1932 1933 1934
  String result;
  String *separator;
  TREE tree_base;
  TREE *tree;
1935
  size_t tree_len;
1936
  Item **ref_pointer_array;
1937 1938 1939 1940 1941 1942 1943 1944 1945

  /**
     If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
     out duplicates. 
     @see Item_func_group_concat::setup
     @see Item_func_group_concat::add
     @see Item_func_group_concat::clear
   */
  Unique *unique_filter;
unknown's avatar
unknown committed
1946 1947 1948
  TABLE *table;
  ORDER **order;
  Name_resolution_context *context;
1949 1950 1951 1952
  /** The number of ORDER BY items. */
  uint arg_count_order;
  /** The number of selected items, aka the expr list. */
  uint arg_count_field;
Marc Alff's avatar
Marc Alff committed
1953
  uint row_count;
unknown's avatar
unknown committed
1954 1955 1956
  bool distinct;
  bool warning_for_row;
  bool always_null;
1957
  bool force_copy_fields;
1958 1959
  /** True if entire result of GROUP_CONCAT has been written to output buffer. */
  bool result_finalized;
1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
  /** Limits the rows in the result */
  Item *row_limit;
  /** Skips a particular number of rows in from the result*/
  Item *offset_limit;
  bool limit_clause;
  /* copy of the offset limit */
  ulonglong copy_offset_limit;
  /*copy of the row limit */
  ulonglong copy_row_limit;

unknown's avatar
unknown committed
1970 1971 1972 1973 1974
  /*
    Following is 0 normal object and pointer to original one for copy
    (to correctly free resources)
  */
  Item_func_group_concat *original;
unknown's avatar
unknown committed
1975

1976 1977 1978 1979 1980 1981
  /*
    Used by Item_func_group_concat and Item_func_json_arrayagg. The latter
    needs null values but the former doesn't.
  */
  bool add(bool exclude_nulls);

1982 1983
  friend int group_concat_key_cmp_with_distinct(void* arg, const void* key1,
                                                const void* key2);
1984 1985 1986
  friend int group_concat_key_cmp_with_distinct_with_nulls(void* arg,
                                                           const void* key1,
                                                           const void* key2);
1987 1988
  friend int group_concat_key_cmp_with_order(void* arg, const void* key1,
					     const void* key2);
1989 1990
  friend int group_concat_key_cmp_with_order_with_nulls(void *arg,
                                       const void *key1, const void *key2);
1991
  friend int dump_leaf_key(void* key_arg,
unknown's avatar
unknown committed
1992
                           element_count count __attribute__((unused)),
1993
			   void* item_arg);
unknown's avatar
unknown committed
1994

1995
  bool repack_tree(THD *thd);
unknown's avatar
unknown committed
1996

1997 1998 1999 2000 2001 2002
  /*
    Says whether the function should skip NULL arguments
    or add them to the result.
    Redefined in JSON_ARRAYAGG.
  */
  virtual bool skip_nulls() const { return true; }
2003 2004 2005 2006 2007
  virtual String *get_str_from_item(Item *i, String *tmp)
    { return i->val_str(tmp); }
  virtual String *get_str_from_field(Item *i, Field *f, String *tmp,
                                     const uchar *key, size_t offset)
    { return f->val_str(tmp, key + offset); }
2008 2009
  virtual void cut_max_length(String *result,
                              uint old_length, uint max_length) const;
2010 2011 2012 2013 2014 2015 2016 2017
public:
  // Methods used by ColumnStore
  bool get_distinct() const { return distinct; }
  uint get_count_field() const { return arg_count_field; }
  uint get_order_field() const { return arg_count_order; }
  const String* get_separator() const { return separator; }
  ORDER** get_order() const { return order; }

unknown's avatar
unknown committed
2018
public:
2019
  Item_func_group_concat(THD *thd, Name_resolution_context *context_arg,
unknown's avatar
unknown committed
2020
                         bool is_distinct, List<Item> *is_select,
2021 2022
                         const SQL_I_List<ORDER> &is_order, String *is_separator,
                         bool limit_clause, Item *row_limit, Item *offset_limit);
unknown's avatar
unknown committed
2023

unknown's avatar
unknown committed
2024
  Item_func_group_concat(THD *thd, Item_func_group_concat *item);
2025
  ~Item_func_group_concat();
2026
  void cleanup() override;
unknown's avatar
unknown committed
2027

2028
  enum Sumfunctype sum_func () const override {return GROUP_CONCAT_FUNC;}
Monty's avatar
Monty committed
2029 2030 2031 2032 2033
  LEX_CSTRING func_name_cstring() const override
  {
    static LEX_CSTRING sum_name= {STRING_WITH_LEN("group_concat(") };
    return sum_name;
  }
2034
  const Type_handler *type_handler() const override
2035
  {
2036
    if (too_big_for_varchar())
2037 2038
      return &type_handler_blob;
    return &type_handler_varchar;
2039
  }
2040 2041
  void clear() override;
  bool add() override
2042
  {
2043
    return add(skip_nulls());
2044
  }
2045 2046 2047 2048 2049 2050
  void reset_field() override { DBUG_ASSERT(0); }        // not used
  void update_field() override { DBUG_ASSERT(0); }       // not used
  bool fix_fields(THD *,Item **) override;
  bool setup(THD *thd) override;
  void make_unique() override;
  double val_real() override
2051
  {
2052 2053 2054 2055 2056 2057 2058
    int error;
    const char *end;
    String *res;
    if (!(res= val_str(&str_value)))
      return 0.0;
    end= res->ptr() + res->length();
    return (my_strtod(res->ptr(), (char**) &end, &error));
2059
  }
2060
  longlong val_int() override
2061
  {
unknown's avatar
unknown committed
2062 2063 2064
    String *res;
    char *end_ptr;
    int error;
2065 2066
    if (!(res= val_str(&str_value)))
      return (longlong) 0;
unknown's avatar
unknown committed
2067
    end_ptr= (char*) res->ptr()+ res->length();
2068
    return my_strtoll10(res->ptr(), &end_ptr, &error);
2069
  }
2070
  my_decimal *val_decimal(my_decimal *decimal_value) override
unknown's avatar
unknown committed
2071 2072 2073
  {
    return val_decimal_from_string(decimal_value);
  }
2074
  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) override
2075
  {
2076
    return get_date_from_string(thd, ltime, fuzzydate);
2077
  }
2078 2079 2080 2081 2082
  String *val_str(String *str) override;
  Item *copy_or_same(THD* thd) override;
  void no_rows_in_result() override {}
  void print(String *str, enum_query_type query_type) override;
  bool change_context_processor(void *cntx) override
unknown's avatar
unknown committed
2083
    { context= (Name_resolution_context *)cntx; return FALSE; }
2084
  Item *get_copy(THD *thd) override
2085
  { return get_item_copy<Item_func_group_concat>(thd, this); }
2086
  qsort_cmp2 get_comparator_function_for_distinct();
2087
  qsort_cmp2 get_comparator_function_for_order_by();
2088 2089 2090
  uchar* get_record_pointer();
  uint get_null_bytes();

2091
};
2092 2093

#endif /* ITEM_SUM_INCLUDED */