MySQL  8.0.22
Source Code Documentation
item_sum.h
Go to the documentation of this file.
1 #ifndef ITEM_SUM_INCLUDED
2 #define ITEM_SUM_INCLUDED
3 
4 /* Copyright (c) 2000, 2020, Oracle and/or its affiliates.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License, version 2.0,
8  as published by the Free Software Foundation.
9 
10  This program is also distributed with certain software (including
11  but not limited to OpenSSL) that is licensed under separate terms,
12  as designated in a particular file or component or in included license
13  documentation. The authors of MySQL hereby grant you an additional
14  permission to link the program and your derivative works with the
15  separately licensed software that they have included with MySQL.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License, version 2.0, for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25 
26 /* classes for sum functions */
27 
28 #include <sys/types.h>
29 
30 #include <climits>
31 #include <cmath>
32 #include <cstdio>
33 #include <map>
34 #include <memory>
35 #include <string>
36 #include <vector>
37 
38 #include "field_types.h" // enum_field_types
39 #include "m_ctype.h"
40 #include "m_string.h"
41 #include "my_alloc.h"
42 #include "my_compiler.h"
43 #include "my_dbug.h"
44 #include "my_inttypes.h"
45 #include "my_sys.h"
46 #include "my_table_map.h"
47 #include "my_time.h"
48 #include "my_tree.h" // TREE
50 #include "mysql_time.h"
51 #include "mysqld_error.h"
52 #include "sql/enum_query_type.h"
53 #include "sql/item.h" // Item_result_field
54 #include "sql/item_func.h" // Item_int_func
55 #include "sql/mem_root_array.h"
56 #include "sql/my_decimal.h"
57 #include "sql/parse_location.h" // POS
58 #include "sql/parse_tree_window.h" // PT_window
59 #include "sql/sql_base.h"
60 #include "sql/sql_const.h"
61 #include "sql/sql_list.h"
62 #include "sql/sql_udf.h" // udf_handler
63 #include "sql/thr_malloc.h" // THR_MALLOC
64 #include "sql/window_lex.h"
65 #include "sql_string.h"
66 #include "template_utils.h"
67 
68 class Field;
69 class Item_sum;
70 class Json_array;
71 class Json_object;
72 class Json_wrapper;
73 class PT_item_list;
74 class PT_order_list;
75 class SELECT_LEX;
76 class THD;
77 class Temp_table_param;
78 class Window;
79 struct ORDER;
80 struct Parse_context;
81 struct TABLE;
83 
84 /**
85  The abstract base class for the Aggregator_* classes.
86  It implements the data collection functions (setup/add/clear)
87  as either pass-through to the real functionality or
88  as collectors into an Unique (for distinct) structure.
89 
90  Note that update_field/reset_field are not in that
91  class, because they're simply not called when
92  GROUP BY/DISTINCT can be handled with help of index on grouped
93  fields (allow_group_via_temp_table is false);
94 */
95 
96 class Aggregator {
97  friend class Item_sum;
98  friend class Item_sum_sum;
99  friend class Item_sum_count;
100  friend class Item_sum_avg;
101 
102  /*
103  All members are protected as this class is not usable outside of an
104  Item_sum descendant.
105  */
106  protected:
107  /* the aggregate function class to act on */
109 
110  public:
111  Aggregator(Item_sum *arg) : item_sum(arg) {}
112  virtual ~Aggregator() {}
113 
115  virtual Aggregator_type Aggrtype() = 0;
116 
117  /**
118  Called before adding the first row.
119  Allocates and sets up the internal aggregation structures used,
120  e.g. the Unique instance used to calculate distinct.
121  */
122  virtual bool setup(THD *) = 0;
123 
124  /**
125  Called when we need to wipe out all the data from the aggregator :
126  all the values acumulated and all the state.
127  Cleans up the internal structures and resets them to their initial state.
128  */
129  virtual void clear() = 0;
130 
131  /**
132  Called when there's a new value to be aggregated.
133  Updates the internal state of the aggregator to reflect the new value.
134  */
135  virtual bool add() = 0;
136 
137  /**
138  Called when there are no more data and the final value is to be retrieved.
139  Finalises the state of the aggregator, so the final result can be retrieved.
140  */
141  virtual void endup() = 0;
142 
143  /** Decimal value of being-aggregated argument */
145  /** Floating point value of being-aggregated argument */
146  virtual double arg_val_real() = 0;
147  /**
148  NULLness of being-aggregated argument.
149 
150  @param use_null_value Optimization: to determine if the argument is NULL
151  we must, in the general case, call is_null() on it, which itself might
152  call val_*() on it, which might be costly. If you just have called
153  arg_val*(), you can pass use_null_value=true; this way, arg_is_null()
154  might avoid is_null() and instead do a cheap read of the Item's null_value
155  (updated by arg_val*()).
156  */
157  virtual bool arg_is_null(bool use_null_value) = 0;
158 };
159 
160 /**
161  Class Item_sum is the base class used for special expressions that SQL calls
162  'set functions'. These expressions are formed with the help of aggregate
163  functions such as SUM, MAX, GROUP_CONCAT etc.
164  Class Item_sum is also the base class for Window functions; the text below
165  first documents set functions, then window functions.
166 
167  GENERAL NOTES
168 
169  A set function cannot be used in all positions where expressions are accepted.
170  There are some quite explicable restrictions for the use of set functions.
171 
172  In the query:
173 
174  SELECT AVG(b) FROM t1 WHERE SUM(b) > 20 GROUP by a
175 
176  the set function AVG(b) is valid, while the usage of SUM(b) is invalid.
177  A WHERE condition must contain expressions that can be evaluated for each row
178  of the table. Yet the expression SUM(b) can be evaluated only for each group
179  of rows with the same value of column a.
180  In the query:
181 
182  SELECT AVG(b) FROM t1 WHERE c > 30 GROUP BY a HAVING SUM(b) > 20
183 
184  both set function expressions AVG(b) and SUM(b) are valid.
185 
186  We can say that in a query without nested selects an occurrence of a
187  set function in an expression of the SELECT list or/and in the HAVING
188  clause is valid, while in the WHERE clause, FROM clause or GROUP BY clause
189  it is invalid.
190 
191  The general rule to detect whether a set function is valid in a query with
192  nested subqueries is much more complicated.
193 
194  Consider the following query:
195 
196  SELECT t1.a FROM t1 GROUP BY t1.a
197  HAVING t1.a > ALL (SELECT t2.c FROM t2 WHERE SUM(t1.b) < t2.c).
198 
199  The set function SUM(b) is used here in the WHERE clause of the subquery.
200  Nevertheless it is valid since it is contained in the HAVING clause of the
201  outer query. The expression SUM(t1.b) is evaluated for each group defined
202  in the main query, not for groups of the subquery.
203 
204  The problem of finding the query where to aggregate a particular
205  set function is not so simple as it seems to be.
206 
207  In the query:
208  SELECT t1.a FROM t1 GROUP BY t1.a
209  HAVING t1.a > ALL(SELECT t2.c FROM t2 GROUP BY t2.c
210  HAVING SUM(t1.a) < t2.c)
211 
212  the set function can be evaluated in both the outer and the inner query block.
213  If we evaluate SUM(t1.a) for the outer query then we get the value of t1.a
214  multiplied by the cardinality of a group in table t1. In this case,
215  SUM(t1.a) is used as a constant value in each correlated subquery.
216  But SUM(t1.a) can also be evaluated for the inner query.
217  In this case t1.a will be a constant value for each correlated subquery and
218  summation is performed for each group of table t2.
219  (Here it makes sense to remind that the query
220 
221  SELECT c FROM t GROUP BY a HAVING SUM(1) < a
222 
223  is quite valid in our SQL).
224 
225  So depending on what query block we assign the set function to we
226  can get different results.
227 
228  The general rule to detect the query block Q where a set function will be
229  aggregated (evaluated) can be formulated as follows.
230 
231  Reference: SQL2011 @<set function specification@> syntax rules 6 and 7.
232 
233  Consider a set function S(E) where E is an expression which contains
234  column references C1, ..., Cn. Resolve all column references Ci against
235  the query block Qi containing the set function S(E). Let Q be the innermost
236  query block of all query blocks Qi. (It should be noted here that S(E)
237  in no way can be aggregated in the query block containing the subquery Q,
238  otherwise S(E) would refer to at least one unbound column reference).
239  If S(E) is used in a construct of Q where set functions are allowed then
240  we aggregate S(E) in Q.
241  Otherwise:
242  - if ANSI SQL mode is enabled (MODE_ANSI), then report an error.
243  - otherwise, look for the innermost query block containing S(E) of those
244  where usage of S(E) is allowed. The place of aggregation depends on which
245  clause the subquery is contained within; It will be different when
246  contained in a WHERE clause versus in the select list or in HAVING clause.
247 
248  Let's demonstrate how this rule is applied to the following queries.
249 
250  1. SELECT t1.a FROM t1 GROUP BY t1.a
251  HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
252  HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
253  HAVING SUM(t1.a+t2.b) < t3.c))
254  For this query the set function SUM(t1.a+t2.b) contains t1.a and t2.b
255  with t1.a defined in the outermost query, and t2.b defined for its
256  subquery. The set function is contained in the HAVING clause of the subquery
257  and can be evaluated in this subquery.
258 
259  2. SELECT t1.a FROM t1 GROUP BY t1.a
260  HAVING t1.a > ALL(SELECT t2.b FROM t2
261  WHERE t2.b > ALL (SELECT t3.c FROM t3 GROUP BY t3.c
262  HAVING SUM(t1.a+t2.b) < t3.c))
263  The set function SUM(t1.a+t2.b) is contained in the WHERE clause of the second
264  query block - the outermost query block where t1.a and t2.b are defined.
265  If we evaluate the function in this subquery we violate the context rules.
266  So we evaluate the function in the third query block (over table t3) where it
267  is used under the HAVING clause; if in ANSI SQL mode, an error is thrown.
268 
269  3. SELECT t1.a FROM t1 GROUP BY t1.a
270  HAVING t1.a > ALL(SELECT t2.b FROM t2
271  WHERE t2.b > ALL (SELECT t3.c FROM t3
272  WHERE SUM(t1.a+t2.b) < t3.c))
273  In this query, evaluation of SUM(t1.a+t2.b) is not valid neither in the second
274  nor in the third query block.
275 
276  Set functions can generally not be nested. In the query
277 
278  SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20
279 
280  the expression SUM(b) is not valid, even though it is contained inside
281  a HAVING clause.
282  However, it is acceptable in the query:
283 
284  SELECT t.1 FROM t1 GROUP BY t1.a HAVING SUM(t1.b) > 20.
285 
286  An argument of a set function does not have to be a simple column reference
287  as seen in examples above. This can be a more complex expression
288 
289  SELECT t1.a FROM t1 GROUP BY t1.a HAVING SUM(t1.b+1) > 20.
290 
291  The expression SUM(t1.b+1) has clear semantics in this context:
292  we sum up the values of t1.b+1 where t1.b varies for all values within a
293  group of rows that contain the same t1.a value.
294 
295  A set function for an outer query yields a constant value within a subquery.
296  So the semantics of the query
297 
298  SELECT t1.a FROM t1 GROUP BY t1.a
299  HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
300  HAVING AVG(t2.c+SUM(t1.b)) > 20)
301 
302  is still clear. For a group of rows with the same value for t1.a, calculate
303  the value of SUM(t1.b) as 's'. This value is substituted in the subquery:
304 
305  SELECT t2.c FROM t2 GROUP BY t2.c HAVING AVG(t2.c+s)
306 
307  By the same reason the following query with a subquery
308 
309  SELECT t1.a FROM t1 GROUP BY t1.a
310  HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
311  HAVING AVG(SUM(t1.b)) > 20)
312  is also valid.
313 
314  IMPLEMENTATION NOTES
315 
316  The member base_select contains a reference to the query block that the
317  set function is contained within.
318 
319  The member aggr_select contains a reference to the query block where the
320  set function is aggregated.
321 
322  The field max_aggr_level holds the maximum of the nest levels of the
323  unbound column references contained in the set function. A column reference
324  is unbound within a set function if it is not bound by any subquery
325  used as a subexpression in this function. A column reference is bound by
326  a subquery if it is a reference to the column by which the aggregation
327  of some set function that is used in the subquery is calculated.
328  For the set function used in the query
329 
330  SELECT t1.a FROM t1 GROUP BY t1.a
331  HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
332  HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
333  HAVING SUM(t1.a+t2.b) < t3.c))
334 
335  the value of max_aggr_level is equal to 1 since t1.a is bound in the main
336  query, and t2.b is bound by the first subquery whose nest level is 1.
337  Obviously a set function cannot be aggregated in a subquery whose
338  nest level is less than max_aggr_level. (Yet it can be aggregated in the
339  subqueries whose nest level is greater than max_aggr_level.)
340  In the query
341  SELECT t1.a FROM t1 HAVING AVG(t1.a+(SELECT MIN(t2.c) FROM t2))
342 
343  the value of the max_aggr_level for the AVG set function is 0 since
344  the reference t2.c is bound in the subquery.
345 
346  If a set function contains no column references (like COUNT(*)),
347  max_aggr_level is -1.
348 
349  The field 'max_sum_func_level' is to contain the maximum of the
350  nest levels of the set functions that are used as subexpressions of
351  the arguments of the given set function, but not aggregated in any
352  subquery within this set function. A nested set function s1 can be
353  used within set function s0 only if s1.max_sum_func_level <
354  s0.max_sum_func_level. Set function s1 is considered as nested
355  for set function s0 if s1 is not calculated in any subquery
356  within s0.
357 
358  A set function that is used as a subexpression in an argument of another
359  set function refers to the latter via the field 'in_sum_func'.
360 
361  The condition imposed on the usage of set functions are checked when
362  we traverse query subexpressions with the help of the recursive method
363  fix_fields. When we apply this method to an object of the class
364  Item_sum, first, on the descent, we call the method init_sum_func_check
365  that initialize members used at checking. Then, on the ascent, we
366  call the method check_sum_func that validates the set function usage
367  and reports an error if it is invalid.
368  The method check_sum_func serves to link the items for the set functions
369  that are aggregated in the containing query blocks. Circular chains of such
370  functions are attached to the corresponding SELECT_LEX structures
371  through the field inner_sum_func_list.
372 
373  Exploiting the fact that the members mentioned above are used in one
374  recursive function we could have allocated them on the thread stack.
375  Yet we don't do it now.
376 
377  It is assumed that the nesting level of subqueries does not exceed 63
378  (valid nesting levels are stored in a 64-bit bitmap called nesting_map).
379  The assumption is enforced in LEX::new_query().
380 
381  WINDOW FUNCTIONS
382 
383  Most set functions (e.g. SUM, COUNT, AVG) can also be used as window
384  functions. In that case, notable differences compared to set functions are:
385  - not using any Aggregator
386  - not supporting DISTINCT
387  - val_*() does more than returning the function's current value: it
388  first accumulates the function's argument into the function's
389  state. Execution (e.g. end_write_wf()) manipulates temporary tables which
390  contain input for WFs; each input row is passed to copy_funcs() which calls
391  the WF's val_*() to accumulate it.
392 */
393 
397 
398  friend class Aggregator_distinct;
399  friend class Aggregator_simple;
400 
401  protected:
402  /**
403  Aggregator class instance. Not set initially. Allocated only after
404  it is determined if the incoming data are already distinct.
405  */
407 
408  /**
409  If sum is a window function, this field contains the window.
410  */
412  /**
413  True if we have already resolved this window functions window reference.
414  Used in execution of prepared statement to avoid re-resolve.
415  */
417 
418  private:
419  /**
420  Used in making ROLLUP. Set for the ROLLUP copies of the original
421  Item_sum and passed to create_tmp_field() to cause it to work
422  over the temp table buffer that is referenced by
423  Item_result_field::result_field.
424  */
426 
427  /**
428  Indicates how the aggregate function was specified by the parser :
429  true if it was written as AGGREGATE(DISTINCT),
430  false if it was AGGREGATE()
431  */
433 
434  public:
435  bool has_force_copy_fields() const { return force_copy_fields; }
436  bool has_with_distinct() const { return with_distinct; }
437 
438  enum Sumfunctype {
439  COUNT_FUNC, // COUNT
440  COUNT_DISTINCT_FUNC, // COUNT (DISTINCT)
441  SUM_FUNC, // SUM
442  SUM_DISTINCT_FUNC, // SUM (DISTINCT)
443  AVG_FUNC, // AVG
444  AVG_DISTINCT_FUNC, // AVG (DISTINCT)
445  MIN_FUNC, // MIN
446  MAX_FUNC, // MAX
447  STD_FUNC, // STD/STDDEV/STDDEV_POP
448  VARIANCE_FUNC, // VARIANCE/VAR_POP and VAR_SAMP
449  SUM_BIT_FUNC, // BIT_AND, BIT_OR and BIT_XOR
450  UDF_SUM_FUNC, // user defined functions
451  GROUP_CONCAT_FUNC, // GROUP_CONCAT
452  JSON_AGG_FUNC, // JSON_ARRAYAGG and JSON_OBJECTAGG
453  ROW_NUMBER_FUNC, // Window functions
462  ROLLUP_SUM_SWITCHER_FUNC
463  };
464 
465  /**
466  @note most member variables below serve only for grouped aggregate
467  functions.
468  */
469 
470  /**
471  For a group aggregate which is aggregated into an outer query
472  block; none, or just the first or both cells may be non-zero. They are
473  filled with references to the group aggregate (for example if it is the
474  argument of a function; it is then a pointer to that function's args[i]
475  pointer).
476  */
477  Item **referenced_by[2];
478  Item_sum *next_sum; ///< next in the circular chain of registered objects
479  Item_sum *in_sum_func; ///< the containing set function if any
480  SELECT_LEX *base_select; ///< query block where function is placed
481  /**
482  For a group aggregate, query block where function is aggregated. For a
483  window function, nullptr, as such function is always aggregated in
484  base_select, as it mustn't contain any outer reference.
485  */
487  int8 max_aggr_level; ///< max level of unbound column references
488  int8
489  max_sum_func_level; ///< max level of aggregation for contained functions
490  bool allow_group_via_temp_table; ///< If incremental update of fields is
491  ///< supported.
492  /**
493  WFs are forbidden when resolving Item_sum; this member is used to restore
494  WF allowance status afterwards.
495  */
497 
498  protected:
500  /**
501  True means that this field has been evaluated during optimization.
502  When set, used_tables() returns zero and const_item() returns true.
503  The value must be reset to false after execution.
504  */
506  static ulonglong ram_limitation(THD *thd);
507 
508  public:
509  void mark_as_sum_func();
510  void mark_as_sum_func(SELECT_LEX *);
511 
512  Item_sum(const POS &pos, PT_window *w)
513  : super(pos),
514  super2(nullptr, 0),
515  m_window(w),
516  m_window_resolved(false),
517  next_sum(nullptr),
518  allow_group_via_temp_table(true),
519  used_tables_cache(0),
520  forced_const(false) {
521  init_aggregator();
522  }
523 
525  : super2(1),
526  m_window(nullptr),
527  m_window_resolved(false),
528  next_sum(nullptr),
529  allow_group_via_temp_table(true),
530  used_tables_cache(0),
531  forced_const(false) {
532  args[0] = a;
533  mark_as_sum_func();
534  init_aggregator();
535  }
536 
537  Item_sum(const POS &pos, Item *a, PT_window *w)
538  : super(pos),
539  super2(1),
540  m_window(w),
541  m_window_resolved(false),
542  next_sum(nullptr),
543  allow_group_via_temp_table(true),
544  used_tables_cache(0),
545  forced_const(false) {
546  args[0] = a;
547  init_aggregator();
548  }
549 
550  Item_sum(const POS &pos, Item *a, Item *b, PT_window *w)
551  : super(pos),
552  super2(2),
553  m_window(w),
554  m_window_resolved(false),
555  next_sum(nullptr),
556  allow_group_via_temp_table(true),
557  used_tables_cache(0),
558  forced_const(false) {
559  args[0] = a;
560  args[1] = b;
561  init_aggregator();
562  }
563 
564  Item_sum(const POS &pos, PT_item_list *opt_list, PT_window *w);
565 
566  /// Copy constructor, need to perform subqueries with temporary tables
567  Item_sum(THD *thd, const Item_sum *item);
568 
569  bool itemize(Parse_context *pc, Item **res) override;
570  Type type() const override { return SUM_FUNC_ITEM; }
571  virtual enum Sumfunctype sum_func() const = 0;
572 
573  // Differs only for Item_rollup_sum_switcher.
574  virtual enum Sumfunctype real_sum_func() const { return sum_func(); }
575 
576  /**
577  Resets the aggregate value to its default and aggregates the current
578  value of its attribute(s).
579  */
580  inline bool reset_and_add() {
581  aggregator_clear();
582  return aggregator_add();
583  }
584 
585  /*
586  Called when new group is started and results are being saved in
587  a temporary table. Similarly to reset_and_add() it resets the
588  value to its default and aggregates the value of its
589  attribute(s), but must also store it in result_field.
590  This set of methods (result_item(), reset_field, update_field()) of
591  Item_sum is used only if allow_group_via_temp_table is true. Otherwise
592  copy_or_same() is used to obtain a copy of this item.
593  */
594  virtual void reset_field() = 0;
595  /*
596  Called for each new value in the group, when temporary table is in use.
597  Similar to add(), but uses temporary table field to obtain current value,
598  Updated value is then saved in the field.
599  */
600  virtual void update_field() = 0;
601  virtual bool keep_field_type() const { return false; }
602  bool resolve_type(THD *) override;
603  virtual Item *result_item(Field *field) {
604  Item_field *item = new Item_field(field);
605  if (item == nullptr) return nullptr;
606  // Aggregated fields have no reference to an underlying table
607  DBUG_ASSERT(item->orig_db_name() == nullptr &&
608  item->orig_table_name() == nullptr);
609  // Break the connection to the original field since this is an aggregation
610  item->set_orig_field_name(nullptr);
611  return item;
612  }
613  table_map used_tables() const override {
614  return forced_const ? 0 : used_tables_cache;
615  }
616  void update_used_tables() override;
617  void fix_after_pullout(SELECT_LEX *parent_select,
618  SELECT_LEX *removed_select) override;
619  void add_used_tables_for_aggr_func();
620  bool is_null() override { return null_value; }
621  void make_const() {
622  // "forced_const" will make used_tables() return zero for this object
623  forced_const = true;
624  }
625  void print(const THD *thd, String *str,
626  enum_query_type query_type) const override;
627  void fix_num_length_and_dec();
628  bool eq(const Item *item, bool binary_cmp) const override;
629  /**
630  Mark an aggregate as having no rows.
631 
632  This function is called by the execution engine to assign 'NO ROWS
633  FOUND' value to an aggregate item, when the underlying result set
634  has no rows. Such value, in a general case, may be different from
635  the default value of the item after 'clear()': e.g. a numeric item
636  may be initialized to 0 by clear() and to NULL by
637  no_rows_in_result().
638  */
639  void no_rows_in_result() override {
640  set_aggregator(with_distinct ? Aggregator::DISTINCT_AGGREGATOR
642  aggregator_clear();
643  }
644  virtual void make_unique() { force_copy_fields = true; }
645  virtual Field *create_tmp_field(bool group, TABLE *table);
646 
647  /// argument used by walk method collect_grouped_aggregates ("cga")
649  /// accumulated all aggregates found
650  std::vector<Item_sum *> list;
651  std::set<Item_sum *> aggregates_that_were_hidden;
652  /**
653  The query block we walk from. All found aggregates must aggregate in
654  this; if some aggregate in outer query blocks, break off transformation.
655  */
656  SELECT_LEX *m_select{nullptr};
657  /// true: break off transformation
658  bool m_break_off{false};
659  Collect_grouped_aggregate_info(SELECT_LEX *select) : m_select(select) {}
660  };
661 
662  bool collect_grouped_aggregates(uchar *) override;
663  Item *replace_aggregate(uchar *) override;
664  bool collect_scalar_subqueries(uchar *) override;
665  bool collect_item_field_or_view_ref_processor(uchar *) override;
666 
667  bool walk(Item_processor processor, enum_walk walk, uchar *arg) override;
668  Item *transform(Item_transformer transformer, uchar *arg) override;
669  bool clean_up_after_removal(uchar *arg) override;
670  bool aggregate_check_group(uchar *arg) override;
671  bool aggregate_check_distinct(uchar *arg) override;
672  bool has_aggregate_ref_in_group_by(uchar *arg) override;
673  bool init_sum_func_check(THD *thd);
674  bool check_sum_func(THD *thd, Item **ref);
675 
676  virtual Item *get_arg(uint i) { return args[i]; }
677  virtual Item *set_arg(THD *thd, uint i, Item *new_val);
678  /// @todo delete this when we no longer support temporary transformations
679  Item **get_arg_ptr(uint i) { return &args[i]; }
680 
681  bool fix_fields(THD *thd, Item **ref) override;
682 
683  /* Initialization of distinct related members */
685  aggr = nullptr;
686  with_distinct = false;
687  force_copy_fields = false;
688  }
689 
690  /**
691  Called to initialize the aggregator.
692  */
693 
694  virtual bool aggregator_setup(THD *thd) { return aggr->setup(thd); }
695 
696  /**
697  Called to cleanup the aggregator.
698  */
699 
700  inline void aggregator_clear() { aggr->clear(); }
701 
702  /**
703  Called to add value to the aggregator.
704  */
705 
706  inline bool aggregator_add() { return aggr->add(); }
707 
708  /* stores the declared DISTINCT flag (from the parser) */
709  void set_distinct(bool distinct) {
710  with_distinct = distinct;
711  allow_group_via_temp_table = !with_distinct;
712  }
713 
714  /*
715  Set the type of aggregation : DISTINCT or not.
716 
717  May be called multiple times.
718  */
719 
720  virtual int set_aggregator(Aggregator::Aggregator_type aggregator);
721 
722  virtual void clear() = 0;
723  virtual bool add() = 0;
724  virtual bool setup(THD *) { return false; }
725 
726  /**
727  Only relevant for aggregates qua window functions. Checks semantics after
728  windows have been set up and checked. Window functions have specific
729  requirements on the window specifications. Used at resolution time.
730 
731  @param thd Current thread
732  @param select The current select
733  @param [out] reqs Holds collected requirements from this wf
734 
735  @returns true if error
736  */
737  virtual bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
739 
740  /**
741  Like check_wf_semantics1.
742  For checks which cannot be done in resolution phase (mostly those for
743  input parameters which can be '?' and must be >=0: value isn't known
744  before execution phase).
745  */
746  virtual bool check_wf_semantics2(
747  Window_evaluation_requirements *reqs MY_ATTRIBUTE((unused))) {
748  return false;
749  }
750 
751  void split_sum_func(THD *thd, Ref_item_array ref_item_array,
752  mem_root_deque<Item *> *fields) override;
753 
754  void cleanup() override;
755 
756  const Window *window() const { return down_cast<Window *>(m_window); }
757  bool reset_wf_state(uchar *arg) override;
758 
759  /**
760  All aggregates are framing, i.e. they work on the window's frame. If none
761  is defined, the frame is by default the entire partition, unless ORDER BY
762  is defined, in which case it is the set of rows from the start of the
763  partition to and including the peer set of the current row.
764 
765  Some window functions are not framing, i.e. they always work on the entire
766  partition. For such window functions, the method is overridden to
767  return false.
768  */
769  virtual bool framing() const { return true; }
770 
771  /**
772  Only for framing window functions. True if this function only needs to
773  read one row per frame.
774  */
775  virtual bool uses_only_one_row() const { return false; }
776 
777  /**
778  Return true if we need to make two passes over the rows in the partition -
779  either because we need the cardinality of it (and we need to read all
780  rows to detect the next partition), or we need to have all partition rows
781  available to evaluate the window function for some other reason, e.g.
782  we may need the last row in the partition in the frame buffer to be able
783  to evaluate LEAD.
784  */
785  virtual bool needs_card() const { return false; }
786 
787  /**
788  Common initial actions for window functions. For non-buffered processing
789  ("on-the-fly"), check partition change and possible reset partition
790  state. In this case return false.
791  For buffered processing, if windowing state m_do_copy_null is true, set
792  null_value to true and return true.
793 
794  @return true if case two above holds, else false
795  */
796  bool wf_common_init();
797 
798  protected:
799  /*
800  Raise an error (ER_NOT_SUPPORTED_YET) with the detail that this
801  function is not yet supported as a window function.
802  */
804  char buff[STRING_BUFFER_USUAL_SIZE];
805  snprintf(buff, sizeof(buff), "%s as window function", func_name());
806  my_error(ER_NOT_SUPPORTED_YET, MYF(0), buff);
807  }
808 };
809 
810 class Unique;
811 
812 /**
813  The distinct aggregator.
814  Implements AGGFN (DISTINCT ..)
815  Collects all the data into an Unique (similarly to what Item_sum_distinct
816  does currently) and then (if applicable) iterates over the list of
817  unique values and pumps them back into its object
818 */
819 
821  friend class Item_sum_sum;
822 
823  /*
824  flag to prevent consecutive runs of endup(). Normally in endup there are
825  expensive calculations (like walking the distinct tree for example)
826  which we must do only once if there are no data changes.
827  We can re-use the data for the second and subsequent val_xxx() calls.
828  endup_done set to true also means that the calculated values for
829  the aggregate functions are correct and don't need recalculation.
830  */
832 
833  /*
834  Used depending on the type of the aggregate function and the presence of
835  blob columns in it:
836  - For COUNT(DISTINCT) and no blob fields this points to a real temporary
837  table. It's used as a hash table.
838  - For AVG/SUM(DISTINCT) or COUNT(DISTINCT) with blob fields only the
839  in-memory data structure of a temporary table is constructed.
840  It's used by the Field classes to transform data into row format.
841  */
843 
844  /*
845  An array of field lengths on row allocated and used only for
846  COUNT(DISTINCT) with multiple columns and no blobs. Used in
847  Aggregator_distinct::composite_key_cmp (called from Unique to compare
848  nodes
849  */
851 
852  /*
853  Used in conjunction with 'table' to support the access to Field classes
854  for COUNT(DISTINCT). Needed by copy_fields()/copy_funcs().
855  */
857 
858  /*
859  If there are no blobs in the COUNT(DISTINCT) arguments, we can use a tree,
860  which is faster than heap table. In that case, we still use the table
861  to help get things set up, but we insert nothing in it.
862  For AVG/SUM(DISTINCT) we always use this tree (as it takes a single
863  argument) to get the distinct rows.
864  */
866 
867  /*
868  The length of the temp table row. Must be a member of the class as it
869  gets passed down to simple_raw_key_cmp () as a compare function argument
870  to Unique. simple_raw_key_cmp () is used as a fast comparison function
871  when the entire row can be binary compared.
872  */
874 
876  NOT_CONST = 0,
877  /**
878  Set to true if the result is known to be always NULL.
879  If set deactivates creation and usage of the temporary table (in the
880  'table' member) and the Unique instance (in the 'tree' member) as well as
881  the calculation of the final value on the first call to
882  @c Item_sum::val_xxx(),
883  @c Item_avg::val_xxx(),
884  @c Item_count::val_xxx().
885  */
887  /**
888  Set to true if count distinct is on only const items. Distinct on a const
889  value will always be the constant itself. And count distinct of the same
890  would always be 1. Similar to CONST_NULL, it avoids creation of temporary
891  table and the Unique instance.
892  */
893  CONST_NOT_NULL
894  } const_distinct;
895 
896  /**
897  When feeding back the data in endup() from Unique/temp table back to
898  Item_sum::add() methods we must read the data from Unique (and not
899  recalculate the functions that are given as arguments to the aggregate
900  function.
901  This flag is to tell the arg_*() methods to take the data from the Unique
902  instead of calling the relevant val_..() method.
903  */
905 
906  public:
908  : Aggregator(sum),
909  table(nullptr),
910  tmp_table_param(nullptr),
911  tree(nullptr),
912  const_distinct(NOT_CONST),
913  use_distinct_values(false) {}
914  ~Aggregator_distinct() override;
916 
917  bool setup(THD *) override;
918  void clear() override;
919  bool add() override;
920  void endup() override;
922  double arg_val_real() override;
923  bool arg_is_null(bool use_null_value) override;
924 
925  bool unique_walk_function(void *element);
926  static int composite_key_cmp(const void *arg, const void *a, const void *b);
927 };
928 
929 /**
930  The pass-through aggregator.
931  Implements AGGFN (DISTINCT ..) by knowing it gets distinct data on input.
932  So it just pumps them back to the Item_sum descendant class.
933 */
935  public:
938 
939  bool setup(THD *thd) override { return item_sum->setup(thd); }
940  void clear() override { item_sum->clear(); }
941  bool add() override { return item_sum->add(); }
942  void endup() override {}
944  double arg_val_real() override;
945  bool arg_is_null(bool use_null_value) override;
946 };
947 
948 class Item_sum_num : public Item_sum {
949  typedef Item_sum super;
950 
951  protected:
952  /*
953  val_xxx() functions may be called several times during the execution of a
954  query. Derived classes that require extensive calculation in val_xxx()
955  maintain cache of aggregate value. This variable governs the validity of
956  that cache.
957  */
959 
960  public:
961  Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
962  : Item_sum(pos, item_par, window), is_evaluated(false) {}
963 
965  : Item_sum(pos, list, w), is_evaluated(false) {}
966 
968  : Item_sum(thd, item), is_evaluated(item->is_evaluated) {}
970  return MYSQL_TYPE_DOUBLE;
971  }
972  bool fix_fields(THD *, Item **) override;
973  longlong val_int() override {
974  DBUG_ASSERT(fixed == 1);
975  return llrint_with_overflow_check(val_real()); /* Real as default */
976  }
977  String *val_str(String *str) override;
978  my_decimal *val_decimal(my_decimal *) override;
979  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
980  return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
981  }
982  bool get_time(MYSQL_TIME *ltime) override {
983  return get_time_from_numeric(ltime); /* Decimal or real */
984  }
985  void reset_field() override;
986 };
987 
988 class Item_sum_int : public Item_sum_num {
989  public:
990  Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
991  : Item_sum_num(pos, item_par, w) {
992  set_data_type_longlong();
993  }
994 
996  : Item_sum_num(pos, list, w) {
997  set_data_type_longlong();
998  }
999 
1000  Item_sum_int(THD *thd, Item_sum_int *item) : Item_sum_num(thd, item) {
1001  set_data_type_longlong();
1002  }
1003 
1004  double val_real() override {
1005  DBUG_ASSERT(fixed);
1006  return static_cast<double>(val_int());
1007  }
1008  String *val_str(String *str) override;
1009  my_decimal *val_decimal(my_decimal *) override;
1010  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1011  return get_date_from_int(ltime, fuzzydate);
1012  }
1013  bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
1014  enum Item_result result_type() const override { return INT_RESULT; }
1015 };
1016 
1017 class Item_sum_sum : public Item_sum_num {
1018  protected:
1020  double sum;
1021  my_decimal dec_buffs[2];
1023  bool resolve_type(THD *thd) override;
1024  /**
1025  Execution state: this is for counting rows entering and leaving the window
1026  frame, see #m_frame_null_count.
1027  */
1029 
1030  /**
1031  Execution state: this is for counting NULLs of rows entering and leaving
1032  the window frame, when we use optimized inverse-based computations. By
1033  comparison with m_count we can know how many non-NULLs are in the frame.
1034  */
1036 
1037  public:
1038  Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
1039  : Item_sum_num(pos, item_par, window),
1040  hybrid_type(INVALID_RESULT),
1041  m_count(0),
1042  m_frame_null_count(0) {
1043  set_distinct(distinct);
1044  }
1045 
1046  Item_sum_sum(THD *thd, Item_sum_sum *item);
1047  enum Sumfunctype sum_func() const override {
1048  return has_with_distinct() ? SUM_DISTINCT_FUNC : SUM_FUNC;
1049  }
1050  void clear() override;
1051  bool add() override;
1052  double val_real() override;
1053  longlong val_int() override;
1054  String *val_str(String *str) override;
1055  my_decimal *val_decimal(my_decimal *) override;
1056  enum Item_result result_type() const override { return hybrid_type; }
1057  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
1058  Window_evaluation_requirements *reqs) override;
1059  void reset_field() override;
1060  void update_field() override;
1061  void no_rows_in_result() override {}
1062  const char *func_name() const override { return "sum"; }
1063  Item *copy_or_same(THD *thd) override;
1064 };
1065 
1068 
1069  friend class Aggregator_distinct;
1070 
1071  void clear() override;
1072  bool add() override;
1073  void cleanup() override;
1074 
1075  public:
1076  Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
1077  : Item_sum_int(pos, item_par, w), count(0) {}
1078 
1079  /**
1080  Constructs an instance for COUNT(DISTINCT)
1081 
1082  @param pos Position of token in the parser.
1083  @param list A list of the arguments to the aggregate function
1084  @param w A window, if COUNT is used as a windowing function
1085 
1086  This constructor is called by the parser only for COUNT (DISTINCT).
1087  */
1088 
1090  : Item_sum_int(pos, list, w), count(0) {
1091  set_distinct(true);
1092  }
1094  : Item_sum_int(thd, item), count(item->count) {}
1095  enum Sumfunctype sum_func() const override {
1096  return has_with_distinct() ? COUNT_DISTINCT_FUNC : COUNT_FUNC;
1097  }
1098  bool resolve_type(THD *thd) override {
1099  if (param_type_is_default(thd, 0, -1)) return true;
1100  maybe_null = false;
1101  null_value = false;
1102  return false;
1103  }
1104  void no_rows_in_result() override { count = 0; }
1105  void make_const(longlong count_arg) {
1106  count = count_arg;
1108  }
1109  longlong val_int() override;
1110  void reset_field() override;
1111  void update_field() override;
1112  const char *func_name() const override { return "count"; }
1113  Item *copy_or_same(THD *thd) override;
1114 };
1115 
1116 /* Item to get the value of a stored sum function */
1117 
1118 class Item_sum_avg;
1119 class Item_sum_bit;
1120 
1121 /**
1122  This is used in connection which a parent Item_sum:
1123  - which can produce different result types (is "hybrid")
1124  - which stores function's value into a temporary table's column (one
1125  row per group).
1126  - which stores in the column some internal piece of information which should
1127  not be returned to the user, so special implementations are needed.
1128 */
1130  protected:
1131  /// The tmp table's column containing the value of the set function.
1133  /// Stores the Item's result type.
1135 
1136  public:
1137  enum Item_result result_type() const override { return hybrid_type; }
1138  bool mark_field_in_map(uchar *arg) override {
1139  /*
1140  Filesort (find_all_keys) over a temporary table collects the columns it
1141  needs.
1142  */
1143  return Item::mark_field_in_map(pointer_cast<Mark_field *>(arg), field);
1144  }
1148  func_arg->banned_function_name = func_name();
1149  return true;
1150  }
1151 };
1152 
1153 /**
1154  Common abstract class for:
1155  Item_avg_field
1156  Item_variance_field
1157 */
1159  public:
1160  longlong val_int() override {
1161  /* can't be fix_fields()ed */
1162  return llrint_with_overflow_check(val_real());
1163  }
1164  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1165  return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
1166  }
1167  bool get_time(MYSQL_TIME *ltime) override {
1168  return get_time_from_numeric(ltime); /* Decimal or real */
1169  }
1170  bool is_null() override { return update_null_value() || null_value; }
1171 };
1172 
1174  public:
1175  uint f_precision, f_scale, dec_bin_size;
1177  Item_avg_field(Item_result res_type, Item_sum_avg *item);
1178  enum Type type() const override { return FIELD_AVG_ITEM; }
1179  double val_real() override;
1180  my_decimal *val_decimal(my_decimal *) override;
1181  String *val_str(String *) override;
1182  bool resolve_type(THD *) override { return false; }
1183  const char *func_name() const override {
1184  DBUG_ASSERT(0);
1185  return "avg_field";
1186  }
1187 };
1188 
1189 /// This is used in connection with an Item_sum_bit, @see Item_sum_hybrid_field
1191  protected:
1193 
1194  public:
1196  ulonglong reset_bits);
1197  longlong val_int() override;
1198  double val_real() override;
1199  my_decimal *val_decimal(my_decimal *) override;
1200  String *val_str(String *) override;
1201  bool resolve_type(THD *) override { return false; }
1202  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1203  bool get_time(MYSQL_TIME *ltime) override;
1204  enum Type type() const override { return FIELD_BIT_ITEM; }
1205  const char *func_name() const override {
1206  DBUG_ASSERT(0);
1207  return "sum_bit_field";
1208  }
1209 };
1210 
1211 /// Common abstraction for Item_sum_json_array and Item_sum_json_object
1212 class Item_sum_json : public Item_sum {
1213  typedef Item_sum super;
1214 
1215  protected:
1216  /// String used when reading JSON binary values or JSON text values.
1218  /// String used for converting JSON text values to utf8mb4 charset.
1220  /// Wrapper around the container (object/array) which accumulates the value.
1222 
1223  /**
1224  Construct an Item_sum_json instance.
1225 
1226  @param wrapper a wrapper around the Json_array or Json_object that contains
1227  the aggregated result
1228  @param parent_args arguments to forward to Item_sum's constructor
1229  */
1230  template <typename... Args>
1232  Args &&... parent_args);
1233 
1234  public:
1235  ~Item_sum_json() override;
1236  bool fix_fields(THD *thd, Item **pItem) override;
1237  enum Sumfunctype sum_func() const override { return JSON_AGG_FUNC; }
1238  Item_result result_type() const override { return STRING_RESULT; }
1239 
1240  double val_real() override;
1241  longlong val_int() override;
1242  String *val_str(String *str) override;
1243  bool val_json(Json_wrapper *wr) override;
1244  my_decimal *val_decimal(my_decimal *decimal_buffer) override;
1245  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1246  bool get_time(MYSQL_TIME *ltime) override;
1247 
1248  void reset_field() override;
1249  void update_field() override;
1250 
1251  bool check_wf_semantics1(THD *, SELECT_LEX *,
1252  Window_evaluation_requirements *) override;
1253 };
1254 
1255 /// Implements aggregation of values into an array.
1257  /// Accumulates the final value.
1259 
1260  public:
1261  Item_sum_json_array(THD *thd, Item_sum *item,
1264  Item_sum_json_array(const POS &pos, Item *a, PT_window *w,
1267  ~Item_sum_json_array() override;
1268  const char *func_name() const override { return "json_arrayagg"; }
1269  void clear() override;
1270  bool add() override;
1271  Item *copy_or_same(THD *thd) override;
1272 };
1273 
1274 /// Implements aggregation of values into an object.
1276  /// Accumulates the final value.
1278  /// Buffer used to get the value of the key.
1280  /**
1281  Map of keys in Json_object and the count for each key
1282  within a window frame. It is used in handling rows
1283  leaving a window frame when rows are not sorted
1284  according to the key in Json_object.
1285  */
1286  std::map<std::string, int> m_key_map;
1287  /**
1288  If window provides ordering on the key in Json_object,
1289  a key_map is not needed to handle rows leaving a window
1290  frame. In this case, process_buffered_windowing_record()
1291  will set flags when a key/value pair can be removed from
1292  the Json_object.
1293  */
1294  bool m_optimize{false};
1295 
1296  public:
1297  Item_sum_json_object(THD *thd, Item_sum *item,
1300  Item_sum_json_object(const POS &pos, Item *a, Item *b, PT_window *w,
1303  ~Item_sum_json_object() override;
1304  const char *func_name() const override { return "json_objectagg"; }
1305  void clear() override;
1306  bool add() override;
1307  Item *copy_or_same(THD *thd) override;
1308  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
1309  Window_evaluation_requirements *reqs) override;
1310 };
1311 
1313  public:
1315  uint f_precision, f_scale, dec_bin_size;
1318  double m_avg;
1319 
1320  Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
1321  : Item_sum_sum(pos, item_par, distinct, w) {}
1322 
1324  : Item_sum_sum(thd, item), prec_increment(item->prec_increment) {}
1325 
1326  bool resolve_type(THD *thd) override;
1327  enum Sumfunctype sum_func() const override {
1328  return has_with_distinct() ? AVG_DISTINCT_FUNC : AVG_FUNC;
1329  }
1330  void clear() override;
1331  bool add() override;
1332  double val_real() override;
1333  // In SPs we might force the "wrong" type with select into a declare variable
1334  longlong val_int() override { return llrint_with_overflow_check(val_real()); }
1335  my_decimal *val_decimal(my_decimal *) override;
1336  String *val_str(String *str) override;
1337  void reset_field() override;
1338  void update_field() override;
1339  Item *result_item(Field *) override {
1340  return new Item_avg_field(hybrid_type, this);
1341  }
1342  void no_rows_in_result() override {}
1343  const char *func_name() const override { return "avg"; }
1344  Item *copy_or_same(THD *thd) override;
1345  Field *create_tmp_field(bool group, TABLE *table) override;
1346  void cleanup() override {
1347  m_count = 0;
1348  m_frame_null_count = 0;
1350  }
1351 };
1352 
1353 class Item_sum_variance;
1354 
1356  protected:
1358 
1359  public:
1361  enum Type type() const override { return FIELD_VARIANCE_ITEM; }
1362  double val_real() override;
1363  String *val_str(String *str) override { return val_string_from_real(str); }
1364  my_decimal *val_decimal(my_decimal *dec_buf) override {
1365  return val_decimal_from_real(dec_buf);
1366  }
1367  bool resolve_type(THD *) override { return false; }
1368  const char *func_name() const override {
1369  DBUG_ASSERT(0);
1370  return "variance_field";
1371  }
1375  func_arg->err_code = func_arg->get_unnamed_function_error_code();
1376  return true;
1377  }
1378 };
1379 
1380 /*
1381  variance(a) =
1382 
1383  = sum (ai - avg(a))^2 / count(a) )
1384  = sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
1385  = (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
1386  = (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
1387  = (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2
1388  )/count(a) = = (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a)
1389  )/count(a) = = (sum(ai^2) - sum(a)^2/count(a))/count(a)
1390 
1391  But, this falls prey to catastrophic cancellation.
1392  Instead, we use recurrence formulas in Algorithm I mentoned below
1393  for group aggregates.
1394 
1395  Algorithm I:
1396  M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
1397  S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
1398  for 2 <= k <= n newline
1399  ital variance = S_{n} / (n-1)
1400 
1401  For aggregate window functions algorithm I cannot be optimized for
1402  moving frames since M_{i} changes for every row. So we use the
1403  following algorithm.
1404 
1405  Algorithm II:
1406 
1407  K = 0
1408  n = 0
1409  ex = 0
1410  ex2 = 0
1411 
1412  def add_sample(x):
1413  if (n == 0):
1414  K = x
1415  n = n + 1
1416  ex += x - K
1417  ex2 += (x - K) * (x - K)
1418 
1419  def remove_sample(x):
1420  n = n - 1
1421  ex -= (x - K)
1422  ex2 -= (x - K) * (x - K)
1423 
1424  def variance():
1425  return (ex2 - (ex*ex)/n) / (n-1)
1426 
1427  This formula facilitates incremental computation enabling us to
1428  optimize in case of moving window frames. The optimized codepath is taken
1429  only when windowing_use_high_precision is set to false. By default,
1430  aggregate window functions take the non-optimized codepath.
1431  Note:
1432  Results could differ between optimized and non-optimized code path.
1433  Hence algorithm II is used only when user sets
1434  windowing_use_high_precision to false.
1435 */
1436 
1438  bool resolve_type(THD *) override;
1439 
1440  public:
1442  /**
1443  Used in recurrence relation.
1444  */
1445  double recurrence_m, recurrence_s;
1450  /**
1451  If set, uses a algorithm II mentioned in the class description
1452  to calculate the variance which helps in optimizing windowing
1453  functions in presence of frames.
1454  */
1455  bool optimize;
1456 
1457  Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg,
1458  PT_window *w)
1459  : Item_sum_num(pos, item_par, w),
1460  hybrid_type(REAL_RESULT),
1461  count(0),
1462  sample(sample_arg),
1463  optimize(false) {}
1464 
1466  enum Sumfunctype sum_func() const override { return VARIANCE_FUNC; }
1467  void clear() override;
1468  bool add() override;
1469  double val_real() override;
1470  my_decimal *val_decimal(my_decimal *) override;
1471  void reset_field() override;
1472  void update_field() override;
1473  Item *result_item(Field *) override { return new Item_variance_field(this); }
1474  void no_rows_in_result() override {}
1475  const char *func_name() const override {
1476  return sample ? "var_samp" : "variance";
1477  }
1478  Item *copy_or_same(THD *thd) override;
1479  Field *create_tmp_field(bool group, TABLE *table) override;
1480  enum Item_result result_type() const override { return REAL_RESULT; }
1481  void cleanup() override {
1482  count = 0;
1484  }
1485  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
1486  Window_evaluation_requirements *reqs) override;
1487 };
1488 
1489 class Item_sum_std;
1490 
1492  public:
1494  enum Type type() const override { return FIELD_STD_ITEM; }
1495  double val_real() override;
1496  my_decimal *val_decimal(my_decimal *) override;
1497  enum Item_result result_type() const override { return REAL_RESULT; }
1498  const char *func_name() const override {
1499  DBUG_ASSERT(0);
1500  return "std_field";
1501  }
1505  func_arg->err_code = func_arg->get_unnamed_function_error_code();
1506  return true;
1507  }
1508 };
1509 
1510 /*
1511  standard_deviation(a) = sqrt(variance(a))
1512 */
1513 
1515  public:
1516  Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
1517  : Item_sum_variance(pos, item_par, sample_arg, w) {}
1518 
1519  Item_sum_std(THD *thd, Item_sum_std *item) : Item_sum_variance(thd, item) {}
1520  enum Sumfunctype sum_func() const override { return STD_FUNC; }
1521  double val_real() override;
1522  Item *result_item(Field *) override { return new Item_std_field(this); }
1523  const char *func_name() const override {
1524  return sample ? "stddev_samp" : "std";
1525  }
1526  Item *copy_or_same(THD *thd) override;
1527  enum Item_result result_type() const override { return REAL_RESULT; }
1528 };
1529 
1530 // This class is a string or number function depending on num_func
1531 class Arg_comparator;
1532 
1533 /**
1534  Abstract base class for the MIN and MAX aggregate functions.
1535 */
1536 class Item_sum_hybrid : public Item_sum {
1537  typedef Item_sum super;
1538 
1539  private:
1540  /**
1541  Tells if this is the MIN function (true) or the MAX function (false).
1542  */
1543  const bool m_is_min;
1544  /*
1545  For window functions MIN/MAX with optimized code path, no comparisons
1546  are needed beyond NULL detection: MIN/MAX are then roughly equivalent to
1547  FIRST/LAST_VALUE. For this case, 'value' is the value of
1548  the window function a priori taken from args[0], while arg_cache is used to
1549  remember the value from the previous row. NULLs need a bit of careful
1550  treatment.
1551  */
1552  Item_cache *value, *arg_cache;
1555  bool was_values; // Set if we have found at least one row (for max/min only)
1556  /**
1557  Set to true if the window is ordered ascending.
1558  */
1560  /**
1561  Set to true when min/max can be optimized using window's ordering.
1562  */
1564  /**
1565  For min() - Set to true when results are ordered in ascending and
1566  false when descending.
1567  For max() - Set to true when results are ordered in descending and
1568  false when ascending.
1569  Valid only when m_optimize is true.
1570  */
1571  bool m_want_first; ///< Want first non-null value, else last non_null value
1572  /**
1573  Execution state: keeps track if this is the first row in the frame
1574  when buffering is not needed.
1575  Valid only when m_optimize is true.
1576  */
1578 
1579  /**
1580  Execution state: keeps track of at which row we saved a non-null last
1581  value.
1582  */
1584 
1585  /**
1586  This function implements the optimized version of retrieving min/max
1587  value. When we have "ordered ASC" results in a window, min will always
1588  be the first value in the result set (neglecting the NULL's) and max
1589  will always be the last value (or the other way around, if ordered DESC).
1590  It is based on the implementation of FIRST_VALUE/LAST_VALUE, except for
1591  the NULL handling.
1592 
1593  @return true if computation yielded a NULL or error
1594  */
1595  bool compute();
1596 
1597  /**
1598  MIN/MAX function setup.
1599 
1600  Setup cache/comparator of MIN/MAX functions. When called by the
1601  copy_or_same() function, the value_arg parameter contains the calculated
1602  value of the original MIN/MAX object, and it is saved in this object's
1603  cache.
1604 
1605  @param item the argument of the MIN/MAX function
1606  @param value_arg the calculated value of the MIN/MAX function
1607  @return false on success, true on error
1608  */
1609  bool setup_hybrid(Item *item, Item *value_arg);
1610 
1611  /** Create a clone of this object. */
1612  virtual Item_sum_hybrid *clone_hybrid(THD *thd) const = 0;
1613 
1614  protected:
1615  Item_sum_hybrid(Item *item_par, bool is_min)
1616  : Item_sum(item_par),
1617  m_is_min(is_min),
1618  value(nullptr),
1619  arg_cache(nullptr),
1620  cmp(nullptr),
1621  hybrid_type(INT_RESULT),
1622  was_values(true),
1623  m_nulls_first(false),
1624  m_optimize(false),
1625  m_want_first(false),
1626  m_cnt(0),
1627  m_saved_last_value_at(0) {
1628  collation.set(&my_charset_bin);
1629  }
1630 
1631  Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
1632  : Item_sum(pos, item_par, w),
1633  m_is_min(is_min),
1634  value(nullptr),
1635  arg_cache(nullptr),
1636  cmp(nullptr),
1637  hybrid_type(INT_RESULT),
1638  was_values(true),
1639  m_nulls_first(false),
1640  m_optimize(false),
1641  m_want_first(false),
1642  m_cnt(0),
1643  m_saved_last_value_at(0) {
1644  collation.set(&my_charset_bin);
1645  }
1646 
1648  : Item_sum(thd, item),
1649  m_is_min(item->m_is_min),
1650  value(item->value),
1651  arg_cache(nullptr),
1652  hybrid_type(item->hybrid_type),
1653  was_values(item->was_values),
1654  m_nulls_first(item->m_nulls_first),
1655  m_optimize(item->m_optimize),
1656  m_want_first(item->m_want_first),
1657  m_cnt(item->m_cnt),
1658  m_saved_last_value_at(0) {}
1659 
1660  public:
1661  bool fix_fields(THD *, Item **) override;
1662  void clear() override;
1663  void split_sum_func(THD *thd, Ref_item_array ref_item_array,
1664  mem_root_deque<Item *> *fields) override;
1665  double val_real() override;
1666  longlong val_int() override;
1667  longlong val_time_temporal() override;
1668  longlong val_date_temporal() override;
1669  my_decimal *val_decimal(my_decimal *) override;
1670  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1671  bool get_time(MYSQL_TIME *ltime) override;
1672  void reset_field() override;
1673  String *val_str(String *) override;
1674  bool val_json(Json_wrapper *wr) override;
1675  bool keep_field_type() const override { return true; }
1676  enum Item_result result_type() const override { return hybrid_type; }
1677  void update_field() override;
1678  void cleanup() override;
1679  bool any_value() { return was_values; }
1680  void no_rows_in_result() override;
1681  Field *create_tmp_field(bool group, TABLE *table) override;
1682  bool uses_only_one_row() const override { return m_optimize; }
1683  bool add() override;
1684  Item *copy_or_same(THD *thd) override;
1685  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
1686  Window_evaluation_requirements *r) override;
1687 
1688  private:
1689  /*
1690  These functions check if the value on the current row exceeds the maximum or
1691  minimum value seen so far, and update the current max/min stored in
1692  result_field, if needed.
1693  */
1694  void min_max_update_str_field();
1695  void min_max_update_temporal_field();
1696  void min_max_update_json_field();
1697  void min_max_update_real_field();
1698  void min_max_update_int_field();
1699  void min_max_update_decimal_field();
1700 };
1701 
1703  public:
1704  Item_sum_min(Item *item_par) : Item_sum_hybrid(item_par, true) {}
1705  Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
1706  : Item_sum_hybrid(pos, item_par, true, w) {}
1707  Item_sum_min(THD *thd, const Item_sum_min *item)
1708  : Item_sum_hybrid(thd, item) {}
1709  enum Sumfunctype sum_func() const override { return MIN_FUNC; }
1710  const char *func_name() const override { return "min"; }
1711 
1712  private:
1713  Item_sum_min *clone_hybrid(THD *thd) const override;
1714 };
1715 
1717  public:
1718  Item_sum_max(Item *item_par) : Item_sum_hybrid(item_par, false) {}
1719  Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
1720  : Item_sum_hybrid(pos, item_par, false, w) {}
1721  Item_sum_max(THD *thd, const Item_sum_max *item)
1722  : Item_sum_hybrid(thd, item) {}
1723  enum Sumfunctype sum_func() const override { return MAX_FUNC; }
1724  const char *func_name() const override { return "max"; }
1725 
1726  private:
1727  Item_sum_max *clone_hybrid(THD *thd) const override;
1728 };
1729 
1730 /**
1731  Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
1732 
1733  Each of them is both a set function and a framing window function.
1734 */
1735 class Item_sum_bit : public Item_sum {
1736  typedef Item_sum super;
1737  /// Stores the neutral element for function
1739  /// Stores the result value for the INT_RESULT
1741  /// Stores the result value for the STRING_RESULT
1743  /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1745  /// Buffer used to avoid String allocation in the constructor
1746  const char initial_value_buff_storage[1] = {0};
1747 
1748  /**
1749  Execution state (windowing): this is for counting rows entering and leaving
1750  the window frame, see #m_frame_null_count.
1751  */
1753 
1754  /**
1755  Execution state (windowing): this is for counting NULLs of rows entering
1756  and leaving the window frame, when we use optimized inverse-based
1757  computations. By comparison with m_count we can know how many non-NULLs are
1758  in the frame.
1759  */
1761 
1762  /**
1763  Execution state (windowing): Used for AND, OR to be able to invert window
1764  functions in optimized mode.
1765 
1766  For the optimized code path of BIT_XXX wfs, we keep track of the number of
1767  bit values (0's or 1's; see below) seen in a frame using a 64 bits counter
1768  pr bit. This lets us compute the value of OR by just inspecting:
1769 
1770  - the number of 1's in the previous frame
1771  - whether any removed row(s) is a 1
1772  - whether any added row(s) is a 1
1773 
1774  Similarly for AND, we keep track of the number of 0's seen for a particular
1775  bit. To do this trick we need a counter per bit position. This array holds
1776  these counters.
1777 
1778  Note that for XOR, the inverse operation is identical to the operation,
1779  so we don't need the above.
1780  */
1782  /*
1783  Size of allocated array m_digit_cnt.
1784  The size is DIGIT_CNT_CARD (for integer types) or ::max_length * 8 for bit
1785  strings.
1786  */
1788 
1789  static constexpr uint DIGIT_CNT_CARD = sizeof(ulonglong) * 8;
1790 
1791  protected:
1792  bool m_is_xor; ///< true iff BIT_XOR
1793 
1794  public:
1795  Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg,
1796  PT_window *w)
1797  : Item_sum(pos, item_par, w),
1798  reset_bits(reset_arg),
1799  bits(reset_arg),
1800  value_buff(initial_value_buff_storage, 1, &my_charset_bin),
1801  m_count(0),
1802  m_frame_null_count(0),
1803  m_digit_cnt(nullptr),
1804  m_digit_cnt_card(0),
1805  m_is_xor(false) {}
1806 
1807  /// Copy constructor, used for executing subqueries with temporary tables
1809  : Item_sum(thd, item),
1810  reset_bits(item->reset_bits),
1811  bits(item->bits),
1812  value_buff(initial_value_buff_storage, 1, &my_charset_bin),
1813  hybrid_type(item->hybrid_type),
1814  m_count(item->m_count),
1815  m_frame_null_count(item->m_frame_null_count),
1816  m_digit_cnt(nullptr),
1817  m_digit_cnt_card(0),
1818  m_is_xor(item->m_is_xor) {
1819  /*
1820  This constructor should only be called during the Optimize stage.
1821  Asserting that the item was not evaluated yet.
1822  */
1823  DBUG_ASSERT(item->value_buff.length() == 1);
1824  DBUG_ASSERT(item->bits == item->reset_bits);
1825  }
1826 
1827  Item *result_item(Field *) override {
1828  return new Item_sum_bit_field(hybrid_type, this, reset_bits);
1829  }
1830 
1831  enum Sumfunctype sum_func() const override { return SUM_BIT_FUNC; }
1832  enum Item_result result_type() const override { return hybrid_type; }
1833  void clear() override;
1834  longlong val_int() override;
1835  double val_real() override;
1836  String *val_str(String *str) override;
1837  my_decimal *val_decimal(my_decimal *decimal_value) override;
1838  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1839  bool get_time(MYSQL_TIME *ltime) override;
1840  void reset_field() override;
1841  void update_field() override;
1842  bool resolve_type(THD *) override;
1843  bool fix_fields(THD *thd, Item **ref) override;
1844  void cleanup() override {
1845  bits = reset_bits;
1846  // At end of one execution of statement, free buffer to reclaim memory:
1847  value_buff.set(initial_value_buff_storage, 1, &my_charset_bin);
1849  }
1850 
1851  /**
1852  Common implementation of Item_sum_or::add, Item_sum_and:add
1853  and Item_sum_xor::add.
1854  */
1855  bool add() override;
1856  /// @returns true iff this is BIT_AND.
1857  inline bool is_and() const { return reset_bits != 0; }
1858 
1859  private:
1860  /**
1861  Accumulate the value of 's1' (if in string mode) or of 'b1' (if in integer
1862  mode). Updates 'value_buff' or 'bits'.
1863 
1864  @param s1 argument to accumulate
1865  @param b1 argument to accumulate
1866 
1867  @returns true if error
1868  */
1869  bool add_bits(const String *s1, ulonglong b1);
1870 
1871  /**
1872  For windowing: perform inverse aggregation. "De-accumulate" the value of
1873  's1' (if in string mode) or of 'b1' (if in integer mode). Updates
1874  'value_buff' or 'bits'.
1875 
1876  For BIT_XOR we simply apply XOR as it's its inverse operation. For BIT_OR
1877  and BIT_AND we do the rest below.
1878 
1879  For each bit in argument, decrement the corresponding bits's counter
1880  ('m_digit_cnt') for that bit as follows: for BIT_AND, decrement the
1881  counter if we see a zero in that bit; for BIT_OR decrement the counter if
1882  we see a 1 in that bit. Next, update 'value_buff' or 'bits' using the
1883  resulting counters: for each bit, for BIT_AND, set bit if we have counter
1884  == 0, i.e. we have no zero bits for that bit in the frame (yet). For
1885  BIT_OR, set bit if we have counter > 0, i.e. at least one row in the frame
1886  has that bit set.
1887 
1888  @param s1 the bits to be inverted from the aggregate value
1889  @param b1 the bits to be inverted from the aggregate value
1890  */
1891  void remove_bits(const String *s1, ulonglong b1);
1892 };
1893 
1895  public:
1896  Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
1897  : Item_sum_bit(pos, item_par, 0LL, w) {}
1898 
1899  Item_sum_or(THD *thd, Item_sum_or *item) : Item_sum_bit(thd, item) {}
1900  const char *func_name() const override { return "bit_or"; }
1901  Item *copy_or_same(THD *thd) override;
1902 };
1903 
1905  public:
1906  Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
1907  : Item_sum_bit(pos, item_par, ULLONG_MAX, w) {}
1908 
1909  Item_sum_and(THD *thd, Item_sum_and *item) : Item_sum_bit(thd, item) {}
1910  const char *func_name() const override { return "bit_and"; }
1911  Item *copy_or_same(THD *thd) override;
1912 };
1913 
1915  public:
1916  Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
1917  : Item_sum_bit(pos, item_par, 0LL, w) {
1918  m_is_xor = true;
1919  }
1920 
1921  Item_sum_xor(THD *thd, Item_sum_xor *item) : Item_sum_bit(thd, item) {}
1922  const char *func_name() const override { return "bit_xor"; }
1923  Item *copy_or_same(THD *thd) override;
1924 };
1925 
1926 /*
1927  User defined aggregates
1928 */
1929 
1930 class Item_udf_sum : public Item_sum {
1931  typedef Item_sum super;
1932 
1933  protected:
1935 
1936  public:
1937  Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1938  : Item_sum(pos, opt_list, nullptr), udf(udf_arg) {
1939  allow_group_via_temp_table = false;
1940  }
1942  : Item_sum(thd, item), udf(item->udf) {
1943  udf.m_original = false;
1944  }
1945  ~Item_udf_sum() override {
1946  if (udf.is_initialized()) udf.free_handler();
1947  }
1948 
1949  bool itemize(Parse_context *pc, Item **res) override;
1950  const char *func_name() const override { return udf.name(); }
1951  bool fix_fields(THD *thd, Item **ref) override {
1952  DBUG_ASSERT(fixed == 0);
1953 
1954  if (init_sum_func_check(thd)) return true;
1955 
1956  fixed = true;
1957  if (udf.fix_fields(thd, this, this->arg_count, this->args)) return true;
1958 
1959  return check_sum_func(thd, ref);
1960  }
1961  enum Sumfunctype sum_func() const override { return UDF_SUM_FUNC; }
1962 
1963  void clear() override;
1964  bool add() override;
1965  void reset_field() override {}
1966  void update_field() override {}
1967  void cleanup() override;
1968  void print(const THD *thd, String *str,
1969  enum_query_type query_type) const override;
1970 };
1971 
1973  public:
1974  Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1975  : Item_udf_sum(pos, udf_arg, opt_list) {}
1977  : Item_udf_sum(thd, item) {}
1978  longlong val_int() override {
1979  DBUG_ASSERT(fixed == 1);
1980  return (longlong)rint(Item_sum_udf_float::val_real());
1981  }
1982  double val_real() override;
1983  String *val_str(String *str) override;
1984  my_decimal *val_decimal(my_decimal *) override;
1985  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1986  return get_date_from_real(ltime, fuzzydate);
1987  }
1988  bool get_time(MYSQL_TIME *ltime) override {
1989  return get_time_from_real(ltime);
1990  }
1991  bool resolve_type(THD *) override {
1992  set_data_type(MYSQL_TYPE_DOUBLE);
1993  fix_num_length_and_dec();
1994  return false;
1995  }
1996  Item *copy_or_same(THD *thd) override;
1997 };
1998 
2000  public:
2001  Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2002  : Item_udf_sum(pos, udf_arg, opt_list) {}
2004  : Item_udf_sum(thd, item) {}
2005  longlong val_int() override;
2006  double val_real() override {
2007  DBUG_ASSERT(fixed == 1);
2008  return (double)Item_sum_udf_int::val_int();
2009  }
2010  String *val_str(String *str) override;
2011  my_decimal *val_decimal(my_decimal *) override;
2012  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2013  return get_date_from_int(ltime, fuzzydate);
2014  }
2015  bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2016  enum Item_result result_type() const override { return INT_RESULT; }
2017  bool resolve_type(THD *) override {
2018  set_data_type_longlong();
2019  return false;
2020  }
2021  Item *copy_or_same(THD *thd) override;
2022 };
2023 
2025  public:
2026  Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2027  : Item_udf_sum(pos, udf_arg, opt_list) {}
2029  : Item_udf_sum(thd, item) {}
2030  String *val_str(String *) override;
2031  double val_real() override {
2032  int err_not_used;
2033  const char *end_not_used;
2034  String *res;
2035  res = val_str(&str_value);
2036  return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2037  &end_not_used, &err_not_used)
2038  : 0.0;
2039  }
2040  longlong val_int() override {
2041  int err_not_used;
2042  String *res;
2043  const CHARSET_INFO *cs;
2044 
2045  if (!(res = val_str(&str_value))) return 0; /* Null value */
2046  cs = res->charset();
2047  const char *end = res->ptr() + res->length();
2048  return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
2049  }
2050  my_decimal *val_decimal(my_decimal *dec) override;
2051  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2052  return get_date_from_string(ltime, fuzzydate);
2053  }
2054  bool get_time(MYSQL_TIME *ltime) override {
2055  return get_time_from_string(ltime);
2056  }
2057  enum Item_result result_type() const override { return STRING_RESULT; }
2058  bool resolve_type(THD *) override;
2059  Item *copy_or_same(THD *thd) override;
2060 };
2061 
2063  public:
2064  Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg,
2065  PT_item_list *opt_list)
2066  : Item_udf_sum(pos, udf_arg, opt_list) {}
2068  : Item_udf_sum(thd, item) {}
2069  String *val_str(String *) override;
2070  double val_real() override;
2071  longlong val_int() override;
2072  my_decimal *val_decimal(my_decimal *) override;
2073  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2074  return get_date_from_decimal(ltime, fuzzydate);
2075  }
2076  bool get_time(MYSQL_TIME *ltime) override {
2077  return get_time_from_decimal(ltime);
2078  }
2079  enum Item_result result_type() const override { return DECIMAL_RESULT; }
2080  bool resolve_type(THD *) override {
2081  set_data_type(MYSQL_TYPE_NEWDECIMAL);
2082  fix_num_length_and_dec();
2083  return false;
2084  }
2085  Item *copy_or_same(THD *thd) override;
2086 };
2087 
2088 int group_concat_key_cmp_with_distinct(const void *arg, const void *key1,
2089  const void *key2);
2090 int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2091  const void *key2);
2092 int dump_leaf_key(void *key_arg, element_count count MY_ATTRIBUTE((unused)),
2093  void *item_arg);
2094 
2096  typedef Item_sum super;
2097 
2103 
2104  /**
2105  If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
2106  out duplicates.
2107  @see Item_func_group_concat::setup
2108  @see Item_func_group_concat::add
2109  @see Item_func_group_concat::clear
2110  */
2115  /** The number of ORDER BY items. */
2117  /** The number of selected items, aka the expr list. */
2120  /** The maximum permitted result length in bytes as set for
2121  group_concat_max_len system variable */
2123  bool distinct;
2127  /** True if result has been written to output buffer. */
2129  /*
2130  Following is 0 normal object and pointer to original one for copy
2131  (to correctly free resources)
2132  */
2134 
2135  friend int group_concat_key_cmp_with_distinct(const void *arg,
2136  const void *key1,
2137  const void *key2);
2138  friend int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2139  const void *key2);
2140  friend int dump_leaf_key(void *key_arg,
2141  element_count count MY_ATTRIBUTE((unused)),
2142  void *item_arg);
2143 
2144  public:
2145  Item_func_group_concat(const POS &pos, bool is_distinct,
2146  PT_item_list *select_list,
2147  PT_order_list *opt_order_list, String *separator,
2148  PT_window *w);
2149 
2152  DBUG_ASSERT(original != nullptr || unique_filter == nullptr);
2153  }
2154 
2155  bool itemize(Parse_context *pc, Item **res) override;
2156  void cleanup() override;
2157 
2158  enum Sumfunctype sum_func() const override { return GROUP_CONCAT_FUNC; }
2159  const char *func_name() const override { return "group_concat"; }
2160  Item_result result_type() const override { return STRING_RESULT; }
2161  Field *make_string_field(TABLE *table_arg) const override;
2162  void clear() override;
2163  bool add() override;
2164  void reset_field() override { DBUG_ASSERT(0); } // not used
2165  void update_field() override { DBUG_ASSERT(0); } // not used
2166  bool fix_fields(THD *, Item **) override;
2167  bool setup(THD *thd) override;
2168  void make_unique() override;
2169  double val_real() override {
2170  String *res;
2171  res = val_str(&str_value);
2172  return res ? my_atof(res->c_ptr()) : 0.0;
2173  }
2174  longlong val_int() override {
2175  String *res;
2176  int error;
2177  if (!(res = val_str(&str_value))) return (longlong)0;
2178  const char *end_ptr = res->ptr() + res->length();
2179  return my_strtoll10(res->ptr(), &end_ptr, &error);
2180  }
2181  my_decimal *val_decimal(my_decimal *decimal_value) override {
2182  return val_decimal_from_string(decimal_value);
2183  }
2184  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2185  return get_date_from_string(ltime, fuzzydate);
2186  }
2187  bool get_time(MYSQL_TIME *ltime) override {
2188  return get_time_from_string(ltime);
2189  }
2190  String *val_str(String *str) override;
2191  Item *copy_or_same(THD *thd) override;
2192  void no_rows_in_result() override {}
2193  void print(const THD *thd, String *str,
2194  enum_query_type query_type) const override;
2195  bool change_context_processor(uchar *arg) override {
2196  context = reinterpret_cast<Item_ident::Change_context *>(arg)->m_context;
2197  return false;
2198  }
2199 
2201  Window_evaluation_requirements *) override {
2202  unsupported_as_wf();
2203  return true;
2204  }
2205 };
2206 
2207 /**
2208  Common parent class for window functions that always work on the entire
2209  partition, even if a frame is defined.
2210 
2211  The subclasses can be divided in two disjoint sub-categories:
2212  - one-pass
2213  - two-pass (requires partition cardinality to be evaluated)
2214  cf. method needs_card.
2215 */
2217  typedef Item_sum super;
2218 
2219  public:
2220  Item_non_framing_wf(const POS &pos, PT_window *w) : Item_sum(pos, w) {}
2222  : Item_sum(pos, a, w) {}
2223  Item_non_framing_wf(const POS &pos, PT_item_list *opt_list, PT_window *w)
2224  : Item_sum(pos, opt_list, w) {}
2226 
2227  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2228  return get_date_from_numeric(ltime, fuzzydate);
2229  }
2230 
2231  bool get_time(MYSQL_TIME *ltime) override {
2232  return get_time_from_numeric(ltime);
2233  }
2234 
2235  void reset_field() override { DBUG_ASSERT(false); }
2236  void update_field() override { DBUG_ASSERT(false); }
2237  bool add() override {
2238  DBUG_ASSERT(false);
2239  return false;
2240  }
2241 
2242  bool fix_fields(THD *thd, Item **items) override;
2243 
2244  bool framing() const override { return false; }
2245 };
2246 
2247 /**
2248  ROW_NUMBER window function, cf. SQL 2003 Section 6.10 <window function>
2249 */
2251  // Execution state variables
2252  ulonglong m_ctr; ///< Increment for each row in partition
2253 
2254  public:
2256  : Item_non_framing_wf(pos, w), m_ctr(0) {
2257  unsigned_flag = true;
2258  }
2259 
2260  const char *func_name() const override { return "row_number"; }
2261  enum Sumfunctype sum_func() const override { return ROW_NUMBER_FUNC; }
2262 
2263  bool resolve_type(THD *thd MY_ATTRIBUTE((unused))) override {
2264  set_data_type_longlong();
2265  return false;
2266  }
2267 
2268  longlong val_int() override;
2269  double val_real() override;
2270  my_decimal *val_decimal(my_decimal *buff) override;
2271  String *val_str(String *) override;
2272 
2273  void clear() override;
2274 
2275  Item_result result_type() const override { return INT_RESULT; }
2276 
2278  Window_evaluation_requirements *) override {
2279  return false;
2280  }
2281 };
2282 
2283 /**
2284  RANK or DENSE_RANK window function, cf. SQL 2003 Section 6.10 <window
2285  function>
2286 */
2289  bool m_dense; ///< If true, the object represents DENSE_RANK
2290  // Execution state variables
2291  ulonglong m_rank_ctr; ///< Increment when window order columns change
2292  ulonglong m_duplicates; ///< Needed to make RANK different from DENSE_RANK
2293  List<Cached_item> m_previous; ///< Values of previous row's ORDER BY items
2294  public:
2295  Item_rank(const POS &pos, bool dense, PT_window *w)
2296  : Item_non_framing_wf(pos, w),
2297  m_dense(dense),
2298  m_rank_ctr(0),
2299  m_duplicates(0),
2300  m_previous()
2301 
2302  {
2303  unsigned_flag = true;
2304  }
2305 
2306  ~Item_rank() override;
2307 
2308  const char *func_name() const override {
2309  return m_dense ? "dense_rank" : "rank";
2310  }
2311 
2312  enum Sumfunctype sum_func() const override {
2313  return m_dense ? DENSE_RANK_FUNC : RANK_FUNC;
2314  }
2315 
2316  bool resolve_type(THD *thd MY_ATTRIBUTE((unused))) override {
2317  set_data_type_longlong();
2318  return false;
2319  }
2320 
2321  longlong val_int() override;
2322  double val_real() override;
2323  my_decimal *val_decimal(my_decimal *buff) override;
2324  String *val_str(String *) override;
2325 
2326  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
2327  Window_evaluation_requirements *reqs) override;
2328  /**
2329  Clear state for a new partition
2330  */
2331  void clear() override;
2332  Item_result result_type() const override { return INT_RESULT; }
2333 };
2334 
2335 /**
2336  CUME_DIST window function, cf. SQL 2003 Section 6.10 <window function>
2337 */
2340 
2341  public:
2342  Item_cume_dist(const POS &pos, PT_window *w) : Item_non_framing_wf(pos, w) {}
2343 
2344  const char *func_name() const override { return "cume_dist"; }
2345  enum Sumfunctype sum_func() const override { return CUME_DIST_FUNC; }
2346 
2347  bool resolve_type(THD *thd MY_ATTRIBUTE((unused))) override {
2348  set_data_type_double();
2349  return false;
2350  }
2351 
2352  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
2353  Window_evaluation_requirements *reqs) override;
2354 
2355  bool needs_card() const override { return true; }
2356  void clear() override {}
2357  longlong val_int() override;
2358  double val_real() override;
2359  String *val_str(String *) override;
2360  my_decimal *val_decimal(my_decimal *buffer) override;
2361  Item_result result_type() const override { return REAL_RESULT; }
2362 };
2363 
2364 /**
2365  PERCENT_RANK window function, cf. SQL 2003 Section 6.10 <window function>
2366 */
2369  // Execution state variables
2370  ulonglong m_rank_ctr; ///< Increment when window order columns change
2371  ulonglong m_peers; ///< Needed to make PERCENT_RANK same for peers
2372  /**
2373  Set when the last peer has been visited. Needed to increment m_rank_ctr.
2374  */
2376 
2377  public:
2379  : Item_non_framing_wf(pos, w),
2380  m_rank_ctr(0),
2381  m_peers(0),
2382  m_last_peer_visited(false) {}
2383 
2384  ~Item_percent_rank() override;
2385  const char *func_name() const override { return "percent_rank"; }
2386  enum Sumfunctype sum_func() const override { return PERCENT_RANK_FUNC; }
2387 
2388  bool resolve_type(THD *thd MY_ATTRIBUTE((unused))) override {
2389  set_data_type_double();
2390  return false;
2391  }
2392 
2393  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
2394  Window_evaluation_requirements *reqs) override;
2395  bool needs_card() const override { return true; }
2396 
2397  void clear() override;
2398  longlong val_int() override;
2399  double val_real() override;
2400  String *val_str(String *) override;
2401  my_decimal *val_decimal(my_decimal *buffer) override;
2402  Item_result result_type() const override { return REAL_RESULT; }
2403 };
2404 
2405 /**
2406  NTILE window function, cf. SQL 2011 Section 6.10 <window function>
2407 */
2411 
2412  public:
2413  Item_ntile(const POS &pos, Item *a, PT_window *w)
2414  : Item_non_framing_wf(pos, a, w), m_value(0) {
2415  unsigned_flag = true;
2416  }
2417 
2418  const char *func_name() const override { return "ntile"; }
2419  enum Sumfunctype sum_func() const override { return NTILE_FUNC; }
2420 
2421  bool resolve_type(THD *thd) override {
2422  if (args[0]->propagate_type(thd, MYSQL_TYPE_LONGLONG, true)) return true;
2423  set_data_type_longlong();
2424  return false;
2425  }
2426 
2427  bool fix_fields(THD *thd, Item **items) override;
2428 
2429  longlong val_int() override;
2430  double val_real() override;
2431  my_decimal *val_decimal(my_decimal *buff) override;
2432  String *val_str(String *) override;
2433 
2434  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
2435  Window_evaluation_requirements *reqs) override;
2436  bool check_wf_semantics2(Window_evaluation_requirements *reqs) override;
2437  Item_result result_type() const override { return INT_RESULT; }
2438  void clear() override {}
2439  bool needs_card() const override { return true; }
2440 };
2441 
2442 /**
2443  LEAD/LAG window functions, cf. SQL 2011 Section 6.10 <window function>
2444 */
2447  bool m_is_lead; ///< if true, the function is LEAD, else LAG
2448  int64 m_n; ///< canonicalized offset value
2452  /**
2453  Execution state: if set, we already have a value for current row.
2454  State is used to avoid interference with other LEAD/LAG functions on
2455  the same window, since they share the same eval loop and they should
2456  trigger evaluation only when they are on the "right" row relative to
2457  current row. For other offsets, return NULL if we don't know the value
2458  for this function yet, or if we do (m_has_value==true), return the
2459  found value.
2460  */
2462  bool m_use_default; ///< execution state: use default value for current row
2464 
2465  public:
2466  Item_lead_lag(const POS &pos, bool lead,
2467  PT_item_list *opt_list, // [0] expr, [1] offset, [2] default
2468  enum_null_treatment null_treatment, PT_window *w)
2469  : Item_non_framing_wf(pos, opt_list, w),
2470  m_null_treatment(null_treatment),
2471  m_is_lead(lead),
2472  m_n(0),
2473  m_hybrid_type(INVALID_RESULT),
2474  m_value(nullptr),
2475  m_default(nullptr),
2476  m_has_value(false),
2477  m_use_default(false) {}
2478 
2479  const char *func_name() const override {
2480  return (m_is_lead ? "lead" : "lag");
2481  }
2482  enum Sumfunctype sum_func() const override { return LEAD_LAG_FUNC; }
2483 
2484  bool resolve_type(THD *thd) override;
2485  bool fix_fields(THD *thd, Item **items) override;
2486  void clear() override;
2487  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
2488  Window_evaluation_requirements *reqs) override;
2489  bool check_wf_semantics2(Window_evaluation_requirements *reqs) override;
2490  enum Item_result result_type() const override { return m_hybrid_type; }
2491 
2492  longlong val_int() override;
2493  double val_real() override;
2494  String *val_str(String *str) override;
2495  my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2496 
2497  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2498  bool get_time(MYSQL_TIME *ltime) override;
2499  bool val_json(Json_wrapper *wr) override;
2500 
2501  bool needs_card() const override {
2502  /*
2503  A possible optimization here: if LAG, we are only interested in rows we
2504  have already seen, so we might compute the result without reading the
2505  entire partition as soon as we have the current row. Similarly, a small
2506  LEAD value might avoid reading the entire partition also, giving shorter
2507  time to first result. For now, we read the entirely partition for these
2508  window functions - for simplicity.
2509  */
2510  return true;
2511  }
2512 
2513  void split_sum_func(THD *thd, Ref_item_array ref_item_array,
2514  mem_root_deque<Item *> *fields) override;
2515 
2516  void set_has_value(bool value) { m_has_value = value; }
2517  bool has_value() const { return m_has_value; }
2518 
2519  void set_use_default(bool value) { m_use_default = value; }
2520  bool use_default() const { return m_use_default; }
2521 
2522  private:
2523  bool setup_lead_lag();
2524  /**
2525  Core logic of LEAD/LAG window functions
2526 
2527  @return true if computation yielded a NULL or error
2528  */
2529  bool compute();
2530 };
2531 
2532 /**
2533  FIRST_VALUE/LAST_VALUE window functions, cf. SQL 2011 Section 6.10 <window
2534  function>
2535 */
2537  bool m_is_first; ///< if true, the function is FIRST_VALUE, else LAST_VALUE
2541  int64 cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2542  typedef Item_sum super;
2543 
2544  public:
2545  Item_first_last_value(const POS &pos, bool first, Item *a,
2546  enum_null_treatment null_treatment, PT_window *w)
2547  : Item_sum(pos, a, w),
2548  m_is_first(first),
2549  m_null_treatment(null_treatment),
2550  m_hybrid_type(INVALID_RESULT),
2551  m_value(nullptr),
2552  cnt(0) {}
2553 
2554  const char *func_name() const override {
2555  return m_is_first ? "first_value" : "last_value";
2556  }
2557 
2558  enum Sumfunctype sum_func() const override { return FIRST_LAST_VALUE_FUNC; }
2559 
2560  bool resolve_type(THD *thd) override;
2561  bool fix_fields(THD *thd, Item **items) override;
2562  void clear() override;
2563  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
2564  Window_evaluation_requirements *reqs) override;
2565  enum Item_result result_type() const override { return m_hybrid_type; }
2566 
2567  longlong val_int() override;
2568  double val_real() override;
2569  String *val_str(String *str) override;
2570  my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2571 
2572  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2573  bool get_time(MYSQL_TIME *ltime) override;
2574  bool val_json(Json_wrapper *wr) override;
2575 
2576  void reset_field() override { DBUG_ASSERT(false); }
2577  void update_field() override { DBUG_ASSERT(false); }
2578  bool add() override {
2579  DBUG_ASSERT(false);
2580  return false;
2581  }
2582 
2583  void split_sum_func(THD *thd, Ref_item_array ref_item_array,
2584  mem_root_deque<Item *> *fields) override;
2585  bool uses_only_one_row() const override { return true; }
2586 
2587  private:
2588  bool setup_first_last();
2589  /**
2590  Core logic of FIRST/LAST_VALUE window functions
2591 
2592  @return true if computation yielded a NULL or error
2593  */
2594  bool compute();
2595 };
2596 
2597 /**
2598  NTH_VALUE window function, cf. SQL 2011 Section 6.10 <window
2599  function>
2600 */
2601 class Item_nth_value : public Item_sum {
2603  int64 m_n; ///< The N of the function
2604  bool m_from_last; ///< true iff FROM_LAST was specified
2608  int64 m_cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2609 
2610  typedef Item_sum super;
2611 
2612  public:
2613  Item_nth_value(const POS &pos, PT_item_list *a, bool from_last,
2614  enum_null_treatment null_treatment, PT_window *w)
2615  : Item_sum(pos, a, w),
2616  m_null_treatment(null_treatment),
2617  m_n(0),
2618  m_from_last(from_last),
2619  m_hybrid_type(INVALID_RESULT),
2620  m_value(nullptr),
2621  m_cnt(0) {}
2622 
2623  const char *func_name() const override { return "nth_value"; }
2624  enum Sumfunctype sum_func() const override { return NTH_VALUE_FUNC; }
2625 
2626  bool resolve_type(THD *thd) override;
2627  bool fix_fields(THD *thd, Item **items) override;
2628  bool setup_nth();
2629  void clear() override;
2630 
2631  bool check_wf_semantics1(THD *thd, SELECT_LEX *select,
2632  Window_evaluation_requirements *reqs) override;
2633  bool check_wf_semantics2(Window_evaluation_requirements *reqs) override;
2634 
2635  enum Item_result result_type() const override { return m_hybrid_type; }
2636 
2637  longlong val_int() override;
2638  double val_real() override;
2639  String *val_str(String *str) override;
2640  my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2641 
2642  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2643  bool get_time(MYSQL_TIME *ltime) override;
2644  bool val_json(Json_wrapper *wr) override;
2645 
2646  void reset_field() override { DBUG_ASSERT(false); }
2647  void update_field() override { DBUG_ASSERT(false); }
2648  bool add() override {
2649  DBUG_ASSERT(false);
2650  return false;
2651  }
2652 
2653  void split_sum_func(THD *thd, Ref_item_array ref_item_array,
2654  mem_root_deque<Item *> *fields) override;
2655  bool uses_only_one_row() const override { return true; }
2656 
2657  private:
2658  /**
2659  Core logic of NTH_VALUE window functions
2660 
2661  @return true if computation yielded a NULL or error
2662  */
2663  bool compute();
2664 };
2665 
2666 /**
2667  Class for implementation of the GROUPING function. The GROUPING
2668  function distinguishes super-aggregate rows from regular grouped
2669  rows. GROUP BY extensions such as ROLLUP and CUBE produce
2670  super-aggregate rows where the set of all values is represented
2671  by null. Using the GROUPING function, you can distinguish a null
2672  representing the set of all values in a super-aggregate row from
2673  a NULL in a regular row.
2674 */
2676  public:
2677  Item_func_grouping(const POS &pos, PT_item_list *a) : Item_int_func(pos, a) {
2678  set_grouping_func();
2679  }
2680  const char *func_name() const override { return "grouping"; }
2681  enum Functype functype() const override { return GROUPING_FUNC; }
2682  longlong val_int() override;
2683  bool aggregate_check_group(uchar *arg) override;
2684  bool fix_fields(THD *thd, Item **ref) override;
2685  void update_used_tables() override;
2686 };
2687 
2688 /**
2689  A wrapper Item that contains a number of aggregate items, one for each level
2690  of rollup (see Item_rollup_group_item for numbering conventions). When
2691  aggregating, every aggregator is either reset or updated as per the correct
2692  level, and when returning a value, the correct child item corresponding to
2693  the current rollup level is queried.
2694  */
2696  public:
2697  explicit Item_rollup_sum_switcher(List<Item> *sum_func_per_level)
2698  : Item_sum((*sum_func_per_level)[0]),
2699  m_num_levels(sum_func_per_level->size()) {
2700  args = (*THR_MALLOC)->ArrayAlloc<Item *>(sum_func_per_level->size());
2701  int i = 0;
2702  for (Item &item : *sum_func_per_level) {
2703  args[i++] = &item;
2704  }
2705  item_name = master()->item_name;
2706  base_select = master()->base_select;
2707  aggr_select = master()->aggr_select;
2708  hidden = master()->hidden;
2709  set_distinct(master()->has_with_distinct());
2710  set_data_type_from_item(master());
2711  }
2712  double val_real() override;
2713  longlong val_int() override;
2714  String *val_str(String *str) override;
2715  my_decimal *val_decimal(my_decimal *dec) override;
2716  bool val_json(Json_wrapper *result) override;
2717  bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2718  bool get_time(MYSQL_TIME *ltime) override;
2719  const char *func_name() const override { return "rollup_sum_switcher"; }
2720  table_map used_tables() const override { return master()->used_tables(); }
2721  Item_result result_type() const override { return master()->result_type(); }
2722  bool resolve_type(THD *) override {
2723  set_data_type_from_item(master());
2724  return false;
2725  }
2726  void print(const THD *thd, String *str,
2727  enum_query_type query_type) const override;
2728  Field *create_tmp_field(bool group, TABLE *table) override;
2729 
2730  enum Sumfunctype sum_func() const override { return master()->sum_func(); }
2731  enum Sumfunctype real_sum_func() const override {
2732  return ROLLUP_SUM_SWITCHER_FUNC;
2733  }
2734  void reset_field() override { DBUG_ASSERT(false); }
2735  void update_field() override { DBUG_ASSERT(false); }
2736  void clear() override;
2737  bool add() override {
2738  DBUG_ASSERT(false);
2739  return true;
2740  }
2741 
2742  bool reset_and_add_for_rollup(int last_unchanged_group_item_idx);
2743 
2744  int set_aggregator(Aggregator::Aggregator_type aggregator) override;
2745  bool aggregator_setup(THD *thd) override;
2746  inline bool aggregator_add_all() {
2747  for (int i = 0; i < m_num_levels; ++i) {
2748  if (child(i)->aggregator_add()) {
2749  return true;
2750  }
2751  }
2752  return false;
2753  }
2754 
2755  // Used when create_tmp_table() needs to delay application of aggregate
2756  // functions to a later stage in the query processing.
2757  Item *get_arg(uint i) override { return master()->get_arg(i); }
2758  Item *set_arg(THD *thd, uint i, Item *new_val) override {
2759  Item *ret = nullptr;
2760  for (int j = 0; j < m_num_levels; ++j) {
2761  ret = child(j)->set_arg(thd, i, new_val);
2762  }
2763  return ret; // Return the last one, arbitrarily.
2764  }
2765  uint argument_count() const override { return master()->argument_count(); }
2766 
2767  // Used by AggregateIterator.
2768  void set_current_rollup_level(int level) { m_current_rollup_level = level; }
2769  inline Item_sum *master() const { return child(0); }
2770 
2771  private:
2772  inline Item *current_arg() const;
2773  inline Item_sum *child(size_t i) const {
2774  return down_cast<Item_sum *>(args[i]);
2775  }
2776 
2777  const int m_num_levels;
2778  int m_current_rollup_level = INT_MAX;
2779 };
2780 
2781 #endif /* ITEM_SUM_INCLUDED */
table_map used_tables() const override
Definition: item_sum.h:2720
uint size() const
Definition: sql_list.h:269
Item_result hybrid_type
Definition: item_sum.h:1441
Definition: sql_udf.h:43
ulonglong * m_digit_cnt
Execution state (windowing): Used for AND, OR to be able to invert window functions in optimized mode...
Definition: item_sum.h:1781
void reset_field() override
Definition: item_sum.h:2734
const char * func_name() const override
Definition: item_sum.h:1900
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1166
Item *(Item::* Item_transformer)(uchar *arg)
Type for transformers used by Item::transform and Item::compile.
Definition: item.h:717
static uint16 key1[1001]
Definition: hp_test2.cc:46
Item_sum * child(size_t i) const
Definition: item_sum.h:2773
const char * func_name() const override
Definition: item_sum.h:1475
Item_cache * value
Definition: item_sum.h:1552
Definition: item.h:3908
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.h:2181
unsigned long long int ulonglong
Definition: my_inttypes.h:55
Item_name_string item_name
Name from query.
Definition: item.h:3135
not valid for UDFs
Definition: udf_registration_types.h:44
const char * func_name() const override
Definition: item_sum.h:2385
virtual bool check_wf_semantics2(Window_evaluation_requirements *reqs)
Like check_wf_semantics1.
Definition: item_sum.h:746
This file contains the field type.
Definition: item_sum.h:1355
static void print(const char *key, int keylen, const char *val, int vallen)
Print the key value pair.
Definition: mcstat.c:106
bool is_evaluated
Definition: item_sum.h:958
const char * func_name() const override
Definition: item_sum.h:1710
Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
Definition: item_sum.h:961
unsigned char uchar
Definition: my_inttypes.h:51
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
Definition: item_sum.h:2028
Our own string classes, used pervasively throughout the executor.
Definition: item_sum.h:988
virtual bool setup(THD *)
Definition: item_sum.h:724
Item_sum_avg(THD *thd, Item_sum_avg *item)
Definition: item_sum.h:1323
The distinct aggregator.
Definition: item_sum.h:820
Item_non_framing_wf super
Definition: item_sum.h:2339
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2695
Definition: item_sum.h:445
Item_result
Type of the user defined function return slot and arguments.
Definition: udf_registration_types.h:38
Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1457
< Argument object to change_context_processor
Definition: item.h:3830
const int m_num_levels
Definition: item_sum.h:2777
not valid for UDFs
Definition: udf_registration_types.h:40
NTH_VALUE window function, cf.
Definition: item_sum.h:2601
void free_handler()
Definition: item_func.cc:4157
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1010
const char * func_name() const override
Definition: item_sum.h:1304
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
Definition: item_sum.h:1976
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
Definition: item_sum.h:2067
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:2316
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1844
Item_result result_type() const override
Definition: item_sum.h:2437
T pointer_cast(void *p)
Casts from one pointer type, to another, without using reinterpret_cast or C-style cast: foo f; bar *...
Definition: template_utils.h:73
void clear() override
Called when we need to wipe out all the data from the aggregator : all the values acumulated and all ...
Definition: item_sum.h:940
Definition: item_sum.h:1894
Item_cache * m_default
Definition: item_sum.h:2451
bool force_copy_fields
Used in making ROLLUP.
Definition: item_sum.h:425
Item_sum_hybrid(THD *thd, const Item_sum_hybrid *item)
Definition: item_sum.h:1647
bool always_null
Definition: item_sum.h:2125
ssize_t count
Definition: memcached.c:386
ulonglong m_duplicates
Needed to make RANK different from DENSE_RANK.
Definition: item_sum.h:2292
Definition: item_sum.h:1999
Item_udf_sum(THD *thd, Item_udf_sum *item)
Definition: item_sum.h:1941
Temp_table_param * tmp_table_param
Definition: item_sum.h:856
Item_non_framing_wf super
Definition: item_sum.h:2288
virtual double arg_val_real()=0
Floating point value of being-aggregated argument.
Definition: table.h:272
Item * result_item(Field *) override
Definition: item_sum.h:1339
char buffer[STRING_BUFFER]
Definition: test_sql_9_sessions.cc:57
PT_window * m_window
If sum is a window function, this field contains the window.
Definition: item_sum.h:411
Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1076
static uchar key2[100]
Definition: mi_test2.cc:58
Common parent class for window functions that always work on the entire partition, even if a frame is defined.
Definition: item_sum.h:2216
FIRST_VALUE/LAST_VALUE window functions, cf.
Definition: item_sum.h:2536
Collects evaluation requirements from a window function, used by Item_sum::check_wf_semantics and its...
Definition: window.h:1445
ulonglong reset_bits
Definition: item_sum.h:1192
The abstract base class for the Aggregator_* classes.
Definition: item_sum.h:96
my_decimal * val_decimal(my_decimal *dec_buf) override
Definition: item_sum.h:1364
bool fix_fields(THD *thd, Item_result_field *item, uint arg_count, Item **args)
Definition: item_func.cc:4173
File containing constants that can be used throughout the server.
bool force_copy_fields
Definition: item_sum.h:2126
const char * func_name() const override
Definition: item_sum.h:1910
udf_handler udf
Definition: item_sum.h:1934
Item_result hybrid_type
Definition: item_sum.h:1554
Item_sum_count(const POS &pos, PT_item_list *list, PT_window *w)
Constructs an instance for COUNT(DISTINCT)
Definition: item_sum.h:1089
Some integer typedefs for easier portability.
bool check_function_as_value_generator(uchar *args) override
Check if this item is allowed for a virtual column or inside a default expression.
Definition: item_sum.h:1145
Item_sum * item_sum
Definition: item_sum.h:108
void my_error(int nr, myf MyFlags,...)
Fill in and print a previously registered error message.
Definition: my_error.cc:215
Definition: item_sum.h:2062
virtual bool aggregator_setup(THD *thd)
Called to initialize the aggregator.
Definition: item_sum.h:694
Item_cache * m_value
Definition: item_sum.h:2540
ulonglong m_count
Execution state (windowing): this is for counting rows entering and leaving the window frame...
Definition: item_sum.h:1752
virtual bool uses_only_one_row() const
Only for framing window functions.
Definition: item_sum.h:775
const char * func_name() const override
Definition: item_sum.h:1343
A wrapper class which provides array bounds checking.
Definition: sql_array.h:41
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:109
This is used in connection which a parent Item_sum:
Definition: item_sum.h:1129
const char * func_name() const override
Definition: item_sum.h:2159
double val_real() override
Definition: item_sum.h:2031
int64 m_cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2608
Definition: parse_tree_nodes.h:211
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2051
int8 max_sum_func_level
max level of aggregation for contained functions
Definition: item_sum.h:489
void update_field() override
Definition: item_sum.h:1966
~Item_func_group_concat() override
Definition: item_sum.h:2151
longlong val_int() override
Definition: item_sum.h:1978
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2184
Unique – class for unique (removing of duplicates).
Definition: uniques.h:52
std::map< std::string, int > m_key_map
Map of keys in Json_object and the count for each key within a window frame.
Definition: item_sum.h:1286
virtual ~Aggregator()
Definition: item_sum.h:112
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2585
bool m_from_last
true iff FROM_LAST was specified
Definition: item_sum.h:2604
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1089
Item_result hybrid_type
Definition: item_sum.h:1019
Definition: field.h:723
Type dec(Shards< COUNT > &shards, size_t id)
Decrement the counter of a shard by 1.
Definition: ut0counter.h:301
Definition: item_sum.h:1702
virtual bool setup(THD *)=0
Called before adding the first row.
Item ** get_arg_ptr(uint i)
Definition: item_sum.h:679
void make_const()
Definition: item_sum.h:621
Definition: item_sum.h:1312
bool keep_field_type() const override
Definition: item_sum.h:1675
virtual bool add()=0
Called when there&#39;s a new value to be aggregated.
const bool m_is_min
Tells if this is the MIN function (true) or the MAX function (false).
Definition: item_sum.h:1543
Definition: item.h:6444
bool framing() const override
All aggregates are framing, i.e.
Definition: item_sum.h:2244
Item_non_framing_wf(const POS &pos, PT_item_list *opt_list, PT_window *w)
Definition: item_sum.h:2223
Represents the (explicit) window of a SQL 2003 section 7.11 <window clause>, or the implicit (inlined...
Definition: window.h:94
Item_sum_min(Item *item_par)
Definition: item_sum.h:1704
int8 max_aggr_level
max level of unbound column references
Definition: item_sum.h:487
Definition: item_sum.h:1066
bool check_wf_semantics1(THD *, SELECT_LEX *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2277
Definition: item_sum.h:1716
friend class Item_sum
Definition: item_sum.h:97
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1481
const char * func_name() const override
Definition: item_sum.h:1922
String * val_str(String *str) override
Definition: item_sum.h:1363
ulonglong reset_bits
Stores the neutral element for function.
Definition: item_sum.h:1738
ulonglong m_ctr
Increment for each row in partition.
Definition: item_sum.h:2252
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1104
const char * name() const
Definition: sql_udf.h:114
bool add() override
Called when there&#39;s a new value to be aggregated.
Definition: item_sum.h:941
int group_concat_key_cmp_with_distinct(const void *arg, const void *key1, const void *key2)
Compares the values for fields in expr list of GROUP_CONCAT.
Definition: item_sum.cc:3933
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:1201
TREE * tree
Definition: item_sum.h:2102
uint curr_dec_buff
Definition: item_sum.h:1022
Parse tree node for a window; just a shallow wrapper for class Window, q.v.
Definition: parse_tree_window.h:38
CUME_DIST window function, cf.
Definition: item_sum.h:2338
Unique * tree
Definition: item_sum.h:865
Definition: item_sum.h:1972
class udf_list * list
Aggregator(Item_sum *arg)
Definition: item_sum.h:111
uint m_digit_cnt_card
Definition: item_sum.h:1787
const char * func_name() const override
Definition: item_sum.h:2308
uint prec_increment
Definition: item_sum.h:1449
bool mark_field_in_map(uchar *arg) override
Mark underlying field in read or write map of a table.
Definition: item_sum.h:1138
char * c_ptr()
Definition: sql_string.h:248
const char * func_name() const override
Definition: item_sum.h:2680
char *
Definition: udf_registration_types.h:41
enum_walk
Enumeration for {Item,SELECT_LEX[_UNIT],Table_function}walk.
Definition: sql_const.h:438
void reset_field() override
Definition: item_sum.h:2235
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2012
Item_row_number(const POS &pos, PT_window *w)
Definition: item_sum.h:2255
Definition: item_sum.h:441
Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1896
double val_real() override
Definition: item_sum.cc:3822
Aggregator_type Aggrtype() override
Definition: item_sum.h:915
Item_sum_sum super
Definition: item_sum.h:1316
Item_func_group_concat * original
Definition: item_sum.h:2133
Item_result result_type() const override
Definition: item_sum.h:1238
Common abstract class for: Item_avg_field Item_variance_field.
Definition: item_sum.h:1158
Item_first_last_value(const POS &pos, bool first, Item *a, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2545
Item_sum_std(THD *thd, Item_sum_std *item)
Definition: item_sum.h:1519
Item_non_framing_wf super
Definition: item_sum.h:2368
Wrapper class for an Item list head, used to allocate Item lists in the parser in a context-independe...
Definition: parse_tree_helpers.h:100
static char str_value[32]
Definition: test_status_var_service_reg_only.cc:45
Item_result m_hybrid_type
Definition: item_sum.h:2605
std::vector< Item_sum * > list
accumulated all aggregates found
Definition: item_sum.h:650
const char * func_name() const override
Definition: item_sum.h:1183
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1061
Definition: sql_udf.h:82
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.h:2758
enum_query_type
Query type constants (usable as bitmap flags).
Definition: enum_query_type.h:30
Aggregator_distinct(Item_sum *sum)
Definition: item_sum.h:907
Arg_comparator * cmp
Definition: item_sum.h:1553
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:2421
enum_field_types default_data_type() const override
Get the default data (output) type for the specific item.
Definition: item_sum.h:969
Using this class is fraught with peril, and you need to be very careful when doing so...
Definition: sql_string.h:164
const char * orig_table_name() const
Definition: item.h:3777
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:1991
double val_real() override
Definition: item_sum.h:2169
virtual bool arg_is_null(bool use_null_value)=0
NULLness of being-aggregated argument.
uint tree_key_length
Definition: item_sum.h:873
void aggregator_clear()
Called to cleanup the aggregator.
Definition: item_sum.h:700
Item_sum_xor(THD *thd, Item_sum_xor *item)
Definition: item_sum.h:1921
Item_sum_min(THD *thd, const Item_sum_min *item)
Definition: item_sum.h:1707
virtual void clear()=0
Called when we need to wipe out all the data from the aggregator : all the values acumulated and all ...
uint prec_increment
Definition: item_sum.h:1314
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2187
Definition: table.h:1363
Sumfunctype
Definition: item_sum.h:438
virtual bool framing() const
All aggregates are framing, i.e.
Definition: item_sum.h:769
Definition: item_sum.h:439
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2227
Item_sum_num(THD *thd, Item_sum_num *item)
Definition: item_sum.h:967
const char * func_name() const override
Definition: item_sum.h:1368
Item * get_arg(uint i) override
Definition: item_sum.h:2757
Definition: field_types.h:65
Definition: item_sum.h:454
String * separator
Definition: item_sum.h:2100
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1342
Represents a JSON array container, i.e.
Definition: json_dom.h:519
const char * func_name() const override
Definition: item_sum.h:1498
void set_has_value(bool value)
Definition: item_sum.h:2516
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1167
Item_non_framing_wf super
Definition: item_sum.h:2409
Item_lead_lag(const POS &pos, bool lead, PT_item_list *opt_list, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2466
Type type() const override
Definition: item_sum.h:570
nesting_map save_deny_window_func
WFs are forbidden when resolving Item_sum; this member is used to restore WF allowance status afterwa...
Definition: item_sum.h:496
bool add() override
Definition: item_sum.h:2578
Definition: item_sum.h:1914
table_map used_tables_cache
Definition: item_sum.h:499
enum_null_treatment m_null_treatment
Definition: item_sum.h:2446
Item_sum_max(Item *item_par)
Definition: item_sum.h:1718
bool use_distinct_values
When feeding back the data in endup() from Unique/temp table back to Item_sum::add() methods we must ...
Definition: item_sum.h:904
longlong val_int() override
Definition: item_sum.h:1334
ulonglong m_peers
Needed to make PERCENT_RANK same for peers.
Definition: item_sum.h:2371
const char * func_name() const override
Definition: item_sum.h:2479
Temp_table_param * tmp_table_param
Definition: item_sum.h:2098
int64 m_saved_last_value_at
Execution state: keeps track of at which row we saved a non-null last value.
Definition: item_sum.h:1583
Item_sum_bit(THD *thd, Item_sum_bit *item)
Copy constructor, used for executing subqueries with temporary tables.
Definition: item_sum.h:1808
uint32 element_count
Definition: my_tree.h:51
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it...
Definition: item_sum.h:620
Item_non_framing_wf super
Definition: item_sum.h:2463
virtual bool keep_field_type() const
Definition: item_sum.h:601
Item_sum super
Definition: item_sum.h:1931
#define DBUG_ASSERT(A)
Definition: my_dbug.h:199
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:2347
Definition: item_sum.h:456
Item_sum(Item *a)
Definition: item_sum.h:524
int dump_leaf_key(void *key_arg, element_count count, void *item_arg)
Append data from current leaf to item->result.
Definition: item_sum.cc:4011
Definition: item_cmpfunc.h:131
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:1682
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2015
Field * create_tmp_field(THD *thd, TABLE *table, Item *item, Item::Type type, Func_ptr_array *copy_func, Field **from_field, Field **default_field, bool group, bool modify_item, bool table_cant_handle_bit_fields, bool make_copy_field, bool copy_result_field)
Create field for temporary table.
Definition: sql_tmp_table.cc:343
SELECT_LEX * aggr_select
For a group aggregate, query block where function is aggregated.
Definition: item_sum.h:486
Definition: item_sum.h:448
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:1182
Aggregator_type
Definition: item_sum.h:114
bool has_value() const
Definition: item_sum.h:2517
Item_sum super
Definition: item_sum.h:2542
void update_field() override
Definition: item_sum.h:2236
Definition: item_sum.h:450
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it...
Definition: item_sum.h:1170
const char * func_name() const override
Definition: item_sum.h:2344
bool with_distinct
Indicates how the aggregate function was specified by the parser : true if it was written as AGGREGAT...
Definition: item_sum.h:432
const char * func_name() const override
Definition: item_sum.h:2418
Name_resolution_context * context
Definition: item_sum.h:2114
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2231
char * pos
Definition: do_ctype.cc:76
Item_non_framing_wf(THD *thd, Item_non_framing_wf *i)
Definition: item_sum.h:2225
Class for implementation of the GROUPING function.
Definition: item_sum.h:2675
virtual Item * result_item(Field *field)
Definition: item_sum.h:603
bool check_function_as_value_generator(uchar *args) override
Check if this item is allowed for a virtual column or inside a default expression.
Definition: item_sum.h:1502
Definition: item_sum.h:1930
const char * func_name() const override
Definition: item_sum.h:1523
#define MYF(v)
Unique * unique_filter
If DISTINCT is used with this GROUP_CONCAT, this member is used to filter out duplicates.
Definition: item_sum.h:2111
const char * func_name() const override
Definition: item_sum.h:1112
void reset_field() override
Definition: item_sum.h:2164
Bison "location" class.
Definition: parse_location.h:42
Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:990
bool m_optimize
Set to true when min/max can be optimized using window&#39;s ordering.
Definition: item_sum.h:1563
bool has_with_distinct() const
Definition: item_sum.h:436
Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1916
Definition: item_sum.h:114
Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1516
This is used in connection with an Item_sum_bit,.
Definition: item_sum.h:1190
Item_sum super
Definition: item_sum.h:1213
bool has_force_copy_fields() const
Definition: item_sum.h:435
longlong val_int() override
Definition: item_sum.h:1160
bool add() override
Definition: item_sum.h:2237
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:2017
Class Item_sum is the base class used for special expressions that SQL calls &#39;set functions&#39;...
Definition: item_sum.h:394
bool m_last_peer_visited
Set when the last peer has been visited.
Definition: item_sum.h:2375
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2655
Item_ntile(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2413
Item * result_item(Field *) override
Definition: item_sum.h:1827
Item_sum super
Definition: item_sum.h:1736
Definition: item_sum.h:1514
std::set< Item_sum * > aggregates_that_were_hidden
Definition: item_sum.h:651
uint group_concat_max_len
The maximum permitted result length in bytes as set for group_concat_max_len system variable...
Definition: item_sum.h:2122
Item_result m_hybrid_type
Definition: item_sum.h:2449
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:188
Item_result result_type() const override
Definition: item_sum.h:2332
int64_t int64
Definition: my_inttypes.h:67
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:1098
bool endup_done
Definition: item_sum.h:831
Definition: my_tree.h:67
enum_null_treatment m_null_treatment
Definition: item_sum.h:2538
Definition: item_sum.h:1491
my_decimal class limits &#39;decimal_t&#39; type to what we need in MySQL.
Definition: my_decimal.h:91
Func_args_handle super2
Definition: item_sum.h:396
Definition: item_sum.h:442
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1013
Functype
Definition: item_func.h:168
Definition: udf_registration_types.h:39
Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1906
Definition: item_sum.h:1904
double
Definition: udf_registration_types.h:42
bool m_dense
If true, the object represents DENSE_RANK.
Definition: item_sum.h:2289
const std::string collation("collation")
bool m_nulls_first
Set to true if the window is ordered ascending.
Definition: item_sum.h:1559
Item_sum(const POS &pos, Item *a, Item *b, PT_window *w)
Definition: item_sum.h:550
Definition: item_sum.h:461
void init_aggregator()
Definition: item_sum.h:684
Header for compiler-dependent features.
Definition: item_sum.h:446
Item_percent_rank(const POS &pos, PT_window *w)
Definition: item_sum.h:2378
String m_tmp_key_value
Buffer used to get the value of the key.
Definition: item_sum.h:1279
Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2026
Mem_root_array< ORDER > order_array
Definition: item_sum.h:2113
Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1974
~Item_udf_sum() override
Definition: item_sum.h:1945
Definition: item_sum.h:1437
int get_unnamed_function_error_code() const
Return the correct error code, based on whether or not if we are checking for disallowed functions in...
Definition: item.h:503
Const_distinct
Definition: item_sum.h:875
enum_field_types
Column types for MySQL.
Definition: field_types.h:52
Item_sum_or(THD *thd, Item_sum_or *item)
Definition: item_sum.h:1899
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1164
Definition: item.h:775
bool aggregator_add()
Called to add value to the aggregator.
Definition: item_sum.h:706
bool m_window_resolved
True if we have already resolved this window functions window reference.
Definition: item_sum.h:416
unsigned int uint
Definition: uca-dump.cc:29
#define final(a, b, c)
Definition: hash.c:109
Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2001
Item_result result_type() const override
Definition: item_sum.h:2361
void set(String &str, size_t offset, size_t arg_length)
Definition: sql_string.h:277
void update_field() override
Definition: item_sum.h:2647
#define true
Definition: config_static.h:44
bool is_and() const
Definition: item_sum.h:1857
Item_sum_hybrid(Item *item_par, bool is_min)
Definition: item_sum.h:1615
long long int longlong
Definition: my_inttypes.h:54
bool m_is_lead
if true, the function is LEAD, else LAG
Definition: item_sum.h:2447
Item_result result_type() const override
Definition: item_sum.h:2275
ulonglong m_frame_null_count
Execution state: this is for counting NULLs of rows entering and leaving the window frame...
Definition: item_sum.h:1035
Definition: item_sum.h:2095
Type
Definition: item.h:811
SELECT_LEX * base_select
query block where function is placed
Definition: item_sum.h:480
bool check_wf_semantics1(THD *, SELECT_LEX *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2200
List< Cached_item > m_previous
Values of previous row&#39;s ORDER BY items.
Definition: item_sum.h:2293
String m_value
String used when reading JSON binary values or JSON text values.
Definition: item_sum.h:1217
Definition: item_sum.h:458
uint32_t uint32
Definition: my_inttypes.h:66
argument used by walk method collect_grouped_aggregates ("cga")
Definition: item_sum.h:648
longlong(* strtoll10)(const CHARSET_INFO *cs, const char *nptr, const char **endptr, int *error)
Definition: m_ctype.h:346
Item_non_framing_wf(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2221
const char * banned_function_name
the name of the function which is not allowed
Definition: item.h:498
const char * func_name() const override
Definition: item_sum.h:1205
Item_sum_and(THD *thd, Item_sum_and *item)
Definition: item_sum.h:1909
Definition: item_sum.h:948
bool allow_group_via_temp_table
If incremental update of fields is supported.
Definition: item_sum.h:490
TREE tree_base
Definition: item_sum.h:2101
double m_avg
Definition: item_sum.h:1318
Item * result_item(Field *) override
Definition: item_sum.h:1522
Item_cache * m_value
Definition: item_sum.h:2450
It is interface module to fixed precision decimals library.
Definition: item_sum.h:451
Definition: item_func.h:888
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2054
Definition: item_sum.h:457
Definition: item_sum.h:455
Definition: m_ctype.h:359
Item_sum(const POS &pos, PT_window *w)
Definition: item_sum.h:512
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1985
Item_result hybrid_type
Stores the Item&#39;s result type.
Definition: item_sum.h:1134
Definition: item_sum.h:452
#define my_strntod(s, a, b, c, d)
Definition: m_ctype.h:744
Definition: item_sum.h:1173
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:89
const char * func_name() const override
Definition: item_sum.h:2719
virtual bool needs_card() const
Return true if we need to make two passes over the rows in the partition - either because we need the...
Definition: item_sum.h:785
const char * func_name() const override
Definition: item_sum.h:2554
NTILE window function, cf.
Definition: item_sum.h:2408
Item_sum super
Definition: item_sum.h:2217
Common header for many mysys elements.
bool any_value()
Definition: item_sum.h:1679
bool was_values
Definition: item_sum.h:1555
bool setup(THD *thd) override
Called before adding the first row.
Definition: item_sum.h:939
Definition: item_sum.h:444
Item_sum(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:537
Mixin class for classes which have an array of arguments (&#39;args&#39;) and a count of them (&#39;arg_count&#39;)...
Definition: item_func.h:90
Definition: item_sum.h:1017
my_decimal m_avg_dec
Definition: item_sum.h:1317
Aggregator * aggr
Aggregator class instance.
Definition: item_sum.h:406
Definition: item_sum.h:447
Aggregator_simple(Item_sum *sum)
Definition: item_sum.h:936
longlong val_int() override
Definition: item_sum.h:2040
Item_non_framing_wf(const POS &pos, PT_window *w)
Definition: item_sum.h:2220
std::unique_ptr< T, Destroy_only< T > > unique_ptr_destroy_only
std::unique_ptr, but only destroying.
Definition: my_alloc.h:409
Item_sum super
Definition: item_sum.h:2096
bool m_result_finalized
True if result has been written to output buffer.
Definition: item_sum.h:2128
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2076
Definition: item_sum.h:443
Definition: item_sum.h:460
String result
Definition: item_sum.h:2099
enum_null_treatment m_null_treatment
Definition: item_sum.h:2602
Definition: item_sum.h:459
bool(Item::* Item_processor)(uchar *arg)
Processor type for {Item,SELECT_LEX[_UNIT],Table_function}walk.
Definition: sql_const.h:456
unsigned int my_time_flags_t
Flags to str_to_datetime and number_to_datetime.
Definition: my_time.h:82
Definition: item.h:394
RANK or DENSE_RANK window function, cf.
Definition: item_sum.h:2287
const char * func_name() const override
Definition: item_sum.h:2260
int err_code
the error code found during check(if any)
Definition: item.h:491
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
Definition: item_sum.h:2003
longlong my_strtoll10(const char *nptr, const char **endptr, int *error)
Definition: my_strtoll10.cc:86
TABLE * table
Definition: item_sum.h:2112
enum_field_types m_hybrid_field_type
Definition: item_sum.h:2606
Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
Definition: item_sum.h:1735
Item_sum super
Definition: item_sum.h:2610
Item_result hybrid_type
Stores the Item&#39;s result type. Can only be INT_RESULT or STRING_RESULT.
Definition: item_sum.h:1744
Definition: mysql_time.h:81
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2291
bool needs_card() const override
Return true if we need to make two passes over the rows in the partition - either because we need the...
Definition: item_sum.h:2501
Item_result_field super
Definition: item_sum.h:395
Definition: item_sum.h:449
const Window * window() const
Definition: item_sum.h:756
void update_field() override
Definition: item_sum.h:2577
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:85
void clear() override
Definition: item_sum.h:2438
Item_sum_int(THD *thd, Item_sum_int *item)
Definition: item_sum.h:1000
Definition: item_sum.h:2024
Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg, PT_window *w)
Definition: item_sum.h:1795
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.h:1951
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1474
uint f_scale
Definition: item_sum.h:1315
bool needs_card() const override
Return true if we need to make two passes over the rows in the partition - either because we need the...
Definition: item_sum.h:2355
uint row_count
Definition: item_sum.h:2119
int64 m_n
canonicalized offset value
Definition: item_sum.h:2448
bool change_context_processor(uchar *arg) override
Definition: item_sum.h:2195
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1988
void unsupported_as_wf()
Definition: item_sum.h:803
ulonglong count
Definition: item_sum.h:1447
bool distinct
Definition: item_sum.h:2123
void set_distinct(bool distinct)
Definition: item_sum.h:709
uint argument_count() const override
Definition: item_sum.h:2765
Item_cume_dist(const POS &pos, PT_window *w)
Definition: item_sum.h:2342
bool add() override
Definition: item_sum.h:2648
PERCENT_RANK window function, cf.
Definition: item_sum.h:2367
Item_cache * m_value
Definition: item_sum.h:2607
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:2722
Represents a JSON container value of type "object" (ECMA), type J_OBJECT here.
Definition: json_dom.h:368
double recurrence_s
Definition: item_sum.h:1445
Item_sum_max(THD *thd, const Item_sum_max *item)
Definition: item_sum.h:1721
Item_sum * master() const
Definition: item_sum.h:2769
ulonglong m_frame_null_count
Execution state (windowing): this is for counting NULLs of rows entering and leaving the window frame...
Definition: item_sum.h:1760
Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1937
bool aggregator_add_all()
Definition: item_sum.h:2746
Definition: item_sum.h:453
Item_result result_type() const override
Definition: item_sum.h:2402
longlong val_int() override
Definition: item_sum.cc:3863
void get_date(char *to, int flag, time_t date)
Store textual representation of date in a character array.
Definition: my_systime.cc:107
bool forced_const
True means that this field has been evaluated during optimization.
Definition: item_sum.h:505
bool m_want_first
For min() - Set to true when results are ordered in ascending and false when descending.
Definition: item_sum.h:1571
Definition: item_sum.h:440
virtual void endup()=0
Called when there are no more data and the final value is to be retrieved.
longlong val_int() override
Definition: item_sum.h:973
bool check_function_as_value_generator(uchar *args) override
Check if this item is allowed for a virtual column or inside a default expression.
Definition: item_sum.h:1372
void update_field() override
Definition: item_sum.h:2735
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:1367
bool m_has_value
Execution state: if set, we already have a value for current row.
Definition: item_sum.h:2461
void reset_field() override
Definition: item_sum.h:2646
void set_use_default(bool value)
Definition: item_sum.h:2519
Item_sum super
Definition: item_sum.h:1537
virtual bool add()=0
unique_ptr_destroy_only< Json_wrapper > m_wrapper
Wrapper around the container (object/array) which accumulates the value.
Definition: item_sum.h:1221
const char * orig_db_name() const
Definition: item.h:3776
unique_ptr_destroy_only< Json_object > m_json_object
Accumulates the final value.
Definition: item_sum.h:1277
Definition: item_sum.h:114
Time declarations shared between the server and client API: you should not add anything to this heade...
Item with result field.
Definition: item.h:5295
double val_real() override
Definition: item_sum.h:2006
bool needs_card() const override
Return true if we need to make two passes over the rows in the partition - either because we need the...
Definition: item_sum.h:2395
String value_buff
Stores the result value for the STRING_RESULT.
Definition: item_sum.h:1742
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1346
unique_ptr_destroy_only< Json_array > m_json_array
Accumulates the final value.
Definition: item_sum.h:1258
Struct used to pass around arguments to/from check_function_as_value_generator.
Definition: item.h:484
double sum
Definition: item_sum.h:1020
int64 m_cnt
Execution state: keeps track if this is the first row in the frame when buffering is not needed...
Definition: item_sum.h:1577
Definition: field_types.h:81
double val_real() override
Definition: item_sum.h:1004
void set_orig_field_name(const char *name_arg)
Definition: item.h:3773
uint64_t nesting_map
Definition: my_table_map.h:30
bool reset_and_add()
Resets the aggregate value to its default and aggregates the current value of its attribute(s)...
Definition: item_sum.h:580
Item_sum super
Definition: item_sum.h:949
void update_field() override
Definition: item_sum.h:2165
int group_concat_key_cmp_with_order(const void *arg, const void *key1, const void *key2)
function of sort for syntax: GROUP_CONCAT(expr,...
Definition: item_sum.cc:3968
void set_current_rollup_level(int level)
Definition: item_sum.h:2768
Item_sum_int(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:995
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:2263
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:979
void endup() override
Called when there are no more data and the final value is to be retrieved.
Definition: item_sum.h:942
Collect_grouped_aggregate_info(SELECT_LEX *select)
Definition: item_sum.h:659
Item_result m_hybrid_type
Definition: item_sum.h:2539
const CHARSET_INFO * charset() const
Definition: sql_string.h:237
Target down_cast(Source *arg)
Casts from one pointer type to another in a type hierarchy.
Definition: template_utils.h:94
A better implementation of the UNIX ctype(3) library.
uint64_t table_map
Definition: my_table_map.h:29
friend class Item_sum_sum
Definition: item_sum.h:98
MYSQL_PLUGIN_IMPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:510
Item_nth_value(const POS &pos, PT_item_list *a, bool from_last, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2613
Item_sum * next_sum
next in the circular chain of registered objects
Definition: item_sum.h:478
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:982
int64 cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2541
TABLE * table
Definition: item_sum.h:842
const char * func_name() const override
Definition: item_sum.h:1268
longlong val_int() override
Definition: item_sum.h:2174
Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1719
void reset_field() override
Definition: item_sum.h:1965
Item_sum * in_sum_func
the containing set function if any
Definition: item_sum.h:479
Definition: field_types.h:62
const string value("\alue\)
Item * result_item(Field *) override
Definition: item_sum.h:1473
ulonglong bits
Stores the result value for the INT_RESULT.
Definition: item_sum.h:1740
LEAD/LAG window functions, cf.
Definition: item_sum.h:2445
longlong m_value
Definition: item_sum.h:2410
const char * func_name() const override
Definition: item_sum.h:1950
bool use_default() const
Definition: item_sum.h:2520
Interface for low level time utilities.
String m_conversion_buffer
String used for converting JSON text values to utf8mb4 charset.
Definition: item_sum.h:1219
uint prec_increment
Definition: item_sum.h:1176
const char * func_name() const override
Definition: item_sum.h:2623
Common abstraction for Item_sum_json_array and Item_sum_json_object.
Definition: item_sum.h:1212
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:639
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:2080
const char * ptr() const
Definition: sql_string.h:246
uint sample
Definition: item_sum.h:1448
virtual my_decimal * arg_val_decimal(my_decimal *value)=0
Decimal value of being-aggregated argument.
Abstract base class for the MIN and MAX aggregate functions.
Definition: item_sum.h:1536
const char * func_name() const override
Definition: item_sum.h:1724
virtual void make_unique()
Definition: item_sum.h:644
Item_result result_type() const override
Definition: item_sum.h:2160
bool m_is_first
if true, the function is FIRST_VALUE, else LAST_VALUE
Definition: item_sum.h:2537
Item_result result_type() const override
Definition: item_sum.h:2721
Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
Definition: item_sum.h:1631
#define STRING_BUFFER_USUAL_SIZE
Definition: sql_const.h:126
bool m_use_default
execution state: use default value for current row
Definition: item_sum.h:2462
MY_CHARSET_HANDLER * cset
Definition: m_ctype.h:391
uint32 * field_lengths
Definition: item_sum.h:850
uint sample
Definition: item_sum.h:1357
bool add() override
Definition: item_sum.h:2737
The pass-through aggregator.
Definition: item_sum.h:934
virtual Aggregator_type Aggrtype()=0
Aggregator_type Aggrtype() override
Definition: item_sum.h:937
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2370
int8_t int8
Definition: my_inttypes.h:61
bool m_is_xor
true iff BIT_XOR
Definition: item_sum.h:1792
Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1705
enum_null_treatment
Cf.
Definition: window_lex.h:57
int64 m_n
The N of the function.
Definition: item_sum.h:2603
bool needs_card() const override
Return true if we need to make two passes over the rows in the partition - either because we need the...
Definition: item_sum.h:2439
virtual Item * get_arg(uint i)
Definition: item_sum.h:676
Field * field
The tmp table&#39;s column containing the value of the set function.
Definition: item_sum.h:1132
uint arg_count_order
The number of ORDER BY items.
Definition: item_sum.h:2116
Item_func_grouping(const POS &pos, PT_item_list *a)
Definition: item_sum.h:2677
Item_rollup_sum_switcher(List< Item > *sum_func_per_level)
Definition: item_sum.h:2697
bool warning_for_row
Definition: item_sum.h:2124
void reset_field() override
Definition: item_sum.h:2576
uint arg_count_field
The number of selected items, aka the expr list.
Definition: item_sum.h:2118
bool m_original
Definition: sql_udf.h:101
Set to true if the result is known to be always NULL.
Definition: item_sum.h:886
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:2192
#define false
Definition: config_static.h:43
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2073
table_map used_tables() const override
Definition: item_sum.h:613
bool optimize
If set, uses a algorithm II mentioned in the class description to calculate the variance which helps ...
Definition: item_sum.h:1455
Item_rank(const POS &pos, bool dense, PT_window *w)
Definition: item_sum.h:2295
size_t length() const
Definition: sql_string.h:238
void make_const(longlong count_arg)
Definition: item_sum.h:1105
Item_sum_num(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:964
double my_atof(const char *nptr)
Definition: dtoa.cc:528
ulonglong m_count
Execution state: this is for counting rows entering and leaving the window frame, see m_frame_null_co...
Definition: item_sum.h:1028
bool is_initialized() const
Definition: sql_udf.h:112
bool transform(const dd::Spatial_reference_system *source_srs, const Geometry &in, const dd::Spatial_reference_system *target_srs, const char *func_name, std::unique_ptr< Geometry > *out) noexcept
Transforms a geometry from one SRS to another.
Definition: transform.cc:228
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:74
ROW_NUMBER window function, cf.
Definition: item_sum.h:2250
Definition: items.h:34
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:870
double recurrence_s2
Definition: item_sum.h:1446
Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
Definition: item_sum.h:1320
Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2064
void clear() override
Definition: item_sum.h:2356
virtual bool mark_field_in_map(uchar *arg)
Mark underlying field in read or write map of a table.
Definition: item.h:2449
Implements aggregation of values into an object.
Definition: item_sum.h:1275
Implements aggregation of values into an array.
Definition: item_sum.h:1256
Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
Definition: item_sum.h:1038
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:803
Dialog Client Authentication nullptr
Definition: dialog.cc:353
const char * func_name() const override
Definition: item_sum.h:1062
longlong count
Definition: item_sum.h:1067
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size...
Definition: item_sum.h:2388
uint f_scale
Definition: item_sum.h:1175
virtual void clear()=0
Item_sum_count(THD *thd, Item_sum_count *item)
Definition: item_sum.h:1093