MySQL 8.0.39
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 "m_ctype.h"
43#include "m_string.h"
44#include "my_alloc.h"
45#include "my_compiler.h"
46
47#include "my_inttypes.h"
48#include "my_sys.h"
49#include "my_table_map.h"
50#include "my_time.h"
51#include "my_tree.h" // TREE
53#include "mysql_time.h"
54#include "mysqld_error.h"
55#include "sql/enum_query_type.h"
57#include "sql/gis/wkb.h"
58#include "sql/item.h" // Item_result_field
59#include "sql/item_func.h" // Item_int_func
60#include "sql/mem_root_array.h"
61#include "sql/my_decimal.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 bool itemize(Parse_context *pc, Item **res) override;
541 Type type() const override { return SUM_FUNC_ITEM; }
542 virtual enum Sumfunctype sum_func() const = 0;
543
544 // Differs only for Item_rollup_sum_switcher.
545 virtual enum Sumfunctype real_sum_func() const { return sum_func(); }
546
547 /**
548 Resets the aggregate value to its default and aggregates the current
549 value of its attribute(s).
550 */
551 inline bool reset_and_add() {
553 return aggregator_add();
554 }
555
556 /*
557 Called when new group is started and results are being saved in
558 a temporary table. Similarly to reset_and_add() it resets the
559 value to its default and aggregates the value of its
560 attribute(s), but must also store it in result_field.
561 This set of methods (result_item(), reset_field, update_field()) of
562 Item_sum is used only if allow_group_via_temp_table is true. Otherwise
563 copy_or_same() is used to obtain a copy of this item.
564 */
565 virtual void reset_field() = 0;
566 /*
567 Called for each new value in the group, when temporary table is in use.
568 Similar to add(), but uses temporary table field to obtain current value,
569 Updated value is then saved in the field.
570 */
571 virtual void update_field() = 0;
572 virtual bool keep_field_type() const { return false; }
573 bool resolve_type(THD *) override;
574 virtual Item *result_item(Field *field) {
575 Item_field *item = new Item_field(field);
576 if (item == nullptr) return nullptr;
577 // Aggregated fields have no reference to an underlying table
578 assert(item->original_db_name() == nullptr &&
579 item->original_table_name() == nullptr);
580 // Break the connection to the original field since this is an aggregation
581 item->set_original_field_name(nullptr);
582 return item;
583 }
584 table_map used_tables() const override {
585 return forced_const ? 0 : used_tables_cache;
586 }
587 table_map not_null_tables() const override { return used_tables(); }
588 void update_used_tables() override;
589 void fix_after_pullout(Query_block *parent_query_block,
590 Query_block *removed_query_block) override;
592 bool is_null() override { return null_value; }
593
594 void make_const() {
595 // "forced_const" will make used_tables() return zero for this object
596 forced_const = true;
597 }
598 void print(const THD *thd, String *str,
599 enum_query_type query_type) const override;
600 bool eq(const Item *item, bool binary_cmp) const override;
601 /**
602 Mark an aggregate as having no rows.
603
604 This function is called by the execution engine to assign 'NO ROWS
605 FOUND' value to an aggregate item, when the underlying result set
606 has no rows. Such value, in a general case, may be different from
607 the default value of the item after 'clear()': e.g. a numeric item
608 may be initialized to 0 by clear() and to NULL by
609 no_rows_in_result().
610 */
611 void no_rows_in_result() override {
615 }
616 virtual void make_unique() { force_copy_fields = true; }
617 virtual Field *create_tmp_field(bool group, TABLE *table);
618
619 /// argument used by walk method collect_grouped_aggregates ("cga")
621 /// accumulated all aggregates found
622 std::vector<Item_sum *> list;
623 std::set<Item_sum *> aggregates_that_were_hidden;
624 /**
625 The query block we walk from. All found aggregates must aggregate in
626 this; if some aggregate in outer query blocks, break off transformation.
627 */
629 /// true: break off transformation
630 bool m_break_off{false};
631 /// true: an aggregate aggregates outside m_query_block
632 bool m_outside{false};
634 : m_query_block(select) {}
635 };
636
637 bool collect_grouped_aggregates(uchar *) override;
638 Item *replace_aggregate(uchar *) override;
639 bool collect_scalar_subqueries(uchar *) override;
641
642 bool clean_up_after_removal(uchar *arg) override;
643 bool aggregate_check_group(uchar *arg) override;
644 bool aggregate_check_distinct(uchar *arg) override;
645 bool has_aggregate_ref_in_group_by(uchar *arg) override;
646 bool init_sum_func_check(THD *thd);
647 bool check_sum_func(THD *thd, Item **ref);
648
649 Item *set_arg(THD *thd, uint i, Item *new_val) override;
650 /// @todo delete this when we no longer support temporary transformations
651 Item **get_arg_ptr(uint i) { return &args[i]; }
652
653 bool fix_fields(THD *thd, Item **ref) override;
654
655 /**
656 Signal to the function that its arguments may have changed,
657 and that any internal caches etc. based on those arguments
658 must be updated accordingly.
659
660 This is used by the hypergraph optimizer when it rewrites
661 arguments to window functions to take into account that they
662 have been materialized into temporary tables, or that they
663 should read their values from the framebuffer.
664 */
666
667 /**
668 Called to initialize the aggregator.
669 */
670
671 virtual bool aggregator_setup(THD *thd) { return aggr->setup(thd); }
672
673 /**
674 Called to cleanup the aggregator.
675 */
676
677 inline void aggregator_clear() { aggr->clear(); }
678
679 /**
680 Called to add value to the aggregator.
681 */
682
683 inline bool aggregator_add() { return aggr->add(); }
684
685 /* stores the declared DISTINCT flag (from the parser) */
686 void set_distinct(bool distinct) {
687 with_distinct = distinct;
689 }
690
691 /*
692 Set the type of aggregation : DISTINCT or not.
693
694 May be called multiple times.
695 */
696
697 virtual int set_aggregator(Aggregator::Aggregator_type aggregator);
698
699 virtual void clear() = 0;
700 virtual bool add() = 0;
701 virtual bool setup(THD *) { return false; }
702
703 /**
704 Only relevant for aggregates qua window functions. Checks semantics after
705 windows have been set up and checked. Window functions have specific
706 requirements on the window specifications. Used at resolution time.
707
708 @param thd Current thread
709 @param select The current select
710 @param [out] reqs Holds collected requirements from this wf
711
712 @returns true if error
713 */
714 virtual bool check_wf_semantics1(THD *thd, Query_block *select,
716
717 /**
718 Like check_wf_semantics1.
719 For checks which cannot be done in resolution phase (mostly those for
720 input parameters which can be '?' and must be >=0: value isn't known
721 before execution phase).
722 */
724 [[maybe_unused]]) {
725 return false;
726 }
727
728 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
729 mem_root_deque<Item *> *fields) override;
730
731 void cleanup() override;
732
733 Window *window() { return m_window; }
734 const Window *window() const { return m_window; }
735 bool reset_wf_state(uchar *arg) override;
736
737 /**
738 All aggregates are framing, i.e. they work on the window's frame. If none
739 is defined, the frame is by default the entire partition, unless ORDER BY
740 is defined, in which case it is the set of rows from the start of the
741 partition to and including the peer set of the current row.
742
743 Some window functions are not framing, i.e. they always work on the entire
744 partition. For such window functions, the method is overridden to
745 return false.
746 */
747 virtual bool framing() const { return true; }
748
749 /**
750 Only for framing window functions. True if this function only needs to
751 read one row per frame.
752 */
753 virtual bool uses_only_one_row() const { return false; }
754
755 /**
756 Return true if we need to make two passes over the rows in the partition -
757 either because we need the cardinality of it (and we need to read all
758 rows to detect the next partition), or we need to have all partition rows
759 available to evaluate the window function for some other reason, e.g.
760 we may need the last row in the partition in the frame buffer to be able
761 to evaluate LEAD.
762 */
763 virtual bool needs_partition_cardinality() const { return false; }
764
765 /**
766 Common initial actions for window functions. For non-buffered processing
767 ("on-the-fly"), check partition change and possible reset partition
768 state. In this case return false.
769 For buffered processing, if windowing state m_do_copy_null is true, set
770 null_value to is_nullable() and return true.
771
772 @return true if case two above holds, else false
773 */
774 bool wf_common_init();
775
776 /// Overridden by Item_rollup_sum_switcher.
777 virtual bool is_rollup_sum_wrapper() const { return false; }
778 /**
779 * In case we are an Item_rollup_sum_switcher,
780 * return the underlying Item_sum, otherwise, return this.
781 * Overridden by Item_rollup_sum_switcher.
782 */
783 virtual const Item_sum *unwrap_sum() const { return this; }
784 /// Non-const version
785 virtual Item_sum *unwrap_sum() { return this; }
786
787 protected:
788 /*
789 Raise an error (ER_NOT_SUPPORTED_YET) with the detail that this
790 function is not yet supported as a window function.
791 */
793 char buff[STRING_BUFFER_USUAL_SIZE];
794 snprintf(buff, sizeof(buff), "%s as window function", func_name());
795 my_error(ER_NOT_SUPPORTED_YET, MYF(0), buff);
796 }
797};
798
799class Unique;
800
801/**
802 The distinct aggregator.
803 Implements AGGFN (DISTINCT ..)
804 Collects all the data into an Unique (similarly to what Item_sum_distinct
805 does currently) and then (if applicable) iterates over the list of
806 unique values and pumps them back into its object
807*/
808
810 friend class Item_sum_sum;
811
812 /*
813 flag to prevent consecutive runs of endup(). Normally in endup there are
814 expensive calculations (like walking the distinct tree for example)
815 which we must do only once if there are no data changes.
816 We can re-use the data for the second and subsequent val_xxx() calls.
817 endup_done set to true also means that the calculated values for
818 the aggregate functions are correct and don't need recalculation.
819 */
821
822 /*
823 Used depending on the type of the aggregate function and the presence of
824 blob columns in it:
825 - For COUNT(DISTINCT) and no blob fields this points to a real temporary
826 table. It's used as a hash table.
827 - For AVG/SUM(DISTINCT) or COUNT(DISTINCT) with blob fields only the
828 in-memory data structure of a temporary table is constructed.
829 It's used by the Field classes to transform data into row format.
830 */
832
833 /*
834 An array of field lengths on row allocated and used only for
835 COUNT(DISTINCT) with multiple columns and no blobs. Used in
836 Aggregator_distinct::composite_key_cmp (called from Unique to compare
837 nodes
838 */
840
841 /*
842 Used in conjunction with 'table' to support the access to Field classes
843 for COUNT(DISTINCT). Needed by copy_fields()/copy_funcs().
844 */
846
847 /*
848 If there are no blobs in the COUNT(DISTINCT) arguments, we can use a tree,
849 which is faster than heap table. In that case, we still use the table
850 to help get things set up, but we insert nothing in it.
851 For AVG/SUM(DISTINCT) we always use this tree (as it takes a single
852 argument) to get the distinct rows.
853 */
855
856 /*
857 The length of the temp table row. Must be a member of the class as it
858 gets passed down to simple_raw_key_cmp () as a compare function argument
859 to Unique. simple_raw_key_cmp () is used as a fast comparison function
860 when the entire row can be binary compared.
861 */
863
866 /**
867 Set to true if the result is known to be always NULL.
868 If set deactivates creation and usage of the temporary table (in the
869 'table' member) and the Unique instance (in the 'tree' member) as well as
870 the calculation of the final value on the first call to
871 @c Item_sum::val_xxx(),
872 @c Item_avg::val_xxx(),
873 @c Item_count::val_xxx().
874 */
876 /**
877 Set to true if count distinct is on only const items. Distinct on a const
878 value will always be the constant itself. And count distinct of the same
879 would always be 1. Similar to CONST_NULL, it avoids creation of temporary
880 table and the Unique instance.
881 */
884
885 /**
886 When feeding back the data in endup() from Unique/temp table back to
887 Item_sum::add() methods we must read the data from Unique (and not
888 recalculate the functions that are given as arguments to the aggregate
889 function.
890 This flag is to tell the arg_*() methods to take the data from the Unique
891 instead of calling the relevant val_..() method.
892 */
894
895 public:
897 : Aggregator(sum),
898 table(nullptr),
900 tree(nullptr),
902 use_distinct_values(false) {}
903 ~Aggregator_distinct() override;
905
906 bool setup(THD *) override;
907 void clear() override;
908 bool add() override;
909 void endup() override;
910 my_decimal *arg_val_decimal(my_decimal *value) override;
911 double arg_val_real() override;
912 bool arg_is_null(bool use_null_value) override;
913
914 bool unique_walk_function(void *element);
915 static int composite_key_cmp(const void *arg, const void *a, const void *b);
916};
917
918/**
919 The pass-through aggregator.
920 Implements AGGFN (DISTINCT ..) by knowing it gets distinct data on input.
921 So it just pumps them back to the Item_sum descendant class.
922*/
924 public:
927
928 bool setup(THD *thd) override { return item_sum->setup(thd); }
929 void clear() override { item_sum->clear(); }
930 bool add() override { return item_sum->add(); }
931 void endup() override {}
932 my_decimal *arg_val_decimal(my_decimal *value) override;
933 double arg_val_real() override;
934 bool arg_is_null(bool use_null_value) override;
935};
936
937class Item_sum_num : public Item_sum {
939
940 protected:
941 /*
942 val_xxx() functions may be called several times during the execution of a
943 query. Derived classes that require extensive calculation in val_xxx()
944 maintain cache of aggregate value. This variable governs the validity of
945 that cache.
946 */
948
949 public:
950 Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
951 : Item_sum(pos, item_par, window), is_evaluated(false) {}
952
954 : Item_sum(pos, list, w), is_evaluated(false) {}
955
957 : Item_sum(thd, item), is_evaluated(item->is_evaluated) {}
959 return MYSQL_TYPE_DOUBLE;
960 }
961
962 Item_sum_num(Item *item_par) : Item_sum(item_par), is_evaluated(false) {}
963
964 bool fix_fields(THD *, Item **) override;
965 longlong val_int() override {
966 assert(fixed == 1);
967 return llrint_with_overflow_check(val_real()); /* Real as default */
968 }
969 String *val_str(String *str) override;
970 my_decimal *val_decimal(my_decimal *) override;
971 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
972 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
973 }
974 bool get_time(MYSQL_TIME *ltime) override {
975 return get_time_from_numeric(ltime); /* Decimal or real */
976 }
977 void reset_field() override;
978};
979
981 public:
982 Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
983 : Item_sum_num(pos, item_par, w) {
985 }
986
988 : Item_sum_num(pos, list, w) {
990 }
991
992 Item_sum_int(THD *thd, Item_sum_int *item) : Item_sum_num(thd, item) {
994 }
995
996 Item_sum_int(Item *item_par) : Item_sum_num(item_par) {
998 }
999
1000 double val_real() override {
1001 assert(fixed);
1002 return static_cast<double>(val_int());
1003 }
1004 String *val_str(String *str) override;
1005 my_decimal *val_decimal(my_decimal *) override;
1006 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1007 return get_date_from_int(ltime, fuzzydate);
1008 }
1009 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
1010 enum Item_result result_type() const override { return INT_RESULT; }
1011};
1012
1014 protected:
1016 double sum;
1019 bool resolve_type(THD *thd) override;
1020 /**
1021 Execution state: this is for counting rows entering and leaving the window
1022 frame, see #m_frame_null_count.
1023 */
1025
1026 /**
1027 Execution state: this is for counting NULLs of rows entering and leaving
1028 the window frame, when we use optimized inverse-based computations. By
1029 comparison with m_count we can know how many non-NULLs are in the frame.
1030 */
1032
1033 public:
1034 Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
1035 : Item_sum_num(pos, item_par, window),
1037 m_count(0),
1039 set_distinct(distinct);
1040 }
1041
1042 Item_sum_sum(THD *thd, Item_sum_sum *item);
1043 enum Sumfunctype sum_func() const override {
1045 }
1046 void clear() override;
1047 bool add() override;
1048 double val_real() override;
1049 longlong val_int() override;
1050 String *val_str(String *str) override;
1051 my_decimal *val_decimal(my_decimal *) override;
1052 enum Item_result result_type() const override { return hybrid_type; }
1053 bool check_wf_semantics1(THD *thd, Query_block *select,
1054 Window_evaluation_requirements *reqs) override;
1055 void no_rows_in_result() override;
1056 void reset_field() override;
1057 void update_field() override;
1058 const char *func_name() const override { return "sum"; }
1059 Item *copy_or_same(THD *thd) override;
1060};
1061
1064
1066
1067 void clear() override;
1068 bool add() override;
1069 void cleanup() override;
1070
1071 public:
1072 Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
1073 : Item_sum_int(pos, item_par, w), count(0) {}
1074 Item_sum_count(Item_int *number) : Item_sum_int(number), count(0) {}
1075 /**
1076 Constructs an instance for COUNT(DISTINCT)
1077
1078 @param pos Position of token in the parser.
1079 @param list A list of the arguments to the aggregate function
1080 @param w A window, if COUNT is used as a windowing function
1081
1082 This constructor is called by the parser only for COUNT (DISTINCT).
1083 */
1084
1086 : Item_sum_int(pos, list, w), count(0) {
1087 set_distinct(true);
1088 }
1090 : Item_sum_int(thd, item), count(item->count) {}
1091 enum Sumfunctype sum_func() const override {
1093 }
1094 bool resolve_type(THD *thd) override {
1095 if (param_type_is_default(thd, 0, -1)) return true;
1096 set_nullable(false);
1097 null_value = false;
1098 return false;
1099 }
1100 void no_rows_in_result() override { count = 0; }
1101 void make_const(longlong count_arg) {
1102 count = count_arg;
1104 }
1105 longlong val_int() override;
1106 void reset_field() override;
1107 void update_field() override;
1108 const char *func_name() const override { return "count"; }
1109 Item *copy_or_same(THD *thd) override;
1110};
1111
1112/* Item to get the value of a stored sum function */
1113
1114class Item_sum_avg;
1115class Item_sum_bit;
1116
1117/**
1118 This is used in connection which a parent Item_sum:
1119 - which can produce different result types (is "hybrid")
1120 - which stores function's value into a temporary table's column (one
1121 row per group).
1122 - which stores in the column some internal piece of information which should
1123 not be returned to the user, so special implementations are needed.
1124*/
1126 protected:
1127 /// The tmp table's column containing the value of the set function.
1129 /// Stores the Item's result type.
1131
1132 public:
1133 enum Item_result result_type() const override { return hybrid_type; }
1134 bool mark_field_in_map(uchar *arg) override {
1135 /*
1136 Filesort (find_all_keys) over a temporary table collects the columns it
1137 needs.
1138 */
1139 return Item::mark_field_in_map(pointer_cast<Mark_field *>(arg), field);
1140 }
1143 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1144 func_arg->banned_function_name = func_name();
1145 return true;
1146 }
1147 void cleanup() override {
1148 field = nullptr;
1150 }
1151};
1152
1153/**
1154 Common abstract class for:
1155 Item_avg_field
1156 Item_variance_field
1157*/
1159 public:
1160 longlong val_int() override {
1161 /* can't be fix_fields()ed */
1163 }
1164 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1165 return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
1166 }
1167 bool get_time(MYSQL_TIME *ltime) override {
1168 return get_time_from_numeric(ltime); /* Decimal or real */
1169 }
1170 bool is_null() override { return update_null_value() || null_value; }
1171};
1172
1174 public:
1177 Item_avg_field(Item_result res_type, Item_sum_avg *item);
1178 enum Type type() const override { return FIELD_AVG_ITEM; }
1179 double val_real() override;
1180 my_decimal *val_decimal(my_decimal *) override;
1181 String *val_str(String *) override;
1182 bool resolve_type(THD *) override { return false; }
1183 const char *func_name() const override {
1184 assert(0);
1185 return "avg_field";
1186 }
1187};
1188
1189/// This is used in connection with an Item_sum_bit, @see Item_sum_hybrid_field
1191 protected:
1193
1194 public:
1197 longlong val_int() override;
1198 double val_real() override;
1199 my_decimal *val_decimal(my_decimal *) override;
1200 String *val_str(String *) override;
1201 bool resolve_type(THD *) override { return false; }
1202 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1203 bool get_time(MYSQL_TIME *ltime) override;
1204 enum Type type() const override { return FIELD_BIT_ITEM; }
1205 const char *func_name() const override {
1206 assert(0);
1207 return "sum_bit_field";
1208 }
1209};
1210
1211/// Common abstraction for Item_sum_json_array and Item_sum_json_object
1212class Item_sum_json : public Item_sum {
1214
1215 protected:
1216 /// String used when reading JSON binary values or JSON text values.
1218 /// String used for converting JSON text values to utf8mb4 charset.
1220 /// Wrapper around the container (object/array) which accumulates the value.
1222
1223 /**
1224 Construct an Item_sum_json instance.
1225
1226 @param wrapper a wrapper around the Json_array or Json_object that contains
1227 the aggregated result
1228 @param parent_args arguments to forward to Item_sum's constructor
1229 */
1230 template <typename... Args>
1232 Args &&... parent_args);
1233
1234 public:
1235 ~Item_sum_json() override;
1236 bool fix_fields(THD *thd, Item **pItem) override;
1237 enum Sumfunctype sum_func() const override { return JSON_AGG_FUNC; }
1238 Item_result result_type() const override { return STRING_RESULT; }
1239
1240 double val_real() override;
1241 longlong val_int() override;
1242 String *val_str(String *str) override;
1243 bool val_json(Json_wrapper *wr) override;
1244 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
1245 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1246 bool get_time(MYSQL_TIME *ltime) override;
1247
1248 void reset_field() override;
1249 void update_field() override;
1250
1253};
1254
1255/// Implements aggregation of values into an array.
1257 /// Accumulates the final value.
1259
1260 public:
1261 Item_sum_json_array(THD *thd, Item_sum *item,
1264 Item_sum_json_array(const POS &pos, Item *a, PT_window *w,
1268 const char *func_name() const override { return "json_arrayagg"; }
1269 void clear() override;
1270 bool add() override;
1271 Item *copy_or_same(THD *thd) override;
1272};
1273
1274/// Implements aggregation of values into an object.
1276 /// Accumulates the final value.
1278 /// Buffer used to get the value of the key.
1280 /**
1281 Map of keys in Json_object and the count for each key
1282 within a window frame. It is used in handling rows
1283 leaving a window frame when rows are not sorted
1284 according to the key in Json_object.
1285 */
1286 std::map<std::string, int> m_key_map;
1287 /**
1288 If window provides ordering on the key in Json_object,
1289 a key_map is not needed to handle rows leaving a window
1290 frame. In this case, process_buffered_windowing_record()
1291 will set flags when a key/value pair can be removed from
1292 the Json_object.
1293 */
1294 bool m_optimize{false};
1295
1296 public:
1297 Item_sum_json_object(THD *thd, Item_sum *item,
1300 Item_sum_json_object(const POS &pos, Item *a, Item *b, PT_window *w,
1304 const char *func_name() const override { return "json_objectagg"; }
1305 void clear() override;
1306 bool add() override;
1307 Item *copy_or_same(THD *thd) override;
1308 bool check_wf_semantics1(THD *thd, Query_block *select,
1309 Window_evaluation_requirements *reqs) override;
1310};
1311
1312class Item_sum_avg final : public Item_sum_sum {
1313 public:
1318 double m_avg;
1319
1320 Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
1321 : Item_sum_sum(pos, item_par, distinct, w) {}
1322
1324 : Item_sum_sum(thd, item), prec_increment(item->prec_increment) {}
1325
1326 bool resolve_type(THD *thd) override;
1327 enum Sumfunctype sum_func() const override {
1329 }
1330 void clear() override;
1331 bool add() override;
1332 double val_real() override;
1333 // In SPs we might force the "wrong" type with select into a declare variable
1335 my_decimal *val_decimal(my_decimal *) override;
1336 String *val_str(String *str) override;
1337 void reset_field() override;
1338 void update_field() override;
1339 Item *result_item(Field *) override {
1340 return new Item_avg_field(hybrid_type, this);
1341 }
1342 const char *func_name() const override { return "avg"; }
1343 Item *copy_or_same(THD *thd) override;
1344 Field *create_tmp_field(bool group, TABLE *table) override;
1345 void cleanup() override {
1346 m_count = 0;
1349 }
1350};
1351
1352class Item_sum_variance;
1353
1355 protected:
1357
1358 public:
1360 enum Type type() const override { return FIELD_VARIANCE_ITEM; }
1361 double val_real() override;
1363 my_decimal *val_decimal(my_decimal *dec_buf) override {
1364 return val_decimal_from_real(dec_buf);
1365 }
1366 bool resolve_type(THD *) override { return false; }
1367 const char *func_name() const override {
1368 assert(0);
1369 return "variance_field";
1370 }
1373 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1374 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1375 return true;
1376 }
1377};
1378
1379/*
1380 variance(a) =
1381
1382 = sum (ai - avg(a))^2 / count(a) )
1383 = sum (ai^2 - 2*ai*avg(a) + avg(a)^2) / count(a)
1384 = (sum(ai^2) - sum(2*ai*avg(a)) + sum(avg(a)^2))/count(a) =
1385 = (sum(ai^2) - 2*avg(a)*sum(a) + count(a)*avg(a)^2)/count(a) =
1386 = (sum(ai^2) - 2*sum(a)*sum(a)/count(a) + count(a)*sum(a)^2/count(a)^2
1387 )/count(a) = = (sum(ai^2) - 2*sum(a)^2/count(a) + sum(a)^2/count(a)
1388 )/count(a) = = (sum(ai^2) - sum(a)^2/count(a))/count(a)
1389
1390 But, this falls prey to catastrophic cancellation.
1391 Instead, we use recurrence formulas in Algorithm I mentoned below
1392 for group aggregates.
1393
1394 Algorithm I:
1395 M_{1} = x_{1}, ~ M_{k} = M_{k-1} + (x_{k} - M_{k-1}) / k newline
1396 S_{1} = 0, ~ S_{k} = S_{k-1} + (x_{k} - M_{k-1}) times (x_{k} - M_{k}) newline
1397 for 2 <= k <= n newline
1398 ital variance = S_{n} / (n-1)
1399
1400 For aggregate window functions algorithm I cannot be optimized for
1401 moving frames since M_{i} changes for every row. So we use the
1402 following algorithm.
1403
1404 Algorithm II:
1405
1406 K = 0
1407 n = 0
1408 ex = 0
1409 ex2 = 0
1410
1411 def add_sample(x):
1412 if (n == 0):
1413 K = x
1414 n = n + 1
1415 ex += x - K
1416 ex2 += (x - K) * (x - K)
1417
1418 def remove_sample(x):
1419 n = n - 1
1420 ex -= (x - K)
1421 ex2 -= (x - K) * (x - K)
1422
1423 def variance():
1424 return (ex2 - (ex*ex)/n) / (n-1)
1425
1426 This formula facilitates incremental computation enabling us to
1427 optimize in case of moving window frames. The optimized codepath is taken
1428 only when windowing_use_high_precision is set to false. By default,
1429 aggregate window functions take the non-optimized codepath.
1430 Note:
1431 Results could differ between optimized and non-optimized code path.
1432 Hence algorithm II is used only when user sets
1433 windowing_use_high_precision to false.
1434*/
1435
1437 bool resolve_type(THD *) override;
1438
1439 public:
1441 /**
1442 Used in recurrence relation.
1443 */
1444 double recurrence_m{0.0};
1445 double recurrence_s{0.0};
1446 double recurrence_s2{0.0};
1450 /**
1451 If set, uses a algorithm II mentioned in the class description
1452 to calculate the variance which helps in optimizing windowing
1453 functions in presence of frames.
1454 */
1456
1457 Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg,
1458 PT_window *w)
1459 : Item_sum_num(pos, item_par, w),
1461 count(0),
1462 sample(sample_arg),
1463 optimize(false) {}
1464
1466 enum Sumfunctype sum_func() const override { return VARIANCE_FUNC; }
1467 void clear() override;
1468 bool add() override;
1469 double val_real() override;
1470 my_decimal *val_decimal(my_decimal *) override;
1471 void reset_field() override;
1472 void update_field() override;
1473 Item *result_item(Field *) override { return new Item_variance_field(this); }
1474 void no_rows_in_result() override {}
1475 const char *func_name() const override {
1476 return sample ? "var_samp" : "variance";
1477 }
1478 Item *copy_or_same(THD *thd) override;
1479 Field *create_tmp_field(bool group, TABLE *table) override;
1480 enum Item_result result_type() const override { return REAL_RESULT; }
1481 void cleanup() override {
1482 count = 0;
1484 }
1485 bool check_wf_semantics1(THD *thd, Query_block *select,
1486 Window_evaluation_requirements *reqs) override;
1487};
1488
1489class Item_sum_std;
1490
1492 public:
1494 enum Type type() const override { return FIELD_STD_ITEM; }
1495 double val_real() override;
1496 my_decimal *val_decimal(my_decimal *) override;
1497 enum Item_result result_type() const override { return REAL_RESULT; }
1498 const char *func_name() const override {
1499 assert(0);
1500 return "std_field";
1501 }
1504 pointer_cast<Check_function_as_value_generator_parameters *>(args);
1505 func_arg->err_code = func_arg->get_unnamed_function_error_code();
1506 return true;
1507 }
1508};
1509
1510/*
1511 standard_deviation(a) = sqrt(variance(a))
1512*/
1513
1515 public:
1516 Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
1517 : Item_sum_variance(pos, item_par, sample_arg, w) {}
1518
1519 Item_sum_std(THD *thd, Item_sum_std *item) : Item_sum_variance(thd, item) {}
1520 enum Sumfunctype sum_func() const override { return STD_FUNC; }
1521 double val_real() override;
1522 Item *result_item(Field *) override { return new Item_std_field(this); }
1523 const char *func_name() const override {
1524 return sample ? "stddev_samp" : "std";
1525 }
1526 Item *copy_or_same(THD *thd) override;
1527 enum Item_result result_type() const override { return REAL_RESULT; }
1528};
1529
1530// This class is a string or number function depending on num_func
1531class Arg_comparator;
1532
1533/**
1534 Abstract base class for the MIN and MAX aggregate functions.
1535*/
1538
1539 private:
1540 /**
1541 Tells if this is the MIN function (true) or the MAX function (false).
1542 */
1543 const bool m_is_min;
1544 /*
1545 For window functions MIN/MAX with optimized code path, no comparisons
1546 are needed beyond NULL detection: MIN/MAX are then roughly equivalent to
1547 FIRST/LAST_VALUE. For this case, 'value' is the value of
1548 the window function a priori taken from args[0], while arg_cache is used to
1549 remember the value from the previous row. NULLs need a bit of careful
1550 treatment.
1551 */
1555 bool was_values; // Set if we have found at least one row (for max/min only)
1556 /**
1557 Set to true if the window is ordered ascending.
1558 */
1560 /**
1561 Set to true when min/max can be optimized using window's ordering.
1562 */
1564 /**
1565 For min() - Set to true when results are ordered in ascending and
1566 false when descending.
1567 For max() - Set to true when results are ordered in descending and
1568 false when ascending.
1569 Valid only when m_optimize is true.
1570 */
1571 bool m_want_first; ///< Want first non-null value, else last non_null value
1572 /**
1573 Execution state: keeps track if this is the first row in the frame
1574 when buffering is not needed.
1575 Valid only when m_optimize is true.
1576 */
1578
1579 /**
1580 Execution state: keeps track of at which row we saved a non-null last
1581 value.
1582 */
1584
1585 /**
1586 This function implements the optimized version of retrieving min/max
1587 value. When we have "ordered ASC" results in a window, min will always
1588 be the first value in the result set (neglecting the NULL's) and max
1589 will always be the last value (or the other way around, if ordered DESC).
1590 It is based on the implementation of FIRST_VALUE/LAST_VALUE, except for
1591 the NULL handling.
1592
1593 @return true if computation yielded a NULL or error
1594 */
1595 bool compute();
1596
1597 /**
1598 MIN/MAX function setup.
1599
1600 Setup cache/comparator of MIN/MAX functions. When called by the
1601 copy_or_same() function, the value_arg parameter contains the calculated
1602 value of the original MIN/MAX object, and it is saved in this object's
1603 cache.
1604
1605 @param item the argument of the MIN/MAX function
1606 @param value_arg the calculated value of the MIN/MAX function
1607 @return false on success, true on error
1608 */
1609 bool setup_hybrid(Item *item, Item *value_arg);
1610
1611 /** Create a clone of this object. */
1612 virtual Item_sum_hybrid *clone_hybrid(THD *thd) const = 0;
1613
1614 protected:
1615 Item_sum_hybrid(Item *item_par, bool is_min)
1616 : Item_sum(item_par),
1617 m_is_min(is_min),
1618 value(nullptr),
1620 cmp(nullptr),
1622 was_values(true),
1623 m_nulls_first(false),
1624 m_optimize(false),
1625 m_want_first(false),
1626 m_cnt(0),
1629 }
1630
1631 Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
1632 : Item_sum(pos, item_par, w),
1633 m_is_min(is_min),
1634 value(nullptr),
1636 cmp(nullptr),
1638 was_values(true),
1639 m_nulls_first(false),
1640 m_optimize(false),
1641 m_want_first(false),
1642 m_cnt(0),
1645 }
1646
1648 : Item_sum(thd, item),
1649 m_is_min(item->m_is_min),
1650 value(item->value),
1652 hybrid_type(item->hybrid_type),
1653 was_values(item->was_values),
1655 m_optimize(item->m_optimize),
1657 m_cnt(item->m_cnt),
1659
1660 public:
1661 bool fix_fields(THD *, Item **) override;
1662 void clear() override;
1663 void update_after_wf_arguments_changed(THD *thd) override;
1664 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
1665 mem_root_deque<Item *> *fields) override;
1666 double val_real() override;
1667 longlong val_int() override;
1668 longlong val_time_temporal() override;
1669 longlong val_date_temporal() override;
1670 my_decimal *val_decimal(my_decimal *) override;
1671 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1672 bool get_time(MYSQL_TIME *ltime) override;
1673 void reset_field() override;
1674 String *val_str(String *) override;
1675 bool val_json(Json_wrapper *wr) override;
1676 bool keep_field_type() const override { return true; }
1677 enum Item_result result_type() const override { return hybrid_type; }
1678 TYPELIB *get_typelib() const override {
1679 assert(sum_func() == MAX_FUNC || sum_func() == MIN_FUNC);
1680 return arguments()[0]->get_typelib();
1681 }
1682 void update_field() override;
1683 void cleanup() override;
1684 bool any_value() { return was_values; }
1685 void no_rows_in_result() override;
1686 Field *create_tmp_field(bool group, TABLE *table) override;
1687 bool uses_only_one_row() const override { return m_optimize; }
1688 bool add() override;
1689 Item *copy_or_same(THD *thd) override;
1690 bool check_wf_semantics1(THD *thd, Query_block *select,
1692
1693 private:
1694 /*
1695 These functions check if the value on the current row exceeds the maximum or
1696 minimum value seen so far, and update the current max/min stored in
1697 result_field, if needed.
1698 */
1705};
1706
1707class Item_sum_min final : public Item_sum_hybrid {
1708 public:
1709 Item_sum_min(Item *item_par) : Item_sum_hybrid(item_par, true) {}
1710 Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
1711 : Item_sum_hybrid(pos, item_par, true, w) {}
1712 Item_sum_min(THD *thd, const Item_sum_min *item)
1713 : Item_sum_hybrid(thd, item) {}
1714 enum Sumfunctype sum_func() const override { return MIN_FUNC; }
1715 const char *func_name() const override { return "min"; }
1716
1717 private:
1718 Item_sum_min *clone_hybrid(THD *thd) const override;
1719};
1720
1721class Item_sum_max final : public Item_sum_hybrid {
1722 public:
1723 Item_sum_max(Item *item_par) : Item_sum_hybrid(item_par, false) {}
1724 Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
1725 : Item_sum_hybrid(pos, item_par, false, w) {}
1726 Item_sum_max(THD *thd, const Item_sum_max *item)
1727 : Item_sum_hybrid(thd, item) {}
1728 enum Sumfunctype sum_func() const override { return MAX_FUNC; }
1729 const char *func_name() const override { return "max"; }
1730
1731 private:
1732 Item_sum_max *clone_hybrid(THD *thd) const override;
1733};
1734
1735/**
1736 Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
1737
1738 Each of them is both a set function and a framing window function.
1739*/
1740class Item_sum_bit : public Item_sum {
1742 /// Stores the neutral element for function
1744 /// Stores the result value for the INT_RESULT
1746 /// Stores the result value for the STRING_RESULT
1748 /// Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT
1750 /// Buffer used to avoid String allocation in the constructor
1751 const char initial_value_buff_storage[1] = {0};
1752
1753 /**
1754 Execution state (windowing): this is for counting rows entering and leaving
1755 the window frame, see #m_frame_null_count.
1756 */
1758
1759 /**
1760 Execution state (windowing): this is for counting NULLs of rows entering
1761 and leaving the window frame, when we use optimized inverse-based
1762 computations. By comparison with m_count we can know how many non-NULLs are
1763 in the frame.
1764 */
1766
1767 /**
1768 Execution state (windowing): Used for AND, OR to be able to invert window
1769 functions in optimized mode.
1770
1771 For the optimized code path of BIT_XXX wfs, we keep track of the number of
1772 bit values (0's or 1's; see below) seen in a frame using a 64 bits counter
1773 pr bit. This lets us compute the value of OR by just inspecting:
1774
1775 - the number of 1's in the previous frame
1776 - whether any removed row(s) is a 1
1777 - whether any added row(s) is a 1
1778
1779 Similarly for AND, we keep track of the number of 0's seen for a particular
1780 bit. To do this trick we need a counter per bit position. This array holds
1781 these counters.
1782
1783 Note that for XOR, the inverse operation is identical to the operation,
1784 so we don't need the above.
1785 */
1787 /*
1788 Size of allocated array m_digit_cnt.
1789 The size is DIGIT_CNT_CARD (for integer types) or ::max_length * 8 for bit
1790 strings.
1791 */
1793
1794 static constexpr uint DIGIT_CNT_CARD = sizeof(ulonglong) * 8;
1795
1796 protected:
1797 bool m_is_xor; ///< true iff BIT_XOR
1798
1799 public:
1800 Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg,
1801 PT_window *w)
1802 : Item_sum(pos, item_par, w),
1803 reset_bits(reset_arg),
1804 bits(reset_arg),
1806 m_count(0),
1810 m_is_xor(false) {}
1811
1812 /// Copy constructor, used for executing subqueries with temporary tables
1814 : Item_sum(thd, item),
1815 reset_bits(item->reset_bits),
1816 bits(item->bits),
1818 hybrid_type(item->hybrid_type),
1819 m_count(item->m_count),
1823 m_is_xor(item->m_is_xor) {
1824 /*
1825 This constructor should only be called during the Optimize stage.
1826 Asserting that the item was not evaluated yet.
1827 */
1828 assert(item->value_buff.length() == 1);
1829 assert(item->bits == item->reset_bits);
1830 }
1831
1832 Item *result_item(Field *) override {
1833 return new Item_sum_bit_field(hybrid_type, this, reset_bits);
1834 }
1835
1836 enum Sumfunctype sum_func() const override { return SUM_BIT_FUNC; }
1837 enum Item_result result_type() const override { return hybrid_type; }
1838 void clear() override;
1839 longlong val_int() override;
1840 double val_real() override;
1841 String *val_str(String *str) override;
1842 my_decimal *val_decimal(my_decimal *decimal_value) override;
1843 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
1844 bool get_time(MYSQL_TIME *ltime) override;
1845 void reset_field() override;
1846 void update_field() override;
1847 bool resolve_type(THD *) override;
1848 bool fix_fields(THD *thd, Item **ref) override;
1849 void cleanup() override {
1850 bits = reset_bits;
1851 // At end of one execution of statement, free buffer to reclaim memory:
1854 }
1855
1856 /**
1857 Common implementation of Item_sum_or::add, Item_sum_and:add
1858 and Item_sum_xor::add.
1859 */
1860 bool add() override;
1861 /// @returns true iff this is BIT_AND.
1862 inline bool is_and() const { return reset_bits != 0; }
1863
1864 private:
1865 /**
1866 Accumulate the value of 's1' (if in string mode) or of 'b1' (if in integer
1867 mode). Updates 'value_buff' or 'bits'.
1868
1869 @param s1 argument to accumulate
1870 @param b1 argument to accumulate
1871
1872 @returns true if error
1873 */
1874 bool add_bits(const String *s1, ulonglong b1);
1875
1876 /**
1877 For windowing: perform inverse aggregation. "De-accumulate" the value of
1878 's1' (if in string mode) or of 'b1' (if in integer mode). Updates
1879 'value_buff' or 'bits'.
1880
1881 For BIT_XOR we simply apply XOR as it's its inverse operation. For BIT_OR
1882 and BIT_AND we do the rest below.
1883
1884 For each bit in argument, decrement the corresponding bits's counter
1885 ('m_digit_cnt') for that bit as follows: for BIT_AND, decrement the
1886 counter if we see a zero in that bit; for BIT_OR decrement the counter if
1887 we see a 1 in that bit. Next, update 'value_buff' or 'bits' using the
1888 resulting counters: for each bit, for BIT_AND, set bit if we have counter
1889 == 0, i.e. we have no zero bits for that bit in the frame (yet). For
1890 BIT_OR, set bit if we have counter > 0, i.e. at least one row in the frame
1891 has that bit set.
1892
1893 @param s1 the bits to be inverted from the aggregate value
1894 @param b1 the bits to be inverted from the aggregate value
1895 */
1896 void remove_bits(const String *s1, ulonglong b1);
1897};
1898
1899class Item_sum_or final : public Item_sum_bit {
1900 public:
1901 Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
1902 : Item_sum_bit(pos, item_par, 0LL, w) {}
1903
1904 Item_sum_or(THD *thd, Item_sum_or *item) : Item_sum_bit(thd, item) {}
1905 const char *func_name() const override { return "bit_or"; }
1906 Item *copy_or_same(THD *thd) override;
1907};
1908
1909class Item_sum_and final : public Item_sum_bit {
1910 public:
1911 Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
1912 : Item_sum_bit(pos, item_par, ULLONG_MAX, w) {}
1913
1914 Item_sum_and(THD *thd, Item_sum_and *item) : Item_sum_bit(thd, item) {}
1915 const char *func_name() const override { return "bit_and"; }
1916 Item *copy_or_same(THD *thd) override;
1917};
1918
1919class Item_sum_xor final : public Item_sum_bit {
1920 public:
1921 Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
1922 : Item_sum_bit(pos, item_par, 0LL, w) {
1923 m_is_xor = true;
1924 }
1925
1926 Item_sum_xor(THD *thd, Item_sum_xor *item) : Item_sum_bit(thd, item) {}
1927 const char *func_name() const override { return "bit_xor"; }
1928 Item *copy_or_same(THD *thd) override;
1929};
1930
1931/*
1932 User defined aggregates
1933*/
1934
1935class Item_udf_sum : public Item_sum {
1937
1938 protected:
1940
1941 public:
1942 Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1943 : Item_sum(pos, opt_list, nullptr), udf(udf_arg) {
1945 }
1947 : Item_sum(thd, item), udf(item->udf) {
1948 udf.m_original = false;
1949 }
1950 ~Item_udf_sum() override {
1952 }
1953
1954 bool itemize(Parse_context *pc, Item **res) override;
1955 const char *func_name() const override { return udf.name(); }
1956 bool fix_fields(THD *thd, Item **ref) override {
1957 assert(fixed == 0);
1958
1959 if (init_sum_func_check(thd)) return true;
1960
1961 fixed = true;
1962 if (udf.fix_fields(thd, this, this->arg_count, this->args)) return true;
1963
1964 return check_sum_func(thd, ref);
1965 }
1966 enum Sumfunctype sum_func() const override { return UDF_SUM_FUNC; }
1967
1968 void clear() override;
1969 bool add() override;
1970 void reset_field() override {}
1971 void update_field() override {}
1972 void cleanup() override;
1973 void print(const THD *thd, String *str,
1974 enum_query_type query_type) const override;
1975};
1976
1977class Item_sum_udf_float final : public Item_udf_sum {
1978 public:
1979 Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
1980 : Item_udf_sum(pos, udf_arg, opt_list) {}
1982 : Item_udf_sum(thd, item) {}
1983 longlong val_int() override {
1984 assert(fixed == 1);
1985 return (longlong)rint(Item_sum_udf_float::val_real());
1986 }
1987 double val_real() override;
1988 String *val_str(String *str) override;
1989 my_decimal *val_decimal(my_decimal *) override;
1990 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
1991 return get_date_from_real(ltime, fuzzydate);
1992 }
1993 bool get_time(MYSQL_TIME *ltime) override {
1994 return get_time_from_real(ltime);
1995 }
1996 bool resolve_type(THD *) override {
1999 return false;
2000 }
2001 Item *copy_or_same(THD *thd) override;
2002};
2003
2004class Item_sum_udf_int final : public Item_udf_sum {
2005 public:
2006 Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2007 : Item_udf_sum(pos, udf_arg, opt_list) {}
2009 : Item_udf_sum(thd, item) {}
2010 longlong val_int() override;
2011 double val_real() override {
2012 assert(fixed == 1);
2013 return (double)Item_sum_udf_int::val_int();
2014 }
2015 String *val_str(String *str) override;
2016 my_decimal *val_decimal(my_decimal *) override;
2017 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2018 return get_date_from_int(ltime, fuzzydate);
2019 }
2020 bool get_time(MYSQL_TIME *ltime) override { return get_time_from_int(ltime); }
2021 enum Item_result result_type() const override { return INT_RESULT; }
2022 bool resolve_type(THD *) override {
2024 return false;
2025 }
2026 Item *copy_or_same(THD *thd) override;
2027};
2028
2029class Item_sum_udf_str final : public Item_udf_sum {
2030 public:
2031 Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
2032 : Item_udf_sum(pos, udf_arg, opt_list) {}
2034 : Item_udf_sum(thd, item) {}
2035 String *val_str(String *) override;
2036 double val_real() override {
2037 int err_not_used;
2038 const char *end_not_used;
2039 String *res;
2040 res = val_str(&str_value);
2041 return res ? my_strntod(res->charset(), res->ptr(), res->length(),
2042 &end_not_used, &err_not_used)
2043 : 0.0;
2044 }
2045 longlong val_int() override {
2046 int err_not_used;
2047 String *res;
2048 const CHARSET_INFO *cs;
2049
2050 if (!(res = val_str(&str_value))) return 0; /* Null value */
2051 cs = res->charset();
2052 const char *end = res->ptr() + res->length();
2053 return cs->cset->strtoll10(cs, res->ptr(), &end, &err_not_used);
2054 }
2056 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2057 return get_date_from_string(ltime, fuzzydate);
2058 }
2059 bool get_time(MYSQL_TIME *ltime) override {
2060 return get_time_from_string(ltime);
2061 }
2062 enum Item_result result_type() const override { return STRING_RESULT; }
2063 bool resolve_type(THD *) override;
2064 Item *copy_or_same(THD *thd) override;
2065};
2066
2068 public:
2069 Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg,
2070 PT_item_list *opt_list)
2071 : Item_udf_sum(pos, udf_arg, opt_list) {}
2073 : Item_udf_sum(thd, item) {}
2074 String *val_str(String *) override;
2075 double val_real() override;
2076 longlong val_int() override;
2077 my_decimal *val_decimal(my_decimal *) override;
2078 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2079 return get_date_from_decimal(ltime, fuzzydate);
2080 }
2081 bool get_time(MYSQL_TIME *ltime) override {
2082 return get_time_from_decimal(ltime);
2083 }
2084 enum Item_result result_type() const override { return DECIMAL_RESULT; }
2085 bool resolve_type(THD *) override {
2088 return false;
2089 }
2090 Item *copy_or_same(THD *thd) override;
2091};
2092
2093int group_concat_key_cmp_with_distinct(const void *arg, const void *key1,
2094 const void *key2);
2095int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2096 const void *key2);
2097int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2098 void *item_arg);
2099
2100class Item_func_group_concat final : public Item_sum {
2102
2103 /// True if GROUP CONCAT has the DISTINCT attribute
2105 /// The number of ORDER BY items.
2107 /// The number of selected items, aka the concat field list
2109 /// Resolver context, points to containing query block
2111 /// String containing separator between group items
2113 /// Describes the temporary table used to perform group concat
2117 TREE *tree{nullptr};
2118
2119 /**
2120 If DISTINCT is used with this GROUP_CONCAT, this member is used to filter
2121 out duplicates.
2122 @see Item_func_group_concat::setup
2123 @see Item_func_group_concat::add
2124 @see Item_func_group_concat::clear
2125 */
2127 /// Temporary table used to perform group concat
2128 TABLE *table{nullptr};
2131 /**
2132 The maximum permitted result length in bytes as set in
2133 group_concat_max_len system variable
2134 */
2136 bool warning_for_row{false};
2138 /// True if result has been written to output buffer.
2140 /**
2141 Following is 0 normal object and pointer to original one for copy
2142 (to correctly free resources)
2143 */
2145
2146 friend int group_concat_key_cmp_with_distinct(const void *arg,
2147 const void *key1,
2148 const void *key2);
2149 friend int group_concat_key_cmp_with_order(const void *arg, const void *key1,
2150 const void *key2);
2151 friend int dump_leaf_key(void *key_arg, element_count count [[maybe_unused]],
2152 void *item_arg);
2153
2154 public:
2155 Item_func_group_concat(const POS &pos, bool is_distinct,
2156 PT_item_list *select_list,
2157 PT_order_list *opt_order_list, String *separator,
2158 PT_window *w);
2159
2162 assert(original != nullptr || unique_filter == nullptr);
2163 }
2164
2165 bool itemize(Parse_context *pc, Item **res) override;
2166 void cleanup() override;
2167
2168 enum Sumfunctype sum_func() const override { return GROUP_CONCAT_FUNC; }
2169 const char *func_name() const override { return "group_concat"; }
2170 Item_result result_type() const override { return STRING_RESULT; }
2171 Field *make_string_field(TABLE *table_arg) const override;
2172 void clear() override;
2173 bool add() override;
2174 void reset_field() override { assert(0); } // not used
2175 void update_field() override { assert(0); } // not used
2176 bool fix_fields(THD *, Item **) override;
2177 bool setup(THD *thd) override;
2178 void make_unique() override;
2179 double val_real() override;
2180 longlong val_int() override {
2181 String *res;
2182 int error;
2183 if (!(res = val_str(&str_value))) return (longlong)0;
2184 const char *end_ptr = res->ptr() + res->length();
2185 return my_strtoll10(res->ptr(), &end_ptr, &error);
2186 }
2187 my_decimal *val_decimal(my_decimal *decimal_value) override {
2188 return val_decimal_from_string(decimal_value);
2189 }
2190 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2191 return get_date_from_string(ltime, fuzzydate);
2192 }
2193 bool get_time(MYSQL_TIME *ltime) override {
2194 return get_time_from_string(ltime);
2195 }
2196
2197 bool has_distinct() const noexcept { return distinct; }
2198 const String *get_separator_str() const noexcept { return separator; }
2199 uint32_t get_group_concat_max_len() const noexcept {
2200 return group_concat_max_len;
2201 }
2202 const Mem_root_array<ORDER> &get_order_array() const noexcept {
2203 return order_array;
2204 }
2205
2206 String *val_str(String *str) override;
2207 Item *copy_or_same(THD *thd) override;
2208 void no_rows_in_result() override;
2209 void print(const THD *thd, String *str,
2210 enum_query_type query_type) const override;
2211 bool change_context_processor(uchar *arg) override {
2212 context = pointer_cast<Item_ident::Change_context *>(arg)->m_context;
2213 return false;
2214 }
2215
2217 Window_evaluation_requirements *) override {
2219 return true;
2220 }
2221};
2222
2223/**
2224 Common parent class for window functions that always work on the entire
2225 partition, even if a frame is defined.
2226
2227 The subclasses can be divided in two disjoint sub-categories:
2228 - one-pass
2229 - two-pass (requires partition cardinality to be evaluated)
2230 cf. method needs_partition_cardinality.
2231*/
2234
2235 public:
2236 Item_non_framing_wf(const POS &pos, PT_window *w) : Item_sum(pos, w) {}
2238 : Item_sum(pos, a, w) {}
2240 : Item_sum(pos, opt_list, w) {}
2242
2243 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
2244 return get_date_from_numeric(ltime, fuzzydate);
2245 }
2246
2247 bool get_time(MYSQL_TIME *ltime) override {
2248 return get_time_from_numeric(ltime);
2249 }
2250
2251 void reset_field() override { assert(false); }
2252 void update_field() override { assert(false); }
2253 bool add() override {
2254 assert(false);
2255 return false;
2256 }
2257
2258 bool fix_fields(THD *thd, Item **items) override;
2259
2260 bool framing() const override { return false; }
2261};
2262
2263/**
2264 ROW_NUMBER window function, cf. SQL 2003 Section 6.10 <window function>
2265*/
2267 // Execution state variables
2268 ulonglong m_ctr; ///< Increment for each row in partition
2269
2270 public:
2272 : Item_non_framing_wf(pos, w), m_ctr(0) {
2273 unsigned_flag = true;
2274 }
2275
2276 const char *func_name() const override { return "row_number"; }
2277 enum Sumfunctype sum_func() const override { return ROW_NUMBER_FUNC; }
2278
2279 bool resolve_type(THD *thd [[maybe_unused]]) override {
2281 return false;
2282 }
2283
2284 longlong val_int() override;
2285 double val_real() override;
2286 my_decimal *val_decimal(my_decimal *buff) override;
2287 String *val_str(String *) override;
2288
2289 void clear() override;
2290
2291 Item_result result_type() const override { return INT_RESULT; }
2292
2294 Window_evaluation_requirements *) override {
2295 return false;
2296 }
2297};
2298
2299/**
2300 RANK or DENSE_RANK window function, cf. SQL 2003 Section 6.10 <window
2301 function>
2302*/
2305 bool m_dense; ///< If true, the object represents DENSE_RANK
2306 // Execution state variables
2307 ulonglong m_rank_ctr; ///< Increment when window order columns change
2308 ulonglong m_duplicates; ///< Needed to make RANK different from DENSE_RANK
2310 m_previous; ///< Values of previous row's ORDER BY items
2311 public:
2312 Item_rank(const POS &pos, bool dense, PT_window *w)
2313 : Item_non_framing_wf(pos, w),
2314 m_dense(dense),
2315 m_rank_ctr(0),
2316 m_duplicates(0),
2318 unsigned_flag = true;
2319 }
2320
2321 ~Item_rank() override;
2322
2323 const char *func_name() const override {
2324 return m_dense ? "dense_rank" : "rank";
2325 }
2326
2327 enum Sumfunctype sum_func() const override {
2329 }
2330
2331 bool resolve_type(THD *thd [[maybe_unused]]) override {
2333 return false;
2334 }
2335
2336 longlong val_int() override;
2337 double val_real() override;
2338 my_decimal *val_decimal(my_decimal *buff) override;
2339 String *val_str(String *) override;
2340
2341 void update_after_wf_arguments_changed(THD *thd) override;
2342 bool check_wf_semantics1(THD *thd, Query_block *select,
2343 Window_evaluation_requirements *reqs) override;
2344 /**
2345 Clear state for a new partition
2346 */
2347 void clear() override;
2348 Item_result result_type() const override { return INT_RESULT; }
2349};
2350
2351/**
2352 CUME_DIST window function, cf. SQL 2003 Section 6.10 <window function>
2353*/
2356
2357 public:
2359
2360 const char *func_name() const override { return "cume_dist"; }
2361 enum Sumfunctype sum_func() const override { return CUME_DIST_FUNC; }
2362
2363 bool resolve_type(THD *thd [[maybe_unused]]) override {
2365 return false;
2366 }
2367
2368 bool check_wf_semantics1(THD *thd, Query_block *select,
2369 Window_evaluation_requirements *reqs) override;
2370
2371 bool needs_partition_cardinality() const override { return true; }
2372 void clear() override {}
2373 longlong val_int() override;
2374 double val_real() override;
2375 String *val_str(String *) override;
2377 Item_result result_type() const override { return REAL_RESULT; }
2378};
2379
2380/**
2381 PERCENT_RANK window function, cf. SQL 2003 Section 6.10 <window function>
2382*/
2385 // Execution state variables
2386 ulonglong m_rank_ctr; ///< Increment when window order columns change
2387 ulonglong m_peers; ///< Needed to make PERCENT_RANK same for peers
2388 /**
2389 Set when the last peer has been visited. Needed to increment m_rank_ctr.
2390 */
2392
2393 public:
2395 : Item_non_framing_wf(pos, w),
2396 m_rank_ctr(0),
2397 m_peers(0),
2398 m_last_peer_visited(false) {}
2399
2401 const char *func_name() const override { return "percent_rank"; }
2402 enum Sumfunctype sum_func() const override { return PERCENT_RANK_FUNC; }
2403
2404 bool resolve_type(THD *thd [[maybe_unused]]) override {
2406 return false;
2407 }
2408
2409 bool check_wf_semantics1(THD *thd, Query_block *select,
2410 Window_evaluation_requirements *reqs) override;
2411 bool needs_partition_cardinality() const override { return true; }
2412
2413 void clear() override;
2414 longlong val_int() override;
2415 double val_real() override;
2416 String *val_str(String *) override;
2418 Item_result result_type() const override { return REAL_RESULT; }
2419};
2420
2421/**
2422 NTILE window function, cf. SQL 2011 Section 6.10 <window function>
2423*/
2427
2428 public:
2429 Item_ntile(const POS &pos, Item *a, PT_window *w)
2430 : Item_non_framing_wf(pos, a, w), m_value(0) {
2431 unsigned_flag = true;
2432 }
2433
2434 const char *func_name() const override { return "ntile"; }
2435 enum Sumfunctype sum_func() const override { return NTILE_FUNC; }
2436
2437 bool resolve_type(THD *thd) override {
2438 if (args[0]->propagate_type(thd, MYSQL_TYPE_LONGLONG, true)) return true;
2440 return false;
2441 }
2442
2443 bool fix_fields(THD *thd, Item **items) override;
2444
2445 longlong val_int() override;
2446 double val_real() override;
2447 my_decimal *val_decimal(my_decimal *buff) override;
2448 String *val_str(String *) override;
2449
2450 bool check_wf_semantics1(THD *thd, Query_block *select,
2451 Window_evaluation_requirements *reqs) override;
2453 Item_result result_type() const override { return INT_RESULT; }
2454 void clear() override {}
2455 bool needs_partition_cardinality() const override { return true; }
2456};
2457
2458/**
2459 LEAD/LAG window functions, cf. SQL 2011 Section 6.10 <window function>
2460*/
2463 bool m_is_lead; ///< if true, the function is LEAD, else LAG
2464 int64 m_n; ///< canonicalized offset value
2468 /**
2469 Execution state: if set, we already have a value for current row.
2470 State is used to avoid interference with other LEAD/LAG functions on
2471 the same window, since they share the same eval loop and they should
2472 trigger evaluation only when they are on the "right" row relative to
2473 current row. For other offsets, return NULL if we don't know the value
2474 for this function yet, or if we do (m_has_value==true), return the
2475 found value.
2476 */
2478 bool m_use_default; ///< execution state: use default value for current row
2480
2481 public:
2482 Item_lead_lag(const POS &pos, bool lead,
2483 PT_item_list *opt_list, // [0] expr, [1] offset, [2] default
2484 enum_null_treatment null_treatment, PT_window *w)
2485 : Item_non_framing_wf(pos, opt_list, w),
2486 m_null_treatment(null_treatment),
2487 m_is_lead(lead),
2488 m_n(0),
2492 m_has_value(false),
2493 m_use_default(false) {}
2494
2495 const char *func_name() const override {
2496 return (m_is_lead ? "lead" : "lag");
2497 }
2498 enum Sumfunctype sum_func() const override { return LEAD_LAG_FUNC; }
2499
2500 bool resolve_type(THD *thd) override;
2501 bool fix_fields(THD *thd, Item **items) override;
2502 void update_after_wf_arguments_changed(THD *thd) override;
2503 void clear() override;
2504 bool check_wf_semantics1(THD *thd, Query_block *select,
2505 Window_evaluation_requirements *reqs) override;
2507 enum Item_result result_type() const override { return m_hybrid_type; }
2508
2509 longlong val_int() override;
2510 double val_real() override;
2511 String *val_str(String *str) override;
2512 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2513
2514 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2515 bool get_time(MYSQL_TIME *ltime) override;
2516 bool val_json(Json_wrapper *wr) override;
2517
2518 bool needs_partition_cardinality() const override {
2519 /*
2520 A possible optimization here: if LAG, we are only interested in rows we
2521 have already seen, so we might compute the result without reading the
2522 entire partition as soon as we have the current row. Similarly, a small
2523 LEAD value might avoid reading the entire partition also, giving shorter
2524 time to first result. For now, we read the entirely partition for these
2525 window functions - for simplicity.
2526 */
2527 return true;
2528 }
2529
2530 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
2531 mem_root_deque<Item *> *fields) override;
2532
2533 void set_has_value(bool value) { m_has_value = value; }
2534 bool has_value() const { return m_has_value; }
2535
2536 void set_use_default(bool value) { m_use_default = value; }
2537 bool use_default() const { return m_use_default; }
2538
2539 private:
2540 bool setup_lead_lag();
2541 /**
2542 Core logic of LEAD/LAG window functions
2543
2544 @return true if computation yielded a NULL or error
2545 */
2546 bool compute();
2547};
2548
2549/**
2550 FIRST_VALUE/LAST_VALUE window functions, cf. SQL 2011 Section 6.10 <window
2551 function>
2552*/
2554 bool m_is_first; ///< if true, the function is FIRST_VALUE, else LAST_VALUE
2558 int64 cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2560
2561 public:
2562 Item_first_last_value(const POS &pos, bool first, Item *a,
2563 enum_null_treatment null_treatment, PT_window *w)
2564 : Item_sum(pos, a, w),
2565 m_is_first(first),
2566 m_null_treatment(null_treatment),
2569 cnt(0) {}
2570
2571 const char *func_name() const override {
2572 return m_is_first ? "first_value" : "last_value";
2573 }
2574
2575 enum Sumfunctype sum_func() const override { return FIRST_LAST_VALUE_FUNC; }
2576
2577 bool resolve_type(THD *thd) override;
2578 bool fix_fields(THD *thd, Item **items) override;
2579 void update_after_wf_arguments_changed(THD *thd) override;
2580 void clear() override;
2581 bool check_wf_semantics1(THD *thd, Query_block *select,
2582 Window_evaluation_requirements *reqs) override;
2583 enum Item_result result_type() const override { return m_hybrid_type; }
2584
2585 longlong val_int() override;
2586 double val_real() override;
2587 String *val_str(String *str) override;
2588 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2589
2590 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2591 bool get_time(MYSQL_TIME *ltime) override;
2592 bool val_json(Json_wrapper *wr) override;
2593
2594 void reset_field() override { assert(false); }
2595 void update_field() override { assert(false); }
2596 bool add() override {
2597 assert(false);
2598 return false;
2599 }
2600
2601 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
2602 mem_root_deque<Item *> *fields) override;
2603 bool uses_only_one_row() const override { return true; }
2604
2605 private:
2606 bool setup_first_last();
2607 /**
2608 Core logic of FIRST/LAST_VALUE window functions
2609
2610 @return true if computation yielded a NULL or error
2611 */
2612 bool compute();
2613};
2614
2615/**
2616 NTH_VALUE window function, cf. SQL 2011 Section 6.10 <window
2617 function>
2618*/
2619class Item_nth_value : public Item_sum {
2621 int64 m_n; ///< The N of the function
2622 bool m_from_last; ///< true iff FROM_LAST was specified
2626 int64 m_cnt; ///< used when evaluating on-the-fly (non-buffered processing)
2627
2629
2630 public:
2631 Item_nth_value(const POS &pos, PT_item_list *a, bool from_last,
2632 enum_null_treatment null_treatment, PT_window *w)
2633 : Item_sum(pos, a, w),
2634 m_null_treatment(null_treatment),
2635 m_n(0),
2636 m_from_last(from_last),
2639 m_cnt(0) {}
2640
2641 const char *func_name() const override { return "nth_value"; }
2642 enum Sumfunctype sum_func() const override { return NTH_VALUE_FUNC; }
2643
2644 bool resolve_type(THD *thd) override;
2645 bool fix_fields(THD *thd, Item **items) override;
2646 void update_after_wf_arguments_changed(THD *thd) override;
2647 bool setup_nth();
2648 void clear() override;
2649
2650 bool check_wf_semantics1(THD *thd, Query_block *select,
2651 Window_evaluation_requirements *reqs) override;
2653
2654 enum Item_result result_type() const override { return m_hybrid_type; }
2655
2656 longlong val_int() override;
2657 double val_real() override;
2658 String *val_str(String *str) override;
2659 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2660
2661 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2662 bool get_time(MYSQL_TIME *ltime) override;
2663 bool val_json(Json_wrapper *wr) override;
2664
2665 void reset_field() override { assert(false); }
2666 void update_field() override { assert(false); }
2667 bool add() override {
2668 assert(false);
2669 return false;
2670 }
2671
2672 void split_sum_func(THD *thd, Ref_item_array ref_item_array,
2673 mem_root_deque<Item *> *fields) override;
2674 bool uses_only_one_row() const override { return true; }
2675
2676 private:
2677 /**
2678 Core logic of NTH_VALUE window functions
2679
2680 @return true if computation yielded a NULL or error
2681 */
2682 bool compute();
2683};
2684
2685/**
2686 Class for implementation of the GROUPING function. The GROUPING
2687 function distinguishes super-aggregate rows from regular grouped
2688 rows. GROUP BY extensions such as ROLLUP and CUBE produce
2689 super-aggregate rows where the set of all values is represented
2690 by null. Using the GROUPING function, you can distinguish a null
2691 representing the set of all values in a super-aggregate row from
2692 a NULL in a regular row.
2693*/
2695 public:
2698 }
2699 const char *func_name() const override { return "grouping"; }
2700 enum Functype functype() const override { return GROUPING_FUNC; }
2701 longlong val_int() override;
2702 bool aggregate_check_group(uchar *arg) override;
2703 bool fix_fields(THD *thd, Item **ref) override;
2704 void update_used_tables() override;
2705 bool aggregate_check_distinct(uchar *arg) override;
2706};
2707
2708/**
2709 A wrapper Item that contains a number of aggregate items, one for each level
2710 of rollup (see Item_rollup_group_item for numbering conventions). When
2711 aggregating, every aggregator is either reset or updated as per the correct
2712 level, and when returning a value, the correct child item corresponding to
2713 the current rollup level is queried.
2714 */
2716 public:
2717 explicit Item_rollup_sum_switcher(List<Item> *sum_func_per_level)
2718 : Item_sum((*sum_func_per_level)[0]),
2719 m_num_levels(sum_func_per_level->size()) {
2720 args = (*THR_MALLOC)->ArrayAlloc<Item *>(sum_func_per_level->size());
2721 int i = 0;
2722 for (Item &item : *sum_func_per_level) {
2723 args[i++] = &item;
2724 }
2728 hidden = master()->hidden;
2732 }
2733 double val_real() override;
2734 longlong val_int() override;
2735 String *val_str(String *str) override;
2737 bool val_json(Json_wrapper *result) override;
2738 bool is_null() override;
2739 bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override;
2740 bool get_time(MYSQL_TIME *ltime) override;
2741 const char *func_name() const override { return "rollup_sum_switcher"; }
2742 table_map used_tables() const override { return master()->used_tables(); }
2743 Item_result result_type() const override { return master()->result_type(); }
2744 bool resolve_type(THD *) override {
2746 return false;
2747 }
2748 void print(const THD *thd, String *str,
2749 enum_query_type query_type) const override;
2750 Field *create_tmp_field(bool group, TABLE *table) override;
2751
2752 enum Sumfunctype sum_func() const override { return master()->sum_func(); }
2753 enum Sumfunctype real_sum_func() const override {
2755 }
2756 void reset_field() override { assert(false); }
2757 void update_field() override { assert(false); }
2758 void clear() override;
2759 bool add() override {
2760 assert(false);
2761 return true;
2762 }
2763
2764 bool reset_and_add_for_rollup(int last_unchanged_group_item_idx);
2765
2766 int set_aggregator(Aggregator::Aggregator_type aggregator) override;
2767 bool aggregator_setup(THD *thd) override;
2768 inline bool aggregator_add_all() {
2769 for (int i = 0; i < m_num_levels; ++i) {
2770 if (child(i)->aggregator_add()) {
2771 return true;
2772 }
2773 }
2774 return false;
2775 }
2776
2777 // Used when create_tmp_table() needs to delay application of aggregate
2778 // functions to a later stage in the query processing.
2779 Item *get_arg(uint i) override { return master()->get_arg(i); }
2780 const Item *get_arg(uint i) const override { return master()->get_arg(i); }
2781 Item *set_arg(THD *thd, uint i, Item *new_val) override {
2782 Item *ret = nullptr;
2783 for (int j = 0; j < m_num_levels; ++j) {
2784 ret = child(j)->set_arg(thd, i, new_val);
2785 }
2786 return ret; // Return the last one, arbitrarily.
2787 }
2788 uint argument_count() const override { return master()->argument_count(); }
2789
2790 // Used by AggregateIterator.
2792 inline Item_sum *master() const { return child(0); }
2793
2794 bool is_rollup_sum_wrapper() const override { return true; }
2795 const Item_sum *unwrap_sum() const override { return master(); }
2796 Item_sum *unwrap_sum() override { return master(); }
2797
2798 private:
2799 inline Item *current_arg() const;
2800 inline Item_sum *child(size_t i) const {
2801 return down_cast<Item_sum *>(args[i]);
2802 }
2803
2804 const int m_num_levels;
2806};
2807/// Implements ST_Collect which aggregates geometries into Multipoints,
2808/// Multilinestrings, Multipolygons and Geometrycollections.
2809
2811 private:
2812 std::optional<gis::srid_t> srid;
2813 std::unique_ptr<gis::Geometrycollection> m_geometrycollection;
2814 void pop_front();
2815
2816 public:
2818 Item_sum_collect(THD *thd, Item_sum *item) : Item_sum(thd, item) {
2820 }
2821 Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
2822 : Item_sum(pos, a, w) {
2823 set_distinct(distinct);
2825 }
2826
2827 my_decimal *val_decimal(my_decimal *decimal_buffer) override;
2828 longlong val_int() override { return val_int_from_string(); }
2829 double val_real() override { return val_real_from_string(); }
2830 bool get_date(MYSQL_TIME *, my_time_flags_t) override { return true; }
2831 bool get_time(MYSQL_TIME *) override { return true; }
2832 enum Sumfunctype sum_func() const override { return GEOMETRY_AGGREGATE_FUNC; }
2833 Item_result result_type() const override { return STRING_RESULT; }
2835 /*
2836 In contrast to Item_sum, Item_sum_collect always uses Aggregator_simple,
2837 and only needs to reset its aggregator when called.
2838 */
2839 if (aggr) {
2840 aggr->clear();
2841 return false;
2842 }
2843
2844 destroy(aggr);
2845 aggr = new (*THR_MALLOC) Aggregator_simple(this);
2846 return aggr ? false : true;
2847 }
2848
2849 bool fix_fields(THD *thd, Item **ref) override;
2850 /*
2851 We need to override check_wf_semantics1 because it reports an error when
2852 with_distinct is set.
2853 */
2854 bool check_wf_semantics1(THD *thd, Query_block *,
2856
2857 Item *copy_or_same(THD *thd) override;
2858
2859 void update_field() override;
2860 void reset_field() override;
2861
2862 String *val_str(String *str) override;
2863
2864 bool add() override;
2865
2866 void read_result_field();
2867 void store_result_field();
2868
2869 void clear() override;
2870 const char *func_name() const override { return "st_collect"; }
2871};
2872
2873#endif /* ITEM_SUM_INCLUDED */
The distinct aggregator.
Definition: item_sum.h:809
uint tree_key_length
Definition: item_sum.h:862
uint32 * field_lengths
Definition: item_sum.h:839
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2151
bool endup_done
Definition: item_sum.h:820
Const_distinct
Definition: item_sum.h:864
@ CONST_NULL
Set to true if the result is known to be always NULL.
Definition: item_sum.h:875
@ CONST_NOT_NULL
Set to true if count distinct is on only const items.
Definition: item_sum.h:882
@ NOT_CONST
Definition: item_sum.h:865
static int composite_key_cmp(const void *arg, const void *a, const void *b)
Correctly compare composite keys.
Definition: item_sum.cc:958
void endup() override
Calculate the aggregate function value.
Definition: item_sum.cc:1330
~Aggregator_distinct() override
Definition: item_sum.cc:2101
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:893
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2146
void clear() override
Invalidate calculated value and clear the distinct rows.
Definition: item_sum.cc:1229
bool setup(THD *) override
Called before feeding the first row.
Definition: item_sum.cc:1029
bool unique_walk_function(void *element)
Aggregate a distinct row from the distinct hash table.
Definition: item_sum.cc:2094
Unique * tree
Definition: item_sum.h:854
Temp_table_param * tmp_table_param
Definition: item_sum.h:845
bool add() override
Process incoming row.
Definition: item_sum.cc:1261
TABLE * table
Definition: item_sum.h:831
Aggregator_type Aggrtype() override
Definition: item_sum.h:904
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2141
enum Aggregator_distinct::Const_distinct const_distinct
Aggregator_distinct(Item_sum *sum)
Definition: item_sum.h:896
The pass-through aggregator.
Definition: item_sum.h:923
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:929
Aggregator_simple(Item_sum *sum)
Definition: item_sum.h:925
bool setup(THD *thd) override
Called before adding the first row.
Definition: item_sum.h:928
Aggregator_type Aggrtype() override
Definition: item_sum.h:926
my_decimal * arg_val_decimal(my_decimal *value) override
Decimal value of being-aggregated argument.
Definition: item_sum.cc:2118
void endup() override
Called when there are no more data and the final value is to be retrieved.
Definition: item_sum.h:931
double arg_val_real() override
Floating point value of being-aggregated argument.
Definition: item_sum.cc:2122
bool add() override
Called when there's a new value to be aggregated.
Definition: item_sum.h:930
bool arg_is_null(bool use_null_value) override
NULLness of being-aggregated argument.
Definition: item_sum.cc:2126
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:195
Definition: field.h:575
Definition: item_sum.h:1173
const char * func_name() const override
Definition: item_sum.h:1183
uint prec_increment
Definition: item_sum.h:1176
uint f_precision
Definition: item_sum.h:1175
uint dec_bin_size
Definition: item_sum.h:1175
uint f_scale
Definition: item_sum.h:1175
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3687
String * val_str(String *) override
Definition: item_sum.cc:3702
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1182
enum Type type() const override
Definition: item_sum.h:1178
double val_real() override
Definition: item_sum.cc:3672
Item_avg_field(Item_result res_type, Item_sum_avg *item)
Definition: item_sum.cc:3653
Definition: item.h:6649
CUME_DIST window function, cf.
Definition: item_sum.h:2354
Item_cume_dist(const POS &pos, PT_window *w)
Definition: item_sum.h:2358
Item_non_framing_wf super
Definition: item_sum.h:2355
double val_real() override
Definition: item_sum.cc:4880
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2361
void clear() override
Definition: item_sum.h:2372
const char * func_name() const override
Definition: item_sum.h:2360
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:2371
String * val_str(String *) override
Definition: item_sum.cc:4900
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:4870
longlong val_int() override
Definition: item_sum.cc:4892
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2363
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:4904
Item_result result_type() const override
Definition: item_sum.h:2377
Definition: item.h:4163
FIRST_VALUE/LAST_VALUE window functions, cf.
Definition: item_sum.h:2553
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2575
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5108
void reset_field() override
Definition: item_sum.h:2594
const char * func_name() const override
Definition: item_sum.h:2571
bool setup_first_last()
Definition: item_sum.cc:5145
double val_real() override
Definition: item_sum.cc:5194
String * val_str(String *str) override
Definition: item_sum.cc:5249
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:5162
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:5094
bool compute()
Core logic of FIRST/LAST_VALUE window functions.
Definition: item_sum.cc:5166
int64 cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2558
Item_result m_hybrid_type
Definition: item_sum.h:2556
enum Item_result result_type() const override
Definition: item_sum.h:2583
Item_cache * m_value
Definition: item_sum.h:2557
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5225
void update_field() override
Definition: item_sum.h:2595
enum_null_treatment m_null_treatment
Definition: item_sum.h:2555
Item_sum super
Definition: item_sum.h:2559
void clear() override
Definition: item_sum.cc:5156
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2603
void 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:5137
bool add() override
Definition: item_sum.h:2596
Item_first_last_value(const POS &pos, bool first, Item *a, enum_null_treatment null_treatment, PT_window *w)
Definition: item_sum.h:2562
bool m_is_first
if true, the function is FIRST_VALUE, else LAST_VALUE
Definition: item_sum.h:2554
longlong val_int() override
Definition: item_sum.cc:5184
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5235
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5215
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5118
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5204
Definition: item_sum.h:2100
void clear() override
Definition: item_sum.cc:4348
bool m_result_finalized
True if result has been written to output buffer.
Definition: item_sum.h:2139
String result
Definition: item_sum.h:2115
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:2144
longlong val_int() override
Definition: item_sum.h:2180
const String * get_separator_str() const noexcept
Definition: item_sum.h:2198
uint32_t get_group_concat_max_len() const noexcept
Definition: item_sum.h:2199
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.h:2187
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:4095
uint row_count
Definition: item_sum.h:2130
void make_unique() override
Definition: item_sum.cc:4634
bool change_context_processor(uchar *arg) override
Definition: item_sum.h:2211
bool setup(THD *thd) override
Definition: item_sum.cc:4503
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize() but with additional parameter.
Definition: item_sum.cc:4223
Item_sum super
Definition: item_sum.h:2101
bool distinct
True if GROUP CONCAT has the DISTINCT attribute.
Definition: item_sum.h:2104
TABLE * table
Temporary table used to perform group concat.
Definition: item_sum.h:2128
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:4017
double val_real() override
Definition: item_sum.cc:4642
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2168
String * val_str(String *str) override
Definition: item_sum.cc:4649
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:4052
bool force_copy_fields
Definition: item_sum.h:2137
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:4411
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2190
Unique * unique_filter
If DISTINCT is used with this GROUP_CONCAT, this member is used to filter out duplicates.
Definition: item_sum.h:2126
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2216
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:4346
Mem_root_array< ORDER > order_array
Definition: item_sum.h:2129
TREE tree_base
Definition: item_sum.h:2116
void update_field() override
Definition: item_sum.h:2175
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:4674
Temp_table_param * tmp_table_param
Describes the temporary table used to perform group concat.
Definition: item_sum.h:2114
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:4275
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:4338
const char * func_name() const override
Definition: item_sum.h:2169
Item_result result_type() const override
Definition: item_sum.h:2170
Field * make_string_field(TABLE *table_arg) const override
Create a field to hold a string value from an item.
Definition: item_sum.cc:4305
uint m_order_arg_count
The number of ORDER BY items.
Definition: item_sum.h:2106
TREE * tree
Definition: item_sum.h:2117
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:4185
bool has_distinct() const noexcept
Definition: item_sum.h:2197
bool add() override
Definition: item_sum.cc:4360
Name_resolution_context * context
Resolver context, points to containing query block.
Definition: item_sum.h:2110
void reset_field() override
Definition: item_sum.h:2174
uint m_field_arg_count
The number of selected items, aka the concat field list.
Definition: item_sum.h:2108
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:2135
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2193
bool warning_for_row
Definition: item_sum.h:2136
String * separator
String containing separator between group items.
Definition: item_sum.h:2112
~Item_func_group_concat() override
Definition: item_sum.h:2161
const Mem_root_array< ORDER > & get_order_array() const noexcept
Definition: item_sum.h:2202
Class for implementation of the GROUPING function.
Definition: item_sum.h:2694
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:6278
longlong val_int() override
Evaluation of the GROUPING function.
Definition: item_sum.cc:6227
Item_func_grouping(const POS &pos, PT_item_list *a)
Definition: item_sum.h:2696
const char * func_name() const override
Definition: item_sum.h:2699
bool fix_fields(THD *thd, Item **ref) override
Resolve the fields in the GROUPING function.
Definition: item_sum.cc:6172
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:6248
void update_used_tables() override
Updates used tables, not null tables information and accumulates properties up the item tree,...
Definition: item_sum.cc:6290
enum Functype functype() const override
Definition: item_sum.h:2700
Definition: item_func.h:102
Item ** args
Array of pointers to arguments.
Definition: item_func.h:109
virtual Item * get_arg(uint i)
Get the i'th argument of the function that this object represents.
Definition: item_func.h:445
Functype
Definition: item_func.h:185
@ GROUPING_FUNC
Definition: item_func.h:238
virtual uint argument_count() const
Definition: item_func.h:133
table_map used_tables_cache
Value used in calculation of result of used_tables()
Definition: item_func.h:171
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:519
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:495
Item ** arguments() const
Definition: item_func.h:134
virtual void fix_num_length_and_dec()
Definition: item_func.cc:837
const char * original_db_name() const
Definition: item.h:4052
void set_original_field_name(const char *name_arg)
Definition: item.h:4049
const char * original_table_name() const
Definition: item.h:4053
Definition: item_func.h:935
Definition: item.h:4916
LEAD/LAG window functions, cf.
Definition: item_sum.h:2461
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5520
bool has_value() const
Definition: item_sum.h:2534
bool m_is_lead
if true, the function is LEAD, else LAG
Definition: item_sum.h:2463
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:2518
const char * func_name() const override
Definition: item_sum.h:2495
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5641
void set_use_default(bool value)
Definition: item_sum.h:2536
String * val_str(String *str) override
Definition: item_sum.cc:5633
bool m_use_default
execution state: use default value for current row
Definition: item_sum.h:2478
enum Item_result result_type() const override
Definition: item_sum.h:2507
Item_cache * m_default
Definition: item_sum.h:2467
bool compute()
Core logic of LEAD/LAG window functions.
Definition: item_sum.cc:5668
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:2482
bool check_wf_semantics1(THD *thd, Query_block *select, Window_evaluation_requirements *reqs) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:5580
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2498
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5650
enum_null_treatment m_null_treatment
Definition: item_sum.h:2462
void clear() override
Definition: item_sum.cc:5592
bool use_default() const
Definition: item_sum.h:2537
bool m_has_value
Execution state: if set, we already have a value for current row.
Definition: item_sum.h:2477
longlong val_int() override
Definition: item_sum.cc:5604
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5620
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5529
Item_result m_hybrid_type
Definition: item_sum.h:2465
int64 m_n
canonicalized offset value
Definition: item_sum.h:2464
double val_real() override
Definition: item_sum.cc:5612
void set_has_value(bool value)
Definition: item_sum.h:2533
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5467
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5658
bool setup_lead_lag()
Definition: item_sum.cc:5564
Item_cache * m_value
Definition: item_sum.h:2466
void 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:5557
Item_non_framing_wf super
Definition: item_sum.h:2479
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:5599
Common parent class for window functions that always work on the entire partition,...
Definition: item_sum.h:2232
Item_non_framing_wf(const POS &pos, PT_window *w)
Definition: item_sum.h:2236
bool add() override
Definition: item_sum.h:2253
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2247
Item_non_framing_wf(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2237
Item_non_framing_wf(const POS &pos, PT_item_list *opt_list, PT_window *w)
Definition: item_sum.h:2239
Item_sum super
Definition: item_sum.h:2233
Item_non_framing_wf(THD *thd, Item_non_framing_wf *i)
Definition: item_sum.h:2241
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2243
void update_field() override
Definition: item_sum.h:2252
void reset_field() override
Definition: item_sum.h:2251
bool framing() const override
All aggregates are framing, i.e.
Definition: item_sum.h:2260
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:4710
NTH_VALUE window function, cf.
Definition: item_sum.h:2619
int64 m_cnt
used when evaluating on-the-fly (non-buffered processing)
Definition: item_sum.h:2626
void update_field() override
Definition: item_sum.h:2666
bool compute()
Core logic of NTH_VALUE window functions.
Definition: item_sum.cc:5360
String * val_str(String *str) override
Definition: item_sum.cc:5427
Item_sum super
Definition: item_sum.h:2628
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:4984
enum Item_result result_type() const override
Definition: item_sum.h:2654
Item_result m_hybrid_type
Definition: item_sum.h:2623
void 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:5312
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5413
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5447
bool setup_nth()
Definition: item_sum.cc:5319
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5272
enum_field_types m_hybrid_field_type
Definition: item_sum.h:2624
int64 m_n
The N of the function.
Definition: item_sum.h:2621
double val_real() override
Definition: item_sum.cc:5403
bool m_from_last
true iff FROM_LAST was specified
Definition: item_sum.h:2622
Item_cache * m_value
Definition: item_sum.h:2625
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5457
enum_null_treatment m_null_treatment
Definition: item_sum.h:2620
void reset_field() override
Definition: item_sum.h:2665
longlong val_int() override
Definition: item_sum.cc:5393
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:2631
void clear() override
Definition: item_sum.cc:5330
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:5259
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2642
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:5336
bool add() override
Definition: item_sum.h:2667
const char * func_name() const override
Definition: item_sum.h:2641
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5437
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:5340
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:2674
NTILE window function, cf.
Definition: item_sum.h:2424
void clear() override
Definition: item_sum.h:2454
Item_non_framing_wf super
Definition: item_sum.h:2425
String * val_str(String *) override
Definition: item_sum.cc:5063
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2437
const char * func_name() const override
Definition: item_sum.h:2434
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:2455
bool check_wf_semantics2(Window_evaluation_requirements *reqs) override
Like check_wf_semantics1.
Definition: item_sum.cc:5078
bool fix_fields(THD *thd, Item **items) override
Definition: item_sum.cc:5002
double val_real() override
Definition: item_sum.cc:5058
longlong val_int() override
Definition: item_sum.cc:5008
longlong m_value
Definition: item_sum.h:2426
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:5065
Item_ntile(const POS &pos, Item *a, PT_window *w)
Definition: item_sum.h:2429
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2435
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:5070
Item_result result_type() const override
Definition: item_sum.h:2453
PERCENT_RANK window function, cf.
Definition: item_sum.h:2383
String * val_str(String *) override
Definition: item_sum.cc:4967
Item_percent_rank(const POS &pos, PT_window *w)
Definition: item_sum.h:2394
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2386
const char * func_name() const override
Definition: item_sum.h:2401
Item_result result_type() const override
Definition: item_sum.h:2418
~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:4909
ulonglong m_peers
Needed to make PERCENT_RANK same for peers.
Definition: item_sum.h:2387
double val_real() override
Definition: item_sum.cc:4933
Item_non_framing_wf super
Definition: item_sum.h:2384
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2404
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2402
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:2411
bool m_last_peer_visited
Set when the last peer has been visited.
Definition: item_sum.h:2391
void clear() override
Definition: item_sum.cc:4976
my_decimal * val_decimal(my_decimal *buffer) override
Definition: item_sum.cc:4971
longlong val_int() override
Definition: item_sum.cc:4959
RANK or DENSE_RANK window function, cf.
Definition: item_sum.h:2303
Item_result result_type() const override
Definition: item_sum.h:2348
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:4765
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:4789
ulonglong m_duplicates
Needed to make RANK different from DENSE_RANK.
Definition: item_sum.h:2308
bool m_dense
If true, the object represents DENSE_RANK.
Definition: item_sum.h:2305
longlong val_int() override
Definition: item_sum.cc:4807
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2327
~Item_rank() override
Definition: item_sum.cc:4863
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2331
Item_rank(const POS &pos, bool dense, PT_window *w)
Definition: item_sum.h:2312
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4842
const char * func_name() const override
Definition: item_sum.h:2323
double val_real() override
Definition: item_sum.cc:4835
String * val_str(String *) override
Definition: item_sum.cc:4840
ulonglong m_rank_ctr
Increment when window order columns change.
Definition: item_sum.h:2307
void clear() override
Clear state for a new partition.
Definition: item_sum.cc:4847
Mem_root_array< Cached_item * > m_previous
Values of previous row's ORDER BY items.
Definition: item_sum.h:2310
Item_non_framing_wf super
Definition: item_sum.h:2304
Item with result field.
Definition: item.h:5626
longlong llrint_with_overflow_check(double realval)
Definition: item.h:5677
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item.cc:10681
virtual const char * func_name() const =0
A wrapper Item that contains a number of aggregate items, one for each level of rollup (see Item_roll...
Definition: item_sum.h:2715
table_map used_tables() const override
Definition: item_sum.h:2742
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:6367
void update_field() override
Definition: item_sum.h:2757
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2744
Item * get_arg(uint i) override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2779
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.h:2781
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.cc:6353
void set_current_rollup_level(int level)
Definition: item_sum.h:2791
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:6307
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:6358
bool aggregator_setup(THD *thd) override
Called to initialize the aggregator.
Definition: item_sum.cc:6400
Item_result result_type() const override
Definition: item_sum.h:2743
String * val_str(String *str) override
Definition: item_sum.cc:6332
int set_aggregator(Aggregator::Aggregator_type aggregator) override
Definition: item_sum.cc:6389
Item_sum * unwrap_sum() override
Non-const version.
Definition: item_sum.h:2796
longlong val_int() override
Definition: item_sum.cc:6325
void reset_field() override
Definition: item_sum.h:2756
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:2795
Item_rollup_sum_switcher(List< Item > *sum_func_per_level)
Definition: item_sum.h:2717
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:6313
bool aggregator_add_all()
Definition: item_sum.h:2768
Item * current_arg() const
Definition: item_sum.cc:6302
Item_sum * master() const
Definition: item_sum.h:2792
Item_sum * child(size_t i) const
Definition: item_sum.h:2800
enum Sumfunctype real_sum_func() const override
Definition: item_sum.h:2753
bool is_rollup_sum_wrapper() const override
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:2794
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2752
const char * func_name() const override
Definition: item_sum.h:2741
int m_current_rollup_level
Definition: item_sum.h:2805
double val_real() override
Definition: item_sum.cc:6318
bool val_json(Json_wrapper *result) override
Get a JSON value from an Item.
Definition: item_sum.cc:6346
uint argument_count() const override
Definition: item_sum.h:2788
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:6339
bool add() override
Definition: item_sum.h:2759
void clear() override
Definition: item_sum.cc:6371
bool reset_and_add_for_rollup(int last_unchanged_group_item_idx)
Definition: item_sum.cc:6377
const int m_num_levels
Definition: item_sum.h:2804
const Item * get_arg(uint i) const override
Get the i'th argument of the function that this object represents.
Definition: item_sum.h:2780
ROW_NUMBER window function, cf.
Definition: item_sum.h:2266
ulonglong m_ctr
Increment for each row in partition.
Definition: item_sum.h:2268
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2279
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2277
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.h:2293
Item_row_number(const POS &pos, PT_window *w)
Definition: item_sum.h:2271
my_decimal * val_decimal(my_decimal *buff) override
Definition: item_sum.cc:4758
Item_result result_type() const override
Definition: item_sum.h:2291
String * val_str(String *) override
Definition: item_sum.cc:4754
longlong val_int() override
Definition: item_sum.cc:4735
void clear() override
Definition: item_sum.cc:4763
const char * func_name() const override
Definition: item_sum.h:2276
double val_real() override
Definition: item_sum.cc:4749
Definition: item_sum.h:1491
enum Type type() const override
Definition: item_sum.h:1494
Item_std_field(Item_sum_std *item)
Definition: item_sum.cc:3800
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:1502
enum Item_result result_type() const override
Definition: item_sum.h:1497
double val_real() override
Definition: item_sum.cc:3803
const char * func_name() const override
Definition: item_sum.h:1498
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3811
Definition: item_sum.h:1909
Item_sum_and(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1911
const char * func_name() const override
Definition: item_sum.h:1915
Item_sum_and(THD *thd, Item_sum_and *item)
Definition: item_sum.h:1914
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3268
Definition: item_sum.h:1312
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2219
void clear() override
Definition: item_sum.cc:2274
String * val_str(String *str) override
Definition: item_sum.cc:2389
const char * func_name() const override
Definition: item_sum.h:1342
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3497
Item_sum_sum super
Definition: item_sum.h:1316
Item * result_item(Field *) override
Definition: item_sum.h:1339
double val_real() override
Definition: item_sum.cc:2283
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1345
Item_sum_avg(const POS &pos, Item *item_par, bool distinct, PT_window *w)
Definition: item_sum.h:1320
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:2252
my_decimal m_avg_dec
Definition: item_sum.h:1317
uint f_scale
Definition: item_sum.h:1315
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1327
double m_avg
Definition: item_sum.h:1318
longlong val_int() override
Definition: item_sum.h:1334
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2245
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2306
void reset_field() override
Definition: item_sum.cc:3397
uint prec_increment
Definition: item_sum.h:1314
uint dec_bin_size
Definition: item_sum.h:1315
bool add() override
Definition: item_sum.cc:2276
Item_sum_avg(THD *thd, Item_sum_avg *item)
Definition: item_sum.h:1323
uint f_precision
Definition: item_sum.h:1315
This is used in connection with an Item_sum_bit,.
Definition: item_sum.h:1190
double val_real() override
Definition: item_sum.cc:3743
enum Type type() const override
Definition: item_sum.h:1204
ulonglong reset_bits
Definition: item_sum.h:1192
Item_sum_bit_field(Item_result res_type, Item_sum_bit *item, ulonglong reset_bits)
Definition: item_sum.cc:3708
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1201
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3760
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3786
longlong val_int() override
Definition: item_sum.cc:3728
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3793
String * val_str(String *) override
Definition: item_sum.cc:3768
const char * func_name() const override
Definition: item_sum.h:1205
Base class used to implement BIT_AND, BIT_OR and BIT_XOR.
Definition: item_sum.h:1740
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3157
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:1592
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1849
void remove_bits(const String *s1, ulonglong b1)
For windowing: perform inverse aggregation.
Definition: item_sum.cc:1514
const char initial_value_buff_storage[1]
Buffer used to avoid String allocation in the constructor.
Definition: item_sum.h:1751
static constexpr uint DIGIT_CNT_CARD
Definition: item_sum.h:1794
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1836
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1458
bool m_is_xor
true iff BIT_XOR
Definition: item_sum.h:1797
void clear() override
Definition: item_sum.cc:3240
Item * result_item(Field *) override
Definition: item_sum.h:1832
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:1433
longlong val_int() override
Definition: item_sum.cc:3215
Item_sum super
Definition: item_sum.h:1741
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:1786
Item_result hybrid_type
Stores the Item's result type. Can only be INT_RESULT or STRING_RESULT.
Definition: item_sum.h:1749
ulonglong reset_bits
Stores the neutral element for function.
Definition: item_sum.h:1743
Item_sum_bit(const POS &pos, Item *item_par, ulonglong reset_arg, PT_window *w)
Definition: item_sum.h:1800
bool is_and() const
Definition: item_sum.h:1862
void reset_field() override
Definition: item_sum.cc:3425
ulonglong m_count
Execution state (windowing): this is for counting rows entering and leaving the window frame,...
Definition: item_sum.h:1757
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3164
double val_real() override
Definition: item_sum.cc:3189
bool add() override
Common implementation of Item_sum_or::add, Item_sum_and:add and Item_sum_xor::add.
Definition: item_sum.cc:1676
my_decimal * val_decimal(my_decimal *decimal_value) override
Definition: item_sum.cc:3171
enum Item_result result_type() const override
Definition: item_sum.h:1837
uint m_digit_cnt_card
Definition: item_sum.h:1792
Item_sum_bit(THD *thd, Item_sum_bit *item)
Copy constructor, used for executing subqueries with temporary tables.
Definition: item_sum.h:1813
String * val_str(String *str) override
Definition: item_sum.cc:3126
void update_field() override
Definition: item_sum.cc:3435
ulonglong bits
Stores the result value for the INT_RESULT.
Definition: item_sum.h:1745
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:1765
String value_buff
Stores the result value for the STRING_RESULT.
Definition: item_sum.h:1747
Implements ST_Collect which aggregates geometries into Multipoints, Multilinestrings,...
Definition: item_sum.h:2810
bool add() override
Definition: item_sum.cc:6475
void clear() override
Definition: item_sum.cc:6469
void read_result_field()
Definition: item_sum.cc:6527
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:6438
bool get_date(MYSQL_TIME *, my_time_flags_t) override
Definition: item_sum.h:2830
std::optional< gis::srid_t > srid
Definition: item_sum.h:2812
Item_sum_collect(const POS &pos, Item *a, PT_window *w, bool distinct)
Definition: item_sum.h:2821
void update_field() override
Definition: item_sum.cc:6617
longlong val_int() override
Definition: item_sum.h:2828
bool get_time(MYSQL_TIME *) override
Definition: item_sum.h:2831
std::unique_ptr< gis::Geometrycollection > m_geometrycollection
Definition: item_sum.h:2813
String * val_str(String *str) override
Definition: item_sum.cc:6570
void reset_field() override
Definition: item_sum.cc:6680
Item_sum_collect(THD *thd, Item_sum *item)
Definition: item_sum.h:2818
void store_result_field()
Definition: item_sum.cc:6623
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:6675
bool check_wf_semantics1(THD *thd, Query_block *, Window_evaluation_requirements *r) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:6460
const char * func_name() const override
Definition: item_sum.h:2870
void pop_front()
Definition: item_sum.cc:6563
enum Sumfunctype sum_func() const override
Definition: item_sum.h:2832
Item_result result_type() const override
Definition: item_sum.h:2833
int set_aggregator(Aggregator::Aggregator_type) override
Definition: item_sum.h:2834
double val_real() override
Definition: item_sum.h:2829
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6522
Definition: item_sum.h:1062
longlong count
Definition: item_sum.h:1063
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1094
longlong val_int() override
Definition: item_sum.cc:2181
void update_field() override
Definition: item_sum.cc:3488
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2162
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1091
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:2213
void make_const(longlong count_arg)
Definition: item_sum.h:1101
void reset_field() override
Definition: item_sum.cc:3389
Item_sum_count(Item_int *number)
Definition: item_sum.h:1074
Item_sum_count(THD *thd, Item_sum_count *item)
Definition: item_sum.h:1089
Item_sum_count(const POS &pos, PT_item_list *list, PT_window *w)
Constructs an instance for COUNT(DISTINCT)
Definition: item_sum.h:1085
void clear() override
Definition: item_sum.cc:2170
const char * func_name() const override
Definition: item_sum.h:1108
Item_sum_count(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1072
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1100
bool add() override
Definition: item_sum.cc:2172
This is used in connection which a parent Item_sum:
Definition: item_sum.h:1125
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1147
Item_result hybrid_type
Stores the Item's result type.
Definition: item_sum.h:1130
enum Item_result result_type() const override
Definition: item_sum.h:1133
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:1141
Field * field
The tmp table's column containing the value of the set function.
Definition: item_sum.h:1128
bool mark_field_in_map(uchar *arg) override
Mark underlying field in read or write map of a table.
Definition: item_sum.h:1134
Abstract base class for the MIN and MAX aggregate functions.
Definition: item_sum.h:1536
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3079
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:3074
bool setup_hybrid(Item *item, Item *value_arg)
MIN/MAX function setup.
Definition: item_sum.cc:1768
bool uses_only_one_row() const override
Only for framing window functions.
Definition: item_sum.h:1687
virtual Item_sum_hybrid * clone_hybrid(THD *thd) const =0
Create a clone of this object.
bool add() override
Definition: item_sum.cc:3109
const bool m_is_min
Tells if this is the MIN function (true) or the MAX function (false).
Definition: item_sum.h:1543
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:3012
bool compute()
This function implements the optimized version of retrieving min/max value.
Definition: item_sum.cc:2814
void min_max_update_int_field()
Definition: item_sum.cc:3619
longlong val_int() override
Definition: item_sum.cc:2947
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:3002
void reset_field() override
Definition: item_sum.cc:3292
void clear() override
Definition: item_sum.cc:2765
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:1583
Item_cache * arg_cache
Definition: item_sum.h:1552
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3060
void min_max_update_json_field()
Definition: item_sum.cc:3572
void update_field() override
Definition: item_sum.cc:3533
longlong val_date_temporal() override
Return date value of item in packed longlong format.
Definition: item_sum.cc:2973
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:2775
bool keep_field_type() const override
Definition: item_sum.h:1676
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2985
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:1577
void min_max_update_real_field()
Definition: item_sum.cc:3606
Item_sum_hybrid(THD *thd, const Item_sum_hybrid *item)
Definition: item_sum.h:1647
bool any_value()
Definition: item_sum.h:1684
Item_result hybrid_type
Definition: item_sum.h:1554
bool was_values
Definition: item_sum.h:1555
longlong val_time_temporal() override
Return time value of item in packed longlong format.
Definition: item_sum.cc:2961
bool m_optimize
Set to true when min/max can be optimized using window's ordering.
Definition: item_sum.h:1563
Item_sum_hybrid(const POS &pos, Item *item_par, bool is_min, PT_window *w)
Definition: item_sum.h:1631
Item_sum_hybrid(Item *item_par, bool is_min)
Definition: item_sum.h:1615
Item_sum super
Definition: item_sum.h:1537
void min_max_update_temporal_field()
Definition: item_sum.cc:3554
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:2780
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1737
String * val_str(String *) override
Definition: item_sum.cc:3022
Arg_comparator * cmp
Definition: item_sum.h:1553
void min_max_update_str_field()
Definition: item_sum.cc:3590
enum Item_result result_type() const override
Definition: item_sum.h:1677
void min_max_update_decimal_field()
Definition: item_sum.cc:3637
double val_real() override
Definition: item_sum.cc:2933
Field * create_tmp_field(bool group, TABLE *table) override
Definition: item_sum.cc:1785
bool m_want_first
For min() - Set to true when results are ordered in ascending and false when descending.
Definition: item_sum.h:1571
Item_cache * value
Definition: item_sum.h:1552
bool m_nulls_first
Set to true if the window is ordered ascending.
Definition: item_sum.h:1559
void 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:3049
TYPELIB * get_typelib() const override
Get the typelib information for an item of type set or enum.
Definition: item_sum.h:1678
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:3035
Definition: item_sum.h:980
Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:982
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1006
Item_sum_int(THD *thd, Item_sum_int *item)
Definition: item_sum.h:992
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1009
double val_real() override
Definition: item_sum.h:1000
Item_sum_int(Item *item_par)
Definition: item_sum.h:996
enum Item_result result_type() const override
Definition: item_sum.h:1010
String * val_str(String *str) override
Definition: item_sum.cc:1400
Item_sum_int(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:987
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1402
Implements aggregation of values into an array.
Definition: item_sum.h:1256
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:5908
~Item_sum_json_array() override
bool add() override
Definition: item_sum.cc:5981
unique_ptr_destroy_only< Json_array > m_json_array
Accumulates the final value.
Definition: item_sum.h:1258
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6030
const char * func_name() const override
Definition: item_sum.h:1268
void clear() override
Definition: item_sum.cc:5923
Implements aggregation of values into an object.
Definition: item_sum.h:1275
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:5932
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:6138
bool add() override
Definition: item_sum.cc:6043
unique_ptr_destroy_only< Json_object > m_json_object
Accumulates the final value.
Definition: item_sum.h:1277
~Item_sum_json_object() override
const char * func_name() const override
Definition: item_sum.h:1304
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:5957
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:1286
void clear() override
Definition: item_sum.cc:5947
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:1294
String m_tmp_key_value
Buffer used to get the value of the key.
Definition: item_sum.h:1279
Common abstraction for Item_sum_json_array and Item_sum_json_object.
Definition: item_sum.h:1212
void reset_field() override
Definition: item_sum.cc:5868
void update_field() override
Definition: item_sum.cc:5888
bool val_json(Json_wrapper *wr) override
Get a JSON value from an Item.
Definition: item_sum.cc:5786
unique_ptr_destroy_only< Json_wrapper > m_wrapper
Wrapper around the container (object/array) which accumulates the value.
Definition: item_sum.h:1221
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1237
longlong val_int() override
Definition: item_sum.cc:5824
String m_value
String used when reading JSON binary values or JSON text values.
Definition: item_sum.h:1217
String m_conversion_buffer
String used for converting JSON text values to utf8mb4 charset.
Definition: item_sum.h:1219
Item_sum super
Definition: item_sum.h:1213
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.cc:5862
~Item_sum_json() override
String * val_str(String *str) override
Definition: item_sum.cc:5766
my_decimal * val_decimal(my_decimal *decimal_buffer) override
Definition: item_sum.cc:5839
Item_result result_type() const override
Definition: item_sum.h:1238
bool fix_fields(THD *thd, Item **pItem) override
Definition: item_sum.cc:5740
bool check_wf_semantics1(THD *, Query_block *, Window_evaluation_requirements *) override
Only relevant for aggregates qua window functions.
Definition: item_sum.cc:5735
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.cc:5856
Item_sum_json(unique_ptr_destroy_only< Json_wrapper > wrapper, Args &&... parent_args)
Construct an Item_sum_json instance.
Definition: item_sum.cc:5726
double val_real() override
Definition: item_sum.cc:5809
Definition: item_sum.h:1721
Item_sum_max(Item *item_par)
Definition: item_sum.h:1723
Item_sum_max(THD *thd, const Item_sum_max *item)
Definition: item_sum.h:1726
const char * func_name() const override
Definition: item_sum.h:1729
Item_sum_max * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3090
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1728
Item_sum_max(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1724
Definition: item_sum.h:1707
Item_sum_min(THD *thd, const Item_sum_min *item)
Definition: item_sum.h:1712
Item_sum_min(Item *item_par)
Definition: item_sum.h:1709
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1714
const char * func_name() const override
Definition: item_sum.h:1715
Item_sum_min(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1710
Item_sum_min * clone_hybrid(THD *thd) const override
Create a clone of this object.
Definition: item_sum.cc:3086
Common abstract class for: Item_avg_field Item_variance_field.
Definition: item_sum.h:1158
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1164
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.h:1170
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1167
longlong val_int() override
Definition: item_sum.h:1160
Definition: item_sum.h:937
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:1396
Item_sum_num(THD *thd, Item_sum_num *item)
Definition: item_sum.h:956
bool fix_fields(THD *, Item **) override
Definition: item_sum.cc:1406
longlong val_int() override
Definition: item_sum.h:965
enum_field_types default_data_type() const override
Get the default data (output) type for the specific item.
Definition: item_sum.h:958
String * val_str(String *str) override
Definition: item_sum.cc:1394
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:971
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:974
Item_sum super
Definition: item_sum.h:938
Item_sum_num(Item *item_par)
Definition: item_sum.h:962
bool is_evaluated
Definition: item_sum.h:947
Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
Definition: item_sum.h:950
void reset_field() override
Definition: item_sum.cc:3279
Item_sum_num(const POS &pos, PT_item_list *list, PT_window *w)
Definition: item_sum.h:953
Definition: item_sum.h:1899
Item_sum_or(THD *thd, Item_sum_or *item)
Definition: item_sum.h:1904
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3254
const char * func_name() const override
Definition: item_sum.h:1905
Item_sum_or(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1901
Definition: item_sum.h:1514
double val_real() override
Definition: item_sum.cc:2399
Item_sum_std(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1516
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2408
Item_sum_std(THD *thd, Item_sum_std *item)
Definition: item_sum.h:1519
const char * func_name() const override
Definition: item_sum.h:1523
enum Item_result result_type() const override
Definition: item_sum.h:1527
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1520
Item * result_item(Field *) override
Definition: item_sum.h:1522
Definition: item_sum.h:1013
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:1024
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:1917
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:1849
enum Item_result result_type() const override
Definition: item_sum.h:1052
double sum
Definition: item_sum.h:1016
bool add() override
Definition: item_sum.cc:1939
Item_result hybrid_type
Definition: item_sum.h:1015
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.cc:1868
void reset_field() override
Definition: item_sum.cc:3371
String * val_str(String *str) override
Definition: item_sum.cc:2028
longlong val_int() override
Definition: item_sum.cc:1960
my_decimal dec_buffs[2]
Definition: item_sum.h:1017
void clear() override
Definition: item_sum.cc:1856
const char * func_name() const override
Definition: item_sum.h:1058
Item_sum_sum(const POS &pos, Item *item_par, bool distinct, PT_window *window)
Definition: item_sum.h:1034
void update_field() override
calc next value and merge it with field_value.
Definition: item_sum.cc:3459
uint curr_dec_buff
Definition: item_sum.h:1018
ulonglong m_frame_null_count
Execution state: this is for counting NULLs of rows entering and leaving the window frame,...
Definition: item_sum.h:1031
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2034
bool resolve_type(THD *thd) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:1870
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1043
double val_real() override
Definition: item_sum.cc:1984
Definition: item_sum.h:2067
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2078
longlong val_int() override
Definition: item_sum.cc:3928
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2081
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3930
Item_sum_udf_decimal(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2069
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3939
double val_real() override
Definition: item_sum.cc:3926
enum Item_result result_type() const override
Definition: item_sum.h:2084
String * val_str(String *) override
Definition: item_sum.cc:3922
Item_sum_udf_decimal(THD *thd, Item_sum_udf_decimal *item)
Definition: item_sum.h:2072
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2085
Definition: item_sum.h:1977
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1996
longlong val_int() override
Definition: item_sum.h:1983
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3918
String * val_str(String *str) override
Definition: item_sum.cc:3914
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3901
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:1990
double val_real() override
Definition: item_sum.cc:3906
Item_sum_udf_float(THD *thd, Item_sum_udf_float *item)
Definition: item_sum.h:1981
Item_sum_udf_float(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1979
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:1993
Definition: item_sum.h:2004
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2017
Item_sum_udf_int(THD *thd, Item_sum_udf_int *item)
Definition: item_sum.h:2008
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:2022
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2020
enum Item_result result_type() const override
Definition: item_sum.h:2021
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3943
String * val_str(String *str) override
Definition: item_sum.cc:3955
Item_sum_udf_int(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2006
longlong val_int() override
Definition: item_sum.cc:3947
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:3959
double val_real() override
Definition: item_sum.h:2011
Definition: item_sum.h:2029
bool resolve_type(THD *) override
Default max_length is max argument length.
Definition: item_sum.cc:3965
double val_real() override
Definition: item_sum.h:2036
String * val_str(String *) override
Definition: item_sum.cc:3981
enum Item_result result_type() const override
Definition: item_sum.h:2062
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override
Definition: item_sum.h:2056
bool get_time(MYSQL_TIME *ltime) override
Definition: item_sum.h:2059
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3973
my_decimal * val_decimal(my_decimal *dec) override
Definition: item_sum.cc:3977
Item_sum_udf_str(THD *thd, Item_sum_udf_str *item)
Definition: item_sum.h:2033
longlong val_int() override
Definition: item_sum.h:2045
Item_sum_udf_str(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:2031
Definition: item_sum.h:1436
ulonglong count
Definition: item_sum.h:1447
void update_field() override
Definition: item_sum.cc:2739
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:2607
double val_real() override
Definition: item_sum.cc:2684
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.h:1481
void clear() override
Definition: item_sum.cc:2661
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:2627
Item * result_item(Field *) override
Definition: item_sum.h:1473
Item_sum_variance(const POS &pos, Item *item_par, uint sample_arg, PT_window *w)
Definition: item_sum.h:1457
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1466
double recurrence_s
Definition: item_sum.h:1445
bool optimize
If set, uses a algorithm II mentioned in the class description to calculate the variance which helps ...
Definition: item_sum.h:1455
const char * func_name() const override
Definition: item_sum.h:1475
bool add() override
Definition: item_sum.cc:2663
void no_rows_in_result() override
Mark an aggregate as having no rows.
Definition: item_sum.h:1474
double recurrence_s2
Definition: item_sum.h:1446
Item_result hybrid_type
Definition: item_sum.h:1440
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:2591
double recurrence_m
Used in recurrence relation.
Definition: item_sum.h:1444
my_decimal * val_decimal(my_decimal *) override
Definition: item_sum.cc:2714
uint prec_increment
Definition: item_sum.h:1449
enum Item_result result_type() const override
Definition: item_sum.h:1480
void reset_field() override
Definition: item_sum.cc:2719
uint sample
Definition: item_sum.h:1448
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:2640
Definition: item_sum.h:1919
const char * func_name() const override
Definition: item_sum.h:1927
Item * copy_or_same(THD *thd) override
Definition: item_sum.cc:3261
Item_sum_xor(const POS &pos, Item *item_par, PT_window *w)
Definition: item_sum.h:1921
Item_sum_xor(THD *thd, Item_sum_xor *item)
Definition: item_sum.h:1926
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:665
bool collect_grouped_aggregates(uchar *) override
Definition: item_sum.cc:703
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:587
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:404
void 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:888
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:723
void aggregator_clear()
Called to cleanup the aggregator.
Definition: item_sum.h:677
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:783
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.cc:476
bool aggregate_check_distinct(uchar *arg) override
Definition: item_sum.cc:619
bool init_sum_func_check(THD *thd)
Prepare an aggregate function for checking of context.
Definition: item_sum.cc:156
virtual bool aggregator_setup(THD *thd)
Called to initialize the aggregator.
Definition: item_sum.h:671
bool wf_common_init()
Common initial actions for window functions.
Definition: item_sum.cc:909
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:611
bool allow_group_via_temp_table
If incremental update of fields is supported.
Definition: item_sum.h:494
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:458
void make_const()
Definition: item_sum.h:594
Item * replace_aggregate(uchar *) override
Definition: item_sum.cc:734
Window * window()
Definition: item_sum.h:733
virtual void make_unique()
Definition: item_sum.h:616
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.cc:876
bool reset_and_add()
Resets the aggregate value to its default and aggregates the current value of its attribute(s).
Definition: item_sum.h:551
bool is_null() override
The method allows to determine nullness of a complex expression without fully evaluating it,...
Definition: item_sum.h:592
virtual Item * result_item(Field *field)
Definition: item_sum.h:574
bool forced_const
True means that this field has been evaluated during optimization.
Definition: item_sum.h:508
virtual void update_field()=0
bool check_sum_func(THD *thd, Item **ref)
Validate the semantic requirements of a set function.
Definition: item_sum.cc:241
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:514
void unsupported_as_wf()
Definition: item_sum.h:792
Aggregator * aggr
Aggregator class instance.
Definition: item_sum.h:408
const Window * window() const
Definition: item_sum.h:734
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:763
virtual bool is_rollup_sum_wrapper() const
Overridden by Item_rollup_sum_switcher.
Definition: item_sum.h:777
bool aggregate_check_group(uchar *arg) override
Definition: item_sum.cc:643
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:545
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:742
virtual void reset_field()=0
virtual bool framing() const
All aggregates are framing, i.e.
Definition: item_sum.h:747
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:762
Item * set_arg(THD *thd, uint i, Item *new_val) override
Definition: item_sum.cc:832
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:676
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:820
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:837
bool aggregator_add()
Called to add value to the aggregator.
Definition: item_sum.h:683
Item_sum(Item *a)
Definition: item_sum.h:524
void set_distinct(bool distinct)
Definition: item_sum.h:686
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:572
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:752
bool has_force_copy_fields() const
Definition: item_sum.h:437
Type type() const override
Definition: item_sum.h:541
void mark_as_sum_func()
Definition: item_sum.cc:448
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:128
bool reset_wf_state(uchar *arg) override
Reset execution state for such window function types as determined by arg.
Definition: item_sum.cc:896
table_map used_tables() const override
Definition: item_sum.h:584
virtual Item_sum * unwrap_sum()
Non-const version.
Definition: item_sum.h:785
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:783
virtual bool setup(THD *)
Definition: item_sum.h:701
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:864
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:753
Item ** get_arg_ptr(uint i)
Definition: item_sum.h:651
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize() but with additional parameter.
Definition: item_sum.cc:96
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 eq(const Item *item, bool binary_cmp) const override
Definition: item_sum.cc:592
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:668
Query_block * base_query_block
query block where function is placed
Definition: item_sum.h:484
Definition: item_sum.h:1935
udf_handler udf
Definition: item_sum.h:1939
void update_field() override
Definition: item_sum.h:1971
void cleanup() override
Called for every Item after use (preparation and execution).
Definition: item_sum.cc:3881
bool itemize(Parse_context *pc, Item **res) override
The same as contextualize() but with additional parameter.
Definition: item_sum.cc:3859
void reset_field() override
Definition: item_sum.h:1970
bool add() override
Definition: item_sum.cc:3874
bool fix_fields(THD *thd, Item **ref) override
Definition: item_sum.h:1956
Item_udf_sum(const POS &pos, udf_func *udf_arg, PT_item_list *opt_list)
Definition: item_sum.h:1942
void clear() override
Definition: item_sum.cc:3868
~Item_udf_sum() override
Definition: item_sum.h:1950
void print(const THD *thd, String *str, enum_query_type query_type) const override
This method is used for to:
Definition: item_sum.cc:3890
Item_udf_sum(THD *thd, Item_udf_sum *item)
Definition: item_sum.h:1946
Item_sum super
Definition: item_sum.h:1936
const char * func_name() const override
Definition: item_sum.h:1955
enum Sumfunctype sum_func() const override
Definition: item_sum.h:1966
Definition: item_sum.h:1354
my_decimal * val_decimal(my_decimal *dec_buf) override
Definition: item_sum.h:1363
const char * func_name() const override
Definition: item_sum.h:1367
bool resolve_type(THD *) override
Resolve type-related information for this item, such as result field type, maximum size,...
Definition: item_sum.h:1366
enum Type type() const override
Definition: item_sum.h:1360
Item_variance_field(Item_sum_variance *item)
Definition: item_sum.cc:3830
uint sample
Definition: item_sum.h:1356
String * val_str(String *str) override
Definition: item_sum.h:1362
double val_real() override
Definition: item_sum.cc:3844
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:1371
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:853
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:3367
void set_nullable(bool nullable)
Definition: item.h:3470
DTCollation collation
Character set and collation properties assigned for this Item.
Definition: item.h:3374
bool get_time_from_decimal(MYSQL_TIME *ltime)
Convert val_decimal() to time in MYSQL_TIME.
Definition: item.cc:1559
void set_data_type(enum_field_types data_type)
Set the data type of the current Item.
Definition: item.h:1394
my_decimal * val_decimal_from_string(my_decimal *decimal_value)
Definition: item.cc:343
bool is_nullable() const
Definition: item.h:3469
void set_data_type_geometry()
Set the data type of the Item to be GEOMETRY.
Definition: item.h:1594
bool get_time_from_string(MYSQL_TIME *ltime)
Convert val_str() to time in MYSQL_TIME.
Definition: item.cc:1540
bool get_time_from_real(MYSQL_TIME *ltime)
Convert val_real() to time in MYSQL_TIME.
Definition: item.cc:1550
bool get_date_from_decimal(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_decimal() to date in MYSQL_TIME.
Definition: item.cc:1472
void set_data_type_double()
Set the data type of the Item to be double precision floating point.
Definition: item.h:1438
Item_name_string item_name
Name from query.
Definition: item.h:3375
bool fixed
True if item has been resolved.
Definition: item.h:3458
virtual Item_result result_type() const
Definition: item.h:1332
bool null_value
True if item is null.
Definition: item.h:3495
Type
Definition: item.h:889
@ SUM_FUNC_ITEM
Definition: item.h:893
@ FIELD_VARIANCE_ITEM
Definition: item.h:906
@ FIELD_AVG_ITEM
Definition: item.h:900
@ FIELD_STD_ITEM
Definition: item.h:905
@ FIELD_BIT_ITEM
Definition: item.h:918
bool get_date_from_numeric(MYSQL_TIME *ltime, my_time_flags_t fuzzydate)
Convert a numeric type to date.
Definition: item.cc:1500
virtual TYPELIB * get_typelib() const
Get the typelib information for an item of type set or enum.
Definition: item.h:1657
my_decimal * val_decimal_from_real(my_decimal *decimal_value)
Definition: item.cc:328
bool unsigned_flag
Definition: item.h:3496
longlong val_int_from_string()
Definition: item.cc:500
bool get_date_from_string(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_str() to date in MYSQL_TIME.
Definition: item.cc:1453
void set_grouping_func()
Set the property: this item is a call to GROUPING.
Definition: item.h:3292
bool get_time_from_numeric(MYSQL_TIME *ltime)
Convert a numeric type to time.
Definition: item.cc:1592
virtual bool mark_field_in_map(uchar *arg)
Mark underlying field in read or write map of a table.
Definition: item.h:2657
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:3506
bool get_date_from_int(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_int() to date in MYSQL_TIME.
Definition: item.cc:1481
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:1634
bool get_date_from_real(MYSQL_TIME *ltime, my_time_flags_t flags)
Convert val_real() to date in MYSQL_TIME.
Definition: item.cc:1463
void set_data_type_longlong()
Set the data type of the Item to be longlong.
Definition: item.h:1416
double val_real_from_string()
Definition: item.cc:446
bool update_null_value()
Make sure the null_value member has a correct value.
Definition: item.cc:7419
String * val_string_from_real(String *str)
Definition: item.cc:261
bool get_time_from_int(MYSQL_TIME *ltime)
Convert val_int() to time in MYSQL_TIME.
Definition: item.cc:1568
Represents a JSON array container, i.e.
Definition: json_dom.h:520
Represents a JSON container value of type "object" (ECMA), type J_OBJECT here.
Definition: json_dom.h:373
Abstraction for accessing JSON values irrespective of whether they are (started out as) binary JSON v...
Definition: json_dom.h:1161
Definition: sql_list.h:434
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:214
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:206
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1156
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:168
const CHARSET_INFO * charset() const
Definition: sql_string.h:241
const char * ptr() const
Definition: sql_string.h:250
size_t length() const
Definition: sql_string.h:242
void set(String &str, size_t offset, size_t arg_length)
Definition: sql_string.h:281
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:95
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:105
uint size() const
Definition: sql_list.h:272
A (partial) implementation of std::deque allocating its blocks on a MEM_ROOT.
Definition: mem_root_deque.h:110
my_decimal class limits 'decimal_t' type to what we need in MySQL.
Definition: my_decimal.h:94
Definition: sql_udf.h:83
const char * name() const
Definition: sql_udf.h:117
bool is_initialized() const
Definition: sql_udf.h:115
bool m_original
Definition: sql_udf.h:104
bool fix_fields(THD *thd, Item_result_field *item, uint arg_count, Item **args)
Definition: item_func.cc:4480
void free_handler()
Definition: item_func.cc:4461
enum_query_type
Query type constants (usable as bitmap flags).
Definition: enum_query_type.h:31
Fido Client Authentication nullptr
Definition: fido_client_plugin.cc:222
This file contains the field type.
enum_field_types
Column types for MySQL.
Definition: field_types.h:53
@ MYSQL_TYPE_LONGLONG
Definition: field_types.h:62
@ MYSQL_TYPE_NEWDECIMAL
Definition: field_types.h:78
@ MYSQL_TYPE_DOUBLE
Definition: field_types.h:59
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:47
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:4095
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:4017
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:4052
A better implementation of the UNIX ctype(3) library.
MYSQL_PLUGIN_IMPORT CHARSET_INFO my_charset_bin
Definition: ctype-bin.cc:511
#define my_strntod(s, a, b, c, d)
Definition: m_ctype.h:779
longlong my_strtoll10(const char *nptr, const char **endptr, int *error)
Definition: my_strtoll10.cc:87
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:489
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
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:43
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:1560
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
Definition: commit_order_queue.h:34
PT & ref(PT *tp)
Definition: tablespace_impl.cc:359
static int destroy(mysql_cond_t *that, const char *, unsigned int)
Definition: mysql_cond_v1_native.cc:54
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:420
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:2878
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:385
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
const char * banned_function_name
the name of the function which is not allowed
Definition: item.h:502
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:620
Collect_grouped_aggregate_info(Query_block *select)
Definition: item_sum.h:633
Query_block * m_query_block
The query block we walk from.
Definition: item_sum.h:628
std::vector< Item_sum * > list
accumulated all aggregates found
Definition: item_sum.h:622
std::set< Item_sum * > aggregates_that_were_hidden
Definition: item_sum.h:623
bool m_break_off
true: break off transformation
Definition: item_sum.h:630
bool m_outside
true: an aggregate aggregates outside m_query_block
Definition: item_sum.h:632
Definition: mysql_time.h:82
Definition: item.h:398
Definition: table.h:282
Environment data for the contextualization phase.
Definition: parse_tree_node_base.h:121
Definition: table.h:1399
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:1538
Bison "location" class.
Definition: parse_location.h:43
Definition: result.h:30
Definition: sql_udf.h:44
unsigned int uint
Definition: uca9-dump.cc:75
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...