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