MySQL 9.0.0
Source Code Documentation
item_sum.h
Go to the documentation of this file.
1#ifndef ITEM_SUM_INCLUDED
2#define ITEM_SUM_INCLUDED
3
4/* Copyright (c) 2000, 2024, 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_AGG_FUNC, // JSON_ARRAYAGG and JSON_OBJECTAGG
455 ROW_NUMBER_FUNC, // Window functions
466 };
467
468 /**
469 @note most member variables below serve only for grouped aggregate
470 functions.
471 */
472
473 /**
474 For a group aggregate which is aggregated into an outer query
475 block; none, or just the first or both cells may be non-zero. They are
476 filled with references to the group aggregate (for example if it is the
477 argument of a function; it is then a pointer to that function's args[i]
478 pointer).
479 */
481 /// next in the circular chain of registered objects
483 Item_sum *in_sum_func; ///< the containing set function if any
484 Query_block *base_query_block; ///< query block where function is placed
485 /**
486 For a group aggregate, query block where function is aggregated. For a
487 window function, nullptr, as such function is always aggregated in
488 base_query_block, as it mustn't contain any outer reference.
489 */
491 int8 max_aggr_level; ///< max level of unbound column references
492 int8
493 max_sum_func_level; ///< max level of aggregation for contained functions
494 bool allow_group_via_temp_table; ///< If incremental update of fields is
495 ///< supported.
496 /**
497 WFs are forbidden when resolving Item_sum; this member is used to restore
498 WF allowance status afterwards.
499 */
501
502 protected:
503 /**
504 True means that this field has been evaluated during optimization.
505 When set, used_tables() returns zero and const_item() returns true.
506 The value must be reset to false after execution.
507 */
508 bool forced_const{false};
509
510 /// true if the function is resolved to be always NULL
511 bool m_null_resolved{false};
512 /// true if the function is determined to be NULL at start of execution
513 bool m_null_executed{false};
514
515 static ulonglong ram_limitation(THD *thd);
516
517 public:
518 void mark_as_sum_func();
520
521 Item_sum(const POS &pos, PT_window *w)
523
527 }
528
529 Item_sum(const POS &pos, Item *a, PT_window *w)
530 : Item_func(pos, a), m_window(w), allow_group_via_temp_table(true) {}
531
532 Item_sum(const POS &pos, Item *a, Item *b, PT_window *w)
533 : Item_func(pos, a, b), m_window(w), allow_group_via_temp_table(true) {}
534
535 Item_sum(const POS &pos, PT_item_list *opt_list, PT_window *w);
536
537 /// Copy constructor, need to perform subqueries with temporary tables
538 Item_sum(THD *thd, const Item_sum *item);
539
540 ~Item_sum() override { assert(aggr == nullptr); }
541
542 bool do_itemize(Parse_context *pc, Item **res) override;
543 Type type() const override { return SUM_FUNC_ITEM; }
544 virtual enum Sumfunctype sum_func() const = 0;
545
546 // Differs only for Item_rollup_sum_switcher.
547 virtual enum Sumfunctype real_sum_func() const { return sum_func(); }
548
549 /**
550 Resets the aggregate value to its default and aggregates the current
551 value of its attribute(s).
552 */
553 inline bool reset_and_add() {
555 return aggregator_add();
556 }
557
558 /*
559 Called when new group is started and results are being saved in
560 a temporary table. Similarly to reset_and_add() it resets the
561 value to its default and aggregates the value of its
562 attribute(s), but must also store it in result_field.
563 This set of methods (result_item(), reset_field, update_field()) of
564 Item_sum is used only if allow_group_via_temp_table is true. Otherwise
565 copy_or_same() is used to obtain a copy of this item.
566 */
567 virtual void reset_field() = 0;
568 /*
569 Called for each new value in the group, when temporary table is in use.
570 Similar to add(), but uses temporary table field to obtain current value,
571 Updated value is then saved in the field.
572 */
573 virtual void update_field() = 0;
574 virtual bool keep_field_type() const { return false; }
575 bool resolve_type(THD *) override;
576 virtual Item *result_item(Field *field) {
577 Item_field *item = new Item_field(field);
578 if (item == nullptr) return nullptr;
579 // Aggregated fields have no reference to an underlying table
580 assert(item->original_db_name() == nullptr &&
581 item->original_table_name() == nullptr);
582 // Break the connection to the original field since this is an aggregation
583 item->set_original_field_name(nullptr);
584 return item;
585 }
586 table_map used_tables() const override {
587 return forced_const ? 0 : used_tables_cache;
588 }
589 table_map not_null_tables() const override { return used_tables(); }
590 void update_used_tables() override;
591 void fix_after_pullout(Query_block *parent_query_block,
592 Query_block *removed_query_block) override;
594 bool is_null() override { return null_value; }
595
596 void make_const() {
597 // "forced_const" will make used_tables() return zero for this object
598 forced_const = true;
599 }
600 void print(const THD *thd, String *str,
601 enum_query_type query_type) const override;
602 bool eq(const Item *item) const override;
603 bool eq_specific(const Item *item) const override;
604 /**
605 Mark an aggregate as having no rows.
606
607 This function is called by the execution engine to assign 'NO ROWS
608 FOUND' value to an aggregate item, when the underlying result set
609 has no rows. Such value, in a general case, may be different from
610 the default value of the item after 'clear()': e.g. a numeric item
611 may be initialized to 0 by clear() and to NULL by
612 no_rows_in_result().
613 */
614 void no_rows_in_result() override {
618 }
619 virtual void make_unique() { force_copy_fields = true; }
620 virtual Field *create_tmp_field(bool group, TABLE *table);
621
622 /// argument used by walk method collect_grouped_aggregates ("cga")
624 /// accumulated all aggregates found
625 std::vector<Item_sum *> list;
626 std::set<Item_sum *> aggregates_that_were_hidden;
627 /**
628 The query block we walk from. All found aggregates must aggregate in
629 this; if some aggregate in outer query blocks, break off transformation.
630 */
632 /// true: break off transformation
633 bool m_break_off{false};
634 /// true: an aggregate aggregates outside m_query_block
635 bool m_outside{false};
637 : m_query_block(select) {}
638 };
639
640 bool collect_grouped_aggregates(uchar *) override;
641 Item *replace_aggregate(uchar *) override;
642 bool collect_scalar_subqueries(uchar *) override;
644
645 bool clean_up_after_removal(uchar *arg) override;
646 bool aggregate_check_group(uchar *arg) override;
647 bool aggregate_check_distinct(uchar *arg) override;
648 bool has_aggregate_ref_in_group_by(uchar *arg) override;
649 bool init_sum_func_check(THD *thd);
650 bool check_sum_func(THD *thd, Item **ref);
651
652 Item *set_arg(THD *thd, uint i, Item *new_val) override;
653 /// @todo delete this when we no longer support temporary transformations
654 Item **get_arg_ptr(uint i) { return &args[i]; }
655
656 bool fix_fields(THD *thd, Item **ref) override;
657
658 /**
659 Signal to the function that its arguments may have changed,
660 and that any internal caches etc. based on those arguments
661 must be updated accordingly.
662
663 This is used by the hypergraph optimizer when it rewrites
664 arguments to window functions to take into account that they
665 have been materialized into temporary tables, or that they
666 should read their values from the framebuffer.
667 */
669
670 /**
671 Called to initialize the aggregator.
672 */
673
674 virtual bool aggregator_setup(THD *thd) { return aggr->setup(thd); }
675
676 /**
677 Called to cleanup the aggregator.
678 */
679
680 inline void aggregator_clear() { aggr->clear(); }
681
682 /**
683 Called to add value to the aggregator.
684 */
685
686 inline bool aggregator_add() { return aggr->add(); }
687
688 /* stores the declared DISTINCT flag (from the parser) */
689 void set_distinct(bool distinct) {
690 with_distinct = distinct;
692 }
693
694 /*
695 Set the type of aggregation : DISTINCT or not.
696
697 May be called multiple times.
698 */
699
700 virtual int set_aggregator(Aggregator::Aggregator_type aggregator);
701
702 virtual void clear() = 0;
703 virtual bool add() = 0;
704 virtual bool setup(THD *) { return false; }
705
706 /**
707 Only relevant for aggregates qua window functions. Checks semantics after
708 windows have been set up and checked. Window functions have specific
709 requirements on the window specifications. Used at resolution time.
710
711 @param thd Current thread
712 @param select The current select
713 @param [out] reqs Holds collected requirements from this wf
714
715 @returns true if error
716 */
717 virtual bool check_wf_semantics1(THD *thd, Query_block *select,
719
720 /**
721 Like check_wf_semantics1.
722 For checks which cannot be done in resolution phase (mostly those for
723 input parameters which can be '?' and must be >=0: value isn't known
724 before execution phase).
725 */
727 [[maybe_unused]]) {
728 return false;
729 }
730
731 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
732 mem_root_deque<Item *> *fields) override;
733
734 void cleanup() override;
735
736 Window *window() { return m_window; }
737 const Window *window() const { return m_window; }
738 bool reset_wf_state(uchar *arg) override;
739
740 /**
741 All aggregates are framing, i.e. they work on the window's frame. If none
742 is defined, the frame is by default the entire partition, unless ORDER BY
743 is defined, in which case it is the set of rows from the start of the
744 partition to and including the peer set of the current row.
745
746 Some window functions are not framing, i.e. they always work on the entire
747 partition. For such window functions, the method is overridden to
748 return false.
749 */
750 virtual bool framing() const { return true; }
751
752 /**
753 Only for framing window functions. True if this function only needs to
754 read one row per frame.
755 */
756 virtual bool uses_only_one_row() const { return false; }
757
758 /**
759 Return true if we need to make two passes over the rows in the partition -
760 either because we need the cardinality of it (and we need to read all
761 rows to detect the next partition), or we need to have all partition rows
762 available to evaluate the window function for some other reason, e.g.
763 we may need the last row in the partition in the frame buffer to be able
764 to evaluate LEAD.
765 */
766 virtual bool needs_partition_cardinality() const { return false; }
767
768 /**
769 Common initial actions for window functions. For non-buffered processing
770 ("on-the-fly"), check partition change and possible reset partition
771 state. In this case return false.
772 For buffered processing, if windowing state m_do_copy_null is true, set
773 null_value to is_nullable() and return true.
774
775 @return true if case two above holds, else false
776 */
777 bool wf_common_init();
778
779 /// Overridden by Item_rollup_sum_switcher.
780 virtual bool is_rollup_sum_wrapper() const { return false; }
781 /**
782 * In case we are an Item_rollup_sum_switcher,
783 * return the underlying Item_sum, otherwise, return this.
784 * Overridden by Item_rollup_sum_switcher.
785 */
786 virtual const Item_sum *unwrap_sum() const { return this; }
787 /// Non-const version
788 virtual Item_sum *unwrap_sum() { return this; }
789
790 protected:
791 /*
792 Raise an error (ER_NOT_SUPPORTED_YET) with the detail that this
793 function is not yet supported as a window function.
794 */
796 char buff[STRING_BUFFER_USUAL_SIZE];
797 snprintf(buff, sizeof(buff), "%s as window function", func_name());
798 my_error(ER_NOT_SUPPORTED_YET, MYF(0), buff);
799 }
800
801 void add_json_info(Json_object *obj) override {
802 obj->add_alias("distinct", create_dom_ptr<Json_boolean>(with_distinct));
803 }
804};
805
806class Unique;
807
808/**
809 The distinct aggregator.
810 Implements AGGFN (DISTINCT ..)
811 Collects all the data into an Unique (similarly to what Item_sum_distinct
812 does currently) and then (if applicable) iterates over the list of
813 unique values and pumps them back into its object
814*/
815
817 friend class Item_sum_sum;
818
819 /*
820 flag to prevent consecutive runs of endup(). Normally in endup there are
821 expensive calculations (like walking the distinct tree for example)
822 which we must do only once if there are no data changes.
823 We can re-use the data for the second and subsequent val_xxx() calls.
824 endup_done set to true also means that the calculated values for
825 the aggregate functions are correct and don't need recalculation.
826 */
828
829 /*
830 Used depending on the type of the aggregate function and the presence of
831 blob columns in it:
832 - For COUNT(DISTINCT) and no blob fields this points to a real temporary
833 table. It's used as a hash table.
834 - For AVG/SUM(DISTINCT) or COUNT(DISTINCT) with blob fields only the
835 in-memory data structure of a temporary table is constructed.
836 It's used by the Field classes to transform data into row format.
837 */
839
840 /*
841 An array of field lengths on row allocated and used only for
842 COUNT(DISTINCT) with multiple columns and no blobs. Used in
843 Aggregator_distinct::composite_key_cmp (called from Unique to compare
844 nodes
845 */
847
848 /*
849 Used in conjunction with 'table' to support the access to Field classes
850 for COUNT(DISTINCT). Needed by copy_fields()/copy_funcs().
851 */
853
854 /*
855 If there are no blobs in the COUNT(DISTINCT) arguments, we can use a tree,
856 which is faster than heap table. In that case, we still use the table
857 to help get things set up, but we insert nothing in it.
858 For AVG/SUM(DISTINCT) we always use this tree (as it takes a single
859 argument) to get the distinct rows.
860 */
862
863 /*
864 The length of the temp table row. Must be a member of the class as it
865 gets passed down to simple_raw_key_cmp () as a compare function argument
866 to Unique. simple_raw_key_cmp () is used as a fast comparison function
867 when the entire row can be binary compared.
868 */
870
873 /**
874 Set to true if the result is known to be always NULL.
875 If set deactivates creation and usage of the temporary table (in the
876 'table' member) and the Unique instance (in the 'tree' member) as well as
877 the calculation of the final value on the first call to
878 @c Item_sum::val_xxx(),
879 @c Item_avg::val_xxx(),
880 @c Item_count::val_xxx().
881 */
883 /**
884 Set to true if count distinct is on only const items. Distinct on a const
885 value will always be the constant itself. And count distinct of the same
886 would always be 1. Similar to CONST_NULL, it avoids creation of temporary
887 table and the Unique instance.
888 */
891
892 /**
893 When feeding back the data in endup() from Unique/temp table back to
894 Item_sum::add() methods we must read the data from Unique (and not
895 recalculate the functions that are given as arguments to the aggregate
896 function.
897 This flag is to tell the arg_*() methods to take the data from the Unique
898 instead of calling the relevant val_..() method.
899 */
901
902 public:
904 : Aggregator(sum),
905 table(nullptr),
907 tree(nullptr),
909 use_distinct_values(false) {}
910 ~Aggregator_distinct() override;
912
913 bool setup(THD *) override;
914 void clear() override;
915 bool add() override;
916 void endup() override;
917 my_decimal *arg_val_decimal(my_decimal *value) override;
918 double arg_val_real() override;
919 bool arg_is_null(bool use_null_value) override;
920
921 bool unique_walk_function(void *element);
922 static int composite_key_cmp(const void *arg, const void *a, const void *b);
923};
924
925/**
926 The pass-through aggregator.
927 Implements AGGFN (DISTINCT ..) by knowing it gets distinct data on input.
928 So it just pumps them back to the Item_sum descendant class.
929*/
931 public:
934
935 bool setup(THD *thd) override { return item_sum->setup(thd); }
936 void clear() override { item_sum->clear(); }
937 bool add() override { return item_sum->add(); }
938 void endup() override {}
939 my_decimal *arg_val_decimal(my_decimal *value) override;
940 double arg_val_real() override;
941 bool arg_is_null(bool use_null_value) override;
942};
943
944class Item_sum_num : public Item_sum {
946
947 protected:
948 /*
949 val_xxx() functions may be called several times during the execution of a
950 query. Derived classes that require extensive calculation in val_xxx()
951 maintain cache of aggregate value. This variable governs the validity of
952 that cache.
953 */
955
956 public:
957 Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
958 : Item_sum(pos, item_par, window), is_evaluated(false) {}
959
961 : Item_sum(pos, list, w), is_evaluated(false) {}
962
964 : Item_sum(thd, item), is_evaluated(item->is_evaluated) {}
966 return MYSQL_TYPE_DOUBLE;
967 }
968
969 Item_sum_num(Item *item_par) : Item_sum(item_par), is_evaluated(false) {}
970
971 bool fix_fields(THD *, Item **) override;
972 longlong val_int() override {
973 assert(fixed);
974 return llrint_with_overflow_check(val_real()); /* Real as default */
975 }
976 String *val_str(String *str) override;
977 my_decimal *val_decimal(my_decimal *) override;
978 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
979 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
980 }
981 bool get_time(MYSQL_TIME *ltime) override {
982 return get_time_from_numeric(ltime); /* Decimal or real */
983 }
984 void reset_field() override;
985};
986
988 public:
989 Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
990 : Item_sum_num(pos, item_par, w) {
992 }
993
995 : Item_sum_num(pos, list, w) {
997 }
998
999 Item_sum_int(THD *thd, Item_sum_int *item) : Item_sum_num(thd, item) {
1001 }
1002
1003 Item_sum_int(Item *item_par) : Item_sum_num(item_par) {
1005 }
1006
1007 double val_real() override {
1008 assert(fixed);
1009 return static_cast<double>(val_int());
1010 }
1011 String *val_str(String *str) override;
1012 my_decimal *val_decimal(my_decimal *) override;
1013 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1014 return get_date_from_int(ltime, fuzzydate);
1015 }
1016 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
1017 enum Item_result result_type() const override { return INT_RESULT; }
1018};
1019
1021 protected:
1023 double sum;
1026 bool resolve_type(THD *thd) override;
1027 /**
1028 Execution state: this is for counting rows entering and leaving the window
1029 frame, see #m_frame_null_count.
1030 */
1032
1033 /**
1034 Execution state: this is for counting NULLs of rows entering and leaving
1035 the window frame, when we use optimized inverse-based computations. By
1036 comparison with m_count we can know how many non-NULLs are in the frame.
1037 */
1039
1040 public:
1041 Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
1042 : Item_sum_num(pos, item_par, window),
1044 m_count(0),
1046 set_distinct(distinct);
1047 }
1048
1049 Item_sum_sum(THD *thd, Item_sum_sum *item);
1050 enum Sumfunctype sum_func() const override {
1052 }
1053 void clear() override;
1054 bool add() override;
1055 double val_real() override;
1056 longlong val_int() override;
1057 String *val_str(String *str) override;
1058 my_decimal *val_decimal(my_decimal *) override;
1059 enum Item_result result_type() const override { return hybrid_type; }
1060 bool check_wf_semantics1(THD *thd, Query_block *select,
1061 Window_evaluation_requirements *reqs) override;
1062 void no_rows_in_result() override;
1063 void reset_field() override;
1064 void update_field() override;
1065 const char *func_name() const override { return "sum"; }
1066 Item *copy_or_same(THD *thd) override;
1067};
1068
1071
1073
1074 void clear() override;
1075 bool add() override;
1076 void cleanup() override;
1077
1078 public:
1079 Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
1080 : Item_sum_int(pos, item_par, w), count(0) {}
1081 Item_sum_count(Item_int *number) : Item_sum_int(number), count(0) {}
1082 /**
1083 Constructs an instance for COUNT(DISTINCT)
1084
1085 @param pos Position of token in the parser.
1086 @param list A list of the arguments to the aggregate function
1087 @param w A window, if COUNT is used as a windowing function
1088
1089 This constructor is called by the parser only for COUNT (DISTINCT).
1090 */
1091
1093 : Item_sum_int(pos, list, w), count(0) {
1094 set_distinct(true);
1095 }
1097 : Item_sum_int(thd, item), count(item->count) {}
1098 enum Sumfunctype sum_func() const override {
1100 }
1101 bool resolve_type(THD *thd) override {
1102 if (param_type_is_default(thd, 0, -1)) return true;
1103 set_nullable(false);
1104 null_value = false;
1105 return false;
1106 }
1107 void no_rows_in_result() override { count = 0; }
1108 void make_const(longlong count_arg) {
1109 count = count_arg;
1111 }
1112 longlong val_int() override;
1113 void reset_field() override;
1114 void update_field() override;
1115 const char *func_name() const override { return "count"; }
1116 Item *copy_or_same(THD *thd) override;
1117};
1118
1119/* Item to get the value of a stored sum function */
1120
1121class Item_sum_avg;
1122class Item_sum_bit;
1123
1124/**
1125 This is used in connection which a parent Item_sum:
1126 - which can produce different result types (is "hybrid")
1127 - which stores function's value into a temporary table's column (one
1128 row per group).
1129 - which stores in the column some internal piece of information which should
1130 not be returned to the user, so special implementations are needed.
1131*/
1133 protected:
1134 /// The tmp table's column containing the value of the set function.
1136 /// Stores the Item's result type.
1138
1139 public:
1140 enum Item_result result_type() const override { return hybrid_type; }
1141 bool mark_field_in_map(uchar *arg) override {
1142 /*
1143 Filesort (find_all_keys) over a temporary table collects the columns it
1144 needs.
1145 */
1146 return Item::mark_field_in_map(pointer_cast<Mark_field *>(arg), field);
1147 }
1150 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1151 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1152 return true;
1153 }
1154 void cleanup() override {
1155 field = nullptr;
1157 }
1158};
1159
1160/**
1161 Common abstract class for:
1162 Item_avg_field
1163 Item_variance_field
1164*/
1166 public:
1167 longlong val_int() override {
1168 /* can't be fix_fields()ed */
1170 }
1171 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1172 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
1173 }
1174 bool get_time(MYSQL_TIME *ltime) override {
1175 return get_time_from_numeric(ltime); /* Decimal or real */
1176 }
1177 bool is_null() override { return update_null_value() || null_value; }
1178};
1179
1181 public:
1184 Item_avg_field(Item_result res_type, Item_sum_avg *item);
1185 enum Type type() const override { return AGGR_FIELD_ITEM; }
1186 double val_real() override;
1187 my_decimal *val_decimal(my_decimal *) override;
1188 String *val_str(String *) override;
1189 bool resolve_type(THD *) override { return false; }
1190};
1191
1192/// This is used in connection with an Item_sum_bit, @see Item_sum_hybrid_field
1194 protected:
1196
1197 public:
1200 longlong val_int() override;
1201 double val_real() override;
1202 my_decimal *val_decimal(my_decimal *) override;
1203 String *val_str(String *) override;
1204 bool resolve_type(THD *) override { return false; }
1205 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1206 bool get_time(MYSQL_TIME *ltime) override;
1207 enum Type type() const override { return AGGR_FIELD_ITEM; }
1208};
1209
1210/// Common abstraction for Item_sum_json_array and Item_sum_json_object
1211class Item_sum_json : public Item_sum {
1213
1214 protected:
1215 /// String used when reading JSON binary values or JSON text values.
1217 /// String used for converting JSON text values to utf8mb4 charset.
1219 /// Wrapper around the container (object/array) which accumulates the value.
1221
1222 /**
1223 Construct an Item_sum_json instance.
1224
1225 @param wrapper a wrapper around the Json_array or Json_object that contains
1226 the aggregated result
1227 @param parent_args arguments to forward to Item_sum's constructor
1228 */
1229 template <typename... Args>
1231 Args &&... parent_args);
1232
1233 public:
1234 ~Item_sum_json() override;
1235 bool fix_fields(THD *thd, Item **pItem) override;
1236 enum Sumfunctype sum_func() const override { return JSON_AGG_FUNC; }
1237 Item_result result_type() const override { return STRING_RESULT; }
1238
1239 double val_real() override;
1240 longlong val_int() override;
1241 String *val_str(String *str) override;
1242 bool val_json(Json_wrapper *wr) override;
1243 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
1244 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1245 bool get_time(MYSQL_TIME *ltime) override;
1246
1247 void reset_field() override;
1248 void update_field() override;
1249
1252};
1253
1254/// Implements aggregation of values into an array.
1256 /// Accumulates the final value.
1258
1259 public:
1260 Item_sum_json_array(THD *thd, Item_sum *item,
1263 Item_sum_json_array(const POS &pos, Item *a, PT_window *w,
1267 const char *func_name() const override { return "json_arrayagg"; }
1268 void clear() override;
1269 bool add() override;
1270 Item *copy_or_same(THD *thd) override;
1271};
1272
1273/// Implements aggregation of values into an object.
1275 /// Accumulates the final value.
1277 /// Buffer used to get the value of the key.
1279 /**
1280 Map of keys in Json_object and the count for each key
1281 within a window frame. It is used in handling rows
1282 leaving a window frame when rows are not sorted
1283 according to the key in Json_object.
1284 */
1285 std::map<std::string, int> m_key_map;
1286 /**
1287 If window provides ordering on the key in Json_object,
1288 a key_map is not needed to handle rows leaving a window
1289 frame. In this case, process_buffered_windowing_record()
1290 will set flags when a key/value pair can be removed from
1291 the Json_object.
1292 */
1293 bool m_optimize{false};
1294
1295 public:
1296 Item_sum_json_object(THD *thd, Item_sum *item,
1299 Item_sum_json_object(const POS &pos, Item *a, Item *b, PT_window *w,
1303 const char *func_name() const override { return "json_objectagg"; }
1304 void clear() override;
1305 bool add() override;
1306 Item *copy_or_same(THD *thd) override;
1307 bool check_wf_semantics1(THD *thd, Query_block *select,
1308 Window_evaluation_requirements *reqs) override;
1309};
1310
1311class Item_sum_avg final : public Item_sum_sum {
1312 public:
1317 double m_avg;
1318
1319 Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
1320 : Item_sum_sum(pos, item_par, distinct, w) {}
1321
1323 : Item_sum_sum(thd, item), prec_increment(item->prec_increment) {}
1324
1325 bool resolve_type(THD *thd) override;
1326 enum Sumfunctype sum_func() const override {
1328 }
1329 void clear() override;
1330 bool add() override;
1331 double val_real() override;
1332 // In SPs we might force the "wrong" type with select into a declare variable
1334 my_decimal *val_decimal(my_decimal *) override;
1335 String *val_str(String *str) override;
1336 void reset_field() override;
1337 void update_field() override;
1338 Item *result_item(Field *) override {
1339 return new Item_avg_field(hybrid_type, this);
1340 }
1341 const char *func_name() const override { return "avg"; }
1342 Item *copy_or_same(THD *thd) override;
1343 Field *create_tmp_field(bool group, TABLE *table) override;
1344 void cleanup() override {
1345 m_count = 0;
1348 }
1349};
1350
1351class Item_sum_variance;
1352
1354 protected:
1356
1357 public:
1359 enum Type type() const override { return AGGR_FIELD_ITEM; }
1360 double val_real() override;
1362 my_decimal *val_decimal(my_decimal *dec_buf) override {
1363 return val_decimal_from_real(dec_buf);
1364 }
1365 bool resolve_type(THD *) override { return false; }
1368 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1369 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1370 return true;
1371 }
1372};
1373
1374/*
1375 variance(a) =
1376
1377 = sum (ai - avg(a))^2 / count(a) )
1378 = sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
1379 = (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
1380 = (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
1381 = (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2
1382 )/count(a) = = (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a)
1383 )/count(a) = = (sum(ai^2) - sum(a)^2/count(a))/count(a)
1384
1385 But, this falls prey to catastrophic cancellation.
1386 Instead, we use recurrence formulas in Algorithm I mentoned below
1387 for group aggregates.
1388
1389 Algorithm I:
1390 M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
1391 S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
1392 for 2 <= k <= n newline
1393 ital variance = S_{n} / (n-1)
1394
1395 For aggregate window functions algorithm I cannot be optimized for
1396 moving frames since M_{i} changes for every row. So we use the
1397 following algorithm.
1398
1399 Algorithm II:
1400
1401 K = 0
1402 n = 0
1403 ex = 0
1404 ex2 = 0
1405
1406 def add_sample(x):
1407 if (n == 0):
1408 K = x
1409 n = n + 1
1410 ex += x - K
1411 ex2 += (x - K) * (x - K)
1412
1413 def remove_sample(x):
1414 n = n - 1
1415 ex -= (x - K)
1416 ex2 -= (x - K) * (x - K)
1417
1418 def variance():
1419 return (ex2 - (ex*ex)/n) / (n-1)
1420
1421 This formula facilitates incremental computation enabling us to
1422 optimize in case of moving window frames. The optimized codepath is taken
1423 only when windowing_use_high_precision is set to false. By default,
1424 aggregate window functions take the non-optimized codepath.
1425 Note:
1426 Results could differ between optimized and non-optimized code path.
1427 Hence algorithm II is used only when user sets
1428 windowing_use_high_precision to false.
1429*/
1430
1432 bool resolve_type(THD *) override;
1433
1434 public:
1436 /**
1437 Used in recurrence relation.
1438 */
1439 double recurrence_m{0.0};
1440 double recurrence_s{0.0};
1441 double recurrence_s2{0.0};
1445 /**
1446 If set, uses a algorithm II mentioned in the class description
1447 to calculate the variance which helps in optimizing windowing
1448 functions in presence of frames.
1449 */
1451
1452 Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg,
1453 PT_window *w)
1454 : Item_sum_num(pos, item_par, w),
1456 count(0),
1457 sample(sample_arg),
1458 optimize(false) {}
1459
1461 enum Sumfunctype sum_func() const override { return VARIANCE_FUNC; }
1462 void clear() override;
1463 bool add() override;
1464 double val_real() override;
1465 my_decimal *val_decimal(my_decimal *) override;
1466 void reset_field() override;
1467 void update_field() override;
1468 Item *result_item(Field *) override { return new Item_variance_field(this); }
1469 void no_rows_in_result() override {}
1470 const char *func_name() const override {
1471 return sample ? "var_samp" : "variance";
1472 }
1473 Item *copy_or_same(THD *thd) override;
1474 Field *create_tmp_field(bool group, TABLE *table) override;
1475 enum Item_result result_type() const override { return REAL_RESULT; }
1476 void cleanup() override {
1477 count = 0;
1479 }
1480 bool check_wf_semantics1(THD *thd, Query_block *select,
1481 Window_evaluation_requirements *reqs) override;
1482};
1483
1484class Item_sum_std;
1485
1487 public:
1489 enum Type type() const override { return AGGR_FIELD_ITEM; }
1490 double val_real() override;
1491 my_decimal *val_decimal(my_decimal *) override;
1492 enum Item_result result_type() const override { return REAL_RESULT; }
1495 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1496 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1497 return true;
1498 }
1499};
1500
1501/*
1502 standard_deviation(a) = sqrt(variance(a))
1503*/
1504
1506 public:
1507 Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
1508 : Item_sum_variance(pos, item_par, sample_arg, w) {}
1509
1510 Item_sum_std(THD *thd, Item_sum_std *item) : Item_sum_variance(thd, item) {}
1511 enum Sumfunctype sum_func() const override { return STD_FUNC; }
1512 double val_real() override;
1513 Item *result_item(Field *) override { return new Item_std_field(this); }
1514 const char *func_name() const override {
1515 return sample ? "stddev_samp" : "std";
1516 }
1517 Item *copy_or_same(THD *thd) override;
1518 enum Item_result result_type() const override { return REAL_RESULT; }
1519};
1520
1521// This class is a string or number function depending on num_func
1522class Arg_comparator;
1523
1524/**
1525 Abstract base class for the MIN and MAX aggregate functions.
1526*/
1529
1530 private:
1531 /**
1532 Tells if this is the MIN function (true) or the MAX function (false).
1533 */
1534 const bool m_is_min;
1535 /*
1536 For window functions MIN/MAX with optimized code path, no comparisons
1537 are needed beyond NULL detection: MIN/MAX are then roughly equivalent to
1538 FIRST/LAST_VALUE. For this case, 'value' is the value of
1539 the window function a priori taken from args[0], while arg_cache is used to
1540 remember the value from the previous row. NULLs need a bit of careful
1541 treatment.
1542 */
1546 bool m_has_values; // Set if at least one row is found (for max/min only)
1547 /**
1548 Set to true if the window is ordered ascending.
1549 */
1551 /**
1552 Set to true when min/max can be optimized using window's ordering.
1553 */
1555 /**
1556 For min() - Set to true when results are ordered in ascending and
1557 false when descending.
1558 For max() - Set to true when results are ordered in descending and
1559 false when ascending.
1560 Valid only when m_optimize is true.
1561 */
1562 bool m_want_first; ///< Want first non-null value, else last non_null value
1563 /**
1564 Execution state: keeps track if this is the first row in the frame
1565 when buffering is not needed.
1566 Valid only when m_optimize is true.
1567 */
1569
1570 /**
1571 Execution state: keeps track of at which row we saved a non-null last
1572 value.
1573 */
1575
1576 /**
1577 This function implements the optimized version of retrieving min/max
1578 value. When we have "ordered ASC" results in a window, min will always
1579 be the first value in the result set (neglecting the NULL's) and max
1580 will always be the last value (or the other way around, if ordered DESC).
1581 It is based on the implementation of FIRST_VALUE/LAST_VALUE, except for
1582 the NULL handling.
1583
1584 @return true if computation yielded a NULL or error
1585 */
1586 bool compute();
1587
1588 /**
1589 MIN/MAX function setup.
1590
1591 Setup cache/comparator of MIN/MAX functions. When called by the
1592 copy_or_same() function, the value_arg parameter contains the calculated
1593 value of the original MIN/MAX object, and it is saved in this object's
1594 cache.
1595
1596 @param item the argument of the MIN/MAX function
1597 @param value_arg the calculated value of the MIN/MAX function
1598 @return false on success, true on error
1599 */
1600 bool setup_hybrid(Item *item, Item *value_arg);
1601
1602 /** Create a clone of this object. */
1603 virtual Item_sum_hybrid *clone_hybrid(THD *thd) const = 0;
1604
1605 protected:
1606 Item_sum_hybrid(Item *item_par, bool is_min)
1607 : Item_sum(item_par),
1608 m_is_min(is_min),
1609 value(nullptr),
1611 cmp(nullptr),
1613 m_has_values(true),
1614 m_nulls_first(false),
1615 m_optimize(false),
1616 m_want_first(false),
1617 m_cnt(0),
1620 }
1621
1622 Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
1623 : Item_sum(pos, item_par, w),
1624 m_is_min(is_min),
1625 value(nullptr),
1627 cmp(nullptr),
1629 m_has_values(true),
1630 m_nulls_first(false),
1631 m_optimize(false),
1632 m_want_first(false),
1633 m_cnt(0),
1636 }
1637
1639 : Item_sum(thd, item),
1640 m_is_min(item->m_is_min),
1641 value(item->value),
1643 hybrid_type(item->hybrid_type),
1646 m_optimize(item->m_optimize),
1648 m_cnt(item->m_cnt),
1650
1651 public:
1652 bool fix_fields(THD *, Item **) override;
1653 void clear() override;
1654 void update_after_wf_arguments_changed(THD *thd) override;
1655 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
1656 mem_root_deque<Item *> *fields) override;
1657 double val_real() override;
1658 longlong val_int() override;
1659 longlong val_time_temporal() override;
1660 longlong val_date_temporal() override;
1661 my_decimal *val_decimal(my_decimal *) override;
1662 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1663 bool get_time(MYSQL_TIME *ltime) override;
1664 void reset_field() override;
1665 String *val_str(String *) override;
1666 bool val_json(Json_wrapper *wr) override;
1667 bool keep_field_type() const override { return true; }
1668 enum Item_result result_type() const override { return hybrid_type; }
1669 TYPELIB *get_typelib() const override {
1670 assert(sum_func() == MAX_FUNC || sum_func() == MIN_FUNC);
1671 return arguments()[0]->get_typelib();
1672 }
1673 void update_field() override;
1674 void cleanup() override;
1675 bool has_values() { return m_has_values; }
1676 void no_rows_in_result() override;
1677 Field *create_tmp_field(bool group, TABLE *table) override;
1678 bool uses_only_one_row() const override { return m_optimize; }
1679 bool add() override;
1680 Item *copy_or_same(THD *thd) override;
1681 bool check_wf_semantics1(THD *thd, Query_block *select,
1683
1684 private:
1685 /*
1686 These functions check if the value on the current row exceeds the maximum or
1687 minimum value seen so far, and update the current max/min stored in
1688 result_field, if needed.
1689 */
1696};
1697
1698class Item_sum_min final : public Item_sum_hybrid {
1699 public:
1700 Item_sum_min(Item *item_par) : Item_sum_hybrid(item_par, true) {}
1701 Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
1702 : Item_sum_hybrid(pos, item_par, true, w) {}
1703 Item_sum_min(THD *thd, const Item_sum_min *item)
1704 : Item_sum_hybrid(thd, item) {}
1705 enum Sumfunctype sum_func() const override { return MIN_FUNC; }
1706 const char *func_name() const override { return "min"; }
1707
1708 private:
1709 Item_sum_min *clone_hybrid(THD *thd) const override;
1710};
1711
1712class Item_sum_max final : public Item_sum_hybrid {
1713 public:
1714 Item_sum_max(Item *item_par) : Item_sum_hybrid(item_par, false) {}
1715 Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
1716 : Item_sum_hybrid(pos, item_par, false, w) {}
1717 Item_sum_max(THD *thd, const Item_sum_max *item)
1718 : Item_sum_hybrid(thd, item) {}
1719 enum Sumfunctype sum_func() const override { return MAX_FUNC; }
1720 const char *func_name() const override { return "max"; }
1721
1722 private:
1723 Item_sum_max *clone_hybrid(THD *thd) const override;
1724};
1725
1726/**
1727 Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
1728
1729 Each of them is both a set function and a framing window function.
1730*/
1731class Item_sum_bit : public Item_sum {
1733 /// Stores the neutral element for function
1735 /// Stores the result value for the INT_RESULT
1737 /// Stores the result value for the STRING_RESULT
1739 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1741 /// Buffer used to avoid String allocation in the constructor
1742 const char initial_value_buff_storage[1] = {0};
1743
1744 /**
1745 Execution state (windowing): this is for counting rows entering and leaving
1746 the window frame, see #m_frame_null_count.
1747 */
1749
1750 /**
1751 Execution state (windowing): this is for counting NULLs of rows entering
1752 and leaving the window frame, when we use optimized inverse-based
1753 computations. By comparison with m_count we can know how many non-NULLs are
1754 in the frame.
1755 */
1757
1758 /**
1759 Execution state (windowing): Used for AND, OR to be able to invert window
1760 functions in optimized mode.
1761
1762 For the optimized code path of BIT_XXX wfs, we keep track of the number of
1763 bit values (0's or 1's; see below) seen in a frame using a 64 bits counter
1764 pr bit. This lets us compute the value of OR by just inspecting:
1765
1766 - the number of 1's in the previous frame
1767 - whether any removed row(s) is a 1
1768 - whether any added row(s) is a 1
1769
1770 Similarly for AND, we keep track of the number of 0's seen for a particular
1771 bit. To do this trick we need a counter per bit position. This array holds
1772 these counters.
1773
1774 Note that for XOR, the inverse operation is identical to the operation,
1775 so we don't need the above.
1776 */
1778 /*
1779 Size of allocated array m_digit_cnt.
1780 The size is DIGIT_CNT_CARD (for integer types) or ::max_length * 8 for bit
1781 strings.
1782 */
1784
1785 static constexpr uint DIGIT_CNT_CARD = sizeof(ulonglong) * 8;
1786
1787 protected:
1788 bool m_is_xor; ///< true iff BIT_XOR
1789
1790 public:
1791 Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg,
1792 PT_window *w)
1793 : Item_sum(pos, item_par, w),
1794 reset_bits(reset_arg),
1795 bits(reset_arg),
1797 m_count(0),
1801 m_is_xor(false) {}
1802
1803 /// Copy constructor, used for executing subqueries with temporary tables
1805 : Item_sum(thd, item),
1806 reset_bits(item->reset_bits),
1807 bits(item->bits),
1809 hybrid_type(item->hybrid_type),
1810 m_count(item->m_count),
1814 m_is_xor(item->m_is_xor) {
1815 /*
1816 This constructor should only be called during the Optimize stage.
1817 Asserting that the item was not evaluated yet.
1818 */
1819 assert(item->value_buff.length() == 1);
1820 assert(item->bits == item->reset_bits);
1821 }
1822
1823 Item *result_item(Field *) override {
1824 return new Item_sum_bit_field(hybrid_type, this, reset_bits);
1825 }
1826
1827 enum Sumfunctype sum_func() const override { return SUM_BIT_FUNC; }
1828 enum Item_result result_type() const override { return hybrid_type; }
1829 void clear() override;
1830 longlong val_int() override;
1831 double val_real() override;
1832 String *val_str(String *str) override;
1833 my_decimal *val_decimal(my_decimal *decimal_value) override;
1834 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1835 bool get_time(MYSQL_TIME *ltime) override;
1836 void reset_field() override;
1837 void update_field() override;
1838 bool resolve_type(THD *) override;
1839 bool fix_fields(THD *thd, Item **ref) override;
1840 void cleanup() override {
1841 bits = reset_bits;
1842 // At end of one execution of statement, free buffer to reclaim memory:
1845 }
1846
1847 /**
1848 Common implementation of Item_sum_or::add, Item_sum_and:add
1849 and Item_sum_xor::add.
1850 */
1851 bool add() override;
1852 /// @returns true iff this is BIT_AND.
1853 inline bool is_and() const { return reset_bits != 0; }
1854
1855 private:
1856 /**
1857 Accumulate the value of 's1' (if in string mode) or of 'b1' (if in integer
1858 mode). Updates 'value_buff' or 'bits'.
1859
1860 @param s1 argument to accumulate
1861 @param b1 argument to accumulate
1862
1863 @returns true if error
1864 */
1865 bool add_bits(const String *s1, ulonglong b1);
1866
1867 /**
1868 For windowing: perform inverse aggregation. "De-accumulate" the value of
1869 's1' (if in string mode) or of 'b1' (if in integer mode). Updates
1870 'value_buff' or 'bits'.
1871
1872 For BIT_XOR we simply apply XOR as it's its inverse operation. For BIT_OR
1873 and BIT_AND we do the rest below.
1874
1875 For each bit in argument, decrement the corresponding bits's counter
1876 ('m_digit_cnt') for that bit as follows: for BIT_AND, decrement the
1877 counter if we see a zero in that bit; for BIT_OR decrement the counter if
1878 we see a 1 in that bit. Next, update 'value_buff' or 'bits' using the
1879 resulting counters: for each bit, for BIT_AND, set bit if we have counter
1880 == 0, i.e. we have no zero bits for that bit in the frame (yet). For
1881 BIT_OR, set bit if we have counter > 0, i.e. at least one row in the frame
1882 has that bit set.
1883
1884 @param s1 the bits to be inverted from the aggregate value
1885 @param b1 the bits to be inverted from the aggregate value
1886 */
1887 void remove_bits(const String *s1, ulonglong b1);
1888};
1889
1890class Item_sum_or final : public Item_sum_bit {
1891 public:
1892 Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
1893 : Item_sum_bit(pos, item_par, 0LL, w) {}
1894
1895 Item_sum_or(THD *thd, Item_sum_or *item) : Item_sum_bit(thd, item) {}
1896 const char *func_name() const override { return "bit_or"; }
1897 Item *copy_or_same(THD *thd) override;
1898};
1899
1900class Item_sum_and final : public Item_sum_bit {
1901 public:
1902 Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
1903 : Item_sum_bit(pos, item_par, ULLONG_MAX, w) {}
1904
1905 Item_sum_and(THD *thd, Item_sum_and *item) : Item_sum_bit(thd, item) {}
1906 const char *func_name() const override { return "bit_and"; }
1907 Item *copy_or_same(THD *thd) override;
1908};
1909
1910class Item_sum_xor final : public Item_sum_bit {
1911 public:
1912 Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
1913 : Item_sum_bit(pos, item_par, 0LL, w) {
1914 m_is_xor = true;
1915 }
1916
1917 Item_sum_xor(THD *thd, Item_sum_xor *item) : Item_sum_bit(thd, item) {}
1918 const char *func_name() const override { return "bit_xor"; }
1919 Item *copy_or_same(THD *thd) override;
1920};
1921
1922/*
1923 User defined aggregates
1924*/
1925
1926class Item_udf_sum : public Item_sum {
1928
1929 protected:
1931
1932 public:
1933 Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1934 : Item_sum(pos, opt_list, nullptr), udf(udf_arg) {
1936 }
1938 : Item_sum(thd, item), udf(item->udf) {
1939 udf.m_original = false;
1940 }
1941 ~Item_udf_sum() override {
1943 }
1944
1945 bool do_itemize(Parse_context *pc, Item **res) override;
1946 const char *func_name() const override { return udf.name(); }
1947 bool fix_fields(THD *thd, Item **ref) override {
1948 assert(!fixed);
1949
1950 if (init_sum_func_check(thd)) return true;
1951
1952 fixed = true;
1953 if (udf.fix_fields(thd, this, this->arg_count, this->args)) return true;
1954
1955 return check_sum_func(thd, ref);
1956 }
1957 enum Sumfunctype sum_func() const override { return UDF_SUM_FUNC; }
1958
1959 void clear() override;
1960 bool add() override;
1961 void reset_field() override {}
1962 void update_field() override {}
1963 void cleanup() override;
1964 void print(const THD *thd, String *str,
1965 enum_query_type query_type) const override;
1966};
1967
1968class Item_sum_udf_float final : public Item_udf_sum {
1969 public:
1970 Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1971 : Item_udf_sum(pos, udf_arg, opt_list) {}
1973 : Item_udf_sum(thd, item) {}
1974 longlong val_int() override {
1975 assert(fixed);
1976 return (longlong)rint(Item_sum_udf_float::val_real());
1977 }
1978 double val_real() override;
1979 String *val_str(String *str) override;
1980 my_decimal *val_decimal(my_decimal *) override;
1981 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1982 return get_date_from_real(ltime, fuzzydate);
1983 }
1984 bool get_time(MYSQL_TIME *ltime) override {
1985 return get_time_from_real(ltime);
1986 }
1987 bool resolve_type(THD *) override {
1990 return false;
1991 }
1992 Item *copy_or_same(THD *thd) override;
1993};
1994
1995class Item_sum_udf_int final : public Item_udf_sum {
1996 public:
1997 Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1998 : Item_udf_sum(pos, udf_arg, opt_list) {}
2000 : Item_udf_sum(thd, item) {}
2001 longlong val_int() override;
2002 double val_real() override {
2003 assert(fixed);
2004 return (double)Item_sum_udf_int::val_int();
2005 }
2006 String *val_str(String *str) override;
2007 my_decimal *val_decimal(my_decimal *) override;
2008 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2009 return get_date_from_int(ltime, fuzzydate);
2010 }
2011 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2012 enum Item_result result_type() const override { return INT_RESULT; }
2013 bool resolve_type(THD *) override {
2015 return false;
2016 }
2017 Item *copy_or_same(THD *thd) override;
2018};
2019
2020class Item_sum_udf_str final : public Item_udf_sum {
2021 public:
2022 Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2023 : Item_udf_sum(pos, udf_arg, opt_list) {}
2025 : Item_udf_sum(thd, item) {}
2026 String *val_str(String *) override;
2027 double val_real() override {
2028 int err_not_used;
2029 const char *end_not_used;
2030 String *res;
2031 res = val_str(&str_value);
2032 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2033 &end_not_used, &err_not_used)
2034 : 0.0;
2035 }
2036 longlong val_int() override {
2037 int err_not_used;
2038 String *res;
2039 const CHARSET_INFO *cs;
2040
2041 if (!(res = val_str(&str_value))) return 0; /* Null value */
2042 cs = res->charset();
2043 const char *end = res->ptr() + res->length();
2044 return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
2045 }
2047 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2048 return get_date_from_string(ltime, fuzzydate);
2049 }
2050 bool get_time(MYSQL_TIME *ltime) override {
2051 return get_time_from_string(ltime);
2052 }
2053 enum Item_result result_type() const override { return STRING_RESULT; }
2054 bool resolve_type(THD *) override;
2055 Item *copy_or_same(THD *thd) override;
2056};
2057
2059 public:
2060 Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg,
2061 PT_item_list *opt_list)
2062 : Item_udf_sum(pos, udf_arg, opt_list) {}
2064 : Item_udf_sum(thd, item) {}
2065 String *val_str(String *) override;
2066 double val_real() override;
2067 longlong val_int() override;
2068 my_decimal *val_decimal(my_decimal *) override;
2069 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2070 return get_date_from_decimal(ltime, fuzzydate);
2071 }
2072 bool get_time(MYSQL_TIME *ltime) override {
2073 return get_time_from_decimal(ltime);
2074 }
2075 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2076 bool resolve_type(THD *) override {
2079 return false;
2080 }
2081 Item *copy_or_same(THD *thd) override;
2082};
2083
2084int group_concat_key_cmp_with_distinct(const void *arg, const void *key1,
2085 const void *key2);
2086int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2087 const void *key2);
2088int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2089 void *item_arg);
2090
2091class Item_func_group_concat final : public Item_sum {
2093
2094 /// True if GROUP CONCAT has the DISTINCT attribute
2096 /// The number of ORDER BY items.
2098 /// The number of selected items, aka the concat field list
2100 /// Resolver context, points to containing query block
2102 /// String containing separator between group items
2104 /// Describes the temporary table used to perform group concat
2108 TREE *tree{nullptr};
2109
2110 /**
2111 If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
2112 out duplicates.
2113 @see Item_func_group_concat::setup
2114 @see Item_func_group_concat::add
2115 @see Item_func_group_concat::clear
2116 */
2118 /// Temporary table used to perform group concat
2119 TABLE *table{nullptr};
2121 uint row_count{0};
2122 /**
2123 The maximum permitted result length in bytes as set in
2124 group_concat_max_len system variable
2125 */
2127 bool warning_for_row{false};
2129 /// True if result has been written to output buffer.
2131 /**
2132 Following is 0 normal object and pointer to original one for copy
2133 (to correctly free resources)
2134 */
2136
2137 friend int group_concat_key_cmp_with_distinct(const void *arg,
2138 const void *key1,
2139 const void *key2);
2140 friend int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2141 const void *key2);
2142 friend int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2143 void *item_arg);
2144
2145 public:
2146 Item_func_group_concat(const POS &pos, bool is_distinct,
2147 PT_item_list *select_list,
2148 PT_order_list *opt_order_list, String *separator,
2149 PT_window *w);
2150
2153 assert(original != nullptr || unique_filter == nullptr);
2154 }
2155
2156 bool do_itemize(Parse_context *pc, Item **res) override;
2157 void cleanup() override;
2158
2159 enum Sumfunctype sum_func() const override { return GROUP_CONCAT_FUNC; }
2160 const char *func_name() const override { return "group_concat"; }
2161 Item_result result_type() const override { return STRING_RESULT; }
2162 Field *make_string_field(TABLE *table_arg) const override;
2163 void clear() override;
2164 bool add() override;
2165 void reset_field() override { assert(0); } // not used
2166 void update_field() override { assert(0); } // not used
2167 bool fix_fields(THD *, Item **) override;
2168 bool setup(THD *thd) override;
2169 void make_unique() override;
2170 double val_real() override;
2171 longlong val_int() override {
2172 String *res;
2173 int error;
2174 if (!(res = val_str(&str_value))) return (longlong)0;
2175 const char *end_ptr = res->ptr() + res->length();
2176 return my_strtoll10(res->ptr(), &end_ptr, &error);
2177 }
2178 my_decimal *val_decimal(my_decimal *decimal_value) override {
2179 return val_decimal_from_string(decimal_value);
2180 }
2181 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2182 return get_date_from_string(ltime, fuzzydate);
2183 }
2184 bool get_time(MYSQL_TIME *ltime) override {
2185 return get_time_from_string(ltime);
2186 }
2187
2188 bool has_distinct() const noexcept { return distinct; }
2189 const String *get_separator_str() const noexcept { return separator; }
2190 uint32_t get_group_concat_max_len() const noexcept {
2191 return group_concat_max_len;
2192 }
2193 const Mem_root_array<ORDER> &get_order_array() const noexcept {
2194 return order_array;
2195 }
2196
2197 String *val_str(String *str) override;
2198 Item *copy_or_same(THD *thd) override;
2199 void no_rows_in_result() override;
2200 void print(const THD *thd, String *str,
2201 enum_query_type query_type) const override;
2202 bool change_context_processor(uchar *arg) override {
2203 context = pointer_cast<Item_ident::Change_context *>(arg)->m_context;
2204 return false;
2205 }
2206
2208 Window_evaluation_requirements *) override {
2210 return true;
2211 }
2212};
2213
2214/**
2215 Common parent class for window functions that always work on the entire
2216 partition, even if a frame is defined.
2217
2218 The subclasses can be divided in two disjoint sub-categories:
2219 - one-pass
2220 - two-pass (requires partition cardinality to be evaluated)
2221 cf. method needs_partition_cardinality.
2222*/
2225
2226 public:
2227 Item_non_framing_wf(const POS &pos, PT_window *w) : Item_sum(pos, w) {}
2229 : Item_sum(pos, a, w) {}
2231 : Item_sum(pos, opt_list, w) {}
2233
2234 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2235 return get_date_from_numeric(ltime, fuzzydate);
2236 }
2237
2238 bool get_time(MYSQL_TIME *ltime) override {
2239 return get_time_from_numeric(ltime);
2240 }
2241
2242 void reset_field() override { assert(false); }
2243 void update_field() override { assert(false); }
2244 bool add() override {
2245 assert(false);
2246 return false;
2247 }
2248
2249 bool fix_fields(THD *thd, Item **items) override;
2250
2251 bool framing() const override { return false; }
2252};
2253
2254/**
2255 ROW_NUMBER window function, cf. SQL 2003 Section 6.10 <window function>
2256*/
2258 // Execution state variables
2259 ulonglong m_ctr; ///< Increment for each row in partition
2260
2261 public:
2263 : Item_non_framing_wf(pos, w), m_ctr(0) {
2264 unsigned_flag = true;
2265 }
2266
2267 const char *func_name() const override { return "row_number"; }
2268 enum Sumfunctype sum_func() const override { return ROW_NUMBER_FUNC; }
2269
2270 bool resolve_type(THD *thd [[maybe_unused]]) override {
2272 return false;
2273 }
2274
2275 longlong val_int() override;
2276 double val_real() override;
2277 my_decimal *val_decimal(my_decimal *buff) override;
2278 String *val_str(String *) override;
2279
2280 void clear() override;
2281
2282 Item_result result_type() const override { return INT_RESULT; }
2283
2285 Window_evaluation_requirements *) override {
2286 return false;
2287 }
2288};
2289
2290/**
2291 RANK or DENSE_RANK window function, cf. SQL 2003 Section 6.10 <window
2292 function>
2293*/
2296 bool m_dense; ///< If true, the object represents DENSE_RANK
2297 // Execution state variables
2298 ulonglong m_rank_ctr; ///< Increment when window order columns change
2299 ulonglong m_duplicates; ///< Needed to make RANK different from DENSE_RANK
2301 m_previous; ///< Values of previous row's ORDER BY items
2302 public:
2303 Item_rank(const POS &pos, bool dense, PT_window *w)
2304 : Item_non_framing_wf(pos, w),
2305 m_dense(dense),
2306 m_rank_ctr(0),
2307 m_duplicates(0),
2309 unsigned_flag = true;
2310 }
2311
2312 ~Item_rank() override;
2313
2314 const char *func_name() const override {
2315 return m_dense ? "dense_rank" : "rank";
2316 }
2317
2318 enum Sumfunctype sum_func() const override {
2320 }
2321
2322 bool resolve_type(THD *thd [[maybe_unused]]) override {
2324 return false;
2325 }
2326
2327 longlong val_int() override;
2328 double val_real() override;
2329 my_decimal *val_decimal(my_decimal *buff) override;
2330 String *val_str(String *) override;
2331
2332 void update_after_wf_arguments_changed(THD *thd) override;
2333 bool check_wf_semantics1(THD *thd, Query_block *select,
2334 Window_evaluation_requirements *reqs) override;
2335 /**
2336 Clear state for a new partition
2337 */
2338 void clear() override;
2339 Item_result result_type() const override { return INT_RESULT; }
2340};
2341
2342/**
2343 CUME_DIST window function, cf. SQL 2003 Section 6.10 <window function>
2344*/
2347
2348 public:
2350
2351 const char *func_name() const override { return "cume_dist"; }
2352 enum Sumfunctype sum_func() const override { return CUME_DIST_FUNC; }
2353
2354 bool resolve_type(THD *thd [[maybe_unused]]) override {
2356 return false;
2357 }
2358
2359 bool check_wf_semantics1(THD *thd, Query_block *select,
2360 Window_evaluation_requirements *reqs) override;
2361
2362 bool needs_partition_cardinality() const override { return true; }
2363 void clear() override {}
2364 longlong val_int() override;
2365 double val_real() override;
2366 String *val_str(String *) override;
2368 Item_result result_type() const override { return REAL_RESULT; }
2369};
2370
2371/**
2372 PERCENT_RANK window function, cf. SQL 2003 Section 6.10 <window function>
2373*/
2376 // Execution state variables
2377 ulonglong m_rank_ctr; ///< Increment when window order columns change
2378 ulonglong m_peers; ///< Needed to make PERCENT_RANK same for peers
2379 /**
2380 Set when the last peer has been visited. Needed to increment m_rank_ctr.
2381 */
2383
2384 public:
2386 : Item_non_framing_wf(pos, w),
2387 m_rank_ctr(0),
2388 m_peers(0),
2389 m_last_peer_visited(false) {}
2390
2392 const char *func_name() const override { return "percent_rank"; }
2393 enum Sumfunctype sum_func() const override { return PERCENT_RANK_FUNC; }
2394
2395 bool resolve_type(THD *thd [[maybe_unused]]) override {
2397 return false;
2398 }
2399
2400 bool check_wf_semantics1(THD *thd, Query_block *select,
2401 Window_evaluation_requirements *reqs) override;
2402 bool needs_partition_cardinality() const override { return true; }
2403
2404 void clear() override;
2405 longlong val_int() override;
2406 double val_real() override;
2407 String *val_str(String *) override;
2409 Item_result result_type() const override { return REAL_RESULT; }
2410};
2411
2412/**
2413 NTILE window function, cf. SQL 2011 Section 6.10 <window function>
2414*/
2418
2419 public:
2420 Item_ntile(const POS &pos, Item *a, PT_window *w)
2421 : Item_non_framing_wf(pos, a, w), m_value(0) {
2422 unsigned_flag = true;
2423 }
2424
2425 const char *func_name() const override { return "ntile"; }
2426 enum Sumfunctype sum_func() const override { return NTILE_FUNC; }
2427
2428 bool resolve_type(THD *thd) override {
2429 if (args[0]->propagate_type(thd, MYSQL_TYPE_LONGLONG, true)) return true;
2431 return false;
2432 }
2433
2434 bool fix_fields(THD *thd, Item **items) override;
2435
2436 longlong val_int() override;
2437 double val_real() override;
2438 my_decimal *val_decimal(my_decimal *buff) override;
2439 String *val_str(String *) override;
2440
2441 bool check_wf_semantics1(THD *thd, Query_block *select,
2442 Window_evaluation_requirements *reqs) override;
2444 Item_result result_type() const override { return INT_RESULT; }
2445 void clear() override {}
2446 bool needs_partition_cardinality() const override { return true; }
2447};
2448
2449/**
2450 LEAD/LAG window functions, cf. SQL 2011 Section 6.10 <window function>
2451*/
2454 bool m_is_lead; ///< if true, the function is LEAD, else LAG
2455 int64 m_n; ///< canonicalized offset value
2459 /**
2460 Execution state: if set, we already have a value for current row.
2461 State is used to avoid interference with other LEAD/LAG functions on
2462 the same window, since they share the same eval loop and they should
2463 trigger evaluation only when they are on the "right" row relative to
2464 current row. For other offsets, return NULL if we don't know the value
2465 for this function yet, or if we do (m_has_value==true), return the
2466 found value.
2467 */
2469 bool m_use_default; ///< execution state: use default value for current row
2471
2472 public:
2473 Item_lead_lag(const POS &pos, bool lead,
2474 PT_item_list *opt_list, // [0] expr, [1] offset, [2] default
2475 enum_null_treatment null_treatment, PT_window *w)
2476 : Item_non_framing_wf(pos, opt_list, w),
2477 m_null_treatment(null_treatment),
2478 m_is_lead(lead),
2479 m_n(0),
2483 m_has_value(false),
2484 m_use_default(false) {}
2485
2486 const char *func_name() const override {
2487 return (m_is_lead ? "lead" : "lag");
2488 }
2489 enum Sumfunctype sum_func() const override { return LEAD_LAG_FUNC; }
2490
2491 bool resolve_type(THD *thd) override;
2492 bool fix_fields(THD *thd, Item **items) override;
2493 TYPELIB *get_typelib() const override;
2494 void update_after_wf_arguments_changed(THD *thd) override;
2495 void clear() override;
2496 bool check_wf_semantics1(THD *thd, Query_block *select,
2497 Window_evaluation_requirements *reqs) override;
2499 enum Item_result result_type() const override { return m_hybrid_type; }
2500
2501 longlong val_int() override;
2502 double val_real() override;
2503 String *val_str(String *str) override;
2504 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2505
2506 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2507 bool get_time(MYSQL_TIME *ltime) override;
2508 bool val_json(Json_wrapper *wr) override;
2509
2510 bool needs_partition_cardinality() const override {
2511 /*
2512 A possible optimization here: if LAG, we are only interested in rows we
2513 have already seen, so we might compute the result without reading the
2514 entire partition as soon as we have the current row. Similarly, a small
2515 LEAD value might avoid reading the entire partition also, giving shorter
2516 time to first result. For now, we read the entirely partition for these
2517 window functions - for simplicity.
2518 */
2519 return true;
2520 }
2521
2522 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2523 mem_root_deque<Item *> *fields) override;
2524
2525 void set_has_value(bool value) { m_has_value = value; }
2526 bool has_value() const { return m_has_value; }
2527
2528 void set_use_default(bool value) { m_use_default = value; }
2529 bool use_default() const { return m_use_default; }
2530
2531 private:
2532 bool setup_lead_lag();
2533 /**
2534 Core logic of LEAD/LAG window functions
2535
2536 @return true if computation yielded a NULL or error
2537 */
2538 bool compute();
2539};
2540
2541/**
2542 FIRST_VALUE/LAST_VALUE window functions, cf. SQL 2011 Section 6.10 <window
2543 function>
2544*/
2546 bool m_is_first; ///< if true, the function is FIRST_VALUE, else LAST_VALUE
2550 int64 cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2552
2553 public:
2554 Item_first_last_value(const POS &pos, bool first, Item *a,
2555 enum_null_treatment null_treatment, PT_window *w)
2556 : Item_sum(pos, a, w),
2557 m_is_first(first),
2558 m_null_treatment(null_treatment),
2561 cnt(0) {}
2562
2563 const char *func_name() const override {
2564 return m_is_first ? "first_value" : "last_value";
2565 }
2566
2567 enum Sumfunctype sum_func() const override { return FIRST_LAST_VALUE_FUNC; }
2568
2569 bool resolve_type(THD *thd) override;
2570 bool fix_fields(THD *thd, Item **items) override;
2571 void update_after_wf_arguments_changed(THD *thd) override;
2572 void clear() override;
2573 bool check_wf_semantics1(THD *thd, Query_block *select,
2574 Window_evaluation_requirements *reqs) override;
2575 enum Item_result result_type() const override { return m_hybrid_type; }
2576
2577 longlong val_int() override;
2578 double val_real() override;
2579 String *val_str(String *str) override;
2580 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2581
2582 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2583 bool get_time(MYSQL_TIME *ltime) override;
2584 bool val_json(Json_wrapper *wr) override;
2585
2586 void reset_field() override { assert(false); }
2587 void update_field() override { assert(false); }
2588 bool add() override {
2589 assert(false);
2590 return false;
2591 }
2592
2593 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2594 mem_root_deque<Item *> *fields) override;
2595 bool uses_only_one_row() const override { return true; }
2596
2597 private:
2598 bool setup_first_last();
2599 /**
2600 Core logic of FIRST/LAST_VALUE window functions
2601
2602 @return true if computation yielded a NULL or error
2603 */
2604 bool compute();
2605};
2606
2607/**
2608 NTH_VALUE window function, cf. SQL 2011 Section 6.10 <window
2609 function>
2610*/
2611class Item_nth_value : public Item_sum {
2613 int64 m_n; ///< The N of the function
2614 bool m_from_last; ///< true iff FROM_LAST was specified
2618 int64 m_cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2619
2621
2622 public:
2623 Item_nth_value(const POS &pos, PT_item_list *a, bool from_last,
2624 enum_null_treatment null_treatment, PT_window *w)
2625 : Item_sum(pos, a, w),
2626 m_null_treatment(null_treatment),
2627 m_n(0),
2628 m_from_last(from_last),
2631 m_cnt(0) {}
2632
2633 const char *func_name() const override { return "nth_value"; }
2634 enum Sumfunctype sum_func() const override { return NTH_VALUE_FUNC; }
2635
2636 bool resolve_type(THD *thd) override;
2637 bool fix_fields(THD *thd, Item **items) override;
2638 void update_after_wf_arguments_changed(THD *thd) override;
2639 bool setup_nth();
2640 void clear() override;
2641
2642 bool check_wf_semantics1(THD *thd, Query_block *select,
2643 Window_evaluation_requirements *reqs) override;
2645
2646 enum Item_result result_type() const override { return m_hybrid_type; }
2647
2648 longlong val_int() override;
2649 double val_real() override;
2650 String *val_str(String *str) override;
2651 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2652
2653 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2654 bool get_time(MYSQL_TIME *ltime) override;
2655 bool val_json(Json_wrapper *wr) override;
2656
2657 void reset_field() override { assert(false); }
2658 void update_field() override { assert(false); }
2659 bool add() override {
2660 assert(false);
2661 return false;
2662 }
2663
2664 bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
2665 mem_root_deque<Item *> *fields) override;
2666 bool uses_only_one_row() const override { return true; }
2667
2668 private:
2669 /**
2670 Core logic of NTH_VALUE window functions
2671
2672 @return true if computation yielded a NULL or error
2673 */
2674 bool compute();
2675};
2676
2677/**
2678 Class for implementation of the GROUPING function. The GROUPING
2679 function distinguishes super-aggregate rows from regular grouped
2680 rows. GROUP BY extensions such as ROLLUP and CUBE produce
2681 super-aggregate rows where the set of all values is represented
2682 by null. Using the GROUPING function, you can distinguish a null
2683 representing the set of all values in a super-aggregate row from
2684 a NULL in a regular row.
2685*/
2687 public:
2690 }
2691 const char *func_name() const override { return "grouping"; }
2692 enum Functype functype() const override { return GROUPING_FUNC; }
2693 longlong val_int() override;
2694 bool aggregate_check_group(uchar *arg) override;
2695 bool fix_fields(THD *thd, Item **ref) override;
2696 void update_used_tables() override;
2697 bool aggregate_check_distinct(uchar *arg) override;
2698
2699 private:
2700 /// The query block in which this function is called.
2702};
2703
2704/**
2705 A wrapper Item that contains a number of aggregate items, one for each level
2706 of rollup (see Item_rollup_group_item for numbering conventions). When
2707 aggregating, every aggregator is either reset or updated as per the correct
2708 level, and when returning a value, the correct child item corresponding to
2709 the current rollup level is queried.
2710 */
2712 public:
2713 explicit Item_rollup_sum_switcher(List<Item> *sum_func_per_level)
2714 : Item_sum((*sum_func_per_level)[0]),
2715 m_num_levels(sum_func_per_level->size()) {
2716 args = (*THR_MALLOC)->ArrayAlloc<Item *>(sum_func_per_level->size());
2717 int i = 0;
2718 for (Item &item : *sum_func_per_level) {
2719 args[i++] = &item;
2720 }
2724 hidden = master()->hidden;
2728 }
2729 double val_real() override;
2730 longlong val_int() override;
2731 String *val_str(String *str) override;
2733 bool val_json(Json_wrapper *result) override;
2734 bool is_null() override;
2735 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2736 bool get_time(MYSQL_TIME *ltime) override;
2737 const char *func_name() const override { return "rollup_sum_switcher"; }
2738 table_map used_tables() const override { return master()->used_tables(); }
2739 Item_result result_type() const override { return master()->result_type(); }
2740 bool resolve_type(THD *) override {
2742 return false;
2743 }
2744 void print(const THD *thd, String *str,
2745 enum_query_type query_type) const override;
2746 Field *create_tmp_field(bool group, TABLE *table) override;
2747
2748 enum Sumfunctype sum_func() const override { return master()->sum_func(); }
2749 enum Sumfunctype real_sum_func() const override {
2751 }
2752 void reset_field() override { assert(false); }
2753 void update_field() override { assert(false); }
2754 void clear() override;
2755 bool add() override {
2756 assert(false);
2757 return true;
2758 }
2759
2760 bool reset_and_add_for_rollup(int last_unchanged_group_item_idx);
2761
2762 int set_aggregator(Aggregator::Aggregator_type aggregator) override;
2763 bool aggregator_setup(THD *thd) override;
2764 inline bool aggregator_add_all() {
2765 for (int i = 0; i < m_num_levels; ++i) {
2766 if (child(i)->aggregator_add()) {
2767 return true;
2768 }
2769 }
2770 return false;
2771 }
2772
2773 // Used when create_tmp_table() needs to delay application of aggregate
2774 // functions to a later stage in the query processing.
2775 Item *get_arg(uint i) override { return master()->get_arg(i); }
2776 const Item *get_arg(uint i) const override { return master()->get_arg(i); }
2777 Item *set_arg(THD *thd, uint i, Item *new_val) override {
2778 Item *ret = nullptr;
2779 for (int j = 0; j < m_num_levels; ++j) {
2780 ret = child(j)->set_arg(thd, i, new_val);
2781 }
2782 return ret; // Return the last one, arbitrarily.
2783 }
2784 uint argument_count() const override { return master()->argument_count(); }
2785
2786 // Used by AggregateIterator.
2788 inline Item_sum *master() const { return child(0); }
2789
2790 bool is_rollup_sum_wrapper() const override { return true; }
2791 const Item_sum *unwrap_sum() const override { return master(); }
2792 Item_sum *unwrap_sum() override { return master(); }
2793
2794 private:
2795 inline Item *current_arg() const;
2796 inline Item_sum *child(size_t i) const {
2797 return down_cast<Item_sum *>(args[i]);
2798 }
2799
2800 const int m_num_levels;
2802};
2803/// Implements ST_Collect which aggregates geometries into Multipoints,
2804/// Multilinestrings, Multipolygons and Geometrycollections.
2805
2807 private:
2808 std::optional<gis::srid_t> srid;
2809 std::unique_ptr<gis::Geometrycollection> m_geometrycollection;
2810 void pop_front();
2811
2812 public:
2814 Item_sum_collect(THD *thd, Item_sum *item) : Item_sum(thd, item) {
2816 }
2817 Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
2818 : Item_sum(pos, a, w) {
2819 set_distinct(distinct);
2821 }
2822
2823 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2824 longlong val_int() override { return val_int_from_string(); }
2825 double val_real() override { return val_real_from_string(); }
2826 bool get_date(MYSQL_TIME *, my_time_flags_t) override { return true; }
2827 bool get_time(MYSQL_TIME *) override { return true; }
2828 enum Sumfunctype sum_func() const override { return GEOMETRY_AGGREGATE_FUNC; }
2829 Item_result result_type() const override { return STRING_RESULT; }
2831 /*
2832 In contrast to Item_sum, Item_sum_collect always uses Aggregator_simple,
2833 and only needs to reset its aggregator when called.
2834 */
2835 if (aggr != nullptr) {
2836 aggr->clear();
2837 return false;
2838 }
2839
2840 aggr = new (*THR_MALLOC) Aggregator_simple(this);
2841 return aggr ? false : true;
2842 }
2843
2844 bool fix_fields(THD *thd, Item **ref) override;
2845 /*
2846 We need to override check_wf_semantics1 because it reports an error when
2847 with_distinct is set.
2848 */
2849 bool check_wf_semantics1(THD *thd, Query_block *,
2851
2852 Item *copy_or_same(THD *thd) override;
2853
2854 void update_field() override;
2855 void reset_field() override;
2856
2857 String *val_str(String *str) override;
2858
2859 bool add() override;
2860
2861 void read_result_field();
2862 void store_result_field();
2863
2864 void clear() override;
2865 const char *func_name() const override { return "st_collect"; }
2866};
2867
2868#endif /* ITEM_SUM_INCLUDED */
Kerberos Client Authentication nullptr
Definition: auth_kerberos_client_plugin.cc:251
The distinct aggregator.
Definition: item_sum.h:816
uint tree_key_length
Definition: item_sum.h:869
uint32 * field_lengths
Definition: item_sum.h:846
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2199
bool endup_done
Definition: item_sum.h:827
Const_distinct
Definition: item_sum.h:871
@ CONST_NULL
Set to true if the result is known to be always NULL.
Definition: item_sum.h:882
@ CONST_NOT_NULL
Set to true if count distinct is on only const items.
Definition: item_sum.h:889
@ NOT_CONST
Definition: item_sum.h:872
static int composite_key_cmp(const void *arg, const void *a, const void *b)
Correctly compare composite keys.
Definition: item_sum.cc:987
void endup() override
Calculate the aggregate function value.
Definition: item_sum.cc:1373
~Aggregator_distinct() override
Definition: item_sum.cc:2149
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:900
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2194
void clear() override
Invalidate calculated value and clear the distinct rows.
Definition: item_sum.cc:1267
bool setup(THD *) override
Called before feeding the first row.
Definition: item_sum.cc:1058
bool unique_walk_function(void *element)
Aggregate a distinct row from the distinct hash table.
Definition: item_sum.cc:2142
Unique * tree
Definition: item_sum.h:861
Temp_table_param * tmp_table_param
Definition: item_sum.h:852
bool add() override
Process incoming row.
Definition: item_sum.cc:1299
TABLE * table
Definition: item_sum.h:838
Aggregator_type Aggrtype() override
Definition: item_sum.h:911
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2189
enum Aggregator_distinct::Const_distinct const_distinct
Aggregator_distinct(Item_sum *sum)
Definition: item_sum.h:903
The pass-through aggregator.
Definition: item_sum.h:930
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:936
Aggregator_simple(Item_sum *sum)
Definition: item_sum.h:932
bool setup(THD *thd) override
Called before adding the first row.
Definition: item_sum.h:935
Aggregator_type Aggrtype() override
Definition: item_sum.h:933
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2166
void endup() override
Called when there are no more data and the final value is to be retrieved.
Definition: item_sum.h:938
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2170
bool add() override
Called when there's a new value to be aggregated.
Definition: item_sum.h:937
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2174
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:205
Definition: field.h:577
Definition: item_sum.h:1180
uint prec_increment
Definition: item_sum.h:1183
uint f_precision
Definition: item_sum.h:1182
uint dec_bin_size
Definition: item_sum.h:1182
uint f_scale
Definition: item_sum.h:1182
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3740
String * val_str(String *) override
Definition: item_sum.cc:3755
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1189
enum Type type() const override
Definition: item_sum.h:1185
double val_real() override
Definition: item_sum.cc:3725
Item_avg_field(Item_result res_type, Item_sum_avg *item)
Definition: item_sum.cc:3706
Definition: item.h:6841
CUME_DIST window function, cf.
Definition: item_sum.h:2345
Item_cume_dist(const POS &pos, PT_window *w)
Definition: item_sum.h:2349
Item_non_framing_wf super
Definition: item_sum.h:2346
double val_real() override
Definition: item_sum.cc:4936
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2352
void clear() override
Definition: item_sum.h:2363
const char * func_name() const override
Definition: item_sum.h:2351
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:2362
String * val_str(String *) override
Definition: item_sum.cc:4956
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:4926
longlong val_int() override
Definition: item_sum.cc:4948
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2354
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:4960
Item_result result_type() const override
Definition: item_sum.h:2368
Definition: item.h:4370
FIRST_VALUE/LAST_VALUE window functions, cf.
Definition: item_sum.h:2545
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2567
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5164
void reset_field() override
Definition: item_sum.h:2586
const char * func_name() const override
Definition: item_sum.h:2563
bool setup_first_last()
Definition: item_sum.cc:5206
double val_real() override
Definition: item_sum.cc:5255
String * val_str(String *str) override
Definition: item_sum.cc:5310
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:5223
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:5194
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:5150
bool compute()
Core logic of FIRST/LAST_VALUE window functions.
Definition: item_sum.cc:5227
int64 cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2550
Item_result m_hybrid_type
Definition: item_sum.h:2548
enum Item_result result_type() const override
Definition: item_sum.h:2575
Item_cache * m_value
Definition: item_sum.h:2549
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5286
void update_field() override
Definition: item_sum.h:2587
enum_null_treatment m_null_treatment
Definition: item_sum.h:2547
Item_sum super
Definition: item_sum.h:2551
void clear() override
Definition: item_sum.cc:5217
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2595
bool add() override
Definition: item_sum.h:2588
Item_first_last_value(const POS &pos, bool first, Item *a, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2554
bool m_is_first
if true, the function is FIRST_VALUE, else LAST_VALUE
Definition: item_sum.h:2546
longlong val_int() override
Definition: item_sum.cc:5245
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5296
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5276
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5175
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5265
Definition: item_sum.h:2091
void clear() override
Definition: item_sum.cc:4403
bool m_result_finalized
True if result has been written to output buffer.
Definition: item_sum.h:2130
String result
Definition: item_sum.h:2106
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:2135
longlong val_int() override
Definition: item_sum.h:2171
const String * get_separator_str() const noexcept
Definition: item_sum.h:2189
uint32_t get_group_concat_max_len() const noexcept
Definition: item_sum.h:2190
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.h:2178
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:4149
uint row_count
Definition: item_sum.h:2121
void make_unique() override
Definition: item_sum.cc:4690
bool change_context_processor(uchar *arg) override
Definition: item_sum.h:2202
bool setup(THD *thd) override
Definition: item_sum.cc:4559
Item_sum super
Definition: item_sum.h:2092
bool distinct
True if GROUP CONCAT has the DISTINCT attribute.
Definition: item_sum.h:2095
TABLE * table
Temporary table used to perform group concat.
Definition: item_sum.h:2119
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:4070
double val_real() override
Definition: item_sum.cc:4698
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2159
String * val_str(String *str) override
Definition: item_sum.cc:4705
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:4106
bool force_copy_fields
Definition: item_sum.h:2128
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:4466
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2181
Unique * unique_filter
If DISTINCT is used with this GROUP_CONCAT, this member is used to filter out duplicates.
Definition: item_sum.h:2117
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2207
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:4401
Mem_root_array< ORDER > order_array
Definition: item_sum.h:2120
TREE tree_base
Definition: item_sum.h:2107
void update_field() override
Definition: item_sum.h:2166
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:4730
Temp_table_param * tmp_table_param
Describes the temporary table used to perform group concat.
Definition: item_sum.h:2105
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:4328
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4393
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:4276
const char * func_name() const override
Definition: item_sum.h:2160
Item_result result_type() const override
Definition: item_sum.h:2161
Field * make_string_field(TABLE *table_arg) const override
Create a field to hold a string value from an item.
Definition: item_sum.cc:4360
uint m_order_arg_count
The number of ORDER BY items.
Definition: item_sum.h:2097
TREE * tree
Definition: item_sum.h:2108
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:4238
bool has_distinct() const noexcept
Definition: item_sum.h:2188
bool add() override
Definition: item_sum.cc:4415
Name_resolution_context * context
Resolver context, points to containing query block.
Definition: item_sum.h:2101
void reset_field() override
Definition: item_sum.h:2165
uint m_field_arg_count
The number of selected items, aka the concat field list.
Definition: item_sum.h:2099
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:2126
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2184
bool warning_for_row
Definition: item_sum.h:2127
String * separator
String containing separator between group items.
Definition: item_sum.h:2103
~Item_func_group_concat() override
Definition: item_sum.h:2152
const Mem_root_array< ORDER > & get_order_array() const noexcept
Definition: item_sum.h:2193
Class for implementation of the GROUPING function.
Definition: item_sum.h:2686
const Query_block * m_query_block
The query block in which this function is called.
Definition: item_sum.h:2701
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:6371
longlong val_int() override
Evaluation of the GROUPING function.
Definition: item_sum.cc:6320
Item_func_grouping(const POS &pos, PT_item_list *a)
Definition: item_sum.h:2688
const char * func_name() const override
Definition: item_sum.h:2691
bool fix_fields(THD *thd, Item **ref) override
Resolve the fields in the GROUPING function.
Definition: item_sum.cc:6265
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:6341
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_sum.cc:6383
enum Functype functype() const override
Definition: item_sum.h:2692
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:492
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:4262
void set_original_field_name(const char *name_arg)
Definition: item.h:4259
const char * original_table_name() const
Definition: item.h:4263
Definition: item_func.h:1019
Definition: item.h:5118
LEAD/LAG window functions, cf.
Definition: item_sum.h:2452
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5583
bool has_value() const
Definition: item_sum.h:2526
bool m_is_lead
if true, the function is LEAD, else LAG
Definition: item_sum.h:2454
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:2510
const char * func_name() const override
Definition: item_sum.h:2486
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5731
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:5643
void set_use_default(bool value)
Definition: item_sum.h:2528
String * val_str(String *str) override
Definition: item_sum.cc:5723
bool m_use_default
execution state: use default value for current row
Definition: item_sum.h:2469
enum Item_result result_type() const override
Definition: item_sum.h:2499
Item_cache * m_default
Definition: item_sum.h:2458
bool compute()
Core logic of LEAD/LAG window functions.
Definition: item_sum.cc:5758
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:2473
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:5670
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2489
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5740
enum_null_treatment m_null_treatment
Definition: item_sum.h:2453
void clear() override
Definition: item_sum.cc:5682
bool use_default() const
Definition: item_sum.h:2529
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.cc:5592
bool m_has_value
Execution state: if set, we already have a value for current row.
Definition: item_sum.h:2468
longlong val_int() override
Definition: item_sum.cc:5694
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5710
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5615
Item_result m_hybrid_type
Definition: item_sum.h:2456
int64 m_n
canonicalized offset value
Definition: item_sum.h:2455
double val_real() override
Definition: item_sum.cc:5702
void set_has_value(bool value)
Definition: item_sum.h:2525
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5533
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5748
bool setup_lead_lag()
Definition: item_sum.cc:5654
Item_cache * m_value
Definition: item_sum.h:2457
Item_non_framing_wf super
Definition: item_sum.h:2470
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:5689
Common parent class for window functions that always work on the entire partition,...
Definition: item_sum.h:2223
Item_non_framing_wf(const POS &pos, PT_window *w)
Definition: item_sum.h:2227
bool add() override
Definition: item_sum.h:2244
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2238
Item_non_framing_wf(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2228
Item_non_framing_wf(const POS &pos, PT_item_list *opt_list, PT_window *w)
Definition: item_sum.h:2230
Item_sum super
Definition: item_sum.h:2224
Item_non_framing_wf(THD *thd, Item_non_framing_wf *i)
Definition: item_sum.h:2232
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2234
void update_field() override
Definition: item_sum.h:2243
void reset_field() override
Definition: item_sum.h:2242
bool framing() const override
All aggregates are framing, i.e.
Definition: item_sum.h:2251
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:4766
NTH_VALUE window function, cf.
Definition: item_sum.h:2611
int64 m_cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2618
void update_field() override
Definition: item_sum.h:2658
bool compute()
Core logic of NTH_VALUE window functions.
Definition: item_sum.cc:5426
String * val_str(String *str) override
Definition: item_sum.cc:5493
Item_sum super
Definition: item_sum.h:2620
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5040
enum Item_result result_type() const override
Definition: item_sum.h:2646
Item_result m_hybrid_type
Definition: item_sum.h:2615
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5479
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5513
bool setup_nth()
Definition: item_sum.cc:5385
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5334
enum_field_types m_hybrid_field_type
Definition: item_sum.h:2616
int64 m_n
The N of the function.
Definition: item_sum.h:2613
double val_real() override
Definition: item_sum.cc:5469
bool m_from_last
true iff FROM_LAST was specified
Definition: item_sum.h:2614
Item_cache * m_value
Definition: item_sum.h:2617
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5523
enum_null_treatment m_null_treatment
Definition: item_sum.h:2612
void reset_field() override
Definition: item_sum.h:2657
longlong val_int() override
Definition: item_sum.cc:5459
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:2623
void clear() override
Definition: item_sum.cc:5396
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5320
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:5374
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2634
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:5402
bool add() override
Definition: item_sum.h:2659
const char * func_name() const override
Definition: item_sum.h:2633
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5503
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:5406
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2666
NTILE window function, cf.
Definition: item_sum.h:2415
void clear() override
Definition: item_sum.h:2445
Item_non_framing_wf super
Definition: item_sum.h:2416
String * val_str(String *) override
Definition: item_sum.cc:5119
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2428
const char * func_name() const override
Definition: item_sum.h:2425
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:2446
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5134
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5058
double val_real() override
Definition: item_sum.cc:5114
longlong val_int() override
Definition: item_sum.cc:5064
longlong m_value
Definition: item_sum.h:2417
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:5121
Item_ntile(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2420
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2426
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:5126
Item_result result_type() const override
Definition: item_sum.h:2444
PERCENT_RANK window function, cf.
Definition: item_sum.h:2374
String * val_str(String *) override
Definition: item_sum.cc:5023
Item_percent_rank(const POS &pos, PT_window *w)
Definition: item_sum.h:2385
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2377
const char * func_name() const override
Definition: item_sum.h:2392
Item_result result_type() const override
Definition: item_sum.h:2409
~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:4965
ulonglong m_peers
Needed to make PERCENT_RANK same for peers.
Definition: item_sum.h:2378
double val_real() override
Definition: item_sum.cc:4989
Item_non_framing_wf super
Definition: item_sum.h:2375
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2395
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2393
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:2402
bool m_last_peer_visited
Set when the last peer has been visited.
Definition: item_sum.h:2382
void clear() override
Definition: item_sum.cc:5032
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:5027
longlong val_int() override
Definition: item_sum.cc:5015
RANK or DENSE_RANK window function, cf.
Definition: item_sum.h:2294
Item_result result_type() const override
Definition: item_sum.h:2339
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:4821
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:4845
ulonglong m_duplicates
Needed to make RANK different from DENSE_RANK.
Definition: item_sum.h:2299
bool m_dense
If true, the object represents DENSE_RANK.
Definition: item_sum.h:2296
longlong val_int() override
Definition: item_sum.cc:4863
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2318
~Item_rank() override
Definition: item_sum.cc:4919
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2322
Item_rank(const POS &pos, bool dense, PT_window *w)
Definition: item_sum.h:2303
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4898
const char * func_name() const override
Definition: item_sum.h:2314
double val_real() override
Definition: item_sum.cc:4891
String * val_str(String *) override
Definition: item_sum.cc:4896
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2298
void clear() override
Clear state for a new partition.
Definition: item_sum.cc:4903
Mem_root_array< Cached_item * > m_previous
Values of previous row's ORDER BY items.
Definition: item_sum.h:2301
Item_non_framing_wf super
Definition: item_sum.h:2295
Item with result field.
Definition: item.h:5832
longlong llrint_with_overflow_check(double realval)
Definition: item.h:5871
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item.cc:10910
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2711
table_map used_tables() const override
Definition: item_sum.h:2738
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:6459
void update_field() override
Definition: item_sum.h:2753
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2740
Item * get_arg(uint i) override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2775
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.h:2777
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.cc:6445
void set_current_rollup_level(int level)
Definition: item_sum.h:2787
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:6399
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:6450
bool aggregator_setup(THD *thd) override
Called to initialize the aggregator.
Definition: item_sum.cc:6492
Item_result result_type() const override
Definition: item_sum.h:2739
String * val_str(String *str) override
Definition: item_sum.cc:6424
int set_aggregator(Aggregator::Aggregator_type aggregator) override
Definition: item_sum.cc:6481
Item_sum * unwrap_sum() override
Non-const version.
Definition: item_sum.h:2792
longlong val_int() override
Definition: item_sum.cc:6417
void reset_field() override
Definition: item_sum.h:2752
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:2791
Item_rollup_sum_switcher(List< Item > *sum_func_per_level)
Definition: item_sum.h:2713
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:6405
bool aggregator_add_all()
Definition: item_sum.h:2764
Item * current_arg() const
Definition: item_sum.cc:6394
Item_sum * master() const
Definition: item_sum.h:2788
Item_sum * child(size_t i) const
Definition: item_sum.h:2796
enum Sumfunctype real_sum_func() const override
Definition: item_sum.h:2749
bool is_rollup_sum_wrapper() const override
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:2790
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2748
const char * func_name() const override
Definition: item_sum.h:2737
int m_current_rollup_level
Definition: item_sum.h:2801
double val_real() override
Definition: item_sum.cc:6410
bool val_json(Json_wrapper *result) override
Get a JSON value from an Item.
Definition: item_sum.cc:6438
uint argument_count() const override
Definition: item_sum.h:2784
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:6431
bool add() override
Definition: item_sum.h:2755
void clear() override
Definition: item_sum.cc:6463
bool reset_and_add_for_rollup(int last_unchanged_group_item_idx)
Definition: item_sum.cc:6469
const int m_num_levels
Definition: item_sum.h:2800
const Item * get_arg(uint i) const override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2776
ROW_NUMBER window function, cf.
Definition: item_sum.h:2257
ulonglong m_ctr
Increment for each row in partition.
Definition: item_sum.h:2259
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2270
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2268
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2284
Item_row_number(const POS &pos, PT_window *w)
Definition: item_sum.h:2262
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4814
Item_result result_type() const override
Definition: item_sum.h:2282
String * val_str(String *) override
Definition: item_sum.cc:4810
longlong val_int() override
Definition: item_sum.cc:4791
void clear() override
Definition: item_sum.cc:4819
const char * func_name() const override
Definition: item_sum.h:2267
double val_real() override
Definition: item_sum.cc:4805
Definition: item_sum.h:1486
enum Type type() const override
Definition: item_sum.h:1489
Item_std_field(Item_sum_std *item)
Definition: item_sum.cc:3853
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:1493
enum Item_result result_type() const override
Definition: item_sum.h:1492
double val_real() override
Definition: item_sum.cc:3856
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3864
Definition: item_sum.h:1900
Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1902
const char * func_name() const override
Definition: item_sum.h:1906
Item_sum_and(THD *thd, Item_sum_and *item)
Definition: item_sum.h:1905
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3321
Definition: item_sum.h:1311
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2267
void clear() override
Definition: item_sum.cc:2323
String * val_str(String *str) override
Definition: item_sum.cc:2438
const char * func_name() const override
Definition: item_sum.h:1341
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3550
Item_sum_sum super
Definition: item_sum.h:1315
Item * result_item(Field *) override
Definition: item_sum.h:1338
double val_real() override
Definition: item_sum.cc:2332
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1344
Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
Definition: item_sum.h:1319
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:2301
my_decimal m_avg_dec
Definition: item_sum.h:1316
uint f_scale
Definition: item_sum.h:1314
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1326
double m_avg
Definition: item_sum.h:1317
longlong val_int() override
Definition: item_sum.h:1333
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2294
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2355
void reset_field() override
Definition: item_sum.cc:3450
uint prec_increment
Definition: item_sum.h:1313
uint dec_bin_size
Definition: item_sum.h:1314
bool add() override
Definition: item_sum.cc:2325
Item_sum_avg(THD *thd, Item_sum_avg *item)
Definition: item_sum.h:1322
uint f_precision
Definition: item_sum.h:1314
This is used in connection with an Item_sum_bit,.
Definition: item_sum.h:1193
double val_real() override
Definition: item_sum.cc:3796
enum Type type() const override
Definition: item_sum.h:1207
ulonglong reset_bits
Definition: item_sum.h:1195
Item_sum_bit_field(Item_result res_type, Item_sum_bit *item, ulonglong reset_bits)
Definition: item_sum.cc:3761
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1204
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3813
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3839
longlong val_int() override
Definition: item_sum.cc:3781
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3846
String * val_str(String *) override
Definition: item_sum.cc:3821
Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
Definition: item_sum.h:1731
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3210
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:1637
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1840
void remove_bits(const String *s1, ulonglong b1)
For windowing: perform inverse aggregation.
Definition: item_sum.cc:1559
const char initial_value_buff_storage[1]
Buffer used to avoid String allocation in the constructor.
Definition: item_sum.h:1742
static constexpr uint DIGIT_CNT_CARD
Definition: item_sum.h:1785
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1827
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1501
bool m_is_xor
true iff BIT_XOR
Definition: item_sum.h:1788
void clear() override
Definition: item_sum.cc:3293
Item * result_item(Field *) override
Definition: item_sum.h:1823
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:1476
longlong val_int() override
Definition: item_sum.cc:3268
Item_sum super
Definition: item_sum.h:1732
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:1777
Item_result hybrid_type
Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT.
Definition: item_sum.h:1740
ulonglong reset_bits
Stores the neutral element for function.
Definition: item_sum.h:1734
Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg, PT_window *w)
Definition: item_sum.h:1791
bool is_and() const
Definition: item_sum.h:1853
void reset_field() override
Definition: item_sum.cc:3478
ulonglong m_count
Execution state (windowing): this is for counting rows entering and leaving the window frame,...
Definition: item_sum.h:1748
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3217
double val_real() override
Definition: item_sum.cc:3242
bool add() override
Common implementation of Item_sum_or::add, Item_sum_and:add and Item_sum_xor::add.
Definition: item_sum.cc:1721
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.cc:3224
enum Item_result result_type() const override
Definition: item_sum.h:1828
uint m_digit_cnt_card
Definition: item_sum.h:1783
Item_sum_bit(THD *thd, Item_sum_bit *item)
Copy constructor, used for executing subqueries with temporary tables.
Definition: item_sum.h:1804
String * val_str(String *str) override
Definition: item_sum.cc:3179
void update_field() override
Definition: item_sum.cc:3488
ulonglong bits
Stores the result value for the INT_RESULT.
Definition: item_sum.h:1736
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:1756
String value_buff
Stores the result value for the STRING_RESULT.
Definition: item_sum.h:1738
Implements ST_Collect which aggregates geometries into Multipoints, Multilinestrings,...
Definition: item_sum.h:2806
bool add() override
Definition: item_sum.cc:6567
void clear() override
Definition: item_sum.cc:6561
void read_result_field()
Definition: item_sum.cc:6622
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:6530
bool get_date(MYSQL_TIME *, my_time_flags_t) override
Definition: item_sum.h:2826
std::optional< gis::srid_t > srid
Definition: item_sum.h:2808
Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
Definition: item_sum.h:2817
void update_field() override
Definition: item_sum.cc:6715
longlong val_int() override
Definition: item_sum.h:2824
bool get_time(MYSQL_TIME *) override
Definition: item_sum.h:2827
std::unique_ptr< gis::Geometrycollection > m_geometrycollection
Definition: item_sum.h:2809
String * val_str(String *str) override
Definition: item_sum.cc:6668
void reset_field() override
Definition: item_sum.cc:6778
Item_sum_collect(THD *thd, Item_sum *item)
Definition: item_sum.h:2814
void store_result_field()
Definition: item_sum.cc:6721
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:6773
bool check_wf_semantics1(THD *thd, Query_block *, Window_evaluation_requirements *r) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:6552
const char * func_name() const override
Definition: item_sum.h:2865
void pop_front()
Definition: item_sum.cc:6661
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2828
Item_result result_type() const override
Definition: item_sum.h:2829
int set_aggregator(Aggregator::Aggregator_type) override
Definition: item_sum.h:2830
double val_real() override
Definition: item_sum.h:2825
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6617
Definition: item_sum.h:1069
longlong count
Definition: item_sum.h:1070
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1101
longlong val_int() override
Definition: item_sum.cc:2229
void update_field() override
Definition: item_sum.cc:3541
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2210
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1098
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:2261
void make_const(longlong count_arg)
Definition: item_sum.h:1108
void reset_field() override
Definition: item_sum.cc:3442
Item_sum_count(Item_int *number)
Definition: item_sum.h:1081
Item_sum_count(THD *thd, Item_sum_count *item)
Definition: item_sum.h:1096
Item_sum_count(const POS &pos, PT_item_list *list, PT_window *w)
Constructs an instance for COUNT(DISTINCT)
Definition: item_sum.h:1092
void clear() override
Definition: item_sum.cc:2218
const char * func_name() const override
Definition: item_sum.h:1115
Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1079
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1107
bool add() override
Definition: item_sum.cc:2220
This is used in connection which a parent Item_sum:
Definition: item_sum.h:1132
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1154
Item_result hybrid_type
Stores the Item's result type.
Definition: item_sum.h:1137
enum Item_result result_type() const override
Definition: item_sum.h:1140
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:1148
Field * field
The tmp table's column containing the value of the set function.
Definition: item_sum.h:1135
bool mark_field_in_map(uchar *arg) override
Mark underlying field in read or write map of a table.
Definition: item_sum.h:1141
Abstract base class for the MIN and MAX aggregate functions.
Definition: item_sum.h:1527
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3132
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:3127
bool setup_hybrid(Item *item, Item *value_arg)
MIN/MAX function setup.
Definition: item_sum.cc:1813
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:1678
virtual Item_sum_hybrid * clone_hybrid(THD *thd) const =0
Create a clone of this object.
bool add() override
Definition: item_sum.cc:3162
const bool m_is_min
Tells if this is the MIN function (true) or the MAX function (false).
Definition: item_sum.h:1534
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3061
bool compute()
This function implements the optimized version of retrieving min/max value.
Definition: item_sum.cc:2863
bool has_values()
Definition: item_sum.h:1675
void min_max_update_int_field()
Definition: item_sum.cc:3672
longlong val_int() override
Definition: item_sum.cc:2996
bool m_has_values
Definition: item_sum.h:1546
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3051
void reset_field() override
Definition: item_sum.cc:3345
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:3098
void clear() override
Definition: item_sum.cc:2814
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:1574
Item_cache * arg_cache
Definition: item_sum.h:1543
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3113
void min_max_update_json_field()
Definition: item_sum.cc:3625
void update_field() override
Definition: item_sum.cc:3586
longlong val_date_temporal() override
Return date value of item in packed longlong format.
Definition: item_sum.cc:3022
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:2824
bool keep_field_type() const override
Definition: item_sum.h:1667
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3034
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:1568
void min_max_update_real_field()
Definition: item_sum.cc:3659
Item_sum_hybrid(THD *thd, const Item_sum_hybrid *item)
Definition: item_sum.h:1638
Item_result hybrid_type
Definition: item_sum.h:1545
longlong val_time_temporal() override
Return time value of item in packed longlong format.
Definition: item_sum.cc:3010
bool m_optimize
Set to true when min/max can be optimized using window's ordering.
Definition: item_sum.h:1554
Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
Definition: item_sum.h:1622
Item_sum_hybrid(Item *item_par, bool is_min)
Definition: item_sum.h:1606
Item_sum super
Definition: item_sum.h:1528
void min_max_update_temporal_field()
Definition: item_sum.cc:3607
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:2829
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1782
String * val_str(String *) override
Definition: item_sum.cc:3071
Arg_comparator * cmp
Definition: item_sum.h:1544
void min_max_update_str_field()
Definition: item_sum.cc:3643
enum Item_result result_type() const override
Definition: item_sum.h:1668
void min_max_update_decimal_field()
Definition: item_sum.cc:3690
double val_real() override
Definition: item_sum.cc:2982
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:1830
bool m_want_first
For min() - Set to true when results are ordered in ascending and false when descending.
Definition: item_sum.h:1562
Item_cache * value
Definition: item_sum.h:1543
bool m_nulls_first
Set to true if the window is ordered ascending.
Definition: item_sum.h:1550
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.h:1669
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:3084
Definition: item_sum.h:987
Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:989
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1013
Item_sum_int(THD *thd, Item_sum_int *item)
Definition: item_sum.h:999
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1016
double val_real() override
Definition: item_sum.h:1007
Item_sum_int(Item *item_par)
Definition: item_sum.h:1003
enum Item_result result_type() const override
Definition: item_sum.h:1017
String * val_str(String *str) override
Definition: item_sum.cc:1443
Item_sum_int(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:994
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1445
Implements aggregation of values into an array.
Definition: item_sum.h:1255
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:6001
~Item_sum_json_array() override
bool add() override
Definition: item_sum.cc:6074
unique_ptr_destroy_only< Json_array > m_json_array
Accumulates the final value.
Definition: item_sum.h:1257
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6123
const char * func_name() const override
Definition: item_sum.h:1267
void clear() override
Definition: item_sum.cc:6016
Implements aggregation of values into an object.
Definition: item_sum.h:1274
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:6025
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6231
bool add() override
Definition: item_sum.cc:6136
unique_ptr_destroy_only< Json_object > m_json_object
Accumulates the final value.
Definition: item_sum.h:1276
~Item_sum_json_object() override
const char * func_name() const override
Definition: item_sum.h:1303
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:6050
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:1285
void clear() override
Definition: item_sum.cc:6040
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:1293
String m_tmp_key_value
Buffer used to get the value of the key.
Definition: item_sum.h:1278
Common abstraction for Item_sum_json_array and Item_sum_json_object.
Definition: item_sum.h:1211
void reset_field() override
Definition: item_sum.cc:5961
void update_field() override
Definition: item_sum.cc:5981
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5875
unique_ptr_destroy_only< Json_wrapper > m_wrapper
Wrapper around the container (object/array) which accumulates the value.
Definition: item_sum.h:1220
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1236
longlong val_int() override
Definition: item_sum.cc:5913
String m_value
String used when reading JSON binary values or JSON text values.
Definition: item_sum.h:1216
String m_conversion_buffer
String used for converting JSON text values to utf8mb4 charset.
Definition: item_sum.h:1218
Item_sum super
Definition: item_sum.h:1212
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5954
~Item_sum_json() override
String * val_str(String *str) override
Definition: item_sum.cc:5856
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5928
Item_result result_type() const override
Definition: item_sum.h:1237
bool fix_fields(THD *thd, Item **pItem) override
Definition: item_sum.cc:5830
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:5825
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5946
Item_sum_json(unique_ptr_destroy_only< Json_wrapper > wrapper, Args &&... parent_args)
Construct an Item_sum_json instance.
Definition: item_sum.cc:5816
double val_real() override
Definition: item_sum.cc:5898
Definition: item_sum.h:1712
Item_sum_max(Item *item_par)
Definition: item_sum.h:1714
Item_sum_max(THD *thd, const Item_sum_max *item)
Definition: item_sum.h:1717
const char * func_name() const override
Definition: item_sum.h:1720
Item_sum_max * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3143
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1719
Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1715
Definition: item_sum.h:1698
Item_sum_min(THD *thd, const Item_sum_min *item)
Definition: item_sum.h:1703
Item_sum_min(Item *item_par)
Definition: item_sum.h:1700
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1705
const char * func_name() const override
Definition: item_sum.h:1706
Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1701
Item_sum_min * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3139
Common abstract class for: Item_avg_field Item_variance_field.
Definition: item_sum.h:1165
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1171
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.h:1177
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1174
longlong val_int() override
Definition: item_sum.h:1167
Definition: item_sum.h:944
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1439
Item_sum_num(THD *thd, Item_sum_num *item)
Definition: item_sum.h:963
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1449
longlong val_int() override
Definition: item_sum.h:972
enum_field_types default_data_type() const override
Get the default data (output) type for the specific item.
Definition: item_sum.h:965
String * val_str(String *str) override
Definition: item_sum.cc:1437
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:978
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:981
Item_sum super
Definition: item_sum.h:945
Item_sum_num(Item *item_par)
Definition: item_sum.h:969
bool is_evaluated
Definition: item_sum.h:954
Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
Definition: item_sum.h:957
void reset_field() override
Definition: item_sum.cc:3332
Item_sum_num(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:960
Definition: item_sum.h:1890
Item_sum_or(THD *thd, Item_sum_or *item)
Definition: item_sum.h:1895
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3307
const char * func_name() const override
Definition: item_sum.h:1896
Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1892
Definition: item_sum.h:1505
double val_real() override
Definition: item_sum.cc:2448
Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1507
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2457
Item_sum_std(THD *thd, Item_sum_std *item)
Definition: item_sum.h:1510
const char * func_name() const override
Definition: item_sum.h:1514
enum Item_result result_type() const override
Definition: item_sum.h:1518
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1511
Item * result_item(Field *) override
Definition: item_sum.h:1513
Definition: item_sum.h:1020
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:1031
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:1965
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:1894
enum Item_result result_type() const override
Definition: item_sum.h:1059
double sum
Definition: item_sum.h:1023
bool add() override
Definition: item_sum.cc:1987
Item_result hybrid_type
Definition: item_sum.h:1022
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:1913
void reset_field() override
Definition: item_sum.cc:3424
String * val_str(String *str) override
Definition: item_sum.cc:2076
longlong val_int() override
Definition: item_sum.cc:2008
my_decimal dec_buffs[2]
Definition: item_sum.h:1024
void clear() override
Definition: item_sum.cc:1901
const char * func_name() const override
Definition: item_sum.h:1065
Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
Definition: item_sum.h:1041
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3512
uint curr_dec_buff
Definition: item_sum.h:1025
ulonglong m_frame_null_count
Execution state: this is for counting NULLs of rows entering and leaving the window frame,...
Definition: item_sum.h:1038
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2082
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1915
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1050
double val_real() override
Definition: item_sum.cc:2032
Definition: item_sum.h:2058
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2069
longlong val_int() override
Definition: item_sum.cc:3981
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2072
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3983
Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2060
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3992
double val_real() override
Definition: item_sum.cc:3979
enum Item_result result_type() const override
Definition: item_sum.h:2075
String * val_str(String *) override
Definition: item_sum.cc:3975
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
Definition: item_sum.h:2063
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2076
Definition: item_sum.h:1968
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1987
longlong val_int() override
Definition: item_sum.h:1974
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3971
String * val_str(String *str) override
Definition: item_sum.cc:3967
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3954
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1981
double val_real() override
Definition: item_sum.cc:3959
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
Definition: item_sum.h:1972
Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1970
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1984
Definition: item_sum.h:1995
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2008
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
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:2013
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2011
enum Item_result result_type() const override
Definition: item_sum.h:2012
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3996
String * val_str(String *str) override
Definition: item_sum.cc:4008
Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1997
longlong val_int() override
Definition: item_sum.cc:4000
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:4012
double val_real() override
Definition: item_sum.h:2002
Definition: item_sum.h:2020
bool resolve_type(THD *) override
Default max_length is max argument length.
Definition: item_sum.cc:4018
double val_real() override
Definition: item_sum.h:2027
String * val_str(String *) override
Definition: item_sum.cc:4034
enum Item_result result_type() const override
Definition: item_sum.h:2053
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2047
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2050
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4026
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:4030
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
Definition: item_sum.h:2024
longlong val_int() override
Definition: item_sum.h:2036
Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2022
Definition: item_sum.h:1431
ulonglong count
Definition: item_sum.h:1442
void update_field() override
Definition: item_sum.cc:2788
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2656
double val_real() override
Definition: item_sum.cc:2733
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1476
void clear() override
Definition: item_sum.cc:2710
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2676
Item * result_item(Field *) override
Definition: item_sum.h:1468
Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1452
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1461
double recurrence_s
Definition: item_sum.h:1440
bool optimize
If set, uses a algorithm II mentioned in the class description to calculate the variance which helps ...
Definition: item_sum.h:1450
const char * func_name() const override
Definition: item_sum.h:1470
bool add() override
Definition: item_sum.cc:2712
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1469
double recurrence_s2
Definition: item_sum.h:1441
Item_result hybrid_type
Definition: item_sum.h:1435
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:2640
double recurrence_m
Used in recurrence relation.
Definition: item_sum.h:1439
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2763
uint prec_increment
Definition: item_sum.h:1444
enum Item_result result_type() const override
Definition: item_sum.h:1475
void reset_field() override
Definition: item_sum.cc:2768
uint sample
Definition: item_sum.h:1443
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:2689
Definition: item_sum.h:1910
const char * func_name() const override
Definition: item_sum.h:1918
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3314
Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1912
Item_sum_xor(THD *thd, Item_sum_xor *item)
Definition: item_sum.h:1917
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:668
bool collect_grouped_aggregates(uchar *) override
Definition: item_sum.cc:725
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:589
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:413
bool m_null_executed
true if the function is determined to be NULL at start of execution
Definition: item_sum.h:513
virtual bool check_wf_semantics2(Window_evaluation_requirements *reqs)
Like check_wf_semantics1.
Definition: item_sum.h:726
void aggregator_clear()
Called to cleanup the aggregator.
Definition: item_sum.h:680
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:786
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:485
bool aggregate_check_distinct(uchar *arg) override
Definition: item_sum.cc:641
~Item_sum() override
Definition: item_sum.h:540
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:674
bool wf_common_init()
Common initial actions for window functions.
Definition: item_sum.cc:937
Item_sum(const POS &pos, PT_window *w)
Definition: item_sum.h:521
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:614
bool allow_group_via_temp_table
If incremental update of fields is supported.
Definition: item_sum.h:494
void add_json_info(Json_object *obj) override
Add all the node-specific json fields.
Definition: item_sum.h:801
virtual bool add()=0
int8 max_aggr_level
max level of unbound column references
Definition: item_sum.h:491
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:467
void make_const()
Definition: item_sum.h:596
Item * replace_aggregate(uchar *) override
Definition: item_sum.cc:756
Window * window()
Definition: item_sum.h:736
virtual void make_unique()
Definition: item_sum.h:619
bool eq_specific(const Item *item) const override
Provide a more specific equality check for a function.
Definition: item_sum.cc:633
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:901
bool reset_and_add()
Resets the aggregate value to its default and aggregates the current value of its attribute(s).
Definition: item_sum.h:553
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:913
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.h:594
virtual Item * result_item(Field *field)
Definition: item_sum.h:576
bool forced_const
True means that this field has been evaluated during optimization.
Definition: item_sum.h:508
virtual void update_field()=0
bool eq(const Item *item) const override
Definition: item_sum.cc:610
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:529
void unsupported_as_wf()
Definition: item_sum.h:795
Aggregator * aggr
Aggregator class instance.
Definition: item_sum.h:408
const Window * window() const
Definition: item_sum.h:737
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:766
virtual bool is_rollup_sum_wrapper() const
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:780
bool aggregate_check_group(uchar *arg) override
Definition: item_sum.cc:665
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:547
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:500
bool collect_scalar_subqueries(uchar *) override
Definition: item_sum.cc:764
virtual void reset_field()=0
virtual bool framing() const
All aggregates are framing, i.e.
Definition: item_sum.h:750
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:784
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.cc:854
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:698
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:842
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:480
bool m_null_resolved
true if the function is resolved to be always NULL
Definition: item_sum.h:511
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:457
@ FIRST_LAST_VALUE_FUNC
Definition: item_sum.h:462
@ SUM_BIT_FUNC
Definition: item_sum.h:451
@ COUNT_DISTINCT_FUNC
Definition: item_sum.h:442
@ SUM_FUNC
Definition: item_sum.h:443
@ CUME_DIST_FUNC
Definition: item_sum.h:458
@ SUM_DISTINCT_FUNC
Definition: item_sum.h:444
@ UDF_SUM_FUNC
Definition: item_sum.h:452
@ ROLLUP_SUM_SWITCHER_FUNC
Definition: item_sum.h:464
@ JSON_AGG_FUNC
Definition: item_sum.h:454
@ ROW_NUMBER_FUNC
Definition: item_sum.h:455
@ MAX_FUNC
Definition: item_sum.h:448
@ GEOMETRY_AGGREGATE_FUNC
Definition: item_sum.h:465
@ AVG_FUNC
Definition: item_sum.h:445
@ MIN_FUNC
Definition: item_sum.h:447
@ NTILE_FUNC
Definition: item_sum.h:460
@ NTH_VALUE_FUNC
Definition: item_sum.h:463
@ GROUP_CONCAT_FUNC
Definition: item_sum.h:453
@ RANK_FUNC
Definition: item_sum.h:456
@ LEAD_LAG_FUNC
Definition: item_sum.h:461
@ PERCENT_RANK_FUNC
Definition: item_sum.h:459
@ VARIANCE_FUNC
Definition: item_sum.h:450
virtual int set_aggregator(Aggregator::Aggregator_type aggregator)
Definition: item_sum.cc:859
bool aggregator_add()
Called to add value to the aggregator.
Definition: item_sum.h:686
Item_sum(Item *a)
Definition: item_sum.h:524
void set_distinct(bool distinct)
Definition: item_sum.h:689
Item_sum * next_sum
next in the circular chain of registered objects
Definition: item_sum.h:482
Item_sum * in_sum_func
the containing set function if any
Definition: item_sum.h:483
virtual bool keep_field_type() const
Definition: item_sum.h:574
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:774
bool has_force_copy_fields() const
Definition: item_sum.h:437
Type type() const override
Definition: item_sum.h:543
void mark_as_sum_func()
Definition: item_sum.cc:457
Query_block * aggr_query_block
For a group aggregate, query block where function is aggregated.
Definition: item_sum.h:490
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:529
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:924
table_map used_tables() const override
Definition: item_sum.h:586
virtual Item_sum * unwrap_sum()
Non-const version.
Definition: item_sum.h:788
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:805
virtual bool setup(THD *)
Definition: item_sum.h:704
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:889
int8 max_sum_func_level
max level of aggregation for contained functions
Definition: item_sum.h:493
virtual bool uses_only_one_row() const
Only for framing window functions.
Definition: item_sum.h:756
Item ** get_arg_ptr(uint i)
Definition: item_sum.h:654
virtual enum Sumfunctype sum_func() const =0
Item_sum(const POS &pos, Item *a, Item *b, PT_window *w)
Definition: item_sum.h:532
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:690
Query_block * base_query_block
query block where function is placed
Definition: item_sum.h:484
Definition: item_sum.h:1926
udf_handler udf
Definition: item_sum.h:1930
bool do_itemize(Parse_context *pc, Item **res) override
The core function that does the actual itemization.
Definition: item_sum.cc:3912
void update_field() override
Definition: item_sum.h:1962
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3934
void reset_field() override
Definition: item_sum.h:1961
bool add() override
Definition: item_sum.cc:3927
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.h:1947
Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1933
void clear() override
Definition: item_sum.cc:3921
~Item_udf_sum() override
Definition: item_sum.h:1941
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:3943
Item_udf_sum(THD *thd, Item_udf_sum *item)
Definition: item_sum.h:1937
Item_sum super
Definition: item_sum.h:1927
const char * func_name() const override
Definition: item_sum.h:1946
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1957
Definition: item_sum.h:1353
my_decimal * val_decimal(my_decimal *dec_buf) override
Definition: item_sum.h:1362
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1365
enum Type type() const override
Definition: item_sum.h:1359
Item_variance_field(Item_sum_variance *item)
Definition: item_sum.cc:3883
uint sample
Definition: item_sum.h:1355
String * val_str(String *str) override
Definition: item_sum.h:1361
double val_real() override
Definition: item_sum.cc:3897
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:1366
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:930
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:3561
void set_nullable(bool nullable)
Definition: item.h:3670
DTCollation collation
Character set and collation properties assigned for this Item.
Definition: item.h:3568
bool get_time_from_decimal(MYSQL_TIME *ltime)
Convert val_decimal() to time in MYSQL_TIME.
Definition: item.cc:1758
void set_data_type(enum_field_types data_type)
Set the data type of the current Item.
Definition: item.h:1513
my_decimal * val_decimal_from_string(my_decimal *decimal_value)
Definition: item.cc:365
bool is_nullable() const
Definition: item.h:3669
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:1739
bool get_time_from_real(MYSQL_TIME *ltime)
Convert val_real() to time in MYSQL_TIME.
Definition: item.cc:1749
bool get_date_from_decimal(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_decimal() to date in MYSQL_TIME.
Definition: item.cc:1671
void set_data_type_double()
Set the data type of the Item to be double precision floating point.
Definition: item.h:1579
Item_name_string item_name
Name from query.
Definition: item.h:3569
bool fixed
True if item has been resolved.
Definition: item.h:3658
virtual Item_result result_type() const
Definition: item.h:1451
bool null_value
True if item is null.
Definition: item.h:3695
Type
Definition: item.h:965
@ SUM_FUNC_ITEM
A grouped aggregate function, or window function.
Definition: item.h:969
@ AGGR_FIELD_ITEM
A special field for certain aggregate operations.
Definition: item.h:970
bool get_date_from_numeric(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
Convert a numeric type to date.
Definition: item.cc:1699
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:350
bool unsigned_flag
Definition: item.h:3696
longlong val_int_from_string()
Definition: item.cc:522
bool get_date_from_string(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_str() to date in MYSQL_TIME.
Definition: item.cc:1652
void set_grouping_func()
Set the property: this item is a call to GROUPING.
Definition: item.h:3477
bool get_time_from_numeric(MYSQL_TIME *ltime)
Convert a numeric type to time.
Definition: item.cc:1791
virtual bool mark_field_in_map(uchar *arg)
Mark underlying field in read or write map of a table.
Definition: item.h:2824
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:3706
bool get_date_from_int(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_int() to date in MYSQL_TIME.
Definition: item.cc:1680
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:1662
void set_data_type_longlong()
Set the data type of the Item to be longlong.
Definition: item.h:1555
double val_real_from_string()
Definition: item.cc:468
bool update_null_value()
Make sure the null_value member has a correct value.
Definition: item.cc:7634
String * val_string_from_real(String *str)
Definition: item.cc:283
bool get_time_from_int(MYSQL_TIME *ltime)
Convert val_int() to time in MYSQL_TIME.
Definition: item.cc:1767
Represents a JSON array container, i.e.
Definition: json_dom.h:516
Represents a JSON container value of type "object" (ECMA), type J_OBJECT here.
Definition: json_dom.h:369
bool add_alias(const std::string &key, Json_dom *value)
Insert the value into the object.
Definition: json_dom.h:411
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1153
Definition: sql_list.h:467
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:426
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:233
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:344
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1175
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
const CHARSET_INFO * charset() const
Definition: sql_string.h:240
const char * ptr() const
Definition: sql_string.h:249
size_t length() const
Definition: sql_string.h:241
void set(String &str, size_t offset, size_t arg_length)
Definition: sql_string.h:302
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:305
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:95
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:4619
void free_handler()
Definition: item_func.cc:4600
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:4149
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:4070
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:4106
A better implementation of the UNIX ctype(3) library.
MYSQL_STRINGS_EXPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:500
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:477
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
Time declarations shared between the server and client API: you should not add anything to this heade...
thread_local MEM_ROOT ** THR_MALLOC
Definition: mysqld.cc:1559
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1081
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
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:2879
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:125
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:488
int err_code
the error code found during check(if any)
Definition: item.h:495
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:507
argument used by walk method collect_grouped_aggregates ("cga")
Definition: item_sum.h:623
Collect_grouped_aggregate_info(Query_block *select)
Definition: item_sum.h:636
Query_block * m_query_block
The query block we walk from.
Definition: item_sum.h:631
std::vector< Item_sum * > list
accumulated all aggregates found
Definition: item_sum.h:625
std::set< Item_sum * > aggregates_that_were_hidden
Definition: item_sum.h:626
bool m_break_off
true: break off transformation
Definition: item_sum.h:633
bool m_outside
true: an aggregate aggregates outside m_query_block
Definition: item_sum.h:635
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:415
Definition: table.h:286
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:420
Definition: table.h:1407
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...