-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathitem_sum.h
2871 lines (2472 loc) · 99.4 KB
/
item_sum.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef ITEM_SUM_INCLUDED
#define ITEM_SUM_INCLUDED
/* Copyright (c) 2000, 2024, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* classes for sum functions */
#include <assert.h>
#include <sys/types.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "field_types.h" // enum_field_types
#include "my_alloc.h"
#include "my_compiler.h"
#include "my_inttypes.h"
#include "my_sys.h"
#include "my_table_map.h"
#include "my_time.h"
#include "my_tree.h" // TREE
#include "mysql/strings/m_ctype.h"
#include "mysql/strings/my_strtoll10.h"
#include "mysql/udf_registration_types.h"
#include "mysql_time.h"
#include "mysqld_error.h"
#include "sql-common/my_decimal.h"
#include "sql/enum_query_type.h"
#include "sql/gis/geometries_cs.h"
#include "sql/gis/wkb.h"
#include "sql/item.h" // Item_result_field
#include "sql/item_func.h" // Item_int_func
#include "sql/mem_root_array.h"
#include "sql/parse_location.h" // POS
#include "sql/parse_tree_window.h" // PT_window
#include "sql/sql_base.h"
#include "sql/sql_const.h"
#include "sql/sql_list.h"
#include "sql/sql_udf.h" // udf_handler
#include "sql/thr_malloc.h" // THR_MALLOC
#include "sql/window_lex.h"
#include "sql_string.h"
#include "template_utils.h"
class Field;
class Item_sum;
class Json_array;
class Json_object;
class Json_wrapper;
class PT_item_list;
class PT_order_list;
class Query_block;
class THD;
class Temp_table_param;
class Window;
struct ORDER;
struct Parse_context;
struct TABLE;
struct Window_evaluation_requirements;
/**
The abstract base class for the Aggregator_* classes.
It implements the data collection functions (setup/add/clear)
as either pass-through to the real functionality or
as collectors into an Unique (for distinct) structure.
Note that update_field/reset_field are not in that
class, because they're simply not called when
GROUP BY/DISTINCT can be handled with help of index on grouped
fields (allow_group_via_temp_table is false);
*/
class Aggregator {
friend class Item_sum;
friend class Item_sum_sum;
friend class Item_sum_count;
friend class Item_sum_avg;
/*
All members are protected as this class is not usable outside of an
Item_sum descendant.
*/
protected:
/* the aggregate function class to act on */
Item_sum *item_sum;
public:
Aggregator(Item_sum *arg) : item_sum(arg) {}
virtual ~Aggregator() = default;
enum Aggregator_type { SIMPLE_AGGREGATOR, DISTINCT_AGGREGATOR };
virtual Aggregator_type Aggrtype() = 0;
/**
Called before adding the first row.
Allocates and sets up the internal aggregation structures used,
e.g. the Unique instance used to calculate distinct.
*/
virtual bool setup(THD *) = 0;
/**
Called when we need to wipe out all the data from the aggregator:
all the values accumulated and all the state.
Cleans up the internal structures and resets them to their initial state.
*/
virtual void clear() = 0;
/**
Called when there's a new value to be aggregated.
Updates the internal state of the aggregator to reflect the new value.
*/
virtual bool add() = 0;
/**
Called when there are no more data and the final value is to be retrieved.
Finalises the state of the aggregator, so the final result can be retrieved.
*/
virtual void endup() = 0;
/** Decimal value of being-aggregated argument */
virtual my_decimal *arg_val_decimal(my_decimal *value) = 0;
/** Floating point value of being-aggregated argument */
virtual double arg_val_real() = 0;
/**
NULLness of being-aggregated argument.
@param use_null_value Optimization: to determine if the argument is NULL
we must, in the general case, call is_null() on it, which itself might
call val_*() on it, which might be costly. If you just have called
arg_val*(), you can pass use_null_value=true; this way, arg_is_null()
might avoid is_null() and instead do a cheap read of the Item's null_value
(updated by arg_val*()).
*/
virtual bool arg_is_null(bool use_null_value) = 0;
};
/**
Class Item_sum is the base class used for special expressions that SQL calls
'set functions'. These expressions are formed with the help of aggregate
functions such as SUM, MAX, GROUP_CONCAT etc.
Class Item_sum is also the base class for Window functions; the text below
first documents set functions, then window functions.
GENERAL NOTES
A set function cannot be used in all positions where expressions are accepted.
There are some quite explicable restrictions for the use of set functions.
In the query:
SELECT AVG(b) FROM t1 WHERE SUM(b) > 20 GROUP by a
the set function AVG(b) is valid, while the usage of SUM(b) is invalid.
A WHERE condition must contain expressions that can be evaluated for each row
of the table. Yet the expression SUM(b) can be evaluated only for each group
of rows with the same value of column a.
In the query:
SELECT AVG(b) FROM t1 WHERE c > 30 GROUP BY a HAVING SUM(b) > 20
both set function expressions AVG(b) and SUM(b) are valid.
We can say that in a query without nested selects an occurrence of a
set function in an expression of the SELECT list or/and in the HAVING
clause is valid, while in the WHERE clause, FROM clause or GROUP BY clause
it is invalid.
The general rule to detect whether a set function is valid in a query with
nested subqueries is much more complicated.
Consider the following query:
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a > ALL (SELECT t2.c FROM t2 WHERE SUM(t1.b) < t2.c).
The set function SUM(b) is used here in the WHERE clause of the subquery.
Nevertheless it is valid since it is contained in the HAVING clause of the
outer query. The expression SUM(t1.b) is evaluated for each group defined
in the main query, not for groups of the subquery.
The problem of finding the query where to aggregate a particular
set function is not so simple as it seems to be.
In the query:
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a > ALL(SELECT t2.c FROM t2 GROUP BY t2.c
HAVING SUM(t1.a) < t2.c)
the set function can be evaluated in both the outer and the inner query block.
If we evaluate SUM(t1.a) for the outer query then we get the value of t1.a
multiplied by the cardinality of a group in table t1. In this case,
SUM(t1.a) is used as a constant value in each correlated subquery.
But SUM(t1.a) can also be evaluated for the inner query.
In this case t1.a will be a constant value for each correlated subquery and
summation is performed for each group of table t2.
(Here it makes sense to remind that the query
SELECT c FROM t GROUP BY a HAVING SUM(1) < a
is quite valid in our SQL).
So depending on what query block we assign the set function to we
can get different results.
The general rule to detect the query block Q where a set function will be
aggregated (evaluated) can be formulated as follows.
Reference: SQL2011 @<set function specification@> syntax rules 6 and 7.
Consider a set function S(E) where E is an expression which contains
column references C1, ..., Cn. Resolve all column references Ci against
the query block Qi containing the set function S(E). Let Q be the innermost
query block of all query blocks Qi. (It should be noted here that S(E)
in no way can be aggregated in the query block containing the subquery Q,
otherwise S(E) would refer to at least one unbound column reference).
If S(E) is used in a construct of Q where set functions are allowed then
we aggregate S(E) in Q.
Otherwise:
- if ANSI SQL mode is enabled (MODE_ANSI), then report an error.
- otherwise, look for the innermost query block containing S(E) of those
where usage of S(E) is allowed. The place of aggregation depends on which
clause the subquery is contained within; It will be different when
contained in a WHERE clause versus in the select list or in HAVING clause.
Let's demonstrate how this rule is applied to the following queries.
1. SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
HAVING SUM(t1.a+t2.b) < t3.c))
For this query the set function SUM(t1.a+t2.b) contains t1.a and t2.b
with t1.a defined in the outermost query, and t2.b defined for its
subquery. The set function is contained in the HAVING clause of the subquery
and can be evaluated in this subquery.
2. SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a > ALL(SELECT t2.b FROM t2
WHERE t2.b > ALL (SELECT t3.c FROM t3 GROUP BY t3.c
HAVING SUM(t1.a+t2.b) < t3.c))
The set function SUM(t1.a+t2.b) is contained in the WHERE clause of the second
query block - the outermost query block where t1.a and t2.b are defined.
If we evaluate the function in this subquery we violate the context rules.
So we evaluate the function in the third query block (over table t3) where it
is used under the HAVING clause; if in ANSI SQL mode, an error is thrown.
3. SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a > ALL(SELECT t2.b FROM t2
WHERE t2.b > ALL (SELECT t3.c FROM t3
WHERE SUM(t1.a+t2.b) < t3.c))
In this query, evaluation of SUM(t1.a+t2.b) is not valid neither in the second
nor in the third query block.
Set functions can generally not be nested. In the query
SELECT t1.a from t1 GROUP BY t1.a HAVING AVG(SUM(t1.b)) > 20
the expression SUM(b) is not valid, even though it is contained inside
a HAVING clause.
However, it is acceptable in the query:
SELECT t.1 FROM t1 GROUP BY t1.a HAVING SUM(t1.b) > 20.
An argument of a set function does not have to be a simple column reference
as seen in examples above. This can be a more complex expression
SELECT t1.a FROM t1 GROUP BY t1.a HAVING SUM(t1.b+1) > 20.
The expression SUM(t1.b+1) has clear semantics in this context:
we sum up the values of t1.b+1 where t1.b varies for all values within a
group of rows that contain the same t1.a value.
A set function for an outer query yields a constant value within a subquery.
So the semantics of the query
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
HAVING AVG(t2.c+SUM(t1.b)) > 20)
is still clear. For a group of rows with the same value for t1.a, calculate
the value of SUM(t1.b) as 's'. This value is substituted in the subquery:
SELECT t2.c FROM t2 GROUP BY t2.c HAVING AVG(t2.c+s)
By the same reason the following query with a subquery
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a IN (SELECT t2.c FROM t2 GROUP BY t2.c
HAVING AVG(SUM(t1.b)) > 20)
is also valid.
IMPLEMENTATION NOTES
The member base_query_block contains a reference to the query block that the
set function is contained within.
The member aggr_query_block contains a reference to the query block where the
set function is aggregated.
The field max_aggr_level holds the maximum of the nest levels of the
unbound column references contained in the set function. A column reference
is unbound within a set function if it is not bound by any subquery
used as a subexpression in this function. A column reference is bound by
a subquery if it is a reference to the column by which the aggregation
of some set function that is used in the subquery is calculated.
For the set function used in the query
SELECT t1.a FROM t1 GROUP BY t1.a
HAVING t1.a > ALL(SELECT t2.b FROM t2 GROUP BY t2.b
HAVING t2.b > ALL(SELECT t3.c FROM t3 GROUP BY t3.c
HAVING SUM(t1.a+t2.b) < t3.c))
the value of max_aggr_level is equal to 1 since t1.a is bound in the main
query, and t2.b is bound by the first subquery whose nest level is 1.
Obviously a set function cannot be aggregated in a subquery whose
nest level is less than max_aggr_level. (Yet it can be aggregated in the
subqueries whose nest level is greater than max_aggr_level.)
In the query
SELECT t1.a FROM t1 HAVING AVG(t1.a+(SELECT MIN(t2.c) FROM t2))
the value of the max_aggr_level for the AVG set function is 0 since
the reference t2.c is bound in the subquery.
If a set function contains no column references (like COUNT(*)),
max_aggr_level is -1.
The field 'max_sum_func_level' is to contain the maximum of the
nest levels of the set functions that are used as subexpressions of
the arguments of the given set function, but not aggregated in any
subquery within this set function. A nested set function s1 can be
used within set function s0 only if s1.max_sum_func_level <
s0.max_sum_func_level. Set function s1 is considered as nested
for set function s0 if s1 is not calculated in any subquery
within s0.
A set function that is used as a subexpression in an argument of another
set function refers to the latter via the field 'in_sum_func'.
The condition imposed on the usage of set functions are checked when
we traverse query subexpressions with the help of the recursive method
fix_fields. When we apply this method to an object of the class
Item_sum, first, on the descent, we call the method init_sum_func_check
that initialize members used at checking. Then, on the ascent, we
call the method check_sum_func that validates the set function usage
and reports an error if it is invalid.
The method check_sum_func serves to link the items for the set functions
that are aggregated in the containing query blocks. Circular chains of such
functions are attached to the corresponding Query_block structures
through the field inner_sum_func_list.
Exploiting the fact that the members mentioned above are used in one
recursive function we could have allocated them on the thread stack.
Yet we don't do it now.
It is assumed that the nesting level of subqueries does not exceed 63
(valid nesting levels are stored in a 64-bit bitmap called nesting_map).
The assumption is enforced in LEX::new_query().
WINDOW FUNCTIONS
Most set functions (e.g. SUM, COUNT, AVG) can also be used as window
functions. In that case, notable differences compared to set functions are:
- not using any Aggregator
- not supporting DISTINCT
- val_*() does more than returning the function's current value: it
first accumulates the function's argument into the function's
state. Execution (e.g. end_write_wf()) manipulates temporary tables which
contain input for WFs; each input row is passed to copy_funcs() which calls
the WF's val_*() to accumulate it.
*/
class Item_sum : public Item_func {
friend class Aggregator_distinct;
friend class Aggregator_simple;
protected:
/**
Aggregator class instance. Not set initially. Allocated only after
it is determined if the incoming data are already distinct.
*/
Aggregator *aggr{nullptr};
/**
If sum is a window function, this field contains the window.
*/
PT_window *m_window{nullptr};
/**
True if we have already resolved this window functions window reference.
Used in execution of prepared statement to avoid re-resolve.
*/
bool m_window_resolved{false};
private:
/**
Used in making ROLLUP. Set for the ROLLUP copies of the original
Item_sum and passed to create_tmp_field() to cause it to work
over the temp table buffer that is referenced by
Item_result_field::result_field.
*/
bool force_copy_fields{false};
/**
Indicates how the aggregate function was specified by the parser :
true if it was written as AGGREGATE(DISTINCT),
false if it was AGGREGATE()
*/
bool with_distinct{false};
public:
bool has_force_copy_fields() const { return force_copy_fields; }
bool has_with_distinct() const { return with_distinct; }
enum Sumfunctype {
COUNT_FUNC, // COUNT
COUNT_DISTINCT_FUNC, // COUNT (DISTINCT)
SUM_FUNC, // SUM
SUM_DISTINCT_FUNC, // SUM (DISTINCT)
AVG_FUNC, // AVG
AVG_DISTINCT_FUNC, // AVG (DISTINCT)
MIN_FUNC, // MIN
MAX_FUNC, // MAX
STD_FUNC, // STD/STDDEV/STDDEV_POP
VARIANCE_FUNC, // VARIANCE/VAR_POP and VAR_SAMP
SUM_BIT_FUNC, // BIT_AND, BIT_OR and BIT_XOR
UDF_SUM_FUNC, // user defined functions
GROUP_CONCAT_FUNC, // GROUP_CONCAT
JSON_AGG_FUNC, // JSON_ARRAYAGG and JSON_OBJECTAGG
ROW_NUMBER_FUNC, // Window functions
RANK_FUNC,
DENSE_RANK_FUNC,
CUME_DIST_FUNC,
PERCENT_RANK_FUNC,
NTILE_FUNC,
LEAD_LAG_FUNC,
FIRST_LAST_VALUE_FUNC,
NTH_VALUE_FUNC,
ROLLUP_SUM_SWITCHER_FUNC,
GEOMETRY_AGGREGATE_FUNC
};
/**
@note most member variables below serve only for grouped aggregate
functions.
*/
/**
For a group aggregate which is aggregated into an outer query
block; none, or just the first or both cells may be non-zero. They are
filled with references to the group aggregate (for example if it is the
argument of a function; it is then a pointer to that function's args[i]
pointer).
*/
Item **referenced_by[2];
/// next in the circular chain of registered objects
Item_sum *next_sum{nullptr};
Item_sum *in_sum_func; ///< the containing set function if any
Query_block *base_query_block; ///< query block where function is placed
/**
For a group aggregate, query block where function is aggregated. For a
window function, nullptr, as such function is always aggregated in
base_query_block, as it mustn't contain any outer reference.
*/
Query_block *aggr_query_block;
int8 max_aggr_level; ///< max level of unbound column references
int8
max_sum_func_level; ///< max level of aggregation for contained functions
bool allow_group_via_temp_table; ///< If incremental update of fields is
///< supported.
/**
WFs are forbidden when resolving Item_sum; this member is used to restore
WF allowance status afterwards.
*/
nesting_map save_deny_window_func;
protected:
/**
True means that this field has been evaluated during optimization.
When set, used_tables() returns zero and const_item() returns true.
The value must be reset to false after execution.
*/
bool forced_const{false};
/// true if the function is resolved to be always NULL
bool m_null_resolved{false};
/// true if the function is determined to be NULL at start of execution
bool m_null_executed{false};
static ulonglong ram_limitation(THD *thd);
public:
void mark_as_sum_func();
void mark_as_sum_func(Query_block *);
Item_sum(const POS &pos, PT_window *w)
: Item_func(pos), m_window(w), allow_group_via_temp_table(true) {}
Item_sum(Item *a)
: Item_func(a), m_window(nullptr), allow_group_via_temp_table(true) {
mark_as_sum_func();
}
Item_sum(const POS &pos, Item *a, PT_window *w)
: Item_func(pos, a), m_window(w), allow_group_via_temp_table(true) {}
Item_sum(const POS &pos, Item *a, Item *b, PT_window *w)
: Item_func(pos, a, b), m_window(w), allow_group_via_temp_table(true) {}
Item_sum(const POS &pos, PT_item_list *opt_list, PT_window *w);
/// Copy constructor, need to perform subqueries with temporary tables
Item_sum(THD *thd, const Item_sum *item);
~Item_sum() override { assert(aggr == nullptr); }
bool do_itemize(Parse_context *pc, Item **res) override;
Type type() const override { return SUM_FUNC_ITEM; }
virtual enum Sumfunctype sum_func() const = 0;
// Differs only for Item_rollup_sum_switcher.
virtual enum Sumfunctype real_sum_func() const { return sum_func(); }
/**
Resets the aggregate value to its default and aggregates the current
value of its attribute(s).
*/
inline bool reset_and_add() {
aggregator_clear();
return aggregator_add();
}
/*
Called when new group is started and results are being saved in
a temporary table. Similarly to reset_and_add() it resets the
value to its default and aggregates the value of its
attribute(s), but must also store it in result_field.
This set of methods (result_item(), reset_field, update_field()) of
Item_sum is used only if allow_group_via_temp_table is true. Otherwise
copy_or_same() is used to obtain a copy of this item.
*/
virtual void reset_field() = 0;
/*
Called for each new value in the group, when temporary table is in use.
Similar to add(), but uses temporary table field to obtain current value,
Updated value is then saved in the field.
*/
virtual void update_field() = 0;
virtual bool keep_field_type() const { return false; }
bool resolve_type(THD *) override;
virtual Item *result_item(Field *field) {
Item_field *item = new Item_field(field);
if (item == nullptr) return nullptr;
// Aggregated fields have no reference to an underlying table
assert(item->original_db_name() == nullptr &&
item->original_table_name() == nullptr);
// Break the connection to the original field since this is an aggregation
item->set_original_field_name(nullptr);
return item;
}
table_map used_tables() const override {
return forced_const ? 0 : used_tables_cache;
}
table_map not_null_tables() const override { return used_tables(); }
void update_used_tables() override;
void fix_after_pullout(Query_block *parent_query_block,
Query_block *removed_query_block) override;
void add_used_tables_for_aggr_func();
bool is_null() override { return null_value; }
void make_const() {
// "forced_const" will make used_tables() return zero for this object
forced_const = true;
}
void print(const THD *thd, String *str,
enum_query_type query_type) const override;
bool eq(const Item *item) const override;
bool eq_specific(const Item *item) const override;
/**
Mark an aggregate as having no rows.
This function is called by the execution engine to assign 'NO ROWS
FOUND' value to an aggregate item, when the underlying result set
has no rows. Such value, in a general case, may be different from
the default value of the item after 'clear()': e.g. a numeric item
may be initialized to 0 by clear() and to NULL by
no_rows_in_result().
*/
void no_rows_in_result() override {
set_aggregator(with_distinct ? Aggregator::DISTINCT_AGGREGATOR
: Aggregator::SIMPLE_AGGREGATOR);
aggregator_clear();
}
virtual void make_unique() { force_copy_fields = true; }
virtual Field *create_tmp_field(bool group, TABLE *table);
/// argument used by walk method collect_grouped_aggregates ("cga")
struct Collect_grouped_aggregate_info {
/// accumulated all aggregates found
std::vector<Item_sum *> list;
std::set<Item_sum *> aggregates_that_were_hidden;
/**
The query block we walk from. All found aggregates must aggregate in
this; if some aggregate in outer query blocks, break off transformation.
*/
Query_block *m_query_block{nullptr};
/// true: break off transformation
bool m_break_off{false};
/// true: an aggregate aggregates outside m_query_block
bool m_outside{false};
Collect_grouped_aggregate_info(Query_block *select)
: m_query_block(select) {}
};
bool collect_grouped_aggregates(uchar *) override;
Item *replace_aggregate(uchar *) override;
bool collect_scalar_subqueries(uchar *) override;
bool collect_item_field_or_view_ref_processor(uchar *) override;
bool clean_up_after_removal(uchar *arg) override;
bool aggregate_check_group(uchar *arg) override;
bool aggregate_check_distinct(uchar *arg) override;
bool has_aggregate_ref_in_group_by(uchar *arg) override;
bool init_sum_func_check(THD *thd);
bool check_sum_func(THD *thd, Item **ref);
Item *set_arg(THD *thd, uint i, Item *new_val) override;
/// @todo delete this when we no longer support temporary transformations
Item **get_arg_ptr(uint i) { return &args[i]; }
bool fix_fields(THD *thd, Item **ref) override;
/**
Signal to the function that its arguments may have changed,
and that any internal caches etc. based on those arguments
must be updated accordingly.
This is used by the hypergraph optimizer when it rewrites
arguments to window functions to take into account that they
have been materialized into temporary tables, or that they
should read their values from the framebuffer.
*/
virtual void update_after_wf_arguments_changed(THD *) {}
/**
Called to initialize the aggregator.
*/
virtual bool aggregator_setup(THD *thd) { return aggr->setup(thd); }
/**
Called to cleanup the aggregator.
*/
inline void aggregator_clear() { aggr->clear(); }
/**
Called to add value to the aggregator.
*/
inline bool aggregator_add() { return aggr->add(); }
/* stores the declared DISTINCT flag (from the parser) */
void set_distinct(bool distinct) {
with_distinct = distinct;
allow_group_via_temp_table = !with_distinct;
}
/*
Set the type of aggregation : DISTINCT or not.
May be called multiple times.
*/
virtual int set_aggregator(Aggregator::Aggregator_type aggregator);
virtual void clear() = 0;
virtual bool add() = 0;
virtual bool setup(THD *) { return false; }
/**
Only relevant for aggregates qua window functions. Checks semantics after
windows have been set up and checked. Window functions have specific
requirements on the window specifications. Used at resolution time.
@param thd Current thread
@param select The current select
@param [out] reqs Holds collected requirements from this wf
@returns true if error
*/
virtual bool check_wf_semantics1(THD *thd, Query_block *select,
Window_evaluation_requirements *reqs);
/**
Like check_wf_semantics1.
For checks which cannot be done in resolution phase (mostly those for
input parameters which can be '?' and must be >=0: value isn't known
before execution phase).
*/
virtual bool check_wf_semantics2(Window_evaluation_requirements *reqs
[[maybe_unused]]) {
return false;
}
bool split_sum_func(THD *thd, Ref_item_array ref_item_array,
mem_root_deque<Item *> *fields) override;
void cleanup() override;
Window *window() { return m_window; }
const Window *window() const { return m_window; }
bool reset_wf_state(uchar *arg) override;
/**
All aggregates are framing, i.e. they work on the window's frame. If none
is defined, the frame is by default the entire partition, unless ORDER BY
is defined, in which case it is the set of rows from the start of the
partition to and including the peer set of the current row.
Some window functions are not framing, i.e. they always work on the entire
partition. For such window functions, the method is overridden to
return false.
*/
virtual bool framing() const { return true; }
/**
Only for framing window functions. True if this function only needs to
read one row per frame.
*/
virtual bool uses_only_one_row() const { return false; }
/**
Return true if we need to make two passes over the rows in the partition -
either because we need the cardinality of it (and we need to read all
rows to detect the next partition), or we need to have all partition rows
available to evaluate the window function for some other reason, e.g.
we may need the last row in the partition in the frame buffer to be able
to evaluate LEAD.
*/
virtual bool needs_partition_cardinality() const { return false; }
/**
Common initial actions for window functions. For non-buffered processing
("on-the-fly"), check partition change and possible reset partition
state. In this case return false.
For buffered processing, if windowing state m_do_copy_null is true, set
null_value to is_nullable() and return true.
@return true if case two above holds, else false
*/
bool wf_common_init();
/// Overridden by Item_rollup_sum_switcher.
virtual bool is_rollup_sum_wrapper() const { return false; }
/**
* In case we are an Item_rollup_sum_switcher,
* return the underlying Item_sum, otherwise, return this.
* Overridden by Item_rollup_sum_switcher.
*/
virtual const Item_sum *unwrap_sum() const { return this; }
/// Non-const version
virtual Item_sum *unwrap_sum() { return this; }
protected:
/*
Raise an error (ER_NOT_SUPPORTED_YET) with the detail that this
function is not yet supported as a window function.
*/
void unsupported_as_wf() {
char buff[STRING_BUFFER_USUAL_SIZE];
snprintf(buff, sizeof(buff), "%s as window function", func_name());
my_error(ER_NOT_SUPPORTED_YET, MYF(0), buff);
}
void add_json_info(Json_object *obj) override {
obj->add_alias("distinct", create_dom_ptr<Json_boolean>(with_distinct));
}
};
class Unique;
/**
The distinct aggregator.
Implements AGGFN (DISTINCT ..)
Collects all the data into an Unique (similarly to what Item_sum_distinct
does currently) and then (if applicable) iterates over the list of
unique values and pumps them back into its object
*/
class Aggregator_distinct : public Aggregator {
friend class Item_sum_sum;
/*
flag to prevent consecutive runs of endup(). Normally in endup there are
expensive calculations (like walking the distinct tree for example)
which we must do only once if there are no data changes.
We can re-use the data for the second and subsequent val_xxx() calls.
endup_done set to true also means that the calculated values for
the aggregate functions are correct and don't need recalculation.
*/
bool endup_done;
/*
Used depending on the type of the aggregate function and the presence of
blob columns in it:
- For COUNT(DISTINCT) and no blob fields this points to a real temporary
table. It's used as a hash table.
- For AVG/SUM(DISTINCT) or COUNT(DISTINCT) with blob fields only the
in-memory data structure of a temporary table is constructed.
It's used by the Field classes to transform data into row format.
*/
TABLE *table;
/*
An array of field lengths on row allocated and used only for
COUNT(DISTINCT) with multiple columns and no blobs. Used in
Aggregator_distinct::composite_key_cmp (called from Unique to compare
nodes
*/
uint32 *field_lengths;
/*
Used in conjunction with 'table' to support the access to Field classes
for COUNT(DISTINCT). Needed by copy_fields()/copy_funcs().
*/
Temp_table_param *tmp_table_param;
/*
If there are no blobs in the COUNT(DISTINCT) arguments, we can use a tree,
which is faster than heap table. In that case, we still use the table
to help get things set up, but we insert nothing in it.
For AVG/SUM(DISTINCT) we always use this tree (as it takes a single
argument) to get the distinct rows.
*/
Unique *tree;
/*
The length of the temp table row. Must be a member of the class as it
gets passed down to simple_raw_key_cmp () as a compare function argument
to Unique. simple_raw_key_cmp () is used as a fast comparison function
when the entire row can be binary compared.
*/
uint tree_key_length;
enum Const_distinct {
NOT_CONST = 0,
/**
Set to true if the result is known to be always NULL.
If set deactivates creation and usage of the temporary table (in the
'table' member) and the Unique instance (in the 'tree' member) as well as
the calculation of the final value on the first call to
@c Item_sum::val_xxx(),
@c Item_avg::val_xxx(),
@c Item_count::val_xxx().
*/
CONST_NULL,
/**
Set to true if count distinct is on only const items. Distinct on a const
value will always be the constant itself. And count distinct of the same
would always be 1. Similar to CONST_NULL, it avoids creation of temporary
table and the Unique instance.
*/
CONST_NOT_NULL
} const_distinct;
/**
When feeding back the data in endup() from Unique/temp table back to
Item_sum::add() methods we must read the data from Unique (and not
recalculate the functions that are given as arguments to the aggregate
function.
This flag is to tell the arg_*() methods to take the data from the Unique
instead of calling the relevant val_..() method.
*/
bool use_distinct_values;
public:
Aggregator_distinct(Item_sum *sum)
: Aggregator(sum),
table(nullptr),
tmp_table_param(nullptr),
tree(nullptr),
const_distinct(NOT_CONST),
use_distinct_values(false) {}
~Aggregator_distinct() override;
Aggregator_type Aggrtype() override { return DISTINCT_AGGREGATOR; }
bool setup(THD *) override;
void clear() override;
bool add() override;
void endup() override;
my_decimal *arg_val_decimal(my_decimal *value) override;
double arg_val_real() override;
bool arg_is_null(bool use_null_value) override;
bool unique_walk_function(void *element);
static int composite_key_cmp(const void *arg, const void *a, const void *b);
};
/**
The pass-through aggregator.
Implements AGGFN (DISTINCT ..) by knowing it gets distinct data on input.
So it just pumps them back to the Item_sum descendant class.
*/
class Aggregator_simple : public Aggregator {
public:
Aggregator_simple(Item_sum *sum) : Aggregator(sum) {}
Aggregator_type Aggrtype() override { return Aggregator::SIMPLE_AGGREGATOR; }
bool setup(THD *thd) override { return item_sum->setup(thd); }
void clear() override { item_sum->clear(); }
bool add() override { return item_sum->add(); }
void endup() override {}
my_decimal *arg_val_decimal(my_decimal *value) override;
double arg_val_real() override;
bool arg_is_null(bool use_null_value) override;
};
class Item_sum_num : public Item_sum {
typedef Item_sum super;
protected:
/*
val_xxx() functions may be called several times during the execution of a
query. Derived classes that require extensive calculation in val_xxx()
maintain cache of aggregate value. This variable governs the validity of
that cache.
*/
bool is_evaluated;
public:
Item_sum_num(const POS &pos, Item *item_par, PT_window *window)
: Item_sum(pos, item_par, window), is_evaluated(false) {}
Item_sum_num(const POS &pos, PT_item_list *list, PT_window *w)
: Item_sum(pos, list, w), is_evaluated(false) {}
Item_sum_num(THD *thd, Item_sum_num *item)
: Item_sum(thd, item), is_evaluated(item->is_evaluated) {}
enum_field_types default_data_type() const override {
return MYSQL_TYPE_DOUBLE;
}
Item_sum_num(Item *item_par) : Item_sum(item_par), is_evaluated(false) {}
bool fix_fields(THD *, Item **) override;
longlong val_int() override {
assert(fixed);
return llrint_with_overflow_check(val_real()); /* Real as default */
}
String *val_str(String *str) override;
my_decimal *val_decimal(my_decimal *) override;
bool get_date(MYSQL_TIME *ltime, my_time_flags_t fuzzydate) override {
return get_date_from_numeric(ltime, fuzzydate); /* Decimal or real */
}
bool get_time(MYSQL_TIME *ltime) override {
return get_time_from_numeric(ltime); /* Decimal or real */
}
void reset_field() override;
};
class Item_sum_int : public Item_sum_num {
public:
Item_sum_int(const POS &pos, Item *item_par, PT_window *w)
: Item_sum_num(pos, item_par, w) {
set_data_type_longlong();
}
Item_sum_int(const POS &pos, PT_item_list *list, PT_window *w)
: Item_sum_num(pos, list, w) {
set_data_type_longlong();
}
Item_sum_int(THD *thd, Item_sum_int *item) : Item_sum_num(thd, item) {
set_data_type_longlong();