MySQL 8.0.29
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 acumulated 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
516
520 }
521
522 Item_sum(const POS &pos, Item *a, PT_window *w)
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:
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 */
1442 /**
1443 If set, uses a algorithm II mentioned in the class description
1444 to calculate the variance which helps in optimizing windowing
1445 functions in presence of frames.
1446 */
1448
1449 Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg,
1450 PT_window *w)
1451 : Item_sum_num(pos, item_par, w),
1453 count(0),
1454 sample(sample_arg),
1455 optimize(false) {}
1456
1458 enum Sumfunctype sum_func() const override { return VARIANCE_FUNC; }
1459 void clear() override;
1460 bool add() override;
1461 double val_real() override;
1462 my_decimal *val_decimal(my_decimal *) override;
1463 void reset_field() override;
1464 void update_field() override;
1465 Item *result_item(Field *) override { return new Item_variance_field(this); }
1466 void no_rows_in_result() override {}
1467 const char *func_name() const override {
1468 return sample ? "var_samp" : "variance";
1469 }
1470 Item *copy_or_same(THD *thd) override;
1471 Field *create_tmp_field(bool group, TABLE *table) override;
1472 enum Item_result result_type() const override { return REAL_RESULT; }
1473 void cleanup() override {
1474 count = 0;
1476 }
1477 bool check_wf_semantics1(THD *thd, Query_block *select,
1478 Window_evaluation_requirements *reqs) override;
1479};
1480
1481class Item_sum_std;
1482
1484 public:
1486 enum Type type() const override { return FIELD_STD_ITEM; }
1487 double val_real() override;
1488 my_decimal *val_decimal(my_decimal *) override;
1489 enum Item_result result_type() const override { return REAL_RESULT; }
1490 const char *func_name() const override {
1491 assert(0);
1492 return "std_field";
1493 }
1496 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1497 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1498 return true;
1499 }
1500};
1501
1502/*
1503 standard_deviation(a) = sqrt(variance(a))
1504*/
1505
1507 public:
1508 Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
1509 : Item_sum_variance(pos, item_par, sample_arg, w) {}
1510
1511 Item_sum_std(THD *thd, Item_sum_std *item) : Item_sum_variance(thd, item) {}
1512 enum Sumfunctype sum_func() const override { return STD_FUNC; }
1513 double val_real() override;
1514 Item *result_item(Field *) override { return new Item_std_field(this); }
1515 const char *func_name() const override {
1516 return sample ? "stddev_samp" : "std";
1517 }
1518 Item *copy_or_same(THD *thd) override;
1519 enum Item_result result_type() const override { return REAL_RESULT; }
1520};
1521
1522// This class is a string or number function depending on num_func
1523class Arg_comparator;
1524
1525/**
1526 Abstract base class for the MIN and MAX aggregate functions.
1527*/
1530
1531 private:
1532 /**
1533 Tells if this is the MIN function (true) or the MAX function (false).
1534 */
1535 const bool m_is_min;
1536 /*
1537 For window functions MIN/MAX with optimized code path, no comparisons
1538 are needed beyond NULL detection: MIN/MAX are then roughly equivalent to
1539 FIRST/LAST_VALUE. For this case, 'value' is the value of
1540 the window function a priori taken from args[0], while arg_cache is used to
1541 remember the value from the previous row. NULLs need a bit of careful
1542 treatment.
1543 */
1547 bool was_values; // Set if we have found at least one row (for max/min only)
1548 /**
1549 Set to true if the window is ordered ascending.
1550 */
1552 /**
1553 Set to true when min/max can be optimized using window's ordering.
1554 */
1556 /**
1557 For min() - Set to true when results are ordered in ascending and
1558 false when descending.
1559 For max() - Set to true when results are ordered in descending and
1560 false when ascending.
1561 Valid only when m_optimize is true.
1562 */
1563 bool m_want_first; ///< Want first non-null value, else last non_null value
1564 /**
1565 Execution state: keeps track if this is the first row in the frame
1566 when buffering is not needed.
1567 Valid only when m_optimize is true.
1568 */
1570
1571 /**
1572 Execution state: keeps track of at which row we saved a non-null last
1573 value.
1574 */
1576
1577 /**
1578 This function implements the optimized version of retrieving min/max
1579 value. When we have "ordered ASC" results in a window, min will always
1580 be the first value in the result set (neglecting the NULL's) and max
1581 will always be the last value (or the other way around, if ordered DESC).
1582 It is based on the implementation of FIRST_VALUE/LAST_VALUE, except for
1583 the NULL handling.
1584
1585 @return true if computation yielded a NULL or error
1586 */
1587 bool compute();
1588
1589 /**
1590 MIN/MAX function setup.
1591
1592 Setup cache/comparator of MIN/MAX functions. When called by the
1593 copy_or_same() function, the value_arg parameter contains the calculated
1594 value of the original MIN/MAX object, and it is saved in this object's
1595 cache.
1596
1597 @param item the argument of the MIN/MAX function
1598 @param value_arg the calculated value of the MIN/MAX function
1599 @return false on success, true on error
1600 */
1601 bool setup_hybrid(Item *item, Item *value_arg);
1602
1603 /** Create a clone of this object. */
1604 virtual Item_sum_hybrid *clone_hybrid(THD *thd) const = 0;
1605
1606 protected:
1607 Item_sum_hybrid(Item *item_par, bool is_min)
1608 : Item_sum(item_par),
1609 m_is_min(is_min),
1610 value(nullptr),
1612 cmp(nullptr),
1614 was_values(true),
1615 m_nulls_first(false),
1616 m_optimize(false),
1617 m_want_first(false),
1618 m_cnt(0),
1621 }
1622
1623 Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
1624 : Item_sum(pos, item_par, w),
1625 m_is_min(is_min),
1626 value(nullptr),
1628 cmp(nullptr),
1630 was_values(true),
1631 m_nulls_first(false),
1632 m_optimize(false),
1633 m_want_first(false),
1634 m_cnt(0),
1637 }
1638
1640 : Item_sum(thd, item),
1641 m_is_min(item->m_is_min),
1642 value(item->value),
1644 hybrid_type(item->hybrid_type),
1645 was_values(item->was_values),
1647 m_optimize(item->m_optimize),
1649 m_cnt(item->m_cnt),
1651
1652 public:
1653 bool fix_fields(THD *, Item **) override;
1654 void clear() override;
1655 void update_after_wf_arguments_changed(THD *thd) override;
1656 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
1657 mem_root_deque<Item *> *fields) override;
1658 double val_real() override;
1659 longlong val_int() override;
1660 longlong val_time_temporal() override;
1661 longlong val_date_temporal() override;
1662 my_decimal *val_decimal(my_decimal *) override;
1663 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1664 bool get_time(MYSQL_TIME *ltime) override;
1665 void reset_field() override;
1666 String *val_str(String *) override;
1667 bool val_json(Json_wrapper *wr) override;
1668 bool keep_field_type() const override { return true; }
1669 enum Item_result result_type() const override { return hybrid_type; }
1670 TYPELIB *get_typelib() const override {
1671 assert(sum_func() == MAX_FUNC || sum_func() == MIN_FUNC);
1672 return arguments()[0]->get_typelib();
1673 }
1674 void update_field() override;
1675 void cleanup() override;
1676 bool any_value() { return was_values; }
1677 void no_rows_in_result() override;
1678 Field *create_tmp_field(bool group, TABLE *table) override;
1679 bool uses_only_one_row() const override { return m_optimize; }
1680 bool add() override;
1681 Item *copy_or_same(THD *thd) override;
1682 bool check_wf_semantics1(THD *thd, Query_block *select,
1684
1685 private:
1686 /*
1687 These functions check if the value on the current row exceeds the maximum or
1688 minimum value seen so far, and update the current max/min stored in
1689 result_field, if needed.
1690 */
1697};
1698
1699class Item_sum_min final : public Item_sum_hybrid {
1700 public:
1701 Item_sum_min(Item *item_par) : Item_sum_hybrid(item_par, true) {}
1702 Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
1703 : Item_sum_hybrid(pos, item_par, true, w) {}
1704 Item_sum_min(THD *thd, const Item_sum_min *item)
1705 : Item_sum_hybrid(thd, item) {}
1706 enum Sumfunctype sum_func() const override { return MIN_FUNC; }
1707 const char *func_name() const override { return "min"; }
1708
1709 private:
1710 Item_sum_min *clone_hybrid(THD *thd) const override;
1711};
1712
1713class Item_sum_max final : public Item_sum_hybrid {
1714 public:
1715 Item_sum_max(Item *item_par) : Item_sum_hybrid(item_par, false) {}
1716 Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
1717 : Item_sum_hybrid(pos, item_par, false, w) {}
1718 Item_sum_max(THD *thd, const Item_sum_max *item)
1719 : Item_sum_hybrid(thd, item) {}
1720 enum Sumfunctype sum_func() const override { return MAX_FUNC; }
1721 const char *func_name() const override { return "max"; }
1722
1723 private:
1724 Item_sum_max *clone_hybrid(THD *thd) const override;
1725};
1726
1727/**
1728 Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
1729
1730 Each of them is both a set function and a framing window function.
1731*/
1732class Item_sum_bit : public Item_sum {
1734 /// Stores the neutral element for function
1736 /// Stores the result value for the INT_RESULT
1738 /// Stores the result value for the STRING_RESULT
1740 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1742 /// Buffer used to avoid String allocation in the constructor
1743 const char initial_value_buff_storage[1] = {0};
1744
1745 /**
1746 Execution state (windowing): this is for counting rows entering and leaving
1747 the window frame, see #m_frame_null_count.
1748 */
1750
1751 /**
1752 Execution state (windowing): this is for counting NULLs of rows entering
1753 and leaving the window frame, when we use optimized inverse-based
1754 computations. By comparison with m_count we can know how many non-NULLs are
1755 in the frame.
1756 */
1758
1759 /**
1760 Execution state (windowing): Used for AND, OR to be able to invert window
1761 functions in optimized mode.
1762
1763 For the optimized code path of BIT_XXX wfs, we keep track of the number of
1764 bit values (0's or 1's; see below) seen in a frame using a 64 bits counter
1765 pr bit. This lets us compute the value of OR by just inspecting:
1766
1767 - the number of 1's in the previous frame
1768 - whether any removed row(s) is a 1
1769 - whether any added row(s) is a 1
1770
1771 Similarly for AND, we keep track of the number of 0's seen for a particular
1772 bit. To do this trick we need a counter per bit position. This array holds
1773 these counters.
1774
1775 Note that for XOR, the inverse operation is identical to the operation,
1776 so we don't need the above.
1777 */
1779 /*
1780 Size of allocated array m_digit_cnt.
1781 The size is DIGIT_CNT_CARD (for integer types) or ::max_length * 8 for bit
1782 strings.
1783 */
1785
1786 static constexpr uint DIGIT_CNT_CARD = sizeof(ulonglong) * 8;
1787
1788 protected:
1789 bool m_is_xor; ///< true iff BIT_XOR
1790
1791 public:
1792 Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg,
1793 PT_window *w)
1794 : Item_sum(pos, item_par, w),
1795 reset_bits(reset_arg),
1796 bits(reset_arg),
1798 m_count(0),
1802 m_is_xor(false) {}
1803
1804 /// Copy constructor, used for executing subqueries with temporary tables
1806 : Item_sum(thd, item),
1807 reset_bits(item->reset_bits),
1808 bits(item->bits),
1810 hybrid_type(item->hybrid_type),
1811 m_count(item->m_count),
1815 m_is_xor(item->m_is_xor) {
1816 /*
1817 This constructor should only be called during the Optimize stage.
1818 Asserting that the item was not evaluated yet.
1819 */
1820 assert(item->value_buff.length() == 1);
1821 assert(item->bits == item->reset_bits);
1822 }
1823
1824 Item *result_item(Field *) override {
1825 return new Item_sum_bit_field(hybrid_type, this, reset_bits);
1826 }
1827
1828 enum Sumfunctype sum_func() const override { return SUM_BIT_FUNC; }
1829 enum Item_result result_type() const override { return hybrid_type; }
1830 void clear() override;
1831 longlong val_int() override;
1832 double val_real() override;
1833 String *val_str(String *str) override;
1834 my_decimal *val_decimal(my_decimal *decimal_value) override;
1835 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1836 bool get_time(MYSQL_TIME *ltime) override;
1837 void reset_field() override;
1838 void update_field() override;
1839 bool resolve_type(THD *) override;
1840 bool fix_fields(THD *thd, Item **ref) override;
1841 void cleanup() override {
1842 bits = reset_bits;
1843 // At end of one execution of statement, free buffer to reclaim memory:
1846 }
1847
1848 /**
1849 Common implementation of Item_sum_or::add, Item_sum_and:add
1850 and Item_sum_xor::add.
1851 */
1852 bool add() override;
1853 /// @returns true iff this is BIT_AND.
1854 inline bool is_and() const { return reset_bits != 0; }
1855
1856 private:
1857 /**
1858 Accumulate the value of 's1' (if in string mode) or of 'b1' (if in integer
1859 mode). Updates 'value_buff' or 'bits'.
1860
1861 @param s1 argument to accumulate
1862 @param b1 argument to accumulate
1863
1864 @returns true if error
1865 */
1866 bool add_bits(const String *s1, ulonglong b1);
1867
1868 /**
1869 For windowing: perform inverse aggregation. "De-accumulate" the value of
1870 's1' (if in string mode) or of 'b1' (if in integer mode). Updates
1871 'value_buff' or 'bits'.
1872
1873 For BIT_XOR we simply apply XOR as it's its inverse operation. For BIT_OR
1874 and BIT_AND we do the rest below.
1875
1876 For each bit in argument, decrement the corresponding bits's counter
1877 ('m_digit_cnt') for that bit as follows: for BIT_AND, decrement the
1878 counter if we see a zero in that bit; for BIT_OR decrement the counter if
1879 we see a 1 in that bit. Next, update 'value_buff' or 'bits' using the
1880 resulting counters: for each bit, for BIT_AND, set bit if we have counter
1881 == 0, i.e. we have no zero bits for that bit in the frame (yet). For
1882 BIT_OR, set bit if we have counter > 0, i.e. at least one row in the frame
1883 has that bit set.
1884
1885 @param s1 the bits to be inverted from the aggregate value
1886 @param b1 the bits to be inverted from the aggregate value
1887 */
1888 void remove_bits(const String *s1, ulonglong b1);
1889};
1890
1891class Item_sum_or final : public Item_sum_bit {
1892 public:
1893 Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
1894 : Item_sum_bit(pos, item_par, 0LL, w) {}
1895
1896 Item_sum_or(THD *thd, Item_sum_or *item) : Item_sum_bit(thd, item) {}
1897 const char *func_name() const override { return "bit_or"; }
1898 Item *copy_or_same(THD *thd) override;
1899};
1900
1901class Item_sum_and final : public Item_sum_bit {
1902 public:
1903 Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
1904 : Item_sum_bit(pos, item_par, ULLONG_MAX, w) {}
1905
1906 Item_sum_and(THD *thd, Item_sum_and *item) : Item_sum_bit(thd, item) {}
1907 const char *func_name() const override { return "bit_and"; }
1908 Item *copy_or_same(THD *thd) override;
1909};
1910
1911class Item_sum_xor final : public Item_sum_bit {
1912 public:
1913 Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
1914 : Item_sum_bit(pos, item_par, 0LL, w) {
1915 m_is_xor = true;
1916 }
1917
1918 Item_sum_xor(THD *thd, Item_sum_xor *item) : Item_sum_bit(thd, item) {}
1919 const char *func_name() const override { return "bit_xor"; }
1920 Item *copy_or_same(THD *thd) override;
1921};
1922
1923/*
1924 User defined aggregates
1925*/
1926
1927class Item_udf_sum : public Item_sum {
1929
1930 protected:
1932
1933 public:
1934 Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1935 : Item_sum(pos, opt_list, nullptr), udf(udf_arg) {
1937 }
1939 : Item_sum(thd, item), udf(item->udf) {
1940 udf.m_original = false;
1941 }
1942 ~Item_udf_sum() override {
1944 }
1945
1946 bool itemize(Parse_context *pc, Item **res) override;
1947 const char *func_name() const override { return udf.name(); }
1948 bool fix_fields(THD *thd, Item **ref) override {
1949 assert(fixed == 0);
1950
1951 if (init_sum_func_check(thd)) return true;
1952
1953 fixed = true;
1954 if (udf.fix_fields(thd, this, this->arg_count, this->args)) return true;
1955
1956 return check_sum_func(thd, ref);
1957 }
1958 enum Sumfunctype sum_func() const override { return UDF_SUM_FUNC; }
1959
1960 void clear() override;
1961 bool add() override;
1962 void reset_field() override {}
1963 void update_field() override {}
1964 void cleanup() override;
1965 void print(const THD *thd, String *str,
1966 enum_query_type query_type) const override;
1967};
1968
1969class Item_sum_udf_float final : public Item_udf_sum {
1970 public:
1971 Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1972 : Item_udf_sum(pos, udf_arg, opt_list) {}
1974 : Item_udf_sum(thd, item) {}
1975 longlong val_int() override {
1976 assert(fixed == 1);
1977 return (longlong)rint(Item_sum_udf_float::val_real());
1978 }
1979 double val_real() override;
1980 String *val_str(String *str) override;
1981 my_decimal *val_decimal(my_decimal *) override;
1982 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1983 return get_date_from_real(ltime, fuzzydate);
1984 }
1985 bool get_time(MYSQL_TIME *ltime) override {
1986 return get_time_from_real(ltime);
1987 }
1988 bool resolve_type(THD *) override {
1991 return false;
1992 }
1993 Item *copy_or_same(THD *thd) override;
1994};
1995
1996class Item_sum_udf_int final : public Item_udf_sum {
1997 public:
1998 Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1999 : Item_udf_sum(pos, udf_arg, opt_list) {}
2001 : Item_udf_sum(thd, item) {}
2002 longlong val_int() override;
2003 double val_real() override {
2004 assert(fixed == 1);
2005 return (double)Item_sum_udf_int::val_int();
2006 }
2007 String *val_str(String *str) override;
2008 my_decimal *val_decimal(my_decimal *) override;
2009 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2010 return get_date_from_int(ltime, fuzzydate);
2011 }
2012 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2013 enum Item_result result_type() const override { return INT_RESULT; }
2014 bool resolve_type(THD *) override {
2016 return false;
2017 }
2018 Item *copy_or_same(THD *thd) override;
2019};
2020
2021class Item_sum_udf_str final : public Item_udf_sum {
2022 public:
2023 Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2024 : Item_udf_sum(pos, udf_arg, opt_list) {}
2026 : Item_udf_sum(thd, item) {}
2027 String *val_str(String *) override;
2028 double val_real() override {
2029 int err_not_used;
2030 const char *end_not_used;
2031 String *res;
2032 res = val_str(&str_value);
2033 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2034 &end_not_used, &err_not_used)
2035 : 0.0;
2036 }
2037 longlong val_int() override {
2038 int err_not_used;
2039 String *res;
2040 const CHARSET_INFO *cs;
2041
2042 if (!(res = val_str(&str_value))) return 0; /* Null value */
2043 cs = res->charset();
2044 const char *end = res->ptr() + res->length();
2045 return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
2046 }
2048 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2049 return get_date_from_string(ltime, fuzzydate);
2050 }
2051 bool get_time(MYSQL_TIME *ltime) override {
2052 return get_time_from_string(ltime);
2053 }
2054 enum Item_result result_type() const override { return STRING_RESULT; }
2055 bool resolve_type(THD *) override;
2056 Item *copy_or_same(THD *thd) override;
2057};
2058
2060 public:
2062 PT_item_list *opt_list)
2063 : Item_udf_sum(pos, udf_arg, opt_list) {}
2065 : Item_udf_sum(thd, item) {}
2066 String *val_str(String *) override;
2067 double val_real() override;
2068 longlong val_int() override;
2069 my_decimal *val_decimal(my_decimal *) override;
2070 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2071 return get_date_from_decimal(ltime, fuzzydate);
2072 }
2073 bool get_time(MYSQL_TIME *ltime) override {
2074 return get_time_from_decimal(ltime);
2075 }
2076 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2077 bool resolve_type(THD *) override {
2080 return false;
2081 }
2082 Item *copy_or_same(THD *thd) override;
2083};
2084
2085int group_concat_key_cmp_with_distinct(const void *arg, const void *key1,
2086 const void *key2);
2087int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2088 const void *key2);
2089int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2090 void *item_arg);
2091
2092class Item_func_group_concat final : public Item_sum {
2094
2095 /// True if GROUP CONCAT has the DISTINCT attribute
2097 /// True if the result is always NULL
2098 bool always_null{false};
2099 /// The number of ORDER BY items.
2101 /// The number of selected items, aka the concat field list
2103 /// Resolver context, points to containing query block
2105 /// String containing separator between group items
2107 /// Describes the temporary table used to perform group concat
2111 TREE *tree{nullptr};
2112
2113 /**
2114 If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
2115 out duplicates.
2116 @see Item_func_group_concat::setup
2117 @see Item_func_group_concat::add
2118 @see Item_func_group_concat::clear
2119 */
2121 /// Temporary table used to perform group concat
2122 TABLE *table{nullptr};
2125 /**
2126 The maximum permitted result length in bytes as set in
2127 group_concat_max_len system variable
2128 */
2130 bool warning_for_row{false};
2132 /// True if result has been written to output buffer.
2134 /**
2135 Following is 0 normal object and pointer to original one for copy
2136 (to correctly free resources)
2137 */
2139
2140 friend int group_concat_key_cmp_with_distinct(const void *arg,
2141 const void *key1,
2142 const void *key2);
2143 friend int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2144 const void *key2);
2145 friend int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2146 void *item_arg);
2147
2148 public:
2149 Item_func_group_concat(const POS &pos, bool is_distinct,
2150 PT_item_list *select_list,
2151 PT_order_list *opt_order_list, String *separator,
2152 PT_window *w);
2153
2156 assert(original != nullptr || unique_filter == nullptr);
2157 }
2158
2159 bool itemize(Parse_context *pc, Item **res) override;
2160 void cleanup() override;
2161
2162 enum Sumfunctype sum_func() const override { return GROUP_CONCAT_FUNC; }
2163 const char *func_name() const override { return "group_concat"; }
2164 Item_result result_type() const override { return STRING_RESULT; }
2165 Field *make_string_field(TABLE *table_arg) const override;
2166 void clear() override;
2167 bool add() override;
2168 void reset_field() override { assert(0); } // not used
2169 void update_field() override { assert(0); } // not used
2170 bool fix_fields(THD *, Item **) override;
2171 bool setup(THD *thd) override;
2172 void make_unique() override;
2173 double val_real() override;
2174 longlong val_int() override {
2175 String *res;
2176 int error;
2177 if (!(res = val_str(&str_value))) return (longlong)0;
2178 const char *end_ptr = res->ptr() + res->length();
2179 return my_strtoll10(res->ptr(), &end_ptr, &error);
2180 }
2181 my_decimal *val_decimal(my_decimal *decimal_value) override {
2182 return val_decimal_from_string(decimal_value);
2183 }
2184 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2185 return get_date_from_string(ltime, fuzzydate);
2186 }
2187 bool get_time(MYSQL_TIME *ltime) override {
2188 return get_time_from_string(ltime);
2189 }
2190 String *val_str(String *str) override;
2191 Item *copy_or_same(THD *thd) override;
2192 void no_rows_in_result() override;
2193 void print(const THD *thd, String *str,
2194 enum_query_type query_type) const override;
2195 bool change_context_processor(uchar *arg) override {
2196 context = pointer_cast<Item_ident::Change_context *>(arg)->m_context;
2197 return false;
2198 }
2199
2201 Window_evaluation_requirements *) override {
2203 return true;
2204 }
2205};
2206
2207/**
2208 Common parent class for window functions that always work on the entire
2209 partition, even if a frame is defined.
2210
2211 The subclasses can be divided in two disjoint sub-categories:
2212 - one-pass
2213 - two-pass (requires partition cardinality to be evaluated)
2214 cf. method needs_partition_cardinality.
2215*/
2218
2219 public:
2222 : Item_sum(pos, a, w) {}
2224 : Item_sum(pos, opt_list, w) {}
2226
2227 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2228 return get_date_from_numeric(ltime, fuzzydate);
2229 }
2230
2231 bool get_time(MYSQL_TIME *ltime) override {
2232 return get_time_from_numeric(ltime);
2233 }
2234
2235 void reset_field() override { assert(false); }
2236 void update_field() override { assert(false); }
2237 bool add() override {
2238 assert(false);
2239 return false;
2240 }
2241
2242 bool fix_fields(THD *thd, Item **items) override;
2243
2244 bool framing() const override { return false; }
2245};
2246
2247/**
2248 ROW_NUMBER window function, cf. SQL 2003 Section 6.10 <window function>
2249*/
2251 // Execution state variables
2252 ulonglong m_ctr; ///< Increment for each row in partition
2253
2254 public:
2256 : Item_non_framing_wf(pos, w), m_ctr(0) {
2257 unsigned_flag = true;
2258 }
2259
2260 const char *func_name() const override { return "row_number"; }
2261 enum Sumfunctype sum_func() const override { return ROW_NUMBER_FUNC; }
2262
2263 bool resolve_type(THD *thd [[maybe_unused]]) override {
2265 return false;
2266 }
2267
2268 longlong val_int() override;
2269 double val_real() override;
2270 my_decimal *val_decimal(my_decimal *buff) override;
2271 String *val_str(String *) override;
2272
2273 void clear() override;
2274
2275 Item_result result_type() const override { return INT_RESULT; }
2276
2278 Window_evaluation_requirements *) override {
2279 return false;
2280 }
2281};
2282
2283/**
2284 RANK or DENSE_RANK window function, cf. SQL 2003 Section 6.10 <window
2285 function>
2286*/
2289 bool m_dense; ///< If true, the object represents DENSE_RANK
2290 // Execution state variables
2291 ulonglong m_rank_ctr; ///< Increment when window order columns change
2292 ulonglong m_duplicates; ///< Needed to make RANK different from DENSE_RANK
2294 m_previous; ///< Values of previous row's ORDER BY items
2295 public:
2296 Item_rank(const POS &pos, bool dense, PT_window *w)
2298 m_dense(dense),
2299 m_rank_ctr(0),
2300 m_duplicates(0),
2302 unsigned_flag = true;
2303 }
2304
2305 ~Item_rank() override;
2306
2307 const char *func_name() const override {
2308 return m_dense ? "dense_rank" : "rank";
2309 }
2310
2311 enum Sumfunctype sum_func() const override {
2313 }
2314
2315 bool resolve_type(THD *thd [[maybe_unused]]) override {
2317 return false;
2318 }
2319
2320 longlong val_int() override;
2321 double val_real() override;
2322 my_decimal *val_decimal(my_decimal *buff) override;
2323 String *val_str(String *) override;
2324
2325 void update_after_wf_arguments_changed(THD *thd) override;
2326 bool check_wf_semantics1(THD *thd, Query_block *select,
2327 Window_evaluation_requirements *reqs) override;
2328 /**
2329 Clear state for a new partition
2330 */
2331 void clear() override;
2332 Item_result result_type() const override { return INT_RESULT; }
2333};
2334
2335/**
2336 CUME_DIST window function, cf. SQL 2003 Section 6.10 <window function>
2337*/
2340
2341 public:
2343
2344 const char *func_name() const override { return "cume_dist"; }
2345 enum Sumfunctype sum_func() const override { return CUME_DIST_FUNC; }
2346
2347 bool resolve_type(THD *thd [[maybe_unused]]) override {
2349 return false;
2350 }
2351
2352 bool check_wf_semantics1(THD *thd, Query_block *select,
2353 Window_evaluation_requirements *reqs) override;
2354
2355 bool needs_partition_cardinality() const override { return true; }
2356 void clear() override {}
2357 longlong val_int() override;
2358 double val_real() override;
2359 String *val_str(String *) override;
2361 Item_result result_type() const override { return REAL_RESULT; }
2362};
2363
2364/**
2365 PERCENT_RANK window function, cf. SQL 2003 Section 6.10 <window function>
2366*/
2369 // Execution state variables
2370 ulonglong m_rank_ctr; ///< Increment when window order columns change
2371 ulonglong m_peers; ///< Needed to make PERCENT_RANK same for peers
2372 /**
2373 Set when the last peer has been visited. Needed to increment m_rank_ctr.
2374 */
2376
2377 public:
2380 m_rank_ctr(0),
2381 m_peers(0),
2382 m_last_peer_visited(false) {}
2383
2385 const char *func_name() const override { return "percent_rank"; }
2386 enum Sumfunctype sum_func() const override { return PERCENT_RANK_FUNC; }
2387
2388 bool resolve_type(THD *thd [[maybe_unused]]) override {
2390 return false;
2391 }
2392
2393 bool check_wf_semantics1(THD *thd, Query_block *select,
2394 Window_evaluation_requirements *reqs) override;
2395 bool needs_partition_cardinality() const override { return true; }
2396
2397 void clear() override;
2398 longlong val_int() override;
2399 double val_real() override;
2400 String *val_str(String *) override;
2402 Item_result result_type() const override { return REAL_RESULT; }
2403};
2404
2405/**
2406 NTILE window function, cf. SQL 2011 Section 6.10 <window function>
2407*/
2411
2412 public:
2414 : Item_non_framing_wf(pos, a, w), m_value(0) {
2415 unsigned_flag = true;
2416 }
2417
2418 const char *func_name() const override { return "ntile"; }
2419 enum Sumfunctype sum_func() const override { return NTILE_FUNC; }
2420
2421 bool resolve_type(THD *thd) override {
2422 if (args[0]->propagate_type(thd, MYSQL_TYPE_LONGLONG, true)) return true;
2424 return false;
2425 }
2426
2427 bool fix_fields(THD *thd, Item **items) override;
2428
2429 longlong val_int() override;
2430 double val_real() override;
2431 my_decimal *val_decimal(my_decimal *buff) override;
2432 String *val_str(String *) override;
2433
2434 bool check_wf_semantics1(THD *thd, Query_block *select,
2435 Window_evaluation_requirements *reqs) override;
2437 Item_result result_type() const override { return INT_RESULT; }
2438 void clear() override {}
2439 bool needs_partition_cardinality() const override { return true; }
2440};
2441
2442/**
2443 LEAD/LAG window functions, cf. SQL 2011 Section 6.10 <window function>
2444*/
2447 bool m_is_lead; ///< if true, the function is LEAD, else LAG
2448 int64 m_n; ///< canonicalized offset value
2452 /**
2453 Execution state: if set, we already have a value for current row.
2454 State is used to avoid interference with other LEAD/LAG functions on
2455 the same window, since they share the same eval loop and they should
2456 trigger evaluation only when they are on the "right" row relative to
2457 current row. For other offsets, return NULL if we don't know the value
2458 for this function yet, or if we do (m_has_value==true), return the
2459 found value.
2460 */
2462 bool m_use_default; ///< execution state: use default value for current row
2464
2465 public:
2466 Item_lead_lag(const POS &pos, bool lead,
2467 PT_item_list *opt_list, // [0] expr, [1] offset, [2] default
2468 enum_null_treatment null_treatment, PT_window *w)
2469 : Item_non_framing_wf(pos, opt_list, w),
2470 m_null_treatment(null_treatment),
2471 m_is_lead(lead),
2472 m_n(0),
2476 m_has_value(false),
2477 m_use_default(false) {}
2478
2479 const char *func_name() const override {
2480 return (m_is_lead ? "lead" : "lag");
2481 }
2482 enum Sumfunctype sum_func() const override { return LEAD_LAG_FUNC; }
2483
2484 bool resolve_type(THD *thd) override;
2485 bool fix_fields(THD *thd, Item **items) override;
2486 void update_after_wf_arguments_changed(THD *thd) override;
2487 void clear() override;
2488 bool check_wf_semantics1(THD *thd, Query_block *select,
2489 Window_evaluation_requirements *reqs) override;
2491 enum Item_result result_type() const override { return m_hybrid_type; }
2492
2493 longlong val_int() override;
2494 double val_real() override;
2495 String *val_str(String *str) override;
2496 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2497
2498 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2499 bool get_time(MYSQL_TIME *ltime) override;
2500 bool val_json(Json_wrapper *wr) override;
2501
2502 bool needs_partition_cardinality() const override {
2503 /*
2504 A possible optimization here: if LAG, we are only interested in rows we
2505 have already seen, so we might compute the result without reading the
2506 entire partition as soon as we have the current row. Similarly, a small
2507 LEAD value might avoid reading the entire partition also, giving shorter
2508 time to first result. For now, we read the entirely partition for these
2509 window functions - for simplicity.
2510 */
2511 return true;
2512 }
2513
2514 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
2515 mem_root_deque<Item *> *fields) override;
2516
2517 void set_has_value(bool value) { m_has_value = value; }
2518 bool has_value() const { return m_has_value; }
2519
2520 void set_use_default(bool value) { m_use_default = value; }
2521 bool use_default() const { return m_use_default; }
2522
2523 private:
2524 bool setup_lead_lag();
2525 /**
2526 Core logic of LEAD/LAG window functions
2527
2528 @return true if computation yielded a NULL or error
2529 */
2530 bool compute();
2531};
2532
2533/**
2534 FIRST_VALUE/LAST_VALUE window functions, cf. SQL 2011 Section 6.10 <window
2535 function>
2536*/
2538 bool m_is_first; ///< if true, the function is FIRST_VALUE, else LAST_VALUE
2542 int64 cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2544
2545 public:
2546 Item_first_last_value(const POS &pos, bool first, Item *a,
2547 enum_null_treatment null_treatment, PT_window *w)
2548 : Item_sum(pos, a, w),
2549 m_is_first(first),
2550 m_null_treatment(null_treatment),
2553 cnt(0) {}
2554
2555 const char *func_name() const override {
2556 return m_is_first ? "first_value" : "last_value";
2557 }
2558
2559 enum Sumfunctype sum_func() const override { return FIRST_LAST_VALUE_FUNC; }
2560
2561 bool resolve_type(THD *thd) override;
2562 bool fix_fields(THD *thd, Item **items) override;
2563 void update_after_wf_arguments_changed(THD *thd) override;
2564 void clear() override;
2565 bool check_wf_semantics1(THD *thd, Query_block *select,
2566 Window_evaluation_requirements *reqs) override;
2567 enum Item_result result_type() const override { return m_hybrid_type; }
2568
2569 longlong val_int() override;
2570 double val_real() override;
2571 String *val_str(String *str) override;
2572 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2573
2574 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2575 bool get_time(MYSQL_TIME *ltime) override;
2576 bool val_json(Json_wrapper *wr) override;
2577
2578 void reset_field() override { assert(false); }
2579 void update_field() override { assert(false); }
2580 bool add() override {
2581 assert(false);
2582 return false;
2583 }
2584
2585 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
2586 mem_root_deque<Item *> *fields) override;
2587 bool uses_only_one_row() const override { return true; }
2588
2589 private:
2590 bool setup_first_last();
2591 /**
2592 Core logic of FIRST/LAST_VALUE window functions
2593
2594 @return true if computation yielded a NULL or error
2595 */
2596 bool compute();
2597};
2598
2599/**
2600 NTH_VALUE window function, cf. SQL 2011 Section 6.10 <window
2601 function>
2602*/
2603class Item_nth_value : public Item_sum {
2605 int64 m_n; ///< The N of the function
2606 bool m_from_last; ///< true iff FROM_LAST was specified
2610 int64 m_cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2611
2613
2614 public:
2615 Item_nth_value(const POS &pos, PT_item_list *a, bool from_last,
2616 enum_null_treatment null_treatment, PT_window *w)
2617 : Item_sum(pos, a, w),
2618 m_null_treatment(null_treatment),
2619 m_n(0),
2620 m_from_last(from_last),
2623 m_cnt(0) {}
2624
2625 const char *func_name() const override { return "nth_value"; }
2626 enum Sumfunctype sum_func() const override { return NTH_VALUE_FUNC; }
2627
2628 bool resolve_type(THD *thd) override;
2629 bool fix_fields(THD *thd, Item **items) override;
2630 void update_after_wf_arguments_changed(THD *thd) override;
2631 bool setup_nth();
2632 void clear() override;
2633
2634 bool check_wf_semantics1(THD *thd, Query_block *select,
2635 Window_evaluation_requirements *reqs) override;
2637
2638 enum Item_result result_type() const override { return m_hybrid_type; }
2639
2640 longlong val_int() override;
2641 double val_real() override;
2642 String *val_str(String *str) override;
2643 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2644
2645 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2646 bool get_time(MYSQL_TIME *ltime) override;
2647 bool val_json(Json_wrapper *wr) override;
2648
2649 void reset_field() override { assert(false); }
2650 void update_field() override { assert(false); }
2651 bool add() override {
2652 assert(false);
2653 return false;
2654 }
2655
2656 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
2657 mem_root_deque<Item *> *fields) override;
2658 bool uses_only_one_row() const override { return true; }
2659
2660 private:
2661 /**
2662 Core logic of NTH_VALUE window functions
2663
2664 @return true if computation yielded a NULL or error
2665 */
2666 bool compute();
2667};
2668
2669/**
2670 Class for implementation of the GROUPING function. The GROUPING
2671 function distinguishes super-aggregate rows from regular grouped
2672 rows. GROUP BY extensions such as ROLLUP and CUBE produce
2673 super-aggregate rows where the set of all values is represented
2674 by null. Using the GROUPING function, you can distinguish a null
2675 representing the set of all values in a super-aggregate row from
2676 a NULL in a regular row.
2677*/
2679 public:
2682 }
2683 const char *func_name() const override { return "grouping"; }
2684 enum Functype functype() const override { return GROUPING_FUNC; }
2685 longlong val_int() override;
2686 bool aggregate_check_group(uchar *arg) override;
2687 bool fix_fields(THD *thd, Item **ref) override;
2688 void update_used_tables() override;
2689};
2690
2691/**
2692 A wrapper Item that contains a number of aggregate items, one for each level
2693 of rollup (see Item_rollup_group_item for numbering conventions). When
2694 aggregating, every aggregator is either reset or updated as per the correct
2695 level, and when returning a value, the correct child item corresponding to
2696 the current rollup level is queried.
2697 */
2699 public:
2700 explicit Item_rollup_sum_switcher(List<Item> *sum_func_per_level)
2701 : Item_sum((*sum_func_per_level)[0]),
2702 m_num_levels(sum_func_per_level->size()) {
2703 args = (*THR_MALLOC)->ArrayAlloc<Item *>(sum_func_per_level->size());
2704 int i = 0;
2705 for (Item &item : *sum_func_per_level) {
2706 args[i++] = &item;
2707 }
2711 hidden = master()->hidden;
2715 }
2716 double val_real() override;
2717 longlong val_int() override;
2718 String *val_str(String *str) override;
2720 bool val_json(Json_wrapper *result) override;
2721 bool is_null() override;
2722 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2723 bool get_time(MYSQL_TIME *ltime) override;
2724 const char *func_name() const override { return "rollup_sum_switcher"; }
2725 table_map used_tables() const override { return master()->used_tables(); }
2726 Item_result result_type() const override { return master()->result_type(); }
2727 bool resolve_type(THD *) override {
2729 return false;
2730 }
2731 void print(const THD *thd, String *str,
2732 enum_query_type query_type) const override;
2733 Field *create_tmp_field(bool group, TABLE *table) override;
2734
2735 enum Sumfunctype sum_func() const override { return master()->sum_func(); }
2736 enum Sumfunctype real_sum_func() const override {
2738 }
2739 void reset_field() override { assert(false); }
2740 void update_field() override { assert(false); }
2741 void clear() override;
2742 bool add() override {
2743 assert(false);
2744 return true;
2745 }
2746
2747 bool reset_and_add_for_rollup(int last_unchanged_group_item_idx);
2748
2749 int set_aggregator(Aggregator::Aggregator_type aggregator) override;
2750 bool aggregator_setup(THD *thd) override;
2751 inline bool aggregator_add_all() {
2752 for (int i = 0; i < m_num_levels; ++i) {
2753 if (child(i)->aggregator_add()) {
2754 return true;
2755 }
2756 }
2757 return false;
2758 }
2759
2760 // Used when create_tmp_table() needs to delay application of aggregate
2761 // functions to a later stage in the query processing.
2762 Item *get_arg(uint i) override { return master()->get_arg(i); }
2763 Item *set_arg(THD *thd, uint i, Item *new_val) override {
2764 Item *ret = nullptr;
2765 for (int j = 0; j < m_num_levels; ++j) {
2766 ret = child(j)->set_arg(thd, i, new_val);
2767 }
2768 return ret; // Return the last one, arbitrarily.
2769 }
2770 uint argument_count() const override { return master()->argument_count(); }
2771
2772 // Used by AggregateIterator.
2774 inline Item_sum *master() const { return child(0); }
2775
2776 bool is_rollup_sum_wrapper() const override { return true; }
2777 const Item_sum *unwrap_sum() const override { return master(); }
2778 Item_sum *unwrap_sum() override { return master(); }
2779
2780 private:
2781 inline Item *current_arg() const;
2782 inline Item_sum *child(size_t i) const {
2783 return down_cast<Item_sum *>(args[i]);
2784 }
2785
2786 const int m_num_levels;
2788};
2789/// Implements ST_Collect which aggregates geometries into Multipoints,
2790/// Multilinestrings, Multipolygons and Geometrycollections.
2791
2793 private:
2794 std::optional<gis::srid_t> srid;
2795 std::unique_ptr<gis::Geometrycollection> m_geometrycollection;
2796 void pop_front();
2797
2798 public:
2800 Item_sum_collect(THD *thd, Item_sum *item) : Item_sum(thd, item) {
2802 }
2803 Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
2804 : Item_sum(pos, a, w) {
2805 set_distinct(distinct);
2807 }
2808
2809 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2810 longlong val_int() override { return val_int_from_string(); }
2811 double val_real() override { return val_real_from_string(); }
2812 bool get_date(MYSQL_TIME *, my_time_flags_t) override { return true; }
2813 bool get_time(MYSQL_TIME *) override { return true; }
2814 enum Sumfunctype sum_func() const override { return GEOMETRY_AGGREGATE_FUNC; }
2815 Item_result result_type() const override { return STRING_RESULT; }
2817 /*
2818 In contrast to Item_sum, Item_sum_collect always uses Aggregator_simple,
2819 and only needs to reset its aggregator when called.
2820 */
2821 if (aggr) {
2822 aggr->clear();
2823 return false;
2824 }
2825
2826 destroy(aggr);
2827 aggr = new (*THR_MALLOC) Aggregator_simple(this);
2828 return aggr ? false : true;
2829 }
2830
2831 bool fix_fields(THD *thd, Item **ref) override;
2832 /*
2833 We need to override check_wf_semantics1 because it reports an error when
2834 with_distinct is set.
2835 */
2836 bool check_wf_semantics1(THD *thd, Query_block *,
2838
2839 Item *copy_or_same(THD *thd) override;
2840
2841 void update_field() override;
2842 void reset_field() override;
2843
2844 String *val_str(String *str) override;
2845
2846 bool add() override;
2847
2848 void read_result_field();
2849 void store_result_field();
2850
2851 void clear() override;
2852 const char *func_name() const override { return "st_collect"; }
2853};
2854
2855#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:2134
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:941
void endup() override
Calculate the aggregate function value.
Definition: item_sum.cc:1313
~Aggregator_distinct() override
Definition: item_sum.cc:2084
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:2129
void clear() override
Invalidate calculated value and clear the distinct rows.
Definition: item_sum.cc:1212
bool setup(THD *) override
Called before feeding the first row.
Definition: item_sum.cc:1012
bool unique_walk_function(void *element)
Aggregate a distinct row from the distinct hash table.
Definition: item_sum.cc:2077
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:1244
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:2124
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 acumulated 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:2101
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:2105
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:2109
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 acumulated 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:132
void set(const DTCollation &dt)
Definition: item.h:192
Definition: field.h:573
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:3640
String * val_str(String *) override
Definition: item_sum.cc:3655
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:3625
Item_avg_field(Item_result res_type, Item_sum_avg *item)
Definition: item_sum.cc:3606
Definition: item.h:6492
CUME_DIST window function, cf.
Definition: item_sum.h:2338
Item_cume_dist(const POS &pos, PT_window *w)
Definition: item_sum.h:2342
Item_non_framing_wf super
Definition: item_sum.h:2339
double val_real() override
Definition: item_sum.cc:4811
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2345
void clear() override
Definition: item_sum.h:2356
const char * func_name() const override
Definition: item_sum.h:2344
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:2355
String * val_str(String *) override
Definition: item_sum.cc:4831
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:4801
longlong val_int() override
Definition: item_sum.cc:4823
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2347
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:4835
Item_result result_type() const override
Definition: item_sum.h:2361
Definition: item.h:4043
FIRST_VALUE/LAST_VALUE window functions, cf.
Definition: item_sum.h:2537
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2559
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5035
void reset_field() override
Definition: item_sum.h:2578
const char * func_name() const override
Definition: item_sum.h:2555
bool setup_first_last()
Definition: item_sum.cc:5072
double val_real() override
Definition: item_sum.cc:5122
String * val_str(String *str) override
Definition: item_sum.cc:5177
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:5089
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:5021
bool compute()
Core logic of FIRST/LAST_VALUE window functions.
Definition: item_sum.cc:5093
int64 cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2542
Item_result m_hybrid_type
Definition: item_sum.h:2540
enum Item_result result_type() const override
Definition: item_sum.h:2567
Item_cache * m_value
Definition: item_sum.h:2541
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5153
void update_field() override
Definition: item_sum.h:2579
enum_null_treatment m_null_treatment
Definition: item_sum.h:2539
Item_sum super
Definition: item_sum.h:2543
void clear() override
Definition: item_sum.cc:5083
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2587
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:5064
bool add() override
Definition: item_sum.h:2580
Item_first_last_value(const POS &pos, bool first, Item *a, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2546
bool m_is_first
if true, the function is FIRST_VALUE, else LAST_VALUE
Definition: item_sum.h:2538
longlong val_int() override
Definition: item_sum.cc:5112
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5163
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5143
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5045
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5132
Definition: item_sum.h:2092
void clear() override
Definition: item_sum.cc:4302
bool m_result_finalized
True if result has been written to output buffer.
Definition: item_sum.h:2133
String result
Definition: item_sum.h:2109
Item_func_group_concat * original
Following is 0 normal object and pointer to original one for copy (to correctly free resources)
Definition: item_sum.h:2138
longlong val_int() override
Definition: item_sum.h:2174
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.h:2181
friend int dump_leaf_key(void *key_arg, element_count count, void *item_arg)
Append data from current leaf to item->result.
Definition: item_sum.cc:4048
uint row_count
Definition: item_sum.h:2124
void make_unique() override
Definition: item_sum.cc:4576
bool change_context_processor(uchar *arg) override
Definition: item_sum.h:2195
bool setup(THD *thd) override
Definition: item_sum.cc:4454
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize() but with additional parameter.
Definition: item_sum.cc:4176
Item_sum super
Definition: item_sum.h:2093
bool distinct
True if GROUP CONCAT has the DISTINCT attribute.
Definition: item_sum.h:2096
TABLE * table
Temporary table used to perform group concat.
Definition: item_sum.h:2122
friend int group_concat_key_cmp_with_distinct(const void *arg, const void *key1, const void *key2)
Compares the values for fields in expr list of GROUP_CONCAT.
Definition: item_sum.cc:3970
double val_real() override
Definition: item_sum.cc:4584
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2162
String * val_str(String *str) override
Definition: item_sum.cc:4591
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:4005
bool force_copy_fields
Definition: item_sum.h:2131
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:4363
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2184
Unique * unique_filter
If DISTINCT is used with this GROUP_CONCAT, this member is used to filter out duplicates.
Definition: item_sum.h:2120
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2200
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:4300
Mem_root_array< ORDER > order_array
Definition: item_sum.h:2123
TREE tree_base
Definition: item_sum.h:2110
void update_field() override
Definition: item_sum.h:2169
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:4616
bool always_null
True if the result is always NULL.
Definition: item_sum.h:2098
Temp_table_param * tmp_table_param
Describes the temporary table used to perform group concat.
Definition: item_sum.h:2108
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:4229
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4292
const char * func_name() const override
Definition: item_sum.h:2163
Item_result result_type() const override
Definition: item_sum.h:2164
Field * make_string_field(TABLE *table_arg) const override
Create a field to hold a string value from an item.
Definition: item_sum.cc:4259
uint m_order_arg_count
The number of ORDER BY items.
Definition: item_sum.h:2100
TREE * tree
Definition: item_sum.h:2111
Item_func_group_concat(const POS &pos, bool is_distinct, PT_item_list *select_list, PT_order_list *opt_order_list, String *separator, PT_window *w)
Constructor of Item_func_group_concat.
Definition: item_sum.cc:4138
bool add() override
Definition: item_sum.cc:4314
Name_resolution_context * context
Resolver context, points to containing query block.
Definition: item_sum.h:2104
void reset_field() override
Definition: item_sum.h:2168
uint m_field_arg_count
The number of selected items, aka the concat field list.
Definition: item_sum.h:2102
uint group_concat_max_len
The maximum permitted result length in bytes as set in group_concat_max_len system variable.
Definition: item_sum.h:2129
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2187
bool warning_for_row
Definition: item_sum.h:2130
String * separator
String containing separator between group items.
Definition: item_sum.h:2106
~Item_func_group_concat() override
Definition: item_sum.h:2155
Class for implementation of the GROUPING function.
Definition: item_sum.h:2678
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:6182
longlong val_int() override
Evaluation of the GROUPING function.
Definition: item_sum.cc:6153
Item_func_grouping(const POS &pos, PT_item_list *a)
Definition: item_sum.h:2680
const char * func_name() const override
Definition: item_sum.h:2683
bool fix_fields(THD *thd, Item **ref) override
Resolve the fields in the GROUPING function.
Definition: item_sum.cc:6098
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_sum.cc:6194
enum Functype functype() const override
Definition: item_sum.h:2684
Definition: item_func.h:93
Item ** args
Array of pointers to arguments.
Definition: item_func.h:100
virtual Item * get_arg(uint i)
Definition: item_func.h:406
Functype
Definition: item_func.h:178
@ GROUPING_FUNC
Definition: item_func.h:230
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:3922
void set_original_field_name(const char *name_arg)
Definition: item.h:3919
const char * original_table_name() const
Definition: item.h:3923
Definition: item_func.h:895
Definition: item.h:4794
LEAD/LAG window functions, cf.
Definition: item_sum.h:2445
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5448
bool has_value() const
Definition: item_sum.h:2518
bool m_is_lead
if true, the function is LEAD, else LAG
Definition: item_sum.h:2447
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:2502
const char * func_name() const override
Definition: item_sum.h:2479
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5569
void set_use_default(bool value)
Definition: item_sum.h:2520
String * val_str(String *str) override
Definition: item_sum.cc:5561
bool m_use_default
execution state: use default value for current row
Definition: item_sum.h:2462
enum Item_result result_type() const override
Definition: item_sum.h:2491
Item_cache * m_default
Definition: item_sum.h:2451
bool compute()
Core logic of LEAD/LAG window functions.
Definition: item_sum.cc:5596
Item_lead_lag(const POS &pos, bool lead, PT_item_list *opt_list, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2466
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:5508
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2482
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5578
enum_null_treatment m_null_treatment
Definition: item_sum.h:2446
void clear() override
Definition: item_sum.cc:5520
bool use_default() const
Definition: item_sum.h:2521
bool m_has_value
Execution state: if set, we already have a value for current row.
Definition: item_sum.h:2461
longlong val_int() override
Definition: item_sum.cc:5532
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5548
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5457
Item_result m_hybrid_type
Definition: item_sum.h:2449
int64 m_n
canonicalized offset value
Definition: item_sum.h:2448
double val_real() override
Definition: item_sum.cc:5540
void set_has_value(bool value)
Definition: item_sum.h:2517
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5395
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5586
bool setup_lead_lag()
Definition: item_sum.cc:5492
Item_cache * m_value
Definition: item_sum.h:2450
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:5485
Item_non_framing_wf super
Definition: item_sum.h:2463
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:5527
Common parent class for window functions that always work on the entire partition,...
Definition: item_sum.h:2216
Item_non_framing_wf(const POS &pos, PT_window *w)
Definition: item_sum.h:2220
bool add() override
Definition: item_sum.h:2237
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2231
Item_non_framing_wf(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2221
Item_non_framing_wf(const POS &pos, PT_item_list *opt_list, PT_window *w)
Definition: item_sum.h:2223
Item_sum super
Definition: item_sum.h:2217
Item_non_framing_wf(THD *thd, Item_non_framing_wf *i)
Definition: item_sum.h:2225
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2227
void update_field() override
Definition: item_sum.h:2236
void reset_field() override
Definition: item_sum.h:2235
bool framing() const override
All aggregates are framing, i.e.
Definition: item_sum.h:2244
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:4652
NTH_VALUE window function, cf.
Definition: item_sum.h:2603
int64 m_cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2610
void update_field() override
Definition: item_sum.h:2650
bool compute()
Core logic of NTH_VALUE window functions.
Definition: item_sum.cc:5288
String * val_str(String *str) override
Definition: item_sum.cc:5355
Item_sum super
Definition: item_sum.h:2612
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:4915
enum Item_result result_type() const override
Definition: item_sum.h:2638
Item_result m_hybrid_type
Definition: item_sum.h:2607
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:5240
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5341
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5375
bool setup_nth()
Definition: item_sum.cc:5247
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5200
enum_field_types m_hybrid_field_type
Definition: item_sum.h:2608
int64 m_n
The N of the function.
Definition: item_sum.h:2605
double val_real() override
Definition: item_sum.cc:5331
bool m_from_last
true iff FROM_LAST was specified
Definition: item_sum.h:2606
Item_cache * m_value
Definition: item_sum.h:2609
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5385
enum_null_treatment m_null_treatment
Definition: item_sum.h:2604
void reset_field() override
Definition: item_sum.h:2649
longlong val_int() override
Definition: item_sum.cc:5321
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:2615
void clear() override
Definition: item_sum.cc:5258
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5187
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2626
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:5264
bool add() override
Definition: item_sum.h:2651
const char * func_name() const override
Definition: item_sum.h:2625
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5365
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:5268
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2658
NTILE window function, cf.
Definition: item_sum.h:2408
void clear() override
Definition: item_sum.h:2438
Item_non_framing_wf super
Definition: item_sum.h:2409
String * val_str(String *) override
Definition: item_sum.cc:4990
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2421
const char * func_name() const override
Definition: item_sum.h:2418
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:2439
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5005
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:4933
double val_real() override
Definition: item_sum.cc:4985
longlong val_int() override
Definition: item_sum.cc:4939
longlong m_value
Definition: item_sum.h:2410
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4992
Item_ntile(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2413
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2419
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:4997
Item_result result_type() const override
Definition: item_sum.h:2437
PERCENT_RANK window function, cf.
Definition: item_sum.h:2367
String * val_str(String *) override
Definition: item_sum.cc:4898
Item_percent_rank(const POS &pos, PT_window *w)
Definition: item_sum.h:2378
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2370
const char * func_name() const override
Definition: item_sum.h:2385
Item_result result_type() const override
Definition: item_sum.h:2402
~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:4840
ulonglong m_peers
Needed to make PERCENT_RANK same for peers.
Definition: item_sum.h:2371
double val_real() override
Definition: item_sum.cc:4864
Item_non_framing_wf super
Definition: item_sum.h:2368
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2388
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2386
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:2395
bool m_last_peer_visited
Set when the last peer has been visited.
Definition: item_sum.h:2375
void clear() override
Definition: item_sum.cc:4907
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:4902
longlong val_int() override
Definition: item_sum.cc:4890
RANK or DENSE_RANK window function, cf.
Definition: item_sum.h:2287
Item_result result_type() const override
Definition: item_sum.h:2332
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:4707
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:4720
ulonglong m_duplicates
Needed to make RANK different from DENSE_RANK.
Definition: item_sum.h:2292
bool m_dense
If true, the object represents DENSE_RANK.
Definition: item_sum.h:2289
longlong val_int() override
Definition: item_sum.cc:4738
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2311
~Item_rank() override
Definition: item_sum.cc:4794
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2315
Item_rank(const POS &pos, bool dense, PT_window *w)
Definition: item_sum.h:2296
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4773
const char * func_name() const override
Definition: item_sum.h:2307
double val_real() override
Definition: item_sum.cc:4766
String * val_str(String *) override
Definition: item_sum.cc:4771
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2291
void clear() override
Clear state for a new partition.
Definition: item_sum.cc:4778
Mem_root_array< Cached_item * > m_previous
Values of previous row's ORDER BY items.
Definition: item_sum.h:2294
Item_non_framing_wf super
Definition: item_sum.h:2288
Item with result field.
Definition: item.h:5503
longlong llrint_with_overflow_check(double realval)
Definition: item.h:5554
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item.cc:10495
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:2698
table_map used_tables() const override
Definition: item_sum.h:2725
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:6271
void update_field() override
Definition: item_sum.h:2740
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2727
Item * get_arg(uint i) override
Definition: item_sum.h:2762
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.h:2763
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.cc:6257
void set_current_rollup_level(int level)
Definition: item_sum.h:2773
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:6211
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:6262
bool aggregator_setup(THD *thd) override
Called to initialize the aggregator.
Definition: item_sum.cc:6304
Item_result result_type() const override
Definition: item_sum.h:2726
String * val_str(String *str) override
Definition: item_sum.cc:6236
int set_aggregator(Aggregator::Aggregator_type aggregator) override
Definition: item_sum.cc:6293
Item_sum * unwrap_sum() override
Non-const version.
Definition: item_sum.h:2778
longlong val_int() override
Definition: item_sum.cc:6229
void reset_field() override
Definition: item_sum.h:2739
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:2777
Item_rollup_sum_switcher(List< Item > *sum_func_per_level)
Definition: item_sum.h:2700
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:6217
bool aggregator_add_all()
Definition: item_sum.h:2751
Item * current_arg() const
Definition: item_sum.cc:6206
Item_sum * master() const
Definition: item_sum.h:2774
Item_sum * child(size_t i) const
Definition: item_sum.h:2782
enum Sumfunctype real_sum_func() const override
Definition: item_sum.h:2736
bool is_rollup_sum_wrapper() const override
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:2776
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2735
const char * func_name() const override
Definition: item_sum.h:2724
int m_current_rollup_level
Definition: item_sum.h:2787
double val_real() override
Definition: item_sum.cc:6222
bool val_json(Json_wrapper *result) override
Get a JSON value from an Item.
Definition: item_sum.cc:6250
uint argument_count() const override
Definition: item_sum.h:2770
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:6243
bool add() override
Definition: item_sum.h:2742
void clear() override
Definition: item_sum.cc:6275
bool reset_and_add_for_rollup(int last_unchanged_group_item_idx)
Definition: item_sum.cc:6281
const int m_num_levels
Definition: item_sum.h:2786
ROW_NUMBER window function, cf.
Definition: item_sum.h:2250
ulonglong m_ctr
Increment for each row in partition.
Definition: item_sum.h:2252
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2263
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2261
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2277
Item_row_number(const POS &pos, PT_window *w)
Definition: item_sum.h:2255
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4700
Item_result result_type() const override
Definition: item_sum.h:2275
String * val_str(String *) override
Definition: item_sum.cc:4696
longlong val_int() override
Definition: item_sum.cc:4677
void clear() override
Definition: item_sum.cc:4705
const char * func_name() const override
Definition: item_sum.h:2260
double val_real() override
Definition: item_sum.cc:4691
Definition: item_sum.h:1483
enum Type type() const override
Definition: item_sum.h:1486
Item_std_field(Item_sum_std *item)
Definition: item_sum.cc:3753
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:1494
enum Item_result result_type() const override
Definition: item_sum.h:1489
double val_real() override
Definition: item_sum.cc:3756
const char * func_name() const override
Definition: item_sum.h:1490
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3764
Definition: item_sum.h:1901
Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1903
const char * func_name() const override
Definition: item_sum.h:1907
Item_sum_and(THD *thd, Item_sum_and *item)
Definition: item_sum.h:1906
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3221
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:2202
void clear() override
Definition: item_sum.cc:2257
String * val_str(String *str) override
Definition: item_sum.cc:2376
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:3450
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:2266
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:2235
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:2228
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2291
void reset_field() override
Definition: item_sum.cc:3350
uint prec_increment
Definition: item_sum.h:1307
uint dec_bin_size
Definition: item_sum.h:1308
bool add() override
Definition: item_sum.cc:2259
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:3696
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:3661
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:3713
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3739
longlong val_int() override
Definition: item_sum.cc:3681
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3746
String * val_str(String *) override
Definition: item_sum.cc:3721
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:1732
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3110
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:1575
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1841
void remove_bits(const String *s1, ulonglong b1)
For windowing: perform inverse aggregation.
Definition: item_sum.cc:1497
const char initial_value_buff_storage[1]
Buffer used to avoid String allocation in the constructor.
Definition: item_sum.h:1743
static constexpr uint DIGIT_CNT_CARD
Definition: item_sum.h:1786
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1828
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1441
bool m_is_xor
true iff BIT_XOR
Definition: item_sum.h:1789
void clear() override
Definition: item_sum.cc:3193
Item * result_item(Field *) override
Definition: item_sum.h:1824
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:1416
longlong val_int() override
Definition: item_sum.cc:3168
Item_sum super
Definition: item_sum.h:1733
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:1778
Item_result hybrid_type
Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT.
Definition: item_sum.h:1741
ulonglong reset_bits
Stores the neutral element for function.
Definition: item_sum.h:1735
Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg, PT_window *w)
Definition: item_sum.h:1792
bool is_and() const
Definition: item_sum.h:1854
void reset_field() override
Definition: item_sum.cc:3378
ulonglong m_count
Execution state (windowing): this is for counting rows entering and leaving the window frame,...
Definition: item_sum.h:1749
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3117
double val_real() override
Definition: item_sum.cc:3142
bool add() override
Common implementation of Item_sum_or::add, Item_sum_and:add and Item_sum_xor::add.
Definition: item_sum.cc:1659
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.cc:3124
enum Item_result result_type() const override
Definition: item_sum.h:1829
uint m_digit_cnt_card
Definition: item_sum.h:1784
Item_sum_bit(THD *thd, Item_sum_bit *item)
Copy constructor, used for executing subqueries with temporary tables.
Definition: item_sum.h:1805
String * val_str(String *str) override
Definition: item_sum.cc:3079
void update_field() override
Definition: item_sum.cc:3388
ulonglong bits
Stores the result value for the INT_RESULT.
Definition: item_sum.h:1737
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:1757
String value_buff
Stores the result value for the STRING_RESULT.
Definition: item_sum.h:1739
Implements ST_Collect which aggregates geometries into Multipoints, Multilinestrings,...
Definition: item_sum.h:2792
bool add() override
Definition: item_sum.cc:6379
void clear() override
Definition: item_sum.cc:6373
void read_result_field()
Definition: item_sum.cc:6431
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:6342
bool get_date(MYSQL_TIME *, my_time_flags_t) override
Definition: item_sum.h:2812
std::optional< gis::srid_t > srid
Definition: item_sum.h:2794
Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
Definition: item_sum.h:2803
void update_field() override
Definition: item_sum.cc:6521
longlong val_int() override
Definition: item_sum.h:2810
bool get_time(MYSQL_TIME *) override
Definition: item_sum.h:2813
std::unique_ptr< gis::Geometrycollection > m_geometrycollection
Definition: item_sum.h:2795
String * val_str(String *str) override
Definition: item_sum.cc:6474
void reset_field() override
Definition: item_sum.cc:6584
Item_sum_collect(THD *thd, Item_sum *item)
Definition: item_sum.h:2800
void store_result_field()
Definition: item_sum.cc:6527
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:6579
bool check_wf_semantics1(THD *thd, Query_block *, Window_evaluation_requirements *r) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:6364
const char * func_name() const override
Definition: item_sum.h:2852
void pop_front()
Definition: item_sum.cc:6467
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2814
Item_result result_type() const override
Definition: item_sum.h:2815
int set_aggregator(Aggregator::Aggregator_type) override
Definition: item_sum.h:2816
double val_real() override
Definition: item_sum.h:2811
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6426
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:2164
void update_field() override
Definition: item_sum.cc:3441
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2145
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:2196
void make_const(longlong count_arg)
Definition: item_sum.h:1094
void reset_field() override
Definition: item_sum.cc:3342
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:2153
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:2155
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:1528
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3032
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:3027
bool setup_hybrid(Item *item, Item *value_arg)
MIN/MAX function setup.
Definition: item_sum.cc:1751
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:1679
virtual Item_sum_hybrid * clone_hybrid(THD *thd) const =0
Create a clone of this object.
bool add() override
Definition: item_sum.cc:3062
const bool m_is_min
Tells if this is the MIN function (true) or the MAX function (false).
Definition: item_sum.h:1535
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:2963
bool compute()
This function implements the optimized version of retrieving min/max value.
Definition: item_sum.cc:2776
void min_max_update_int_field()
Definition: item_sum.cc:3572
longlong val_int() override
Definition: item_sum.cc:2898
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:2953
void reset_field() override
Definition: item_sum.cc:3245
void clear() override
Definition: item_sum.cc:2727
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:1575
Item_cache * arg_cache
Definition: item_sum.h:1544
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3013
void min_max_update_json_field()
Definition: item_sum.cc:3525
void update_field() override
Definition: item_sum.cc:3486
longlong val_date_temporal() override
Return date value of item in packed longlong format.
Definition: item_sum.cc:2924
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:2737
bool keep_field_type() const override
Definition: item_sum.h:1668
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2936
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:1569
void min_max_update_real_field()
Definition: item_sum.cc:3559
Item_sum_hybrid(THD *thd, const Item_sum_hybrid *item)
Definition: item_sum.h:1639
bool any_value()
Definition: item_sum.h:1676
Item_result hybrid_type
Definition: item_sum.h:1546
bool was_values
Definition: item_sum.h:1547
longlong val_time_temporal() override
Return time value of item in packed longlong format.
Definition: item_sum.cc:2912
bool m_optimize
Set to true when min/max can be optimized using window's ordering.
Definition: item_sum.h:1555
Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
Definition: item_sum.h:1623
Item_sum_hybrid(Item *item_par, bool is_min)
Definition: item_sum.h:1607
Item_sum super
Definition: item_sum.h:1529
void min_max_update_temporal_field()
Definition: item_sum.cc:3507
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:2742
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1720
String * val_str(String *) override
Definition: item_sum.cc:2973
Arg_comparator * cmp
Definition: item_sum.h:1545
void min_max_update_str_field()
Definition: item_sum.cc:3543
enum Item_result result_type() const override
Definition: item_sum.h:1669
void min_max_update_decimal_field()
Definition: item_sum.cc:3590
double val_real() override
Definition: item_sum.cc:2884
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:1768
bool m_want_first
For min() - Set to true when results are ordered in ascending and false when descending.
Definition: item_sum.h:1563
Item_cache * value
Definition: item_sum.h:1544
bool m_nulls_first
Set to true if the window is ordered ascending.
Definition: item_sum.h:1551
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:3002
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.h:1670
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:2988
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:1383
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:1385
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:5834
~Item_sum_json_array() override
bool add() override
Definition: item_sum.cc:5907
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:5956
const char * func_name() const override
Definition: item_sum.h:1261
void clear() override
Definition: item_sum.cc:5849
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:5858
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6064
bool add() override
Definition: item_sum.cc:5969
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:5883
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:5873
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:5794
void update_field() override
Definition: item_sum.cc:5814
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5712
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:5750
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:5788
~Item_sum_json() override
String * val_str(String *str) override
Definition: item_sum.cc:5694
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5765
Item_result result_type() const override
Definition: item_sum.h:1231
bool fix_fields(THD *thd, Item **pItem) override
Definition: item_sum.cc:5668
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:5663
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5782
Item_sum_json(unique_ptr_destroy_only< Json_wrapper > wrapper, Args &&... parent_args)
Construct an Item_sum_json instance.
Definition: item_sum.cc:5654
double val_real() override
Definition: item_sum.cc:5735
Definition: item_sum.h:1713
Item_sum_max(Item *item_par)
Definition: item_sum.h:1715
Item_sum_max(THD *thd, const Item_sum_max *item)
Definition: item_sum.h:1718
const char * func_name() const override
Definition: item_sum.h:1721
Item_sum_max * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3043
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1720
Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1716
Definition: item_sum.h:1699
Item_sum_min(THD *thd, const Item_sum_min *item)
Definition: item_sum.h:1704
Item_sum_min(Item *item_par)
Definition: item_sum.h:1701
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1706
const char * func_name() const override
Definition: item_sum.h:1707
Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1702
Item_sum_min * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3039
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:1379
Item_sum_num(THD *thd, Item_sum_num *item)
Definition: item_sum.h:949
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1389
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:1377
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:3232
Item_sum_num(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:946
Definition: item_sum.h:1891
Item_sum_or(THD *thd, Item_sum_or *item)
Definition: item_sum.h:1896
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3207
const char * func_name() const override
Definition: item_sum.h:1897
Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1893
Definition: item_sum.h:1506
double val_real() override
Definition: item_sum.cc:2386
Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1508
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2395
Item_sum_std(THD *thd, Item_sum_std *item)
Definition: item_sum.h:1511
const char * func_name() const override
Definition: item_sum.h:1515
enum Item_result result_type() const override
Definition: item_sum.h:1519
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1512
Item * result_item(Field *) override
Definition: item_sum.h:1514
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:1900
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:1832
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:1922
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:1851
void reset_field() override
Definition: item_sum.cc:3324
String * val_str(String *str) override
Definition: item_sum.cc:2011
longlong val_int() override
Definition: item_sum.cc:1943
my_decimal dec_buffs[2]
Definition: item_sum.h:1010
void clear() override
Definition: item_sum.cc:1839
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:3412
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:2017
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1853
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1036
double val_real() override
Definition: item_sum.cc:1967
Definition: item_sum.h:2059
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2070
longlong val_int() override
Definition: item_sum.cc:3881
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2073
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3883
Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2061
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3892
double val_real() override
Definition: item_sum.cc:3879
enum Item_result result_type() const override
Definition: item_sum.h:2076
String * val_str(String *) override
Definition: item_sum.cc:3875
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
Definition: item_sum.h:2064
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2077
Definition: item_sum.h:1969
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1988
longlong val_int() override
Definition: item_sum.h:1975
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3871
String * val_str(String *str) override
Definition: item_sum.cc:3867
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3854
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1982
double val_real() override
Definition: item_sum.cc:3859
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
Definition: item_sum.h:1973
Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1971
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1985
Definition: item_sum.h:1996
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2009
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
Definition: item_sum.h:2000
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2014
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2012
enum Item_result result_type() const override
Definition: item_sum.h:2013
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3896
String * val_str(String *str) override
Definition: item_sum.cc:3908
Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1998
longlong val_int() override
Definition: item_sum.cc:3900
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3912
double val_real() override
Definition: item_sum.h:2003
Definition: item_sum.h:2021
bool resolve_type(THD *) override
Default max_length is max argument length.
Definition: item_sum.cc:3918
double val_real() override
Definition: item_sum.h:2028
String * val_str(String *) override
Definition: item_sum.cc:3934
enum Item_result result_type() const override
Definition: item_sum.h:2054
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2048
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2051
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3926
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:3930
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
Definition: item_sum.h:2025
longlong val_int() override
Definition: item_sum.h:2037
Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2023
Definition: item_sum.h:1429
ulonglong count
Definition: item_sum.h:1439
void update_field() override
Definition: item_sum.cc:2703
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2574
double val_real() override
Definition: item_sum.cc:2648
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1473
void clear() override
Definition: item_sum.cc:2628
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2594
Item * result_item(Field *) override
Definition: item_sum.h:1465
Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1449
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1458
double recurrence_s
Definition: item_sum.h:1437
bool optimize
If set, uses a algorithm II mentioned in the class description to calculate the variance which helps ...
Definition: item_sum.h:1447
const char * func_name() const override
Definition: item_sum.h:1467
bool add() override
Definition: item_sum.cc:2630
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1466
double recurrence_s2
Definition: item_sum.h:1438
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:2558
double recurrence_m
Used in recurrence relation.
Definition: item_sum.h:1437
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2678
uint prec_increment
Definition: item_sum.h:1441
enum Item_result result_type() const override
Definition: item_sum.h:1472
void reset_field() override
Definition: item_sum.cc:2683
uint sample
Definition: item_sum.h:1440
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:2607
Definition: item_sum.h:1911
const char * func_name() const override
Definition: item_sum.h:1919
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3214
Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1913
Item_sum_xor(THD *thd, Item_sum_xor *item)
Definition: item_sum.h:1918
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:688
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:409
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:871
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:479
bool aggregate_check_distinct(uchar *arg) override
Definition: item_sum.cc:604
bool init_sum_func_check(THD *thd)
Prepare an aggregate function for checking of context.
Definition: item_sum.cc:153
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:892
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:461
void make_const()
Definition: item_sum.h:587
Item * replace_aggregate(uchar *) override
Definition: item_sum.cc:719
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:859
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:246
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:517
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:628
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:727
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:747
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.cc:817
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:661
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:805
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:822
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:737
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:451
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:125
bool reset_wf_state(uchar *arg) override
Reset execution state for such window function types as determined by arg.
Definition: item_sum.cc:879
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:768
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:849
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:93
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:577
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:653
Query_block * base_query_block
query block where function is placed
Definition: item_sum.h:483
Definition: item_sum.h:1927
udf_handler udf
Definition: item_sum.h:1931
void update_field() override
Definition: item_sum.h:1963
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3834
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize() but with additional parameter.
Definition: item_sum.cc:3812
void reset_field() override
Definition: item_sum.h:1962
bool add() override
Definition: item_sum.cc:3827
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.h:1948
Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1934
void clear() override
Definition: item_sum.cc:3821
~Item_udf_sum() override
Definition: item_sum.h:1942
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:3843
Item_udf_sum(THD *thd, Item_udf_sum *item)
Definition: item_sum.h:1938
Item_sum super
Definition: item_sum.h:1928
const char * func_name() const override
Definition: item_sum.h:1947
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1958
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,...
Definition: item_sum.h:1359
enum Type type() const override
Definition: item_sum.h:1353
Item_variance_field(Item_sum_variance *item)
Definition: item_sum.cc:3783
uint sample
Definition: item_sum.h:1349
String * val_str(String *str) override
Definition: item_sum.h:1355
double val_real() override
Definition: item_sum.cc:3797
bool check_function_as_value_generator(uchar *args) override
Check if this item is allowed for a virtual column or inside a default expression.
Definition: item_sum.h:1364
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
virtual double val_real()=0
String str_value
str_values's main purpose is to cache the value in save_in_field
Definition: item.h:3261
void set_nullable(bool nullable)
Definition: item.h:3344
DTCollation collation
Character set and collation properties assigned for this Item.
Definition: item.h:3268
bool get_time_from_decimal(MYSQL_TIME *ltime)
Convert val_decimal() to time in MYSQL_TIME.
Definition: item.cc:1506