MySQL 9.5.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, 2025, 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);
653
654 Item *set_arg(THD *thd, uint i, Item *new_val) override;
655 /// @todo delete this when we no longer support temporary transformations
656 Item **get_arg_ptr(uint i) { return &args[i]; }
657
658 bool fix_fields(THD *thd, Item **ref) override;
659
660 /**
661 Signal to the function that its arguments may have changed,
662 and that any internal caches etc. based on those arguments
663 must be updated accordingly.
664
665 This is used by the hypergraph optimizer when it rewrites
666 arguments to window functions to take into account that they
667 have been materialized into temporary tables, or that they
668 should read their values from the framebuffer.
669 */
671
672 /**
673 Called to initialize the aggregator.
674 */
675
676 virtual bool aggregator_setup(THD *thd) { return aggr->setup(thd); }
677
678 /**
679 Called to cleanup the aggregator.
680 */
681
682 inline void aggregator_clear() { aggr->clear(); }
683
684 /**
685 Called to add value to the aggregator.
686 */
687
688 inline bool aggregator_add() { return aggr->add(); }
689
690 /* stores the declared DISTINCT flag (from the parser) */
691 void set_distinct(bool distinct) {
692 with_distinct = distinct;
694 }
695
696 /*
697 Set the type of aggregation : DISTINCT or not.
698
699 May be called multiple times.
700 */
701
702 virtual int set_aggregator(Aggregator::Aggregator_type aggregator);
703
704 virtual void clear() = 0;
705 virtual bool add() = 0;
706 virtual bool setup(THD *) { return false; }
707
708 /**
709 Only relevant for aggregates qua window functions. Checks semantics after
710 windows have been set up and checked. Window functions have specific
711 requirements on the window specifications. Used at resolution time.
712
713 @param thd Current thread
714 @param select The current select
715 @param [out] reqs Holds collected requirements from this wf
716
717 @returns true if error
718 */
719 virtual bool check_wf_semantics1(THD *thd, Query_block *select,
721
722 /**
723 Like check_wf_semantics1.
724 For checks which cannot be done in resolution phase (mostly those for
725 input parameters which can be '?' and must be >=0: value isn't known
726 before execution phase).
727 */
729 [[maybe_unused]]) {
730 return false;
731 }
732
733 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
734 mem_root_deque<Item *> *fields) override;
735
736 void cleanup() override;
737
738 Window *window() { return m_window; }
739 const Window *window() const { return m_window; }
740 bool reset_wf_state(uchar *arg) override;
741
742 /**
743 All aggregates are framing, i.e. they work on the window's frame. If none
744 is defined, the frame is by default the entire partition, unless ORDER BY
745 is defined, in which case it is the set of rows from the start of the
746 partition to and including the peer set of the current row.
747
748 Some window functions are not framing, i.e. they always work on the entire
749 partition. For such window functions, the method is overridden to
750 return false.
751 */
752 virtual bool framing() const { return true; }
753
754 /**
755 Only for framing window functions. True if this function only needs to
756 read one row per frame.
757 */
758 virtual bool uses_only_one_row() const { return false; }
759
760 /**
761 Return true if we need to make two passes over the rows in the partition -
762 either because we need the cardinality of it (and we need to read all
763 rows to detect the next partition), or we need to have all partition rows
764 available to evaluate the window function for some other reason, e.g.
765 we may need the last row in the partition in the frame buffer to be able
766 to evaluate LEAD.
767 */
768 virtual bool needs_partition_cardinality() const { return false; }
769
770 /**
771 Common initial actions for window functions. For non-buffered processing
772 ("on-the-fly"), check partition change and possible reset partition
773 state. In this case return false.
774 For buffered processing, if windowing state m_do_copy_null is true, set
775 null_value to is_nullable() and return true.
776
777 @return true if case two above holds, else false
778 */
779 bool wf_common_init();
780
781 /// Overridden by Item_rollup_sum_switcher.
782 virtual bool is_rollup_sum_wrapper() const { return false; }
783 /**
784 * In case we are an Item_rollup_sum_switcher,
785 * return the underlying Item_sum, otherwise, return this.
786 * Overridden by Item_rollup_sum_switcher.
787 */
788 virtual const Item_sum *unwrap_sum() const { return this; }
789 /// Non-const version
790 virtual Item_sum *unwrap_sum() { return this; }
791
792 protected:
793 /*
794 Raise an error (ER_NOT_SUPPORTED_YET) with the detail that this
795 function is not yet supported as a window function.
796 */
798 char buff[STRING_BUFFER_USUAL_SIZE];
799 snprintf(buff, sizeof(buff), "%s as window function", func_name());
800 my_error(ER_NOT_SUPPORTED_YET, MYF(0), buff);
801 }
802
803 void add_json_info(Json_object *obj) override {
804 obj->add_alias("distinct", create_dom_ptr<Json_boolean>(with_distinct));
805 }
806};
807
808class Unique;
809
810/**
811 The distinct aggregator.
812 Implements AGGFN (DISTINCT ..)
813 Collects all the data into an Unique (similarly to what Item_sum_distinct
814 does currently) and then (if applicable) iterates over the list of
815 unique values and pumps them back into its object
816*/
817
819 friend class Item_sum_sum;
820
821 /*
822 flag to prevent consecutive runs of endup(). Normally in endup there are
823 expensive calculations (like walking the distinct tree for example)
824 which we must do only once if there are no data changes.
825 We can re-use the data for the second and subsequent val_xxx() calls.
826 endup_done set to true also means that the calculated values for
827 the aggregate functions are correct and don't need recalculation.
828 */
830
831 /*
832 Used depending on the type of the aggregate function and the presence of
833 blob columns in it:
834 - For COUNT(DISTINCT) and no blob fields this points to a real temporary
835 table. It's used as a hash table.
836 - For AVG/SUM(DISTINCT) or COUNT(DISTINCT) with blob fields only the
837 in-memory data structure of a temporary table is constructed.
838 It's used by the Field classes to transform data into row format.
839 */
841
842 /*
843 An array of field lengths on row allocated and used only for
844 COUNT(DISTINCT) with multiple columns and no blobs. Used in
845 Aggregator_distinct::composite_key_cmp (called from Unique to compare
846 nodes
847 */
849
850 /*
851 Used in conjunction with 'table' to support the access to Field classes
852 for COUNT(DISTINCT). Needed by copy_fields()/copy_funcs().
853 */
855
856 /*
857 If there are no blobs in the COUNT(DISTINCT) arguments, we can use a tree,
858 which is faster than heap table. In that case, we still use the table
859 to help get things set up, but we insert nothing in it.
860 For AVG/SUM(DISTINCT) we always use this tree (as it takes a single
861 argument) to get the distinct rows.
862 */
864
865 /*
866 The length of the temp table row. Must be a member of the class as it
867 gets passed down to simple_raw_key_cmp () as a compare function argument
868 to Unique. simple_raw_key_cmp () is used as a fast comparison function
869 when the entire row can be binary compared.
870 */
872
875 /**
876 Set to true if the result is known to be always NULL.
877 If set deactivates creation and usage of the temporary table (in the
878 'table' member) and the Unique instance (in the 'tree' member) as well as
879 the calculation of the final value on the first call to
880 @c Item_sum::val_xxx(),
881 @c Item_avg::val_xxx(),
882 @c Item_count::val_xxx().
883 */
885 /**
886 Set to true if count distinct is on only const items. Distinct on a const
887 value will always be the constant itself. And count distinct of the same
888 would always be 1. Similar to CONST_NULL, it avoids creation of temporary
889 table and the Unique instance.
890 */
893
894 /**
895 When feeding back the data in endup() from Unique/temp table back to
896 Item_sum::add() methods we must read the data from Unique (and not
897 recalculate the functions that are given as arguments to the aggregate
898 function.
899 This flag is to tell the arg_*() methods to take the data from the Unique
900 instead of calling the relevant val_..() method.
901 */
903
904 public:
906 : Aggregator(sum),
907 table(nullptr),
909 tree(nullptr),
911 use_distinct_values(false) {}
912 ~Aggregator_distinct() override;
914
915 bool setup(THD *) override;
916 void clear() override;
917 bool add() override;
918 void endup() override;
920 double arg_val_real() override;
921 bool arg_is_null(bool use_null_value) override;
922
923 bool unique_walk_function(void *element);
924 static int composite_key_cmp(const void *arg, const void *a, const void *b);
925};
926
927/**
928 The pass-through aggregator.
929 Implements AGGFN (DISTINCT ..) by knowing it gets distinct data on input.
930 So it just pumps them back to the Item_sum descendant class.
931*/
933 public:
936
937 bool setup(THD *thd) override { return item_sum->setup(thd); }
938 void clear() override { item_sum->clear(); }
939 bool add() override { return item_sum->add(); }
940 void endup() override {}
942 double arg_val_real() override;
943 bool arg_is_null(bool use_null_value) override;
944};
945
946class Item_sum_num : public Item_sum {
948
949 protected:
950 /*
951 val_xxx() functions may be called several times during the execution of a
952 query. Derived classes that require extensive calculation in val_xxx()
953 maintain cache of aggregate value. This variable governs the validity of
954 that cache.
955 */
957
958 public:
959 Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
960 : Item_sum(pos, item_par, window), is_evaluated(false) {}
961
963 : Item_sum(pos, list, w), is_evaluated(false) {}
964
966 : Item_sum(thd, item), is_evaluated(item->is_evaluated) {}
968 return MYSQL_TYPE_DOUBLE;
969 }
970
971 Item_sum_num(Item *item_par) : Item_sum(item_par), is_evaluated(false) {}
972
973 bool fix_fields(THD *, Item **) override;
974 longlong val_int() override {
975 assert(fixed);
976 return llrint_with_overflow_check(val_real()); /* Real as default */
977 }
978 String *val_str(String *str) override;
979 my_decimal *val_decimal(my_decimal *) override;
980 bool val_date(Date_val *date, my_time_flags_t flags) override {
981 return get_date_from_numeric(date, flags); /* Decimal or real */
982 }
983 bool val_time(Time_val *time) override {
984 return get_time_from_numeric(time); /* Decimal or real */
985 }
988 }
989 void reset_field() override;
990};
991
993 public:
994 Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
995 : Item_sum_num(pos, item_par, w) {
997 }
998
1000 : Item_sum_num(pos, list, w) {
1002 }
1003
1004 Item_sum_int(THD *thd, Item_sum_int *item) : Item_sum_num(thd, item) {
1006 }
1007
1008 Item_sum_int(Item *item_par) : Item_sum_num(item_par) {
1010 }
1011
1012 double val_real() override {
1013 assert(fixed);
1014 return static_cast<double>(val_int());
1015 }
1016 String *val_str(String *str) override;
1017 my_decimal *val_decimal(my_decimal *) override;
1018 bool val_date(Date_val *date, my_time_flags_t flags) override {
1019 return get_date_from_int(date, flags);
1020 }
1021 bool val_time(Time_val *time) override { return get_time_from_int(time); }
1023 return get_datetime_from_int(dt, flags);
1024 }
1025 enum Item_result result_type() const override { return INT_RESULT; }
1026};
1027
1029 protected:
1031 double sum;
1034 bool resolve_type(THD *thd) override;
1035 /**
1036 Execution state: this is for counting rows entering and leaving the window
1037 frame, see #m_frame_null_count.
1038 */
1040
1041 /**
1042 Execution state: this is for counting NULLs of rows entering and leaving
1043 the window frame, when we use optimized inverse-based computations. By
1044 comparison with m_count we can know how many non-NULLs are in the frame.
1045 */
1047
1048 public:
1049 Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
1050 : Item_sum_num(pos, item_par, window),
1052 m_count(0),
1054 set_distinct(distinct);
1055 }
1056
1058 : Item_sum_num(item_par),
1060 m_count(0),
1061 m_frame_null_count(0) {}
1062
1063 Item_sum_sum(THD *thd, Item_sum_sum *item);
1064 enum Sumfunctype sum_func() const override {
1066 }
1067 void clear() override;
1068 bool add() override;
1069 double val_real() override;
1070 longlong val_int() override;
1071 String *val_str(String *str) override;
1072 my_decimal *val_decimal(my_decimal *) override;
1073 enum Item_result result_type() const override { return hybrid_type; }
1074 bool check_wf_semantics1(THD *thd, Query_block *select,
1075 Window_evaluation_requirements *reqs) override;
1076 void no_rows_in_result() override;
1077 void reset_field() override;
1078 void update_field() override;
1079 const char *func_name() const override { return "sum"; }
1080 Item *copy_or_same(THD *thd) override;
1081};
1082
1085
1087
1088 void clear() override;
1089 bool add() override;
1090 void cleanup() override;
1091
1092 public:
1093 Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
1094 : Item_sum_int(pos, item_par, w), count(0) {}
1095 Item_sum_count(Item_int *number) : Item_sum_int(number), count(0) {}
1096 Item_sum_count(Item *item, bool distinct) : Item_sum_int(item), count(0) {
1097 set_distinct(distinct);
1098 }
1099 /**
1100 Constructs an instance for COUNT(DISTINCT)
1101
1102 @param pos Position of token in the parser.
1103 @param list A list of the arguments to the aggregate function
1104 @param w A window, if COUNT is used as a windowing function
1105
1106 This constructor is called by the parser only for COUNT (DISTINCT).
1107 */
1109 : Item_sum_int(pos, list, w), count(0) {
1110 set_distinct(true);
1111 }
1113 : Item_sum_int(thd, item), count(item->count) {}
1114 enum Sumfunctype sum_func() const override {
1116 }
1117 bool resolve_type(THD *thd) override {
1118 if (param_type_is_default(thd, 0, -1)) return true;
1119 set_nullable(false);
1120 null_value = false;
1121 return false;
1122 }
1123 void no_rows_in_result() override { count = 0; }
1124 void make_const(longlong count_arg) {
1125 count = count_arg;
1127 }
1128 longlong val_int() override;
1129 void reset_field() override;
1130 void update_field() override;
1131 const char *func_name() const override { return "count"; }
1132 Item *copy_or_same(THD *thd) override;
1133};
1134
1135/* Item to get the value of a stored sum function */
1136
1137class Item_sum_avg;
1138class Item_sum_bit;
1139
1140/**
1141 This is used in connection with a parent aggregate Item:
1142 - which stores function's value into a temporary table's column (one row
1143 per group).
1144 - except when the output is a local variable in a stored procedure, in which
1145 case the variable is used as the target.
1146 - which stores in the column some internal piece of information which should
1147 not be returned to the user, so special implementations are needed.
1148 The classes that inherit from Item_aggregate_field are created during
1149 optimization and resolved upon construction, thus the fix_fields() function
1150 is not needed and thus not implemented. The resolve_type() needs a default
1151 implementation since it is a virtual function.
1152*/
1154 protected:
1155 /// The tmp table's column containing the value of the set function.
1156 Field *m_field{nullptr};
1157 /// Stores the Item's result type.
1159
1160 public:
1161 enum Item_result result_type() const override { return m_result_type; }
1162 // resolve_type is not used for these classes, but is needed bc it is virtual
1163 bool resolve_type(THD *) override { return false; }
1164 bool mark_field_in_map(uchar *arg) override {
1165 /*
1166 Filesort (find_all_keys) over a temporary table collects the columns it
1167 needs.
1168 */
1169 return Item::mark_field_in_map(pointer_cast<Mark_field *>(arg), m_field);
1170 }
1173 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1174 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1175 return true;
1176 }
1177};
1178
1179/**
1180 Common abstract class for aggregate field classes that return numeric values:
1181 Item_aggr_avg_field
1182 Item_aggr_variance_field
1183*/
1185 public:
1186 longlong val_int() override {
1187 /* can't be fix_fields()ed */
1189 }
1190 bool val_date(Date_val *date, my_time_flags_t flags) override {
1191 return get_date_from_numeric(date, flags); /* Decimal or real */
1192 }
1193 bool val_time(Time_val *time) override {
1194 return get_time_from_numeric(time); /* Decimal or real */
1195 }
1197 return get_datetime_from_numeric(dt, flags);
1198 }
1199 bool is_null() override { return update_null_value() || null_value; }
1200};
1201
1203 public:
1205 enum Type type() const override { return AGGR_FIELD_ITEM; }
1206 double val_real() override;
1207 my_decimal *val_decimal(my_decimal *) override;
1208 String *val_str(String *) override;
1209
1210 private:
1215};
1216
1217/// This is used in connection with an Item_sum_bit, @see Item_aggregate_field
1219 public:
1220 Item_aggr_bit_field(Item_sum_bit *item, ulonglong reset_bits);
1221 longlong val_int() override;
1222 double val_real() override;
1223 my_decimal *val_decimal(my_decimal *) override;
1224 String *val_str(String *) override;
1225 bool val_date(Date_val *date, my_time_flags_t flags) override;
1226 bool val_time(Time_val *time) override;
1227 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
1228 enum Type type() const override { return AGGR_FIELD_ITEM; }
1229
1230 private:
1232};
1233
1235
1236/// Common abstraction for Item_sum_json_array and Item_sum_json_object
1237class Item_sum_json : public Item_sum {
1239
1240 protected:
1241 /// String used when reading JSON binary values or JSON text values.
1243 /// String used for converting JSON text values to utf8mb4 charset.
1245 /// Wrapper around the container (object/array) which accumulates the value.
1247 /// JSON constructor null clause
1250
1251 /**
1252 Construct an Item_sum_json instance.
1253
1254 @param wrapper a wrapper around the Json_array or Json_object that contains
1255 the aggregated result
1256 @param json_constructor_null_clause Specifies the behavior for
1257 handling NULL values in JSON
1258 constructors
1259 i.e, NULL_ON_NULL and ABSENT_ON_NULL
1260 @param parent_args arguments to forward to Item_sum's constructor
1261 */
1262 template <typename... Args>
1263 explicit Item_sum_json(
1265 Json_constructor_null_clause json_constructor_null_clause,
1266 Args &&...parent_args);
1267
1268 public:
1269 ~Item_sum_json() override;
1270 bool fix_fields(THD *thd, Item **pItem) override;
1271 Item_result result_type() const override { return STRING_RESULT; }
1272
1273 bool do_itemize(Parse_context *pc, Item **res) override;
1274
1275 double val_real() override;
1276 longlong val_int() override;
1277 String *val_str(String *str) override;
1278 bool val_json(Json_wrapper *wr) override;
1279 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
1280 bool val_date(Date_val *date, my_time_flags_t flags) override;
1281 bool val_time(Time_val *time) override;
1282 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
1283 void reset_field() override;
1284 void update_field() override;
1285
1286 void print(const THD *thd, String *str,
1287 enum_query_type query_type) const override;
1288
1291};
1292
1293/// Implements aggregation of values into an array.
1295 /// Accumulates the final value.
1297
1298 public:
1299 Item_sum_json_array(THD *thd, Item_sum *item,
1302 Item_sum_json_array(const POS &pos, Item *a,
1303 Json_constructor_null_clause json_constructor_null_clause,
1304 PT_window *w,
1308 const char *func_name() const override { return "json_arrayagg"; }
1309 enum Sumfunctype sum_func() const override { return JSON_ARRAYAGG_FUNC; }
1310 void clear() override;
1311 bool add() override;
1312 Item *copy_or_same(THD *thd) override;
1313};
1314
1315/// Implements aggregation of values into an object.
1317 /// Accumulates the final value.
1319 /// Buffer used to get the value of the key.
1321 /**
1322 Map of keys in Json_object and the count for each key
1323 within a window frame. It is used in handling rows
1324 leaving a window frame when rows are not sorted
1325 according to the key in Json_object.
1326 */
1327 std::map<std::string, int> m_key_map;
1328 /**
1329 If window provides ordering on the key in Json_object,
1330 a key_map is not needed to handle rows leaving a window
1331 frame. In this case, process_buffered_windowing_record()
1332 will set flags when a key/value pair can be removed from
1333 the Json_object.
1334 */
1335 bool m_optimize{false};
1336
1337 public:
1338 Item_sum_json_object(THD *thd, Item_sum *item,
1341 Item_sum_json_object(const POS &pos, Item *a, Item *b, PT_window *w,
1345 const char *func_name() const override { return "json_objectagg"; }
1346 enum Sumfunctype sum_func() const override { return JSON_OBJECTAGG_FUNC; }
1347 void clear() override;
1348 bool add() override;
1349 Item *copy_or_same(THD *thd) override;
1350 bool check_wf_semantics1(THD *thd, Query_block *select,
1351 Window_evaluation_requirements *reqs) override;
1352};
1353
1354class Item_sum_avg final : public Item_sum_sum {
1355 public:
1360 double m_avg;
1361
1362 Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
1363 : Item_sum_sum(pos, item_par, distinct, w) {}
1364
1366 : Item_sum_sum(thd, item), prec_increment(item->prec_increment) {}
1367
1368 bool resolve_type(THD *thd) override;
1369 enum Sumfunctype sum_func() const override {
1371 }
1372 void clear() override;
1373 bool add() override;
1374 double val_real() override;
1375 // In SPs we might force the "wrong" type with select into a declare variable
1377 my_decimal *val_decimal(my_decimal *) override;
1378 String *val_str(String *str) override;
1379 void reset_field() override;
1380 void update_field() override;
1381 Item *result_item(Field *) override { return new Item_aggr_avg_field(this); }
1382 const char *func_name() const override { return "avg"; }
1383 Item *copy_or_same(THD *thd) override;
1384 Field *create_tmp_field(bool group, TABLE *table) override;
1385 void cleanup() override {
1386 m_count = 0;
1389 }
1390};
1391
1392class Item_sum_variance;
1393
1395 public:
1397 enum Type type() const override { return AGGR_FIELD_ITEM; }
1398 double val_real() override;
1400 my_decimal *val_decimal(my_decimal *dec_buf) override {
1401 return val_decimal_from_real(dec_buf);
1402 }
1405 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1406 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1407 return true;
1408 }
1409
1410 private:
1412};
1413
1414/*
1415 variance(a) =
1416
1417 = sum (ai - avg(a))^2 / count(a) )
1418 = sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
1419 = (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
1420 = (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
1421 = (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2
1422 )/count(a) = = (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a)
1423 )/count(a) = = (sum(ai^2) - sum(a)^2/count(a))/count(a)
1424
1425 But, this falls prey to catastrophic cancellation.
1426 Instead, we use recurrence formulas in Algorithm I mentoned below
1427 for group aggregates.
1428
1429 Algorithm I:
1430 M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
1431 S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
1432 for 2 <= k <= n newline
1433 ital variance = S_{n} / (n-1)
1434
1435 For aggregate window functions algorithm I cannot be optimized for
1436 moving frames since M_{i} changes for every row. So we use the
1437 following algorithm.
1438
1439 Algorithm II:
1440
1441 K = 0
1442 n = 0
1443 ex = 0
1444 ex2 = 0
1445
1446 def add_sample(x):
1447 if (n == 0):
1448 K = x
1449 n = n + 1
1450 ex += x - K
1451 ex2 += (x - K) * (x - K)
1452
1453 def remove_sample(x):
1454 n = n - 1
1455 ex -= (x - K)
1456 ex2 -= (x - K) * (x - K)
1457
1458 def variance():
1459 return (ex2 - (ex*ex)/n) / (n-1)
1460
1461 This formula facilitates incremental computation enabling us to
1462 optimize in case of moving window frames. The optimized codepath is taken
1463 only when windowing_use_high_precision is set to false. By default,
1464 aggregate window functions take the non-optimized codepath.
1465 Note:
1466 Results could differ between optimized and non-optimized code path.
1467 Hence algorithm II is used only when user sets
1468 windowing_use_high_precision to false.
1469*/
1470
1472 bool resolve_type(THD *) override;
1473
1474 public:
1476 /**
1477 Used in recurrence relation.
1478 */
1479 double recurrence_m{0.0};
1480 double recurrence_s{0.0};
1481 double recurrence_s2{0.0};
1485 /**
1486 If set, uses a algorithm II mentioned in the class description
1487 to calculate the variance which helps in optimizing windowing
1488 functions in presence of frames.
1489 */
1491
1492 Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg,
1493 PT_window *w)
1494 : Item_sum_num(pos, item_par, w),
1496 count(0),
1497 sample(sample_arg),
1498 optimize(false) {}
1499
1501 enum Sumfunctype sum_func() const override { return VARIANCE_FUNC; }
1502 void clear() override;
1503 bool add() override;
1504 double val_real() override;
1505 my_decimal *val_decimal(my_decimal *) override;
1506 void reset_field() override;
1507 void update_field() override;
1508 Item *result_item(Field *) override {
1509 return new Item_aggr_variance_field(this);
1510 }
1511 void no_rows_in_result() override {}
1512 const char *func_name() const override {
1513 return sample ? "var_samp" : "variance";
1514 }
1515 Item *copy_or_same(THD *thd) override;
1516 Field *create_tmp_field(bool group, TABLE *table) override;
1517 enum Item_result result_type() const override { return REAL_RESULT; }
1518 void cleanup() override {
1519 count = 0;
1521 }
1522 bool check_wf_semantics1(THD *thd, Query_block *select,
1523 Window_evaluation_requirements *reqs) override;
1524};
1525
1526class Item_sum_std;
1527
1529 public:
1531 enum Type type() const override { return AGGR_FIELD_ITEM; }
1532 double val_real() override;
1533 my_decimal *val_decimal(my_decimal *) override;
1534 enum Item_result result_type() const override { return REAL_RESULT; }
1537 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1538 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1539 return true;
1540 }
1541};
1542
1543/*
1544 standard_deviation(a) = sqrt(variance(a))
1545*/
1546
1548 public:
1549 Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
1550 : Item_sum_variance(pos, item_par, sample_arg, w) {}
1551
1552 Item_sum_std(THD *thd, Item_sum_std *item) : Item_sum_variance(thd, item) {}
1553 enum Sumfunctype sum_func() const override { return STD_FUNC; }
1554 double val_real() override;
1555 Item *result_item(Field *) override { return new Item_aggr_std_field(this); }
1556 const char *func_name() const override {
1557 return sample ? "stddev_samp" : "std";
1558 }
1559 Item *copy_or_same(THD *thd) override;
1560 enum Item_result result_type() const override { return REAL_RESULT; }
1561};
1562
1563// This class is a string or number function depending on num_func
1564class Arg_comparator;
1565
1566/**
1567 Abstract base class for the MIN and MAX aggregate functions.
1568*/
1571
1572 private:
1573 /**
1574 Tells if this is the MIN function (true) or the MAX function (false).
1575 */
1576 const bool m_is_min;
1577 /*
1578 For window functions MIN/MAX with optimized code path, no comparisons
1579 are needed beyond NULL detection: MIN/MAX are then roughly equivalent to
1580 FIRST/LAST_VALUE. For this case, 'value' is the value of
1581 the window function a priori taken from args[0], while arg_cache is used to
1582 remember the value from the previous row. NULLs need a bit of careful
1583 treatment.
1584 */
1588 bool m_has_values; // Set if at least one row is found (for max/min only)
1589 /**
1590 Set to true if the window is ordered ascending.
1591 */
1593 /**
1594 Set to true when min/max can be optimized using window's ordering.
1595 */
1597 /**
1598 For min() - Set to true when results are ordered in ascending and
1599 false when descending.
1600 For max() - Set to true when results are ordered in descending and
1601 false when ascending.
1602 Valid only when m_optimize is true.
1603 */
1604 bool m_want_first; ///< Want first non-null value, else last non_null value
1605 /**
1606 Execution state: keeps track if this is the first row in the frame
1607 when buffering is not needed.
1608 Valid only when m_optimize is true.
1609 */
1611
1612 /**
1613 Execution state: keeps track of at which row we saved a non-null last
1614 value.
1615 */
1617
1618 /**
1619 This function implements the optimized version of retrieving min/max
1620 value. When we have "ordered ASC" results in a window, min will always
1621 be the first value in the result set (neglecting the NULL's) and max
1622 will always be the last value (or the other way around, if ordered DESC).
1623 It is based on the implementation of FIRST_VALUE/LAST_VALUE, except for
1624 the NULL handling.
1625
1626 @return true if computation yielded a NULL or error
1627 */
1628 bool compute();
1629
1630 /**
1631 MIN/MAX function setup.
1632
1633 Setup cache/comparator of MIN/MAX functions. When called by the
1634 copy_or_same() function, the value_arg parameter contains the calculated
1635 value of the original MIN/MAX object, and it is saved in this object's
1636 cache.
1637
1638 @param item the argument of the MIN/MAX function
1639 @param value_arg the calculated value of the MIN/MAX function
1640 @return false on success, true on error
1641 */
1642 bool setup_hybrid(Item *item, Item *value_arg);
1643
1644 /** Create a clone of this object. */
1645 virtual Item_sum_hybrid *clone_hybrid(THD *thd) const = 0;
1646
1647 protected:
1648 Item_sum_hybrid(Item *item_par, bool is_min)
1649 : Item_sum(item_par),
1650 m_is_min(is_min),
1651 value(nullptr),
1653 cmp(nullptr),
1655 m_has_values(true),
1656 m_nulls_first(false),
1657 m_optimize(false),
1658 m_want_first(false),
1659 m_cnt(0),
1662 }
1663
1664 Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
1665 : Item_sum(pos, item_par, w),
1666 m_is_min(is_min),
1667 value(nullptr),
1669 cmp(nullptr),
1671 m_has_values(true),
1672 m_nulls_first(false),
1673 m_optimize(false),
1674 m_want_first(false),
1675 m_cnt(0),
1678 }
1679
1681 : Item_sum(thd, item),
1682 m_is_min(item->m_is_min),
1683 value(item->value),
1685 hybrid_type(item->hybrid_type),
1688 m_optimize(item->m_optimize),
1690 m_cnt(item->m_cnt),
1692
1693 public:
1694 bool fix_fields(THD *, Item **) override;
1695 void clear() override;
1696 void update_after_wf_arguments_changed(THD *thd) override;
1697 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
1698 mem_root_deque<Item *> *fields) override;
1699 double val_real() override;
1700 longlong val_int() override;
1701 longlong val_date_temporal() override;
1702 my_decimal *val_decimal(my_decimal *) override;
1703 bool val_date(Date_val *date, my_time_flags_t flags) override;
1704 bool val_time(Time_val *time) override;
1705 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
1706 void reset_field() override;
1707 String *val_str(String *) override;
1708 bool val_json(Json_wrapper *wr) override;
1709 bool keep_field_type() const override { return true; }
1710 enum Item_result result_type() const override { return hybrid_type; }
1711 TYPELIB *get_typelib() const override {
1712 assert(sum_func() == MAX_FUNC || sum_func() == MIN_FUNC);
1713 return arguments()[0]->get_typelib();
1714 }
1715 void update_field() override;
1716 void cleanup() override;
1717 bool has_values() { return m_has_values; }
1718 void no_rows_in_result() override;
1719 Field *create_tmp_field(bool group, TABLE *table) override;
1720 bool uses_only_one_row() const override { return m_optimize; }
1721 bool add() override;
1722 Item *copy_or_same(THD *thd) override;
1723 bool check_wf_semantics1(THD *thd, Query_block *select,
1725
1726 private:
1727 /*
1728 These functions check if the value on the current row exceeds the maximum or
1729 minimum value seen so far, and update the current max/min stored in
1730 result_field, if needed.
1731 */
1739};
1740
1741class Item_sum_min final : public Item_sum_hybrid {
1742 public:
1743 Item_sum_min(Item *item_par) : Item_sum_hybrid(item_par, true) {}
1744 Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
1745 : Item_sum_hybrid(pos, item_par, true, w) {}
1746 Item_sum_min(THD *thd, const Item_sum_min *item)
1747 : Item_sum_hybrid(thd, item) {}
1748 enum Sumfunctype sum_func() const override { return MIN_FUNC; }
1749 const char *func_name() const override { return "min"; }
1750
1751 private:
1752 Item_sum_min *clone_hybrid(THD *thd) const override;
1753};
1754
1755class Item_sum_max final : public Item_sum_hybrid {
1756 public:
1757 Item_sum_max(Item *item_par) : Item_sum_hybrid(item_par, false) {}
1758 Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
1759 : Item_sum_hybrid(pos, item_par, false, w) {}
1760 Item_sum_max(THD *thd, const Item_sum_max *item)
1761 : Item_sum_hybrid(thd, item) {}
1762 enum Sumfunctype sum_func() const override { return MAX_FUNC; }
1763 const char *func_name() const override { return "max"; }
1764
1765 private:
1766 Item_sum_max *clone_hybrid(THD *thd) const override;
1767};
1768
1769/**
1770 Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
1771
1772 Each of them is both a set function and a framing window function.
1773*/
1774class Item_sum_bit : public Item_sum {
1776 /// Stores the neutral element for function
1778 /// Stores the result value for the INT_RESULT
1780 /// Stores the result value for the STRING_RESULT
1782 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1784 /// Buffer used to avoid String allocation in the constructor
1785 const char initial_value_buff_storage[1] = {0};
1786
1787 /**
1788 Execution state (windowing): this is for counting rows entering and leaving
1789 the window frame, see #m_frame_null_count.
1790 */
1792
1793 /**
1794 Execution state (windowing): this is for counting NULLs of rows entering
1795 and leaving the window frame, when we use optimized inverse-based
1796 computations. By comparison with m_count we can know how many non-NULLs are
1797 in the frame.
1798 */
1800
1801 /**
1802 Execution state (windowing): Used for AND, OR to be able to invert window
1803 functions in optimized mode.
1804
1805 For the optimized code path of BIT_XXX wfs, we keep track of the number of
1806 bit values (0's or 1's; see below) seen in a frame using a 64 bits counter
1807 pr bit. This lets us compute the value of OR by just inspecting:
1808
1809 - the number of 1's in the previous frame
1810 - whether any removed row(s) is a 1
1811 - whether any added row(s) is a 1
1812
1813 Similarly for AND, we keep track of the number of 0's seen for a particular
1814 bit. To do this trick we need a counter per bit position. This array holds
1815 these counters.
1816
1817 Note that for XOR, the inverse operation is identical to the operation,
1818 so we don't need the above.
1819 */
1821 /*
1822 Size of allocated array m_digit_cnt.
1823 The size is DIGIT_CNT_CARD (for integer types) or ::max_length * 8 for bit
1824 strings.
1825 */
1827
1828 static constexpr uint DIGIT_CNT_CARD = sizeof(ulonglong) * 8;
1829
1830 protected:
1831 bool m_is_xor; ///< true iff BIT_XOR
1832
1833 public:
1834 Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg,
1835 PT_window *w)
1836 : Item_sum(pos, item_par, w),
1837 reset_bits(reset_arg),
1838 bits(reset_arg),
1840 m_count(0),
1844 m_is_xor(false) {}
1845
1846 /// Copy constructor, used for executing subqueries with temporary tables
1848 : Item_sum(thd, item),
1849 reset_bits(item->reset_bits),
1850 bits(item->bits),
1852 hybrid_type(item->hybrid_type),
1853 m_count(item->m_count),
1857 m_is_xor(item->m_is_xor) {
1858 /*
1859 This constructor should only be called during the Optimize stage.
1860 Asserting that the item was not evaluated yet.
1861 */
1862 assert(item->value_buff.length() == 1);
1863 assert(item->bits == item->reset_bits);
1864 }
1865
1866 Item *result_item(Field *) override {
1867 return new Item_aggr_bit_field(this, reset_bits);
1868 }
1869
1870 enum Sumfunctype sum_func() const override { return SUM_BIT_FUNC; }
1871 enum Item_result result_type() const override { return hybrid_type; }
1872 void clear() override;
1873 longlong val_int() override;
1874 double val_real() override;
1875 String *val_str(String *str) override;
1876 my_decimal *val_decimal(my_decimal *decimal_value) override;
1877 bool val_date(Date_val *date, my_time_flags_t flags) override;
1878 bool val_time(Time_val *time) override;
1879 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
1880 void reset_field() override;
1881 void update_field() override;
1882 bool resolve_type(THD *) override;
1883 bool fix_fields(THD *thd, Item **ref) override;
1884 void cleanup() override {
1885 bits = reset_bits;
1886 // At end of one execution of statement, free buffer to reclaim memory:
1889 }
1890
1891 /**
1892 Common implementation of Item_sum_or::add, Item_sum_and:add
1893 and Item_sum_xor::add.
1894 */
1895 bool add() override;
1896 /// @returns true iff this is BIT_AND.
1897 inline bool is_and() const { return reset_bits != 0; }
1898
1899 private:
1900 /**
1901 Accumulate the value of 's1' (if in string mode) or of 'b1' (if in integer
1902 mode). Updates 'value_buff' or 'bits'.
1903
1904 @param s1 argument to accumulate
1905 @param b1 argument to accumulate
1906
1907 @returns true if error
1908 */
1909 bool add_bits(const String *s1, ulonglong b1);
1910
1911 /**
1912 For windowing: perform inverse aggregation. "De-accumulate" the value of
1913 's1' (if in string mode) or of 'b1' (if in integer mode). Updates
1914 'value_buff' or 'bits'.
1915
1916 For BIT_XOR we simply apply XOR as it's its inverse operation. For BIT_OR
1917 and BIT_AND we do the rest below.
1918
1919 For each bit in argument, decrement the corresponding bits's counter
1920 ('m_digit_cnt') for that bit as follows: for BIT_AND, decrement the
1921 counter if we see a zero in that bit; for BIT_OR decrement the counter if
1922 we see a 1 in that bit. Next, update 'value_buff' or 'bits' using the
1923 resulting counters: for each bit, for BIT_AND, set bit if we have counter
1924 == 0, i.e. we have no zero bits for that bit in the frame (yet). For
1925 BIT_OR, set bit if we have counter > 0, i.e. at least one row in the frame
1926 has that bit set.
1927
1928 @param s1 the bits to be inverted from the aggregate value
1929 @param b1 the bits to be inverted from the aggregate value
1930 */
1931 void remove_bits(const String *s1, ulonglong b1);
1932};
1933
1934class Item_sum_or final : public Item_sum_bit {
1935 public:
1936 Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
1937 : Item_sum_bit(pos, item_par, 0LL, w) {}
1938
1939 Item_sum_or(THD *thd, Item_sum_or *item) : Item_sum_bit(thd, item) {}
1940 const char *func_name() const override { return "bit_or"; }
1941 Item *copy_or_same(THD *thd) override;
1942};
1943
1944class Item_sum_and final : public Item_sum_bit {
1945 public:
1946 Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
1947 : Item_sum_bit(pos, item_par, ULLONG_MAX, w) {}
1948
1949 Item_sum_and(THD *thd, Item_sum_and *item) : Item_sum_bit(thd, item) {}
1950 const char *func_name() const override { return "bit_and"; }
1951 Item *copy_or_same(THD *thd) override;
1952};
1953
1954class Item_sum_xor final : public Item_sum_bit {
1955 public:
1956 Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
1957 : Item_sum_bit(pos, item_par, 0LL, w) {
1958 m_is_xor = true;
1959 }
1960
1961 Item_sum_xor(THD *thd, Item_sum_xor *item) : Item_sum_bit(thd, item) {}
1962 const char *func_name() const override { return "bit_xor"; }
1963 Item *copy_or_same(THD *thd) override;
1964};
1965
1966/*
1967 User defined aggregates
1968*/
1969
1970class Item_udf_sum : public Item_sum {
1972
1973 protected:
1975
1976 public:
1977 Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1978 : Item_sum(pos, opt_list, nullptr), udf(udf_arg) {
1980 }
1982 : Item_sum(thd, item), udf(item->udf) {
1983 udf.m_original = false;
1984 }
1985 ~Item_udf_sum() override {
1987 }
1988
1989 bool do_itemize(Parse_context *pc, Item **res) override;
1990 const char *func_name() const override { return udf.name(); }
1991 bool fix_fields(THD *thd, Item **ref) override {
1992 assert(!fixed);
1993
1994 if (init_sum_func_check(thd)) return true;
1995
1996 fixed = true;
1997 if (udf.fix_fields(thd, this, this->arg_count, this->args)) return true;
1998
1999 return check_sum_func(thd, ref);
2000 }
2001 enum Sumfunctype sum_func() const override { return UDF_SUM_FUNC; }
2002
2003 void clear() override;
2004 bool add() override;
2005 void reset_field() override {}
2006 void update_field() override {}
2007 void cleanup() override;
2008 void print(const THD *thd, String *str,
2009 enum_query_type query_type) const override;
2010 uint64 hash() override;
2011};
2012
2013class Item_sum_udf_float final : public Item_udf_sum {
2014 public:
2015 Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2016 : Item_udf_sum(pos, udf_arg, opt_list) {}
2018 : Item_udf_sum(thd, item) {}
2019 longlong val_int() override {
2020 assert(fixed);
2021 return (longlong)rint(Item_sum_udf_float::val_real());
2022 }
2023 double val_real() override;
2024 String *val_str(String *str) override;
2025 my_decimal *val_decimal(my_decimal *) override;
2026 bool val_date(Date_val *date, my_time_flags_t flags) override {
2027 return get_date_from_real(date, flags);
2028 }
2029 bool val_time(Time_val *time) override { return get_time_from_real(time); }
2031 return get_datetime_from_real(dt, flags);
2032 }
2033 bool resolve_type(THD *) override {
2036 return false;
2037 }
2038 Item *copy_or_same(THD *thd) override;
2039};
2040
2041class Item_sum_udf_int final : public Item_udf_sum {
2042 public:
2043 Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2044 : Item_udf_sum(pos, udf_arg, opt_list) {}
2046 : Item_udf_sum(thd, item) {}
2047 longlong val_int() override;
2048 double val_real() override {
2049 assert(fixed);
2050 return (double)Item_sum_udf_int::val_int();
2051 }
2052 String *val_str(String *str) override;
2053 my_decimal *val_decimal(my_decimal *) override;
2054 bool val_date(Date_val *date, my_time_flags_t flags) override {
2055 return get_date_from_int(date, flags);
2056 }
2057 bool val_time(Time_val *time) override { return get_time_from_int(time); }
2059 return get_datetime_from_int(dt, flags);
2060 }
2061 enum Item_result result_type() const override { return INT_RESULT; }
2062 bool resolve_type(THD *) override {
2064 return false;
2065 }
2066 Item *copy_or_same(THD *thd) override;
2067};
2068
2069class Item_sum_udf_str final : public Item_udf_sum {
2070 public:
2071 Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2072 : Item_udf_sum(pos, udf_arg, opt_list) {}
2074 : Item_udf_sum(thd, item) {}
2075 String *val_str(String *) override;
2076 double val_real() override {
2077 int err_not_used;
2078 const char *end_not_used;
2079 String *res;
2080 res = val_str(&str_value);
2081 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2082 &end_not_used, &err_not_used)
2083 : 0.0;
2084 }
2085 longlong val_int() override {
2086 int err_not_used;
2087 String *res;
2088 const CHARSET_INFO *cs;
2089
2090 if (!(res = val_str(&str_value))) return 0; /* Null value */
2091 cs = res->charset();
2092 const char *end = res->ptr() + res->length();
2093 return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
2094 }
2096 bool val_date(Date_val *date, my_time_flags_t flags) override {
2097 return get_date_from_string(date, flags);
2098 }
2099 bool val_time(Time_val *time) override { return get_time_from_string(time); }
2101 return get_datetime_from_string(dt, flags);
2102 }
2103 enum Item_result result_type() const override { return STRING_RESULT; }
2104 bool resolve_type(THD *) override;
2105 Item *copy_or_same(THD *thd) override;
2106};
2107
2109 public:
2110 Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg,
2111 PT_item_list *opt_list)
2112 : Item_udf_sum(pos, udf_arg, opt_list) {}
2114 : Item_udf_sum(thd, item) {}
2115 String *val_str(String *) override;
2116 double val_real() override;
2117 longlong val_int() override;
2118 my_decimal *val_decimal(my_decimal *) override;
2119 bool val_date(Date_val *date, my_time_flags_t flags) override {
2120 return get_date_from_decimal(date, flags);
2121 }
2122 bool val_time(Time_val *time) override { return get_time_from_decimal(time); }
2124 return get_datetime_from_decimal(dt, flags);
2125 }
2126 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2127 bool resolve_type(THD *) override {
2130 return false;
2131 }
2132 Item *copy_or_same(THD *thd) override;
2133};
2134
2135int group_concat_key_cmp_with_distinct(const void *arg, const void *key1,
2136 const void *key2);
2137int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2138 const void *key2);
2139int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2140 void *item_arg);
2141
2142class Item_func_group_concat final : public Item_sum {
2144
2145 /// True if GROUP CONCAT has the DISTINCT attribute
2147 /// The number of ORDER BY items.
2149 /// The number of selected items, aka the concat field list
2151 /// Resolver context, points to containing query block
2153 /// String containing separator between group items
2155 /// Describes the temporary table used to perform group concat
2159 TREE *tree{nullptr};
2160
2161 /**
2162 If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
2163 out duplicates.
2164 @see Item_func_group_concat::setup
2165 @see Item_func_group_concat::add
2166 @see Item_func_group_concat::clear
2167 */
2169 /// Temporary table used to perform group concat
2170 TABLE *table{nullptr};
2172 uint row_count{0};
2173 /**
2174 The maximum permitted result length in bytes as set in
2175 group_concat_max_len system variable
2176 */
2178 bool warning_for_row{false};
2180 /// True if result has been written to output buffer.
2182 /**
2183 Following is 0 normal object and pointer to original one for copy
2184 (to correctly free resources)
2185 */
2187
2188 friend int group_concat_key_cmp_with_distinct(const void *arg,
2189 const void *key1,
2190 const void *key2);
2191 friend int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2192 const void *key2);
2193 friend int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2194 void *item_arg);
2195
2196 public:
2197 Item_func_group_concat(const POS &pos, bool is_distinct,
2198 PT_item_list *select_list,
2199 PT_order_list *opt_order_list, String *separator,
2200 PT_window *w);
2201
2204 assert(original != nullptr || unique_filter == nullptr);
2205 }
2206
2207 bool do_itemize(Parse_context *pc, Item **res) override;
2208 void cleanup() override;
2209
2210 enum Sumfunctype sum_func() const override { return GROUP_CONCAT_FUNC; }
2211 const char *func_name() const override { return "group_concat"; }
2212 Item_result result_type() const override { return STRING_RESULT; }
2213 Field *make_string_field(TABLE *table_arg) const override;
2214 void clear() override;
2215 bool add() override;
2216 void reset_field() override { assert(0); } // not used
2217 void update_field() override { assert(0); } // not used
2218 bool fix_fields(THD *, Item **) override;
2219 bool setup(THD *thd) override;
2220 void make_unique() override;
2221 double val_real() override;
2222 longlong val_int() override {
2223 String *res;
2224 int error;
2225 if (!(res = val_str(&str_value))) return (longlong)0;
2226 const char *end_ptr = res->ptr() + res->length();
2227 return my_strtoll10(res->ptr(), &end_ptr, &error);
2228 }
2229 my_decimal *val_decimal(my_decimal *decimal_value) override {
2230 return val_decimal_from_string(decimal_value);
2231 }
2232 bool val_date(Date_val *date, my_time_flags_t flags) override {
2233 return get_date_from_string(date, flags);
2234 }
2235 bool val_time(Time_val *time) override { return get_time_from_string(time); }
2237 return get_datetime_from_string(dt, flags);
2238 }
2239
2240 bool has_distinct() const noexcept { return distinct; }
2241 const String *get_separator_str() const noexcept { return separator; }
2242 uint32_t get_group_concat_max_len() const noexcept {
2243 return group_concat_max_len;
2244 }
2245 const Mem_root_array<ORDER> &get_order_array() const noexcept {
2246 return order_array;
2247 }
2248
2249 String *val_str(String *str) override;
2250 Item *copy_or_same(THD *thd) override;
2251 void no_rows_in_result() override;
2252 void print(const THD *thd, String *str,
2253 enum_query_type query_type) const override;
2254 uint64 hash() override;
2255 bool change_context_processor(uchar *arg) override {
2256 context = pointer_cast<Item_ident::Change_context *>(arg)->m_context;
2257 return false;
2258 }
2259
2261 Window_evaluation_requirements *) override {
2263 return true;
2264 }
2265};
2266
2267/**
2268 Common parent class for window functions that always work on the entire
2269 partition, even if a frame is defined.
2270
2271 The subclasses can be divided in two disjoint sub-categories:
2272 - one-pass
2273 - two-pass (requires partition cardinality to be evaluated)
2274 cf. method needs_partition_cardinality.
2275*/
2278
2279 public:
2280 Item_non_framing_wf(const POS &pos, PT_window *w) : Item_sum(pos, w) {}
2282 : Item_sum(pos, a, w) {}
2284 : Item_sum(pos, opt_list, w) {}
2286
2287 bool val_date(Date_val *date, my_time_flags_t flags) override {
2288 return get_date_from_numeric(date, flags);
2289 }
2290 bool val_time(Time_val *time) override { return get_time_from_numeric(time); }
2292 return get_datetime_from_numeric(dt, flags);
2293 }
2294 void reset_field() override { assert(false); }
2295 void update_field() override { assert(false); }
2296 bool add() override {
2297 assert(false);
2298 return false;
2299 }
2300
2301 bool fix_fields(THD *thd, Item **items) override;
2302
2303 bool framing() const override { return false; }
2304};
2305
2306/**
2307 ROW_NUMBER window function, cf. SQL 2003 Section 6.10 <window function>
2308*/
2310 // Execution state variables
2311 ulonglong m_ctr; ///< Increment for each row in partition
2312
2313 public:
2315 : Item_non_framing_wf(pos, w), m_ctr(0) {
2316 unsigned_flag = true;
2317 }
2318
2319 const char *func_name() const override { return "row_number"; }
2320 enum Sumfunctype sum_func() const override { return ROW_NUMBER_FUNC; }
2321
2322 bool resolve_type(THD *thd [[maybe_unused]]) override {
2324 return false;
2325 }
2326
2327 longlong val_int() override;
2328 double val_real() override;
2329 my_decimal *val_decimal(my_decimal *buff) override;
2330 String *val_str(String *) override;
2331
2332 void clear() override;
2333
2334 Item_result result_type() const override { return INT_RESULT; }
2335
2337 Window_evaluation_requirements *) override {
2338 return false;
2339 }
2340};
2341
2342/**
2343 RANK or DENSE_RANK window function, cf. SQL 2003 Section 6.10 <window
2344 function>
2345*/
2348 bool m_dense; ///< If true, the object represents DENSE_RANK
2349 // Execution state variables
2350 ulonglong m_rank_ctr; ///< Increment when window order columns change
2351 ulonglong m_duplicates; ///< Needed to make RANK different from DENSE_RANK
2353 m_previous; ///< Values of previous row's ORDER BY items
2354 public:
2355 Item_rank(const POS &pos, bool dense, PT_window *w)
2356 : Item_non_framing_wf(pos, w),
2357 m_dense(dense),
2358 m_rank_ctr(0),
2359 m_duplicates(0),
2361 unsigned_flag = true;
2362 }
2363
2364 ~Item_rank() override;
2365
2366 const char *func_name() const override {
2367 return m_dense ? "dense_rank" : "rank";
2368 }
2369
2370 enum Sumfunctype sum_func() const override {
2372 }
2373
2374 bool resolve_type(THD *thd [[maybe_unused]]) override {
2376 return false;
2377 }
2378
2379 longlong val_int() override;
2380 double val_real() override;
2381 my_decimal *val_decimal(my_decimal *buff) override;
2382 String *val_str(String *) override;
2383
2384 void update_after_wf_arguments_changed(THD *thd) override;
2385 bool check_wf_semantics1(THD *thd, Query_block *select,
2386 Window_evaluation_requirements *reqs) override;
2387 /**
2388 Clear state for a new partition
2389 */
2390 void clear() override;
2391 Item_result result_type() const override { return INT_RESULT; }
2392};
2393
2394/**
2395 CUME_DIST window function, cf. SQL 2003 Section 6.10 <window function>
2396*/
2399
2400 public:
2402
2403 const char *func_name() const override { return "cume_dist"; }
2404 enum Sumfunctype sum_func() const override { return CUME_DIST_FUNC; }
2405
2406 bool resolve_type(THD *thd [[maybe_unused]]) override {
2408 return false;
2409 }
2410
2411 bool check_wf_semantics1(THD *thd, Query_block *select,
2412 Window_evaluation_requirements *reqs) override;
2413
2414 bool needs_partition_cardinality() const override { return true; }
2415 void clear() override {}
2416 longlong val_int() override;
2417 double val_real() override;
2418 String *val_str(String *) override;
2420 Item_result result_type() const override { return REAL_RESULT; }
2421};
2422
2423/**
2424 PERCENT_RANK window function, cf. SQL 2003 Section 6.10 <window function>
2425*/
2428 // Execution state variables
2429 ulonglong m_rank_ctr; ///< Increment when window order columns change
2430 ulonglong m_peers; ///< Needed to make PERCENT_RANK same for peers
2431 /**
2432 Set when the last peer has been visited. Needed to increment m_rank_ctr.
2433 */
2435
2436 public:
2438 : Item_non_framing_wf(pos, w),
2439 m_rank_ctr(0),
2440 m_peers(0),
2441 m_last_peer_visited(false) {}
2442
2444 const char *func_name() const override { return "percent_rank"; }
2445 enum Sumfunctype sum_func() const override { return PERCENT_RANK_FUNC; }
2446
2447 bool resolve_type(THD *thd [[maybe_unused]]) override {
2449 return false;
2450 }
2451
2452 bool check_wf_semantics1(THD *thd, Query_block *select,
2453 Window_evaluation_requirements *reqs) override;
2454 bool needs_partition_cardinality() const override { return true; }
2455
2456 void clear() override;
2457 longlong val_int() override;
2458 double val_real() override;
2459 String *val_str(String *) override;
2461 Item_result result_type() const override { return REAL_RESULT; }
2462};
2463
2464/**
2465 NTILE window function, cf. SQL 2011 Section 6.10 <window function>
2466*/
2470
2471 public:
2472 Item_ntile(const POS &pos, Item *a, PT_window *w)
2473 : Item_non_framing_wf(pos, a, w), m_value(0) {
2474 unsigned_flag = true;
2475 }
2476
2477 const char *func_name() const override { return "ntile"; }
2478 enum Sumfunctype sum_func() const override { return NTILE_FUNC; }
2479
2480 bool resolve_type(THD *thd) override {
2481 if (args[0]->propagate_type(thd, MYSQL_TYPE_LONGLONG, true)) return true;
2483 return false;
2484 }
2485
2486 bool fix_fields(THD *thd, Item **items) override;
2487
2488 longlong val_int() override;
2489 double val_real() override;
2490 my_decimal *val_decimal(my_decimal *buff) override;
2491 String *val_str(String *) override;
2492
2493 bool check_wf_semantics1(THD *thd, Query_block *select,
2494 Window_evaluation_requirements *reqs) override;
2496 Item_result result_type() const override { return INT_RESULT; }
2497 void clear() override {}
2498 bool needs_partition_cardinality() const override { return true; }
2499};
2500
2501/**
2502 LEAD/LAG window functions, cf. SQL 2011 Section 6.10 <window function>
2503*/
2506 bool m_is_lead; ///< if true, the function is LEAD, else LAG
2507 int64 m_n; ///< canonicalized offset value
2511 /**
2512 Execution state: if set, we already have a value for current row.
2513 State is used to avoid interference with other LEAD/LAG functions on
2514 the same window, since they share the same eval loop and they should
2515 trigger evaluation only when they are on the "right" row relative to
2516 current row. For other offsets, return NULL if we don't know the value
2517 for this function yet, or if we do (m_has_value==true), return the
2518 found value.
2519 */
2521 bool m_use_default; ///< execution state: use default value for current row
2523
2524 public:
2525 Item_lead_lag(const POS &pos, bool lead,
2526 PT_item_list *opt_list, // [0] expr, [1] offset, [2] default
2527 enum_null_treatment null_treatment, PT_window *w)
2528 : Item_non_framing_wf(pos, opt_list, w),
2529 m_null_treatment(null_treatment),
2530 m_is_lead(lead),
2531 m_n(0),
2535 m_has_value(false),
2536 m_use_default(false) {}
2537
2538 const char *func_name() const override {
2539 return (m_is_lead ? "lead" : "lag");
2540 }
2541 enum Sumfunctype sum_func() const override { return LEAD_LAG_FUNC; }
2542
2543 bool resolve_type(THD *thd) override;
2544 bool fix_fields(THD *thd, Item **items) override;
2545 TYPELIB *get_typelib() const override;
2546 void update_after_wf_arguments_changed(THD *thd) override;
2547 void clear() override;
2548 bool check_wf_semantics1(THD *thd, Query_block *select,
2549 Window_evaluation_requirements *reqs) override;
2551 enum Item_result result_type() const override { return m_hybrid_type; }
2552
2553 longlong val_int() override;
2554 double val_real() override;
2555 String *val_str(String *str) override;
2556 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2557
2558 bool val_date(Date_val *date, my_time_flags_t flags) override;
2559 bool val_time(Time_val *time) override;
2560 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
2561 bool val_json(Json_wrapper *wr) override;
2562
2563 bool needs_partition_cardinality() const override {
2564 /*
2565 A possible optimization here: if LAG, we are only interested in rows we
2566 have already seen, so we might compute the result without reading the
2567 entire partition as soon as we have the current row. Similarly, a small
2568 LEAD value might avoid reading the entire partition also, giving shorter
2569 time to first result. For now, we read the entirely partition for these
2570 window functions - for simplicity.
2571 */
2572 return true;
2573 }
2574
2575 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2576 mem_root_deque<Item *> *fields) override;
2577
2579 bool has_value() const { return m_has_value; }
2580
2582 bool use_default() const { return m_use_default; }
2583
2584 private:
2585 bool setup_lead_lag();
2586 /**
2587 Core logic of LEAD/LAG window functions
2588
2589 @return true if computation yielded a NULL or error
2590 */
2591 bool compute();
2592};
2593
2594/**
2595 FIRST_VALUE/LAST_VALUE window functions, cf. SQL 2011 Section 6.10 <window
2596 function>
2597*/
2599 bool m_is_first; ///< if true, the function is FIRST_VALUE, else LAST_VALUE
2603 int64 cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2605
2606 public:
2607 Item_first_last_value(const POS &pos, bool first, Item *a,
2608 enum_null_treatment null_treatment, PT_window *w)
2609 : Item_sum(pos, a, w),
2610 m_is_first(first),
2611 m_null_treatment(null_treatment),
2614 cnt(0) {}
2615
2616 const char *func_name() const override {
2617 return m_is_first ? "first_value" : "last_value";
2618 }
2619
2620 enum Sumfunctype sum_func() const override { return FIRST_LAST_VALUE_FUNC; }
2621
2622 bool resolve_type(THD *thd) override;
2623 bool fix_fields(THD *thd, Item **items) override;
2624 void update_after_wf_arguments_changed(THD *thd) override;
2625 void clear() override;
2626 bool check_wf_semantics1(THD *thd, Query_block *select,
2627 Window_evaluation_requirements *reqs) override;
2628 enum Item_result result_type() const override { return m_hybrid_type; }
2629
2630 longlong val_int() override;
2631 double val_real() override;
2632 String *val_str(String *str) override;
2633 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2634
2635 bool val_date(Date_val *date, my_time_flags_t flags) override;
2636 bool val_time(Time_val *time) override;
2637 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
2638 bool val_json(Json_wrapper *wr) override;
2639
2640 void reset_field() override { assert(false); }
2641 void update_field() override { assert(false); }
2642 bool add() override {
2643 assert(false);
2644 return false;
2645 }
2646
2647 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2648 mem_root_deque<Item *> *fields) override;
2649 bool uses_only_one_row() const override { return true; }
2650
2651 private:
2652 bool setup_first_last();
2653 /**
2654 Core logic of FIRST/LAST_VALUE window functions
2655
2656 @return true if computation yielded a NULL or error
2657 */
2658 bool compute();
2659};
2660
2661/**
2662 NTH_VALUE window function, cf. SQL 2011 Section 6.10 <window
2663 function>
2664*/
2665class Item_nth_value : public Item_sum {
2667 int64 m_n; ///< The N of the function
2668 bool m_from_last; ///< true iff FROM_LAST was specified
2672 int64 m_cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2673
2675
2676 public:
2677 Item_nth_value(const POS &pos, PT_item_list *a, bool from_last,
2678 enum_null_treatment null_treatment, PT_window *w)
2679 : Item_sum(pos, a, w),
2680 m_null_treatment(null_treatment),
2681 m_n(0),
2682 m_from_last(from_last),
2685 m_cnt(0) {}
2686
2687 const char *func_name() const override { return "nth_value"; }
2688 enum Sumfunctype sum_func() const override { return NTH_VALUE_FUNC; }
2689
2690 bool resolve_type(THD *thd) override;
2691 bool fix_fields(THD *thd, Item **items) override;
2692 void update_after_wf_arguments_changed(THD *thd) override;
2693 bool setup_nth();
2694 void clear() override;
2695
2696 bool check_wf_semantics1(THD *thd, Query_block *select,
2697 Window_evaluation_requirements *reqs) override;
2699
2700 enum Item_result result_type() const override { return m_hybrid_type; }
2701
2702 longlong val_int() override;
2703 double val_real() override;
2704 String *val_str(String *str) override;
2705 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2706
2707 bool val_date(Date_val *date, my_time_flags_t flags) override;
2708 bool val_time(Time_val *time) override;
2709 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
2710 bool val_json(Json_wrapper *wr) override;
2711
2712 void reset_field() override { assert(false); }
2713 void update_field() override { assert(false); }
2714 bool add() override {
2715 assert(false);
2716 return false;
2717 }
2718
2719 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2720 mem_root_deque<Item *> *fields) override;
2721 bool uses_only_one_row() const override { return true; }
2722
2723 private:
2724 /**
2725 Core logic of NTH_VALUE window functions
2726
2727 @return true if computation yielded a NULL or error
2728 */
2729 bool compute();
2730};
2731
2732/**
2733 Class for implementation of the GROUPING function. The GROUPING
2734 function distinguishes super-aggregate rows from regular grouped
2735 rows. GROUP BY extensions such as ROLLUP and CUBE produce
2736 super-aggregate rows where the set of all values is represented
2737 by null. Using the GROUPING function, you can distinguish a null
2738 representing the set of all values in a super-aggregate row from
2739 a NULL in a regular row.
2740*/
2742 public:
2745 }
2746 const char *func_name() const override { return "grouping"; }
2747 enum Functype functype() const override { return GROUPING_FUNC; }
2748 longlong val_int() override;
2749 bool aggregate_check_group(uchar *arg) override;
2750 bool fix_fields(THD *thd, Item **ref) override;
2751 void update_used_tables() override;
2752 bool aggregate_check_distinct(uchar *arg) override;
2753
2754 private:
2755 /// The query block in which this function is called.
2757};
2758
2759/**
2760 A wrapper Item that contains a number of aggregate items, one for each level
2761 of rollup (see Item_rollup_group_item for numbering conventions). When
2762 aggregating, every aggregator is either reset or updated as per the correct
2763 level, and when returning a value, the correct child item corresponding to
2764 the current rollup level is queried.
2765 */
2767 public:
2768 explicit Item_rollup_sum_switcher(List<Item> *sum_func_per_level)
2769 : Item_sum((*sum_func_per_level)[0]),
2770 m_num_levels(sum_func_per_level->size()) {
2771 args = (*THR_MALLOC)->ArrayAlloc<Item *>(sum_func_per_level->size());
2772 int i = 0;
2773 for (Item &item : *sum_func_per_level) {
2774 args[i++] = &item;
2775 }
2779 hidden = master()->hidden;
2783 }
2784 double val_real() override;
2785 longlong val_int() override;
2786 String *val_str(String *str) override;
2788 bool val_json(Json_wrapper *result) override;
2789 bool is_null() override;
2790 bool val_date(Date_val *date, my_time_flags_t flags) override;
2791 bool val_time(Time_val *time) override;
2792 bool val_datetime(Datetime_val *dt, my_time_flags_t flags) override;
2793 const char *func_name() const override { return "rollup_sum_switcher"; }
2794 table_map used_tables() const override { return master()->used_tables(); }
2795 Item_result result_type() const override { return master()->result_type(); }
2796 bool resolve_type(THD *) override {
2798 return false;
2799 }
2800 void print(const THD *thd, String *str,
2801 enum_query_type query_type) const override;
2802 Field *create_tmp_field(bool group, TABLE *table) override;
2803
2804 enum Sumfunctype sum_func() const override { return master()->sum_func(); }
2805 enum Sumfunctype real_sum_func() const override {
2807 }
2808 void reset_field() override { assert(false); }
2809 void update_field() override { assert(false); }
2810 void clear() override;
2811 bool add() override {
2812 assert(false);
2813 return true;
2814 }
2815 void no_rows_in_result() override {
2816 // Produce a single grouping row with NULLs for all group by
2817 // expressions and grand total for aggregates.
2820 }
2821
2822 bool reset_and_add_for_rollup(int last_unchanged_group_item_idx);
2823
2824 int set_aggregator(Aggregator::Aggregator_type aggregator) override;
2825 bool aggregator_setup(THD *thd) override;
2826 inline bool aggregator_add_all() {
2827 for (int i = 0; i < m_num_levels; ++i) {
2828 if (child(i)->aggregator_add()) {
2829 return true;
2830 }
2831 }
2832 return false;
2833 }
2834
2835 // Used when create_tmp_table() needs to delay application of aggregate
2836 // functions to a later stage in the query processing.
2837 Item *get_arg(uint i) override { return master()->get_arg(i); }
2838 const Item *get_arg(uint i) const override { return master()->get_arg(i); }
2839 Item *set_arg(THD *thd, uint i, Item *new_val) override {
2840 Item *ret = nullptr;
2841 for (int j = 0; j < m_num_levels; ++j) {
2842 ret = child(j)->set_arg(thd, i, new_val);
2843 }
2844 return ret; // Return the last one, arbitrarily.
2845 }
2846 uint argument_count() const override { return master()->argument_count(); }
2847
2848 // Used by AggregateIterator.
2850 inline Item_sum *master() const { return child(0); }
2851
2852 bool is_rollup_sum_wrapper() const override { return true; }
2853 const Item_sum *unwrap_sum() const override { return master(); }
2854 Item_sum *unwrap_sum() override { return master(); }
2855
2856 private:
2857 inline Item *current_arg() const;
2858 inline Item_sum *child(size_t i) const {
2859 return down_cast<Item_sum *>(args[i]);
2860 }
2861
2862 const int m_num_levels;
2864};
2865/// Implements ST_Collect which aggregates geometries into Multipoints,
2866/// Multilinestrings, Multipolygons and Geometrycollections.
2867
2869 private:
2870 std::optional<gis::srid_t> srid;
2871 std::unique_ptr<gis::Geometrycollection> m_geometrycollection;
2872 void pop_front();
2873
2874 public:
2876 Item_sum_collect(THD *thd, Item_sum *item) : Item_sum(thd, item) {
2878 }
2879 Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
2880 : Item_sum(pos, a, w) {
2881 set_distinct(distinct);
2883 }
2884
2885 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2886 longlong val_int() override { return val_int_from_string(); }
2887 double val_real() override { return val_real_from_string(); }
2888 bool val_date(Date_val *, my_time_flags_t) override { return true; }
2889 bool val_time(Time_val *) override { return true; }
2890 bool val_datetime(Datetime_val *, my_time_flags_t) override { return true; }
2891 enum Sumfunctype sum_func() const override { return GEOMETRY_AGGREGATE_FUNC; }
2892 Item_result result_type() const override { return STRING_RESULT; }
2894 /*
2895 In contrast to Item_sum, Item_sum_collect always uses Aggregator_simple,
2896 and only needs to reset its aggregator when called.
2897 */
2898 if (aggr != nullptr) {
2899 aggr->clear();
2900 return false;
2901 }
2902
2903 aggr = new (*THR_MALLOC) Aggregator_simple(this);
2904 return aggr ? false : true;
2905 }
2906
2907 bool fix_fields(THD *thd, Item **ref) override;
2908 /*
2909 We need to override check_wf_semantics1 because it reports an error when
2910 with_distinct is set.
2911 */
2912 bool check_wf_semantics1(THD *thd, Query_block *,
2914
2915 Item *copy_or_same(THD *thd) override;
2916
2917 void update_field() override;
2918 void reset_field() override;
2919
2920 String *val_str(String *str) override;
2921
2922 bool add() override;
2923
2924 void read_result_field();
2925 void store_result_field();
2926
2927 void clear() override;
2928 const char *func_name() const override { return "st_collect"; }
2929};
2930
2931#endif /* ITEM_SUM_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
The distinct aggregator.
Definition: item_sum.h:818
uint tree_key_length
Definition: item_sum.h:871
uint32 * field_lengths
Definition: item_sum.h:848
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2238
bool endup_done
Definition: item_sum.h:829
Const_distinct
Definition: item_sum.h:873
@ CONST_NULL
Set to true if the result is known to be always NULL.
Definition: item_sum.h:884
@ CONST_NOT_NULL
Set to true if count distinct is on only const items.
Definition: item_sum.h:891
@ NOT_CONST
Definition: item_sum.h:874
static int composite_key_cmp(const void *arg, const void *a, const void *b)
Correctly compare composite keys.
Definition: item_sum.cc:1022
void endup() override
Calculate the aggregate function value.
Definition: item_sum.cc:1415
~Aggregator_distinct() override
Definition: item_sum.cc:2188
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:902
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2233
void clear() override
Invalidate calculated value and clear the distinct rows.
Definition: item_sum.cc:1309
bool setup(THD *) override
Called before feeding the first row.
Definition: item_sum.cc:1093
bool unique_walk_function(void *element)
Aggregate a distinct row from the distinct hash table.
Definition: item_sum.cc:2181
Unique * tree
Definition: item_sum.h:863
Temp_table_param * tmp_table_param
Definition: item_sum.h:854
bool add() override
Process incoming row.
Definition: item_sum.cc:1341
TABLE * table
Definition: item_sum.h:840
Aggregator_type Aggrtype() override
Definition: item_sum.h:913
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2228
enum Aggregator_distinct::Const_distinct const_distinct
Aggregator_distinct(Item_sum *sum)
Definition: item_sum.h:905
The pass-through aggregator.
Definition: item_sum.h:932
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:938
Aggregator_simple(Item_sum *sum)
Definition: item_sum.h:934
bool setup(THD *thd) override
Called before adding the first row.
Definition: item_sum.h:937
Aggregator_type Aggrtype() override
Definition: item_sum.h:935
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2205
void endup() override
Called when there are no more data and the final value is to be retrieved.
Definition: item_sum.h:940
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2209
bool add() override
Called when there's a new value to be aggregated.
Definition: item_sum.h:939
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2213
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:203
Definition: my_temporal.h:395
Definition: my_temporal.h:339
Definition: field.h:570
Definition: item_sum.h:1202
Item_aggr_avg_field(Item_sum_avg *item)
Definition: item_sum.cc:3812
String * val_str(String *) override
Definition: item_sum.cc:3854
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3840
uint m_prec_increment
Definition: item_sum.h:1214
uint m_dec_bin_size
Definition: item_sum.h:1213
double val_real() override
Definition: item_sum.cc:3829
enum Type type() const override
Definition: item_sum.h:1205
uint m_scale
Definition: item_sum.h:1212
uint m_precision
Definition: item_sum.h:1211
This is used in connection with an Item_sum_bit,.
Definition: item_sum.h:1218
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:3937
enum Type type() const override
Definition: item_sum.h:1228
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:3952
ulonglong m_reset_bits
Definition: item_sum.h:1231
longlong val_int() override
Definition: item_sum.cc:3876
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:3944
double val_real() override
Definition: item_sum.cc:3891
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3908
String * val_str(String *) override
Definition: item_sum.cc:3916
Item_aggr_bit_field(Item_sum_bit *item, ulonglong reset_bits)
Definition: item_sum.cc:3859
Common abstract class for aggregate field classes that return numeric values: Item_aggr_avg_field Ite...
Definition: item_sum.h:1184
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:1193
longlong val_int() override
Definition: item_sum.h:1186
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:1190
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:1196
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.h:1199
Definition: item_sum.h:1528
enum Type type() const override
Definition: item_sum.h:1531
enum Item_result result_type() const override
Definition: item_sum.h:1534
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:1535
double val_real() override
Definition: item_sum.cc:3962
Item_aggr_std_field(Item_sum_std *item)
Definition: item_sum.cc:3959
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3968
Definition: item_sum.h:1394
double val_real() override
Definition: item_sum.cc:4000
Item_aggr_variance_field(Item_sum_variance *item)
Definition: item_sum.cc:3988
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:1403
String * val_str(String *str) override
Definition: item_sum.h:1399
my_decimal * val_decimal(my_decimal *dec_buf) override
Definition: item_sum.h:1400
uint m_sample
Definition: item_sum.h:1411
enum Type type() const override
Definition: item_sum.h:1397
This is used in connection with a parent aggregate Item:
Definition: item_sum.h:1153
bool mark_field_in_map(uchar *arg) override
Mark underlying field in read or write map of a table.
Definition: item_sum.h:1164
Item_result m_result_type
Stores the Item's result type.
Definition: item_sum.h:1158
Field * m_field
The tmp table's column containing the value of the set function.
Definition: item_sum.h:1156
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:1171
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1163
enum Item_result result_type() const override
Definition: item_sum.h:1161
Definition: item.h:6899
CUME_DIST window function, cf.
Definition: item_sum.h:2397
Item_cume_dist(const POS &pos, PT_window *w)
Definition: item_sum.h:2401
Item_non_framing_wf super
Definition: item_sum.h:2398
double val_real() override
Definition: item_sum.cc:5052
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2404
void clear() override
Definition: item_sum.h:2415
const char * func_name() const override
Definition: item_sum.h:2403
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:2414
String * val_str(String *) override
Definition: item_sum.cc:5072
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:5042
longlong val_int() override
Definition: item_sum.cc:5064
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2406
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:5076
Item_result result_type() const override
Definition: item_sum.h:2420
Definition: item.h:4466
FIRST_VALUE/LAST_VALUE window functions, cf.
Definition: item_sum.h:2598
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2620
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:5391
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5280
void reset_field() override
Definition: item_sum.h:2640
const char * func_name() const override
Definition: item_sum.h:2616
bool setup_first_last()
Definition: item_sum.cc:5322
double val_real() override
Definition: item_sum.cc:5371
String * val_str(String *str) override
Definition: item_sum.cc:5436
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:5339
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:5310
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:5266
bool compute()
Core logic of FIRST/LAST_VALUE window functions.
Definition: item_sum.cc:5343
int64 cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2603
Item_result m_hybrid_type
Definition: item_sum.h:2601
enum Item_result result_type() const override
Definition: item_sum.h:2628
Item_cache * m_value
Definition: item_sum.h:2602
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5412
void update_field() override
Definition: item_sum.h:2641
enum_null_treatment m_null_treatment
Definition: item_sum.h:2600
Item_sum super
Definition: item_sum.h:2604
void clear() override
Definition: item_sum.cc:5333
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2649
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:5381
bool add() override
Definition: item_sum.h:2642
Item_first_last_value(const POS &pos, bool first, Item *a, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2607
bool m_is_first
if true, the function is FIRST_VALUE, else LAST_VALUE
Definition: item_sum.h:2599
longlong val_int() override
Definition: item_sum.cc:5361
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5422
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5291
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:5402
Definition: item_sum.h:2142
void clear() override
Definition: item_sum.cc:4514
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2235
bool m_result_finalized
True if result has been written to output buffer.
Definition: item_sum.h:2181
String result
Definition: item_sum.h:2157
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:2186
longlong val_int() override
Definition: item_sum.h:2222
const String * get_separator_str() const noexcept
Definition: item_sum.h:2241
uint32_t get_group_concat_max_len() const noexcept
Definition: item_sum.h:2242
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.h:2229
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:4260
uint row_count
Definition: item_sum.h:2172
void make_unique() override
Definition: item_sum.cc:4787
bool change_context_processor(uchar *arg) override
Definition: item_sum.h:2255
bool setup(THD *thd) override
Definition: item_sum.cc:4670
Item_sum super
Definition: item_sum.h:2143
bool distinct
True if GROUP CONCAT has the DISTINCT attribute.
Definition: item_sum.h:2146
TABLE * table
Temporary table used to perform group concat.
Definition: item_sum.h:2170
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:4181
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:2236
double val_real() override
Definition: item_sum.cc:4795
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2210
String * val_str(String *str) override
Definition: item_sum.cc:4802
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:4217
bool force_copy_fields
Definition: item_sum.h:2179
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:4577
Unique * unique_filter
If DISTINCT is used with this GROUP_CONCAT, this member is used to filter out duplicates.
Definition: item_sum.h:2168
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2260
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:4512
Mem_root_array< ORDER > order_array
Definition: item_sum.h:2171
TREE tree_base
Definition: item_sum.h:2158
void update_field() override
Definition: item_sum.h:2217
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:4827
Temp_table_param * tmp_table_param
Describes the temporary table used to perform group concat.
Definition: item_sum.h:2156
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:4439
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4504
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:4387
const char * func_name() const override
Definition: item_sum.h:2211
Item_result result_type() const override
Definition: item_sum.h:2212
Field * make_string_field(TABLE *table_arg) const override
Create a field to hold a string value from an item.
Definition: item_sum.cc:4471
uint64 hash() override
Generate hash unique to an item depending on its attributes.
Definition: item_sum.cc:4863
uint m_order_arg_count
The number of ORDER BY items.
Definition: item_sum.h:2148
TREE * tree
Definition: item_sum.h:2159
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:4349
bool has_distinct() const noexcept
Definition: item_sum.h:2240
bool add() override
Definition: item_sum.cc:4526
Name_resolution_context * context
Resolver context, points to containing query block.
Definition: item_sum.h:2152
void reset_field() override
Definition: item_sum.h:2216
uint m_field_arg_count
The number of selected items, aka the concat field list.
Definition: item_sum.h:2150
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:2177
bool warning_for_row
Definition: item_sum.h:2178
String * separator
String containing separator between group items.
Definition: item_sum.h:2154
~Item_func_group_concat() override
Definition: item_sum.h:2203
const Mem_root_array< ORDER > & get_order_array() const noexcept
Definition: item_sum.h:2245
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:2232
Class for implementation of the GROUPING function.
Definition: item_sum.h:2741
const Query_block * m_query_block
The query block in which this function is called.
Definition: item_sum.h:2756
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:6575
longlong val_int() override
Evaluation of the GROUPING function.
Definition: item_sum.cc:6524
Item_func_grouping(const POS &pos, PT_item_list *a)
Definition: item_sum.h:2743
const char * func_name() const override
Definition: item_sum.h:2746
bool fix_fields(THD *thd, Item **ref) override
Resolve the fields in the GROUPING function.
Definition: item_sum.cc:6469
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:6545
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_sum.cc:6587
enum Functype functype() const override
Definition: item_sum.h:2747
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:502
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:882
const char * original_db_name() const
Definition: item.h:4352
void set_original_field_name(const char *name_arg)
Definition: item.h:4349
const char * original_table_name() const
Definition: item.h:4353
Definition: item_func.h:1044
Definition: item.h:5219
LEAD/LAG window functions, cf.
Definition: item_sum.h:2504
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5719
bool has_value() const
Definition: item_sum.h:2579
bool m_is_lead
if true, the function is LEAD, else LAG
Definition: item_sum.h:2506
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:2563
const char * func_name() const override
Definition: item_sum.h:2538
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:5779
void set_use_default(bool value)
Definition: item_sum.h:2581
String * val_str(String *str) override
Definition: item_sum.cc:5859
bool m_use_default
execution state: use default value for current row
Definition: item_sum.h:2521
enum Item_result result_type() const override
Definition: item_sum.h:2551
Item_cache * m_default
Definition: item_sum.h:2510
bool compute()
Core logic of LEAD/LAG window functions.
Definition: item_sum.cc:5908
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:5881
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:2525
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:5806
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2541
enum_null_treatment m_null_treatment
Definition: item_sum.h:2505
void clear() override
Definition: item_sum.cc:5818
bool use_default() const
Definition: item_sum.h:2582
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:5872
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.cc:5728
bool m_has_value
Execution state: if set, we already have a value for current row.
Definition: item_sum.h:2520
longlong val_int() override
Definition: item_sum.cc:5830
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5846
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5751
Item_result m_hybrid_type
Definition: item_sum.h:2508
int64 m_n
canonicalized offset value
Definition: item_sum.h:2507
double val_real() override
Definition: item_sum.cc:5838
void set_has_value(bool value)
Definition: item_sum.h:2578
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5669
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5898
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:5890
bool setup_lead_lag()
Definition: item_sum.cc:5790
Item_cache * m_value
Definition: item_sum.h:2509
Item_non_framing_wf super
Definition: item_sum.h:2522
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:5825
Common parent class for window functions that always work on the entire partition,...
Definition: item_sum.h:2276
Item_non_framing_wf(const POS &pos, PT_window *w)
Definition: item_sum.h:2280
bool add() override
Definition: item_sum.h:2296
Item_non_framing_wf(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2281
Item_non_framing_wf(const POS &pos, PT_item_list *opt_list, PT_window *w)
Definition: item_sum.h:2283
Item_sum super
Definition: item_sum.h:2277
Item_non_framing_wf(THD *thd, Item_non_framing_wf *i)
Definition: item_sum.h:2285
void update_field() override
Definition: item_sum.h:2295
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:2287
void reset_field() override
Definition: item_sum.h:2294
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2290
bool framing() const override
All aggregates are framing, i.e.
Definition: item_sum.h:2303
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:2291
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:4882
NTH_VALUE window function, cf.
Definition: item_sum.h:2665
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:5649
int64 m_cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2672
void update_field() override
Definition: item_sum.h:2713
bool compute()
Core logic of NTH_VALUE window functions.
Definition: item_sum.cc:5552
String * val_str(String *str) override
Definition: item_sum.cc:5619
Item_sum super
Definition: item_sum.h:2674
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5156
enum Item_result result_type() const override
Definition: item_sum.h:2700
Item_result m_hybrid_type
Definition: item_sum.h:2669
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5605
bool setup_nth()
Definition: item_sum.cc:5511
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:5639
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5460
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:5629
enum_field_types m_hybrid_field_type
Definition: item_sum.h:2670
int64 m_n
The N of the function.
Definition: item_sum.h:2667
double val_real() override
Definition: item_sum.cc:5595
bool m_from_last
true iff FROM_LAST was specified
Definition: item_sum.h:2668
Item_cache * m_value
Definition: item_sum.h:2671
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5659
enum_null_treatment m_null_treatment
Definition: item_sum.h:2666
void reset_field() override
Definition: item_sum.h:2712
longlong val_int() override
Definition: item_sum.cc:5585
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:2677
void clear() override
Definition: item_sum.cc:5522
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5446
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:5500
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2688
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:5528
bool add() override
Definition: item_sum.h:2714
const char * func_name() const override
Definition: item_sum.h:2687
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:5532
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2721
NTILE window function, cf.
Definition: item_sum.h:2467
void clear() override
Definition: item_sum.h:2497
Item_non_framing_wf super
Definition: item_sum.h:2468
String * val_str(String *) override
Definition: item_sum.cc:5235
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2480
const char * func_name() const override
Definition: item_sum.h:2477
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:2498
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5250
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5174
double val_real() override
Definition: item_sum.cc:5230
longlong val_int() override
Definition: item_sum.cc:5180
longlong m_value
Definition: item_sum.h:2469
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:5237
Item_ntile(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2472
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2478
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:5242
Item_result result_type() const override
Definition: item_sum.h:2496
PERCENT_RANK window function, cf.
Definition: item_sum.h:2426
String * val_str(String *) override
Definition: item_sum.cc:5139
Item_percent_rank(const POS &pos, PT_window *w)
Definition: item_sum.h:2437
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2429
const char * func_name() const override
Definition: item_sum.h:2444
Item_result result_type() const override
Definition: item_sum.h:2461
~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:5081
ulonglong m_peers
Needed to make PERCENT_RANK same for peers.
Definition: item_sum.h:2430
double val_real() override
Definition: item_sum.cc:5105
Item_non_framing_wf super
Definition: item_sum.h:2427
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2447
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2445
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:2454
bool m_last_peer_visited
Set when the last peer has been visited.
Definition: item_sum.h:2434
void clear() override
Definition: item_sum.cc:5148
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:5143
longlong val_int() override
Definition: item_sum.cc:5131
RANK or DENSE_RANK window function, cf.
Definition: item_sum.h:2346
Item_result result_type() const override
Definition: item_sum.h:2391
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:4937
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:4961
ulonglong m_duplicates
Needed to make RANK different from DENSE_RANK.
Definition: item_sum.h:2351
bool m_dense
If true, the object represents DENSE_RANK.
Definition: item_sum.h:2348
longlong val_int() override
Definition: item_sum.cc:4979
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2370
~Item_rank() override
Definition: item_sum.cc:5035
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2374
Item_rank(const POS &pos, bool dense, PT_window *w)
Definition: item_sum.h:2355
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:5014
const char * func_name() const override
Definition: item_sum.h:2366
double val_real() override
Definition: item_sum.cc:5007
String * val_str(String *) override
Definition: item_sum.cc:5012
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2350
void clear() override
Clear state for a new partition.
Definition: item_sum.cc:5019
Mem_root_array< Cached_item * > m_previous
Values of previous row's ORDER BY items.
Definition: item_sum.h:2353
Item_non_framing_wf super
Definition: item_sum.h:2347
Item with result field.
Definition: item.h:5946
longlong llrint_with_overflow_check(double realval)
Definition: item.h:5985
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2766
table_map used_tables() const override
Definition: item_sum.h:2794
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:6603
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:6668
void update_field() override
Definition: item_sum.h:2809
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2796
Item * get_arg(uint i) override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2837
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.h:2839
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.cc:6654
void set_current_rollup_level(int level)
Definition: item_sum.h:2849
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:6659
bool aggregator_setup(THD *thd) override
Called to initialize the aggregator.
Definition: item_sum.cc:6701
Item_result result_type() const override
Definition: item_sum.h:2795
String * val_str(String *str) override
Definition: item_sum.cc:6633
int set_aggregator(Aggregator::Aggregator_type aggregator) override
Definition: item_sum.cc:6690
Item_sum * unwrap_sum() override
Non-const version.
Definition: item_sum.h:2854
longlong val_int() override
Definition: item_sum.cc:6626
void reset_field() override
Definition: item_sum.h:2808
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:2853
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:6614
Item_rollup_sum_switcher(List< Item > *sum_func_per_level)
Definition: item_sum.h:2768
bool aggregator_add_all()
Definition: item_sum.h:2826
Item * current_arg() const
Definition: item_sum.cc:6598
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:2815
Item_sum * master() const
Definition: item_sum.h:2850
Item_sum * child(size_t i) const
Definition: item_sum.h:2858
enum Sumfunctype real_sum_func() const override
Definition: item_sum.h:2805
bool is_rollup_sum_wrapper() const override
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:2852
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2804
const char * func_name() const override
Definition: item_sum.h:2793
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:6608
int m_current_rollup_level
Definition: item_sum.h:2863
double val_real() override
Definition: item_sum.cc:6619
bool val_json(Json_wrapper *result) override
Get a JSON value from an Item.
Definition: item_sum.cc:6647
uint argument_count() const override
Definition: item_sum.h:2846
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:6640
bool add() override
Definition: item_sum.h:2811
void clear() override
Definition: item_sum.cc:6672
bool reset_and_add_for_rollup(int last_unchanged_group_item_idx)
Definition: item_sum.cc:6678
const int m_num_levels
Definition: item_sum.h:2862
const Item * get_arg(uint i) const override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2838
ROW_NUMBER window function, cf.
Definition: item_sum.h:2309
ulonglong m_ctr
Increment for each row in partition.
Definition: item_sum.h:2311
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2322
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2320
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2336
Item_row_number(const POS &pos, PT_window *w)
Definition: item_sum.h:2314
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4930
Item_result result_type() const override
Definition: item_sum.h:2334
String * val_str(String *) override
Definition: item_sum.cc:4926
longlong val_int() override
Definition: item_sum.cc:4907
void clear() override
Definition: item_sum.cc:4935
const char * func_name() const override
Definition: item_sum.h:2319
double val_real() override
Definition: item_sum.cc:4921
Definition: item_sum.h:1944
Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1946
const char * func_name() const override
Definition: item_sum.h:1950
Item_sum_and(THD *thd, Item_sum_and *item)
Definition: item_sum.h:1949
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3364
Definition: item_sum.h:1354
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2306
void clear() override
Definition: item_sum.cc:2362
String * val_str(String *str) override
Definition: item_sum.cc:2477
const char * func_name() const override
Definition: item_sum.h:1382
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3626
Item_sum_sum super
Definition: item_sum.h:1358
Item * result_item(Field *) override
Definition: item_sum.h:1381
double val_real() override
Definition: item_sum.cc:2371
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1385
Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
Definition: item_sum.h:1362
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:2340
my_decimal m_avg_dec
Definition: item_sum.h:1359
uint f_scale
Definition: item_sum.h:1357
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1369
double m_avg
Definition: item_sum.h:1360
longlong val_int() override
Definition: item_sum.h:1376
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2333
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2394
void reset_field() override
Definition: item_sum.cc:3524
uint prec_increment
Definition: item_sum.h:1356
uint dec_bin_size
Definition: item_sum.h:1357
bool add() override
Definition: item_sum.cc:2364
Item_sum_avg(THD *thd, Item_sum_avg *item)
Definition: item_sum.h:1365
uint f_precision
Definition: item_sum.h:1357
Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
Definition: item_sum.h:1774
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:1679
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1884
void remove_bits(const String *s1, ulonglong b1)
For windowing: perform inverse aggregation.
Definition: item_sum.cc:1601
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:3247
const char initial_value_buff_storage[1]
Buffer used to avoid String allocation in the constructor.
Definition: item_sum.h:1785
static constexpr uint DIGIT_CNT_CARD
Definition: item_sum.h:1828
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1870
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1543
bool m_is_xor
true iff BIT_XOR
Definition: item_sum.h:1831
void clear() override
Definition: item_sum.cc:3336
Item * result_item(Field *) override
Definition: item_sum.h:1866
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:3261
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:1518
longlong val_int() override
Definition: item_sum.cc:3312
Item_sum super
Definition: item_sum.h:1775
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:1820
Item_result hybrid_type
Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT.
Definition: item_sum.h:1783
ulonglong reset_bits
Stores the neutral element for function.
Definition: item_sum.h:1777
Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg, PT_window *w)
Definition: item_sum.h:1834
bool is_and() const
Definition: item_sum.h:1897
void reset_field() override
Definition: item_sum.cc:3554
ulonglong m_count
Execution state (windowing): this is for counting rows entering and leaving the window frame,...
Definition: item_sum.h:1791
double val_real() override
Definition: item_sum.cc:3286
bool add() override
Common implementation of Item_sum_or::add, Item_sum_and:add and Item_sum_xor::add.
Definition: item_sum.cc:1763
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.cc:3268
enum Item_result result_type() const override
Definition: item_sum.h:1871
uint m_digit_cnt_card
Definition: item_sum.h:1826
Item_sum_bit(THD *thd, Item_sum_bit *item)
Copy constructor, used for executing subqueries with temporary tables.
Definition: item_sum.h:1847
String * val_str(String *str) override
Definition: item_sum.cc:3216
void update_field() override
Definition: item_sum.cc:3564
ulonglong bits
Stores the result value for the INT_RESULT.
Definition: item_sum.h:1779
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:1799
String value_buff
Stores the result value for the STRING_RESULT.
Definition: item_sum.h:1781
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:3254
Implements ST_Collect which aggregates geometries into Multipoints, Multilinestrings,...
Definition: item_sum.h:2868
bool add() override
Definition: item_sum.cc:6776
void clear() override
Definition: item_sum.cc:6770
void read_result_field()
Definition: item_sum.cc:6831
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:6739
std::optional< gis::srid_t > srid
Definition: item_sum.h:2870
Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
Definition: item_sum.h:2879
void update_field() override
Definition: item_sum.cc:6924
longlong val_int() override
Definition: item_sum.h:2886
bool val_time(Time_val *) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2889
std::unique_ptr< gis::Geometrycollection > m_geometrycollection
Definition: item_sum.h:2871
String * val_str(String *str) override
Definition: item_sum.cc:6877
bool val_datetime(Datetime_val *, my_time_flags_t) override
Evaluate the item and return result as a datetime value.
Definition: item_sum.h:2890
void reset_field() override
Definition: item_sum.cc:6987
Item_sum_collect(THD *thd, Item_sum *item)
Definition: item_sum.h:2876
void store_result_field()
Definition: item_sum.cc:6930
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:6982
bool check_wf_semantics1(THD *thd, Query_block *, Window_evaluation_requirements *r) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:6761
const char * func_name() const override
Definition: item_sum.h:2928
bool val_date(Date_val *, my_time_flags_t) override
Evaluate the item and return result as a date value.
Definition: item_sum.h:2888
void pop_front()
Definition: item_sum.cc:6870
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2891
Item_result result_type() const override
Definition: item_sum.h:2892
int set_aggregator(Aggregator::Aggregator_type) override
Definition: item_sum.h:2893
double val_real() override
Definition: item_sum.h:2887
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6826
Definition: item_sum.h:1083
longlong count
Definition: item_sum.h:1084
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1117
longlong val_int() override
Definition: item_sum.cc:2268
Item_sum_count(Item *item, bool distinct)
Definition: item_sum.h:1096
void update_field() override
Definition: item_sum.cc:3617
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2249
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1114
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:2300
void make_const(longlong count_arg)
Definition: item_sum.h:1124
void reset_field() override
Definition: item_sum.cc:3516
Item_sum_count(Item_int *number)
Definition: item_sum.h:1095
Item_sum_count(THD *thd, Item_sum_count *item)
Definition: item_sum.h:1112
Item_sum_count(const POS &pos, PT_item_list *list, PT_window *w)
Constructs an instance for COUNT(DISTINCT)
Definition: item_sum.h:1108
void clear() override
Definition: item_sum.cc:2257
const char * func_name() const override
Definition: item_sum.h:1131
Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1093
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1123
bool add() override
Definition: item_sum.cc:2259
Abstract base class for the MIN and MAX aggregate functions.
Definition: item_sum.h:1569
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3169
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:3164
bool setup_hybrid(Item *item, Item *value_arg)
MIN/MAX function setup.
Definition: item_sum.cc:1855
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:1720
virtual Item_sum_hybrid * clone_hybrid(THD *thd) const =0
Create a clone of this object.
bool add() override
Definition: item_sum.cc:3199
const bool m_is_min
Tells if this is the MIN function (true) or the MAX function (false).
Definition: item_sum.h:1576
bool compute()
This function implements the optimized version of retrieving min/max value.
Definition: item_sum.cc:2902
bool has_values()
Definition: item_sum.h:1717
void min_max_update_int_field()
Definition: item_sum.cc:3778
longlong val_int() override
Definition: item_sum.cc:3035
bool m_has_values
Definition: item_sum.h:1588
void reset_field() override
Reset field before aggregation.
Definition: item_sum.cc:3394
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:3078
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:3135
void clear() override
Definition: item_sum.cc:2853
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:1616
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:3098
Item_cache * arg_cache
Definition: item_sum.h:1585
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3150
void min_max_update_json_field()
Definition: item_sum.cc:3731
void update_field() override
Definition: item_sum.cc:3662
longlong val_date_temporal() override
Return date value of item in packed longlong format.
Definition: item_sum.cc:3049
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:2863
bool keep_field_type() const override
Definition: item_sum.h:1709
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3061
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:1610
void min_max_update_real_field()
Definition: item_sum.cc:3765
Item_sum_hybrid(THD *thd, const Item_sum_hybrid *item)
Definition: item_sum.h:1680
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:3088
Item_result hybrid_type
Definition: item_sum.h:1587
bool m_optimize
Set to true when min/max can be optimized using window's ordering.
Definition: item_sum.h:1596
void min_max_update_time_field()
Definition: item_sum.cc:3693
Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
Definition: item_sum.h:1664
Item_sum_hybrid(Item *item_par, bool is_min)
Definition: item_sum.h:1648
Item_sum super
Definition: item_sum.h:1570
void min_max_update_temporal_field()
Definition: item_sum.cc:3712
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:2868
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1824
String * val_str(String *) override
Definition: item_sum.cc:3108
Arg_comparator * cmp
Definition: item_sum.h:1586
void min_max_update_str_field()
Definition: item_sum.cc:3749
enum Item_result result_type() const override
Definition: item_sum.h:1710
void min_max_update_decimal_field()
Definition: item_sum.cc:3796
double val_real() override
Definition: item_sum.cc:3021
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:1872
bool m_want_first
For min() - Set to true when results are ordered in ascending and false when descending.
Definition: item_sum.h:1604
Item_cache * value
Definition: item_sum.h:1585
bool m_nulls_first
Set to true if the window is ordered ascending.
Definition: item_sum.h:1592
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.h:1711
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:3121
Definition: item_sum.h:992
Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:994
Item_sum_int(THD *thd, Item_sum_int *item)
Definition: item_sum.h:1004
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:1022
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:1018
double val_real() override
Definition: item_sum.h:1012
Item_sum_int(Item *item_par)
Definition: item_sum.h:1008
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:1021
enum Item_result result_type() const override
Definition: item_sum.h:1025
String * val_str(String *str) override
Definition: item_sum.cc:1485
Item_sum_int(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:999
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1487
Implements aggregation of values into an array.
Definition: item_sum.h:1294
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:6200
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1309
~Item_sum_json_array() override
bool add() override
Definition: item_sum.cc:6278
unique_ptr_destroy_only< Json_array > m_json_array
Accumulates the final value.
Definition: item_sum.h:1296
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6327
const char * func_name() const override
Definition: item_sum.h:1308
void clear() override
Definition: item_sum.cc:6218
Implements aggregation of values into an object.
Definition: item_sum.h:1316
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:6227
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6435
bool add() override
Definition: item_sum.cc:6340
unique_ptr_destroy_only< Json_object > m_json_object
Accumulates the final value.
Definition: item_sum.h:1318
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1346
~Item_sum_json_object() override
const char * func_name() const override
Definition: item_sum.h:1345
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:6254
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:1327
void clear() override
Definition: item_sum.cc:6244
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:1335
String m_tmp_key_value
Buffer used to get the value of the key.
Definition: item_sum.h:1320
Common abstraction for Item_sum_json_array and Item_sum_json_object.
Definition: item_sum.h:1237
void reset_field() override
Definition: item_sum.cc:6160
void update_field() override
Definition: item_sum.cc:6180
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:6065
Json_constructor_null_clause m_json_constructor_null_clause
JSON constructor null clause.
Definition: item_sum.h:1248
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:6145
unique_ptr_destroy_only< Json_wrapper > m_wrapper
Wrapper around the container (object/array) which accumulates the value.
Definition: item_sum.h:1246
longlong val_int() override
Definition: item_sum.cc:6108
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:6141
String m_value
String used when reading JSON binary values or JSON text values.
Definition: item_sum.h:1242
String m_conversion_buffer
String used for converting JSON text values to utf8mb4 charset.
Definition: item_sum.h:1244
Item_sum super
Definition: item_sum.h:1238
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:6009
~Item_sum_json() override
String * val_str(String *str) override
Definition: item_sum.cc:6046
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:6123
Item_result result_type() const override
Definition: item_sum.h:1271
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.cc:6153
bool fix_fields(THD *thd, Item **pItem) override
Definition: item_sum.cc:5983
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:5978
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:6025
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:5966
double val_real() override
Definition: item_sum.cc:6093
Definition: item_sum.h:1755
Item_sum_max(Item *item_par)
Definition: item_sum.h:1757
Item_sum_max(THD *thd, const Item_sum_max *item)
Definition: item_sum.h:1760
const char * func_name() const override
Definition: item_sum.h:1763
Item_sum_max * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3180
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1762
Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1758
Definition: item_sum.h:1741
Item_sum_min(THD *thd, const Item_sum_min *item)
Definition: item_sum.h:1746
Item_sum_min(Item *item_par)
Definition: item_sum.h:1743
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1748
const char * func_name() const override
Definition: item_sum.h:1749
Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1744
Item_sum_min * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3176
Definition: item_sum.h:946
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1481
Item_sum_num(THD *thd, Item_sum_num *item)
Definition: item_sum.h:965
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1491
longlong val_int() override
Definition: item_sum.h:974
enum_field_types default_data_type() const override
Get the default data (output) type for the specific item.
Definition: item_sum.h:967
String * val_str(String *str) override
Definition: item_sum.cc:1479
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:983
Item_sum super
Definition: item_sum.h:947
Item_sum_num(Item *item_par)
Definition: item_sum.h:971
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:986
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:980
bool is_evaluated
Definition: item_sum.h:956
Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
Definition: item_sum.h:959
void reset_field() override
Definition: item_sum.cc:3375
Item_sum_num(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:962
Definition: item_sum.h:1934
Item_sum_or(THD *thd, Item_sum_or *item)
Definition: item_sum.h:1939
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3350
const char * func_name() const override
Definition: item_sum.h:1940
Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1936
Definition: item_sum.h:1547
double val_real() override
Definition: item_sum.cc:2487
Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1549
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2496
Item_sum_std(THD *thd, Item_sum_std *item)
Definition: item_sum.h:1552
const char * func_name() const override
Definition: item_sum.h:1556
enum Item_result result_type() const override
Definition: item_sum.h:1560
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1553
Item * result_item(Field *) override
Definition: item_sum.h:1555
Definition: item_sum.h:1028
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:1039
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:2004
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:1936
enum Item_result result_type() const override
Definition: item_sum.h:1073
double sum
Definition: item_sum.h:1031
bool add() override
Definition: item_sum.cc:2026
Item_result hybrid_type
Definition: item_sum.h:1030
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:1955
void reset_field() override
Definition: item_sum.cc:3495
String * val_str(String *str) override
Definition: item_sum.cc:2115
longlong val_int() override
Definition: item_sum.cc:2047
my_decimal dec_buffs[2]
Definition: item_sum.h:1032
void clear() override
Definition: item_sum.cc:1943
const char * func_name() const override
Definition: item_sum.h:1079
Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
Definition: item_sum.h:1049
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3588
Item_sum_sum(Item *item_par)
Definition: item_sum.h:1057
uint curr_dec_buff
Definition: item_sum.h:1033
ulonglong m_frame_null_count
Execution state: this is for counting NULLs of rows entering and leaving the window frame,...
Definition: item_sum.h:1046
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2121
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1957
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1064
double val_real() override
Definition: item_sum.cc:2071
Definition: item_sum.h:2108
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:2123
longlong val_int() override
Definition: item_sum.cc:4092
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:2119
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4094
Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2110
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4103
double val_real() override
Definition: item_sum.cc:4090
enum Item_result result_type() const override
Definition: item_sum.h:2126
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2122
String * val_str(String *) override
Definition: item_sum.cc:4086
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
Definition: item_sum.h:2113
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2127
Definition: item_sum.h:2013
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2033
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:2030
longlong val_int() override
Definition: item_sum.h:2019
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4082
String * val_str(String *str) override
Definition: item_sum.cc:4078
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4065
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:2026
double val_real() override
Definition: item_sum.cc:4070
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
Definition: item_sum.h:2017
Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2015
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2029
Definition: item_sum.h:2041
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2057
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
Definition: item_sum.h:2045
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2062
enum Item_result result_type() const override
Definition: item_sum.h:2061
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:2054
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4107
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:2058
String * val_str(String *str) override
Definition: item_sum.cc:4119
Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2043
longlong val_int() override
Definition: item_sum.cc:4111
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4123
double val_real() override
Definition: item_sum.h:2048
Definition: item_sum.h:2069
bool resolve_type(THD *) override
Default max_length is max argument length.
Definition: item_sum.cc:4129
double val_real() override
Definition: item_sum.h:2076
String * val_str(String *) override
Definition: item_sum.cc:4145
bool val_time(Time_val *time) override
Evaluate the item and return result as a time value.
Definition: item_sum.h:2099
enum Item_result result_type() const override
Definition: item_sum.h:2103
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:2096
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4137
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:4141
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
Definition: item_sum.h:2073
longlong val_int() override
Definition: item_sum.h:2085
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:2100
Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2071
Definition: item_sum.h:1471
ulonglong count
Definition: item_sum.h:1482
void update_field() override
Definition: item_sum.cc:2827
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2695
double val_real() override
Definition: item_sum.cc:2772
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1518
void clear() override
Definition: item_sum.cc:2749
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2715
Item * result_item(Field *) override
Definition: item_sum.h:1508
Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1492
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1501
double recurrence_s
Definition: item_sum.h:1480
bool optimize
If set, uses a algorithm II mentioned in the class description to calculate the variance which helps ...
Definition: item_sum.h:1490
const char * func_name() const override
Definition: item_sum.h:1512
bool add() override
Definition: item_sum.cc:2751
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1511
double recurrence_s2
Definition: item_sum.h:1481
Item_result hybrid_type
Definition: item_sum.h:1475
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:2679
double recurrence_m
Used in recurrence relation.
Definition: item_sum.h:1479
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2802
uint prec_increment
Definition: item_sum.h:1484
enum Item_result result_type() const override
Definition: item_sum.h:1517
void reset_field() override
Definition: item_sum.cc:2807
uint sample
Definition: item_sum.h:1483
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:2728
Definition: item_sum.h:1954
const char * func_name() const override
Definition: item_sum.h:1962
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3357
Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1956
Item_sum_xor(THD *thd, Item_sum_xor *item)
Definition: item_sum.h:1961
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:670
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:728
void aggregator_clear()
Called to cleanup the aggregator.
Definition: item_sum.h:682
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:788
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:676
bool wf_common_init()
Common initial actions for window functions.
Definition: item_sum.cc:972
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:803
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:738
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 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:797
Aggregator * aggr
Aggregator class instance.
Definition: item_sum.h:408
const Window * window() const
Definition: item_sum.h:739
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:768
virtual bool is_rollup_sum_wrapper() const
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:782
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:752
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:688
Item_sum(Item *a)
Definition: item_sum.h:525
void set_distinct(bool distinct)
Definition: item_sum.h:691
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:959
table_map used_tables() const override
Definition: item_sum.h:587
virtual Item_sum * unwrap_sum()
Non-const version.
Definition: item_sum.h:790
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:706
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:758
Item ** get_arg_ptr(uint i)
Definition: item_sum.h:656
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:1970
uint64 hash() override
Generate hash unique to an item depending on its attributes.
Definition: item_sum.cc:4057
udf_handler udf
Definition: item_sum.h:1974
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:4016
void update_field() override
Definition: item_sum.h:2006
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:4037
void reset_field() override
Definition: item_sum.h:2005
bool add() override
Definition: item_sum.cc:4031
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.h:1991
Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1977
void clear() override
Definition: item_sum.cc:4025
~Item_udf_sum() override
Definition: item_sum.h:1985
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:4046
Item_udf_sum(THD *thd, Item_udf_sum *item)
Definition: item_sum.h:1981
Item_sum super
Definition: item_sum.h:1971
const char * func_name() const override
Definition: item_sum.h:1990
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2001
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:928
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:3636
void set_nullable(bool nullable)
Definition: item.h:3748
bool get_datetime_from_numeric(Datetime_val *dt, my_time_flags_t flags)
Convert a numeric type to datetime.
Definition: item.cc:1643
DTCollation collation
Character set and collation properties assigned for this Item.
Definition: item.h:3643
bool get_date_from_int(Date_val *date, my_time_flags_t flags)
Convert val_int() to date.
Definition: item.cc:1599
void set_data_type(enum_field_types data_type)
Set the data type of the current Item.
Definition: item.h:1511
my_decimal * val_decimal_from_string(my_decimal *decimal_value)
Definition: item.cc:377
bool is_nullable() const
Definition: item.h:3747
void set_data_type_geometry()
Set the data type of the Item to be GEOMETRY.
Definition: item.h:1753
bool get_time_from_decimal(Time_val *time)
Convert val_decimal() to time.
Definition: item.cc:1715
void set_data_type_double()
Set the data type of the Item to be double precision floating point.
Definition: item.h:1577
bool get_time_from_numeric(Time_val *time)
Convert a numeric type to time.
Definition: item.cc:1746
Item_name_string item_name
Name from query.
Definition: item.h:3644
bool fixed
True if item has been resolved.
Definition: item.h:3736
virtual Item_result result_type() const
Definition: item.h:1449
bool null_value
True if item is null.
Definition: item.h:3773
Type
Definition: item.h:963
@ SUM_FUNC_ITEM
A grouped aggregate function, or window function.
Definition: item.h:967
@ AGGR_FIELD_ITEM
A special field for certain aggregate operations.
Definition: item.h:968
bool get_datetime_from_string(Datetime_val *dt, my_time_flags_t flags)
Convert val_str() to datetime.
Definition: item.cc:1555
bool get_datetime_from_int(Datetime_val *dt, my_time_flags_t flags)
Convert val_int() to datetime.
Definition: item.cc:1593
bool get_datetime_from_real(Datetime_val *dt, my_time_flags_t flags)
Convert val_real() to datetime.
Definition: item.cc:1573
virtual TYPELIB * get_typelib() const
Get the typelib information for an item of type set or enum.
Definition: item.h:1820
my_decimal * val_decimal_from_real(my_decimal *decimal_value)
Definition: item.cc:362
bool get_date_from_real(Date_val *date, my_time_flags_t flags)
Convert val_real() to date.
Definition: item.cc:1567
bool get_time_from_int(Time_val *time)
Convert val_int() to time.
Definition: item.cc:1722
bool unsigned_flag
Definition: item.h:3774
bool get_date_from_string(Date_val *date, my_time_flags_t flags)
Convert val_str() to date.
Definition: item.cc:1563
longlong val_int_from_string()
Definition: item.cc:516
void set_grouping_func()
Set the property: this item is a call to GROUPING.
Definition: item.h:3553
bool get_time_from_real(Time_val *time)
Convert val_real() to time.
Definition: item.cc:1709
virtual bool mark_field_in_map(uchar *arg)
Mark underlying field in read or write map of a table.
Definition: item.h:2886
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:3784
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:1797
bool get_datetime_from_decimal(Datetime_val *dt, my_time_flags_t flags)
Convert val_decimal() to datetime.
Definition: item.cc:1586
bool get_date_from_numeric(Date_val *date, my_time_flags_t flags)
Convert a numeric type to date.
Definition: item.cc:1627
bool get_date_from_decimal(Date_val *date, my_time_flags_t flags)
Convert val_decimal() to date.
Definition: item.cc:1579
void set_data_type_longlong()
Set the data type of the Item to be longlong.
Definition: item.h:1553
double val_real_from_string()
Definition: item.cc:468
bool update_null_value()
Make sure the null_value member has a correct value.
Definition: item.cc:7876
bool get_time_from_string(Time_val *time)
Convert val_str() to time.
Definition: item.cc:1701
String * val_string_from_real(String *str)
Definition: item.cc:291
Represents a JSON array container, i.e.
Definition: json_dom.h:519
Represents a JSON container value of type "object" (ECMA), type J_OBJECT here.
Definition: json_dom.h:373
bool add_alias(std::string_view key, Json_dom *value)
Insert the value into the object.
Definition: json_dom.h:415
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1225
Definition: sql_list.h:494
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:432
Wrapper class for an Item list head, used to allocate Item lists in the parser in a context-independe...
Definition: parse_tree_helpers.h:109
Definition: parse_tree_nodes.h:234
Parse tree node for a window; just a shallow wrapper for class Window, q.v.
Definition: parse_tree_window.h:39
void error(Context *pc, const POS &pos) const
syntax_error() function replacement for deferred reporting of syntax errors
Definition: parse_tree_node_base.h:346
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1179
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:169
const CHARSET_INFO * charset() const
Definition: sql_string.h:242
const char * ptr() const
Definition: sql_string.h:251
size_t length() const
Definition: sql_string.h:243
void set(String &str, size_t offset, size_t arg_length)
Definition: sql_string.h:304
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:97
Time_val is a temporal type that represents only time.
Definition: my_temporal.h:55
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:111
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:4687
void free_handler()
Definition: item_func.cc:4668
enum_query_type
Query type constants (usable as bitmap flags).
Definition: enum_query_type.h:31
This file contains the field type.
enum_field_types
Column types for MySQL Note: Keep include/mysql/components/services/bits/stored_program_bits....
Definition: field_types.h:55
@ MYSQL_TYPE_LONGLONG
Definition: field_types.h:64
@ MYSQL_TYPE_NEWDECIMAL
Definition: field_types.h:81
@ MYSQL_TYPE_DOUBLE
Definition: field_types.h:61
static const std::string dec("DECRYPTION")
This file declares the coordinate system specific subclasses of the geometry class hierarchy.
void my_error(int nr, myf MyFlags,...)
Fill in and print a previously registered error message.
Definition: my_error.cc:217
static int flags[50]
Definition: hp_test1.cc:40
static uint16 key1[1001]
Definition: hp_test2.cc:50
int dump_leaf_key(void *key_arg, element_count count, void *item_arg)
Append data from current leaf to item->result.
Definition: item_sum.cc:4260
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:4181
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:4217
Json_constructor_null_clause
Definition: item_sum.h:1234
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 number_to_datetime.
Definition: my_time.h:85
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:1589
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1078
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
Definition: commit_order_queue.h:34
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
ValueType value(const std::optional< ValueType > &v)
Definition: gtid.h:83
size_t size(const char *const c)
Definition: base64.h:46
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
Cursor end()
A past-the-end Cursor.
Definition: rules_table_service.cc:192
std::list< T, ut::allocator< T > > list
Specialization of list which uses ut_allocator.
Definition: ut0new.h:2884
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
File containing constants that can be used throughout the server.
constexpr const size_t STRING_BUFFER_USUAL_SIZE
Definition: sql_const.h:126
Our own string classes, used pervasively throughout the executor.
Definition: m_ctype.h:421
Struct used to pass around arguments to/from check_function_as_value_generator.
Definition: item.h:486
int err_code
the error code found during check(if any)
Definition: item.h:493
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:505
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:413
Definition: table.h:298
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:422
Definition: table.h:1435
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...