MySQL 8.0.32
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, 2022, Oracle and/or its affiliates.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License, version 2.0,
8 as published by the Free Software Foundation.
9
10 This program is also distributed with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have included with MySQL.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License, version 2.0, for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25
26/* classes for sum functions */
27
28#include <assert.h>
29#include <sys/types.h>
30
31#include <climits>
32#include <cmath>
33#include <cstdio>
34#include <map>
35#include <memory>
36#include <optional>
37#include <string>
38#include <vector>
39
40#include "field_types.h" // enum_field_types
41#include "m_ctype.h"
42#include "m_string.h"
43#include "my_alloc.h"
44#include "my_compiler.h"
45
46#include "my_inttypes.h"
47#include "my_sys.h"
48#include "my_table_map.h"
49#include "my_time.h"
50#include "my_tree.h" // TREE
52#include "mysql_time.h"
53#include "mysqld_error.h"
54#include "sql/enum_query_type.h"
56#include "sql/gis/wkb.h"
57#include "sql/item.h" // Item_result_field
58#include "sql/item_func.h" // Item_int_func
59#include "sql/mem_root_array.h"
60#include "sql/my_decimal.h"
61#include "sql/parse_location.h" // POS
62#include "sql/parse_tree_window.h" // PT_window
63#include "sql/sql_base.h"
64#include "sql/sql_const.h"
65#include "sql/sql_list.h"
66#include "sql/sql_udf.h" // udf_handler
67#include "sql/thr_malloc.h" // THR_MALLOC
68#include "sql/window_lex.h"
69#include "sql_string.h"
70#include "template_utils.h"
71
72class Field;
73class Item_sum;
74class Json_array;
75class Json_object;
76class Json_wrapper;
77class PT_item_list;
78class PT_order_list;
79class Query_block;
80class THD;
82class Window;
83struct ORDER;
84struct Parse_context;
85struct TABLE;
87
88/**
89 The abstract base class for the Aggregator_* classes.
90 It implements the data collection functions (setup/add/clear)
91 as either pass-through to the real functionality or
92 as collectors into an Unique (for distinct) structure.
93
94 Note that update_field/reset_field are not in that
95 class, because they're simply not called when
96 GROUP BY/DISTINCT can be handled with help of index on grouped
97 fields (allow_group_via_temp_table is false);
98*/
99
101 friend class Item_sum;
102 friend class Item_sum_sum;
103 friend class Item_sum_count;
104 friend class Item_sum_avg;
105
106 /*
107 All members are protected as this class is not usable outside of an
108 Item_sum descendant.
109 */
110 protected:
111 /* the aggregate function class to act on */
113
114 public:
116 virtual ~Aggregator() = default;
117
120
121 /**
122 Called before adding the first row.
123 Allocates and sets up the internal aggregation structures used,
124 e.g. the Unique instance used to calculate distinct.
125 */
126 virtual bool setup(THD *) = 0;
127
128 /**
129 Called when we need to wipe out all the data from the aggregator:
130 all the values accumulated and all the state.
131 Cleans up the internal structures and resets them to their initial state.
132 */
133 virtual void clear() = 0;
134
135 /**
136 Called when there's a new value to be aggregated.
137 Updates the internal state of the aggregator to reflect the new value.
138 */
139 virtual bool add() = 0;
140
141 /**
142 Called when there are no more data and the final value is to be retrieved.
143 Finalises the state of the aggregator, so the final result can be retrieved.
144 */
145 virtual void endup() = 0;
146
147 /** Decimal value of being-aggregated argument */
149 /** Floating point value of being-aggregated argument */
150 virtual double arg_val_real() = 0;
151 /**
152 NULLness of being-aggregated argument.
153
154 @param use_null_value Optimization: to determine if the argument is NULL
155 we must, in the general case, call is_null() on it, which itself might
156 call val_*() on it, which might be costly. If you just have called
157 arg_val*(), you can pass use_null_value=true; this way, arg_is_null()
158 might avoid is_null() and instead do a cheap read of the Item's null_value
159 (updated by arg_val*()).
160 */
161 virtual bool arg_is_null(bool use_null_value) = 0;
162};
163
164/**
165 Class Item_sum is the base class used for special expressions that SQL calls
166 'set functions'. These expressions are formed with the help of aggregate
167 functions such as SUM, MAX, GROUP_CONCAT etc.
168 Class Item_sum is also the base class for Window functions; the text below
169 first documents set functions, then window functions.
170
171 GENERAL NOTES
172
173 A set function cannot be used in all positions where expressions are accepted.
174 There are some quite explicable restrictions for the use of set functions.
175
176 In the query:
177
178 SELECT AVG(b) FROM t1 WHERE SUM(b) > 20 GROUP by a
179
180 the set function AVG(b) is valid, while the usage of SUM(b) is invalid.
181 A WHERE condition must contain expressions that can be evaluated for each row
182 of the table. Yet the expression SUM(b) can be evaluated only for each group
183 of rows with the same value of column a.
184 In the query:
185
186 SELECT AVG(b) FROM t1 WHERE c > 30 GROUP BY a HAVING SUM(b) > 20
187
188 both set function expressions AVG(b) and SUM(b) are valid.
189
190 We can say that in a query without nested selects an occurrence of a
191 set function in an expression of the SELECT list or/and in the HAVING
192 clause is valid, while in the WHERE clause, FROM clause or GROUP BY clause
193 it is invalid.
194
195 The general rule to detect whether a set function is valid in a query with
196 nested subqueries is much more complicated.
197
198 Consider the following query:
199
200 SELECT t1.a FROM t1 GROUP BY t1.a
201 HAVING t1.a > ALL (SELECT t2.c FROM t2 WHERE SUM(t1.b) < t2.c).
202
203 The set function SUM(b) is used here in the WHERE clause of the subquery.
204 Nevertheless it is valid since it is contained in the HAVING clause of the
205 outer query. The expression SUM(t1.b) is evaluated for each group defined
206 in the main query, not for groups of the subquery.
207
208 The problem of finding the query where to aggregate a particular
209 set function is not so simple as it seems to be.
210
211 In the query:
212 SELECT t1.a FROM t1 GROUP BY t1.a
213 HAVING t1.a > ALL(SELECT t2.c FROM t2 GROUP BY t2.c
214 HAVING SUM(t1.a) < t2.c)
215
216 the set function can be evaluated in both the outer and the inner query block.
217 If we evaluate SUM(t1.a) for the outer query then we get the value of t1.a
218 multiplied by the cardinality of a group in table t1. In this case,
219 SUM(t1.a) is used as a constant value in each correlated subquery.
220 But SUM(t1.a) can also be evaluated for the inner query.
221 In this case t1.a will be a constant value for each correlated subquery and
222 summation is performed for each group of table t2.
223 (Here it makes sense to remind that the query
224
225 SELECT c FROM t GROUP BY a HAVING SUM(1) < a
226
227 is quite valid in our SQL).
228
229 So depending on what query block we assign the set function to we
230 can get different results.
231
232 The general rule to detect the query block Q where a set function will be
233 aggregated (evaluated) can be formulated as follows.
234
235 Reference: SQL2011 @<set function specification@> syntax rules 6 and 7.
236
237 Consider a set function S(E) where E is an expression which contains
238 column references C1, ..., Cn. Resolve all column references Ci against
239 the query block Qi containing the set function S(E). Let Q be the innermost
240 query block of all query blocks Qi. (It should be noted here that S(E)
241 in no way can be aggregated in the query block containing the subquery Q,
242 otherwise S(E) would refer to at least one unbound column reference).
243 If S(E) is used in a construct of Q where set functions are allowed then
244 we aggregate S(E) in Q.
245 Otherwise:
246 - if ANSI SQL mode is enabled (MODE_ANSI), then report an error.
247 - otherwise, look for the innermost query block containing S(E) of those
248 where usage of S(E) is allowed. The place of aggregation depends on which
249 clause the subquery is contained within; It will be different when
250 contained in a WHERE clause versus in the select list or in HAVING clause.
251
252 Let's demonstrate how this rule is applied to the following queries.
253
254 1. SELECT t1.a FROM t1 GROUP BY t1.a
255 HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
256 HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
257 HAVING SUM(t1.a+t2.b) < t3.c))
258 For this query the set function SUM(t1.a+t2.b) contains t1.a and t2.b
259 with t1.a defined in the outermost query, and t2.b defined for its
260 subquery. The set function is contained in the HAVING clause of the subquery
261 and can be evaluated in this subquery.
262
263 2. SELECT t1.a FROM t1 GROUP BY t1.a
264 HAVING t1.a > ALL(SELECT t2.b FROM t2
265 WHERE t2.b > ALL (SELECT t3.c FROM t3 GROUP BY t3.c
266 HAVING SUM(t1.a+t2.b) < t3.c))
267 The set function SUM(t1.a+t2.b) is contained in the WHERE clause of the second
268 query block - the outermost query block where t1.a and t2.b are defined.
269 If we evaluate the function in this subquery we violate the context rules.
270 So we evaluate the function in the third query block (over table t3) where it
271 is used under the HAVING clause; if in ANSI SQL mode, an error is thrown.
272
273 3. SELECT t1.a FROM t1 GROUP BY t1.a
274 HAVING t1.a > ALL(SELECT t2.b FROM t2
275 WHERE t2.b > ALL (SELECT t3.c FROM t3
276 WHERE SUM(t1.a+t2.b) < t3.c))
277 In this query, evaluation of SUM(t1.a+t2.b) is not valid neither in the second
278 nor in the third query block.
279
280 Set functions can generally not be nested. In the query
281
282 SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20
283
284 the expression SUM(b) is not valid, even though it is contained inside
285 a HAVING clause.
286 However, it is acceptable in the query:
287
288 SELECT t.1 FROM t1 GROUP BY t1.a HAVING SUM(t1.b) > 20.
289
290 An argument of a set function does not have to be a simple column reference
291 as seen in examples above. This can be a more complex expression
292
293 SELECT t1.a FROM t1 GROUP BY t1.a HAVING SUM(t1.b+1) > 20.
294
295 The expression SUM(t1.b+1) has clear semantics in this context:
296 we sum up the values of t1.b+1 where t1.b varies for all values within a
297 group of rows that contain the same t1.a value.
298
299 A set function for an outer query yields a constant value within a subquery.
300 So the semantics of the query
301
302 SELECT t1.a FROM t1 GROUP BY t1.a
303 HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
304 HAVING AVG(t2.c+SUM(t1.b)) > 20)
305
306 is still clear. For a group of rows with the same value for t1.a, calculate
307 the value of SUM(t1.b) as 's'. This value is substituted in the subquery:
308
309 SELECT t2.c FROM t2 GROUP BY t2.c HAVING AVG(t2.c+s)
310
311 By the same reason the following query with a subquery
312
313 SELECT t1.a FROM t1 GROUP BY t1.a
314 HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
315 HAVING AVG(SUM(t1.b)) > 20)
316 is also valid.
317
318 IMPLEMENTATION NOTES
319
320 The member base_query_block contains a reference to the query block that the
321 set function is contained within.
322
323 The member aggr_query_block contains a reference to the query block where the
324 set function is aggregated.
325
326 The field max_aggr_level holds the maximum of the nest levels of the
327 unbound column references contained in the set function. A column reference
328 is unbound within a set function if it is not bound by any subquery
329 used as a subexpression in this function. A column reference is bound by
330 a subquery if it is a reference to the column by which the aggregation
331 of some set function that is used in the subquery is calculated.
332 For the set function used in the query
333
334 SELECT t1.a FROM t1 GROUP BY t1.a
335 HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
336 HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
337 HAVING SUM(t1.a+t2.b) < t3.c))
338
339 the value of max_aggr_level is equal to 1 since t1.a is bound in the main
340 query, and t2.b is bound by the first subquery whose nest level is 1.
341 Obviously a set function cannot be aggregated in a subquery whose
342 nest level is less than max_aggr_level. (Yet it can be aggregated in the
343 subqueries whose nest level is greater than max_aggr_level.)
344 In the query
345 SELECT t1.a FROM t1 HAVING AVG(t1.a+(SELECT MIN(t2.c) FROM t2))
346
347 the value of the max_aggr_level for the AVG set function is 0 since
348 the reference t2.c is bound in the subquery.
349
350 If a set function contains no column references (like COUNT(*)),
351 max_aggr_level is -1.
352
353 The field 'max_sum_func_level' is to contain the maximum of the
354 nest levels of the set functions that are used as subexpressions of
355 the arguments of the given set function, but not aggregated in any
356 subquery within this set function. A nested set function s1 can be
357 used within set function s0 only if s1.max_sum_func_level <
358 s0.max_sum_func_level. Set function s1 is considered as nested
359 for set function s0 if s1 is not calculated in any subquery
360 within s0.
361
362 A set function that is used as a subexpression in an argument of another
363 set function refers to the latter via the field 'in_sum_func'.
364
365 The condition imposed on the usage of set functions are checked when
366 we traverse query subexpressions with the help of the recursive method
367 fix_fields. When we apply this method to an object of the class
368 Item_sum, first, on the descent, we call the method init_sum_func_check
369 that initialize members used at checking. Then, on the ascent, we
370 call the method check_sum_func that validates the set function usage
371 and reports an error if it is invalid.
372 The method check_sum_func serves to link the items for the set functions
373 that are aggregated in the containing query blocks. Circular chains of such
374 functions are attached to the corresponding Query_block structures
375 through the field inner_sum_func_list.
376
377 Exploiting the fact that the members mentioned above are used in one
378 recursive function we could have allocated them on the thread stack.
379 Yet we don't do it now.
380
381 It is assumed that the nesting level of subqueries does not exceed 63
382 (valid nesting levels are stored in a 64-bit bitmap called nesting_map).
383 The assumption is enforced in LEX::new_query().
384
385 WINDOW FUNCTIONS
386
387 Most set functions (e.g. SUM, COUNT, AVG) can also be used as window
388 functions. In that case, notable differences compared to set functions are:
389 - not using any Aggregator
390 - not supporting DISTINCT
391 - val_*() does more than returning the function's current value: it
392 first accumulates the function's argument into the function's
393 state. Execution (e.g. end_write_wf()) manipulates temporary tables which
394 contain input for WFs; each input row is passed to copy_funcs() which calls
395 the WF's val_*() to accumulate it.
396*/
397
398class Item_sum : public Item_func {
400 friend class Aggregator_simple;
401
402 protected:
403 /**
404 Aggregator class instance. Not set initially. Allocated only after
405 it is determined if the incoming data are already distinct.
406 */
407 Aggregator *aggr{nullptr};
408
409 /**
410 If sum is a window function, this field contains the window.
411 */
413 /**
414 True if we have already resolved this window functions window reference.
415 Used in execution of prepared statement to avoid re-resolve.
416 */
417 bool m_window_resolved{false};
418
419 private:
420 /**
421 Used in making ROLLUP. Set for the ROLLUP copies of the original
422 Item_sum and passed to create_tmp_field() to cause it to work
423 over the temp table buffer that is referenced by
424 Item_result_field::result_field.
425 */
426 bool force_copy_fields{false};
427
428 /**
429 Indicates how the aggregate function was specified by the parser :
430 true if it was written as AGGREGATE(DISTINCT),
431 false if it was AGGREGATE()
432 */
433 bool with_distinct{false};
434
435 public:
437 bool has_with_distinct() const { return with_distinct; }
438
440 COUNT_FUNC, // COUNT
441 COUNT_DISTINCT_FUNC, // COUNT (DISTINCT)
442 SUM_FUNC, // SUM
443 SUM_DISTINCT_FUNC, // SUM (DISTINCT)
444 AVG_FUNC, // AVG
445 AVG_DISTINCT_FUNC, // AVG (DISTINCT)
446 MIN_FUNC, // MIN
447 MAX_FUNC, // MAX
448 STD_FUNC, // STD/STDDEV/STDDEV_POP
449 VARIANCE_FUNC, // VARIANCE/VAR_POP and VAR_SAMP
450 SUM_BIT_FUNC, // BIT_AND, BIT_OR and BIT_XOR
451 UDF_SUM_FUNC, // user defined functions
452 GROUP_CONCAT_FUNC, // GROUP_CONCAT
453 JSON_AGG_FUNC, // JSON_ARRAYAGG and JSON_OBJECTAGG
454 ROW_NUMBER_FUNC, // Window functions
465 };
466
467 /**
468 @note most member variables below serve only for grouped aggregate
469 functions.
470 */
471
472 /**
473 For a group aggregate which is aggregated into an outer query
474 block; none, or just the first or both cells may be non-zero. They are
475 filled with references to the group aggregate (for example if it is the
476 argument of a function; it is then a pointer to that function's args[i]
477 pointer).
478 */
480 /// next in the circular chain of registered objects
482 Item_sum *in_sum_func; ///< the containing set function if any
483 Query_block *base_query_block; ///< query block where function is placed
484 /**
485 For a group aggregate, query block where function is aggregated. For a
486 window function, nullptr, as such function is always aggregated in
487 base_query_block, as it mustn't contain any outer reference.
488 */
490 int8 max_aggr_level; ///< max level of unbound column references
491 int8
492 max_sum_func_level; ///< max level of aggregation for contained functions
493 bool allow_group_via_temp_table; ///< If incremental update of fields is
494 ///< supported.
495 /**
496 WFs are forbidden when resolving Item_sum; this member is used to restore
497 WF allowance status afterwards.
498 */
500
501 protected:
502 /**
503 True means that this field has been evaluated during optimization.
504 When set, used_tables() returns zero and const_item() returns true.
505 The value must be reset to false after execution.
506 */
507 bool forced_const{false};
508 static ulonglong ram_limitation(THD *thd);
509
510 public:
511 void mark_as_sum_func();
513
514 Item_sum(const POS &pos, PT_window *w)
516
520 }
521
522 Item_sum(const POS &pos, Item *a, PT_window *w)
523 : Item_func(pos, a), m_window(w), allow_group_via_temp_table(true) {}
524
525 Item_sum(const POS &pos, Item *a, Item *b, PT_window *w)
526 : Item_func(pos, a, b), m_window(w), allow_group_via_temp_table(true) {}
527
528 Item_sum(const POS &pos, PT_item_list *opt_list, PT_window *w);
529
530 /// Copy constructor, need to perform subqueries with temporary tables
531 Item_sum(THD *thd, const Item_sum *item);
532
533 bool itemize(Parse_context *pc, Item **res) override;
534 Type type() const override { return SUM_FUNC_ITEM; }
535 virtual enum Sumfunctype sum_func() const = 0;
536
537 // Differs only for Item_rollup_sum_switcher.
538 virtual enum Sumfunctype real_sum_func() const { return sum_func(); }
539
540 /**
541 Resets the aggregate value to its default and aggregates the current
542 value of its attribute(s).
543 */
544 inline bool reset_and_add() {
546 return aggregator_add();
547 }
548
549 /*
550 Called when new group is started and results are being saved in
551 a temporary table. Similarly to reset_and_add() it resets the
552 value to its default and aggregates the value of its
553 attribute(s), but must also store it in result_field.
554 This set of methods (result_item(), reset_field, update_field()) of
555 Item_sum is used only if allow_group_via_temp_table is true. Otherwise
556 copy_or_same() is used to obtain a copy of this item.
557 */
558 virtual void reset_field() = 0;
559 /*
560 Called for each new value in the group, when temporary table is in use.
561 Similar to add(), but uses temporary table field to obtain current value,
562 Updated value is then saved in the field.
563 */
564 virtual void update_field() = 0;
565 virtual bool keep_field_type() const { return false; }
566 bool resolve_type(THD *) override;
567 virtual Item *result_item(Field *field) {
568 Item_field *item = new Item_field(field);
569 if (item == nullptr) return nullptr;
570 // Aggregated fields have no reference to an underlying table
571 assert(item->original_db_name() == nullptr &&
572 item->original_table_name() == nullptr);
573 // Break the connection to the original field since this is an aggregation
574 item->set_original_field_name(nullptr);
575 return item;
576 }
577 table_map used_tables() const override {
578 return forced_const ? 0 : used_tables_cache;
579 }
580 table_map not_null_tables() const override { return used_tables(); }
581 void update_used_tables() override;
582 void fix_after_pullout(Query_block *parent_query_block,
583 Query_block *removed_query_block) override;
585 bool is_null() override { return null_value; }
586
587 void make_const() {
588 // "forced_const" will make used_tables() return zero for this object
589 forced_const = true;
590 }
591 void print(const THD *thd, String *str,
592 enum_query_type query_type) const override;
593 bool eq(const Item *item, bool binary_cmp) const override;
594 /**
595 Mark an aggregate as having no rows.
596
597 This function is called by the execution engine to assign 'NO ROWS
598 FOUND' value to an aggregate item, when the underlying result set
599 has no rows. Such value, in a general case, may be different from
600 the default value of the item after 'clear()': e.g. a numeric item
601 may be initialized to 0 by clear() and to NULL by
602 no_rows_in_result().
603 */
604 void no_rows_in_result() override {
608 }
609 virtual void make_unique() { force_copy_fields = true; }
610 virtual Field *create_tmp_field(bool group, TABLE *table);
611
612 /// argument used by walk method collect_grouped_aggregates ("cga")
614 /// accumulated all aggregates found
615 std::vector<Item_sum *> list;
616 std::set<Item_sum *> aggregates_that_were_hidden;
617 /**
618 The query block we walk from. All found aggregates must aggregate in
619 this; if some aggregate in outer query blocks, break off transformation.
620 */
622 /// true: break off transformation
623 bool m_break_off{false};
624 /// true: an aggregate aggregates outside m_query_block
625 bool m_outside{false};
627 : m_query_block(select) {}
628 };
629
630 bool collect_grouped_aggregates(uchar *) override;
631 Item *replace_aggregate(uchar *) override;
632 bool collect_scalar_subqueries(uchar *) override;
634
635 bool clean_up_after_removal(uchar *arg) override;
636 bool aggregate_check_group(uchar *arg) override;
637 bool aggregate_check_distinct(uchar *arg) override;
638 bool has_aggregate_ref_in_group_by(uchar *arg) override;
639 bool init_sum_func_check(THD *thd);
640 bool check_sum_func(THD *thd, Item **ref);
641
642 Item *set_arg(THD *thd, uint i, Item *new_val) override;
643 /// @todo delete this when we no longer support temporary transformations
644 Item **get_arg_ptr(uint i) { return &args[i]; }
645
646 bool fix_fields(THD *thd, Item **ref) override;
647
648 /**
649 Signal to the function that its arguments may have changed,
650 and that any internal caches etc. based on those arguments
651 must be updated accordingly.
652
653 This is used by the hypergraph optimizer when it rewrites
654 arguments to window functions to take into account that they
655 have been materialized into temporary tables, or that they
656 should read their values from the framebuffer.
657 */
659
660 /**
661 Called to initialize the aggregator.
662 */
663
664 virtual bool aggregator_setup(THD *thd) { return aggr->setup(thd); }
665
666 /**
667 Called to cleanup the aggregator.
668 */
669
670 inline void aggregator_clear() { aggr->clear(); }
671
672 /**
673 Called to add value to the aggregator.
674 */
675
676 inline bool aggregator_add() { return aggr->add(); }
677
678 /* stores the declared DISTINCT flag (from the parser) */
679 void set_distinct(bool distinct) {
680 with_distinct = distinct;
682 }
683
684 /*
685 Set the type of aggregation : DISTINCT or not.
686
687 May be called multiple times.
688 */
689
690 virtual int set_aggregator(Aggregator::Aggregator_type aggregator);
691
692 virtual void clear() = 0;
693 virtual bool add() = 0;
694 virtual bool setup(THD *) { return false; }
695
696 /**
697 Only relevant for aggregates qua window functions. Checks semantics after
698 windows have been set up and checked. Window functions have specific
699 requirements on the window specifications. Used at resolution time.
700
701 @param thd Current thread
702 @param select The current select
703 @param [out] reqs Holds collected requirements from this wf
704
705 @returns true if error
706 */
707 virtual bool check_wf_semantics1(THD *thd, Query_block *select,
709
710 /**
711 Like check_wf_semantics1.
712 For checks which cannot be done in resolution phase (mostly those for
713 input parameters which can be '?' and must be >=0: value isn't known
714 before execution phase).
715 */
717 [[maybe_unused]]) {
718 return false;
719 }
720
721 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
722 mem_root_deque<Item *> *fields) override;
723
724 void cleanup() override;
725
726 Window *window() { return m_window; }
727 const Window *window() const { return m_window; }
728 bool reset_wf_state(uchar *arg) override;
729
730 /**
731 All aggregates are framing, i.e. they work on the window's frame. If none
732 is defined, the frame is by default the entire partition, unless ORDER BY
733 is defined, in which case it is the set of rows from the start of the
734 partition to and including the peer set of the current row.
735
736 Some window functions are not framing, i.e. they always work on the entire
737 partition. For such window functions, the method is overridden to
738 return false.
739 */
740 virtual bool framing() const { return true; }
741
742 /**
743 Only for framing window functions. True if this function only needs to
744 read one row per frame.
745 */
746 virtual bool uses_only_one_row() const { return false; }
747
748 /**
749 Return true if we need to make two passes over the rows in the partition -
750 either because we need the cardinality of it (and we need to read all
751 rows to detect the next partition), or we need to have all partition rows
752 available to evaluate the window function for some other reason, e.g.
753 we may need the last row in the partition in the frame buffer to be able
754 to evaluate LEAD.
755 */
756 virtual bool needs_partition_cardinality() const { return false; }
757
758 /**
759 Common initial actions for window functions. For non-buffered processing
760 ("on-the-fly"), check partition change and possible reset partition
761 state. In this case return false.
762 For buffered processing, if windowing state m_do_copy_null is true, set
763 null_value to is_nullable() and return true.
764
765 @return true if case two above holds, else false
766 */
767 bool wf_common_init();
768
769 /// Overridden by Item_rollup_sum_switcher.
770 virtual bool is_rollup_sum_wrapper() const { return false; }
771 /**
772 * In case we are an Item_rollup_sum_switcher,
773 * return the underlying Item_sum, otherwise, return this.
774 * Overridden by Item_rollup_sum_switcher.
775 */
776 virtual const Item_sum *unwrap_sum() const { return this; }
777 /// Non-const version
778 virtual Item_sum *unwrap_sum() { return this; }
779
780 protected:
781 /*
782 Raise an error (ER_NOT_SUPPORTED_YET) with the detail that this
783 function is not yet supported as a window function.
784 */
786 char buff[STRING_BUFFER_USUAL_SIZE];
787 snprintf(buff, sizeof(buff), "%s as window function", func_name());
788 my_error(ER_NOT_SUPPORTED_YET, MYF(0), buff);
789 }
790};
791
792class Unique;
793
794/**
795 The distinct aggregator.
796 Implements AGGFN (DISTINCT ..)
797 Collects all the data into an Unique (similarly to what Item_sum_distinct
798 does currently) and then (if applicable) iterates over the list of
799 unique values and pumps them back into its object
800*/
801
803 friend class Item_sum_sum;
804
805 /*
806 flag to prevent consecutive runs of endup(). Normally in endup there are
807 expensive calculations (like walking the distinct tree for example)
808 which we must do only once if there are no data changes.
809 We can re-use the data for the second and subsequent val_xxx() calls.
810 endup_done set to true also means that the calculated values for
811 the aggregate functions are correct and don't need recalculation.
812 */
814
815 /*
816 Used depending on the type of the aggregate function and the presence of
817 blob columns in it:
818 - For COUNT(DISTINCT) and no blob fields this points to a real temporary
819 table. It's used as a hash table.
820 - For AVG/SUM(DISTINCT) or COUNT(DISTINCT) with blob fields only the
821 in-memory data structure of a temporary table is constructed.
822 It's used by the Field classes to transform data into row format.
823 */
825
826 /*
827 An array of field lengths on row allocated and used only for
828 COUNT(DISTINCT) with multiple columns and no blobs. Used in
829 Aggregator_distinct::composite_key_cmp (called from Unique to compare
830 nodes
831 */
833
834 /*
835 Used in conjunction with 'table' to support the access to Field classes
836 for COUNT(DISTINCT). Needed by copy_fields()/copy_funcs().
837 */
839
840 /*
841 If there are no blobs in the COUNT(DISTINCT) arguments, we can use a tree,
842 which is faster than heap table. In that case, we still use the table
843 to help get things set up, but we insert nothing in it.
844 For AVG/SUM(DISTINCT) we always use this tree (as it takes a single
845 argument) to get the distinct rows.
846 */
848
849 /*
850 The length of the temp table row. Must be a member of the class as it
851 gets passed down to simple_raw_key_cmp () as a compare function argument
852 to Unique. simple_raw_key_cmp () is used as a fast comparison function
853 when the entire row can be binary compared.
854 */
856
859 /**
860 Set to true if the result is known to be always NULL.
861 If set deactivates creation and usage of the temporary table (in the
862 'table' member) and the Unique instance (in the 'tree' member) as well as
863 the calculation of the final value on the first call to
864 @c Item_sum::val_xxx(),
865 @c Item_avg::val_xxx(),
866 @c Item_count::val_xxx().
867 */
869 /**
870 Set to true if count distinct is on only const items. Distinct on a const
871 value will always be the constant itself. And count distinct of the same
872 would always be 1. Similar to CONST_NULL, it avoids creation of temporary
873 table and the Unique instance.
874 */
877
878 /**
879 When feeding back the data in endup() from Unique/temp table back to
880 Item_sum::add() methods we must read the data from Unique (and not
881 recalculate the functions that are given as arguments to the aggregate
882 function.
883 This flag is to tell the arg_*() methods to take the data from the Unique
884 instead of calling the relevant val_..() method.
885 */
887
888 public:
890 : Aggregator(sum),
891 table(nullptr),
893 tree(nullptr),
895 use_distinct_values(false) {}
896 ~Aggregator_distinct() override;
898
899 bool setup(THD *) override;
900 void clear() override;
901 bool add() override;
902 void endup() override;
903 my_decimal *arg_val_decimal(my_decimal *value) override;
904 double arg_val_real() override;
905 bool arg_is_null(bool use_null_value) override;
906
907 bool unique_walk_function(void *element);
908 static int composite_key_cmp(const void *arg, const void *a, const void *b);
909};
910
911/**
912 The pass-through aggregator.
913 Implements AGGFN (DISTINCT ..) by knowing it gets distinct data on input.
914 So it just pumps them back to the Item_sum descendant class.
915*/
917 public:
920
921 bool setup(THD *thd) override { return item_sum->setup(thd); }
922 void clear() override { item_sum->clear(); }
923 bool add() override { return item_sum->add(); }
924 void endup() override {}
925 my_decimal *arg_val_decimal(my_decimal *value) override;
926 double arg_val_real() override;
927 bool arg_is_null(bool use_null_value) override;
928};
929
930class Item_sum_num : public Item_sum {
932
933 protected:
934 /*
935 val_xxx() functions may be called several times during the execution of a
936 query. Derived classes that require extensive calculation in val_xxx()
937 maintain cache of aggregate value. This variable governs the validity of
938 that cache.
939 */
941
942 public:
943 Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
944 : Item_sum(pos, item_par, window), is_evaluated(false) {}
945
947 : Item_sum(pos, list, w), is_evaluated(false) {}
948
950 : Item_sum(thd, item), is_evaluated(item->is_evaluated) {}
952 return MYSQL_TYPE_DOUBLE;
953 }
954
955 Item_sum_num(Item *item_par) : Item_sum(item_par), is_evaluated(false) {}
956
957 bool fix_fields(THD *, Item **) override;
958 longlong val_int() override {
959 assert(fixed == 1);
960 return llrint_with_overflow_check(val_real()); /* Real as default */
961 }
962 String *val_str(String *str) override;
963 my_decimal *val_decimal(my_decimal *) override;
964 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
965 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
966 }
967 bool get_time(MYSQL_TIME *ltime) override {
968 return get_time_from_numeric(ltime); /* Decimal or real */
969 }
970 void reset_field() override;
971};
972
974 public:
975 Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
976 : Item_sum_num(pos, item_par, w) {
978 }
979
981 : Item_sum_num(pos, list, w) {
983 }
984
985 Item_sum_int(THD *thd, Item_sum_int *item) : Item_sum_num(thd, item) {
987 }
988
989 Item_sum_int(Item *item_par) : Item_sum_num(item_par) {
991 }
992
993 double val_real() override {
994 assert(fixed);
995 return static_cast<double>(val_int());
996 }
997 String *val_str(String *str) override;
998 my_decimal *val_decimal(my_decimal *) override;
999 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1000 return get_date_from_int(ltime, fuzzydate);
1001 }
1002 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
1003 enum Item_result result_type() const override { return INT_RESULT; }
1004};
1005
1007 protected:
1009 double sum;
1012 bool resolve_type(THD *thd) override;
1013 /**
1014 Execution state: this is for counting rows entering and leaving the window
1015 frame, see #m_frame_null_count.
1016 */
1018
1019 /**
1020 Execution state: this is for counting NULLs of rows entering and leaving
1021 the window frame, when we use optimized inverse-based computations. By
1022 comparison with m_count we can know how many non-NULLs are in the frame.
1023 */
1025
1026 public:
1027 Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
1028 : Item_sum_num(pos, item_par, window),
1030 m_count(0),
1032 set_distinct(distinct);
1033 }
1034
1035 Item_sum_sum(THD *thd, Item_sum_sum *item);
1036 enum Sumfunctype sum_func() const override {
1038 }
1039 void clear() override;
1040 bool add() override;
1041 double val_real() override;
1042 longlong val_int() override;
1043 String *val_str(String *str) override;
1044 my_decimal *val_decimal(my_decimal *) override;
1045 enum Item_result result_type() const override { return hybrid_type; }
1046 bool check_wf_semantics1(THD *thd, Query_block *select,
1047 Window_evaluation_requirements *reqs) override;
1048 void no_rows_in_result() override;
1049 void reset_field() override;
1050 void update_field() override;
1051 const char *func_name() const override { return "sum"; }
1052 Item *copy_or_same(THD *thd) override;
1053};
1054
1057
1059
1060 void clear() override;
1061 bool add() override;
1062 void cleanup() override;
1063
1064 public:
1065 Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
1066 : Item_sum_int(pos, item_par, w), count(0) {}
1067 Item_sum_count(Item_int *number) : Item_sum_int(number), count(0) {}
1068 /**
1069 Constructs an instance for COUNT(DISTINCT)
1070
1071 @param pos Position of token in the parser.
1072 @param list A list of the arguments to the aggregate function
1073 @param w A window, if COUNT is used as a windowing function
1074
1075 This constructor is called by the parser only for COUNT (DISTINCT).
1076 */
1077
1079 : Item_sum_int(pos, list, w), count(0) {
1080 set_distinct(true);
1081 }
1083 : Item_sum_int(thd, item), count(item->count) {}
1084 enum Sumfunctype sum_func() const override {
1086 }
1087 bool resolve_type(THD *thd) override {
1088 if (param_type_is_default(thd, 0, -1)) return true;
1089 set_nullable(false);
1090 null_value = false;
1091 return false;
1092 }
1093 void no_rows_in_result() override { count = 0; }
1094 void make_const(longlong count_arg) {
1095 count = count_arg;
1097 }
1098 longlong val_int() override;
1099 void reset_field() override;
1100 void update_field() override;
1101 const char *func_name() const override { return "count"; }
1102 Item *copy_or_same(THD *thd) override;
1103};
1104
1105/* Item to get the value of a stored sum function */
1106
1107class Item_sum_avg;
1108class Item_sum_bit;
1109
1110/**
1111 This is used in connection which a parent Item_sum:
1112 - which can produce different result types (is "hybrid")
1113 - which stores function's value into a temporary table's column (one
1114 row per group).
1115 - which stores in the column some internal piece of information which should
1116 not be returned to the user, so special implementations are needed.
1117*/
1119 protected:
1120 /// The tmp table's column containing the value of the set function.
1122 /// Stores the Item's result type.
1124
1125 public:
1126 enum Item_result result_type() const override { return hybrid_type; }
1127 bool mark_field_in_map(uchar *arg) override {
1128 /*
1129 Filesort (find_all_keys) over a temporary table collects the columns it
1130 needs.
1131 */
1132 return Item::mark_field_in_map(pointer_cast<Mark_field *>(arg), field);
1133 }
1136 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1137 func_arg->banned_function_name = func_name();
1138 return true;
1139 }
1140 void cleanup() override {
1141 field = nullptr;
1143 }
1144};
1145
1146/**
1147 Common abstract class for:
1148 Item_avg_field
1149 Item_variance_field
1150*/
1152 public:
1153 longlong val_int() override {
1154 /* can't be fix_fields()ed */
1156 }
1157 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1158 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
1159 }
1160 bool get_time(MYSQL_TIME *ltime) override {
1161 return get_time_from_numeric(ltime); /* Decimal or real */
1162 }
1163 bool is_null() override { return update_null_value() || null_value; }
1164};
1165
1167 public:
1170 Item_avg_field(Item_result res_type, Item_sum_avg *item);
1171 enum Type type() const override { return FIELD_AVG_ITEM; }
1172 double val_real() override;
1173 my_decimal *val_decimal(my_decimal *) override;
1174 String *val_str(String *) override;
1175 bool resolve_type(THD *) override { return false; }
1176 const char *func_name() const override {
1177 assert(0);
1178 return "avg_field";
1179 }
1180};
1181
1182/// This is used in connection with an Item_sum_bit, @see Item_sum_hybrid_field
1184 protected:
1186
1187 public:
1190 longlong val_int() override;
1191 double val_real() override;
1192 my_decimal *val_decimal(my_decimal *) override;
1193 String *val_str(String *) override;
1194 bool resolve_type(THD *) override { return false; }
1195 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1196 bool get_time(MYSQL_TIME *ltime) override;
1197 enum Type type() const override { return FIELD_BIT_ITEM; }
1198 const char *func_name() const override {
1199 assert(0);
1200 return "sum_bit_field";
1201 }
1202};
1203
1204/// Common abstraction for Item_sum_json_array and Item_sum_json_object
1205class Item_sum_json : public Item_sum {
1207
1208 protected:
1209 /// String used when reading JSON binary values or JSON text values.
1211 /// String used for converting JSON text values to utf8mb4 charset.
1213 /// Wrapper around the container (object/array) which accumulates the value.
1215
1216 /**
1217 Construct an Item_sum_json instance.
1218
1219 @param wrapper a wrapper around the Json_array or Json_object that contains
1220 the aggregated result
1221 @param parent_args arguments to forward to Item_sum's constructor
1222 */
1223 template <typename... Args>
1225 Args &&... parent_args);
1226
1227 public:
1228 ~Item_sum_json() override;
1229 bool fix_fields(THD *thd, Item **pItem) override;
1230 enum Sumfunctype sum_func() const override { return JSON_AGG_FUNC; }
1231 Item_result result_type() const override { return STRING_RESULT; }
1232
1233 double val_real() override;
1234 longlong val_int() override;
1235 String *val_str(String *str) override;
1236 bool val_json(Json_wrapper *wr) override;
1237 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
1238 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1239 bool get_time(MYSQL_TIME *ltime) override;
1240
1241 void reset_field() override;
1242 void update_field() override;
1243
1246};
1247
1248/// Implements aggregation of values into an array.
1250 /// Accumulates the final value.
1252
1253 public:
1254 Item_sum_json_array(THD *thd, Item_sum *item,
1257 Item_sum_json_array(const POS &pos, Item *a, PT_window *w,
1261 const char *func_name() const override { return "json_arrayagg"; }
1262 void clear() override;
1263 bool add() override;
1264 Item *copy_or_same(THD *thd) override;
1265};
1266
1267/// Implements aggregation of values into an object.
1269 /// Accumulates the final value.
1271 /// Buffer used to get the value of the key.
1273 /**
1274 Map of keys in Json_object and the count for each key
1275 within a window frame. It is used in handling rows
1276 leaving a window frame when rows are not sorted
1277 according to the key in Json_object.
1278 */
1279 std::map<std::string, int> m_key_map;
1280 /**
1281 If window provides ordering on the key in Json_object,
1282 a key_map is not needed to handle rows leaving a window
1283 frame. In this case, process_buffered_windowing_record()
1284 will set flags when a key/value pair can be removed from
1285 the Json_object.
1286 */
1287 bool m_optimize{false};
1288
1289 public:
1290 Item_sum_json_object(THD *thd, Item_sum *item,
1293 Item_sum_json_object(const POS &pos, Item *a, Item *b, PT_window *w,
1297 const char *func_name() const override { return "json_objectagg"; }
1298 void clear() override;
1299 bool add() override;
1300 Item *copy_or_same(THD *thd) override;
1301 bool check_wf_semantics1(THD *thd, Query_block *select,
1302 Window_evaluation_requirements *reqs) override;
1303};
1304
1305class Item_sum_avg final : public Item_sum_sum {
1306 public:
1311 double m_avg;
1312
1313 Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
1314 : Item_sum_sum(pos, item_par, distinct, w) {}
1315
1317 : Item_sum_sum(thd, item), prec_increment(item->prec_increment) {}
1318
1319 bool resolve_type(THD *thd) override;
1320 enum Sumfunctype sum_func() const override {
1322 }
1323 void clear() override;
1324 bool add() override;
1325 double val_real() override;
1326 // In SPs we might force the "wrong" type with select into a declare variable
1328 my_decimal *val_decimal(my_decimal *) override;
1329 String *val_str(String *str) override;
1330 void reset_field() override;
1331 void update_field() override;
1332 Item *result_item(Field *) override {
1333 return new Item_avg_field(hybrid_type, this);
1334 }
1335 const char *func_name() const override { return "avg"; }
1336 Item *copy_or_same(THD *thd) override;
1337 Field *create_tmp_field(bool group, TABLE *table) override;
1338 void cleanup() override {
1339 m_count = 0;
1342 }
1343};
1344
1345class Item_sum_variance;
1346
1348 protected:
1350
1351 public:
1353 enum Type type() const override { return FIELD_VARIANCE_ITEM; }
1354 double val_real() override;
1356 my_decimal *val_decimal(my_decimal *dec_buf) override {
1357 return val_decimal_from_real(dec_buf);
1358 }
1359 bool resolve_type(THD *) override { return false; }
1360 const char *func_name() const override {
1361 assert(0);
1362 return "variance_field";
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
1372/*
1373 variance(a) =
1374
1375 = sum (ai - avg(a))^2 / count(a) )
1376 = sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
1377 = (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
1378 = (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
1379 = (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2
1380 )/count(a) = = (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a)
1381 )/count(a) = = (sum(ai^2) - sum(a)^2/count(a))/count(a)
1382
1383 But, this falls prey to catastrophic cancellation.
1384 Instead, we use recurrence formulas in Algorithm I mentoned below
1385 for group aggregates.
1386
1387 Algorithm I:
1388 M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
1389 S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
1390 for 2 <= k <= n newline
1391 ital variance = S_{n} / (n-1)
1392
1393 For aggregate window functions algorithm I cannot be optimized for
1394 moving frames since M_{i} changes for every row. So we use the
1395 following algorithm.
1396
1397 Algorithm II:
1398
1399 K = 0
1400 n = 0
1401 ex = 0
1402 ex2 = 0
1403
1404 def add_sample(x):
1405 if (n == 0):
1406 K = x
1407 n = n + 1
1408 ex += x - K
1409 ex2 += (x - K) * (x - K)
1410
1411 def remove_sample(x):
1412 n = n - 1
1413 ex -= (x - K)
1414 ex2 -= (x - K) * (x - K)
1415
1416 def variance():
1417 return (ex2 - (ex*ex)/n) / (n-1)
1418
1419 This formula facilitates incremental computation enabling us to
1420 optimize in case of moving window frames. The optimized codepath is taken
1421 only when windowing_use_high_precision is set to false. By default,
1422 aggregate window functions take the non-optimized codepath.
1423 Note:
1424 Results could differ between optimized and non-optimized code path.
1425 Hence algorithm II is used only when user sets
1426 windowing_use_high_precision to false.
1427*/
1428
1430 bool resolve_type(THD *) override;
1431
1432 public:
1434 /**
1435 Used in recurrence relation.
1436 */
1437 double recurrence_m{0.0};
1438 double recurrence_s{0.0};
1439 double recurrence_s2{0.0};
1443 /**
1444 If set, uses a algorithm II mentioned in the class description
1445 to calculate the variance which helps in optimizing windowing
1446 functions in presence of frames.
1447 */
1449
1450 Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg,
1451 PT_window *w)
1452 : Item_sum_num(pos, item_par, w),
1454 count(0),
1455 sample(sample_arg),
1456 optimize(false) {}
1457
1459 enum Sumfunctype sum_func() const override { return VARIANCE_FUNC; }
1460 void clear() override;
1461 bool add() override;
1462 double val_real() override;
1463 my_decimal *val_decimal(my_decimal *) override;
1464 void reset_field() override;
1465 void update_field() override;
1466 Item *result_item(Field *) override { return new Item_variance_field(this); }
1467 void no_rows_in_result() override {}
1468 const char *func_name() const override {
1469 return sample ? "var_samp" : "variance";
1470 }
1471 Item *copy_or_same(THD *thd) override;
1472 Field *create_tmp_field(bool group, TABLE *table) override;
1473 enum Item_result result_type() const override { return REAL_RESULT; }
1474 void cleanup() override {
1475 count = 0;
1477 }
1478 bool check_wf_semantics1(THD *thd, Query_block *select,
1479 Window_evaluation_requirements *reqs) override;
1480};
1481
1482class Item_sum_std;
1483
1485 public:
1487 enum Type type() const override { return FIELD_STD_ITEM; }
1488 double val_real() override;
1489 my_decimal *val_decimal(my_decimal *) override;
1490 enum Item_result result_type() const override { return REAL_RESULT; }
1491 const char *func_name() const override {
1492 assert(0);
1493 return "std_field";
1494 }
1497 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1498 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1499 return true;
1500 }
1501};
1502
1503/*
1504 standard_deviation(a) = sqrt(variance(a))
1505*/
1506
1508 public:
1509 Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
1510 : Item_sum_variance(pos, item_par, sample_arg, w) {}
1511
1512 Item_sum_std(THD *thd, Item_sum_std *item) : Item_sum_variance(thd, item) {}
1513 enum Sumfunctype sum_func() const override { return STD_FUNC; }
1514 double val_real() override;
1515 Item *result_item(Field *) override { return new Item_std_field(this); }
1516 const char *func_name() const override {
1517 return sample ? "stddev_samp" : "std";
1518 }
1519 Item *copy_or_same(THD *thd) override;
1520 enum Item_result result_type() const override { return REAL_RESULT; }
1521};
1522
1523// This class is a string or number function depending on num_func
1524class Arg_comparator;
1525
1526/**
1527 Abstract base class for the MIN and MAX aggregate functions.
1528*/
1531
1532 private:
1533 /**
1534 Tells if this is the MIN function (true) or the MAX function (false).
1535 */
1536 const bool m_is_min;
1537 /*
1538 For window functions MIN/MAX with optimized code path, no comparisons
1539 are needed beyond NULL detection: MIN/MAX are then roughly equivalent to
1540 FIRST/LAST_VALUE. For this case, 'value' is the value of
1541 the window function a priori taken from args[0], while arg_cache is used to
1542 remember the value from the previous row. NULLs need a bit of careful
1543 treatment.
1544 */
1548 bool was_values; // Set if we have found at least one row (for max/min only)
1549 /**
1550 Set to true if the window is ordered ascending.
1551 */
1553 /**
1554 Set to true when min/max can be optimized using window's ordering.
1555 */
1557 /**
1558 For min() - Set to true when results are ordered in ascending and
1559 false when descending.
1560 For max() - Set to true when results are ordered in descending and
1561 false when ascending.
1562 Valid only when m_optimize is true.
1563 */
1564 bool m_want_first; ///< Want first non-null value, else last non_null value
1565 /**
1566 Execution state: keeps track if this is the first row in the frame
1567 when buffering is not needed.
1568 Valid only when m_optimize is true.
1569 */
1571
1572 /**
1573 Execution state: keeps track of at which row we saved a non-null last
1574 value.
1575 */
1577
1578 /**
1579 This function implements the optimized version of retrieving min/max
1580 value. When we have "ordered ASC" results in a window, min will always
1581 be the first value in the result set (neglecting the NULL's) and max
1582 will always be the last value (or the other way around, if ordered DESC).
1583 It is based on the implementation of FIRST_VALUE/LAST_VALUE, except for
1584 the NULL handling.
1585
1586 @return true if computation yielded a NULL or error
1587 */
1588 bool compute();
1589
1590 /**
1591 MIN/MAX function setup.
1592
1593 Setup cache/comparator of MIN/MAX functions. When called by the
1594 copy_or_same() function, the value_arg parameter contains the calculated
1595 value of the original MIN/MAX object, and it is saved in this object's
1596 cache.
1597
1598 @param item the argument of the MIN/MAX function
1599 @param value_arg the calculated value of the MIN/MAX function
1600 @return false on success, true on error
1601 */
1602 bool setup_hybrid(Item *item, Item *value_arg);
1603
1604 /** Create a clone of this object. */
1605 virtual Item_sum_hybrid *clone_hybrid(THD *thd) const = 0;
1606
1607 protected:
1608 Item_sum_hybrid(Item *item_par, bool is_min)
1609 : Item_sum(item_par),
1610 m_is_min(is_min),
1611 value(nullptr),
1613 cmp(nullptr),
1615 was_values(true),
1616 m_nulls_first(false),
1617 m_optimize(false),
1618 m_want_first(false),
1619 m_cnt(0),
1622 }
1623
1624 Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
1625 : Item_sum(pos, item_par, w),
1626 m_is_min(is_min),
1627 value(nullptr),
1629 cmp(nullptr),
1631 was_values(true),
1632 m_nulls_first(false),
1633 m_optimize(false),
1634 m_want_first(false),
1635 m_cnt(0),
1638 }
1639
1641 : Item_sum(thd, item),
1642 m_is_min(item->m_is_min),
1643 value(item->value),
1645 hybrid_type(item->hybrid_type),
1646 was_values(item->was_values),
1648 m_optimize(item->m_optimize),
1650 m_cnt(item->m_cnt),
1652
1653 public:
1654 bool fix_fields(THD *, Item **) override;
1655 void clear() override;
1656 void update_after_wf_arguments_changed(THD *thd) override;
1657 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
1658 mem_root_deque<Item *> *fields) override;
1659 double val_real() override;
1660 longlong val_int() override;
1661 longlong val_time_temporal() override;
1662 longlong val_date_temporal() override;
1663 my_decimal *val_decimal(my_decimal *) override;
1664 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1665 bool get_time(MYSQL_TIME *ltime) override;
1666 void reset_field() override;
1667 String *val_str(String *) override;
1668 bool val_json(Json_wrapper *wr) override;
1669 bool keep_field_type() const override { return true; }
1670 enum Item_result result_type() const override { return hybrid_type; }
1671 TYPELIB *get_typelib() const override {
1672 assert(sum_func() == MAX_FUNC || sum_func() == MIN_FUNC);
1673 return arguments()[0]->get_typelib();
1674 }
1675 void update_field() override;
1676 void cleanup() override;
1677 bool any_value() { return was_values; }
1678 void no_rows_in_result() override;
1679 Field *create_tmp_field(bool group, TABLE *table) override;
1680 bool uses_only_one_row() const override { return m_optimize; }
1681 bool add() override;
1682 Item *copy_or_same(THD *thd) override;
1683 bool check_wf_semantics1(THD *thd, Query_block *select,
1685
1686 private:
1687 /*
1688 These functions check if the value on the current row exceeds the maximum or
1689 minimum value seen so far, and update the current max/min stored in
1690 result_field, if needed.
1691 */
1698};
1699
1700class Item_sum_min final : public Item_sum_hybrid {
1701 public:
1702 Item_sum_min(Item *item_par) : Item_sum_hybrid(item_par, true) {}
1703 Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
1704 : Item_sum_hybrid(pos, item_par, true, w) {}
1705 Item_sum_min(THD *thd, const Item_sum_min *item)
1706 : Item_sum_hybrid(thd, item) {}
1707 enum Sumfunctype sum_func() const override { return MIN_FUNC; }
1708 const char *func_name() const override { return "min"; }
1709
1710 private:
1711 Item_sum_min *clone_hybrid(THD *thd) const override;
1712};
1713
1714class Item_sum_max final : public Item_sum_hybrid {
1715 public:
1716 Item_sum_max(Item *item_par) : Item_sum_hybrid(item_par, false) {}
1717 Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
1718 : Item_sum_hybrid(pos, item_par, false, w) {}
1719 Item_sum_max(THD *thd, const Item_sum_max *item)
1720 : Item_sum_hybrid(thd, item) {}
1721 enum Sumfunctype sum_func() const override { return MAX_FUNC; }
1722 const char *func_name() const override { return "max"; }
1723
1724 private:
1725 Item_sum_max *clone_hybrid(THD *thd) const override;
1726};
1727
1728/**
1729 Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
1730
1731 Each of them is both a set function and a framing window function.
1732*/
1733class Item_sum_bit : public Item_sum {
1735 /// Stores the neutral element for function
1737 /// Stores the result value for the INT_RESULT
1739 /// Stores the result value for the STRING_RESULT
1741 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1743 /// Buffer used to avoid String allocation in the constructor
1744 const char initial_value_buff_storage[1] = {0};
1745
1746 /**
1747 Execution state (windowing): this is for counting rows entering and leaving
1748 the window frame, see #m_frame_null_count.
1749 */
1751
1752 /**
1753 Execution state (windowing): this is for counting NULLs of rows entering
1754 and leaving the window frame, when we use optimized inverse-based
1755 computations. By comparison with m_count we can know how many non-NULLs are
1756 in the frame.
1757 */
1759
1760 /**
1761 Execution state (windowing): Used for AND, OR to be able to invert window
1762 functions in optimized mode.
1763
1764 For the optimized code path of BIT_XXX wfs, we keep track of the number of
1765 bit values (0's or 1's; see below) seen in a frame using a 64 bits counter
1766 pr bit. This lets us compute the value of OR by just inspecting:
1767
1768 - the number of 1's in the previous frame
1769 - whether any removed row(s) is a 1
1770 - whether any added row(s) is a 1
1771
1772 Similarly for AND, we keep track of the number of 0's seen for a particular
1773 bit. To do this trick we need a counter per bit position. This array holds
1774 these counters.
1775
1776 Note that for XOR, the inverse operation is identical to the operation,
1777 so we don't need the above.
1778 */
1780 /*
1781 Size of allocated array m_digit_cnt.
1782 The size is DIGIT_CNT_CARD (for integer types) or ::max_length * 8 for bit
1783 strings.
1784 */
1786
1787 static constexpr uint DIGIT_CNT_CARD = sizeof(ulonglong) * 8;
1788
1789 protected:
1790 bool m_is_xor; ///< true iff BIT_XOR
1791
1792 public:
1793 Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg,
1794 PT_window *w)
1795 : Item_sum(pos, item_par, w),
1796 reset_bits(reset_arg),
1797 bits(reset_arg),
1799 m_count(0),
1803 m_is_xor(false) {}
1804
1805 /// Copy constructor, used for executing subqueries with temporary tables
1807 : Item_sum(thd, item),
1808 reset_bits(item->reset_bits),
1809 bits(item->bits),
1811 hybrid_type(item->hybrid_type),
1812 m_count(item->m_count),
1816 m_is_xor(item->m_is_xor) {
1817 /*
1818 This constructor should only be called during the Optimize stage.
1819 Asserting that the item was not evaluated yet.
1820 */
1821 assert(item->value_buff.length() == 1);
1822 assert(item->bits == item->reset_bits);
1823 }
1824
1825 Item *result_item(Field *) override {
1826 return new Item_sum_bit_field(hybrid_type, this, reset_bits);
1827 }
1828
1829 enum Sumfunctype sum_func() const override { return SUM_BIT_FUNC; }
1830 enum Item_result result_type() const override { return hybrid_type; }
1831 void clear() override;
1832 longlong val_int() override;
1833 double val_real() override;
1834 String *val_str(String *str) override;
1835 my_decimal *val_decimal(my_decimal *decimal_value) override;
1836 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1837 bool get_time(MYSQL_TIME *ltime) override;
1838 void reset_field() override;
1839 void update_field() override;
1840 bool resolve_type(THD *) override;
1841 bool fix_fields(THD *thd, Item **ref) override;
1842 void cleanup() override {
1843 bits = reset_bits;
1844 // At end of one execution of statement, free buffer to reclaim memory:
1847 }
1848
1849 /**
1850 Common implementation of Item_sum_or::add, Item_sum_and:add
1851 and Item_sum_xor::add.
1852 */
1853 bool add() override;
1854 /// @returns true iff this is BIT_AND.
1855 inline bool is_and() const { return reset_bits != 0; }
1856
1857 private:
1858 /**
1859 Accumulate the value of 's1' (if in string mode) or of 'b1' (if in integer
1860 mode). Updates 'value_buff' or 'bits'.
1861
1862 @param s1 argument to accumulate
1863 @param b1 argument to accumulate
1864
1865 @returns true if error
1866 */
1867 bool add_bits(const String *s1, ulonglong b1);
1868
1869 /**
1870 For windowing: perform inverse aggregation. "De-accumulate" the value of
1871 's1' (if in string mode) or of 'b1' (if in integer mode). Updates
1872 'value_buff' or 'bits'.
1873
1874 For BIT_XOR we simply apply XOR as it's its inverse operation. For BIT_OR
1875 and BIT_AND we do the rest below.
1876
1877 For each bit in argument, decrement the corresponding bits's counter
1878 ('m_digit_cnt') for that bit as follows: for BIT_AND, decrement the
1879 counter if we see a zero in that bit; for BIT_OR decrement the counter if
1880 we see a 1 in that bit. Next, update 'value_buff' or 'bits' using the
1881 resulting counters: for each bit, for BIT_AND, set bit if we have counter
1882 == 0, i.e. we have no zero bits for that bit in the frame (yet). For
1883 BIT_OR, set bit if we have counter > 0, i.e. at least one row in the frame
1884 has that bit set.
1885
1886 @param s1 the bits to be inverted from the aggregate value
1887 @param b1 the bits to be inverted from the aggregate value
1888 */
1889 void remove_bits(const String *s1, ulonglong b1);
1890};
1891
1892class Item_sum_or final : public Item_sum_bit {
1893 public:
1894 Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
1895 : Item_sum_bit(pos, item_par, 0LL, w) {}
1896
1897 Item_sum_or(THD *thd, Item_sum_or *item) : Item_sum_bit(thd, item) {}
1898 const char *func_name() const override { return "bit_or"; }
1899 Item *copy_or_same(THD *thd) override;
1900};
1901
1902class Item_sum_and final : public Item_sum_bit {
1903 public:
1904 Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
1905 : Item_sum_bit(pos, item_par, ULLONG_MAX, w) {}
1906
1907 Item_sum_and(THD *thd, Item_sum_and *item) : Item_sum_bit(thd, item) {}
1908 const char *func_name() const override { return "bit_and"; }
1909 Item *copy_or_same(THD *thd) override;
1910};
1911
1912class Item_sum_xor final : public Item_sum_bit {
1913 public:
1914 Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
1915 : Item_sum_bit(pos, item_par, 0LL, w) {
1916 m_is_xor = true;
1917 }
1918
1919 Item_sum_xor(THD *thd, Item_sum_xor *item) : Item_sum_bit(thd, item) {}
1920 const char *func_name() const override { return "bit_xor"; }
1921 Item *copy_or_same(THD *thd) override;
1922};
1923
1924/*
1925 User defined aggregates
1926*/
1927
1928class Item_udf_sum : public Item_sum {
1930
1931 protected:
1933
1934 public:
1935 Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1936 : Item_sum(pos, opt_list, nullptr), udf(udf_arg) {
1938 }
1940 : Item_sum(thd, item), udf(item->udf) {
1941 udf.m_original = false;
1942 }
1943 ~Item_udf_sum() override {
1945 }
1946
1947 bool itemize(Parse_context *pc, Item **res) override;
1948 const char *func_name() const override { return udf.name(); }
1949 bool fix_fields(THD *thd, Item **ref) override {
1950 assert(fixed == 0);
1951
1952 if (init_sum_func_check(thd)) return true;
1953
1954 fixed = true;
1955 if (udf.fix_fields(thd, this, this->arg_count, this->args)) return true;
1956
1957 return check_sum_func(thd, ref);
1958 }
1959 enum Sumfunctype sum_func() const override { return UDF_SUM_FUNC; }
1960
1961 void clear() override;
1962 bool add() override;
1963 void reset_field() override {}
1964 void update_field() override {}
1965 void cleanup() override;
1966 void print(const THD *thd, String *str,
1967 enum_query_type query_type) const override;
1968};
1969
1970class Item_sum_udf_float final : public Item_udf_sum {
1971 public:
1972 Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1973 : Item_udf_sum(pos, udf_arg, opt_list) {}
1975 : Item_udf_sum(thd, item) {}
1976 longlong val_int() override {
1977 assert(fixed == 1);
1978 return (longlong)rint(Item_sum_udf_float::val_real());
1979 }
1980 double val_real() override;
1981 String *val_str(String *str) override;
1982 my_decimal *val_decimal(my_decimal *) override;
1983 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1984 return get_date_from_real(ltime, fuzzydate);
1985 }
1986 bool get_time(MYSQL_TIME *ltime) override {
1987 return get_time_from_real(ltime);
1988 }
1989 bool resolve_type(THD *) override {
1992 return false;
1993 }
1994 Item *copy_or_same(THD *thd) override;
1995};
1996
1997class Item_sum_udf_int final : public Item_udf_sum {
1998 public:
1999 Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2000 : Item_udf_sum(pos, udf_arg, opt_list) {}
2002 : Item_udf_sum(thd, item) {}
2003 longlong val_int() override;
2004 double val_real() override {
2005 assert(fixed == 1);
2006 return (double)Item_sum_udf_int::val_int();
2007 }
2008 String *val_str(String *str) override;
2009 my_decimal *val_decimal(my_decimal *) override;
2010 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2011 return get_date_from_int(ltime, fuzzydate);
2012 }
2013 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2014 enum Item_result result_type() const override { return INT_RESULT; }
2015 bool resolve_type(THD *) override {
2017 return false;
2018 }
2019 Item *copy_or_same(THD *thd) override;
2020};
2021
2022class Item_sum_udf_str final : public Item_udf_sum {
2023 public:
2024 Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2025 : Item_udf_sum(pos, udf_arg, opt_list) {}
2027 : Item_udf_sum(thd, item) {}
2028 String *val_str(String *) override;
2029 double val_real() override {
2030 int err_not_used;
2031 const char *end_not_used;
2032 String *res;
2033 res = val_str(&str_value);
2034 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2035 &end_not_used, &err_not_used)
2036 : 0.0;
2037 }
2038 longlong val_int() override {
2039 int err_not_used;
2040 String *res;
2041 const CHARSET_INFO *cs;
2042
2043 if (!(res = val_str(&str_value))) return 0; /* Null value */
2044 cs = res->charset();
2045 const char *end = res->ptr() + res->length();
2046 return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
2047 }
2049 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2050 return get_date_from_string(ltime, fuzzydate);
2051 }
2052 bool get_time(MYSQL_TIME *ltime) override {
2053 return get_time_from_string(ltime);
2054 }
2055 enum Item_result result_type() const override { return STRING_RESULT; }
2056 bool resolve_type(THD *) override;
2057 Item *copy_or_same(THD *thd) override;
2058};
2059
2061 public:
2062 Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg,
2063 PT_item_list *opt_list)
2064 : Item_udf_sum(pos, udf_arg, opt_list) {}
2066 : Item_udf_sum(thd, item) {}
2067 String *val_str(String *) override;
2068 double val_real() override;
2069 longlong val_int() override;
2070 my_decimal *val_decimal(my_decimal *) override;
2071 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2072 return get_date_from_decimal(ltime, fuzzydate);
2073 }
2074 bool get_time(MYSQL_TIME *ltime) override {
2075 return get_time_from_decimal(ltime);
2076 }
2077 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2078 bool resolve_type(THD *) override {
2081 return false;
2082 }
2083 Item *copy_or_same(THD *thd) override;
2084};
2085
2086int group_concat_key_cmp_with_distinct(const void *arg, const void *key1,
2087 const void *key2);
2088int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2089 const void *key2);
2090int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2091 void *item_arg);
2092
2093class Item_func_group_concat final : public Item_sum {
2095
2096 /// True if GROUP CONCAT has the DISTINCT attribute
2098 /// True if the result is always NULL
2099 bool always_null{false};
2100 /// The number of ORDER BY items.
2102 /// The number of selected items, aka the concat field list
2104 /// Resolver context, points to containing query block
2106 /// String containing separator between group items
2108 /// Describes the temporary table used to perform group concat
2112 TREE *tree{nullptr};
2113
2114 /**
2115 If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
2116 out duplicates.
2117 @see Item_func_group_concat::setup
2118 @see Item_func_group_concat::add
2119 @see Item_func_group_concat::clear
2120 */
2122 /// Temporary table used to perform group concat
2123 TABLE *table{nullptr};
2126 /**
2127 The maximum permitted result length in bytes as set in
2128 group_concat_max_len system variable
2129 */
2131 bool warning_for_row{false};
2133 /// True if result has been written to output buffer.
2135 /**
2136 Following is 0 normal object and pointer to original one for copy
2137 (to correctly free resources)
2138 */
2140
2141 friend int group_concat_key_cmp_with_distinct(const void *arg,
2142 const void *key1,
2143 const void *key2);
2144 friend int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2145 const void *key2);
2146 friend int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2147 void *item_arg);
2148
2149 public:
2150 Item_func_group_concat(const POS &pos, bool is_distinct,
2151 PT_item_list *select_list,
2152 PT_order_list *opt_order_list, String *separator,
2153 PT_window *w);
2154
2157 assert(original != nullptr || unique_filter == nullptr);
2158 }
2159
2160 bool itemize(Parse_context *pc, Item **res) override;
2161 void cleanup() override;
2162
2163 enum Sumfunctype sum_func() const override { return GROUP_CONCAT_FUNC; }
2164 const char *func_name() const override { return "group_concat"; }
2165 Item_result result_type() const override { return STRING_RESULT; }
2166 Field *make_string_field(TABLE *table_arg) const override;
2167 void clear() override;
2168 bool add() override;
2169 void reset_field() override { assert(0); } // not used
2170 void update_field() override { assert(0); } // not used
2171 bool fix_fields(THD *, Item **) override;
2172 bool setup(THD *thd) override;
2173 void make_unique() override;
2174 double val_real() override;
2175 longlong val_int() override {
2176 String *res;
2177 int error;
2178 if (!(res = val_str(&str_value))) return (longlong)0;
2179 const char *end_ptr = res->ptr() + res->length();
2180 return my_strtoll10(res->ptr(), &end_ptr, &error);
2181 }
2182 my_decimal *val_decimal(my_decimal *decimal_value) override {
2183 return val_decimal_from_string(decimal_value);
2184 }
2185 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2186 return get_date_from_string(ltime, fuzzydate);
2187 }
2188 bool get_time(MYSQL_TIME *ltime) override {
2189 return get_time_from_string(ltime);
2190 }
2191
2192 bool has_distinct() const noexcept { return distinct; }
2193 const String *get_separator_str() const noexcept { return separator; }
2194 uint32_t get_group_concat_max_len() const noexcept {
2195 return group_concat_max_len;
2196 }
2197 const Mem_root_array<ORDER> &get_order_array() const noexcept {
2198 return order_array;
2199 }
2200
2201 String *val_str(String *str) override;
2202 Item *copy_or_same(THD *thd) override;
2203 void no_rows_in_result() override;
2204 void print(const THD *thd, String *str,
2205 enum_query_type query_type) const override;
2206 bool change_context_processor(uchar *arg) override {
2207 context = pointer_cast<Item_ident::Change_context *>(arg)->m_context;
2208 return false;
2209 }
2210
2212 Window_evaluation_requirements *) override {
2214 return true;
2215 }
2216};
2217
2218/**
2219 Common parent class for window functions that always work on the entire
2220 partition, even if a frame is defined.
2221
2222 The subclasses can be divided in two disjoint sub-categories:
2223 - one-pass
2224 - two-pass (requires partition cardinality to be evaluated)
2225 cf. method needs_partition_cardinality.
2226*/
2229
2230 public:
2231 Item_non_framing_wf(const POS &pos, PT_window *w) : Item_sum(pos, w) {}
2233 : Item_sum(pos, a, w) {}
2235 : Item_sum(pos, opt_list, w) {}
2237
2238 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2239 return get_date_from_numeric(ltime, fuzzydate);
2240 }
2241
2242 bool get_time(MYSQL_TIME *ltime) override {
2243 return get_time_from_numeric(ltime);
2244 }
2245
2246 void reset_field() override { assert(false); }
2247 void update_field() override { assert(false); }
2248 bool add() override {
2249 assert(false);
2250 return false;
2251 }
2252
2253 bool fix_fields(THD *thd, Item **items) override;
2254
2255 bool framing() const override { return false; }
2256};
2257
2258/**
2259 ROW_NUMBER window function, cf. SQL 2003 Section 6.10 <window function>
2260*/
2262 // Execution state variables
2263 ulonglong m_ctr; ///< Increment for each row in partition
2264
2265 public:
2267 : Item_non_framing_wf(pos, w), m_ctr(0) {
2268 unsigned_flag = true;
2269 }
2270
2271 const char *func_name() const override { return "row_number"; }
2272 enum Sumfunctype sum_func() const override { return ROW_NUMBER_FUNC; }
2273
2274 bool resolve_type(THD *thd [[maybe_unused]]) override {
2276 return false;
2277 }
2278
2279 longlong val_int() override;
2280 double val_real() override;
2281 my_decimal *val_decimal(my_decimal *buff) override;
2282 String *val_str(String *) override;
2283
2284 void clear() override;
2285
2286 Item_result result_type() const override { return INT_RESULT; }
2287
2289 Window_evaluation_requirements *) override {
2290 return false;
2291 }
2292};
2293
2294/**
2295 RANK or DENSE_RANK window function, cf. SQL 2003 Section 6.10 <window
2296 function>
2297*/
2300 bool m_dense; ///< If true, the object represents DENSE_RANK
2301 // Execution state variables
2302 ulonglong m_rank_ctr; ///< Increment when window order columns change
2303 ulonglong m_duplicates; ///< Needed to make RANK different from DENSE_RANK
2305 m_previous; ///< Values of previous row's ORDER BY items
2306 public:
2307 Item_rank(const POS &pos, bool dense, PT_window *w)
2308 : Item_non_framing_wf(pos, w),
2309 m_dense(dense),
2310 m_rank_ctr(0),
2311 m_duplicates(0),
2313 unsigned_flag = true;
2314 }
2315
2316 ~Item_rank() override;
2317
2318 const char *func_name() const override {
2319 return m_dense ? "dense_rank" : "rank";
2320 }
2321
2322 enum Sumfunctype sum_func() const override {
2324 }
2325
2326 bool resolve_type(THD *thd [[maybe_unused]]) override {
2328 return false;
2329 }
2330
2331 longlong val_int() override;
2332 double val_real() override;
2333 my_decimal *val_decimal(my_decimal *buff) override;
2334 String *val_str(String *) override;
2335
2336 void update_after_wf_arguments_changed(THD *thd) override;
2337 bool check_wf_semantics1(THD *thd, Query_block *select,
2338 Window_evaluation_requirements *reqs) override;
2339 /**
2340 Clear state for a new partition
2341 */
2342 void clear() override;
2343 Item_result result_type() const override { return INT_RESULT; }
2344};
2345
2346/**
2347 CUME_DIST window function, cf. SQL 2003 Section 6.10 <window function>
2348*/
2351
2352 public:
2354
2355 const char *func_name() const override { return "cume_dist"; }
2356 enum Sumfunctype sum_func() const override { return CUME_DIST_FUNC; }
2357
2358 bool resolve_type(THD *thd [[maybe_unused]]) override {
2360 return false;
2361 }
2362
2363 bool check_wf_semantics1(THD *thd, Query_block *select,
2364 Window_evaluation_requirements *reqs) override;
2365
2366 bool needs_partition_cardinality() const override { return true; }
2367 void clear() override {}
2368 longlong val_int() override;
2369 double val_real() override;
2370 String *val_str(String *) override;
2372 Item_result result_type() const override { return REAL_RESULT; }
2373};
2374
2375/**
2376 PERCENT_RANK window function, cf. SQL 2003 Section 6.10 <window function>
2377*/
2380 // Execution state variables
2381 ulonglong m_rank_ctr; ///< Increment when window order columns change
2382 ulonglong m_peers; ///< Needed to make PERCENT_RANK same for peers
2383 /**
2384 Set when the last peer has been visited. Needed to increment m_rank_ctr.
2385 */
2387
2388 public:
2390 : Item_non_framing_wf(pos, w),
2391 m_rank_ctr(0),
2392 m_peers(0),
2393 m_last_peer_visited(false) {}
2394
2396 const char *func_name() const override { return "percent_rank"; }
2397 enum Sumfunctype sum_func() const override { return PERCENT_RANK_FUNC; }
2398
2399 bool resolve_type(THD *thd [[maybe_unused]]) override {
2401 return false;
2402 }
2403
2404 bool check_wf_semantics1(THD *thd, Query_block *select,
2405 Window_evaluation_requirements *reqs) override;
2406 bool needs_partition_cardinality() const override { return true; }
2407
2408 void clear() override;
2409 longlong val_int() override;
2410 double val_real() override;
2411 String *val_str(String *) override;
2413 Item_result result_type() const override { return REAL_RESULT; }
2414};
2415
2416/**
2417 NTILE window function, cf. SQL 2011 Section 6.10 <window function>
2418*/
2422
2423 public:
2424 Item_ntile(const POS &pos, Item *a, PT_window *w)
2425 : Item_non_framing_wf(pos, a, w), m_value(0) {
2426 unsigned_flag = true;
2427 }
2428
2429 const char *func_name() const override { return "ntile"; }
2430 enum Sumfunctype sum_func() const override { return NTILE_FUNC; }
2431
2432 bool resolve_type(THD *thd) override {
2433 if (args[0]->propagate_type(thd, MYSQL_TYPE_LONGLONG, true)) return true;
2435 return false;
2436 }
2437
2438 bool fix_fields(THD *thd, Item **items) override;
2439
2440 longlong val_int() override;
2441 double val_real() override;
2442 my_decimal *val_decimal(my_decimal *buff) override;
2443 String *val_str(String *) override;
2444
2445 bool check_wf_semantics1(THD *thd, Query_block *select,
2446 Window_evaluation_requirements *reqs) override;
2448 Item_result result_type() const override { return INT_RESULT; }
2449 void clear() override {}
2450 bool needs_partition_cardinality() const override { return true; }
2451};
2452
2453/**
2454 LEAD/LAG window functions, cf. SQL 2011 Section 6.10 <window function>
2455*/
2458 bool m_is_lead; ///< if true, the function is LEAD, else LAG
2459 int64 m_n; ///< canonicalized offset value
2463 /**
2464 Execution state: if set, we already have a value for current row.
2465 State is used to avoid interference with other LEAD/LAG functions on
2466 the same window, since they share the same eval loop and they should
2467 trigger evaluation only when they are on the "right" row relative to
2468 current row. For other offsets, return NULL if we don't know the value
2469 for this function yet, or if we do (m_has_value==true), return the
2470 found value.
2471 */
2473 bool m_use_default; ///< execution state: use default value for current row
2475
2476 public:
2477 Item_lead_lag(const POS &pos, bool lead,
2478 PT_item_list *opt_list, // [0] expr, [1] offset, [2] default
2479 enum_null_treatment null_treatment, PT_window *w)
2480 : Item_non_framing_wf(pos, opt_list, w),
2481 m_null_treatment(null_treatment),
2482 m_is_lead(lead),
2483 m_n(0),
2487 m_has_value(false),
2488 m_use_default(false) {}
2489
2490 const char *func_name() const override {
2491 return (m_is_lead ? "lead" : "lag");
2492 }
2493 enum Sumfunctype sum_func() const override { return LEAD_LAG_FUNC; }
2494
2495 bool resolve_type(THD *thd) override;
2496 bool fix_fields(THD *thd, Item **items) 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 void 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 void 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 void 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
2703/**
2704 A wrapper Item that contains a number of aggregate items, one for each level
2705 of rollup (see Item_rollup_group_item for numbering conventions). When
2706 aggregating, every aggregator is either reset or updated as per the correct
2707 level, and when returning a value, the correct child item corresponding to
2708 the current rollup level is queried.
2709 */
2711 public:
2712 explicit Item_rollup_sum_switcher(List<Item> *sum_func_per_level)
2713 : Item_sum((*sum_func_per_level)[0]),
2714 m_num_levels(sum_func_per_level->size()) {
2715 args = (*THR_MALLOC)->ArrayAlloc<Item *>(sum_func_per_level->size());
2716 int i = 0;
2717 for (Item &item : *sum_func_per_level) {
2718 args[i++] = &item;
2719 }
2723 hidden = master()->hidden;
2727 }
2728 double val_real() override;
2729 longlong val_int() override;
2730 String *val_str(String *str) override;
2732 bool val_json(Json_wrapper *result) override;
2733 bool is_null() override;
2734 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2735 bool get_time(MYSQL_TIME *ltime) override;
2736 const char *func_name() const override { return "rollup_sum_switcher"; }
2737 table_map used_tables() const override { return master()->used_tables(); }
2738 Item_result result_type() const override { return master()->result_type(); }
2739 bool resolve_type(THD *) override {
2741 return false;
2742 }
2743 void print(const THD *thd, String *str,
2744 enum_query_type query_type) const override;
2745 Field *create_tmp_field(bool group, TABLE *table) override;
2746
2747 enum Sumfunctype sum_func() const override { return master()->sum_func(); }
2748 enum Sumfunctype real_sum_func() const override {
2750 }
2751 void reset_field() override { assert(false); }
2752 void update_field() override { assert(false); }
2753 void clear() override;
2754 bool add() override {
2755 assert(false);
2756 return true;
2757 }
2758
2759 bool reset_and_add_for_rollup(int last_unchanged_group_item_idx);
2760
2761 int set_aggregator(Aggregator::Aggregator_type aggregator) override;
2762 bool aggregator_setup(THD *thd) override;
2763 inline bool aggregator_add_all() {
2764 for (int i = 0; i < m_num_levels; ++i) {
2765 if (child(i)->aggregator_add()) {
2766 return true;
2767 }
2768 }
2769 return false;
2770 }
2771
2772 // Used when create_tmp_table() needs to delay application of aggregate
2773 // functions to a later stage in the query processing.
2774 Item *get_arg(uint i) override { return master()->get_arg(i); }
2775 const Item *get_arg(uint i) const override { return master()->get_arg(i); }
2776 Item *set_arg(THD *thd, uint i, Item *new_val) override {
2777 Item *ret = nullptr;
2778 for (int j = 0; j < m_num_levels; ++j) {
2779 ret = child(j)->set_arg(thd, i, new_val);
2780 }
2781 return ret; // Return the last one, arbitrarily.
2782 }
2783 uint argument_count() const override { return master()->argument_count(); }
2784
2785 // Used by AggregateIterator.
2787 inline Item_sum *master() const { return child(0); }
2788
2789 bool is_rollup_sum_wrapper() const override { return true; }
2790 const Item_sum *unwrap_sum() const override { return master(); }
2791 Item_sum *unwrap_sum() override { return master(); }
2792
2793 private:
2794 inline Item *current_arg() const;
2795 inline Item_sum *child(size_t i) const {
2796 return down_cast<Item_sum *>(args[i]);
2797 }
2798
2799 const int m_num_levels;
2801};
2802/// Implements ST_Collect which aggregates geometries into Multipoints,
2803/// Multilinestrings, Multipolygons and Geometrycollections.
2804
2806 private:
2807 std::optional<gis::srid_t> srid;
2808 std::unique_ptr<gis::Geometrycollection> m_geometrycollection;
2809 void pop_front();
2810
2811 public:
2813 Item_sum_collect(THD *thd, Item_sum *item) : Item_sum(thd, item) {
2815 }
2816 Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
2817 : Item_sum(pos, a, w) {
2818 set_distinct(distinct);
2820 }
2821
2822 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2823 longlong val_int() override { return val_int_from_string(); }
2824 double val_real() override { return val_real_from_string(); }
2825 bool get_date(MYSQL_TIME *, my_time_flags_t) override { return true; }
2826 bool get_time(MYSQL_TIME *) override { return true; }
2827 enum Sumfunctype sum_func() const override { return GEOMETRY_AGGREGATE_FUNC; }
2828 Item_result result_type() const override { return STRING_RESULT; }
2830 /*
2831 In contrast to Item_sum, Item_sum_collect always uses Aggregator_simple,
2832 and only needs to reset its aggregator when called.
2833 */
2834 if (aggr) {
2835 aggr->clear();
2836 return false;
2837 }
2838
2839 destroy(aggr);
2840 aggr = new (*THR_MALLOC) Aggregator_simple(this);
2841 return aggr ? false : true;
2842 }
2843
2844 bool fix_fields(THD *thd, Item **ref) override;
2845 /*
2846 We need to override check_wf_semantics1 because it reports an error when
2847 with_distinct is set.
2848 */
2849 bool check_wf_semantics1(THD *thd, Query_block *,
2851
2852 Item *copy_or_same(THD *thd) override;
2853
2854 void update_field() override;
2855 void reset_field() override;
2856
2857 String *val_str(String *str) override;
2858
2859 bool add() override;
2860
2861 void read_result_field();
2862 void store_result_field();
2863
2864 void clear() override;
2865 const char *func_name() const override { return "st_collect"; }
2866};
2867
2868#endif /* ITEM_SUM_INCLUDED */
The distinct aggregator.
Definition: item_sum.h:802
uint tree_key_length
Definition: item_sum.h:855
uint32 * field_lengths
Definition: item_sum.h:832
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2147
bool endup_done
Definition: item_sum.h:813
Const_distinct
Definition: item_sum.h:857
@ CONST_NULL
Set to true if the result is known to be always NULL.
Definition: item_sum.h:868
@ CONST_NOT_NULL
Set to true if count distinct is on only const items.
Definition: item_sum.h:875
@ NOT_CONST
Definition: item_sum.h:858
static int composite_key_cmp(const void *arg, const void *a, const void *b)
Correctly compare composite keys.
Definition: item_sum.cc:954
void endup() override
Calculate the aggregate function value.
Definition: item_sum.cc:1326
~Aggregator_distinct() override
Definition: item_sum.cc:2097
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:886
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2142
void clear() override
Invalidate calculated value and clear the distinct rows.
Definition: item_sum.cc:1225
bool setup(THD *) override
Called before feeding the first row.
Definition: item_sum.cc:1025
bool unique_walk_function(void *element)
Aggregate a distinct row from the distinct hash table.
Definition: item_sum.cc:2090
Unique * tree
Definition: item_sum.h:847
Temp_table_param * tmp_table_param
Definition: item_sum.h:838
bool add() override
Process incoming row.
Definition: item_sum.cc:1257
TABLE * table
Definition: item_sum.h:824
Aggregator_type Aggrtype() override
Definition: item_sum.h:897
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2137
enum Aggregator_distinct::Const_distinct const_distinct
Aggregator_distinct(Item_sum *sum)
Definition: item_sum.h:889
The pass-through aggregator.
Definition: item_sum.h:916
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:922
Aggregator_simple(Item_sum *sum)
Definition: item_sum.h:918
bool setup(THD *thd) override
Called before adding the first row.
Definition: item_sum.h:921
Aggregator_type Aggrtype() override
Definition: item_sum.h:919
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2114
void endup() override
Called when there are no more data and the final value is to be retrieved.
Definition: item_sum.h:924
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2118
bool add() override
Called when there's a new value to be aggregated.
Definition: item_sum.h:923
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2122
The abstract base class for the Aggregator_* classes.
Definition: item_sum.h:100
Item_sum * item_sum
Definition: item_sum.h:112
virtual bool arg_is_null(bool use_null_value)=0
NULLness of being-aggregated argument.
Aggregator_type
Definition: item_sum.h:118
@ SIMPLE_AGGREGATOR
Definition: item_sum.h:118
@ DISTINCT_AGGREGATOR
Definition: item_sum.h:118
virtual bool add()=0
Called when there's a new value to be aggregated.
virtual void endup()=0
Called when there are no more data and the final value is to be retrieved.
virtual Aggregator_type Aggrtype()=0
virtual ~Aggregator()=default
virtual my_decimal * arg_val_decimal(my_decimal *value)=0
Decimal value of being-aggregated argument.
virtual void clear()=0
Called when we need to wipe out all the data from the aggregator: all the values accumulated and all ...
Aggregator(Item_sum *arg)
Definition: item_sum.h:115
virtual double arg_val_real()=0
Floating point value of being-aggregated argument.
virtual bool setup(THD *)=0
Called before adding the first row.
Definition: item_cmpfunc.h:139
void set(const DTCollation &dt)
Definition: item.h:193
Definition: field.h:574
Definition: item_sum.h:1166
const char * func_name() const override
Definition: item_sum.h:1176
uint prec_increment
Definition: item_sum.h:1169
uint f_precision
Definition: item_sum.h:1168
uint dec_bin_size
Definition: item_sum.h:1168
uint f_scale
Definition: item_sum.h:1168
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3687
String * val_str(String *) override
Definition: item_sum.cc:3702
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1175
enum Type type() const override
Definition: item_sum.h:1171
double val_real() override
Definition: item_sum.cc:3672
Item_avg_field(Item_result res_type, Item_sum_avg *item)
Definition: item_sum.cc:3653
Definition: item.h:6585
CUME_DIST window function, cf.
Definition: item_sum.h:2349
Item_cume_dist(const POS &pos, PT_window *w)
Definition: item_sum.h:2353
Item_non_framing_wf super
Definition: item_sum.h:2350
double val_real() override
Definition: item_sum.cc:4859
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2356
void clear() override
Definition: item_sum.h:2367
const char * func_name() const override
Definition: item_sum.h:2355
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:2366
String * val_str(String *) override
Definition: item_sum.cc:4879
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:4849
longlong val_int() override
Definition: item_sum.cc:4871
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2358
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:4883
Item_result result_type() const override
Definition: item_sum.h:2372
Definition: item.h:4103
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:5083
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:5120
double val_real() override
Definition: item_sum.cc:5169
String * val_str(String *str) override
Definition: item_sum.cc:5224
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:5137
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:5069
bool compute()
Core logic of FIRST/LAST_VALUE window functions.
Definition: item_sum.cc:5141
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:5200
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:5131
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2598
void 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:5112
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:5159
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5210
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5190
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5093
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5179
Definition: item_sum.h:2093
void clear() override
Definition: item_sum.cc:4349
bool m_result_finalized
True if result has been written to output buffer.
Definition: item_sum.h:2134
String result
Definition: item_sum.h:2110
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:2139
longlong val_int() override
Definition: item_sum.h:2175
const String * get_separator_str() const noexcept
Definition: item_sum.h:2193
uint32_t get_group_concat_max_len() const noexcept
Definition: item_sum.h:2194
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.h:2182
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:4095
uint row_count
Definition: item_sum.h:2125
void make_unique() override
Definition: item_sum.cc:4624
bool change_context_processor(uchar *arg) override
Definition: item_sum.h:2206
bool setup(THD *thd) override
Definition: item_sum.cc:4502
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize() but with additional parameter.
Definition: item_sum.cc:4223
Item_sum super
Definition: item_sum.h:2094
bool distinct
True if GROUP CONCAT has the DISTINCT attribute.
Definition: item_sum.h:2097
TABLE * table
Temporary table used to perform group concat.
Definition: item_sum.h:2123
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:4017
double val_real() override
Definition: item_sum.cc:4632
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2163
String * val_str(String *str) override
Definition: item_sum.cc:4639
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:4052
bool force_copy_fields
Definition: item_sum.h:2132
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:4410
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2185
Unique * unique_filter
If DISTINCT is used with this GROUP_CONCAT, this member is used to filter out duplicates.
Definition: item_sum.h:2121
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2211
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:4347
Mem_root_array< ORDER > order_array
Definition: item_sum.h:2124
TREE tree_base
Definition: item_sum.h:2111
void update_field() override
Definition: item_sum.h:2170
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:4664
bool always_null
True if the result is always NULL.
Definition: item_sum.h:2099
Temp_table_param * tmp_table_param
Describes the temporary table used to perform group concat.
Definition: item_sum.h:2109
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:4276
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4339
const char * func_name() const override
Definition: item_sum.h:2164
Item_result result_type() const override
Definition: item_sum.h:2165
Field * make_string_field(TABLE *table_arg) const override
Create a field to hold a string value from an item.
Definition: item_sum.cc:4306
uint m_order_arg_count
The number of ORDER BY items.
Definition: item_sum.h:2101
TREE * tree
Definition: item_sum.h:2112
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:4185
bool has_distinct() const noexcept
Definition: item_sum.h:2192
bool add() override
Definition: item_sum.cc:4361
Name_resolution_context * context
Resolver context, points to containing query block.
Definition: item_sum.h:2105
void reset_field() override
Definition: item_sum.h:2169
uint m_field_arg_count
The number of selected items, aka the concat field list.
Definition: item_sum.h:2103
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:2130
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2188
bool warning_for_row
Definition: item_sum.h:2131
String * separator
String containing separator between group items.
Definition: item_sum.h:2107
~Item_func_group_concat() override
Definition: item_sum.h:2156
const Mem_root_array< ORDER > & get_order_array() const noexcept
Definition: item_sum.h:2197
Class for implementation of the GROUPING function.
Definition: item_sum.h:2689
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:6253
longlong val_int() override
Evaluation of the GROUPING function.
Definition: item_sum.cc:6202
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:6147
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:6223
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_sum.cc:6265
enum Functype functype() const override
Definition: item_sum.h:2695
Definition: item_func.h:93
Item ** args
Array of pointers to arguments.
Definition: item_func.h:100
virtual Item * get_arg(uint i)
Get the i'th argument of the function that this object represents.
Definition: item_func.h:438
Functype
Definition: item_func.h:178
@ GROUPING_FUNC
Definition: item_func.h:231
virtual uint argument_count() const
Definition: item_func.h:126
table_map used_tables_cache
Value used in calculation of result of used_tables()
Definition: item_func.h:164
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:516
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:492
Item ** arguments() const
Definition: item_func.h:127
virtual void fix_num_length_and_dec()
Definition: item_func.cc:832
const char * original_db_name() const
Definition: item.h:3982
void set_original_field_name(const char *name_arg)
Definition: item.h:3979
const char * original_table_name() const
Definition: item.h:3983
Definition: item_func.h:928
Definition: item.h:4856
LEAD/LAG window functions, cf.
Definition: item_sum.h:2456
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5495
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:2458
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:2490
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5616
void set_use_default(bool value)
Definition: item_sum.h:2531
String * val_str(String *str) override
Definition: item_sum.cc:5608
bool m_use_default
execution state: use default value for current row
Definition: item_sum.h:2473
enum Item_result result_type() const override
Definition: item_sum.h:2502
Item_cache * m_default
Definition: item_sum.h:2462
bool compute()
Core logic of LEAD/LAG window functions.
Definition: item_sum.cc:5643
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:2477
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:5555
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2493
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5625
enum_null_treatment m_null_treatment
Definition: item_sum.h:2457
void clear() override
Definition: item_sum.cc:5567
bool use_default() const
Definition: item_sum.h:2532
bool m_has_value
Execution state: if set, we already have a value for current row.
Definition: item_sum.h:2472
longlong val_int() override
Definition: item_sum.cc:5579
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5595
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5504
Item_result m_hybrid_type
Definition: item_sum.h:2460
int64 m_n
canonicalized offset value
Definition: item_sum.h:2459
double val_real() override
Definition: item_sum.cc:5587
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:5442
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5633
bool setup_lead_lag()
Definition: item_sum.cc:5539
Item_cache * m_value
Definition: item_sum.h:2461
void 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:5532
Item_non_framing_wf super
Definition: item_sum.h:2474
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:5574
Common parent class for window functions that always work on the entire partition,...
Definition: item_sum.h:2227
Item_non_framing_wf(const POS &pos, PT_window *w)
Definition: item_sum.h:2231
bool add() override
Definition: item_sum.h:2248
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2242
Item_non_framing_wf(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2232
Item_non_framing_wf(const POS &pos, PT_item_list *opt_list, PT_window *w)
Definition: item_sum.h:2234
Item_sum super
Definition: item_sum.h:2228
Item_non_framing_wf(THD *thd, Item_non_framing_wf *i)
Definition: item_sum.h:2236
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2238
void update_field() override
Definition: item_sum.h:2247
void reset_field() override
Definition: item_sum.h:2246
bool framing() const override
All aggregates are framing, i.e.
Definition: item_sum.h:2255
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:4700
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:5335
String * val_str(String *str) override
Definition: item_sum.cc:5402
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:4963
enum Item_result result_type() const override
Definition: item_sum.h:2649
Item_result m_hybrid_type
Definition: item_sum.h:2618
void 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:5287
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5388
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5422
bool setup_nth()
Definition: item_sum.cc:5294
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5247
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:5378
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:5432
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:5368
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:5305
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5234
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:5311
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:5412
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:5315
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:2419
void clear() override
Definition: item_sum.h:2449
Item_non_framing_wf super
Definition: item_sum.h:2420
String * val_str(String *) override
Definition: item_sum.cc:5038
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2432
const char * func_name() const override
Definition: item_sum.h:2429
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:2450
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5053
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:4981
double val_real() override
Definition: item_sum.cc:5033
longlong val_int() override
Definition: item_sum.cc:4987
longlong m_value
Definition: item_sum.h:2421
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:5040
Item_ntile(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2424
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2430
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:5045
Item_result result_type() const override
Definition: item_sum.h:2448
PERCENT_RANK window function, cf.
Definition: item_sum.h:2378
String * val_str(String *) override
Definition: item_sum.cc:4946
Item_percent_rank(const POS &pos, PT_window *w)
Definition: item_sum.h:2389
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2381
const char * func_name() const override
Definition: item_sum.h:2396
Item_result result_type() const override
Definition: item_sum.h:2413
~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:4888
ulonglong m_peers
Needed to make PERCENT_RANK same for peers.
Definition: item_sum.h:2382
double val_real() override
Definition: item_sum.cc:4912
Item_non_framing_wf super
Definition: item_sum.h:2379
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2399
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2397
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:2406
bool m_last_peer_visited
Set when the last peer has been visited.
Definition: item_sum.h:2386
void clear() override
Definition: item_sum.cc:4955
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:4950
longlong val_int() override
Definition: item_sum.cc:4938
RANK or DENSE_RANK window function, cf.
Definition: item_sum.h:2298
Item_result result_type() const override
Definition: item_sum.h:2343
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:4755
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:4768
ulonglong m_duplicates
Needed to make RANK different from DENSE_RANK.
Definition: item_sum.h:2303
bool m_dense
If true, the object represents DENSE_RANK.
Definition: item_sum.h:2300
longlong val_int() override
Definition: item_sum.cc:4786
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2322
~Item_rank() override
Definition: item_sum.cc:4842
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2326
Item_rank(const POS &pos, bool dense, PT_window *w)
Definition: item_sum.h:2307
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4821
const char * func_name() const override
Definition: item_sum.h:2318
double val_real() override
Definition: item_sum.cc:4814
String * val_str(String *) override
Definition: item_sum.cc:4819
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2302
void clear() override
Clear state for a new partition.
Definition: item_sum.cc:4826
Mem_root_array< Cached_item * > m_previous
Values of previous row's ORDER BY items.
Definition: item_sum.h:2305
Item_non_framing_wf super
Definition: item_sum.h:2299
Item with result field.
Definition: item.h:5566
longlong llrint_with_overflow_check(double realval)
Definition: item.h:5617
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item.cc:10588
virtual const char * func_name() const =0
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2710
table_map used_tables() const override
Definition: item_sum.h:2737
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:6342
void update_field() override
Definition: item_sum.h:2752
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2739
Item * get_arg(uint i) override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2774
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.h:2776
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.cc:6328
void set_current_rollup_level(int level)
Definition: item_sum.h:2786
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:6282
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:6333
bool aggregator_setup(THD *thd) override
Called to initialize the aggregator.
Definition: item_sum.cc:6375
Item_result result_type() const override
Definition: item_sum.h:2738
String * val_str(String *str) override
Definition: item_sum.cc:6307
int set_aggregator(Aggregator::Aggregator_type aggregator) override
Definition: item_sum.cc:6364
Item_sum * unwrap_sum() override
Non-const version.
Definition: item_sum.h:2791
longlong val_int() override
Definition: item_sum.cc:6300
void reset_field() override
Definition: item_sum.h:2751
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:2790
Item_rollup_sum_switcher(List< Item > *sum_func_per_level)
Definition: item_sum.h:2712
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:6288
bool aggregator_add_all()
Definition: item_sum.h:2763
Item * current_arg() const
Definition: item_sum.cc:6277
Item_sum * master() const
Definition: item_sum.h:2787
Item_sum * child(size_t i) const
Definition: item_sum.h:2795
enum Sumfunctype real_sum_func() const override
Definition: item_sum.h:2748
bool is_rollup_sum_wrapper() const override
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:2789
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2747
const char * func_name() const override
Definition: item_sum.h:2736
int m_current_rollup_level
Definition: item_sum.h:2800
double val_real() override
Definition: item_sum.cc:6293
bool val_json(Json_wrapper *result) override
Get a JSON value from an Item.
Definition: item_sum.cc:6321
uint argument_count() const override
Definition: item_sum.h:2783
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:6314
bool add() override
Definition: item_sum.h:2754
void clear() override
Definition: item_sum.cc:6346
bool reset_and_add_for_rollup(int last_unchanged_group_item_idx)
Definition: item_sum.cc:6352
const int m_num_levels
Definition: item_sum.h:2799
const Item * get_arg(uint i) const override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2775
ROW_NUMBER window function, cf.
Definition: item_sum.h:2261
ulonglong m_ctr
Increment for each row in partition.
Definition: item_sum.h:2263
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2274
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2272
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2288
Item_row_number(const POS &pos, PT_window *w)
Definition: item_sum.h:2266
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4748
Item_result result_type() const override
Definition: item_sum.h:2286
String * val_str(String *) override
Definition: item_sum.cc:4744
longlong val_int() override
Definition: item_sum.cc:4725
void clear() override
Definition: item_sum.cc:4753
const char * func_name() const override
Definition: item_sum.h:2271
double val_real() override
Definition: item_sum.cc:4739
Definition: item_sum.h:1484
enum Type type() const override
Definition: item_sum.h:1487
Item_std_field(Item_sum_std *item)
Definition: item_sum.cc:3800
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:1495
enum Item_result result_type() const override
Definition: item_sum.h:1490
double val_real() override
Definition: item_sum.cc:3803
const char * func_name() const override
Definition: item_sum.h:1491
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3811
Definition: item_sum.h:1902
Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1904
const char * func_name() const override
Definition: item_sum.h:1908
Item_sum_and(THD *thd, Item_sum_and *item)
Definition: item_sum.h:1907
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3268
Definition: item_sum.h:1305
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2215
void clear() override
Definition: item_sum.cc:2270
String * val_str(String *str) override
Definition: item_sum.cc:2389
const char * func_name() const override
Definition: item_sum.h:1335
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3497
Item_sum_sum super
Definition: item_sum.h:1309
Item * result_item(Field *) override
Definition: item_sum.h:1332
double val_real() override
Definition: item_sum.cc:2279
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1338
Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
Definition: item_sum.h:1313
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:2248
my_decimal m_avg_dec
Definition: item_sum.h:1310
uint f_scale
Definition: item_sum.h:1308
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1320
double m_avg
Definition: item_sum.h:1311
longlong val_int() override
Definition: item_sum.h:1327
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2241
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2304
void reset_field() override
Definition: item_sum.cc:3397
uint prec_increment
Definition: item_sum.h:1307
uint dec_bin_size
Definition: item_sum.h:1308
bool add() override
Definition: item_sum.cc:2272
Item_sum_avg(THD *thd, Item_sum_avg *item)
Definition: item_sum.h:1316
uint f_precision
Definition: item_sum.h:1308
This is used in connection with an Item_sum_bit,.
Definition: item_sum.h:1183
double val_real() override
Definition: item_sum.cc:3743
enum Type type() const override
Definition: item_sum.h:1197
ulonglong reset_bits
Definition: item_sum.h:1185
Item_sum_bit_field(Item_result res_type, Item_sum_bit *item, ulonglong reset_bits)
Definition: item_sum.cc:3708
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1194
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3760
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3786
longlong val_int() override
Definition: item_sum.cc:3728
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3793
String * val_str(String *) override
Definition: item_sum.cc:3768
const char * func_name() const override
Definition: item_sum.h:1198
Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
Definition: item_sum.h:1733
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3157
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:1588
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1842
void remove_bits(const String *s1, ulonglong b1)
For windowing: perform inverse aggregation.
Definition: item_sum.cc:1510
const char initial_value_buff_storage[1]
Buffer used to avoid String allocation in the constructor.
Definition: item_sum.h:1744
static constexpr uint DIGIT_CNT_CARD
Definition: item_sum.h:1787
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1829
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1454
bool m_is_xor
true iff BIT_XOR
Definition: item_sum.h:1790
void clear() override
Definition: item_sum.cc:3240
Item * result_item(Field *) override
Definition: item_sum.h:1825
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:1429
longlong val_int() override
Definition: item_sum.cc:3215
Item_sum super
Definition: item_sum.h:1734
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:1779
Item_result hybrid_type
Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT.
Definition: item_sum.h:1742
ulonglong reset_bits
Stores the neutral element for function.
Definition: item_sum.h:1736
Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg, PT_window *w)
Definition: item_sum.h:1793
bool is_and() const
Definition: item_sum.h:1855
void reset_field() override
Definition: item_sum.cc:3425
ulonglong m_count
Execution state (windowing): this is for counting rows entering and leaving the window frame,...
Definition: item_sum.h:1750
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3164
double val_real() override
Definition: item_sum.cc:3189
bool add() override
Common implementation of Item_sum_or::add, Item_sum_and:add and Item_sum_xor::add.
Definition: item_sum.cc:1672
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.cc:3171
enum Item_result result_type() const override
Definition: item_sum.h:1830
uint m_digit_cnt_card
Definition: item_sum.h:1785
Item_sum_bit(THD *thd, Item_sum_bit *item)
Copy constructor, used for executing subqueries with temporary tables.
Definition: item_sum.h:1806
String * val_str(String *str) override
Definition: item_sum.cc:3126
void update_field() override
Definition: item_sum.cc:3435
ulonglong bits
Stores the result value for the INT_RESULT.
Definition: item_sum.h:1738
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:1758
String value_buff
Stores the result value for the STRING_RESULT.
Definition: item_sum.h:1740
Implements ST_Collect which aggregates geometries into Multipoints, Multilinestrings,...
Definition: item_sum.h:2805
bool add() override
Definition: item_sum.cc:6450
void clear() override
Definition: item_sum.cc:6444
void read_result_field()
Definition: item_sum.cc:6502
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:6413
bool get_date(MYSQL_TIME *, my_time_flags_t) override
Definition: item_sum.h:2825
std::optional< gis::srid_t > srid
Definition: item_sum.h:2807
Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
Definition: item_sum.h:2816
void update_field() override
Definition: item_sum.cc:6592
longlong val_int() override
Definition: item_sum.h:2823
bool get_time(MYSQL_TIME *) override
Definition: item_sum.h:2826
std::unique_ptr< gis::Geometrycollection > m_geometrycollection
Definition: item_sum.h:2808
String * val_str(String *str) override
Definition: item_sum.cc:6545
void reset_field() override
Definition: item_sum.cc:6655
Item_sum_collect(THD *thd, Item_sum *item)
Definition: item_sum.h:2813
void store_result_field()
Definition: item_sum.cc:6598
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:6650
bool check_wf_semantics1(THD *thd, Query_block *, Window_evaluation_requirements *r) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:6435
const char * func_name() const override
Definition: item_sum.h:2865
void pop_front()
Definition: item_sum.cc:6538
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2827
Item_result result_type() const override
Definition: item_sum.h:2828
int set_aggregator(Aggregator::Aggregator_type) override
Definition: item_sum.h:2829
double val_real() override
Definition: item_sum.h:2824
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6497
Definition: item_sum.h:1055
longlong count
Definition: item_sum.h:1056
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1087
longlong val_int() override
Definition: item_sum.cc:2177
void update_field() override
Definition: item_sum.cc:3488
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2158
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1084
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:2209
void make_const(longlong count_arg)
Definition: item_sum.h:1094
void reset_field() override
Definition: item_sum.cc:3389
Item_sum_count(Item_int *number)
Definition: item_sum.h:1067
Item_sum_count(THD *thd, Item_sum_count *item)
Definition: item_sum.h:1082
Item_sum_count(const POS &pos, PT_item_list *list, PT_window *w)
Constructs an instance for COUNT(DISTINCT)
Definition: item_sum.h:1078
void clear() override
Definition: item_sum.cc:2166
const char * func_name() const override
Definition: item_sum.h:1101
Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1065
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1093
bool add() override
Definition: item_sum.cc:2168
This is used in connection which a parent Item_sum:
Definition: item_sum.h:1118
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1140
Item_result hybrid_type
Stores the Item's result type.
Definition: item_sum.h:1123
enum Item_result result_type() const override
Definition: item_sum.h:1126
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:1134
Field * field
The tmp table's column containing the value of the set function.
Definition: item_sum.h:1121
bool mark_field_in_map(uchar *arg) override
Mark underlying field in read or write map of a table.
Definition: item_sum.h:1127
Abstract base class for the MIN and MAX aggregate functions.
Definition: item_sum.h:1529
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3079
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:3074
bool setup_hybrid(Item *item, Item *value_arg)
MIN/MAX function setup.
Definition: item_sum.cc:1764
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:1680
virtual Item_sum_hybrid * clone_hybrid(THD *thd) const =0
Create a clone of this object.
bool add() override
Definition: item_sum.cc:3109
const bool m_is_min
Tells if this is the MIN function (true) or the MAX function (false).
Definition: item_sum.h:1536
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3012
bool compute()
This function implements the optimized version of retrieving min/max value.
Definition: item_sum.cc:2814
void min_max_update_int_field()
Definition: item_sum.cc:3619
longlong val_int() override
Definition: item_sum.cc:2947
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3002
void reset_field() override
Definition: item_sum.cc:3292
void clear() override
Definition: item_sum.cc:2765
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:1576
Item_cache * arg_cache
Definition: item_sum.h:1545
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3060
void min_max_update_json_field()
Definition: item_sum.cc:3572
void update_field() override
Definition: item_sum.cc:3533
longlong val_date_temporal() override
Return date value of item in packed longlong format.
Definition: item_sum.cc:2973
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:2775
bool keep_field_type() const override
Definition: item_sum.h:1669
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2985
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:1570
void min_max_update_real_field()
Definition: item_sum.cc:3606
Item_sum_hybrid(THD *thd, const Item_sum_hybrid *item)
Definition: item_sum.h:1640
bool any_value()
Definition: item_sum.h:1677
Item_result hybrid_type
Definition: item_sum.h:1547
bool was_values
Definition: item_sum.h:1548
longlong val_time_temporal() override
Return time value of item in packed longlong format.
Definition: item_sum.cc:2961
bool m_optimize
Set to true when min/max can be optimized using window's ordering.
Definition: item_sum.h:1556
Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
Definition: item_sum.h:1624
Item_sum_hybrid(Item *item_par, bool is_min)
Definition: item_sum.h:1608
Item_sum super
Definition: item_sum.h:1530
void min_max_update_temporal_field()
Definition: item_sum.cc:3554
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:2780
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1733
String * val_str(String *) override
Definition: item_sum.cc:3022
Arg_comparator * cmp
Definition: item_sum.h:1546
void min_max_update_str_field()
Definition: item_sum.cc:3590
enum Item_result result_type() const override
Definition: item_sum.h:1670
void min_max_update_decimal_field()
Definition: item_sum.cc:3637
double val_real() override
Definition: item_sum.cc:2933
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:1781
bool m_want_first
For min() - Set to true when results are ordered in ascending and false when descending.
Definition: item_sum.h:1564
Item_cache * value
Definition: item_sum.h:1545
bool m_nulls_first
Set to true if the window is ordered ascending.
Definition: item_sum.h:1552
void 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:3049
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.h:1671
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:3035
Definition: item_sum.h:973
Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:975
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:999
Item_sum_int(THD *thd, Item_sum_int *item)
Definition: item_sum.h:985
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1002
double val_real() override
Definition: item_sum.h:993
Item_sum_int(Item *item_par)
Definition: item_sum.h:989
enum Item_result result_type() const override
Definition: item_sum.h:1003
String * val_str(String *str) override
Definition: item_sum.cc:1396
Item_sum_int(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:980
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1398
Implements aggregation of values into an array.
Definition: item_sum.h:1249
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:5883
~Item_sum_json_array() override
bool add() override
Definition: item_sum.cc:5956
unique_ptr_destroy_only< Json_array > m_json_array
Accumulates the final value.
Definition: item_sum.h:1251
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6005
const char * func_name() const override
Definition: item_sum.h:1261
void clear() override
Definition: item_sum.cc:5898
Implements aggregation of values into an object.
Definition: item_sum.h:1268
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:5907
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6113
bool add() override
Definition: item_sum.cc:6018
unique_ptr_destroy_only< Json_object > m_json_object
Accumulates the final value.
Definition: item_sum.h:1270
~Item_sum_json_object() override
const char * func_name() const override
Definition: item_sum.h:1297
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:5932
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:1279
void clear() override
Definition: item_sum.cc:5922
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:1287
String m_tmp_key_value
Buffer used to get the value of the key.
Definition: item_sum.h:1272
Common abstraction for Item_sum_json_array and Item_sum_json_object.
Definition: item_sum.h:1205
void reset_field() override
Definition: item_sum.cc:5843
void update_field() override
Definition: item_sum.cc:5863
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5761
unique_ptr_destroy_only< Json_wrapper > m_wrapper
Wrapper around the container (object/array) which accumulates the value.
Definition: item_sum.h:1214
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1230
longlong val_int() override
Definition: item_sum.cc:5799
String m_value
String used when reading JSON binary values or JSON text values.
Definition: item_sum.h:1210
String m_conversion_buffer
String used for converting JSON text values to utf8mb4 charset.
Definition: item_sum.h:1212
Item_sum super
Definition: item_sum.h:1206
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5837
~Item_sum_json() override
String * val_str(String *str) override
Definition: item_sum.cc:5741
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5814
Item_result result_type() const override
Definition: item_sum.h:1231
bool fix_fields(THD *thd, Item **pItem) override
Definition: item_sum.cc:5715
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:5710
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5831
Item_sum_json(unique_ptr_destroy_only< Json_wrapper > wrapper, Args &&... parent_args)
Construct an Item_sum_json instance.
Definition: item_sum.cc:5701
double val_real() override
Definition: item_sum.cc:5784
Definition: item_sum.h:1714
Item_sum_max(Item *item_par)
Definition: item_sum.h:1716
Item_sum_max(THD *thd, const Item_sum_max *item)
Definition: item_sum.h:1719
const char * func_name() const override
Definition: item_sum.h:1722
Item_sum_max * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3090
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1721
Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1717
Definition: item_sum.h:1700
Item_sum_min(THD *thd, const Item_sum_min *item)
Definition: item_sum.h:1705
Item_sum_min(Item *item_par)
Definition: item_sum.h:1702
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1707
const char * func_name() const override
Definition: item_sum.h:1708
Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1703
Item_sum_min * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3086
Common abstract class for: Item_avg_field Item_variance_field.
Definition: item_sum.h:1151
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1157
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.h:1163
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1160
longlong val_int() override
Definition: item_sum.h:1153
Definition: item_sum.h:930
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1392
Item_sum_num(THD *thd, Item_sum_num *item)
Definition: item_sum.h:949
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1402
longlong val_int() override
Definition: item_sum.h:958
enum_field_types default_data_type() const override
Get the default data (output) type for the specific item.
Definition: item_sum.h:951
String * val_str(String *str) override
Definition: item_sum.cc:1390
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:964
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:967
Item_sum super
Definition: item_sum.h:931
Item_sum_num(Item *item_par)
Definition: item_sum.h:955
bool is_evaluated
Definition: item_sum.h:940
Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
Definition: item_sum.h:943
void reset_field() override
Definition: item_sum.cc:3279
Item_sum_num(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:946
Definition: item_sum.h:1892
Item_sum_or(THD *thd, Item_sum_or *item)
Definition: item_sum.h:1897
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3254
const char * func_name() const override
Definition: item_sum.h:1898
Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1894
Definition: item_sum.h:1507
double val_real() override
Definition: item_sum.cc:2399
Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1509
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2408
Item_sum_std(THD *thd, Item_sum_std *item)
Definition: item_sum.h:1512
const char * func_name() const override
Definition: item_sum.h:1516
enum Item_result result_type() const override
Definition: item_sum.h:1520
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1513
Item * result_item(Field *) override
Definition: item_sum.h:1515
Definition: item_sum.h:1006
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:1017
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:1913
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:1845
enum Item_result result_type() const override
Definition: item_sum.h:1045
double sum
Definition: item_sum.h:1009
bool add() override
Definition: item_sum.cc:1935
Item_result hybrid_type
Definition: item_sum.h:1008
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:1864
void reset_field() override
Definition: item_sum.cc:3371
String * val_str(String *str) override
Definition: item_sum.cc:2024
longlong val_int() override
Definition: item_sum.cc:1956
my_decimal dec_buffs[2]
Definition: item_sum.h:1010
void clear() override
Definition: item_sum.cc:1852
const char * func_name() const override
Definition: item_sum.h:1051
Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
Definition: item_sum.h:1027
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3459
uint curr_dec_buff
Definition: item_sum.h:1011
ulonglong m_frame_null_count
Execution state: this is for counting NULLs of rows entering and leaving the window frame,...
Definition: item_sum.h:1024
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2030
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1866
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1036
double val_real() override
Definition: item_sum.cc:1980
Definition: item_sum.h:2060
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2071
longlong val_int() override
Definition: item_sum.cc:3928
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2074
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3930
Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2062
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3939
double val_real() override
Definition: item_sum.cc:3926
enum Item_result result_type() const override
Definition: item_sum.h:2077
String * val_str(String *) override
Definition: item_sum.cc:3922
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
Definition: item_sum.h:2065
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2078
Definition: item_sum.h:1970
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1989
longlong val_int() override
Definition: item_sum.h:1976
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3918
String * val_str(String *str) override
Definition: item_sum.cc:3914
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3901
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1983
double val_real() override
Definition: item_sum.cc:3906
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
Definition: item_sum.h:1974
Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1972
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1986
Definition: item_sum.h:1997
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2010
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
Definition: item_sum.h:2001
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2015
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2013
enum Item_result result_type() const override
Definition: item_sum.h:2014
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3943
String * val_str(String *str) override
Definition: item_sum.cc:3955
Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1999
longlong val_int() override
Definition: item_sum.cc:3947
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3959
double val_real() override
Definition: item_sum.h:2004
Definition: item_sum.h:2022
bool resolve_type(THD *) override
Default max_length is max argument length.
Definition: item_sum.cc:3965
double val_real() override
Definition: item_sum.h:2029
String * val_str(String *) override
Definition: item_sum.cc:3981
enum Item_result result_type() const override
Definition: item_sum.h:2055
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2049
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2052
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3973
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:3977
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
Definition: item_sum.h:2026
longlong val_int() override
Definition: item_sum.h:2038
Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2024
Definition: item_sum.h:1429
ulonglong count
Definition: item_sum.h:1440
void update_field() override
Definition: item_sum.cc:2739
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2607
double val_real() override
Definition: item_sum.cc:2684
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1474
void clear() override
Definition: item_sum.cc:2661
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2627
Item * result_item(Field *) override
Definition: item_sum.h:1466
Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1450
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1459
double recurrence_s
Definition: item_sum.h:1438
bool optimize
If set, uses a algorithm II mentioned in the class description to calculate the variance which helps ...
Definition: item_sum.h:1448
const char * func_name() const override
Definition: item_sum.h:1468
bool add() override
Definition: item_sum.cc:2663
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1467
double recurrence_s2
Definition: item_sum.h:1439
Item_result hybrid_type
Definition: item_sum.h:1433
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:2591
double recurrence_m
Used in recurrence relation.
Definition: item_sum.h:1437
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2714
uint prec_increment
Definition: item_sum.h:1442
enum Item_result result_type() const override
Definition: item_sum.h:1473
void reset_field() override
Definition: item_sum.cc:2719
uint sample
Definition: item_sum.h:1441
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:2640
Definition: item_sum.h:1912
const char * func_name() const override
Definition: item_sum.h:1920
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3261
Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1914
Item_sum_xor(THD *thd, Item_sum_xor *item)
Definition: item_sum.h:1919
Class Item_sum is the base class used for special expressions that SQL calls 'set functions'.
Definition: item_sum.h:398
virtual void update_after_wf_arguments_changed(THD *)
Signal to the function that its arguments may have changed, and that any internal caches etc.
Definition: item_sum.h:658
bool collect_grouped_aggregates(uchar *) override
Definition: item_sum.cc:701
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:580
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:410
void 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:884
virtual bool check_wf_semantics2(Window_evaluation_requirements *reqs)
Like check_wf_semantics1.
Definition: item_sum.h:716
void aggregator_clear()
Called to cleanup the aggregator.
Definition: item_sum.h:670
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:776
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:480
bool aggregate_check_distinct(uchar *arg) override
Definition: item_sum.cc:617
bool init_sum_func_check(THD *thd)
Prepare an aggregate function for checking of context.
Definition: item_sum.cc:154
virtual bool aggregator_setup(THD *thd)
Called to initialize the aggregator.
Definition: item_sum.h:664
bool wf_common_init()
Common initial actions for window functions.
Definition: item_sum.cc:905
Item_sum(const POS &pos, PT_window *w)
Definition: item_sum.h:514
bool force_copy_fields
Used in making ROLLUP.
Definition: item_sum.h:426
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:604
bool allow_group_via_temp_table
If incremental update of fields is supported.
Definition: item_sum.h:493
virtual bool add()=0
int8 max_aggr_level
max level of unbound column references
Definition: item_sum.h:490
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:462
void make_const()
Definition: item_sum.h:587
Item * replace_aggregate(uchar *) override
Definition: item_sum.cc:732
Window * window()
Definition: item_sum.h:726
virtual void make_unique()
Definition: item_sum.h:609
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:872
bool reset_and_add()
Resets the aggregate value to its default and aggregates the current value of its attribute(s).
Definition: item_sum.h:544
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.h:585
virtual Item * result_item(Field *field)
Definition: item_sum.h:567
bool forced_const
True means that this field has been evaluated during optimization.
Definition: item_sum.h:507
virtual void update_field()=0
bool check_sum_func(THD *thd, Item **ref)
Validate the semantic requirements of a set function.
Definition: item_sum.cc:247
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:518
void unsupported_as_wf()
Definition: item_sum.h:785
Aggregator * aggr
Aggregator class instance.
Definition: item_sum.h:407
const Window * window() const
Definition: item_sum.h:727
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:756
virtual bool is_rollup_sum_wrapper() const
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:770
bool aggregate_check_group(uchar *arg) override
Definition: item_sum.cc:641
virtual void clear()=0
bool with_distinct
Indicates how the aggregate function was specified by the parser : true if it was written as AGGREGAT...
Definition: item_sum.h:433
virtual enum Sumfunctype real_sum_func() const
Definition: item_sum.h:538
nesting_map save_deny_window_func
WFs are forbidden when resolving Item_sum; this member is used to restore WF allowance status afterwa...
Definition: item_sum.h:499
bool collect_scalar_subqueries(uchar *) override
Definition: item_sum.cc:740
virtual void reset_field()=0
virtual bool framing() const
All aggregates are framing, i.e.
Definition: item_sum.h:740
bool has_with_distinct() const
Definition: item_sum.h:437
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_sum.cc:760
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.cc:830
bool m_window_resolved
True if we have already resolved this window functions window reference.
Definition: item_sum.h:417
virtual Field * create_tmp_field(bool group, TABLE *table)
Definition: item_sum.cc:674
friend class Aggregator_simple
Definition: item_sum.h:400
void add_used_tables_for_aggr_func()
Add used_tables information for aggregate function, based on its aggregated query block.
Definition: item_sum.cc:818
Item ** referenced_by[2]
For a group aggregate which is aggregated into an outer query block; none, or just the first or both ...
Definition: item_sum.h:479
Sumfunctype
Definition: item_sum.h:439
@ COUNT_FUNC
Definition: item_sum.h:440
@ STD_FUNC
Definition: item_sum.h:448
@ AVG_DISTINCT_FUNC
Definition: item_sum.h:445
@ DENSE_RANK_FUNC
Definition: item_sum.h:456
@ FIRST_LAST_VALUE_FUNC
Definition: item_sum.h:461
@ SUM_BIT_FUNC
Definition: item_sum.h:450
@ COUNT_DISTINCT_FUNC
Definition: item_sum.h:441
@ SUM_FUNC
Definition: item_sum.h:442
@ CUME_DIST_FUNC
Definition: item_sum.h:457
@ SUM_DISTINCT_FUNC
Definition: item_sum.h:443
@ UDF_SUM_FUNC
Definition: item_sum.h:451
@ ROLLUP_SUM_SWITCHER_FUNC
Definition: item_sum.h:463
@ JSON_AGG_FUNC
Definition: item_sum.h:453
@ ROW_NUMBER_FUNC
Definition: item_sum.h:454
@ MAX_FUNC
Definition: item_sum.h:447
@ GEOMETRY_AGGREGATE_FUNC
Definition: item_sum.h:464
@ AVG_FUNC
Definition: item_sum.h:444
@ MIN_FUNC
Definition: item_sum.h:446
@ NTILE_FUNC
Definition: item_sum.h:459
@ NTH_VALUE_FUNC
Definition: item_sum.h:462
@ GROUP_CONCAT_FUNC
Definition: item_sum.h:452
@ RANK_FUNC
Definition: item_sum.h:455
@ LEAD_LAG_FUNC
Definition: item_sum.h:460
@ PERCENT_RANK_FUNC
Definition: item_sum.h:458
@ VARIANCE_FUNC
Definition: item_sum.h:449
virtual int set_aggregator(Aggregator::Aggregator_type aggregator)
Definition: item_sum.cc:835
bool aggregator_add()
Called to add value to the aggregator.
Definition: item_sum.h:676
Item_sum(Item *a)
Definition: item_sum.h:517
void set_distinct(bool distinct)
Definition: item_sum.h:679
Item_sum * next_sum
next in the circular chain of registered objects
Definition: item_sum.h:481
Item_sum * in_sum_func
the containing set function if any
Definition: item_sum.h:482
virtual bool keep_field_type() const
Definition: item_sum.h:565
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:750
bool has_force_copy_fields() const
Definition: item_sum.h:436
Type type() const override
Definition: item_sum.h:534
void mark_as_sum_func()
Definition: item_sum.cc:452
Query_block * aggr_query_block
For a group aggregate, query block where function is aggregated.
Definition: item_sum.h:489
PT_window * m_window
If sum is a window function, this field contains the window.
Definition: item_sum.h:412
Item_sum(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:522
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:126
bool reset_wf_state(uchar *arg) override
Reset execution state for such window function types as determined by arg.
Definition: item_sum.cc:892
table_map used_tables() const override
Definition: item_sum.h:577
virtual Item_sum * unwrap_sum()
Non-const version.
Definition: item_sum.h:778
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:781
virtual bool setup(THD *)
Definition: item_sum.h:694
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:862
int8 max_sum_func_level
max level of aggregation for contained functions
Definition: item_sum.h:492
virtual bool uses_only_one_row() const
Only for framing window functions.
Definition: item_sum.h:746
Item ** get_arg_ptr(uint i)
Definition: item_sum.h:644
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize() but with additional parameter.
Definition: item_sum.cc:94
virtual enum Sumfunctype sum_func() const =0
Item_sum(const POS &pos, Item *a, Item *b, PT_window *w)
Definition: item_sum.h:525
bool eq(const Item *item, bool binary_cmp) const override
Definition: item_sum.cc:590
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:666
Query_block * base_query_block
query block where function is placed
Definition: item_sum.h:483
Definition: item_sum.h:1928
udf_handler udf
Definition: item_sum.h:1932
void update_field() override
Definition: item_sum.h:1964
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3881
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize() but with additional parameter.
Definition: item_sum.cc:3859
void reset_field() override
Definition: item_sum.h:1963
bool add() override
Definition: item_sum.cc:3874
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.h:1949
Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1935
void clear() override
Definition: item_sum.cc:3868
~Item_udf_sum() override
Definition: item_sum.h:1943
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:3890
Item_udf_sum(THD *thd, Item_udf_sum *item)
Definition: item_sum.h:1939
Item_sum super
Definition: item_sum.h:1929
const char * func_name() const override
Definition: item_sum.h:1948
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1959
Definition: item_sum.h:1347
my_decimal * val_decimal(my_decimal *dec_buf) override
Definition: item_sum.h:1356
const char * func_name() const override
Definition: item_sum.h:1360
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...<