MySQL 9.4.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 bool eq(const Item *item) const override;
604 bool eq_specific(const Item *item) const override;
605 /**
606 Mark an aggregate as having no rows.
607
608 This function is called by the execution engine to assign 'NO ROWS
609 FOUND' value to an aggregate item, when the underlying result set
610 has no rows. Such value, in a general case, may be different from
611 the default value of the item after 'clear()': e.g. a numeric item
612 may be initialized to 0 by clear() and to NULL by
613 no_rows_in_result().
614 */
615 void no_rows_in_result() override {
619 }
620 virtual void make_unique() { force_copy_fields = true; }
621 virtual Field *create_tmp_field(bool group, TABLE *table);
622
623 /// argument used by walk method collect_grouped_aggregates ("cga")
625 /// accumulated all aggregates found
626 std::vector<Item_sum *> list;
627 std::set<Item_sum *> aggregates_that_were_hidden;
628 /**
629 The query block we walk from. All found aggregates must aggregate in
630 this; if some aggregate in outer query blocks, break off transformation.
631 */
633 /// true: break off transformation
634 bool m_break_off{false};
635 /// true: an aggregate aggregates outside m_query_block
636 bool m_outside{false};
638 : m_query_block(select) {}
639 };
640
641 bool collect_grouped_aggregates(uchar *) override;
642 Item *replace_aggregate(uchar *) override;
643 bool collect_scalar_subqueries(uchar *) override;
645
646 bool clean_up_after_removal(uchar *arg) override;
647 bool aggregate_check_group(uchar *arg) override;
648 bool aggregate_check_distinct(uchar *arg) override;
649 bool has_aggregate_ref_in_group_by(uchar *arg) override;
650 bool init_sum_func_check(THD *thd);
651 bool check_sum_func(THD *thd, Item **ref);
652
653 Item *set_arg(THD *thd, uint i, Item *new_val) override;
654 /// @todo delete this when we no longer support temporary transformations
655 Item **get_arg_ptr(uint i) { return &args[i]; }
656
657 bool fix_fields(THD *thd, Item **ref) override;
658
659 /**
660 Signal to the function that its arguments may have changed,
661 and that any internal caches etc. based on those arguments
662 must be updated accordingly.
663
664 This is used by the hypergraph optimizer when it rewrites
665 arguments to window functions to take into account that they
666 have been materialized into temporary tables, or that they
667 should read their values from the framebuffer.
668 */
670
671 /**
672 Called to initialize the aggregator.
673 */
674
675 virtual bool aggregator_setup(THD *thd) { return aggr->setup(thd); }
676
677 /**
678 Called to cleanup the aggregator.
679 */
680
681 inline void aggregator_clear() { aggr->clear(); }
682
683 /**
684 Called to add value to the aggregator.
685 */
686
687 inline bool aggregator_add() { return aggr->add(); }
688
689 /* stores the declared DISTINCT flag (from the parser) */
690 void set_distinct(bool distinct) {
691 with_distinct = distinct;
693 }
694
695 /*
696 Set the type of aggregation : DISTINCT or not.
697
698 May be called multiple times.
699 */
700
701 virtual int set_aggregator(Aggregator::Aggregator_type aggregator);
702
703 virtual void clear() = 0;
704 virtual bool add() = 0;
705 virtual bool setup(THD *) { return false; }
706
707 /**
708 Only relevant for aggregates qua window functions. Checks semantics after
709 windows have been set up and checked. Window functions have specific
710 requirements on the window specifications. Used at resolution time.
711
712 @param thd Current thread
713 @param select The current select
714 @param [out] reqs Holds collected requirements from this wf
715
716 @returns true if error
717 */
718 virtual bool check_wf_semantics1(THD *thd, Query_block *select,
720
721 /**
722 Like check_wf_semantics1.
723 For checks which cannot be done in resolution phase (mostly those for
724 input parameters which can be '?' and must be >=0: value isn't known
725 before execution phase).
726 */
728 [[maybe_unused]]) {
729 return false;
730 }
731
732 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
733 mem_root_deque<Item *> *fields) override;
734
735 void cleanup() override;
736
737 Window *window() { return m_window; }
738 const Window *window() const { return m_window; }
739 bool reset_wf_state(uchar *arg) override;
740
741 /**
742 All aggregates are framing, i.e. they work on the window's frame. If none
743 is defined, the frame is by default the entire partition, unless ORDER BY
744 is defined, in which case it is the set of rows from the start of the
745 partition to and including the peer set of the current row.
746
747 Some window functions are not framing, i.e. they always work on the entire
748 partition. For such window functions, the method is overridden to
749 return false.
750 */
751 virtual bool framing() const { return true; }
752
753 /**
754 Only for framing window functions. True if this function only needs to
755 read one row per frame.
756 */
757 virtual bool uses_only_one_row() const { return false; }
758
759 /**
760 Return true if we need to make two passes over the rows in the partition -
761 either because we need the cardinality of it (and we need to read all
762 rows to detect the next partition), or we need to have all partition rows
763 available to evaluate the window function for some other reason, e.g.
764 we may need the last row in the partition in the frame buffer to be able
765 to evaluate LEAD.
766 */
767 virtual bool needs_partition_cardinality() const { return false; }
768
769 /**
770 Common initial actions for window functions. For non-buffered processing
771 ("on-the-fly"), check partition change and possible reset partition
772 state. In this case return false.
773 For buffered processing, if windowing state m_do_copy_null is true, set
774 null_value to is_nullable() and return true.
775
776 @return true if case two above holds, else false
777 */
778 bool wf_common_init();
779
780 /// Overridden by Item_rollup_sum_switcher.
781 virtual bool is_rollup_sum_wrapper() const { return false; }
782 /**
783 * In case we are an Item_rollup_sum_switcher,
784 * return the underlying Item_sum, otherwise, return this.
785 * Overridden by Item_rollup_sum_switcher.
786 */
787 virtual const Item_sum *unwrap_sum() const { return this; }
788 /// Non-const version
789 virtual Item_sum *unwrap_sum() { return this; }
790
791 protected:
792 /*
793 Raise an error (ER_NOT_SUPPORTED_YET) with the detail that this
794 function is not yet supported as a window function.
795 */
797 char buff[STRING_BUFFER_USUAL_SIZE];
798 snprintf(buff, sizeof(buff), "%s as window function", func_name());
799 my_error(ER_NOT_SUPPORTED_YET, MYF(0), buff);
800 }
801
802 void add_json_info(Json_object *obj) override {
803 obj->add_alias("distinct", create_dom_ptr<Json_boolean>(with_distinct));
804 }
805};
806
807class Unique;
808
809/**
810 The distinct aggregator.
811 Implements AGGFN (DISTINCT ..)
812 Collects all the data into an Unique (similarly to what Item_sum_distinct
813 does currently) and then (if applicable) iterates over the list of
814 unique values and pumps them back into its object
815*/
816
818 friend class Item_sum_sum;
819
820 /*
821 flag to prevent consecutive runs of endup(). Normally in endup there are
822 expensive calculations (like walking the distinct tree for example)
823 which we must do only once if there are no data changes.
824 We can re-use the data for the second and subsequent val_xxx() calls.
825 endup_done set to true also means that the calculated values for
826 the aggregate functions are correct and don't need recalculation.
827 */
829
830 /*
831 Used depending on the type of the aggregate function and the presence of
832 blob columns in it:
833 - For COUNT(DISTINCT) and no blob fields this points to a real temporary
834 table. It's used as a hash table.
835 - For AVG/SUM(DISTINCT) or COUNT(DISTINCT) with blob fields only the
836 in-memory data structure of a temporary table is constructed.
837 It's used by the Field classes to transform data into row format.
838 */
840
841 /*
842 An array of field lengths on row allocated and used only for
843 COUNT(DISTINCT) with multiple columns and no blobs. Used in
844 Aggregator_distinct::composite_key_cmp (called from Unique to compare
845 nodes
846 */
848
849 /*
850 Used in conjunction with 'table' to support the access to Field classes
851 for COUNT(DISTINCT). Needed by copy_fields()/copy_funcs().
852 */
854
855 /*
856 If there are no blobs in the COUNT(DISTINCT) arguments, we can use a tree,
857 which is faster than heap table. In that case, we still use the table
858 to help get things set up, but we insert nothing in it.
859 For AVG/SUM(DISTINCT) we always use this tree (as it takes a single
860 argument) to get the distinct rows.
861 */
863
864 /*
865 The length of the temp table row. Must be a member of the class as it
866 gets passed down to simple_raw_key_cmp () as a compare function argument
867 to Unique. simple_raw_key_cmp () is used as a fast comparison function
868 when the entire row can be binary compared.
869 */
871
874 /**
875 Set to true if the result is known to be always NULL.
876 If set deactivates creation and usage of the temporary table (in the
877 'table' member) and the Unique instance (in the 'tree' member) as well as
878 the calculation of the final value on the first call to
879 @c Item_sum::val_xxx(),
880 @c Item_avg::val_xxx(),
881 @c Item_count::val_xxx().
882 */
884 /**
885 Set to true if count distinct is on only const items. Distinct on a const
886 value will always be the constant itself. And count distinct of the same
887 would always be 1. Similar to CONST_NULL, it avoids creation of temporary
888 table and the Unique instance.
889 */
892
893 /**
894 When feeding back the data in endup() from Unique/temp table back to
895 Item_sum::add() methods we must read the data from Unique (and not
896 recalculate the functions that are given as arguments to the aggregate
897 function.
898 This flag is to tell the arg_*() methods to take the data from the Unique
899 instead of calling the relevant val_..() method.
900 */
902
903 public:
905 : Aggregator(sum),
906 table(nullptr),
908 tree(nullptr),
910 use_distinct_values(false) {}
911 ~Aggregator_distinct() override;
913
914 bool setup(THD *) override;
915 void clear() override;
916 bool add() override;
917 void endup() override;
919 double arg_val_real() override;
920 bool arg_is_null(bool use_null_value) override;
921
922 bool unique_walk_function(void *element);
923 static int composite_key_cmp(const void *arg, const void *a, const void *b);
924};
925
926/**
927 The pass-through aggregator.
928 Implements AGGFN (DISTINCT ..) by knowing it gets distinct data on input.
929 So it just pumps them back to the Item_sum descendant class.
930*/
932 public:
935
936 bool setup(THD *thd) override { return item_sum->setup(thd); }
937 void clear() override { item_sum->clear(); }
938 bool add() override { return item_sum->add(); }
939 void endup() override {}
941 double arg_val_real() override;
942 bool arg_is_null(bool use_null_value) override;
943};
944
945class Item_sum_num : public Item_sum {
947
948 protected:
949 /*
950 val_xxx() functions may be called several times during the execution of a
951 query. Derived classes that require extensive calculation in val_xxx()
952 maintain cache of aggregate value. This variable governs the validity of
953 that cache.
954 */
956
957 public:
958 Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
959 : Item_sum(pos, item_par, window), is_evaluated(false) {}
960
962 : Item_sum(pos, list, w), is_evaluated(false) {}
963
965 : Item_sum(thd, item), is_evaluated(item->is_evaluated) {}
967 return MYSQL_TYPE_DOUBLE;
968 }
969
970 Item_sum_num(Item *item_par) : Item_sum(item_par), is_evaluated(false) {}
971
972 bool fix_fields(THD *, Item **) override;
973 longlong val_int() override {
974 assert(fixed);
975 return llrint_with_overflow_check(val_real()); /* Real as default */
976 }
977 String *val_str(String *str) override;
978 my_decimal *val_decimal(my_decimal *) override;
979 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
980 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
981 }
982 bool get_time(MYSQL_TIME *ltime) override {
983 return get_time_from_numeric(ltime); /* Decimal or real */
984 }
985 void reset_field() override;
986};
987
989 public:
990 Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
991 : Item_sum_num(pos, item_par, w) {
993 }
994
996 : Item_sum_num(pos, list, w) {
998 }
999
1000 Item_sum_int(THD *thd, Item_sum_int *item) : Item_sum_num(thd, item) {
1002 }
1003
1004 Item_sum_int(Item *item_par) : Item_sum_num(item_par) {
1006 }
1007
1008 double val_real() override {
1009 assert(fixed);
1010 return static_cast<double>(val_int());
1011 }
1012 String *val_str(String *str) override;
1013 my_decimal *val_decimal(my_decimal *) override;
1014 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1015 return get_date_from_int(ltime, fuzzydate);
1016 }
1017 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
1018 enum Item_result result_type() const override { return INT_RESULT; }
1019};
1020
1022 protected:
1024 double sum;
1027 bool resolve_type(THD *thd) override;
1028 /**
1029 Execution state: this is for counting rows entering and leaving the window
1030 frame, see #m_frame_null_count.
1031 */
1033
1034 /**
1035 Execution state: this is for counting NULLs of rows entering and leaving
1036 the window frame, when we use optimized inverse-based computations. By
1037 comparison with m_count we can know how many non-NULLs are in the frame.
1038 */
1040
1041 public:
1042 Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
1043 : Item_sum_num(pos, item_par, window),
1045 m_count(0),
1047 set_distinct(distinct);
1048 }
1049
1051 : Item_sum_num(item_par),
1053 m_count(0),
1054 m_frame_null_count(0) {}
1055
1056 Item_sum_sum(THD *thd, Item_sum_sum *item);
1057 enum Sumfunctype sum_func() const override {
1059 }
1060 void clear() override;
1061 bool add() override;
1062 double val_real() override;
1063 longlong val_int() override;
1064 String *val_str(String *str) override;
1065 my_decimal *val_decimal(my_decimal *) override;
1066 enum Item_result result_type() const override { return hybrid_type; }
1067 bool check_wf_semantics1(THD *thd, Query_block *select,
1068 Window_evaluation_requirements *reqs) override;
1069 void no_rows_in_result() override;
1070 void reset_field() override;
1071 void update_field() override;
1072 const char *func_name() const override { return "sum"; }
1073 Item *copy_or_same(THD *thd) override;
1074};
1075
1078
1080
1081 void clear() override;
1082 bool add() override;
1083 void cleanup() override;
1084
1085 public:
1086 Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
1087 : Item_sum_int(pos, item_par, w), count(0) {}
1088 Item_sum_count(Item_int *number) : Item_sum_int(number), count(0) {}
1089 Item_sum_count(Item *item, bool distinct) : Item_sum_int(item), count(0) {
1090 set_distinct(distinct);
1091 }
1092 /**
1093 Constructs an instance for COUNT(DISTINCT)
1094
1095 @param pos Position of token in the parser.
1096 @param list A list of the arguments to the aggregate function
1097 @param w A window, if COUNT is used as a windowing function
1098
1099 This constructor is called by the parser only for COUNT (DISTINCT).
1100 */
1102 : Item_sum_int(pos, list, w), count(0) {
1103 set_distinct(true);
1104 }
1106 : Item_sum_int(thd, item), count(item->count) {}
1107 enum Sumfunctype sum_func() const override {
1109 }
1110 bool resolve_type(THD *thd) override {
1111 if (param_type_is_default(thd, 0, -1)) return true;
1112 set_nullable(false);
1113 null_value = false;
1114 return false;
1115 }
1116 void no_rows_in_result() override { count = 0; }
1117 void make_const(longlong count_arg) {
1118 count = count_arg;
1120 }
1121 longlong val_int() override;
1122 void reset_field() override;
1123 void update_field() override;
1124 const char *func_name() const override { return "count"; }
1125 Item *copy_or_same(THD *thd) override;
1126};
1127
1128/* Item to get the value of a stored sum function */
1129
1130class Item_sum_avg;
1131class Item_sum_bit;
1132
1133/**
1134 This is used in connection with a parent aggregate Item:
1135 - which stores function's value into a temporary table's column (one row
1136 per group).
1137 - except when the output is a local variable in a stored procedure, in which
1138 case the variable is used as the target.
1139 - which stores in the column some internal piece of information which should
1140 not be returned to the user, so special implementations are needed.
1141 The classes that inherit from Item_aggregate_field are created during
1142 optimization and resolved upon construction, thus the fix_fields() function
1143 is not needed and thus not implemented. The resolve_type() needs a default
1144 implementation since it is a virtual function.
1145*/
1147 protected:
1148 /// The tmp table's column containing the value of the set function.
1149 Field *m_field{nullptr};
1150 /// Stores the Item's result type.
1152
1153 public:
1154 enum Item_result result_type() const override { return m_result_type; }
1155 // resolve_type is not used for these classes, but is needed bc it is virtual
1156 bool resolve_type(THD *) override { return false; }
1157 bool mark_field_in_map(uchar *arg) override {
1158 /*
1159 Filesort (find_all_keys) over a temporary table collects the columns it
1160 needs.
1161 */
1162 return Item::mark_field_in_map(pointer_cast<Mark_field *>(arg), m_field);
1163 }
1166 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1167 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1168 return true;
1169 }
1170};
1171
1172/**
1173 Common abstract class for aggregate field classes that return numeric values:
1174 Item_aggr_avg_field
1175 Item_aggr_variance_field
1176*/
1178 public:
1179 longlong val_int() override {
1180 /* can't be fix_fields()ed */
1182 }
1183 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1184 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
1185 }
1186 bool get_time(MYSQL_TIME *ltime) override {
1187 return get_time_from_numeric(ltime); /* Decimal or real */
1188 }
1189 bool is_null() override { return update_null_value() || null_value; }
1190};
1191
1193 public:
1195 enum Type type() const override { return AGGR_FIELD_ITEM; }
1196 double val_real() override;
1197 my_decimal *val_decimal(my_decimal *) override;
1198 String *val_str(String *) override;
1199
1200 private:
1205};
1206
1207/// This is used in connection with an Item_sum_bit, @see Item_aggregate_field
1209 public:
1210 Item_aggr_bit_field(Item_sum_bit *item, ulonglong reset_bits);
1211 longlong val_int() override;
1212 double val_real() override;
1213 my_decimal *val_decimal(my_decimal *) override;
1214 String *val_str(String *) override;
1215 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1216 bool get_time(MYSQL_TIME *ltime) override;
1217 enum Type type() const override { return AGGR_FIELD_ITEM; }
1218
1219 private:
1221};
1222
1224
1225/// Common abstraction for Item_sum_json_array and Item_sum_json_object
1226class Item_sum_json : public Item_sum {
1228
1229 protected:
1230 /// String used when reading JSON binary values or JSON text values.
1232 /// String used for converting JSON text values to utf8mb4 charset.
1234 /// Wrapper around the container (object/array) which accumulates the value.
1236 /// JSON constructor null clause
1239
1240 /**
1241 Construct an Item_sum_json instance.
1242
1243 @param wrapper a wrapper around the Json_array or Json_object that contains
1244 the aggregated result
1245 @param json_constructor_null_clause Specifies the behavior for
1246 handling NULL values in JSON
1247 constructors
1248 i.e, NULL_ON_NULL and ABSENT_ON_NULL
1249 @param parent_args arguments to forward to Item_sum's constructor
1250 */
1251 template <typename... Args>
1252 explicit Item_sum_json(
1254 Json_constructor_null_clause json_constructor_null_clause,
1255 Args &&...parent_args);
1256
1257 public:
1258 ~Item_sum_json() override;
1259 bool fix_fields(THD *thd, Item **pItem) override;
1260 Item_result result_type() const override { return STRING_RESULT; }
1261
1262 bool do_itemize(Parse_context *pc, Item **res) override;
1263
1264 double val_real() override;
1265 longlong val_int() override;
1266 String *val_str(String *str) override;
1267 bool val_json(Json_wrapper *wr) override;
1268 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
1269 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1270 bool get_time(MYSQL_TIME *ltime) override;
1271
1272 void reset_field() override;
1273 void update_field() override;
1274
1275 void print(const THD *thd, String *str,
1276 enum_query_type query_type) const override;
1277
1280};
1281
1282/// Implements aggregation of values into an array.
1284 /// Accumulates the final value.
1286
1287 public:
1288 Item_sum_json_array(THD *thd, Item_sum *item,
1291 Item_sum_json_array(const POS &pos, Item *a,
1292 Json_constructor_null_clause json_constructor_null_clause,
1293 PT_window *w,
1297 const char *func_name() const override { return "json_arrayagg"; }
1298 enum Sumfunctype sum_func() const override { return JSON_ARRAYAGG_FUNC; }
1299 void clear() override;
1300 bool add() override;
1301 Item *copy_or_same(THD *thd) override;
1302};
1303
1304/// Implements aggregation of values into an object.
1306 /// Accumulates the final value.
1308 /// Buffer used to get the value of the key.
1310 /**
1311 Map of keys in Json_object and the count for each key
1312 within a window frame. It is used in handling rows
1313 leaving a window frame when rows are not sorted
1314 according to the key in Json_object.
1315 */
1316 std::map<std::string, int> m_key_map;
1317 /**
1318 If window provides ordering on the key in Json_object,
1319 a key_map is not needed to handle rows leaving a window
1320 frame. In this case, process_buffered_windowing_record()
1321 will set flags when a key/value pair can be removed from
1322 the Json_object.
1323 */
1324 bool m_optimize{false};
1325
1326 public:
1327 Item_sum_json_object(THD *thd, Item_sum *item,
1330 Item_sum_json_object(const POS &pos, Item *a, Item *b, PT_window *w,
1334 const char *func_name() const override { return "json_objectagg"; }
1335 enum Sumfunctype sum_func() const override { return JSON_OBJECTAGG_FUNC; }
1336 void clear() override;
1337 bool add() override;
1338 Item *copy_or_same(THD *thd) override;
1339 bool check_wf_semantics1(THD *thd, Query_block *select,
1340 Window_evaluation_requirements *reqs) override;
1341};
1342
1343class Item_sum_avg final : public Item_sum_sum {
1344 public:
1349 double m_avg;
1350
1351 Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
1352 : Item_sum_sum(pos, item_par, distinct, w) {}
1353
1355 : Item_sum_sum(thd, item), prec_increment(item->prec_increment) {}
1356
1357 bool resolve_type(THD *thd) override;
1358 enum Sumfunctype sum_func() const override {
1360 }
1361 void clear() override;
1362 bool add() override;
1363 double val_real() override;
1364 // In SPs we might force the "wrong" type with select into a declare variable
1366 my_decimal *val_decimal(my_decimal *) override;
1367 String *val_str(String *str) override;
1368 void reset_field() override;
1369 void update_field() override;
1370 Item *result_item(Field *) override { return new Item_aggr_avg_field(this); }
1371 const char *func_name() const override { return "avg"; }
1372 Item *copy_or_same(THD *thd) override;
1373 Field *create_tmp_field(bool group, TABLE *table) override;
1374 void cleanup() override {
1375 m_count = 0;
1378 }
1379};
1380
1381class Item_sum_variance;
1382
1384 public:
1386 enum Type type() const override { return AGGR_FIELD_ITEM; }
1387 double val_real() override;
1389 my_decimal *val_decimal(my_decimal *dec_buf) override {
1390 return val_decimal_from_real(dec_buf);
1391 }
1394 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1395 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1396 return true;
1397 }
1398
1399 private:
1401};
1402
1403/*
1404 variance(a) =
1405
1406 = sum (ai - avg(a))^2 / count(a) )
1407 = sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
1408 = (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
1409 = (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
1410 = (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2
1411 )/count(a) = = (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a)
1412 )/count(a) = = (sum(ai^2) - sum(a)^2/count(a))/count(a)
1413
1414 But, this falls prey to catastrophic cancellation.
1415 Instead, we use recurrence formulas in Algorithm I mentoned below
1416 for group aggregates.
1417
1418 Algorithm I:
1419 M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
1420 S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
1421 for 2 <= k <= n newline
1422 ital variance = S_{n} / (n-1)
1423
1424 For aggregate window functions algorithm I cannot be optimized for
1425 moving frames since M_{i} changes for every row. So we use the
1426 following algorithm.
1427
1428 Algorithm II:
1429
1430 K = 0
1431 n = 0
1432 ex = 0
1433 ex2 = 0
1434
1435 def add_sample(x):
1436 if (n == 0):
1437 K = x
1438 n = n + 1
1439 ex += x - K
1440 ex2 += (x - K) * (x - K)
1441
1442 def remove_sample(x):
1443 n = n - 1
1444 ex -= (x - K)
1445 ex2 -= (x - K) * (x - K)
1446
1447 def variance():
1448 return (ex2 - (ex*ex)/n) / (n-1)
1449
1450 This formula facilitates incremental computation enabling us to
1451 optimize in case of moving window frames. The optimized codepath is taken
1452 only when windowing_use_high_precision is set to false. By default,
1453 aggregate window functions take the non-optimized codepath.
1454 Note:
1455 Results could differ between optimized and non-optimized code path.
1456 Hence algorithm II is used only when user sets
1457 windowing_use_high_precision to false.
1458*/
1459
1461 bool resolve_type(THD *) override;
1462
1463 public:
1465 /**
1466 Used in recurrence relation.
1467 */
1468 double recurrence_m{0.0};
1469 double recurrence_s{0.0};
1470 double recurrence_s2{0.0};
1474 /**
1475 If set, uses a algorithm II mentioned in the class description
1476 to calculate the variance which helps in optimizing windowing
1477 functions in presence of frames.
1478 */
1480
1481 Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg,
1482 PT_window *w)
1483 : Item_sum_num(pos, item_par, w),
1485 count(0),
1486 sample(sample_arg),
1487 optimize(false) {}
1488
1490 enum Sumfunctype sum_func() const override { return VARIANCE_FUNC; }
1491 void clear() override;
1492 bool add() override;
1493 double val_real() override;
1494 my_decimal *val_decimal(my_decimal *) override;
1495 void reset_field() override;
1496 void update_field() override;
1497 Item *result_item(Field *) override {
1498 return new Item_aggr_variance_field(this);
1499 }
1500 void no_rows_in_result() override {}
1501 const char *func_name() const override {
1502 return sample ? "var_samp" : "variance";
1503 }
1504 Item *copy_or_same(THD *thd) override;
1505 Field *create_tmp_field(bool group, TABLE *table) override;
1506 enum Item_result result_type() const override { return REAL_RESULT; }
1507 void cleanup() override {
1508 count = 0;
1510 }
1511 bool check_wf_semantics1(THD *thd, Query_block *select,
1512 Window_evaluation_requirements *reqs) override;
1513};
1514
1515class Item_sum_std;
1516
1518 public:
1520 enum Type type() const override { return AGGR_FIELD_ITEM; }
1521 double val_real() override;
1522 my_decimal *val_decimal(my_decimal *) override;
1523 enum Item_result result_type() const override { return REAL_RESULT; }
1526 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1527 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1528 return true;
1529 }
1530};
1531
1532/*
1533 standard_deviation(a) = sqrt(variance(a))
1534*/
1535
1537 public:
1538 Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
1539 : Item_sum_variance(pos, item_par, sample_arg, w) {}
1540
1541 Item_sum_std(THD *thd, Item_sum_std *item) : Item_sum_variance(thd, item) {}
1542 enum Sumfunctype sum_func() const override { return STD_FUNC; }
1543 double val_real() override;
1544 Item *result_item(Field *) override { return new Item_aggr_std_field(this); }
1545 const char *func_name() const override {
1546 return sample ? "stddev_samp" : "std";
1547 }
1548 Item *copy_or_same(THD *thd) override;
1549 enum Item_result result_type() const override { return REAL_RESULT; }
1550};
1551
1552// This class is a string or number function depending on num_func
1553class Arg_comparator;
1554
1555/**
1556 Abstract base class for the MIN and MAX aggregate functions.
1557*/
1560
1561 private:
1562 /**
1563 Tells if this is the MIN function (true) or the MAX function (false).
1564 */
1565 const bool m_is_min;
1566 /*
1567 For window functions MIN/MAX with optimized code path, no comparisons
1568 are needed beyond NULL detection: MIN/MAX are then roughly equivalent to
1569 FIRST/LAST_VALUE. For this case, 'value' is the value of
1570 the window function a priori taken from args[0], while arg_cache is used to
1571 remember the value from the previous row. NULLs need a bit of careful
1572 treatment.
1573 */
1577 bool m_has_values; // Set if at least one row is found (for max/min only)
1578 /**
1579 Set to true if the window is ordered ascending.
1580 */
1582 /**
1583 Set to true when min/max can be optimized using window's ordering.
1584 */
1586 /**
1587 For min() - Set to true when results are ordered in ascending and
1588 false when descending.
1589 For max() - Set to true when results are ordered in descending and
1590 false when ascending.
1591 Valid only when m_optimize is true.
1592 */
1593 bool m_want_first; ///< Want first non-null value, else last non_null value
1594 /**
1595 Execution state: keeps track if this is the first row in the frame
1596 when buffering is not needed.
1597 Valid only when m_optimize is true.
1598 */
1600
1601 /**
1602 Execution state: keeps track of at which row we saved a non-null last
1603 value.
1604 */
1606
1607 /**
1608 This function implements the optimized version of retrieving min/max
1609 value. When we have "ordered ASC" results in a window, min will always
1610 be the first value in the result set (neglecting the NULL's) and max
1611 will always be the last value (or the other way around, if ordered DESC).
1612 It is based on the implementation of FIRST_VALUE/LAST_VALUE, except for
1613 the NULL handling.
1614
1615 @return true if computation yielded a NULL or error
1616 */
1617 bool compute();
1618
1619 /**
1620 MIN/MAX function setup.
1621
1622 Setup cache/comparator of MIN/MAX functions. When called by the
1623 copy_or_same() function, the value_arg parameter contains the calculated
1624 value of the original MIN/MAX object, and it is saved in this object's
1625 cache.
1626
1627 @param item the argument of the MIN/MAX function
1628 @param value_arg the calculated value of the MIN/MAX function
1629 @return false on success, true on error
1630 */
1631 bool setup_hybrid(Item *item, Item *value_arg);
1632
1633 /** Create a clone of this object. */
1634 virtual Item_sum_hybrid *clone_hybrid(THD *thd) const = 0;
1635
1636 protected:
1637 Item_sum_hybrid(Item *item_par, bool is_min)
1638 : Item_sum(item_par),
1639 m_is_min(is_min),
1640 value(nullptr),
1642 cmp(nullptr),
1644 m_has_values(true),
1645 m_nulls_first(false),
1646 m_optimize(false),
1647 m_want_first(false),
1648 m_cnt(0),
1651 }
1652
1653 Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
1654 : Item_sum(pos, item_par, w),
1655 m_is_min(is_min),
1656 value(nullptr),
1658 cmp(nullptr),
1660 m_has_values(true),
1661 m_nulls_first(false),
1662 m_optimize(false),
1663 m_want_first(false),
1664 m_cnt(0),
1667 }
1668
1670 : Item_sum(thd, item),
1671 m_is_min(item->m_is_min),
1672 value(item->value),
1674 hybrid_type(item->hybrid_type),
1677 m_optimize(item->m_optimize),
1679 m_cnt(item->m_cnt),
1681
1682 public:
1683 bool fix_fields(THD *, Item **) override;
1684 void clear() override;
1685 void update_after_wf_arguments_changed(THD *thd) override;
1686 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
1687 mem_root_deque<Item *> *fields) override;
1688 double val_real() override;
1689 longlong val_int() override;
1690 longlong val_time_temporal() override;
1691 longlong val_date_temporal() override;
1692 my_decimal *val_decimal(my_decimal *) override;
1693 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1694 bool get_time(MYSQL_TIME *ltime) override;
1695 void reset_field() override;
1696 String *val_str(String *) override;
1697 bool val_json(Json_wrapper *wr) override;
1698 bool keep_field_type() const override { return true; }
1699 enum Item_result result_type() const override { return hybrid_type; }
1700 TYPELIB *get_typelib() const override {
1701 assert(sum_func() == MAX_FUNC || sum_func() == MIN_FUNC);
1702 return arguments()[0]->get_typelib();
1703 }
1704 void update_field() override;
1705 void cleanup() override;
1706 bool has_values() { return m_has_values; }
1707 void no_rows_in_result() override;
1708 Field *create_tmp_field(bool group, TABLE *table) override;
1709 bool uses_only_one_row() const override { return m_optimize; }
1710 bool add() override;
1711 Item *copy_or_same(THD *thd) override;
1712 bool check_wf_semantics1(THD *thd, Query_block *select,
1714
1715 private:
1716 /*
1717 These functions check if the value on the current row exceeds the maximum or
1718 minimum value seen so far, and update the current max/min stored in
1719 result_field, if needed.
1720 */
1727};
1728
1729class Item_sum_min final : public Item_sum_hybrid {
1730 public:
1731 Item_sum_min(Item *item_par) : Item_sum_hybrid(item_par, true) {}
1732 Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
1733 : Item_sum_hybrid(pos, item_par, true, w) {}
1734 Item_sum_min(THD *thd, const Item_sum_min *item)
1735 : Item_sum_hybrid(thd, item) {}
1736 enum Sumfunctype sum_func() const override { return MIN_FUNC; }
1737 const char *func_name() const override { return "min"; }
1738
1739 private:
1740 Item_sum_min *clone_hybrid(THD *thd) const override;
1741};
1742
1743class Item_sum_max final : public Item_sum_hybrid {
1744 public:
1745 Item_sum_max(Item *item_par) : Item_sum_hybrid(item_par, false) {}
1746 Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
1747 : Item_sum_hybrid(pos, item_par, false, w) {}
1748 Item_sum_max(THD *thd, const Item_sum_max *item)
1749 : Item_sum_hybrid(thd, item) {}
1750 enum Sumfunctype sum_func() const override { return MAX_FUNC; }
1751 const char *func_name() const override { return "max"; }
1752
1753 private:
1754 Item_sum_max *clone_hybrid(THD *thd) const override;
1755};
1756
1757/**
1758 Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
1759
1760 Each of them is both a set function and a framing window function.
1761*/
1762class Item_sum_bit : public Item_sum {
1764 /// Stores the neutral element for function
1766 /// Stores the result value for the INT_RESULT
1768 /// Stores the result value for the STRING_RESULT
1770 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1772 /// Buffer used to avoid String allocation in the constructor
1773 const char initial_value_buff_storage[1] = {0};
1774
1775 /**
1776 Execution state (windowing): this is for counting rows entering and leaving
1777 the window frame, see #m_frame_null_count.
1778 */
1780
1781 /**
1782 Execution state (windowing): this is for counting NULLs of rows entering
1783 and leaving the window frame, when we use optimized inverse-based
1784 computations. By comparison with m_count we can know how many non-NULLs are
1785 in the frame.
1786 */
1788
1789 /**
1790 Execution state (windowing): Used for AND, OR to be able to invert window
1791 functions in optimized mode.
1792
1793 For the optimized code path of BIT_XXX wfs, we keep track of the number of
1794 bit values (0's or 1's; see below) seen in a frame using a 64 bits counter
1795 pr bit. This lets us compute the value of OR by just inspecting:
1796
1797 - the number of 1's in the previous frame
1798 - whether any removed row(s) is a 1
1799 - whether any added row(s) is a 1
1800
1801 Similarly for AND, we keep track of the number of 0's seen for a particular
1802 bit. To do this trick we need a counter per bit position. This array holds
1803 these counters.
1804
1805 Note that for XOR, the inverse operation is identical to the operation,
1806 so we don't need the above.
1807 */
1809 /*
1810 Size of allocated array m_digit_cnt.
1811 The size is DIGIT_CNT_CARD (for integer types) or ::max_length * 8 for bit
1812 strings.
1813 */
1815
1816 static constexpr uint DIGIT_CNT_CARD = sizeof(ulonglong) * 8;
1817
1818 protected:
1819 bool m_is_xor; ///< true iff BIT_XOR
1820
1821 public:
1822 Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg,
1823 PT_window *w)
1824 : Item_sum(pos, item_par, w),
1825 reset_bits(reset_arg),
1826 bits(reset_arg),
1828 m_count(0),
1832 m_is_xor(false) {}
1833
1834 /// Copy constructor, used for executing subqueries with temporary tables
1836 : Item_sum(thd, item),
1837 reset_bits(item->reset_bits),
1838 bits(item->bits),
1840 hybrid_type(item->hybrid_type),
1841 m_count(item->m_count),
1845 m_is_xor(item->m_is_xor) {
1846 /*
1847 This constructor should only be called during the Optimize stage.
1848 Asserting that the item was not evaluated yet.
1849 */
1850 assert(item->value_buff.length() == 1);
1851 assert(item->bits == item->reset_bits);
1852 }
1853
1854 Item *result_item(Field *) override {
1855 return new Item_aggr_bit_field(this, reset_bits);
1856 }
1857
1858 enum Sumfunctype sum_func() const override { return SUM_BIT_FUNC; }
1859 enum Item_result result_type() const override { return hybrid_type; }
1860 void clear() override;
1861 longlong val_int() override;
1862 double val_real() override;
1863 String *val_str(String *str) override;
1864 my_decimal *val_decimal(my_decimal *decimal_value) override;
1865 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1866 bool get_time(MYSQL_TIME *ltime) override;
1867 void reset_field() override;
1868 void update_field() override;
1869 bool resolve_type(THD *) override;
1870 bool fix_fields(THD *thd, Item **ref) override;
1871 void cleanup() override {
1872 bits = reset_bits;
1873 // At end of one execution of statement, free buffer to reclaim memory:
1876 }
1877
1878 /**
1879 Common implementation of Item_sum_or::add, Item_sum_and:add
1880 and Item_sum_xor::add.
1881 */
1882 bool add() override;
1883 /// @returns true iff this is BIT_AND.
1884 inline bool is_and() const { return reset_bits != 0; }
1885
1886 private:
1887 /**
1888 Accumulate the value of 's1' (if in string mode) or of 'b1' (if in integer
1889 mode). Updates 'value_buff' or 'bits'.
1890
1891 @param s1 argument to accumulate
1892 @param b1 argument to accumulate
1893
1894 @returns true if error
1895 */
1896 bool add_bits(const String *s1, ulonglong b1);
1897
1898 /**
1899 For windowing: perform inverse aggregation. "De-accumulate" the value of
1900 's1' (if in string mode) or of 'b1' (if in integer mode). Updates
1901 'value_buff' or 'bits'.
1902
1903 For BIT_XOR we simply apply XOR as it's its inverse operation. For BIT_OR
1904 and BIT_AND we do the rest below.
1905
1906 For each bit in argument, decrement the corresponding bits's counter
1907 ('m_digit_cnt') for that bit as follows: for BIT_AND, decrement the
1908 counter if we see a zero in that bit; for BIT_OR decrement the counter if
1909 we see a 1 in that bit. Next, update 'value_buff' or 'bits' using the
1910 resulting counters: for each bit, for BIT_AND, set bit if we have counter
1911 == 0, i.e. we have no zero bits for that bit in the frame (yet). For
1912 BIT_OR, set bit if we have counter > 0, i.e. at least one row in the frame
1913 has that bit set.
1914
1915 @param s1 the bits to be inverted from the aggregate value
1916 @param b1 the bits to be inverted from the aggregate value
1917 */
1918 void remove_bits(const String *s1, ulonglong b1);
1919};
1920
1921class Item_sum_or final : public Item_sum_bit {
1922 public:
1923 Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
1924 : Item_sum_bit(pos, item_par, 0LL, w) {}
1925
1926 Item_sum_or(THD *thd, Item_sum_or *item) : Item_sum_bit(thd, item) {}
1927 const char *func_name() const override { return "bit_or"; }
1928 Item *copy_or_same(THD *thd) override;
1929};
1930
1931class Item_sum_and final : public Item_sum_bit {
1932 public:
1933 Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
1934 : Item_sum_bit(pos, item_par, ULLONG_MAX, w) {}
1935
1936 Item_sum_and(THD *thd, Item_sum_and *item) : Item_sum_bit(thd, item) {}
1937 const char *func_name() const override { return "bit_and"; }
1938 Item *copy_or_same(THD *thd) override;
1939};
1940
1941class Item_sum_xor final : public Item_sum_bit {
1942 public:
1943 Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
1944 : Item_sum_bit(pos, item_par, 0LL, w) {
1945 m_is_xor = true;
1946 }
1947
1948 Item_sum_xor(THD *thd, Item_sum_xor *item) : Item_sum_bit(thd, item) {}
1949 const char *func_name() const override { return "bit_xor"; }
1950 Item *copy_or_same(THD *thd) override;
1951};
1952
1953/*
1954 User defined aggregates
1955*/
1956
1957class Item_udf_sum : public Item_sum {
1959
1960 protected:
1962
1963 public:
1964 Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1965 : Item_sum(pos, opt_list, nullptr), udf(udf_arg) {
1967 }
1969 : Item_sum(thd, item), udf(item->udf) {
1970 udf.m_original = false;
1971 }
1972 ~Item_udf_sum() override {
1974 }
1975
1976 bool do_itemize(Parse_context *pc, Item **res) override;
1977 const char *func_name() const override { return udf.name(); }
1978 bool fix_fields(THD *thd, Item **ref) override {
1979 assert(!fixed);
1980
1981 if (init_sum_func_check(thd)) return true;
1982
1983 fixed = true;
1984 if (udf.fix_fields(thd, this, this->arg_count, this->args)) return true;
1985
1986 return check_sum_func(thd, ref);
1987 }
1988 enum Sumfunctype sum_func() const override { return UDF_SUM_FUNC; }
1989
1990 void clear() override;
1991 bool add() override;
1992 void reset_field() override {}
1993 void update_field() override {}
1994 void cleanup() override;
1995 void print(const THD *thd, String *str,
1996 enum_query_type query_type) const override;
1997};
1998
1999class Item_sum_udf_float final : public Item_udf_sum {
2000 public:
2001 Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2002 : Item_udf_sum(pos, udf_arg, opt_list) {}
2004 : Item_udf_sum(thd, item) {}
2005 longlong val_int() override {
2006 assert(fixed);
2007 return (longlong)rint(Item_sum_udf_float::val_real());
2008 }
2009 double val_real() override;
2010 String *val_str(String *str) override;
2011 my_decimal *val_decimal(my_decimal *) override;
2012 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2013 return get_date_from_real(ltime, fuzzydate);
2014 }
2015 bool get_time(MYSQL_TIME *ltime) override {
2016 return get_time_from_real(ltime);
2017 }
2018 bool resolve_type(THD *) override {
2021 return false;
2022 }
2023 Item *copy_or_same(THD *thd) override;
2024};
2025
2026class Item_sum_udf_int final : public Item_udf_sum {
2027 public:
2028 Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2029 : Item_udf_sum(pos, udf_arg, opt_list) {}
2031 : Item_udf_sum(thd, item) {}
2032 longlong val_int() override;
2033 double val_real() override {
2034 assert(fixed);
2035 return (double)Item_sum_udf_int::val_int();
2036 }
2037 String *val_str(String *str) override;
2038 my_decimal *val_decimal(my_decimal *) override;
2039 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2040 return get_date_from_int(ltime, fuzzydate);
2041 }
2042 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2043 enum Item_result result_type() const override { return INT_RESULT; }
2044 bool resolve_type(THD *) override {
2046 return false;
2047 }
2048 Item *copy_or_same(THD *thd) override;
2049};
2050
2051class Item_sum_udf_str final : public Item_udf_sum {
2052 public:
2053 Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2054 : Item_udf_sum(pos, udf_arg, opt_list) {}
2056 : Item_udf_sum(thd, item) {}
2057 String *val_str(String *) override;
2058 double val_real() override {
2059 int err_not_used;
2060 const char *end_not_used;
2061 String *res;
2062 res = val_str(&str_value);
2063 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2064 &end_not_used, &err_not_used)
2065 : 0.0;
2066 }
2067 longlong val_int() override {
2068 int err_not_used;
2069 String *res;
2070 const CHARSET_INFO *cs;
2071
2072 if (!(res = val_str(&str_value))) return 0; /* Null value */
2073 cs = res->charset();
2074 const char *end = res->ptr() + res->length();
2075 return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
2076 }
2078 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2079 return get_date_from_string(ltime, fuzzydate);
2080 }
2081 bool get_time(MYSQL_TIME *ltime) override {
2082 return get_time_from_string(ltime);
2083 }
2084 enum Item_result result_type() const override { return STRING_RESULT; }
2085 bool resolve_type(THD *) override;
2086 Item *copy_or_same(THD *thd) override;
2087};
2088
2090 public:
2091 Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg,
2092 PT_item_list *opt_list)
2093 : Item_udf_sum(pos, udf_arg, opt_list) {}
2095 : Item_udf_sum(thd, item) {}
2096 String *val_str(String *) override;
2097 double val_real() override;
2098 longlong val_int() override;
2099 my_decimal *val_decimal(my_decimal *) override;
2100 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2101 return get_date_from_decimal(ltime, fuzzydate);
2102 }
2103 bool get_time(MYSQL_TIME *ltime) override {
2104 return get_time_from_decimal(ltime);
2105 }
2106 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2107 bool resolve_type(THD *) override {
2110 return false;
2111 }
2112 Item *copy_or_same(THD *thd) override;
2113};
2114
2115int group_concat_key_cmp_with_distinct(const void *arg, const void *key1,
2116 const void *key2);
2117int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2118 const void *key2);
2119int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2120 void *item_arg);
2121
2122class Item_func_group_concat final : public Item_sum {
2124
2125 /// True if GROUP CONCAT has the DISTINCT attribute
2127 /// The number of ORDER BY items.
2129 /// The number of selected items, aka the concat field list
2131 /// Resolver context, points to containing query block
2133 /// String containing separator between group items
2135 /// Describes the temporary table used to perform group concat
2139 TREE *tree{nullptr};
2140
2141 /**
2142 If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
2143 out duplicates.
2144 @see Item_func_group_concat::setup
2145 @see Item_func_group_concat::add
2146 @see Item_func_group_concat::clear
2147 */
2149 /// Temporary table used to perform group concat
2150 TABLE *table{nullptr};
2152 uint row_count{0};
2153 /**
2154 The maximum permitted result length in bytes as set in
2155 group_concat_max_len system variable
2156 */
2158 bool warning_for_row{false};
2160 /// True if result has been written to output buffer.
2162 /**
2163 Following is 0 normal object and pointer to original one for copy
2164 (to correctly free resources)
2165 */
2167
2168 friend int group_concat_key_cmp_with_distinct(const void *arg,
2169 const void *key1,
2170 const void *key2);
2171 friend int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2172 const void *key2);
2173 friend int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2174 void *item_arg);
2175
2176 public:
2177 Item_func_group_concat(const POS &pos, bool is_distinct,
2178 PT_item_list *select_list,
2179 PT_order_list *opt_order_list, String *separator,
2180 PT_window *w);
2181
2184 assert(original != nullptr || unique_filter == nullptr);
2185 }
2186
2187 bool do_itemize(Parse_context *pc, Item **res) override;
2188 void cleanup() override;
2189
2190 enum Sumfunctype sum_func() const override { return GROUP_CONCAT_FUNC; }
2191 const char *func_name() const override { return "group_concat"; }
2192 Item_result result_type() const override { return STRING_RESULT; }
2193 Field *make_string_field(TABLE *table_arg) const override;
2194 void clear() override;
2195 bool add() override;
2196 void reset_field() override { assert(0); } // not used
2197 void update_field() override { assert(0); } // not used
2198 bool fix_fields(THD *, Item **) override;
2199 bool setup(THD *thd) override;
2200 void make_unique() override;
2201 double val_real() override;
2202 longlong val_int() override {
2203 String *res;
2204 int error;
2205 if (!(res = val_str(&str_value))) return (longlong)0;
2206 const char *end_ptr = res->ptr() + res->length();
2207 return my_strtoll10(res->ptr(), &end_ptr, &error);
2208 }
2209 my_decimal *val_decimal(my_decimal *decimal_value) override {
2210 return val_decimal_from_string(decimal_value);
2211 }
2212 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2213 return get_date_from_string(ltime, fuzzydate);
2214 }
2215 bool get_time(MYSQL_TIME *ltime) override {
2216 return get_time_from_string(ltime);
2217 }
2218
2219 bool has_distinct() const noexcept { return distinct; }
2220 const String *get_separator_str() const noexcept { return separator; }
2221 uint32_t get_group_concat_max_len() const noexcept {
2222 return group_concat_max_len;
2223 }
2224 const Mem_root_array<ORDER> &get_order_array() const noexcept {
2225 return order_array;
2226 }
2227
2228 String *val_str(String *str) override;
2229 Item *copy_or_same(THD *thd) override;
2230 void no_rows_in_result() override;
2231 void print(const THD *thd, String *str,
2232 enum_query_type query_type) const override;
2233 bool change_context_processor(uchar *arg) override {
2234 context = pointer_cast<Item_ident::Change_context *>(arg)->m_context;
2235 return false;
2236 }
2237
2239 Window_evaluation_requirements *) override {
2241 return true;
2242 }
2243};
2244
2245/**
2246 Common parent class for window functions that always work on the entire
2247 partition, even if a frame is defined.
2248
2249 The subclasses can be divided in two disjoint sub-categories:
2250 - one-pass
2251 - two-pass (requires partition cardinality to be evaluated)
2252 cf. method needs_partition_cardinality.
2253*/
2256
2257 public:
2258 Item_non_framing_wf(const POS &pos, PT_window *w) : Item_sum(pos, w) {}
2260 : Item_sum(pos, a, w) {}
2262 : Item_sum(pos, opt_list, w) {}
2264
2265 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2266 return get_date_from_numeric(ltime, fuzzydate);
2267 }
2268
2269 bool get_time(MYSQL_TIME *ltime) override {
2270 return get_time_from_numeric(ltime);
2271 }
2272
2273 void reset_field() override { assert(false); }
2274 void update_field() override { assert(false); }
2275 bool add() override {
2276 assert(false);
2277 return false;
2278 }
2279
2280 bool fix_fields(THD *thd, Item **items) override;
2281
2282 bool framing() const override { return false; }
2283};
2284
2285/**
2286 ROW_NUMBER window function, cf. SQL 2003 Section 6.10 <window function>
2287*/
2289 // Execution state variables
2290 ulonglong m_ctr; ///< Increment for each row in partition
2291
2292 public:
2294 : Item_non_framing_wf(pos, w), m_ctr(0) {
2295 unsigned_flag = true;
2296 }
2297
2298 const char *func_name() const override { return "row_number"; }
2299 enum Sumfunctype sum_func() const override { return ROW_NUMBER_FUNC; }
2300
2301 bool resolve_type(THD *thd [[maybe_unused]]) override {
2303 return false;
2304 }
2305
2306 longlong val_int() override;
2307 double val_real() override;
2308 my_decimal *val_decimal(my_decimal *buff) override;
2309 String *val_str(String *) override;
2310
2311 void clear() override;
2312
2313 Item_result result_type() const override { return INT_RESULT; }
2314
2316 Window_evaluation_requirements *) override {
2317 return false;
2318 }
2319};
2320
2321/**
2322 RANK or DENSE_RANK window function, cf. SQL 2003 Section 6.10 <window
2323 function>
2324*/
2327 bool m_dense; ///< If true, the object represents DENSE_RANK
2328 // Execution state variables
2329 ulonglong m_rank_ctr; ///< Increment when window order columns change
2330 ulonglong m_duplicates; ///< Needed to make RANK different from DENSE_RANK
2332 m_previous; ///< Values of previous row's ORDER BY items
2333 public:
2334 Item_rank(const POS &pos, bool dense, PT_window *w)
2335 : Item_non_framing_wf(pos, w),
2336 m_dense(dense),
2337 m_rank_ctr(0),
2338 m_duplicates(0),
2340 unsigned_flag = true;
2341 }
2342
2343 ~Item_rank() override;
2344
2345 const char *func_name() const override {
2346 return m_dense ? "dense_rank" : "rank";
2347 }
2348
2349 enum Sumfunctype sum_func() const override {
2351 }
2352
2353 bool resolve_type(THD *thd [[maybe_unused]]) override {
2355 return false;
2356 }
2357
2358 longlong val_int() override;
2359 double val_real() override;
2360 my_decimal *val_decimal(my_decimal *buff) override;
2361 String *val_str(String *) override;
2362
2363 void update_after_wf_arguments_changed(THD *thd) override;
2364 bool check_wf_semantics1(THD *thd, Query_block *select,
2365 Window_evaluation_requirements *reqs) override;
2366 /**
2367 Clear state for a new partition
2368 */
2369 void clear() override;
2370 Item_result result_type() const override { return INT_RESULT; }
2371};
2372
2373/**
2374 CUME_DIST window function, cf. SQL 2003 Section 6.10 <window function>
2375*/
2378
2379 public:
2381
2382 const char *func_name() const override { return "cume_dist"; }
2383 enum Sumfunctype sum_func() const override { return CUME_DIST_FUNC; }
2384
2385 bool resolve_type(THD *thd [[maybe_unused]]) override {
2387 return false;
2388 }
2389
2390 bool check_wf_semantics1(THD *thd, Query_block *select,
2391 Window_evaluation_requirements *reqs) override;
2392
2393 bool needs_partition_cardinality() const override { return true; }
2394 void clear() override {}
2395 longlong val_int() override;
2396 double val_real() override;
2397 String *val_str(String *) override;
2399 Item_result result_type() const override { return REAL_RESULT; }
2400};
2401
2402/**
2403 PERCENT_RANK window function, cf. SQL 2003 Section 6.10 <window function>
2404*/
2407 // Execution state variables
2408 ulonglong m_rank_ctr; ///< Increment when window order columns change
2409 ulonglong m_peers; ///< Needed to make PERCENT_RANK same for peers
2410 /**
2411 Set when the last peer has been visited. Needed to increment m_rank_ctr.
2412 */
2414
2415 public:
2417 : Item_non_framing_wf(pos, w),
2418 m_rank_ctr(0),
2419 m_peers(0),
2420 m_last_peer_visited(false) {}
2421
2423 const char *func_name() const override { return "percent_rank"; }
2424 enum Sumfunctype sum_func() const override { return PERCENT_RANK_FUNC; }
2425
2426 bool resolve_type(THD *thd [[maybe_unused]]) override {
2428 return false;
2429 }
2430
2431 bool check_wf_semantics1(THD *thd, Query_block *select,
2432 Window_evaluation_requirements *reqs) override;
2433 bool needs_partition_cardinality() const override { return true; }
2434
2435 void clear() override;
2436 longlong val_int() override;
2437 double val_real() override;
2438 String *val_str(String *) override;
2440 Item_result result_type() const override { return REAL_RESULT; }
2441};
2442
2443/**
2444 NTILE window function, cf. SQL 2011 Section 6.10 <window function>
2445*/
2449
2450 public:
2451 Item_ntile(const POS &pos, Item *a, PT_window *w)
2452 : Item_non_framing_wf(pos, a, w), m_value(0) {
2453 unsigned_flag = true;
2454 }
2455
2456 const char *func_name() const override { return "ntile"; }
2457 enum Sumfunctype sum_func() const override { return NTILE_FUNC; }
2458
2459 bool resolve_type(THD *thd) override {
2460 if (args[0]->propagate_type(thd, MYSQL_TYPE_LONGLONG, true)) return true;
2462 return false;
2463 }
2464
2465 bool fix_fields(THD *thd, Item **items) override;
2466
2467 longlong val_int() override;
2468 double val_real() override;
2469 my_decimal *val_decimal(my_decimal *buff) override;
2470 String *val_str(String *) override;
2471
2472 bool check_wf_semantics1(THD *thd, Query_block *select,
2473 Window_evaluation_requirements *reqs) override;
2475 Item_result result_type() const override { return INT_RESULT; }
2476 void clear() override {}
2477 bool needs_partition_cardinality() const override { return true; }
2478};
2479
2480/**
2481 LEAD/LAG window functions, cf. SQL 2011 Section 6.10 <window function>
2482*/
2485 bool m_is_lead; ///< if true, the function is LEAD, else LAG
2486 int64 m_n; ///< canonicalized offset value
2490 /**
2491 Execution state: if set, we already have a value for current row.
2492 State is used to avoid interference with other LEAD/LAG functions on
2493 the same window, since they share the same eval loop and they should
2494 trigger evaluation only when they are on the "right" row relative to
2495 current row. For other offsets, return NULL if we don't know the value
2496 for this function yet, or if we do (m_has_value==true), return the
2497 found value.
2498 */
2500 bool m_use_default; ///< execution state: use default value for current row
2502
2503 public:
2504 Item_lead_lag(const POS &pos, bool lead,
2505 PT_item_list *opt_list, // [0] expr, [1] offset, [2] default
2506 enum_null_treatment null_treatment, PT_window *w)
2507 : Item_non_framing_wf(pos, opt_list, w),
2508 m_null_treatment(null_treatment),
2509 m_is_lead(lead),
2510 m_n(0),
2514 m_has_value(false),
2515 m_use_default(false) {}
2516
2517 const char *func_name() const override {
2518 return (m_is_lead ? "lead" : "lag");
2519 }
2520 enum Sumfunctype sum_func() const override { return LEAD_LAG_FUNC; }
2521
2522 bool resolve_type(THD *thd) override;
2523 bool fix_fields(THD *thd, Item **items) override;
2524 TYPELIB *get_typelib() const override;
2525 void update_after_wf_arguments_changed(THD *thd) override;
2526 void clear() override;
2527 bool check_wf_semantics1(THD *thd, Query_block *select,
2528 Window_evaluation_requirements *reqs) override;
2530 enum Item_result result_type() const override { return m_hybrid_type; }
2531
2532 longlong val_int() override;
2533 double val_real() override;
2534 String *val_str(String *str) override;
2535 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2536
2537 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2538 bool get_time(MYSQL_TIME *ltime) override;
2539 bool val_json(Json_wrapper *wr) override;
2540
2541 bool needs_partition_cardinality() const override {
2542 /*
2543 A possible optimization here: if LAG, we are only interested in rows we
2544 have already seen, so we might compute the result without reading the
2545 entire partition as soon as we have the current row. Similarly, a small
2546 LEAD value might avoid reading the entire partition also, giving shorter
2547 time to first result. For now, we read the entirely partition for these
2548 window functions - for simplicity.
2549 */
2550 return true;
2551 }
2552
2553 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2554 mem_root_deque<Item *> *fields) override;
2555
2557 bool has_value() const { return m_has_value; }
2558
2560 bool use_default() const { return m_use_default; }
2561
2562 private:
2563 bool setup_lead_lag();
2564 /**
2565 Core logic of LEAD/LAG window functions
2566
2567 @return true if computation yielded a NULL or error
2568 */
2569 bool compute();
2570};
2571
2572/**
2573 FIRST_VALUE/LAST_VALUE window functions, cf. SQL 2011 Section 6.10 <window
2574 function>
2575*/
2577 bool m_is_first; ///< if true, the function is FIRST_VALUE, else LAST_VALUE
2581 int64 cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2583
2584 public:
2585 Item_first_last_value(const POS &pos, bool first, Item *a,
2586 enum_null_treatment null_treatment, PT_window *w)
2587 : Item_sum(pos, a, w),
2588 m_is_first(first),
2589 m_null_treatment(null_treatment),
2592 cnt(0) {}
2593
2594 const char *func_name() const override {
2595 return m_is_first ? "first_value" : "last_value";
2596 }
2597
2598 enum Sumfunctype sum_func() const override { return FIRST_LAST_VALUE_FUNC; }
2599
2600 bool resolve_type(THD *thd) override;
2601 bool fix_fields(THD *thd, Item **items) override;
2602 void update_after_wf_arguments_changed(THD *thd) override;
2603 void clear() override;
2604 bool check_wf_semantics1(THD *thd, Query_block *select,
2605 Window_evaluation_requirements *reqs) override;
2606 enum Item_result result_type() const override { return m_hybrid_type; }
2607
2608 longlong val_int() override;
2609 double val_real() override;
2610 String *val_str(String *str) override;
2611 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2612
2613 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2614 bool get_time(MYSQL_TIME *ltime) override;
2615 bool val_json(Json_wrapper *wr) override;
2616
2617 void reset_field() override { assert(false); }
2618 void update_field() override { assert(false); }
2619 bool add() override {
2620 assert(false);
2621 return false;
2622 }
2623
2624 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2625 mem_root_deque<Item *> *fields) override;
2626 bool uses_only_one_row() const override { return true; }
2627
2628 private:
2629 bool setup_first_last();
2630 /**
2631 Core logic of FIRST/LAST_VALUE window functions
2632
2633 @return true if computation yielded a NULL or error
2634 */
2635 bool compute();
2636};
2637
2638/**
2639 NTH_VALUE window function, cf. SQL 2011 Section 6.10 <window
2640 function>
2641*/
2642class Item_nth_value : public Item_sum {
2644 int64 m_n; ///< The N of the function
2645 bool m_from_last; ///< true iff FROM_LAST was specified
2649 int64 m_cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2650
2652
2653 public:
2654 Item_nth_value(const POS &pos, PT_item_list *a, bool from_last,
2655 enum_null_treatment null_treatment, PT_window *w)
2656 : Item_sum(pos, a, w),
2657 m_null_treatment(null_treatment),
2658 m_n(0),
2659 m_from_last(from_last),
2662 m_cnt(0) {}
2663
2664 const char *func_name() const override { return "nth_value"; }
2665 enum Sumfunctype sum_func() const override { return NTH_VALUE_FUNC; }
2666
2667 bool resolve_type(THD *thd) override;
2668 bool fix_fields(THD *thd, Item **items) override;
2669 void update_after_wf_arguments_changed(THD *thd) override;
2670 bool setup_nth();
2671 void clear() override;
2672
2673 bool check_wf_semantics1(THD *thd, Query_block *select,
2674 Window_evaluation_requirements *reqs) override;
2676
2677 enum Item_result result_type() const override { return m_hybrid_type; }
2678
2679 longlong val_int() override;
2680 double val_real() override;
2681 String *val_str(String *str) override;
2682 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2683
2684 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2685 bool get_time(MYSQL_TIME *ltime) override;
2686 bool val_json(Json_wrapper *wr) override;
2687
2688 void reset_field() override { assert(false); }
2689 void update_field() override { assert(false); }
2690 bool add() override {
2691 assert(false);
2692 return false;
2693 }
2694
2695 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2696 mem_root_deque<Item *> *fields) override;
2697 bool uses_only_one_row() const override { return true; }
2698
2699 private:
2700 /**
2701 Core logic of NTH_VALUE window functions
2702
2703 @return true if computation yielded a NULL or error
2704 */
2705 bool compute();
2706};
2707
2708/**
2709 Class for implementation of the GROUPING function. The GROUPING
2710 function distinguishes super-aggregate rows from regular grouped
2711 rows. GROUP BY extensions such as ROLLUP and CUBE produce
2712 super-aggregate rows where the set of all values is represented
2713 by null. Using the GROUPING function, you can distinguish a null
2714 representing the set of all values in a super-aggregate row from
2715 a NULL in a regular row.
2716*/
2718 public:
2721 }
2722 const char *func_name() const override { return "grouping"; }
2723 enum Functype functype() const override { return GROUPING_FUNC; }
2724 longlong val_int() override;
2725 bool aggregate_check_group(uchar *arg) override;
2726 bool fix_fields(THD *thd, Item **ref) override;
2727 void update_used_tables() override;
2728 bool aggregate_check_distinct(uchar *arg) override;
2729
2730 private:
2731 /// The query block in which this function is called.
2733};
2734
2735/**
2736 A wrapper Item that contains a number of aggregate items, one for each level
2737 of rollup (see Item_rollup_group_item for numbering conventions). When
2738 aggregating, every aggregator is either reset or updated as per the correct
2739 level, and when returning a value, the correct child item corresponding to
2740 the current rollup level is queried.
2741 */
2743 public:
2744 explicit Item_rollup_sum_switcher(List<Item> *sum_func_per_level)
2745 : Item_sum((*sum_func_per_level)[0]),
2746 m_num_levels(sum_func_per_level->size()) {
2747 args = (*THR_MALLOC)->ArrayAlloc<Item *>(sum_func_per_level->size());
2748 int i = 0;
2749 for (Item &item : *sum_func_per_level) {
2750 args[i++] = &item;
2751 }
2755 hidden = master()->hidden;
2759 }
2760 double val_real() override;
2761 longlong val_int() override;
2762 String *val_str(String *str) override;
2764 bool val_json(Json_wrapper *result) override;
2765 bool is_null() override;
2766 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2767 bool get_time(MYSQL_TIME *ltime) override;
2768 const char *func_name() const override { return "rollup_sum_switcher"; }
2769 table_map used_tables() const override { return master()->used_tables(); }
2770 Item_result result_type() const override { return master()->result_type(); }
2771 bool resolve_type(THD *) override {
2773 return false;
2774 }
2775 void print(const THD *thd, String *str,
2776 enum_query_type query_type) const override;
2777 Field *create_tmp_field(bool group, TABLE *table) override;
2778
2779 enum Sumfunctype sum_func() const override { return master()->sum_func(); }
2780 enum Sumfunctype real_sum_func() const override {
2782 }
2783 void reset_field() override { assert(false); }
2784 void update_field() override { assert(false); }
2785 void clear() override;
2786 bool add() override {
2787 assert(false);
2788 return true;
2789 }
2790
2791 bool reset_and_add_for_rollup(int last_unchanged_group_item_idx);
2792
2793 int set_aggregator(Aggregator::Aggregator_type aggregator) override;
2794 bool aggregator_setup(THD *thd) override;
2795 inline bool aggregator_add_all() {
2796 for (int i = 0; i < m_num_levels; ++i) {
2797 if (child(i)->aggregator_add()) {
2798 return true;
2799 }
2800 }
2801 return false;
2802 }
2803
2804 // Used when create_tmp_table() needs to delay application of aggregate
2805 // functions to a later stage in the query processing.
2806 Item *get_arg(uint i) override { return master()->get_arg(i); }
2807 const Item *get_arg(uint i) const override { return master()->get_arg(i); }
2808 Item *set_arg(THD *thd, uint i, Item *new_val) override {
2809 Item *ret = nullptr;
2810 for (int j = 0; j < m_num_levels; ++j) {
2811 ret = child(j)->set_arg(thd, i, new_val);
2812 }
2813 return ret; // Return the last one, arbitrarily.
2814 }
2815 uint argument_count() const override { return master()->argument_count(); }
2816
2817 // Used by AggregateIterator.
2819 inline Item_sum *master() const { return child(0); }
2820
2821 bool is_rollup_sum_wrapper() const override { return true; }
2822 const Item_sum *unwrap_sum() const override { return master(); }
2823 Item_sum *unwrap_sum() override { return master(); }
2824
2825 private:
2826 inline Item *current_arg() const;
2827 inline Item_sum *child(size_t i) const {
2828 return down_cast<Item_sum *>(args[i]);
2829 }
2830
2831 const int m_num_levels;
2833};
2834/// Implements ST_Collect which aggregates geometries into Multipoints,
2835/// Multilinestrings, Multipolygons and Geometrycollections.
2836
2838 private:
2839 std::optional<gis::srid_t> srid;
2840 std::unique_ptr<gis::Geometrycollection> m_geometrycollection;
2841 void pop_front();
2842
2843 public:
2845 Item_sum_collect(THD *thd, Item_sum *item) : Item_sum(thd, item) {
2847 }
2848 Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
2849 : Item_sum(pos, a, w) {
2850 set_distinct(distinct);
2852 }
2853
2854 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2855 longlong val_int() override { return val_int_from_string(); }
2856 double val_real() override { return val_real_from_string(); }
2857 bool get_date(MYSQL_TIME *, my_time_flags_t) override { return true; }
2858 bool get_time(MYSQL_TIME *) override { return true; }
2859 enum Sumfunctype sum_func() const override { return GEOMETRY_AGGREGATE_FUNC; }
2860 Item_result result_type() const override { return STRING_RESULT; }
2862 /*
2863 In contrast to Item_sum, Item_sum_collect always uses Aggregator_simple,
2864 and only needs to reset its aggregator when called.
2865 */
2866 if (aggr != nullptr) {
2867 aggr->clear();
2868 return false;
2869 }
2870
2871 aggr = new (*THR_MALLOC) Aggregator_simple(this);
2872 return aggr ? false : true;
2873 }
2874
2875 bool fix_fields(THD *thd, Item **ref) override;
2876 /*
2877 We need to override check_wf_semantics1 because it reports an error when
2878 with_distinct is set.
2879 */
2880 bool check_wf_semantics1(THD *thd, Query_block *,
2882
2883 Item *copy_or_same(THD *thd) override;
2884
2885 void update_field() override;
2886 void reset_field() override;
2887
2888 String *val_str(String *str) override;
2889
2890 bool add() override;
2891
2892 void read_result_field();
2893 void store_result_field();
2894
2895 void clear() override;
2896 const char *func_name() const override { return "st_collect"; }
2897};
2898
2899#endif /* ITEM_SUM_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:247
The distinct aggregator.
Definition: item_sum.h:817
uint tree_key_length
Definition: item_sum.h:870
uint32 * field_lengths
Definition: item_sum.h:847
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2225
bool endup_done
Definition: item_sum.h:828
Const_distinct
Definition: item_sum.h:872
@ CONST_NULL
Set to true if the result is known to be always NULL.
Definition: item_sum.h:883
@ CONST_NOT_NULL
Set to true if count distinct is on only const items.
Definition: item_sum.h:890
@ NOT_CONST
Definition: item_sum.h:873
static int composite_key_cmp(const void *arg, const void *a, const void *b)
Correctly compare composite keys.
Definition: item_sum.cc:1009
void endup() override
Calculate the aggregate function value.
Definition: item_sum.cc:1402
~Aggregator_distinct() override
Definition: item_sum.cc:2175
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:901
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2220
void clear() override
Invalidate calculated value and clear the distinct rows.
Definition: item_sum.cc:1296
bool setup(THD *) override
Called before feeding the first row.
Definition: item_sum.cc:1080
bool unique_walk_function(void *element)
Aggregate a distinct row from the distinct hash table.
Definition: item_sum.cc:2168
Unique * tree
Definition: item_sum.h:862
Temp_table_param * tmp_table_param
Definition: item_sum.h:853
bool add() override
Process incoming row.
Definition: item_sum.cc:1328
TABLE * table
Definition: item_sum.h:839
Aggregator_type Aggrtype() override
Definition: item_sum.h:912
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2215
enum Aggregator_distinct::Const_distinct const_distinct
Aggregator_distinct(Item_sum *sum)
Definition: item_sum.h:904
The pass-through aggregator.
Definition: item_sum.h:931
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:937
Aggregator_simple(Item_sum *sum)
Definition: item_sum.h:933
bool setup(THD *thd) override
Called before adding the first row.
Definition: item_sum.h:936
Aggregator_type Aggrtype() override
Definition: item_sum.h:934
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2192
void endup() override
Called when there are no more data and the final value is to be retrieved.
Definition: item_sum.h:939
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2196
bool add() override
Called when there's a new value to be aggregated.
Definition: item_sum.h:938
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2200
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:140
void set(const DTCollation &dt)
Definition: item.h:202
Definition: field.h:569
Definition: item_sum.h:1192
Item_aggr_avg_field(Item_sum_avg *item)
Definition: item_sum.cc:3739
String * val_str(String *) override
Definition: item_sum.cc:3781
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3767
uint m_prec_increment
Definition: item_sum.h:1204
uint m_dec_bin_size
Definition: item_sum.h:1203
double val_real() override
Definition: item_sum.cc:3756
enum Type type() const override
Definition: item_sum.h:1195
uint m_scale
Definition: item_sum.h:1202
uint m_precision
Definition: item_sum.h:1201
This is used in connection with an Item_sum_bit,.
Definition: item_sum.h:1208
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3871
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3864
enum Type type() const override
Definition: item_sum.h:1217
ulonglong m_reset_bits
Definition: item_sum.h:1220
longlong val_int() override
Definition: item_sum.cc:3803
double val_real() override
Definition: item_sum.cc:3818
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3835
String * val_str(String *) override
Definition: item_sum.cc:3843
Item_aggr_bit_field(Item_sum_bit *item, ulonglong reset_bits)
Definition: item_sum.cc:3786
Common abstract class for aggregate field classes that return numeric values: Item_aggr_avg_field Ite...
Definition: item_sum.h:1177
longlong val_int() override
Definition: item_sum.h:1179
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1183
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1186
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.h:1189
Definition: item_sum.h:1517
enum Type type() const override
Definition: item_sum.h:1520
enum Item_result result_type() const override
Definition: item_sum.h:1523
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:1524
double val_real() override
Definition: item_sum.cc:3881
Item_aggr_std_field(Item_sum_std *item)
Definition: item_sum.cc:3878
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3887
Definition: item_sum.h:1383
double val_real() override
Definition: item_sum.cc:3919
Item_aggr_variance_field(Item_sum_variance *item)
Definition: item_sum.cc:3907
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:1392
String * val_str(String *str) override
Definition: item_sum.h:1388
my_decimal * val_decimal(my_decimal *dec_buf) override
Definition: item_sum.h:1389
uint m_sample
Definition: item_sum.h:1400
enum Type type() const override
Definition: item_sum.h:1386
This is used in connection with a parent aggregate Item:
Definition: item_sum.h:1146
bool mark_field_in_map(uchar *arg) override
Mark underlying field in read or write map of a table.
Definition: item_sum.h:1157
Item_result m_result_type
Stores the Item's result type.
Definition: item_sum.h:1151
Field * m_field
The tmp table's column containing the value of the set function.
Definition: item_sum.h:1149
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:1164
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1156
enum Item_result result_type() const override
Definition: item_sum.h:1154
Definition: item.h:6804
CUME_DIST window function, cf.
Definition: item_sum.h:2376
Item_cume_dist(const POS &pos, PT_window *w)
Definition: item_sum.h:2380
Item_non_framing_wf super
Definition: item_sum.h:2377
double val_real() override
Definition: item_sum.cc:4944
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2383
void clear() override
Definition: item_sum.h:2394
const char * func_name() const override
Definition: item_sum.h:2382
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:2393
String * val_str(String *) override
Definition: item_sum.cc:4964
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:4934
longlong val_int() override
Definition: item_sum.cc:4956
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2385
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:4968
Item_result result_type() const override
Definition: item_sum.h:2399
Definition: item.h:4389
FIRST_VALUE/LAST_VALUE window functions, cf.
Definition: item_sum.h:2576
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2598
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5172
void reset_field() override
Definition: item_sum.h:2617
const char * func_name() const override
Definition: item_sum.h:2594
bool setup_first_last()
Definition: item_sum.cc:5214
double val_real() override
Definition: item_sum.cc:5263
String * val_str(String *str) override
Definition: item_sum.cc:5318
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:5231
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:5202
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:5158
bool compute()
Core logic of FIRST/LAST_VALUE window functions.
Definition: item_sum.cc:5235
int64 cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2581
Item_result m_hybrid_type
Definition: item_sum.h:2579
enum Item_result result_type() const override
Definition: item_sum.h:2606
Item_cache * m_value
Definition: item_sum.h:2580
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5294
void update_field() override
Definition: item_sum.h:2618
enum_null_treatment m_null_treatment
Definition: item_sum.h:2578
Item_sum super
Definition: item_sum.h:2582
void clear() override
Definition: item_sum.cc:5225
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2626
bool add() override
Definition: item_sum.h:2619
Item_first_last_value(const POS &pos, bool first, Item *a, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2585
bool m_is_first
if true, the function is FIRST_VALUE, else LAST_VALUE
Definition: item_sum.h:2577
longlong val_int() override
Definition: item_sum.cc:5253
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5304
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5284
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5183
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5273
Definition: item_sum.h:2122
void clear() override
Definition: item_sum.cc:4425
bool m_result_finalized
True if result has been written to output buffer.
Definition: item_sum.h:2161
String result
Definition: item_sum.h:2137
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:2166
longlong val_int() override
Definition: item_sum.h:2202
const String * get_separator_str() const noexcept
Definition: item_sum.h:2220
uint32_t get_group_concat_max_len() const noexcept
Definition: item_sum.h:2221
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.h:2209
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:4171
uint row_count
Definition: item_sum.h:2152
void make_unique() override
Definition: item_sum.cc:4698
bool change_context_processor(uchar *arg) override
Definition: item_sum.h:2233
bool setup(THD *thd) override
Definition: item_sum.cc:4581
Item_sum super
Definition: item_sum.h:2123
bool distinct
True if GROUP CONCAT has the DISTINCT attribute.
Definition: item_sum.h:2126
TABLE * table
Temporary table used to perform group concat.
Definition: item_sum.h:2150
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:4092
double val_real() override
Definition: item_sum.cc:4706
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2190
String * val_str(String *str) override
Definition: item_sum.cc:4713
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:4128
bool force_copy_fields
Definition: item_sum.h:2159
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:4488
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2212
Unique * unique_filter
If DISTINCT is used with this GROUP_CONCAT, this member is used to filter out duplicates.
Definition: item_sum.h:2148
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2238
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:4423
Mem_root_array< ORDER > order_array
Definition: item_sum.h:2151
TREE tree_base
Definition: item_sum.h:2138
void update_field() override
Definition: item_sum.h:2197
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:4738
Temp_table_param * tmp_table_param
Describes the temporary table used to perform group concat.
Definition: item_sum.h:2136
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:4350
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4415
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:4298
const char * func_name() const override
Definition: item_sum.h:2191
Item_result result_type() const override
Definition: item_sum.h:2192
Field * make_string_field(TABLE *table_arg) const override
Create a field to hold a string value from an item.
Definition: item_sum.cc:4382
uint m_order_arg_count
The number of ORDER BY items.
Definition: item_sum.h:2128
TREE * tree
Definition: item_sum.h:2139
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:4260
bool has_distinct() const noexcept
Definition: item_sum.h:2219
bool add() override
Definition: item_sum.cc:4437
Name_resolution_context * context
Resolver context, points to containing query block.
Definition: item_sum.h:2132
void reset_field() override
Definition: item_sum.h:2196
uint m_field_arg_count
The number of selected items, aka the concat field list.
Definition: item_sum.h:2130
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:2157
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2215
bool warning_for_row
Definition: item_sum.h:2158
String * separator
String containing separator between group items.
Definition: item_sum.h:2134
~Item_func_group_concat() override
Definition: item_sum.h:2183
const Mem_root_array< ORDER > & get_order_array() const noexcept
Definition: item_sum.h:2224
Class for implementation of the GROUPING function.
Definition: item_sum.h:2717
const Query_block * m_query_block
The query block in which this function is called.
Definition: item_sum.h:2732
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:6434
longlong val_int() override
Evaluation of the GROUPING function.
Definition: item_sum.cc:6383
Item_func_grouping(const POS &pos, PT_item_list *a)
Definition: item_sum.h:2719
const char * func_name() const override
Definition: item_sum.h:2722
bool fix_fields(THD *thd, Item **ref) override
Resolve the fields in the GROUPING function.
Definition: item_sum.cc:6328
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:6404
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_sum.cc:6446
enum Functype functype() const override
Definition: item_sum.h:2723
Definition: item_func.h:100
Item ** args
Array of pointers to arguments.
Definition: item_func.h:107
virtual Item * get_arg(uint i)
Get the i'th argument of the function that this object represents.
Definition: item_func.h:497
Functype
Definition: item_func.h:209
@ GROUPING_FUNC
Definition: item_func.h:263
virtual uint argument_count() const
Definition: item_func.h:131
table_map used_tables_cache
Value used in calculation of result of used_tables()
Definition: item_func.h:193
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:528
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:504
Item ** arguments() const
Definition: item_func.h:132
virtual void fix_num_length_and_dec()
Definition: item_func.cc:861
const char * original_db_name() const
Definition: item.h:4281
void set_original_field_name(const char *name_arg)
Definition: item.h:4278
const char * original_table_name() const
Definition: item.h:4282
Definition: item_func.h:1017
Definition: item.h:5126
LEAD/LAG window functions, cf.
Definition: item_sum.h:2483
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5591
bool has_value() const
Definition: item_sum.h:2557
bool m_is_lead
if true, the function is LEAD, else LAG
Definition: item_sum.h:2485
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:2541
const char * func_name() const override
Definition: item_sum.h:2517
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5744
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:5651
void set_use_default(bool value)
Definition: item_sum.h:2559
String * val_str(String *str) override
Definition: item_sum.cc:5731
bool m_use_default
execution state: use default value for current row
Definition: item_sum.h:2500
enum Item_result result_type() const override
Definition: item_sum.h:2530
Item_cache * m_default
Definition: item_sum.h:2489
bool compute()
Core logic of LEAD/LAG window functions.
Definition: item_sum.cc:5771
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:2504
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:5678
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2520
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5753
enum_null_treatment m_null_treatment
Definition: item_sum.h:2484
void clear() override
Definition: item_sum.cc:5690
bool use_default() const
Definition: item_sum.h:2560
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.cc:5600
bool m_has_value
Execution state: if set, we already have a value for current row.
Definition: item_sum.h:2499
longlong val_int() override
Definition: item_sum.cc:5702
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5718
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5623
Item_result m_hybrid_type
Definition: item_sum.h:2487
int64 m_n
canonicalized offset value
Definition: item_sum.h:2486
double val_real() override
Definition: item_sum.cc:5710
void set_has_value(bool value)
Definition: item_sum.h:2556
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5541
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5761
bool setup_lead_lag()
Definition: item_sum.cc:5662
Item_cache * m_value
Definition: item_sum.h:2488
Item_non_framing_wf super
Definition: item_sum.h:2501
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:5697
Common parent class for window functions that always work on the entire partition,...
Definition: item_sum.h:2254
Item_non_framing_wf(const POS &pos, PT_window *w)
Definition: item_sum.h:2258
bool add() override
Definition: item_sum.h:2275
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2269
Item_non_framing_wf(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2259
Item_non_framing_wf(const POS &pos, PT_item_list *opt_list, PT_window *w)
Definition: item_sum.h:2261
Item_sum super
Definition: item_sum.h:2255
Item_non_framing_wf(THD *thd, Item_non_framing_wf *i)
Definition: item_sum.h:2263
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2265
void update_field() override
Definition: item_sum.h:2274
void reset_field() override
Definition: item_sum.h:2273
bool framing() const override
All aggregates are framing, i.e.
Definition: item_sum.h:2282
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:4774
NTH_VALUE window function, cf.
Definition: item_sum.h:2642
int64 m_cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2649
void update_field() override
Definition: item_sum.h:2689
bool compute()
Core logic of NTH_VALUE window functions.
Definition: item_sum.cc:5434
String * val_str(String *str) override
Definition: item_sum.cc:5501
Item_sum super
Definition: item_sum.h:2651
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5048
enum Item_result result_type() const override
Definition: item_sum.h:2677
Item_result m_hybrid_type
Definition: item_sum.h:2646
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5487
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5521
bool setup_nth()
Definition: item_sum.cc:5393
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5342
enum_field_types m_hybrid_field_type
Definition: item_sum.h:2647
int64 m_n
The N of the function.
Definition: item_sum.h:2644
double val_real() override
Definition: item_sum.cc:5477
bool m_from_last
true iff FROM_LAST was specified
Definition: item_sum.h:2645
Item_cache * m_value
Definition: item_sum.h:2648
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5531
enum_null_treatment m_null_treatment
Definition: item_sum.h:2643
void reset_field() override
Definition: item_sum.h:2688
longlong val_int() override
Definition: item_sum.cc:5467
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:2654
void clear() override
Definition: item_sum.cc:5404
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5328
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:5382
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2665
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:5410
bool add() override
Definition: item_sum.h:2690
const char * func_name() const override
Definition: item_sum.h:2664
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5511
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:5414
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2697
NTILE window function, cf.
Definition: item_sum.h:2446
void clear() override
Definition: item_sum.h:2476
Item_non_framing_wf super
Definition: item_sum.h:2447
String * val_str(String *) override
Definition: item_sum.cc:5127
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2459
const char * func_name() const override
Definition: item_sum.h:2456
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:2477
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5142
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5066
double val_real() override
Definition: item_sum.cc:5122
longlong val_int() override
Definition: item_sum.cc:5072
longlong m_value
Definition: item_sum.h:2448
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:5129
Item_ntile(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2451
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2457
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:5134
Item_result result_type() const override
Definition: item_sum.h:2475
PERCENT_RANK window function, cf.
Definition: item_sum.h:2405
String * val_str(String *) override
Definition: item_sum.cc:5031
Item_percent_rank(const POS &pos, PT_window *w)
Definition: item_sum.h:2416
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2408
const char * func_name() const override
Definition: item_sum.h:2423
Item_result result_type() const override
Definition: item_sum.h:2440
~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:4973
ulonglong m_peers
Needed to make PERCENT_RANK same for peers.
Definition: item_sum.h:2409
double val_real() override
Definition: item_sum.cc:4997
Item_non_framing_wf super
Definition: item_sum.h:2406
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2426
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2424
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:2433
bool m_last_peer_visited
Set when the last peer has been visited.
Definition: item_sum.h:2413
void clear() override
Definition: item_sum.cc:5040
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:5035
longlong val_int() override
Definition: item_sum.cc:5023
RANK or DENSE_RANK window function, cf.
Definition: item_sum.h:2325
Item_result result_type() const override
Definition: item_sum.h:2370
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:4829
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:4853
ulonglong m_duplicates
Needed to make RANK different from DENSE_RANK.
Definition: item_sum.h:2330
bool m_dense
If true, the object represents DENSE_RANK.
Definition: item_sum.h:2327
longlong val_int() override
Definition: item_sum.cc:4871
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2349
~Item_rank() override
Definition: item_sum.cc:4927
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2353
Item_rank(const POS &pos, bool dense, PT_window *w)
Definition: item_sum.h:2334
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4906
const char * func_name() const override
Definition: item_sum.h:2345
double val_real() override
Definition: item_sum.cc:4899
String * val_str(String *) override
Definition: item_sum.cc:4904
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2329
void clear() override
Clear state for a new partition.
Definition: item_sum.cc:4911
Mem_root_array< Cached_item * > m_previous
Values of previous row's ORDER BY items.
Definition: item_sum.h:2332
Item_non_framing_wf super
Definition: item_sum.h:2326
Item with result field.
Definition: item.h:5833
longlong llrint_with_overflow_check(double realval)
Definition: item.h:5872
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2742
table_map used_tables() const override
Definition: item_sum.h:2769
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:6522
void update_field() override
Definition: item_sum.h:2784
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2771
Item * get_arg(uint i) override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2806
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.h:2808
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.cc:6508
void set_current_rollup_level(int level)
Definition: item_sum.h:2818
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:6462
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:6513
bool aggregator_setup(THD *thd) override
Called to initialize the aggregator.
Definition: item_sum.cc:6555
Item_result result_type() const override
Definition: item_sum.h:2770
String * val_str(String *str) override
Definition: item_sum.cc:6487
int set_aggregator(Aggregator::Aggregator_type aggregator) override
Definition: item_sum.cc:6544
Item_sum * unwrap_sum() override
Non-const version.
Definition: item_sum.h:2823
longlong val_int() override
Definition: item_sum.cc:6480
void reset_field() override
Definition: item_sum.h:2783
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:2822
Item_rollup_sum_switcher(List< Item > *sum_func_per_level)
Definition: item_sum.h:2744
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:6468
bool aggregator_add_all()
Definition: item_sum.h:2795
Item * current_arg() const
Definition: item_sum.cc:6457
Item_sum * master() const
Definition: item_sum.h:2819
Item_sum * child(size_t i) const
Definition: item_sum.h:2827
enum Sumfunctype real_sum_func() const override
Definition: item_sum.h:2780
bool is_rollup_sum_wrapper() const override
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:2821
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2779
const char * func_name() const override
Definition: item_sum.h:2768
int m_current_rollup_level
Definition: item_sum.h:2832
double val_real() override
Definition: item_sum.cc:6473
bool val_json(Json_wrapper *result) override
Get a JSON value from an Item.
Definition: item_sum.cc:6501
uint argument_count() const override
Definition: item_sum.h:2815
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:6494
bool add() override
Definition: item_sum.h:2786
void clear() override
Definition: item_sum.cc:6526
bool reset_and_add_for_rollup(int last_unchanged_group_item_idx)
Definition: item_sum.cc:6532
const int m_num_levels
Definition: item_sum.h:2831
const Item * get_arg(uint i) const override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2807
ROW_NUMBER window function, cf.
Definition: item_sum.h:2288
ulonglong m_ctr
Increment for each row in partition.
Definition: item_sum.h:2290
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2301
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2299
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2315
Item_row_number(const POS &pos, PT_window *w)
Definition: item_sum.h:2293
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4822
Item_result result_type() const override
Definition: item_sum.h:2313
String * val_str(String *) override
Definition: item_sum.cc:4818
longlong val_int() override
Definition: item_sum.cc:4799
void clear() override
Definition: item_sum.cc:4827
const char * func_name() const override
Definition: item_sum.h:2298
double val_real() override
Definition: item_sum.cc:4813
Definition: item_sum.h:1931
Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1933
const char * func_name() const override
Definition: item_sum.h:1937
Item_sum_and(THD *thd, Item_sum_and *item)
Definition: item_sum.h:1936
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3346
Definition: item_sum.h:1343
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2293
void clear() override
Definition: item_sum.cc:2349
String * val_str(String *str) override
Definition: item_sum.cc:2464
const char * func_name() const override
Definition: item_sum.h:1371
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3583
Item_sum_sum super
Definition: item_sum.h:1347
Item * result_item(Field *) override
Definition: item_sum.h:1370
double val_real() override
Definition: item_sum.cc:2358
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1374
Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
Definition: item_sum.h:1351
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:2327
my_decimal m_avg_dec
Definition: item_sum.h:1348
uint f_scale
Definition: item_sum.h:1346
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1358
double m_avg
Definition: item_sum.h:1349
longlong val_int() override
Definition: item_sum.h:1365
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2320
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2381
void reset_field() override
Definition: item_sum.cc:3481
uint prec_increment
Definition: item_sum.h:1345
uint dec_bin_size
Definition: item_sum.h:1346
bool add() override
Definition: item_sum.cc:2351
Item_sum_avg(THD *thd, Item_sum_avg *item)
Definition: item_sum.h:1354
uint f_precision
Definition: item_sum.h:1346
Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
Definition: item_sum.h:1762
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3236
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:1666
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1871
void remove_bits(const String *s1, ulonglong b1)
For windowing: perform inverse aggregation.
Definition: item_sum.cc:1588
const char initial_value_buff_storage[1]
Buffer used to avoid String allocation in the constructor.
Definition: item_sum.h:1773
static constexpr uint DIGIT_CNT_CARD
Definition: item_sum.h:1816
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1858
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1530
bool m_is_xor
true iff BIT_XOR
Definition: item_sum.h:1819
void clear() override
Definition: item_sum.cc:3318
Item * result_item(Field *) override
Definition: item_sum.h:1854
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:1505
longlong val_int() override
Definition: item_sum.cc:3294
Item_sum super
Definition: item_sum.h:1763
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:1808
Item_result hybrid_type
Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT.
Definition: item_sum.h:1771
ulonglong reset_bits
Stores the neutral element for function.
Definition: item_sum.h:1765
Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg, PT_window *w)
Definition: item_sum.h:1822
bool is_and() const
Definition: item_sum.h:1884
void reset_field() override
Definition: item_sum.cc:3511
ulonglong m_count
Execution state (windowing): this is for counting rows entering and leaving the window frame,...
Definition: item_sum.h:1779
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3243
double val_real() override
Definition: item_sum.cc:3268
bool add() override
Common implementation of Item_sum_or::add, Item_sum_and:add and Item_sum_xor::add.
Definition: item_sum.cc:1750
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.cc:3250
enum Item_result result_type() const override
Definition: item_sum.h:1859
uint m_digit_cnt_card
Definition: item_sum.h:1814
Item_sum_bit(THD *thd, Item_sum_bit *item)
Copy constructor, used for executing subqueries with temporary tables.
Definition: item_sum.h:1835
String * val_str(String *str) override
Definition: item_sum.cc:3205
void update_field() override
Definition: item_sum.cc:3521
ulonglong bits
Stores the result value for the INT_RESULT.
Definition: item_sum.h:1767
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:1787
String value_buff
Stores the result value for the STRING_RESULT.
Definition: item_sum.h:1769
Implements ST_Collect which aggregates geometries into Multipoints, Multilinestrings,...
Definition: item_sum.h:2837
bool add() override
Definition: item_sum.cc:6630
void clear() override
Definition: item_sum.cc:6624
void read_result_field()
Definition: item_sum.cc:6685
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:6593
bool get_date(MYSQL_TIME *, my_time_flags_t) override
Definition: item_sum.h:2857
std::optional< gis::srid_t > srid
Definition: item_sum.h:2839
Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
Definition: item_sum.h:2848
void update_field() override
Definition: item_sum.cc:6778
longlong val_int() override
Definition: item_sum.h:2855
bool get_time(MYSQL_TIME *) override
Definition: item_sum.h:2858
std::unique_ptr< gis::Geometrycollection > m_geometrycollection
Definition: item_sum.h:2840
String * val_str(String *str) override
Definition: item_sum.cc:6731
void reset_field() override
Definition: item_sum.cc:6841
Item_sum_collect(THD *thd, Item_sum *item)
Definition: item_sum.h:2845
void store_result_field()
Definition: item_sum.cc:6784
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:6836
bool check_wf_semantics1(THD *thd, Query_block *, Window_evaluation_requirements *r) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:6615
const char * func_name() const override
Definition: item_sum.h:2896
void pop_front()
Definition: item_sum.cc:6724
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2859
Item_result result_type() const override
Definition: item_sum.h:2860
int set_aggregator(Aggregator::Aggregator_type) override
Definition: item_sum.h:2861
double val_real() override
Definition: item_sum.h:2856
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6680
Definition: item_sum.h:1076
longlong count
Definition: item_sum.h:1077
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1110
longlong val_int() override
Definition: item_sum.cc:2255
Item_sum_count(Item *item, bool distinct)
Definition: item_sum.h:1089
void update_field() override
Definition: item_sum.cc:3574
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2236
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1107
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:2287
void make_const(longlong count_arg)
Definition: item_sum.h:1117
void reset_field() override
Definition: item_sum.cc:3473
Item_sum_count(Item_int *number)
Definition: item_sum.h:1088
Item_sum_count(THD *thd, Item_sum_count *item)
Definition: item_sum.h:1105
Item_sum_count(const POS &pos, PT_item_list *list, PT_window *w)
Constructs an instance for COUNT(DISTINCT)
Definition: item_sum.h:1101
void clear() override
Definition: item_sum.cc:2244
const char * func_name() const override
Definition: item_sum.h:1124
Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1086
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1116
bool add() override
Definition: item_sum.cc:2246
Abstract base class for the MIN and MAX aggregate functions.
Definition: item_sum.h:1558
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3158
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:3153
bool setup_hybrid(Item *item, Item *value_arg)
MIN/MAX function setup.
Definition: item_sum.cc:1842
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:1709
virtual Item_sum_hybrid * clone_hybrid(THD *thd) const =0
Create a clone of this object.
bool add() override
Definition: item_sum.cc:3188
const bool m_is_min
Tells if this is the MIN function (true) or the MAX function (false).
Definition: item_sum.h:1565
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3087
bool compute()
This function implements the optimized version of retrieving min/max value.
Definition: item_sum.cc:2889
bool has_values()
Definition: item_sum.h:1706
void min_max_update_int_field()
Definition: item_sum.cc:3705
longlong val_int() override
Definition: item_sum.cc:3022
bool m_has_values
Definition: item_sum.h:1577
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3077
void reset_field() override
Definition: item_sum.cc:3370
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:3124
void clear() override
Definition: item_sum.cc:2840
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:1605
Item_cache * arg_cache
Definition: item_sum.h:1574
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3139
void min_max_update_json_field()
Definition: item_sum.cc:3658
void update_field() override
Definition: item_sum.cc:3619
longlong val_date_temporal() override
Return date value of item in packed longlong format.
Definition: item_sum.cc:3048
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:2850
bool keep_field_type() const override
Definition: item_sum.h:1698
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3060
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:1599
void min_max_update_real_field()
Definition: item_sum.cc:3692
Item_sum_hybrid(THD *thd, const Item_sum_hybrid *item)
Definition: item_sum.h:1669
Item_result hybrid_type
Definition: item_sum.h:1576
longlong val_time_temporal() override
Return time value of item in packed longlong format.
Definition: item_sum.cc:3036
bool m_optimize
Set to true when min/max can be optimized using window's ordering.
Definition: item_sum.h:1585
Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
Definition: item_sum.h:1653
Item_sum_hybrid(Item *item_par, bool is_min)
Definition: item_sum.h:1637
Item_sum super
Definition: item_sum.h:1559
void min_max_update_temporal_field()
Definition: item_sum.cc:3640
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:2855
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1811
String * val_str(String *) override
Definition: item_sum.cc:3097
Arg_comparator * cmp
Definition: item_sum.h:1575
void min_max_update_str_field()
Definition: item_sum.cc:3676
enum Item_result result_type() const override
Definition: item_sum.h:1699
void min_max_update_decimal_field()
Definition: item_sum.cc:3723
double val_real() override
Definition: item_sum.cc:3008
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:1859
bool m_want_first
For min() - Set to true when results are ordered in ascending and false when descending.
Definition: item_sum.h:1593
Item_cache * value
Definition: item_sum.h:1574
bool m_nulls_first
Set to true if the window is ordered ascending.
Definition: item_sum.h:1581
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.h:1700
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:3110
Definition: item_sum.h:988
Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:990
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1014
Item_sum_int(THD *thd, Item_sum_int *item)
Definition: item_sum.h:1000
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1017
double val_real() override
Definition: item_sum.h:1008
Item_sum_int(Item *item_par)
Definition: item_sum.h:1004
enum Item_result result_type() const override
Definition: item_sum.h:1018
String * val_str(String *str) override
Definition: item_sum.cc:1472
Item_sum_int(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:995
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1474
Implements aggregation of values into an array.
Definition: item_sum.h:1283
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:6059
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1298
~Item_sum_json_array() override
bool add() override
Definition: item_sum.cc:6137
unique_ptr_destroy_only< Json_array > m_json_array
Accumulates the final value.
Definition: item_sum.h:1285
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6186
const char * func_name() const override
Definition: item_sum.h:1297
void clear() override
Definition: item_sum.cc:6077
Implements aggregation of values into an object.
Definition: item_sum.h:1305
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:6086
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6294
bool add() override
Definition: item_sum.cc:6199
unique_ptr_destroy_only< Json_object > m_json_object
Accumulates the final value.
Definition: item_sum.h:1307
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1335
~Item_sum_json_object() override
const char * func_name() const override
Definition: item_sum.h:1334
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:6113
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:1316
void clear() override
Definition: item_sum.cc:6103
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:1324
String m_tmp_key_value
Buffer used to get the value of the key.
Definition: item_sum.h:1309
Common abstraction for Item_sum_json_array and Item_sum_json_object.
Definition: item_sum.h:1226
void reset_field() override
Definition: item_sum.cc:6019
void update_field() override
Definition: item_sum.cc:6039
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5928
Json_constructor_null_clause m_json_constructor_null_clause
JSON constructor null clause.
Definition: item_sum.h:1237
unique_ptr_destroy_only< Json_wrapper > m_wrapper
Wrapper around the container (object/array) which accumulates the value.
Definition: item_sum.h:1235
longlong val_int() override
Definition: item_sum.cc:5971
String m_value
String used when reading JSON binary values or JSON text values.
Definition: item_sum.h:1231
String m_conversion_buffer
String used for converting JSON text values to utf8mb4 charset.
Definition: item_sum.h:1233
Item_sum super
Definition: item_sum.h:1227
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:5872
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:6012
~Item_sum_json() override
String * val_str(String *str) override
Definition: item_sum.cc:5909
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5986
Item_result result_type() const override
Definition: item_sum.h:1260
bool fix_fields(THD *thd, Item **pItem) override
Definition: item_sum.cc:5846
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:5841
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:5888
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:5829
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:6004
double val_real() override
Definition: item_sum.cc:5956
Definition: item_sum.h:1743
Item_sum_max(Item *item_par)
Definition: item_sum.h:1745
Item_sum_max(THD *thd, const Item_sum_max *item)
Definition: item_sum.h:1748
const char * func_name() const override
Definition: item_sum.h:1751
Item_sum_max * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3169
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1750
Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1746
Definition: item_sum.h:1729
Item_sum_min(THD *thd, const Item_sum_min *item)
Definition: item_sum.h:1734
Item_sum_min(Item *item_par)
Definition: item_sum.h:1731
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1736
const char * func_name() const override
Definition: item_sum.h:1737
Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1732
Item_sum_min * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3165
Definition: item_sum.h:945
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1468
Item_sum_num(THD *thd, Item_sum_num *item)
Definition: item_sum.h:964
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1478
longlong val_int() override
Definition: item_sum.h:973
enum_field_types default_data_type() const override
Get the default data (output) type for the specific item.
Definition: item_sum.h:966
String * val_str(String *str) override
Definition: item_sum.cc:1466
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:979
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:982
Item_sum super
Definition: item_sum.h:946
Item_sum_num(Item *item_par)
Definition: item_sum.h:970
bool is_evaluated
Definition: item_sum.h:955
Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
Definition: item_sum.h:958
void reset_field() override
Definition: item_sum.cc:3357
Item_sum_num(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:961
Definition: item_sum.h:1921
Item_sum_or(THD *thd, Item_sum_or *item)
Definition: item_sum.h:1926
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3332
const char * func_name() const override
Definition: item_sum.h:1927
Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1923
Definition: item_sum.h:1536
double val_real() override
Definition: item_sum.cc:2474
Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1538
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2483
Item_sum_std(THD *thd, Item_sum_std *item)
Definition: item_sum.h:1541
const char * func_name() const override
Definition: item_sum.h:1545
enum Item_result result_type() const override
Definition: item_sum.h:1549
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1542
Item * result_item(Field *) override
Definition: item_sum.h:1544
Definition: item_sum.h:1021
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:1032
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:1991
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:1923
enum Item_result result_type() const override
Definition: item_sum.h:1066
double sum
Definition: item_sum.h:1024
bool add() override
Definition: item_sum.cc:2013
Item_result hybrid_type
Definition: item_sum.h:1023
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:1942
void reset_field() override
Definition: item_sum.cc:3452
String * val_str(String *str) override
Definition: item_sum.cc:2102
longlong val_int() override
Definition: item_sum.cc:2034
my_decimal dec_buffs[2]
Definition: item_sum.h:1025
void clear() override
Definition: item_sum.cc:1930
const char * func_name() const override
Definition: item_sum.h:1072
Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
Definition: item_sum.h:1042
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3545
Item_sum_sum(Item *item_par)
Definition: item_sum.h:1050
uint curr_dec_buff
Definition: item_sum.h:1026
ulonglong m_frame_null_count
Execution state: this is for counting NULLs of rows entering and leaving the window frame,...
Definition: item_sum.h:1039
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2108
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1944
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1057
double val_real() override
Definition: item_sum.cc:2058
Definition: item_sum.h:2089
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2100
longlong val_int() override
Definition: item_sum.cc:4003
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2103
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4005
Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2091
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4014
double val_real() override
Definition: item_sum.cc:4001
enum Item_result result_type() const override
Definition: item_sum.h:2106
String * val_str(String *) override
Definition: item_sum.cc:3997
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
Definition: item_sum.h:2094
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2107
Definition: item_sum.h:1999
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2018
longlong val_int() override
Definition: item_sum.h:2005
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3993
String * val_str(String *str) override
Definition: item_sum.cc:3989
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3976
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2012
double val_real() override
Definition: item_sum.cc:3981
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
Definition: item_sum.h:2003
Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2001
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2015
Definition: item_sum.h:2026
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2039
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
Definition: item_sum.h:2030
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2044
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2042
enum Item_result result_type() const override
Definition: item_sum.h:2043
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4018
String * val_str(String *str) override
Definition: item_sum.cc:4030
Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2028
longlong val_int() override
Definition: item_sum.cc:4022
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4034
double val_real() override
Definition: item_sum.h:2033
Definition: item_sum.h:2051
bool resolve_type(THD *) override
Default max_length is max argument length.
Definition: item_sum.cc:4040
double val_real() override
Definition: item_sum.h:2058
String * val_str(String *) override
Definition: item_sum.cc:4056
enum Item_result result_type() const override
Definition: item_sum.h:2084
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2078
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2081
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4048
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:4052
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
Definition: item_sum.h:2055
longlong val_int() override
Definition: item_sum.h:2067
Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2053
Definition: item_sum.h:1460
ulonglong count
Definition: item_sum.h:1471
void update_field() override
Definition: item_sum.cc:2814
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2682
double val_real() override
Definition: item_sum.cc:2759
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1507
void clear() override
Definition: item_sum.cc:2736
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2702
Item * result_item(Field *) override
Definition: item_sum.h:1497
Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1481
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1490
double recurrence_s
Definition: item_sum.h:1469
bool optimize
If set, uses a algorithm II mentioned in the class description to calculate the variance which helps ...
Definition: item_sum.h:1479
const char * func_name() const override
Definition: item_sum.h:1501
bool add() override
Definition: item_sum.cc:2738
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1500
double recurrence_s2
Definition: item_sum.h:1470
Item_result hybrid_type
Definition: item_sum.h:1464
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:2666
double recurrence_m
Used in recurrence relation.
Definition: item_sum.h:1468
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2789
uint prec_increment
Definition: item_sum.h:1473
enum Item_result result_type() const override
Definition: item_sum.h:1506
void reset_field() override
Definition: item_sum.cc:2794
uint sample
Definition: item_sum.h:1472
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:2715
Definition: item_sum.h:1941
const char * func_name() const override
Definition: item_sum.h:1949
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3339
Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1943
Item_sum_xor(THD *thd, Item_sum_xor *item)
Definition: item_sum.h:1948
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:669
bool collect_grouped_aggregates(uchar *) override
Definition: item_sum.cc:737
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:425
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:727
void aggregator_clear()
Called to cleanup the aggregator.
Definition: item_sum.h:681
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:787
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:497
bool aggregate_check_distinct(uchar *arg) override
Definition: item_sum.cc:653
~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:165
virtual bool aggregator_setup(THD *thd)
Called to initialize the aggregator.
Definition: item_sum.h:675
bool wf_common_init()
Common initial actions for window functions.
Definition: item_sum.cc:959
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:615
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:802
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:479
void make_const()
Definition: item_sum.h:597
Item * replace_aggregate(uchar *) override
Definition: item_sum.cc:768
Window * window()
Definition: item_sum.h:737
virtual void make_unique()
Definition: item_sum.h:620
bool eq_specific(const Item *item) const override
Provide a more specific equality check for a function.
Definition: item_sum.cc:645
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:104
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:923
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:935
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:622
bool check_sum_func(THD *thd, Item **ref)
Validate the semantic requirements of a set function.
Definition: item_sum.cc:250
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:541
void unsupported_as_wf()
Definition: item_sum.h:796
Aggregator * aggr
Aggregator class instance.
Definition: item_sum.h:408
const Window * window() const
Definition: item_sum.h:738
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:767
virtual bool is_rollup_sum_wrapper() const
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:781
bool aggregate_check_group(uchar *arg) override
Definition: item_sum.cc:677
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:776
virtual void reset_field()=0
virtual bool framing() const
All aggregates are framing, i.e.
Definition: item_sum.h:751
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:796
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.cc:876
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:710
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:864
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:881
bool aggregator_add()
Called to add value to the aggregator.
Definition: item_sum.h:687
Item_sum(Item *a)
Definition: item_sum.h:525
void set_distinct(bool distinct)
Definition: item_sum.h:690
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:786
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:469
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:137
bool reset_wf_state(uchar *arg) override
Reset execution state for such window function types as determined by arg.
Definition: item_sum.cc:946
table_map used_tables() const override
Definition: item_sum.h:587
virtual Item_sum * unwrap_sum()
Non-const version.
Definition: item_sum.h:789
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:817
virtual bool setup(THD *)
Definition: item_sum.h:705
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:911
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:757
Item ** get_arg_ptr(uint i)
Definition: item_sum.h:655
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:702
Query_block * base_query_block
query block where function is placed
Definition: item_sum.h:485
Definition: item_sum.h:1957
udf_handler udf
Definition: item_sum.h:1961
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:3935
void update_field() override
Definition: item_sum.h:1993
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3956
void reset_field() override
Definition: item_sum.h:1992
bool add() override
Definition: item_sum.cc:3950
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.h:1978
Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1964
void clear() override
Definition: item_sum.cc:3944
~Item_udf_sum() override
Definition: item_sum.h:1972
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:3965
Item_udf_sum(THD *thd, Item_udf_sum *item)
Definition: item_sum.h:1968
Item_sum super
Definition: item_sum.h:1958
const char * func_name() const override
Definition: item_sum.h:1977
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1988
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:927
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:3576
void set_nullable(bool nullable)
Definition: item.h:3688
DTCollation collation
Character set and collation properties assigned for this Item.
Definition: item.h:3583
bool get_time_from_decimal(MYSQL_TIME *ltime)
Convert val_decimal() to time in MYSQL_TIME.
Definition: item.cc:1667
void set_data_type(enum_field_types data_type)
Set the data type of the current Item.
Definition: item.h:1510
my_decimal * val_decimal_from_string(my_decimal *decimal_value)
Definition: item.cc:369
bool is_nullable() const
Definition: item.h:3687
void set_data_type_geometry()
Set the data type of the Item to be GEOMETRY.
Definition: item.h:1752
bool get_time_from_string(MYSQL_TIME *ltime)
Convert val_str() to time in MYSQL_TIME.
Definition: item.cc:1648
bool get_time_from_real(MYSQL_TIME *ltime)
Convert val_real() to time in MYSQL_TIME.
Definition: item.cc:1658
bool get_date_from_decimal(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_decimal() to date in MYSQL_TIME.
Definition: item.cc:1580
void set_data_type_double()
Set the data type of the Item to be double precision floating point.
Definition: item.h:1576
Item_name_string item_name
Name from query.
Definition: item.h:3584
bool fixed
True if item has been resolved.
Definition: item.h:3676
virtual Item_result result_type() const
Definition: item.h:1448
bool null_value
True if item is null.
Definition: item.h:3713
Type
Definition: item.h:962
@ SUM_FUNC_ITEM
A grouped aggregate function, or window function.
Definition: item.h:966
@ AGGR_FIELD_ITEM
A special field for certain aggregate operations.
Definition: item.h:967
bool get_date_from_numeric(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
Convert a numeric type to date.
Definition: item.cc:1608
virtual TYPELIB * get_typelib() const
Get the typelib information for an item of type set or enum.
Definition: item.h:1819
my_decimal * val_decimal_from_real(my_decimal *decimal_value)
Definition: item.cc:354
bool unsigned_flag
Definition: item.h:3714
longlong val_int_from_string()
Definition: item.cc:526
bool get_date_from_string(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_str() to date in MYSQL_TIME.
Definition: item.cc:1561
void set_grouping_func()
Set the property: this item is a call to GROUPING.
Definition: item.h:3493
bool get_time_from_numeric(MYSQL_TIME *ltime)
Convert a numeric type to time.
Definition: item.cc:1700
virtual bool mark_field_in_map(uchar *arg)
Mark underlying field in read or write map of a table.
Definition: item.h:2827
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:3724
bool get_date_from_int(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_int() to date in MYSQL_TIME.
Definition: item.cc:1589
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:1796
bool get_date_from_real(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_real() to date in MYSQL_TIME.
Definition: item.cc:1571
void set_data_type_longlong()
Set the data type of the Item to be longlong.
Definition: item.h:1552
double val_real_from_string()
Definition: item.cc:472
bool update_null_value()
Make sure the null_value member has a correct value.
Definition: item.cc:7610
String * val_string_from_real(String *str)
Definition: item.cc:287
bool get_time_from_int(MYSQL_TIME *ltime)
Convert val_int() to time in MYSQL_TIME.
Definition: item.cc:1676
Represents a JSON array container, i.e.
Definition: json_dom.h:517
Represents a JSON container value of type "object" (ECMA), type J_OBJECT here.
Definition: json_dom.h:371
bool add_alias(std::string_view key, Json_dom *value)
Insert the value into the object.
Definition: json_dom.h:413
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1160
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:105
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
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:96
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:4624
void free_handler()
Definition: item_func.cc:4605
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:216
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:4171
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:4092
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:4128
Json_constructor_null_clause
Definition: item_sum.h:1223
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
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:94
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:1581
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1084
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:2880
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:485
int err_code
the error code found during check(if any)
Definition: item.h:492
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:504
argument used by walk method collect_grouped_aggregates ("cga")
Definition: item_sum.h:624
Collect_grouped_aggregate_info(Query_block *select)
Definition: item_sum.h:637
Query_block * m_query_block
The query block we walk from.
Definition: item_sum.h:632
std::vector< Item_sum * > list
accumulated all aggregates found
Definition: item_sum.h:626
std::set< Item_sum * > aggregates_that_were_hidden
Definition: item_sum.h:627
bool m_break_off
true: break off transformation
Definition: item_sum.h:634
bool m_outside
true: an aggregate aggregates outside m_query_block
Definition: item_sum.h:636
Definition: mysql_time.h:82
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:412
Definition: table.h:298
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:422
Definition: table.h:1433
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...