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