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