MySQL 26.10.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;
1281 bool no_conversions) override;
1282 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
1283 bool val_date(Date_val *date, my_time_flags_t flags) override;
1284 bool val_time(Time_val *time) override;
1285 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
1286 void reset_field() override;
1287 void update_field() override;
1288
1289 void print(const THD *thd, String *str,
1290 enum_query_type query_type) const override;
1291
1294};
1295
1296/// Implements aggregation of values into an array.
1298 /// Accumulates the final value.
1300
1301 public:
1302 Item_sum_json_array(THD *thd, Item_sum *item,
1305 Item_sum_json_array(const POS &pos, Item *a,
1306 Json_constructor_null_clause json_constructor_null_clause,
1307 PT_window *w,
1311 const char *func_name() const override { return "json_arrayagg"; }
1312 enum Sumfunctype sum_func() const override { return JSON_ARRAYAGG_FUNC; }
1313 void clear() override;
1314 bool add() override;
1315 Item *copy_or_same(THD *thd) override;
1316};
1317
1318/// Implements aggregation of values into an object.
1320 /// Accumulates the final value.
1322 /// Buffer used to get the value of the key.
1324 /**
1325 Map of keys in Json_object and the count for each key
1326 within a window frame. It is used in handling rows
1327 leaving a window frame when rows are not sorted
1328 according to the key in Json_object.
1329 */
1330 std::map<std::string, int> m_key_map;
1331 /**
1332 If window provides ordering on the key in Json_object,
1333 a key_map is not needed to handle rows leaving a window
1334 frame. In this case, process_buffered_windowing_record()
1335 will set flags when a key/value pair can be removed from
1336 the Json_object.
1337 */
1338 bool m_optimize{false};
1339
1340 public:
1341 Item_sum_json_object(THD *thd, Item_sum *item,
1344 Item_sum_json_object(const POS &pos, Item *a, Item *b, PT_window *w,
1348 const char *func_name() const override { return "json_objectagg"; }
1349 enum Sumfunctype sum_func() const override { return JSON_OBJECTAGG_FUNC; }
1350 void clear() override;
1351 bool add() override;
1352 Item *copy_or_same(THD *thd) override;
1353 bool check_wf_semantics1(THD *thd, Query_block *select,
1354 Window_evaluation_requirements *reqs) override;
1355};
1356
1357class Item_sum_avg final : public Item_sum_sum {
1358 public:
1363 double m_avg;
1364
1365 Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
1366 : Item_sum_sum(pos, item_par, distinct, w) {}
1367
1369 : Item_sum_sum(thd, item), prec_increment(item->prec_increment) {}
1370
1371 bool resolve_type(THD *thd) override;
1372 enum Sumfunctype sum_func() const override {
1374 }
1375 void clear() override;
1376 bool add() override;
1377 double val_real() override;
1378 // In SPs we might force the "wrong" type with select into a declare variable
1380 my_decimal *val_decimal(my_decimal *) override;
1381 String *val_str(String *str) override;
1382 void reset_field() override;
1383 void update_field() override;
1384 Item *result_item(Field *) override { return new Item_aggr_avg_field(this); }
1385 const char *func_name() const override { return "avg"; }
1386 Item *copy_or_same(THD *thd) override;
1387 Field *create_tmp_field(bool group, TABLE *table) override;
1388 void cleanup() override {
1389 m_count = 0;
1392 }
1393};
1394
1395class Item_sum_variance;
1396
1398 public:
1400 enum Type type() const override { return AGGR_FIELD_ITEM; }
1401 double val_real() override;
1403 my_decimal *val_decimal(my_decimal *dec_buf) override {
1404 return val_decimal_from_real(dec_buf);
1405 }
1408 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1409 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1410 return true;
1411 }
1412
1413 private:
1415};
1416
1417/*
1418 variance(a) =
1419
1420 = sum (ai - avg(a))^2 / count(a) )
1421 = sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
1422 = (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
1423 = (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
1424 = (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2
1425 )/count(a) = = (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a)
1426 )/count(a) = = (sum(ai^2) - sum(a)^2/count(a))/count(a)
1427
1428 But, this falls prey to catastrophic cancellation.
1429 Instead, we use recurrence formulas in Algorithm I mentoned below
1430 for group aggregates.
1431
1432 Algorithm I:
1433 M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
1434 S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
1435 for 2 <= k <= n newline
1436 ital variance = S_{n} / (n-1)
1437
1438 For aggregate window functions algorithm I cannot be optimized for
1439 moving frames since M_{i} changes for every row. So we use the
1440 following algorithm.
1441
1442 Algorithm II:
1443
1444 K = 0
1445 n = 0
1446 ex = 0
1447 ex2 = 0
1448
1449 def add_sample(x):
1450 if (n == 0):
1451 K = x
1452 n = n + 1
1453 ex += x - K
1454 ex2 += (x - K) * (x - K)
1455
1456 def remove_sample(x):
1457 n = n - 1
1458 ex -= (x - K)
1459 ex2 -= (x - K) * (x - K)
1460
1461 def variance():
1462 return (ex2 - (ex*ex)/n) / (n-1)
1463
1464 This formula facilitates incremental computation enabling us to
1465 optimize in case of moving window frames. The optimized codepath is taken
1466 only when windowing_use_high_precision is set to false. By default,
1467 aggregate window functions take the non-optimized codepath.
1468 Note:
1469 Results could differ between optimized and non-optimized code path.
1470 Hence algorithm II is used only when user sets
1471 windowing_use_high_precision to false.
1472*/
1473
1475 bool resolve_type(THD *) override;
1476
1477 public:
1479 /**
1480 Used in recurrence relation.
1481 */
1482 double recurrence_m{0.0};
1483 double recurrence_s{0.0};
1484 double recurrence_s2{0.0};
1488 /**
1489 If set, uses a algorithm II mentioned in the class description
1490 to calculate the variance which helps in optimizing windowing
1491 functions in presence of frames.
1492 */
1494
1495 Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg,
1496 PT_window *w)
1497 : Item_sum_num(pos, item_par, w),
1499 count(0),
1500 sample(sample_arg),
1501 optimize(false) {}
1502
1504 enum Sumfunctype sum_func() const override { return VARIANCE_FUNC; }
1505 void clear() override;
1506 bool add() override;
1507 double val_real() override;
1508 my_decimal *val_decimal(my_decimal *) override;
1509 void reset_field() override;
1510 void update_field() override;
1511 Item *result_item(Field *) override {
1512 return new Item_aggr_variance_field(this);
1513 }
1514 void no_rows_in_result() override {}
1515 const char *func_name() const override {
1516 return sample ? "var_samp" : "variance";
1517 }
1518 Item *copy_or_same(THD *thd) override;
1519 Field *create_tmp_field(bool group, TABLE *table) override;
1520 enum Item_result result_type() const override { return REAL_RESULT; }
1521 void cleanup() override {
1522 count = 0;
1524 }
1525 bool check_wf_semantics1(THD *thd, Query_block *select,
1526 Window_evaluation_requirements *reqs) override;
1527};
1528
1529class Item_sum_std;
1530
1532 public:
1534 enum Type type() const override { return AGGR_FIELD_ITEM; }
1535 double val_real() override;
1536 my_decimal *val_decimal(my_decimal *) override;
1537 enum Item_result result_type() const override { return REAL_RESULT; }
1540 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1541 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1542 return true;
1543 }
1544};
1545
1546/*
1547 standard_deviation(a) = sqrt(variance(a))
1548*/
1549
1551 public:
1552 Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
1553 : Item_sum_variance(pos, item_par, sample_arg, w) {}
1554
1555 Item_sum_std(THD *thd, Item_sum_std *item) : Item_sum_variance(thd, item) {}
1556 enum Sumfunctype sum_func() const override { return STD_FUNC; }
1557 double val_real() override;
1558 Item *result_item(Field *) override { return new Item_aggr_std_field(this); }
1559 const char *func_name() const override {
1560 return sample ? "stddev_samp" : "std";
1561 }
1562 Item *copy_or_same(THD *thd) override;
1563 enum Item_result result_type() const override { return REAL_RESULT; }
1564};
1565
1566// This class is a string or number function depending on num_func
1567class Arg_comparator;
1568
1569/**
1570 Abstract base class for the MIN and MAX aggregate functions.
1571*/
1574
1575 private:
1576 /**
1577 Tells if this is the MIN function (true) or the MAX function (false).
1578 */
1579 const bool m_is_min;
1580 /*
1581 For window functions MIN/MAX with optimized code path, no comparisons
1582 are needed beyond NULL detection: MIN/MAX are then roughly equivalent to
1583 FIRST/LAST_VALUE. For this case, 'value' is the value of
1584 the window function a priori taken from args[0], while arg_cache is used to
1585 remember the value from the previous row. NULLs need a bit of careful
1586 treatment.
1587 */
1591 bool m_has_values; // Set if at least one row is found (for max/min only)
1592 /**
1593 Set to true if the window is ordered ascending.
1594 */
1596 /**
1597 Set to true when min/max can be optimized using window's ordering.
1598 */
1600 /**
1601 For min() - Set to true when results are ordered in ascending and
1602 false when descending.
1603 For max() - Set to true when results are ordered in descending and
1604 false when ascending.
1605 Valid only when m_optimize is true.
1606 */
1607 bool m_want_first; ///< Want first non-null value, else last non_null value
1608 /**
1609 Execution state: keeps track if this is the first row in the frame
1610 when buffering is not needed.
1611 Valid only when m_optimize is true.
1612 */
1614
1615 /**
1616 Execution state: keeps track of at which row we saved a non-null last
1617 value.
1618 */
1620
1621 /**
1622 This function implements the optimized version of retrieving min/max
1623 value. When we have "ordered ASC" results in a window, min will always
1624 be the first value in the result set (neglecting the NULL's) and max
1625 will always be the last value (or the other way around, if ordered DESC).
1626 It is based on the implementation of FIRST_VALUE/LAST_VALUE, except for
1627 the NULL handling.
1628
1629 @return true if computation yielded a NULL or error
1630 */
1631 bool compute();
1632
1633 /**
1634 MIN/MAX function setup.
1635
1636 Setup cache/comparator of MIN/MAX functions. When called by the
1637 copy_or_same() function, the value_arg parameter contains the calculated
1638 value of the original MIN/MAX object, and it is saved in this object's
1639 cache.
1640
1641 @param item the argument of the MIN/MAX function
1642 @param value_arg the calculated value of the MIN/MAX function
1643 @return false on success, true on error
1644 */
1645 bool setup_hybrid(Item *item, Item *value_arg);
1646
1647 /** Create a clone of this object. */
1648 virtual Item_sum_hybrid *clone_hybrid(THD *thd) const = 0;
1649
1650 protected:
1651 Item_sum_hybrid(Item *item_par, bool is_min)
1652 : Item_sum(item_par),
1653 m_is_min(is_min),
1654 value(nullptr),
1656 cmp(nullptr),
1658 m_has_values(true),
1659 m_nulls_first(false),
1660 m_optimize(false),
1661 m_want_first(false),
1662 m_cnt(0),
1665 }
1666
1667 Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
1668 : Item_sum(pos, item_par, w),
1669 m_is_min(is_min),
1670 value(nullptr),
1672 cmp(nullptr),
1674 m_has_values(true),
1675 m_nulls_first(false),
1676 m_optimize(false),
1677 m_want_first(false),
1678 m_cnt(0),
1681 }
1682
1684 : Item_sum(thd, item),
1685 m_is_min(item->m_is_min),
1686 value(item->value),
1688 hybrid_type(item->hybrid_type),
1691 m_optimize(item->m_optimize),
1693 m_cnt(item->m_cnt),
1695
1696 public:
1697 bool fix_fields(THD *, Item **) override;
1698 void clear() override;
1699 void update_after_wf_arguments_changed(THD *thd) override;
1700 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
1701 mem_root_deque<Item *> *fields) override;
1702 double val_real() override;
1703 longlong val_int() override;
1704 longlong val_date_temporal() override;
1705 my_decimal *val_decimal(my_decimal *) override;
1706 bool val_date(Date_val *date, my_time_flags_t flags) override;
1707 bool val_time(Time_val *time) override;
1708 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
1709 void reset_field() override;
1710 String *val_str(String *) override;
1711 bool val_json(Json_wrapper *wr) override;
1712 bool keep_field_type() const override { return true; }
1713 enum Item_result result_type() const override { return hybrid_type; }
1714 TYPELIB *get_typelib() const override {
1715 assert(sum_func() == MAX_FUNC || sum_func() == MIN_FUNC);
1716 return arguments()[0]->get_typelib();
1717 }
1718 void update_field() override;
1719 void cleanup() override;
1720 bool has_values() { return m_has_values; }
1721 void no_rows_in_result() override;
1722 Field *create_tmp_field(bool group, TABLE *table) override;
1723 bool uses_only_one_row() const override { return m_optimize; }
1724 bool add() override;
1725 Item *copy_or_same(THD *thd) override;
1726 bool check_wf_semantics1(THD *thd, Query_block *select,
1728
1729 private:
1730 /*
1731 These functions check if the value on the current row exceeds the maximum or
1732 minimum value seen so far, and update the current max/min stored in
1733 result_field, if needed.
1734 */
1743};
1744
1745class Item_sum_min final : public Item_sum_hybrid {
1746 public:
1747 Item_sum_min(Item *item_par) : Item_sum_hybrid(item_par, true) {}
1748 Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
1749 : Item_sum_hybrid(pos, item_par, true, w) {}
1750 Item_sum_min(THD *thd, const Item_sum_min *item)
1751 : Item_sum_hybrid(thd, item) {}
1752 enum Sumfunctype sum_func() const override { return MIN_FUNC; }
1753 const char *func_name() const override { return "min"; }
1754
1755 private:
1756 Item_sum_min *clone_hybrid(THD *thd) const override;
1757};
1758
1759class Item_sum_max final : public Item_sum_hybrid {
1760 public:
1761 Item_sum_max(Item *item_par) : Item_sum_hybrid(item_par, false) {}
1762 Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
1763 : Item_sum_hybrid(pos, item_par, false, w) {}
1764 Item_sum_max(THD *thd, const Item_sum_max *item)
1765 : Item_sum_hybrid(thd, item) {}
1766 enum Sumfunctype sum_func() const override { return MAX_FUNC; }
1767 const char *func_name() const override { return "max"; }
1768
1769 private:
1770 Item_sum_max *clone_hybrid(THD *thd) const override;
1771};
1772
1773/**
1774 Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
1775
1776 Each of them is both a set function and a framing window function.
1777*/
1778class Item_sum_bit : public Item_sum {
1780 /// Stores the neutral element for function
1782 /// Stores the result value for the INT_RESULT
1784 /// Stores the result value for the STRING_RESULT
1786 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1788 /// Buffer used to avoid String allocation in the constructor
1789 const char initial_value_buff_storage[1] = {0};
1790
1791 /**
1792 Execution state (windowing): this is for counting rows entering and leaving
1793 the window frame, see #m_frame_null_count.
1794 */
1796
1797 /**
1798 Execution state (windowing): this is for counting NULLs of rows entering
1799 and leaving the window frame, when we use optimized inverse-based
1800 computations. By comparison with m_count we can know how many non-NULLs are
1801 in the frame.
1802 */
1804
1805 /**
1806 Execution state (windowing): Used for AND, OR to be able to invert window
1807 functions in optimized mode.
1808
1809 For the optimized code path of BIT_XXX wfs, we keep track of the number of
1810 bit values (0's or 1's; see below) seen in a frame using a 64 bits counter
1811 pr bit. This lets us compute the value of OR by just inspecting:
1812
1813 - the number of 1's in the previous frame
1814 - whether any removed row(s) is a 1
1815 - whether any added row(s) is a 1
1816
1817 Similarly for AND, we keep track of the number of 0's seen for a particular
1818 bit. To do this trick we need a counter per bit position. This array holds
1819 these counters.
1820
1821 Note that for XOR, the inverse operation is identical to the operation,
1822 so we don't need the above.
1823 */
1825 /*
1826 Size of allocated array m_digit_cnt.
1827 The size is DIGIT_CNT_CARD (for integer types) or ::max_length * 8 for bit
1828 strings.
1829 */
1831
1832 static constexpr uint DIGIT_CNT_CARD = sizeof(ulonglong) * 8;
1833
1834 protected:
1835 bool m_is_xor; ///< true iff BIT_XOR
1836
1837 public:
1838 Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg,
1839 PT_window *w)
1840 : Item_sum(pos, item_par, w),
1841 reset_bits(reset_arg),
1842 bits(reset_arg),
1844 m_count(0),
1848 m_is_xor(false) {}
1849
1850 /// Copy constructor, used for executing subqueries with temporary tables
1852 : Item_sum(thd, item),
1853 reset_bits(item->reset_bits),
1854 bits(item->bits),
1856 hybrid_type(item->hybrid_type),
1857 m_count(item->m_count),
1861 m_is_xor(item->m_is_xor) {
1862 /*
1863 This constructor should only be called during the Optimize stage.
1864 Asserting that the item was not evaluated yet.
1865 */
1866 assert(item->value_buff.length() == 1);
1867 assert(item->bits == item->reset_bits);
1868 }
1869
1870 Item *result_item(Field *) override {
1871 return new Item_aggr_bit_field(this, reset_bits);
1872 }
1873
1874 enum Sumfunctype sum_func() const override { return SUM_BIT_FUNC; }
1875 enum Item_result result_type() const override { return hybrid_type; }
1876 void clear() override;
1877 longlong val_int() override;
1878 double val_real() override;
1879 String *val_str(String *str) override;
1880 my_decimal *val_decimal(my_decimal *decimal_value) override;
1881 bool val_date(Date_val *date, my_time_flags_t flags) override;
1882 bool val_time(Time_val *time) override;
1883 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
1884 void reset_field() override;
1885 void update_field() override;
1886 bool resolve_type(THD *) override;
1887 bool fix_fields(THD *thd, Item **ref) override;
1888 void cleanup() override {
1889 bits = reset_bits;
1890 // At end of one execution of statement, free buffer to reclaim memory:
1893 }
1894
1895 /**
1896 Common implementation of Item_sum_or::add, Item_sum_and:add
1897 and Item_sum_xor::add.
1898 */
1899 bool add() override;
1900 /// @returns true iff this is BIT_AND.
1901 inline bool is_and() const { return reset_bits != 0; }
1902
1903 private:
1904 /**
1905 Accumulate the value of 's1' (if in string mode) or of 'b1' (if in integer
1906 mode). Updates 'value_buff' or 'bits'.
1907
1908 @param s1 argument to accumulate
1909 @param b1 argument to accumulate
1910
1911 @returns true if error
1912 */
1913 bool add_bits(const String *s1, ulonglong b1);
1914
1915 /**
1916 For windowing: perform inverse aggregation. "De-accumulate" the value of
1917 's1' (if in string mode) or of 'b1' (if in integer mode). Updates
1918 'value_buff' or 'bits'.
1919
1920 For BIT_XOR we simply apply XOR as it's its inverse operation. For BIT_OR
1921 and BIT_AND we do the rest below.
1922
1923 For each bit in argument, decrement the corresponding bits's counter
1924 ('m_digit_cnt') for that bit as follows: for BIT_AND, decrement the
1925 counter if we see a zero in that bit; for BIT_OR decrement the counter if
1926 we see a 1 in that bit. Next, update 'value_buff' or 'bits' using the
1927 resulting counters: for each bit, for BIT_AND, set bit if we have counter
1928 == 0, i.e. we have no zero bits for that bit in the frame (yet). For
1929 BIT_OR, set bit if we have counter > 0, i.e. at least one row in the frame
1930 has that bit set.
1931
1932 @param s1 the bits to be inverted from the aggregate value
1933 @param b1 the bits to be inverted from the aggregate value
1934 */
1935 void remove_bits(const String *s1, ulonglong b1);
1936};
1937
1938class Item_sum_or final : public Item_sum_bit {
1939 public:
1940 Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
1941 : Item_sum_bit(pos, item_par, 0LL, w) {}
1942
1943 Item_sum_or(THD *thd, Item_sum_or *item) : Item_sum_bit(thd, item) {}
1944 const char *func_name() const override { return "bit_or"; }
1945 Item *copy_or_same(THD *thd) override;
1946};
1947
1948class Item_sum_and final : public Item_sum_bit {
1949 public:
1950 Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
1951 : Item_sum_bit(pos, item_par, ULLONG_MAX, w) {}
1952
1953 Item_sum_and(THD *thd, Item_sum_and *item) : Item_sum_bit(thd, item) {}
1954 const char *func_name() const override { return "bit_and"; }
1955 Item *copy_or_same(THD *thd) override;
1956};
1957
1958class Item_sum_xor final : public Item_sum_bit {
1959 public:
1960 Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
1961 : Item_sum_bit(pos, item_par, 0LL, w) {
1962 m_is_xor = true;
1963 }
1964
1965 Item_sum_xor(THD *thd, Item_sum_xor *item) : Item_sum_bit(thd, item) {}
1966 const char *func_name() const override { return "bit_xor"; }
1967 Item *copy_or_same(THD *thd) override;
1968};
1969
1970/*
1971 User defined aggregates
1972*/
1973
1974class Item_udf_sum : public Item_sum {
1976
1977 protected:
1979
1980 public:
1981 Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1982 : Item_sum(pos, opt_list, nullptr), udf(udf_arg) {
1984 }
1986 : Item_sum(thd, item), udf(item->udf) {
1987 udf.m_original = false;
1988 }
1989 ~Item_udf_sum() override {
1991 }
1992
1993 bool do_itemize(Parse_context *pc, Item **res) override;
1994 const char *func_name() const override { return udf.name(); }
1995 bool fix_fields(THD *thd, Item **ref) override {
1996 assert(!fixed);
1997
1998 if (init_sum_func_check(thd)) return true;
1999
2000 fixed = true;
2001 if (udf.fix_fields(thd, this, this->arg_count, this->args)) return true;
2002
2003 return check_sum_func(thd, ref);
2004 }
2005 enum Sumfunctype sum_func() const override { return UDF_SUM_FUNC; }
2006
2007 void clear() override;
2008 bool add() override;
2009 void reset_field() override {}
2010 void update_field() override {}
2011 void cleanup() override;
2012 void print(const THD *thd, String *str,
2013 enum_query_type query_type) const override;
2014 uint64 hash() override;
2015};
2016
2017class Item_sum_udf_float final : public Item_udf_sum {
2018 public:
2019 Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2020 : Item_udf_sum(pos, udf_arg, opt_list) {}
2022 : Item_udf_sum(thd, item) {}
2023 longlong val_int() override {
2024 assert(fixed);
2025 return (longlong)rint(Item_sum_udf_float::val_real());
2026 }
2027 double val_real() override;
2028 String *val_str(String *str) override;
2029 my_decimal *val_decimal(my_decimal *) override;
2030 bool val_date(Date_val *date, my_time_flags_t flags) override {
2031 return get_date_from_real(date, flags);
2032 }
2033 bool val_time(Time_val *time) override { return get_time_from_real(time); }
2035 return get_datetime_from_real(dt, flags);
2036 }
2037 bool resolve_type(THD *) override {
2040 return false;
2041 }
2042 Item *copy_or_same(THD *thd) override;
2043};
2044
2045class Item_sum_udf_int final : public Item_udf_sum {
2046 public:
2047 Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2048 : Item_udf_sum(pos, udf_arg, opt_list) {}
2050 : Item_udf_sum(thd, item) {}
2051 longlong val_int() override;
2052 double val_real() override {
2053 assert(fixed);
2054 return (double)Item_sum_udf_int::val_int();
2055 }
2056 String *val_str(String *str) override;
2057 my_decimal *val_decimal(my_decimal *) override;
2058 bool val_date(Date_val *date, my_time_flags_t flags) override {
2059 return get_date_from_int(date, flags);
2060 }
2061 bool val_time(Time_val *time) override { return get_time_from_int(time); }
2063 return get_datetime_from_int(dt, flags);
2064 }
2065 enum Item_result result_type() const override { return INT_RESULT; }
2066 bool resolve_type(THD *) override {
2068 return false;
2069 }
2070 Item *copy_or_same(THD *thd) override;
2071};
2072
2073class Item_sum_udf_str final : public Item_udf_sum {
2074 public:
2075 Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2076 : Item_udf_sum(pos, udf_arg, opt_list) {}
2078 : Item_udf_sum(thd, item) {}
2079 String *val_str(String *) override;
2080 double val_real() override {
2081 int err_not_used;
2082 const char *end_not_used;
2083 String *res;
2084 res = val_str(&str_value);
2085 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2086 &end_not_used, &err_not_used)
2087 : 0.0;
2088 }
2089 longlong val_int() override {
2090 int err_not_used;
2091 String *res;
2092 const CHARSET_INFO *cs;
2093
2094 if (!(res = val_str(&str_value))) return 0; /* Null value */
2095 cs = res->charset();
2096 const char *end = res->ptr() + res->length();
2097 return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
2098 }
2100 bool val_date(Date_val *date, my_time_flags_t flags) override {
2101 return get_date_from_string(date, flags);
2102 }
2103 bool val_time(Time_val *time) override { return get_time_from_string(time); }
2105 return get_datetime_from_string(dt, flags);
2106 }
2107 enum Item_result result_type() const override { return STRING_RESULT; }
2108 bool resolve_type(THD *) override;
2109 Item *copy_or_same(THD *thd) override;
2110};
2111
2113 public:
2114 Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg,
2115 PT_item_list *opt_list)
2116 : Item_udf_sum(pos, udf_arg, opt_list) {}
2118 : Item_udf_sum(thd, item) {}
2119 String *val_str(String *) override;
2120 double val_real() override;
2121 longlong val_int() override;
2122 my_decimal *val_decimal(my_decimal *) override;
2123 bool val_date(Date_val *date, my_time_flags_t flags) override {
2124 return get_date_from_decimal(date, flags);
2125 }
2126 bool val_time(Time_val *time) override { return get_time_from_decimal(time); }
2128 return get_datetime_from_decimal(dt, flags);
2129 }
2130 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2131 bool resolve_type(THD *) override {
2134 return false;
2135 }
2136 Item *copy_or_same(THD *thd) override;
2137};
2138
2139int group_concat_key_cmp_with_distinct(const void *arg, const void *key1,
2140 const void *key2);
2141int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2142 const void *key2);
2143int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2144 void *item_arg);
2145
2146class Item_func_group_concat final : public Item_sum {
2148
2149 /// True if GROUP CONCAT has the DISTINCT attribute
2151 /// The number of ORDER BY items.
2153 /// The number of selected items, aka the concat field list
2155 /// Resolver context, points to containing query block
2157 /// String containing separator between group items
2159 /// Describes the temporary table used to perform group concat
2163 TREE *tree{nullptr};
2164
2165 /**
2166 If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
2167 out duplicates.
2168 @see Item_func_group_concat::setup
2169 @see Item_func_group_concat::add
2170 @see Item_func_group_concat::clear
2171 */
2173 /// Temporary table used to perform group concat
2174 TABLE *table{nullptr};
2176 uint row_count{0};
2177 /**
2178 The maximum permitted result length in bytes as set in
2179 group_concat_max_len system variable
2180 */
2182 bool warning_for_row{false};
2184 /// True if result has been written to output buffer.
2186 /**
2187 Following is 0 normal object and pointer to original one for copy
2188 (to correctly free resources)
2189 */
2191
2192 friend int group_concat_key_cmp_with_distinct(const void *arg,
2193 const void *key1,
2194 const void *key2);
2195 friend int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2196 const void *key2);
2197 friend int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2198 void *item_arg);
2199
2200 public:
2201 Item_func_group_concat(const POS &pos, bool is_distinct,
2202 PT_item_list *select_list,
2203 PT_order_list *opt_order_list, String *separator,
2204 PT_window *w);
2205
2208 assert(original != nullptr || unique_filter == nullptr);
2209 }
2210
2211 bool do_itemize(Parse_context *pc, Item **res) override;
2212 void cleanup() override;
2213
2214 enum Sumfunctype sum_func() const override { return GROUP_CONCAT_FUNC; }
2215 const char *func_name() const override { return "group_concat"; }
2216 Item_result result_type() const override { return STRING_RESULT; }
2217 Field *make_string_field(TABLE *table_arg) const override;
2218 void clear() override;
2219 bool add() override;
2220 void reset_field() override { assert(0); } // not used
2221 void update_field() override { assert(0); } // not used
2222 bool fix_fields(THD *, Item **) override;
2223 bool setup(THD *thd) override;
2224 void make_unique() override;
2225 double val_real() override;
2226 longlong val_int() override {
2227 String *res;
2228 int error;
2229 if (!(res = val_str(&str_value))) return (longlong)0;
2230 const char *end_ptr = res->ptr() + res->length();
2231 return my_strtoll10(res->ptr(), &end_ptr, &error);
2232 }
2233 my_decimal *val_decimal(my_decimal *decimal_value) override {
2234 return val_decimal_from_string(decimal_value);
2235 }
2236 bool val_date(Date_val *date, my_time_flags_t flags) override {
2237 return get_date_from_string(date, flags);
2238 }
2239 bool val_time(Time_val *time) override { return get_time_from_string(time); }
2241 return get_datetime_from_string(dt, flags);
2242 }
2243
2244 bool has_distinct() const noexcept { return distinct; }
2245 const String *get_separator_str() const noexcept { return separator; }
2247 return group_concat_max_len;
2248 }
2250 return order_array;
2251 }
2252
2253 String *val_str(String *str) override;
2254 Item *copy_or_same(THD *thd) override;
2255 void no_rows_in_result() override;
2256 void print(const THD *thd, String *str,
2257 enum_query_type query_type) const override;
2258 uint64 hash() override;
2259 bool change_context_processor(uchar *arg) override {
2260 context = pointer_cast<Item_ident::Change_context *>(arg)->m_context;
2261 return false;
2262 }
2263
2265 Window_evaluation_requirements *) override {
2267 return true;
2268 }
2269};
2270
2271/**
2272 Common parent class for window functions that always work on the entire
2273 partition, even if a frame is defined.
2274
2275 The subclasses can be divided in two disjoint sub-categories:
2276 - one-pass
2277 - two-pass (requires partition cardinality to be evaluated)
2278 cf. method needs_partition_cardinality.
2279*/
2282
2283 public:
2284 Item_non_framing_wf(const POS &pos, PT_window *w) : Item_sum(pos, w) {}
2286 : Item_sum(pos, a, w) {}
2288 : Item_sum(pos, opt_list, w) {}
2290
2291 bool val_date(Date_val *date, my_time_flags_t flags) override {
2292 return get_date_from_numeric(date, flags);
2293 }
2294 bool val_time(Time_val *time) override { return get_time_from_numeric(time); }
2296 return get_datetime_from_numeric(dt, flags);
2297 }
2298 void reset_field() override { assert(false); }
2299 void update_field() override { assert(false); }
2300 bool add() override {
2301 assert(false);
2302 return false;
2303 }
2304
2305 bool fix_fields(THD *thd, Item **items) override;
2306
2307 bool framing() const override { return false; }
2308};
2309
2310/**
2311 ROW_NUMBER window function, cf. SQL 2003 Section 6.10 <window function>
2312*/
2314 // Execution state variables
2315 ulonglong m_ctr; ///< Increment for each row in partition
2316
2317 public:
2319 : Item_non_framing_wf(pos, w), m_ctr(0) {
2320 unsigned_flag = true;
2321 }
2322
2323 const char *func_name() const override { return "row_number"; }
2324 enum Sumfunctype sum_func() const override { return ROW_NUMBER_FUNC; }
2325
2326 bool resolve_type(THD *thd [[maybe_unused]]) override {
2328 return false;
2329 }
2330
2331 longlong val_int() override;
2332 double val_real() override;
2333 my_decimal *val_decimal(my_decimal *buff) override;
2334 String *val_str(String *) override;
2335
2336 void clear() override;
2337
2338 Item_result result_type() const override { return INT_RESULT; }
2339
2341 Window_evaluation_requirements *) override {
2342 return false;
2343 }
2344};
2345
2346/**
2347 RANK or DENSE_RANK window function, cf. SQL 2003 Section 6.10 <window
2348 function>
2349*/
2352 bool m_dense; ///< If true, the object represents DENSE_RANK
2353 // Execution state variables
2354 ulonglong m_rank_ctr; ///< Increment when window order columns change
2355 ulonglong m_duplicates; ///< Needed to make RANK different from DENSE_RANK
2357 m_previous; ///< Values of previous row's ORDER BY items
2358 public:
2359 Item_rank(const POS &pos, bool dense, PT_window *w)
2360 : Item_non_framing_wf(pos, w),
2361 m_dense(dense),
2362 m_rank_ctr(0),
2363 m_duplicates(0),
2365 unsigned_flag = true;
2366 }
2367
2368 ~Item_rank() override;
2369
2370 const char *func_name() const override {
2371 return m_dense ? "dense_rank" : "rank";
2372 }
2373
2374 enum Sumfunctype sum_func() const override {
2376 }
2377
2378 bool resolve_type(THD *thd [[maybe_unused]]) override {
2380 return false;
2381 }
2382
2383 longlong val_int() override;
2384 double val_real() override;
2385 my_decimal *val_decimal(my_decimal *buff) override;
2386 String *val_str(String *) override;
2387
2388 void update_after_wf_arguments_changed(THD *thd) override;
2389 bool check_wf_semantics1(THD *thd, Query_block *select,
2390 Window_evaluation_requirements *reqs) override;
2391 /**
2392 Clear state for a new partition
2393 */
2394 void clear() override;
2395 Item_result result_type() const override { return INT_RESULT; }
2396};
2397
2398/**
2399 CUME_DIST window function, cf. SQL 2003 Section 6.10 <window function>
2400*/
2403
2404 public:
2406
2407 const char *func_name() const override { return "cume_dist"; }
2408 enum Sumfunctype sum_func() const override { return CUME_DIST_FUNC; }
2409
2410 bool resolve_type(THD *thd [[maybe_unused]]) override {
2412 return false;
2413 }
2414
2415 bool check_wf_semantics1(THD *thd, Query_block *select,
2416 Window_evaluation_requirements *reqs) override;
2417
2418 bool needs_partition_cardinality() const override { return true; }
2419 void clear() override {}
2420 longlong val_int() override;
2421 double val_real() override;
2422 String *val_str(String *) override;
2424 Item_result result_type() const override { return REAL_RESULT; }
2425};
2426
2427/**
2428 PERCENT_RANK window function, cf. SQL 2003 Section 6.10 <window function>
2429*/
2432 // Execution state variables
2433 ulonglong m_rank_ctr; ///< Increment when window order columns change
2434 ulonglong m_peers; ///< Needed to make PERCENT_RANK same for peers
2435 /**
2436 Set when the last peer has been visited. Needed to increment m_rank_ctr.
2437 */
2439
2440 public:
2442 : Item_non_framing_wf(pos, w),
2443 m_rank_ctr(0),
2444 m_peers(0),
2445 m_last_peer_visited(false) {}
2446
2448 const char *func_name() const override { return "percent_rank"; }
2449 enum Sumfunctype sum_func() const override { return PERCENT_RANK_FUNC; }
2450
2451 bool resolve_type(THD *thd [[maybe_unused]]) override {
2453 return false;
2454 }
2455
2456 bool check_wf_semantics1(THD *thd, Query_block *select,
2457 Window_evaluation_requirements *reqs) override;
2458 bool needs_partition_cardinality() const override { return true; }
2459
2460 void clear() override;
2461 longlong val_int() override;
2462 double val_real() override;
2463 String *val_str(String *) override;
2465 Item_result result_type() const override { return REAL_RESULT; }
2466};
2467
2468/**
2469 NTILE window function, cf. SQL 2011 Section 6.10 <window function>
2470*/
2474
2475 public:
2476 Item_ntile(const POS &pos, Item *a, PT_window *w)
2477 : Item_non_framing_wf(pos, a, w), m_value(0) {
2478 unsigned_flag = true;
2479 }
2480
2481 const char *func_name() const override { return "ntile"; }
2482 enum Sumfunctype sum_func() const override { return NTILE_FUNC; }
2483
2484 bool resolve_type(THD *thd) override {
2485 if (args[0]->propagate_type(thd, MYSQL_TYPE_LONGLONG, true)) return true;
2487 return false;
2488 }
2489
2490 bool fix_fields(THD *thd, Item **items) override;
2491
2492 longlong val_int() override;
2493 double val_real() override;
2494 my_decimal *val_decimal(my_decimal *buff) override;
2495 String *val_str(String *) override;
2496
2497 bool check_wf_semantics1(THD *thd, Query_block *select,
2498 Window_evaluation_requirements *reqs) override;
2500 Item_result result_type() const override { return INT_RESULT; }
2501 void clear() override {}
2502 bool needs_partition_cardinality() const override { return true; }
2503};
2504
2505/**
2506 LEAD/LAG window functions, cf. SQL 2011 Section 6.10 <window function>
2507*/
2510 bool m_is_lead; ///< if true, the function is LEAD, else LAG
2511 int64 m_n; ///< canonicalized offset value
2515 /**
2516 Execution state: if set, we already have a value for current row.
2517 State is used to avoid interference with other LEAD/LAG functions on
2518 the same window, since they share the same eval loop and they should
2519 trigger evaluation only when they are on the "right" row relative to
2520 current row. For other offsets, return NULL if we don't know the value
2521 for this function yet, or if we do (m_has_value==true), return the
2522 found value.
2523 */
2525 bool m_use_default; ///< execution state: use default value for current row
2527
2528 public:
2529 Item_lead_lag(const POS &pos, bool lead,
2530 PT_item_list *opt_list, // [0] expr, [1] offset, [2] default
2531 enum_null_treatment null_treatment, PT_window *w)
2532 : Item_non_framing_wf(pos, opt_list, w),
2533 m_null_treatment(null_treatment),
2534 m_is_lead(lead),
2535 m_n(0),
2539 m_has_value(false),
2540 m_use_default(false) {}
2541
2542 const char *func_name() const override {
2543 return (m_is_lead ? "lead" : "lag");
2544 }
2545 enum Sumfunctype sum_func() const override { return LEAD_LAG_FUNC; }
2546
2547 bool resolve_type(THD *thd) override;
2548 bool fix_fields(THD *thd, Item **items) override;
2549 TYPELIB *get_typelib() const override;
2550 void update_after_wf_arguments_changed(THD *thd) override;
2551 void clear() override;
2552 bool check_wf_semantics1(THD *thd, Query_block *select,
2553 Window_evaluation_requirements *reqs) override;
2555 enum Item_result result_type() const override { return m_hybrid_type; }
2556
2557 longlong val_int() override;
2558 double val_real() override;
2559 String *val_str(String *str) override;
2560 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2561
2562 bool val_date(Date_val *date, my_time_flags_t flags) override;
2563 bool val_time(Time_val *time) override;
2564 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
2565 bool val_json(Json_wrapper *wr) override;
2566
2567 bool needs_partition_cardinality() const override {
2568 /*
2569 A possible optimization here: if LAG, we are only interested in rows we
2570 have already seen, so we might compute the result without reading the
2571 entire partition as soon as we have the current row. Similarly, a small
2572 LEAD value might avoid reading the entire partition also, giving shorter
2573 time to first result. For now, we read the entirely partition for these
2574 window functions - for simplicity.
2575 */
2576 return true;
2577 }
2578
2579 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2580 mem_root_deque<Item *> *fields) override;
2581
2583 bool has_value() const { return m_has_value; }
2584
2586 bool use_default() const { return m_use_default; }
2587
2588 private:
2589 bool setup_lead_lag();
2590 /**
2591 Core logic of LEAD/LAG window functions
2592
2593 @return true if computation yielded a NULL or error
2594 */
2595 bool compute();
2596};
2597
2598/**
2599 FIRST_VALUE/LAST_VALUE window functions, cf. SQL 2011 Section 6.10 <window
2600 function>
2601*/
2603 bool m_is_first; ///< if true, the function is FIRST_VALUE, else LAST_VALUE
2607 int64 cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2609
2610 public:
2611 Item_first_last_value(const POS &pos, bool first, Item *a,
2612 enum_null_treatment null_treatment, PT_window *w)
2613 : Item_sum(pos, a, w),
2614 m_is_first(first),
2615 m_null_treatment(null_treatment),
2618 cnt(0) {}
2619
2620 const char *func_name() const override {
2621 return m_is_first ? "first_value" : "last_value";
2622 }
2623
2624 enum Sumfunctype sum_func() const override { return FIRST_LAST_VALUE_FUNC; }
2625
2626 bool resolve_type(THD *thd) override;
2627 bool fix_fields(THD *thd, Item **items) override;
2628 void update_after_wf_arguments_changed(THD *thd) override;
2629 void clear() override;
2630 bool check_wf_semantics1(THD *thd, Query_block *select,
2631 Window_evaluation_requirements *reqs) override;
2632 enum Item_result result_type() const override { return m_hybrid_type; }
2633
2634 longlong val_int() override;
2635 double val_real() override;
2636 String *val_str(String *str) override;
2637 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2638
2639 bool val_date(Date_val *date, my_time_flags_t flags) override;
2640 bool val_time(Time_val *time) override;
2641 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
2642 bool val_json(Json_wrapper *wr) override;
2643
2644 void reset_field() override { assert(false); }
2645 void update_field() override { assert(false); }
2646 bool add() override {
2647 assert(false);
2648 return false;
2649 }
2650
2651 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2652 mem_root_deque<Item *> *fields) override;
2653 bool uses_only_one_row() const override { return true; }
2654
2655 private:
2656 bool setup_first_last();
2657 /**
2658 Core logic of FIRST/LAST_VALUE window functions
2659
2660 @return true if computation yielded a NULL or error
2661 */
2662 bool compute();
2663};
2664
2665/**
2666 NTH_VALUE window function, cf. SQL 2011 Section 6.10 <window
2667 function>
2668*/
2669class Item_nth_value : public Item_sum {
2671 int64 m_n; ///< The N of the function
2672 bool m_from_last; ///< true iff FROM_LAST was specified
2676 int64 m_cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2677
2679
2680 public:
2681 Item_nth_value(const POS &pos, PT_item_list *a, bool from_last,
2682 enum_null_treatment null_treatment, PT_window *w)
2683 : Item_sum(pos, a, w),
2684 m_null_treatment(null_treatment),
2685 m_n(0),
2686 m_from_last(from_last),
2689 m_cnt(0) {}
2690
2691 const char *func_name() const override { return "nth_value"; }
2692 enum Sumfunctype sum_func() const override { return NTH_VALUE_FUNC; }
2693
2694 bool resolve_type(THD *thd) override;
2695 bool fix_fields(THD *thd, Item **items) override;
2696 void update_after_wf_arguments_changed(THD *thd) override;
2697 bool setup_nth();
2698 void clear() override;
2699
2700 bool check_wf_semantics1(THD *thd, Query_block *select,
2701 Window_evaluation_requirements *reqs) override;
2703
2704 enum Item_result result_type() const override { return m_hybrid_type; }
2705
2706 longlong val_int() override;
2707 double val_real() override;
2708 String *val_str(String *str) override;
2709 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2710
2711 bool val_date(Date_val *date, my_time_flags_t flags) override;
2712 bool val_time(Time_val *time) override;
2713 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
2714 bool val_json(Json_wrapper *wr) override;
2715
2716 void reset_field() override { assert(false); }
2717 void update_field() override { assert(false); }
2718 bool add() override {
2719 assert(false);
2720 return false;
2721 }
2722
2723 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2724 mem_root_deque<Item *> *fields) override;
2725 bool uses_only_one_row() const override { return true; }
2726
2727 private:
2728 /**
2729 Core logic of NTH_VALUE window functions
2730
2731 @return true if computation yielded a NULL or error
2732 */
2733 bool compute();
2734};
2735
2736/**
2737 Class for implementation of the GROUPING function. The GROUPING
2738 function distinguishes super-aggregate rows from regular grouped
2739 rows. GROUP BY extensions such as ROLLUP and CUBE produce
2740 super-aggregate rows where the set of all values is represented
2741 by null. Using the GROUPING function, you can distinguish a null
2742 representing the set of all values in a super-aggregate row from
2743 a NULL in a regular row.
2744*/
2746 public:
2749 }
2750 const char *func_name() const override { return "grouping"; }
2751 enum Functype functype() const override { return GROUPING_FUNC; }
2752 longlong val_int() override;
2753 bool aggregate_check_group(uchar *arg) override;
2754 bool fix_fields(THD *thd, Item **ref) override;
2755 void update_used_tables() override;
2756 bool aggregate_check_distinct(uchar *arg) override;
2757 bool check_args_found_in_group_by() const;
2759
2760 private:
2761 /// The query block in which this function is called.
2763};
2764
2765/**
2766 A wrapper Item that contains a number of aggregate items, one for each level
2767 of rollup (see Item_rollup_group_item for numbering conventions). When
2768 aggregating, every aggregator is either reset or updated as per the correct
2769 level, and when returning a value, the correct child item corresponding to
2770 the current rollup level is queried.
2771 */
2773 public:
2774 explicit Item_rollup_sum_switcher(List<Item> *sum_func_per_level)
2775 : Item_sum((*sum_func_per_level)[0]),
2776 m_num_levels(sum_func_per_level->size()) {
2777 args = (*THR_MALLOC)->ArrayAlloc<Item *>(sum_func_per_level->size());
2778 int i = 0;
2779 for (Item &item : *sum_func_per_level) {
2780 args[i++] = &item;
2781 }
2785 hidden = master()->hidden;
2789 }
2790 double val_real() override;
2791 longlong val_int() override;
2792 String *val_str(String *str) override;
2794 bool val_json(Json_wrapper *result) override;
2795 bool is_null() override;
2796 bool val_date(Date_val *date, my_time_flags_t flags) override;
2797 bool val_time(Time_val *time) override;
2798 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
2799 const char *func_name() const override { return "rollup_sum_switcher"; }
2800 table_map used_tables() const override { return master()->used_tables(); }
2801 Item_result result_type() const override { return master()->result_type(); }
2802 bool resolve_type(THD *) override {
2804 return false;
2805 }
2806 void print(const THD *thd, String *str,
2807 enum_query_type query_type) const override;
2808 Field *create_tmp_field(bool group, TABLE *table) override;
2809
2810 enum Sumfunctype sum_func() const override { return master()->sum_func(); }
2811 enum Sumfunctype real_sum_func() const override {
2813 }
2814 void reset_field() override { assert(false); }
2815 void update_field() override { assert(false); }
2816 void clear() override;
2817 bool add() override {
2818 assert(false);
2819 return true;
2820 }
2821 void no_rows_in_result() override {
2822 // Produce a single grouping row with NULLs for all group by
2823 // expressions and grand total for aggregates.
2826 }
2827
2828 bool reset_and_add_for_rollup(int last_unchanged_group_item_idx);
2829
2830 int set_aggregator(Aggregator::Aggregator_type aggregator) override;
2831 bool aggregator_setup(THD *thd) override;
2832 inline bool aggregator_add_all() {
2833 for (int i = 0; i < m_num_levels; ++i) {
2834 if (child(i)->aggregator_add()) {
2835 return true;
2836 }
2837 }
2838 return false;
2839 }
2840
2841 // Used when create_tmp_table() needs to delay application of aggregate
2842 // functions to a later stage in the query processing.
2843 Item *get_arg(uint i) override { return master()->get_arg(i); }
2844 const Item *get_arg(uint i) const override { return master()->get_arg(i); }
2845 Item *set_arg(THD *thd, uint i, Item *new_val) override {
2846 Item *ret = nullptr;
2847 for (int j = 0; j < m_num_levels; ++j) {
2848 ret = child(j)->set_arg(thd, i, new_val);
2849 }
2850 return ret; // Return the last one, arbitrarily.
2851 }
2852 uint argument_count() const override { return master()->argument_count(); }
2853
2854 // Used by AggregateIterator.
2856 inline Item_sum *master() const { return child(0); }
2857
2858 bool is_rollup_sum_wrapper() const override { return true; }
2859 const Item_sum *unwrap_sum() const override { return master(); }
2860 Item_sum *unwrap_sum() override { return master(); }
2861
2862 private:
2863 inline Item *current_arg() const;
2864 inline Item_sum *child(size_t i) const {
2865 return down_cast<Item_sum *>(args[i]);
2866 }
2867
2868 const int m_num_levels;
2870};
2871/// Implements ST_Collect which aggregates geometries into Multipoints,
2872/// Multilinestrings, Multipolygons and Geometrycollections.
2873
2875 private:
2876 std::optional<gis::srid_t> srid;
2877 std::unique_ptr<gis::Geometrycollection> m_geometrycollection;
2878 void pop_front();
2879
2880 public:
2882 Item_sum_collect(THD *thd, Item_sum *item) : Item_sum(thd, item) {
2884 }
2885 Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
2886 : Item_sum(pos, a, w) {
2887 set_distinct(distinct);
2889 }
2890
2891 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2892 longlong val_int() override { return val_int_from_string(); }
2893 double val_real() override { return val_real_from_string(); }
2894 bool val_date(Date_val *, my_time_flags_t) override { return true; }
2895 bool val_time(Time_val *) override { return true; }
2896 bool val_datetime(Datetime_val *, my_time_flags_t) override { return true; }
2897 enum Sumfunctype sum_func() const override { return GEOMETRY_AGGREGATE_FUNC; }
2898 Item_result result_type() const override { return STRING_RESULT; }
2900 /*
2901 In contrast to Item_sum, Item_sum_collect always uses Aggregator_simple,
2902 and only needs to reset its aggregator when called.
2903 */
2904 if (aggr != nullptr) {
2905 aggr->clear();
2906 return false;
2907 }
2908
2909 aggr = new (*THR_MALLOC) Aggregator_simple(this);
2910 return aggr ? false : true;
2911 }
2912
2913 bool fix_fields(THD *thd, Item **ref) override;
2914 /*
2915 We need to override check_wf_semantics1 because it reports an error when
2916 with_distinct is set.
2917 */
2918 bool check_wf_semantics1(THD *thd, Query_block *,
2920
2921 Item *copy_or_same(THD *thd) override;
2922
2923 void update_field() override;
2924 void reset_field() override;
2925
2926 String *val_str(String *str) override;
2927
2928 bool add() override;
2929
2930 void read_result_field();
2931 void store_result_field();
2932
2933 void clear() override;
2934 const char *func_name() const override { return "st_collect"; }
2935};
2936
2937#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:2263
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:2213
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:2258
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:2206
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:2253
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:2230
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:2234
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:2238
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:3870
String * val_str(String *) override
Definition: item_sum.cc:3912
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3898
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:3887
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:3995
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:4010
ulonglong m_reset_bits
Definition: item_sum.h:1232
longlong val_int() override
Definition: item_sum.cc:3934
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:4002
double val_real() override
Definition: item_sum.cc:3949
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3966
String * val_str(String *) override
Definition: item_sum.cc:3974
Item_aggr_bit_field(Item_sum_bit *item, ulonglong reset_bits)
Definition: item_sum.cc:3917
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:1531
enum Type type() const override
Definition: item_sum.h:1534
enum Item_result result_type() const override
Definition: item_sum.h:1537
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:1538
double val_real() override
Definition: item_sum.cc:4020
Item_aggr_std_field(Item_sum_std *item)
Definition: item_sum.cc:4017
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4026
Definition: item_sum.h:1397
double val_real() override
Definition: item_sum.cc:4060
Item_aggr_variance_field(Item_sum_variance *item)
Definition: item_sum.cc:4048
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:1406
String * val_str(String *str) override
Definition: item_sum.h:1402
my_decimal * val_decimal(my_decimal *dec_buf) override
Definition: item_sum.h:1403
uint m_sample
Definition: item_sum.h:1414
enum Type type() const override
Definition: item_sum.h:1400
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:6941
CUME_DIST window function, cf.
Definition: item_sum.h:2401
Item_cume_dist(const POS &pos, PT_window *w)
Definition: item_sum.h:2405
Item_non_framing_wf super
Definition: item_sum.h:2402
double val_real() override
Definition: item_sum.cc:5114
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2408
void clear() override
Definition: item_sum.h:2419
const char * func_name() const override
Definition: item_sum.h:2407
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:2418
String * val_str(String *) override
Definition: item_sum.cc:5134
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:5104
longlong val_int() override
Definition: item_sum.cc:5126
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2410
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:5138
Item_result result_type() const override
Definition: item_sum.h:2424
Definition: item.h:4540
FIRST_VALUE/LAST_VALUE window functions, cf.
Definition: item_sum.h:2602
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2624
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:5453
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5342
void reset_field() override
Definition: item_sum.h:2644
const char * func_name() const override
Definition: item_sum.h:2620
bool setup_first_last()
Definition: item_sum.cc:5384
double val_real() override
Definition: item_sum.cc:5433
String * val_str(String *str) override
Definition: item_sum.cc:5496
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:5401
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:5372
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:5328
bool compute()
Core logic of FIRST/LAST_VALUE window functions.
Definition: item_sum.cc:5405
int64 cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2607
Item_result m_hybrid_type
Definition: item_sum.h:2605
enum Item_result result_type() const override
Definition: item_sum.h:2632
Item_cache * m_value
Definition: item_sum.h:2606
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5474
void update_field() override
Definition: item_sum.h:2645
enum_null_treatment m_null_treatment
Definition: item_sum.h:2604
Item_sum super
Definition: item_sum.h:2608
void clear() override
Definition: item_sum.cc:5395
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2653
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:5443
bool add() override
Definition: item_sum.h:2646
Item_first_last_value(const POS &pos, bool first, Item *a, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2611
bool m_is_first
if true, the function is FIRST_VALUE, else LAST_VALUE
Definition: item_sum.h:2603
longlong val_int() override
Definition: item_sum.cc:5423
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5484
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5353
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:5464
Definition: item_sum.h:2146
void clear() override
Definition: item_sum.cc:4576
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2239
bool m_result_finalized
True if result has been written to output buffer.
Definition: item_sum.h:2185
String result
Definition: item_sum.h:2161
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:2190
longlong val_int() override
Definition: item_sum.h:2226
const String * get_separator_str() const noexcept
Definition: item_sum.h:2245
uint32_t get_group_concat_max_len() const noexcept
Definition: item_sum.h:2246
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.h:2233
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:4320
uint row_count
Definition: item_sum.h:2176
void make_unique() override
Definition: item_sum.cc:4849
bool change_context_processor(uchar *arg) override
Definition: item_sum.h:2259
bool setup(THD *thd) override
Definition: item_sum.cc:4732
Item_sum super
Definition: item_sum.h:2147
bool distinct
True if GROUP CONCAT has the DISTINCT attribute.
Definition: item_sum.h:2150
TABLE * table
Temporary table used to perform group concat.
Definition: item_sum.h:2174
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:4241
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:2240
double val_real() override
Definition: item_sum.cc:4857
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2214
String * val_str(String *str) override
Definition: item_sum.cc:4864
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:4277
bool force_copy_fields
Definition: item_sum.h:2183
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:4639
Unique * unique_filter
If DISTINCT is used with this GROUP_CONCAT, this member is used to filter out duplicates.
Definition: item_sum.h:2172
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2264
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:4574
Mem_root_array< ORDER > order_array
Definition: item_sum.h:2175
TREE tree_base
Definition: item_sum.h:2162
void update_field() override
Definition: item_sum.h:2221
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:4889
Temp_table_param * tmp_table_param
Describes the temporary table used to perform group concat.
Definition: item_sum.h:2160
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:4501
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4566
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:4449
const char * func_name() const override
Definition: item_sum.h:2215
Item_result result_type() const override
Definition: item_sum.h:2216
Field * make_string_field(TABLE *table_arg) const override
Create a field to hold a string value from an item.
Definition: item_sum.cc:4533
uint64 hash() override
Generate hash unique to an item depending on its attributes.
Definition: item_sum.cc:4925
uint m_order_arg_count
The number of ORDER BY items.
Definition: item_sum.h:2152
TREE * tree
Definition: item_sum.h:2163
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:4411
bool has_distinct() const noexcept
Definition: item_sum.h:2244
bool add() override
Definition: item_sum.cc:4588
Name_resolution_context * context
Resolver context, points to containing query block.
Definition: item_sum.h:2156
void reset_field() override
Definition: item_sum.h:2220
uint m_field_arg_count
The number of selected items, aka the concat field list.
Definition: item_sum.h:2154
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:2181
bool warning_for_row
Definition: item_sum.h:2182
String * separator
String containing separator between group items.
Definition: item_sum.h:2158
~Item_func_group_concat() override
Definition: item_sum.h:2207
const Mem_root_array< ORDER > & get_order_array() const noexcept
Definition: item_sum.h:2249
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:2236
Class for implementation of the GROUPING function.
Definition: item_sum.h:2745
const Query_block * m_query_block
The query block in which this function is called.
Definition: item_sum.h:2762
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:6677
longlong val_int() override
Evaluation of the GROUPING function.
Definition: item_sum.cc:6626
Item_func_grouping(const POS &pos, PT_item_list *a)
Definition: item_sum.h:2747
const char * func_name() const override
Definition: item_sum.h:2750
bool fix_fields(THD *thd, Item **ref) override
Resolve GROUPING function.
Definition: item_sum.cc:6557
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:6647
bool check_args_found_in_group_by() const
Definition: item_sum.cc:6595
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:6609
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_sum.cc:6689
enum Functype functype() const override
Definition: item_sum.h:2751
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:4426
void set_original_field_name(const char *name_arg)
Definition: item.h:4423
const char * original_table_name() const
Definition: item.h:4427
Definition: item_func.h:1048
Definition: item.h:5302
LEAD/LAG window functions, cf.
Definition: item_sum.h:2508
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5777
bool has_value() const
Definition: item_sum.h:2583
bool m_is_lead
if true, the function is LEAD, else LAG
Definition: item_sum.h:2510
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:2567
const char * func_name() const override
Definition: item_sum.h:2542
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:5837
void set_use_default(bool value)
Definition: item_sum.h:2585
String * val_str(String *str) override
Definition: item_sum.cc:5915
bool m_use_default
execution state: use default value for current row
Definition: item_sum.h:2525
enum Item_result result_type() const override
Definition: item_sum.h:2555
Item_cache * m_default
Definition: item_sum.h:2514
bool compute()
Core logic of LEAD/LAG window functions.
Definition: item_sum.cc:5964
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:5937
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:2529
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:5864
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2545
enum_null_treatment m_null_treatment
Definition: item_sum.h:2509
void clear() override
Definition: item_sum.cc:5876
bool use_default() const
Definition: item_sum.h:2586
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:5928
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.cc:5786
bool m_has_value
Execution state: if set, we already have a value for current row.
Definition: item_sum.h:2524
longlong val_int() override
Definition: item_sum.cc:5888
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5904
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5809
Item_result m_hybrid_type
Definition: item_sum.h:2512
int64 m_n
canonicalized offset value
Definition: item_sum.h:2511
double val_real() override
Definition: item_sum.cc:5896
void set_has_value(bool value)
Definition: item_sum.h:2582
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5727
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5954
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:5946
bool setup_lead_lag()
Definition: item_sum.cc:5848
Item_cache * m_value
Definition: item_sum.h:2513
Item_non_framing_wf super
Definition: item_sum.h:2526
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:5883
Common parent class for window functions that always work on the entire partition,...
Definition: item_sum.h:2280
Item_non_framing_wf(const POS &pos, PT_window *w)
Definition: item_sum.h:2284
bool add() override
Definition: item_sum.h:2300
Item_non_framing_wf(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2285
Item_non_framing_wf(const POS &pos, PT_item_list *opt_list, PT_window *w)
Definition: item_sum.h:2287
Item_sum super
Definition: item_sum.h:2281
Item_non_framing_wf(THD *thd, Item_non_framing_wf *i)
Definition: item_sum.h:2289
void update_field() override
Definition: item_sum.h:2299
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:2291
void reset_field() override
Definition: item_sum.h:2298
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2294
bool framing() const override
All aggregates are framing, i.e.
Definition: item_sum.h:2307
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:2295
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:4944
NTH_VALUE window function, cf.
Definition: item_sum.h:2669
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:5707
int64 m_cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2676
void update_field() override
Definition: item_sum.h:2717
bool compute()
Core logic of NTH_VALUE window functions.
Definition: item_sum.cc:5612
String * val_str(String *str) override
Definition: item_sum.cc:5677
Item_sum super
Definition: item_sum.h:2678
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5218
enum Item_result result_type() const override
Definition: item_sum.h:2704
Item_result m_hybrid_type
Definition: item_sum.h:2673
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5665
bool setup_nth()
Definition: item_sum.cc:5571
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:5697
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5520
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:5687
enum_field_types m_hybrid_field_type
Definition: item_sum.h:2674
int64 m_n
The N of the function.
Definition: item_sum.h:2671
double val_real() override
Definition: item_sum.cc:5655
bool m_from_last
true iff FROM_LAST was specified
Definition: item_sum.h:2672
Item_cache * m_value
Definition: item_sum.h:2675
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5717
enum_null_treatment m_null_treatment
Definition: item_sum.h:2670
void reset_field() override
Definition: item_sum.h:2716
longlong val_int() override
Definition: item_sum.cc:5645
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:2681
void clear() override
Definition: item_sum.cc:5582
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5506
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:5560
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2692
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:5588
bool add() override
Definition: item_sum.h:2718
const char * func_name() const override
Definition: item_sum.h:2691
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:5592
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2725
NTILE window function, cf.
Definition: item_sum.h:2471
void clear() override
Definition: item_sum.h:2501
Item_non_framing_wf super
Definition: item_sum.h:2472
String * val_str(String *) override
Definition: item_sum.cc:5297
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2484
const char * func_name() const override
Definition: item_sum.h:2481
bool needs_partition_cardinality() const override
Return true if we need to make two passes over the rows in the partition - either because we need the...
Definition: item_sum.h:2502
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5312
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5236
double val_real() override
Definition: item_sum.cc:5292
longlong val_int() override
Definition: item_sum.cc:5242
longlong m_value
Definition: item_sum.h:2473
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:5299
Item_ntile(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2476
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2482
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:5304
Item_result result_type() const override
Definition: item_sum.h:2500
PERCENT_RANK window function, cf.
Definition: item_sum.h:2430
String * val_str(String *) override
Definition: item_sum.cc:5201
Item_percent_rank(const POS &pos, PT_window *w)
Definition: item_sum.h:2441
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2433
const char * func_name() const override
Definition: item_sum.h:2448
Item_result result_type() const override
Definition: item_sum.h:2465
~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:5143
ulonglong m_peers
Needed to make PERCENT_RANK same for peers.
Definition: item_sum.h:2434
double val_real() override
Definition: item_sum.cc:5167
Item_non_framing_wf super
Definition: item_sum.h:2431
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2451
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2449
bool needs_partition_cardinality() const override
Return true if we need to make two passes over the rows in the partition - either because we need the...
Definition: item_sum.h:2458
bool m_last_peer_visited
Set when the last peer has been visited.
Definition: item_sum.h:2438
void clear() override
Definition: item_sum.cc:5210
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:5205
longlong val_int() override
Definition: item_sum.cc:5193
RANK or DENSE_RANK window function, cf.
Definition: item_sum.h:2350
Item_result result_type() const override
Definition: item_sum.h:2395
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:4999
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:5023
ulonglong m_duplicates
Needed to make RANK different from DENSE_RANK.
Definition: item_sum.h:2355
bool m_dense
If true, the object represents DENSE_RANK.
Definition: item_sum.h:2352
longlong val_int() override
Definition: item_sum.cc:5041
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2374
~Item_rank() override
Definition: item_sum.cc:5097
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2378
Item_rank(const POS &pos, bool dense, PT_window *w)
Definition: item_sum.h:2359
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:5076
const char * func_name() const override
Definition: item_sum.h:2370
double val_real() override
Definition: item_sum.cc:5069
String * val_str(String *) override
Definition: item_sum.cc:5074
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2354
void clear() override
Clear state for a new partition.
Definition: item_sum.cc:5081
Mem_root_array< Cached_item * > m_previous
Values of previous row's ORDER BY items.
Definition: item_sum.h:2357
Item_non_framing_wf super
Definition: item_sum.h:2351
Item with result field.
Definition: item.h:6031
longlong llrint_with_overflow_check(double realval)
Definition: item.h:6070
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2772
table_map used_tables() const override
Definition: item_sum.h:2800
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:6705
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:6770
void update_field() override
Definition: item_sum.h:2815
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2802
Item * get_arg(uint i) override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2843
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.h:2845
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.cc:6756
void set_current_rollup_level(int level)
Definition: item_sum.h:2855
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:6761
bool aggregator_setup(THD *thd) override
Called to initialize the aggregator.
Definition: item_sum.cc:6817
Item_result result_type() const override
Definition: item_sum.h:2801
String * val_str(String *str) override
Definition: item_sum.cc:6735
int set_aggregator(Aggregator::Aggregator_type aggregator) override
Definition: item_sum.cc:6806
Item_sum * unwrap_sum() override
Non-const version.
Definition: item_sum.h:2860
longlong val_int() override
Definition: item_sum.cc:6728
void reset_field() override
Definition: item_sum.h:2814
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:2859
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:6716
Item_rollup_sum_switcher(List< Item > *sum_func_per_level)
Definition: item_sum.h:2774
bool aggregator_add_all()
Definition: item_sum.h:2832
Item * current_arg() const
Definition: item_sum.cc:6700
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:2821
Item_sum * master() const
Definition: item_sum.h:2856
Item_sum * child(size_t i) const
Definition: item_sum.h:2864
enum Sumfunctype real_sum_func() const override
Definition: item_sum.h:2811
bool is_rollup_sum_wrapper() const override
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:2858
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2810
const char * func_name() const override
Definition: item_sum.h:2799
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:6710
int m_current_rollup_level
Definition: item_sum.h:2869
double val_real() override
Definition: item_sum.cc:6721
bool val_json(Json_wrapper *result) override
Get a JSON value from an Item.
Definition: item_sum.cc:6749
uint argument_count() const override
Definition: item_sum.h:2852
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:6742
bool add() override
Definition: item_sum.h:2817
void clear() override
Definition: item_sum.cc:6788
bool reset_and_add_for_rollup(int last_unchanged_group_item_idx)
Definition: item_sum.cc:6794
const int m_num_levels
Definition: item_sum.h:2868
const Item * get_arg(uint i) const override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2844
ROW_NUMBER window function, cf.
Definition: item_sum.h:2313
ulonglong m_ctr
Increment for each row in partition.
Definition: item_sum.h:2315
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2326
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2324
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2340
Item_row_number(const POS &pos, PT_window *w)
Definition: item_sum.h:2318
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4992
Item_result result_type() const override
Definition: item_sum.h:2338
String * val_str(String *) override
Definition: item_sum.cc:4988
longlong val_int() override
Definition: item_sum.cc:4969
void clear() override
Definition: item_sum.cc:4997
const char * func_name() const override
Definition: item_sum.h:2323
double val_real() override
Definition: item_sum.cc:4983
Definition: item_sum.h:1948
Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1950
const char * func_name() const override
Definition: item_sum.h:1954
Item_sum_and(THD *thd, Item_sum_and *item)
Definition: item_sum.h:1953
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3385
Definition: item_sum.h:1357
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2331
void clear() override
Definition: item_sum.cc:2387
String * val_str(String *str) override
Definition: item_sum.cc:2498
const char * func_name() const override
Definition: item_sum.h:1385
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3661
Item_sum_sum super
Definition: item_sum.h:1361
Item * result_item(Field *) override
Definition: item_sum.h:1384
double val_real() override
Definition: item_sum.cc:2396
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1388
Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
Definition: item_sum.h:1365
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:2365
my_decimal m_avg_dec
Definition: item_sum.h:1362
uint f_scale
Definition: item_sum.h:1360
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1372
double m_avg
Definition: item_sum.h:1363
longlong val_int() override
Definition: item_sum.h:1379
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2358
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2419
void reset_field() override
Definition: item_sum.cc:3556
uint prec_increment
Definition: item_sum.h:1359
uint dec_bin_size
Definition: item_sum.h:1360
bool add() override
Definition: item_sum.cc:2389
Item_sum_avg(THD *thd, Item_sum_avg *item)
Definition: item_sum.h:1368
uint f_precision
Definition: item_sum.h:1360
Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
Definition: item_sum.h:1778
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:1888
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:3267
const char initial_value_buff_storage[1]
Buffer used to avoid String allocation in the constructor.
Definition: item_sum.h:1789
static constexpr uint DIGIT_CNT_CARD
Definition: item_sum.h:1832
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1874
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:1835
void clear() override
Definition: item_sum.cc:3357
Item * result_item(Field *) override
Definition: item_sum.h:1870
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:3281
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:1529
longlong val_int() override
Definition: item_sum.cc:3333
Item_sum super
Definition: item_sum.h:1779
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:1824
Item_result hybrid_type
Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT.
Definition: item_sum.h:1787
ulonglong reset_bits
Stores the neutral element for function.
Definition: item_sum.h:1781
Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg, PT_window *w)
Definition: item_sum.h:1838
bool is_and() const
Definition: item_sum.h:1901
void reset_field() override
Definition: item_sum.cc:3587
ulonglong m_count
Execution state (windowing): this is for counting rows entering and leaving the window frame,...
Definition: item_sum.h:1795
double val_real() override
Definition: item_sum.cc:3307
bool add() override
Common implementation of Item_sum_or::add, Item_sum_and:add and Item_sum_xor::add.
Definition: item_sum.cc:1786
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.cc:3288
enum Item_result result_type() const override
Definition: item_sum.h:1875
uint m_digit_cnt_card
Definition: item_sum.h:1830
Item_sum_bit(THD *thd, Item_sum_bit *item)
Copy constructor, used for executing subqueries with temporary tables.
Definition: item_sum.h:1851
String * val_str(String *str) override
Definition: item_sum.cc:3236
void update_field() override
Definition: item_sum.cc:3597
ulonglong bits
Stores the result value for the INT_RESULT.
Definition: item_sum.h:1783
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:1803
String value_buff
Stores the result value for the STRING_RESULT.
Definition: item_sum.h:1785
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:3274
Implements ST_Collect which aggregates geometries into Multipoints, Multilinestrings,...
Definition: item_sum.h:2874
bool add() override
Definition: item_sum.cc:6892
void clear() override
Definition: item_sum.cc:6886
void read_result_field()
Definition: item_sum.cc:6947
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:6855
std::optional< gis::srid_t > srid
Definition: item_sum.h:2876
Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
Definition: item_sum.h:2885
void update_field() override
Definition: item_sum.cc:7040
longlong val_int() override
Definition: item_sum.h:2892
bool val_time(Time_val *) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2895
std::unique_ptr< gis::Geometrycollection > m_geometrycollection
Definition: item_sum.h:2877
String * val_str(String *str) override
Definition: item_sum.cc:6993
bool val_datetime(Datetime_val *, my_time_flags_t) override
Evaluate the item and return result as a datetime value.
Definition: item_sum.h:2896
void reset_field() override
Definition: item_sum.cc:7103
Item_sum_collect(THD *thd, Item_sum *item)
Definition: item_sum.h:2882
void store_result_field()
Definition: item_sum.cc:7046
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:7098
bool check_wf_semantics1(THD *thd, Query_block *, Window_evaluation_requirements *r) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:6877
const char * func_name() const override
Definition: item_sum.h:2934
bool val_date(Date_val *, my_time_flags_t) override
Evaluate the item and return result as a date value.
Definition: item_sum.h:2894
void pop_front()
Definition: item_sum.cc:6986
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2897
Item_result result_type() const override
Definition: item_sum.h:2898
int set_aggregator(Aggregator::Aggregator_type) override
Definition: item_sum.h:2899
double val_real() override
Definition: item_sum.h:2893
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6942
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:2293
Item_sum_count(Item *item, bool distinct)
Definition: item_sum.h:1097
void update_field() override
Definition: item_sum.cc:3652
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2274
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:2325
void make_const(longlong count_arg)
Definition: item_sum.h:1125
void reset_field() override
Definition: item_sum.cc:3548
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:2282
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:2284
Abstract base class for the MIN and MAX aggregate functions.
Definition: item_sum.h:1572
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3189
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:3184
bool setup_hybrid(Item *item, Item *value_arg)
MIN/MAX function setup.
Definition: item_sum.cc:1878
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:1723
virtual Item_sum_hybrid * clone_hybrid(THD *thd) const =0
Create a clone of this object.
bool add() override
Definition: item_sum.cc:3219
const bool m_is_min
Tells if this is the MIN function (true) or the MAX function (false).
Definition: item_sum.h:1579
bool compute()
This function implements the optimized version of retrieving min/max value.
Definition: item_sum.cc:2923
bool has_values()
Definition: item_sum.h:1720
void min_max_update_int_field()
Definition: item_sum.cc:3835
longlong val_int() override
Definition: item_sum.cc:3056
bool m_has_values
Definition: item_sum.h:1591
void reset_field() override
Reset field before aggregation.
Definition: item_sum.cc:3415
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:3098
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:3155
void clear() override
Definition: item_sum.cc:2874
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:1619
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:3118
Item_cache * arg_cache
Definition: item_sum.h:1588
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3170
void min_max_update_datetime_field()
Definition: item_sum.cc:3768
void min_max_update_json_field()
Definition: item_sum.cc:3788
void update_field() override
Definition: item_sum.cc:3698
longlong val_date_temporal() override
Return date value of item in packed longlong format.
Definition: item_sum.cc:3070
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:2884
bool keep_field_type() const override
Definition: item_sum.h:1712
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3082
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:1613
void min_max_update_real_field()
Definition: item_sum.cc:3822
Item_sum_hybrid(THD *thd, const Item_sum_hybrid *item)
Definition: item_sum.h:1683
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:3108
Item_result hybrid_type
Definition: item_sum.h:1590
bool m_optimize
Set to true when min/max can be optimized using window's ordering.
Definition: item_sum.h:1599
void min_max_update_time_field()
Definition: item_sum.cc:3731
Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
Definition: item_sum.h:1667
Item_sum_hybrid(Item *item_par, bool is_min)
Definition: item_sum.h:1651
Item_sum super
Definition: item_sum.h:1573
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:2889
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1847
void min_max_update_date_field()
Definition: item_sum.cc:3750
String * val_str(String *) override
Definition: item_sum.cc:3128
Arg_comparator * cmp
Definition: item_sum.h:1589
void min_max_update_str_field()
Definition: item_sum.cc:3806
enum Item_result result_type() const override
Definition: item_sum.h:1713
void min_max_update_decimal_field()
Definition: item_sum.cc:3853
double val_real() override
Definition: item_sum.cc:3042
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:1895
bool m_want_first
For min() - Set to true when results are ordered in ascending and false when descending.
Definition: item_sum.h:1607
Item_cache * value
Definition: item_sum.h:1588
bool m_nulls_first
Set to true if the window is ordered ascending.
Definition: item_sum.h:1595
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.h:1714
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:3141
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:1297
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:6290
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1312
~Item_sum_json_array() override
bool add() override
Definition: item_sum.cc:6368
unique_ptr_destroy_only< Json_array > m_json_array
Accumulates the final value.
Definition: item_sum.h:1299
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6417
const char * func_name() const override
Definition: item_sum.h:1311
void clear() override
Definition: item_sum.cc:6308
Implements aggregation of values into an object.
Definition: item_sum.h:1319
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:6317
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6525
bool add() override
Definition: item_sum.cc:6430
unique_ptr_destroy_only< Json_object > m_json_object
Accumulates the final value.
Definition: item_sum.h:1321
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1349
~Item_sum_json_object() override
const char * func_name() const override
Definition: item_sum.h:1348
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:6344
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:1330
void clear() override
Definition: item_sum.cc:6334
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:1338
String m_tmp_key_value
Buffer used to get the value of the key.
Definition: item_sum.h:1323
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:6250
void update_field() override
Definition: item_sum.cc:6270
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:6121
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:6234
type_conversion_status save_in_field_inner(Field *field, bool no_conversions) override
Helper function which does all of the work for save_in_field(Field*, bool), except some error checkin...
Definition: item_sum.cc:6149
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:6192
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:6225
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:6065
~Item_sum_json() override
String * val_str(String *str) override
Definition: item_sum.cc:6102
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:6207
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:6243
bool fix_fields(THD *thd, Item **pItem) override
Definition: item_sum.cc:6039
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:6034
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:6081
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:6022
double val_real() override
Definition: item_sum.cc:6177
Definition: item_sum.h:1759
Item_sum_max(Item *item_par)
Definition: item_sum.h:1761
Item_sum_max(THD *thd, const Item_sum_max *item)
Definition: item_sum.h:1764
const char * func_name() const override
Definition: item_sum.h:1767
Item_sum_max * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3200
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1766
Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1762
Definition: item_sum.h:1745
Item_sum_min(THD *thd, const Item_sum_min *item)
Definition: item_sum.h:1750
Item_sum_min(Item *item_par)
Definition: item_sum.h:1747
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1752
const char * func_name() const override
Definition: item_sum.h:1753
Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1748
Item_sum_min * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3196
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:3396
Item_sum_num(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:963
Definition: item_sum.h:1938
Item_sum_or(THD *thd, Item_sum_or *item)
Definition: item_sum.h:1943
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3371
const char * func_name() const override
Definition: item_sum.h:1944
Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1940
Definition: item_sum.h:1550
double val_real() override
Definition: item_sum.cc:2508
Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1552
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2517
Item_sum_std(THD *thd, Item_sum_std *item)
Definition: item_sum.h:1555
const char * func_name() const override
Definition: item_sum.h:1559
enum Item_result result_type() const override
Definition: item_sum.h:1563
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1556
Item * result_item(Field *) override
Definition: item_sum.h:1558
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:2027
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:1959
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:2049
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:1978
void reset_field() override
Definition: item_sum.cc:3527
String * val_str(String *str) override
Definition: item_sum.cc:2138
longlong val_int() override
Definition: item_sum.cc:2070
my_decimal dec_buffs[2]
Definition: item_sum.h:1033
void clear() override
Definition: item_sum.cc:1966
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:3621
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:2144
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1980
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1065
double val_real() override
Definition: item_sum.cc:2094
Definition: item_sum.h:2112
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:2127
longlong val_int() override
Definition: item_sum.cc:4152
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:2123
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4154
Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2114
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4163
double val_real() override
Definition: item_sum.cc:4150
enum Item_result result_type() const override
Definition: item_sum.h:2130
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2126
String * val_str(String *) override
Definition: item_sum.cc:4146
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
Definition: item_sum.h:2117
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2131
Definition: item_sum.h:2017
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2037
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:2034
longlong val_int() override
Definition: item_sum.h:2023
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4142
String * val_str(String *str) override
Definition: item_sum.cc:4138
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4125
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:2030
double val_real() override
Definition: item_sum.cc:4130
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
Definition: item_sum.h:2021
Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2019
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2033
Definition: item_sum.h:2045
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2061
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
Definition: item_sum.h:2049
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2066
enum Item_result result_type() const override
Definition: item_sum.h:2065
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:2058
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4167
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:2062
String * val_str(String *str) override
Definition: item_sum.cc:4179
Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2047
longlong val_int() override
Definition: item_sum.cc:4171
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4183
double val_real() override
Definition: item_sum.h:2052
Definition: item_sum.h:2073
bool resolve_type(THD *) override
Default max_length is max argument length.
Definition: item_sum.cc:4189
double val_real() override
Definition: item_sum.h:2080
String * val_str(String *) override
Definition: item_sum.cc:4205
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2103
enum Item_result result_type() const override
Definition: item_sum.h:2107
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:2100
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4197
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:4201
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
Definition: item_sum.h:2077
longlong val_int() override
Definition: item_sum.h:2089
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:2104
Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2075
Definition: item_sum.h:1474
ulonglong count
Definition: item_sum.h:1485
void update_field() override
Definition: item_sum.cc:2848
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2716
double val_real() override
Definition: item_sum.cc:2793
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1521
void clear() override
Definition: item_sum.cc:2770
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2736
Item * result_item(Field *) override
Definition: item_sum.h:1511
Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1495
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1504
double recurrence_s
Definition: item_sum.h:1483
bool optimize
If set, uses a algorithm II mentioned in the class description to calculate the variance which helps ...
Definition: item_sum.h:1493
const char * func_name() const override
Definition: item_sum.h:1515
bool add() override
Definition: item_sum.cc:2772
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1514
double recurrence_s2
Definition: item_sum.h:1484
Item_result hybrid_type
Definition: item_sum.h:1478
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:2700
double recurrence_m
Used in recurrence relation.
Definition: item_sum.h:1482
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2823
uint prec_increment
Definition: item_sum.h:1487
enum Item_result result_type() const override
Definition: item_sum.h:1520
void reset_field() override
Definition: item_sum.cc:2828
uint sample
Definition: item_sum.h:1486
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:2749
Definition: item_sum.h:1958
const char * func_name() const override
Definition: item_sum.h:1966
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3378
Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1960
Item_sum_xor(THD *thd, Item_sum_xor *item)
Definition: item_sum.h:1965
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:1974
uint64 hash() override
Generate hash unique to an item depending on its attributes.
Definition: item_sum.cc:4117
udf_handler udf
Definition: item_sum.h:1978
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:4076
void update_field() override
Definition: item_sum.h:2010
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:4097
void reset_field() override
Definition: item_sum.h:2009
bool add() override
Definition: item_sum.cc:4091
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.h:1995
Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1981
void clear() override
Definition: item_sum.cc:4085
~Item_udf_sum() override
Definition: item_sum.h:1989
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:4106
Item_udf_sum(THD *thd, Item_udf_sum *item)
Definition: item_sum.h:1985
Item_sum super
Definition: item_sum.h:1975
const char * func_name() const override
Definition: item_sum.h:1994
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2005
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:935
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:3680
void set_nullable(bool nullable)
Definition: item.h:3792
bool get_datetime_from_numeric(Datetime_val *dt, my_time_flags_t flags)
Convert a numeric type to datetime.
Definition: item.cc:1717
DTCollation collation
Character set and collation properties assigned for this Item.
Definition: item.h:3687
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:1519
my_decimal * val_decimal_from_string(my_decimal *decimal_value)
Definition: item.cc:390
bool is_nullable() const
Definition: item.h:3791
void set_data_type_geometry()
Set the data type of the Item to be GEOMETRY.
Definition: item.h:1761
bool get_time_from_decimal(Time_val *time)
Convert val_decimal() to time.
Definition: item.cc:1789
void set_data_type_double()
Set the data type of the Item to be double precision floating point.
Definition: item.h:1585
bool get_time_from_numeric(Time_val *time)
Convert a numeric type to time.
Definition: item.cc:1819
Item_name_string item_name
Name from query.
Definition: item.h:3688
bool fixed
True if item has been resolved.
Definition: item.h:3780
virtual Item_result result_type() const
Definition: item.h:1457
bool null_value
True if item is null.
Definition: item.h:3817
Type
Definition: item.h:970
@ SUM_FUNC_ITEM
A grouped aggregate function, or window function.
Definition: item.h:974
@ AGGR_FIELD_ITEM
A special field for certain aggregate operations.
Definition: item.h:975
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:1828
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:1796
bool unsigned_flag
Definition: item.h:3818
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:3597
bool get_time_from_real(Time_val *time)
Convert val_real() to time.
Definition: item.cc:1783
virtual bool mark_field_in_map(uchar *arg)
Mark underlying field in read or write map of a table.
Definition: item.h:2930
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:3828
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:1805
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:1701
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:1561
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:8058
bool get_time_from_string(Time_val *time)
Convert val_str() to time.
Definition: item.cc:1775
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:1198
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:4884
void free_handler()
Definition: item_func.cc:4865
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:56
@ MYSQL_TYPE_LONGLONG
Definition: field_types.h:65
@ MYSQL_TYPE_NEWDECIMAL
Definition: field_types.h:82
@ MYSQL_TYPE_DOUBLE
Definition: field_types.h:62
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:4320
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:4241
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:4277
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:1597
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: applier_version.h:27
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:2728
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
type_conversion_status
Status when storing a value in a field or converting from one datatype to another.
Definition: field.h:200
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...