summaryrefslogtreecommitdiff
path: root/sql/item_windowfunc.h
blob: c038cb8d15f4bfcfe6b12c9076843b37bca25d49 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
/*
   Copyright (c) 2016, 2020, MariaDB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   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 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 */

#ifndef ITEM_WINDOWFUNC_INCLUDED
#define ITEM_WINDOWFUNC_INCLUDED

#include "item.h"

class Window_spec;


int test_if_group_changed(List<Cached_item> &list);


/* A wrapper around test_if_group_changed */
class Group_bound_tracker
{
public:

  Group_bound_tracker(THD *thd, SQL_I_List<ORDER> *list)
  {
    for (ORDER *curr = list->first; curr; curr=curr->next)
    {
        Cached_item *tmp= new_Cached_item(thd, curr->item[0], TRUE);
        group_fields.push_back(tmp);
    }
  }

  void init()
  {
    first_check= true;
  }

  /*
    Check if the current row is in a different group than the previous row
    this function was called for.
    XXX: Side-effect: The new row's group becomes the current row's group.

    Returns true if there is a change between the current_group and the cached
    value, or if it is the first check after a call to init.
  */
  bool check_if_next_group()
  {
    if (test_if_group_changed(group_fields) > -1 || first_check)
    {
      first_check= false;
      return true;
    }
    return false;
  }

  /*
    Check if the current row is in a different group than the previous row
    check_if_next_group was called for.

    Compares the groups without the additional side effect of updating the
    current cached values.
  */
  int compare_with_cache()
  {
    List_iterator<Cached_item> li(group_fields);
    Cached_item *ptr;
    int res;
    while ((ptr= li++))
    {
      if ((res= ptr->cmp_read_only()))
        return res;
    }
    return 0;
  }
  ~Group_bound_tracker()
  {
    group_fields.delete_elements();
  }

private:
  List<Cached_item> group_fields;
  /*
    During the first check_if_next_group, the list of cached_items is not
    initialized. The compare function will return that the items match if
    the field's value is the same as the Cached_item's default value (0).
    This flag makes sure that we always return true during the first check.

    XXX This is better to be implemented within test_if_group_changed, but
    since it is used in other parts of the codebase, we keep it here for now.
  */
   bool first_check;
};

/*
  ROW_NUMBER() OVER (...)

  @detail
  - This is a Window function (not just an aggregate)
  - It can be computed by doing one pass over select output, provided 
    the output is sorted according to the window definition.
*/

class Item_sum_row_number: public Item_sum_int
{
  longlong count;

public:

  Item_sum_row_number(THD *thd)
    : Item_sum_int(thd),  count(0) {}

  void clear()
  {
    count= 0;
  }

  bool add()
  {
    count++;
    return false;
  }

  void reset_field() { DBUG_ASSERT(0); }
  void update_field() {}

  enum Sumfunctype sum_func() const
  {
    return ROW_NUMBER_FUNC;
  }

  longlong val_int()
  {
    return count;
  }
  const char*func_name() const
  {
    return "row_number";
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_row_number>(thd, this); }
};


/*
  RANK() OVER (...) Windowing function

  @detail
  - This is a Window function (not just an aggregate)
  - It can be computed by doing one pass over select output, provided 
    the output is sorted according to the window definition.

  The function is defined as:

  "The rank of row R is defined as 1 (one) plus the number of rows that 
  precede R and are not peers of R"

  "This implies that if two or more rows are not distinct with respect to 
  the window ordering, then there will be one or more"
*/

class Item_sum_rank: public Item_sum_int
{
protected:
  longlong row_number; // just ROW_NUMBER()
  longlong cur_rank;   // current value

  Group_bound_tracker *peer_tracker;
public:

  Item_sum_rank(THD *thd) : Item_sum_int(thd), peer_tracker(NULL) {}

  void clear()
  {
    /* This is called on partition start */
    cur_rank= 1;
    row_number= 0;
  }

  bool add();

  longlong val_int()
  {
    return cur_rank;
  }

  void reset_field() { DBUG_ASSERT(0); }
  void update_field() {}

  enum Sumfunctype sum_func () const
  {
    return RANK_FUNC;
  }

  const char*func_name() const
  {
    return "rank";
  }

  void setup_window_func(THD *thd, Window_spec *window_spec);

  void cleanup()
  {
    if (peer_tracker)
    {
      delete peer_tracker;
      peer_tracker= NULL;
    }
    Item_sum_int::cleanup();
  }
  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_rank>(thd, this); }
};


/*
  DENSE_RANK() OVER (...) Windowing function

  @detail
  - This is a Window function (not just an aggregate)
  - It can be computed by doing one pass over select output, provided 
    the output is sorted according to the window definition.

  The function is defined as:

  "If DENSE_RANK is specified, then the rank of row R is defined as the 
  number of rows preceding and including R that are distinct with respect 
  to the window ordering"

  "This implies that there are no gaps in the sequential rank numbering of
  rows in each window partition."
*/


class Item_sum_dense_rank: public Item_sum_int
{
  longlong dense_rank;
  bool first_add;
  Group_bound_tracker *peer_tracker;
 public:
  /*
     XXX(cvicentiu) This class could potentially be implemented in the rank
     class, with a switch for the DENSE case.
  */
  void clear()
  {
    dense_rank= 0;
    first_add= true;
  }
  bool add();
  void reset_field() { DBUG_ASSERT(0); }
  void update_field() {}
  longlong val_int()
  {
    return dense_rank;
  }

  Item_sum_dense_rank(THD *thd)
    : Item_sum_int(thd), dense_rank(0), first_add(true), peer_tracker(NULL) {}
  enum Sumfunctype sum_func () const
  {
    return DENSE_RANK_FUNC;
  }

  const char*func_name() const
  {
    return "dense_rank";
  }

  void setup_window_func(THD *thd, Window_spec *window_spec);

  void cleanup()
  {
    if (peer_tracker)
    {
      delete peer_tracker;
      peer_tracker= NULL;
    }
    Item_sum_int::cleanup();
  }
  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_dense_rank>(thd, this); }
};

class Item_sum_hybrid_simple : public Item_sum_hybrid
{
 public:
  Item_sum_hybrid_simple(THD *thd, Item *arg):
   Item_sum_hybrid(thd, arg),
   value(NULL)
  { }

  Item_sum_hybrid_simple(THD *thd, Item *arg1, Item *arg2):
   Item_sum_hybrid(thd, arg1, arg2),
   value(NULL)
  { }

  bool add();
  bool fix_fields(THD *, Item **);
  bool fix_length_and_dec();
  void setup_hybrid(THD *thd, Item *item);
  double val_real();
  longlong val_int();
  my_decimal *val_decimal(my_decimal *);
  void reset_field();
  String *val_str(String *);
  bool val_native(THD *thd, Native *to);
  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate);
  const Type_handler *type_handler() const
  { return Type_handler_hybrid_field_type::type_handler(); }
  void update_field();
  Field *create_tmp_field(bool group, TABLE *table);
  void clear()
  {
    value->clear();
    null_value= 1;
  }

 private:
  Item_cache *value;
};

/*
   This item will remember the first value added to it. It will not update
   the value unless it is cleared.
*/
class Item_sum_first_value : public Item_sum_hybrid_simple
{
 public:
  Item_sum_first_value(THD* thd, Item* arg_expr) :
    Item_sum_hybrid_simple(thd, arg_expr) {}


  enum Sumfunctype sum_func () const
  {
    return FIRST_VALUE_FUNC;
  }

  const char*func_name() const
  {
    return "first_value";
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_first_value>(thd, this); }
};

/*
   This item will remember the last value added to it.

   This item does not support removal, and can be cleared only by calling
   clear().
*/
class Item_sum_last_value : public Item_sum_hybrid_simple
{
 public:
  Item_sum_last_value(THD* thd, Item* arg_expr) :
    Item_sum_hybrid_simple(thd, arg_expr) {}

  enum Sumfunctype sum_func() const
  {
    return LAST_VALUE_FUNC;
  }

  const char*func_name() const
  {
    return "last_value";
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_last_value>(thd, this); }
};

class Item_sum_nth_value : public Item_sum_hybrid_simple
{
 public:
  Item_sum_nth_value(THD *thd, Item *arg_expr, Item* offset_expr) :
    Item_sum_hybrid_simple(thd, arg_expr, offset_expr) {}

  enum Sumfunctype sum_func() const
  {
    return NTH_VALUE_FUNC;
  }

  const char*func_name() const
  {
    return "nth_value";
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_nth_value>(thd, this); }
};

class Item_sum_lead : public Item_sum_hybrid_simple
{
 public:
  Item_sum_lead(THD *thd, Item *arg_expr, Item* offset_expr) :
    Item_sum_hybrid_simple(thd, arg_expr, offset_expr) {}

  enum Sumfunctype sum_func() const
  {
    return LEAD_FUNC;
  }

  const char*func_name() const
  {
    return "lead";
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_lead>(thd, this); }
};

class Item_sum_lag : public Item_sum_hybrid_simple
{
 public:
  Item_sum_lag(THD *thd, Item *arg_expr, Item* offset_expr) :
    Item_sum_hybrid_simple(thd, arg_expr, offset_expr) {}

  enum Sumfunctype sum_func() const
  {
    return LAG_FUNC;
  }

  const char*func_name() const
  {
    return "lag";
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_lag>(thd, this); }
};


class Partition_row_count
{
public:
  Partition_row_count() :partition_row_count_(0) { }
  void set_partition_row_count(ulonglong count)
  {
    partition_row_count_ = count;
  }
  double calc_val_real(bool *null_value,
                       ulonglong current_row_count)
  {
    if ((*null_value= (partition_row_count_ == 0)))
      return 0;
    return static_cast<double>(current_row_count) / partition_row_count_;
  }
protected:
  longlong get_row_count() { return partition_row_count_; }
  ulonglong partition_row_count_;
};


class Current_row_count
{
public:
  Current_row_count() :current_row_count_(0) { }
protected:
  ulonglong get_row_number() { return current_row_count_ ; }
  ulonglong current_row_count_;
};


/*
  @detail
  "The relative rank of a row R is defined as (RK-1)/(NR-1), where RK is 
  defined to be the RANK of R and NR is defined to be the number of rows in
  the window partition of R."

  Computation of this function requires two passes:
  - First pass to find #rows in the partition
    This is held within the row_count context.
  - Second pass to compute rank of current row and the value of the function
*/
class Item_sum_percent_rank: public Item_sum_double,
                             public Partition_row_count
{
 public:
  Item_sum_percent_rank(THD *thd)
    : Item_sum_double(thd), cur_rank(1), peer_tracker(NULL) {}

  longlong val_int()
  {
   /*
      Percent rank is a real value so calling the integer value should never
      happen. It makes no sense as it gets truncated to either 0 or 1.
   */
    DBUG_ASSERT(0);
    return 0;
  }

  double val_real()
  {
   /*
     We can not get the real value without knowing the number of rows
     in the partition. Don't divide by 0.
   */
   ulonglong partition_rows = get_row_count();
   null_value= partition_rows > 0 ? false : true;

   return partition_rows > 1 ?
             static_cast<double>(cur_rank - 1) / (partition_rows - 1) : 0;
  }

  enum Sumfunctype sum_func () const
  {
    return PERCENT_RANK_FUNC;
  }

  const char*func_name() const
  {
    return "percent_rank";
  }

  void update_field() {}

  void clear()
  {
    cur_rank= 1;
    row_number= 0;
  }
  bool add();
  const Type_handler *type_handler() const { return &type_handler_double; }

  bool fix_length_and_dec()
  {
    decimals = 10;  // TODO-cvicentiu find out how many decimals the standard
                    // requires.
    return FALSE;
  }

  void setup_window_func(THD *thd, Window_spec *window_spec);

  void reset_field() { DBUG_ASSERT(0); }

  void set_partition_row_count(ulonglong count)
  {
    Partition_row_count::set_partition_row_count(count);
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_percent_rank>(thd, this); }

 private:
  longlong cur_rank;   // Current rank of the current row.
  longlong row_number; // Value if this were ROW_NUMBER() function.

  Group_bound_tracker *peer_tracker;

  void cleanup()
  {
    if (peer_tracker)
    {
      delete peer_tracker;
      peer_tracker= NULL;
    }
    Item_sum_num::cleanup();
  }
};




/*
  @detail
  "The relative rank of a row R is defined as NP/NR, where 
  - NP is defined to be the number of rows preceding or peer with R in the 
    window ordering of the window partition of R
  - NR is defined to be the number of rows in the window partition of R.

  Just like with Item_sum_percent_rank, computation of this function requires
  two passes.
*/

class Item_sum_cume_dist: public Item_sum_double,
                          public Partition_row_count,
                          public Current_row_count
{
 public:
  Item_sum_cume_dist(THD *thd) :Item_sum_double(thd) { }
  Item_sum_cume_dist(THD *thd, Item *arg) :Item_sum_double(thd, arg) { }

  double val_real()
  {
    return calc_val_real(&null_value, current_row_count_);
  }

  bool add()
  {
    current_row_count_++;
    return false;
  }

  enum Sumfunctype sum_func() const
  {
    return CUME_DIST_FUNC;
  }

  void clear()
  {
    current_row_count_= 0;
    partition_row_count_= 0;
  }

  const char*func_name() const
  {
    return "cume_dist";
  }

  void update_field() {}
  const Type_handler *type_handler() const { return &type_handler_double; }

  bool fix_length_and_dec()
  {
    decimals = 10;  // TODO-cvicentiu find out how many decimals the standard
                    // requires.
    return FALSE;
  }
  
  void reset_field() { DBUG_ASSERT(0); }

  void set_partition_row_count(ulonglong count)
  {
    Partition_row_count::set_partition_row_count(count);
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_cume_dist>(thd, this); }

};

class Item_sum_ntile : public Item_sum_int,
                       public Partition_row_count,
                       public Current_row_count
{
 public:
  Item_sum_ntile(THD* thd, Item* num_quantiles_expr) :
    Item_sum_int(thd, num_quantiles_expr), n_old_val_(0)
  { }

  longlong val_int()
  {
    if (get_row_count() == 0)
    {
      null_value= true;
      return 0;
    }

    longlong num_quantiles= get_num_quantiles();

    if (num_quantiles <= 0 || 
      (static_cast<ulonglong>(num_quantiles) != n_old_val_ && n_old_val_ > 0))
    {
      my_error(ER_INVALID_NTILE_ARGUMENT, MYF(0));
      return true;
    }
    n_old_val_= static_cast<ulonglong>(num_quantiles);
    null_value= false;
    ulonglong quantile_size = get_row_count() / num_quantiles;
    ulonglong extra_rows = get_row_count() - quantile_size * num_quantiles;

    if (current_row_count_ <= extra_rows * (quantile_size + 1))
      return (current_row_count_ - 1) / (quantile_size + 1) + 1;

    return (current_row_count_ - 1 - extra_rows) / quantile_size + 1;
  }

  bool add()
  {
    current_row_count_++;
    return false;
  }

  enum Sumfunctype sum_func() const
  {
    return NTILE_FUNC;
  }

  void clear()
  {
    current_row_count_= 0;
    partition_row_count_= 0;
    n_old_val_= 0;
  }

  const char*func_name() const
  {
    return "ntile";
  }

  void update_field() {}

  void reset_field() { DBUG_ASSERT(0); }

  void set_partition_row_count(ulonglong count)
  {
    Partition_row_count::set_partition_row_count(count);
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_ntile>(thd, this); }

 private:
  longlong get_num_quantiles() { return args[0]->val_int(); }
  ulonglong n_old_val_;
};

class Item_sum_percentile_disc : public Item_sum_num,
                                 public Type_handler_hybrid_field_type,
                                 public Partition_row_count,
                                 public Current_row_count
{
public:
  Item_sum_percentile_disc(THD *thd, Item* arg) : Item_sum_num(thd, arg),
                           Type_handler_hybrid_field_type(&type_handler_longlong),
                           value(NULL), val_calculated(FALSE), first_call(TRUE),
                           prev_value(0), order_item(NULL){}

  double val_real()
  {
    if (get_row_count() == 0 || get_arg(0)->is_null())
    {
      null_value= true;
      return 0;
    }
    null_value= false;
    return value->val_real();
  }

  longlong val_int()
  {
    if (get_row_count() == 0 || get_arg(0)->is_null())
    {
      null_value= true;
      return 0;
    }
    null_value= false;
    return value->val_int();
  }

  my_decimal* val_decimal(my_decimal* dec)
  {
    if (get_row_count() == 0 || get_arg(0)->is_null())
    {
      null_value= true;
      return 0;
    }
    null_value= false;
    return value->val_decimal(dec);
  }

  String* val_str(String *str)
  {
    if (get_row_count() == 0 || get_arg(0)->is_null())
    {
      null_value= true;
      return 0;
    }
    null_value= false;
    return value->val_str(str);
  }

  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate)
  {
    if (get_row_count() == 0 || get_arg(0)->is_null())
    {
      null_value= true;
      return 0;
    }
    null_value= false;
    return value->get_date(thd, ltime, fuzzydate);
  }

  bool add()
  {
    Item *arg= get_arg(0);
    if (arg->is_null())
      return false;

    if (first_call)
    {
      prev_value= arg->val_real();
      if (prev_value > 1 || prev_value < 0)
      {
        my_error(ER_ARGUMENT_OUT_OF_RANGE, MYF(0), func_name());
        return true;
      }
      first_call= false;
    }

    double arg_val= arg->val_real();

    if (prev_value != arg_val)
    {
      my_error(ER_ARGUMENT_NOT_CONSTANT, MYF(0), func_name());
      return true;
    }

    if (val_calculated)
      return false;

    value->store(order_item);
    value->cache_value();
    if (value->null_value)
      return false;

    current_row_count_++;
    double val= calc_val_real(&null_value, current_row_count_);

    if (val >= prev_value && !val_calculated)
      val_calculated= true;
    return false;
  }

  enum Sumfunctype sum_func() const
  {
    return PERCENTILE_DISC_FUNC;
  }

  void clear()
  {
    val_calculated= false;
    first_call= true;
    value->clear();
    partition_row_count_= 0;
    current_row_count_= 0;
  }

  const char*func_name() const
  {
    return "percentile_disc";
  }

  void update_field() {}
  const Type_handler *type_handler() const
  {return Type_handler_hybrid_field_type::type_handler();}

  bool fix_length_and_dec()
  {
    decimals = 10;  // TODO-cvicentiu find out how many decimals the standard
                    // requires.
    return FALSE;
  }

  void reset_field() { DBUG_ASSERT(0); }

  void set_partition_row_count(ulonglong count)
  {
    Partition_row_count::set_partition_row_count(count);
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_percentile_disc>(thd, this); }
  void setup_window_func(THD *thd, Window_spec *window_spec);
  void setup_hybrid(THD *thd, Item *item);
  bool fix_fields(THD *thd, Item **ref);

private:
  Item_cache *value;
  bool val_calculated;
  bool first_call;
  double prev_value;
  Item *order_item;
};

class Item_sum_percentile_cont : public Item_sum_double,
                                 public Partition_row_count,
                                 public Current_row_count
{
public:
  Item_sum_percentile_cont(THD *thd, Item* arg) : Item_sum_double(thd, arg),
                           floor_value(NULL), ceil_value(NULL), first_call(TRUE),prev_value(0),
                           ceil_val_calculated(FALSE), floor_val_calculated(FALSE), order_item(NULL){}

  double val_real()
  {
    if (get_row_count() == 0 || get_arg(0)->is_null())
    {
      null_value= true;
      return 0;
    }
    null_value= false;
    double val= 1 + prev_value * (get_row_count()-1);

    /*
      Applying the formula to get the value
      If (CRN = FRN = RN) then the result is (value of expression from row at RN)
      Otherwise the result is
      (CRN - RN) * (value of expression for row at FRN) +
      (RN - FRN) * (value of expression for row at CRN)
    */

    if(ceil(val) == floor(val))
      return floor_value->val_real();

    double ret_val=  ((val - floor(val)) * ceil_value->val_real()) +
                  ((ceil(val) - val) * floor_value->val_real());

    return ret_val;
  }

  bool add()
  {
    Item *arg= get_arg(0);
    if (arg->is_null())
      return false;

    if (first_call)
    {
      first_call= false;
      prev_value= arg->val_real();
      if (prev_value > 1 || prev_value < 0)
      {
        my_error(ER_ARGUMENT_OUT_OF_RANGE, MYF(0), func_name());
        return true;
      }
    }

    double arg_val= arg->val_real();
    if (prev_value != arg_val)
    {
      my_error(ER_ARGUMENT_NOT_CONSTANT, MYF(0), func_name());
      return true;
    }

    if (!floor_val_calculated)
    {
      floor_value->store(order_item);
      floor_value->cache_value();
      if (floor_value->null_value)
        return false;
    }
    if (floor_val_calculated && !ceil_val_calculated)
    {
      ceil_value->store(order_item);
      ceil_value->cache_value();
      if (ceil_value->null_value)
        return false;
    }

    current_row_count_++;
    double val= 1 + prev_value * (get_row_count()-1);

    if (!floor_val_calculated && get_row_number() == floor(val))
      floor_val_calculated= true;

    if (!ceil_val_calculated && get_row_number() == ceil(val))
      ceil_val_calculated= true;
    return false;
  }

  enum Sumfunctype sum_func() const
  {
    return PERCENTILE_CONT_FUNC;
  }

  void clear()
  {
    first_call= true;
    floor_value->clear();
    ceil_value->clear();
    floor_val_calculated= false;
    ceil_val_calculated= false;
    partition_row_count_= 0;
    current_row_count_= 0;
  }

  const char*func_name() const
  {
    return "percentile_cont";
  }
  void update_field() {}

  bool fix_length_and_dec()
  {
    decimals = 10;  // TODO-cvicentiu find out how many decimals the standard
                    // requires.
    return FALSE;
  }

  void reset_field() { DBUG_ASSERT(0); }

  void set_partition_row_count(ulonglong count)
  {
    Partition_row_count::set_partition_row_count(count);
  }

  Item *get_copy(THD *thd)
  { return get_item_copy<Item_sum_percentile_cont>(thd, this); }
  void setup_window_func(THD *thd, Window_spec *window_spec);
  void setup_hybrid(THD *thd, Item *item);
  bool fix_fields(THD *thd, Item **ref);

private:
  Item_cache *floor_value;
  Item_cache *ceil_value;
  bool first_call;
  double prev_value;
  bool ceil_val_calculated;
  bool floor_val_calculated;
  Item *order_item;
};




class Item_window_func : public Item_func_or_sum
{
  /* Window function parameters as we've got them from the parser */
public:
  LEX_CSTRING *window_name;
public:
  Window_spec *window_spec;
  
public:
  Item_window_func(THD *thd, Item_sum *win_func, LEX_CSTRING *win_name)
    : Item_func_or_sum(thd, (Item *) win_func),
      window_name(win_name), window_spec(NULL), 
      force_return_blank(true),
      read_value_from_result_field(false) {}

  Item_window_func(THD *thd, Item_sum *win_func, Window_spec *win_spec)
    : Item_func_or_sum(thd, (Item *) win_func), 
      window_name(NULL), window_spec(win_spec), 
      force_return_blank(true),
      read_value_from_result_field(false) {}

  Item_sum *window_func() const { return (Item_sum *) args[0]; }

  void update_used_tables();

  /*
    This is used by filesort to mark the columns it needs to read (because they
    participate in the sort criteria and/or row retrieval. Window functions can
    only be used in sort criteria).

    Sorting by window function value is only done after the window functions
    have been computed. In that case, window function will need to read its
    temp.table field. In order to allow that, mark that field in the read_set.
  */
  bool register_field_in_read_map(void *arg)
  {
    TABLE *table= (TABLE*) arg;
    if (result_field && (result_field->table == table || !table))
    {
      bitmap_set_bit(result_field->table->read_set, result_field->field_index);
    }
    return 0;
  }

  bool is_frame_prohibited() const
  {
    switch (window_func()->sum_func()) {
    case Item_sum::ROW_NUMBER_FUNC:
    case Item_sum::RANK_FUNC:
    case Item_sum::DENSE_RANK_FUNC:
    case Item_sum::PERCENT_RANK_FUNC:
    case Item_sum::CUME_DIST_FUNC:
    case Item_sum::NTILE_FUNC:
    case Item_sum::PERCENTILE_CONT_FUNC:
    case Item_sum::PERCENTILE_DISC_FUNC:
      return true;
    default: 
      return false;
    }
  }

  bool requires_special_cursors() const
  {
    switch (window_func()->sum_func()) {
    case Item_sum::FIRST_VALUE_FUNC:
    case Item_sum::LAST_VALUE_FUNC:
    case Item_sum::NTH_VALUE_FUNC:
    case Item_sum::LAG_FUNC:
    case Item_sum::LEAD_FUNC:
      return true;
    default:
      return false;
    }
  }

  bool requires_partition_size() const
  {
    switch (window_func()->sum_func()) {
    case Item_sum::PERCENT_RANK_FUNC:
    case Item_sum::CUME_DIST_FUNC:
    case Item_sum::NTILE_FUNC:
    case Item_sum::PERCENTILE_CONT_FUNC:
    case Item_sum::PERCENTILE_DISC_FUNC:
      return true;
    default:
      return false;
    }
  }

  bool requires_peer_size() const
  {
    switch (window_func()->sum_func()) {
    case Item_sum::CUME_DIST_FUNC:
      return true;
    default:
      return false;
    }
  }

  bool is_order_list_mandatory() const
  {
    switch (window_func()->sum_func()) {
    case Item_sum::RANK_FUNC:
    case Item_sum::DENSE_RANK_FUNC:
    case Item_sum::PERCENT_RANK_FUNC:
    case Item_sum::CUME_DIST_FUNC:
    case Item_sum::LAG_FUNC:
    case Item_sum::LEAD_FUNC:
    case Item_sum::PERCENTILE_CONT_FUNC:
    case Item_sum::PERCENTILE_DISC_FUNC:
      return true;
    default: 
      return false;
    }
  }  

  bool only_single_element_order_list() const
  {
    switch (window_func()->sum_func()){
    case Item_sum::PERCENTILE_CONT_FUNC:
    case Item_sum::PERCENTILE_DISC_FUNC:
      return true;
    default:
      return false;
    }
  }

  bool check_result_type_of_order_item();



  /*
    Computation functions.
    TODO: consoder merging these with class Group_bound_tracker.
  */
  void setup_partition_border_check(THD *thd);

  const Type_handler *type_handler() const
  {
    return ((Item_sum *) args[0])->type_handler();
  }
  enum Item::Type type() const { return Item::WINDOW_FUNC_ITEM; }

private:
  /* 
    Window functions are very special functions, so val_() methods have
    special meaning for them:

    - Phase#1, "Initial" we run the join and put its result into temporary 
      table. For window functions, we write the default value (NULL?) as 
      a placeholder.
      
    - Phase#2: "Computation": executor does the scan in {PARTITION, ORDER BY} 
      order of this window function. It calls appropriate methods to inform 
      the window function about rows entering/leaving the window. 
      It calls window_func()->val_int() so that current window function value
      can be saved and stored in the temp.table.

    - Phase#3: "Retrieval" the temporary table is read and passed to query 
      output. However, Item_window_func still remains in the select list,
      so item_windowfunc->val_int() will be called.
      During Phase#3, read_value_from_result_field= true.
  */
  bool force_return_blank;
  bool read_value_from_result_field;
  void print_for_percentile_functions(String *str, enum_query_type query_type);

public:
  void set_phase_to_initial()
  {
    force_return_blank= true;
    read_value_from_result_field= false;
  }
  void set_phase_to_computation()
  {
    force_return_blank= false;
    read_value_from_result_field= false;
  }
  void set_phase_to_retrieval()
  {
    force_return_blank= false;
    read_value_from_result_field= true;
  }

  bool is_null()
  {
    if (force_return_blank)
      return true;

    if (read_value_from_result_field)
      return result_field->is_null();

    return window_func()->is_null();
  }

  double val_real() 
  {
    double res;
    if (force_return_blank)
    {
      res= 0.0;
      null_value= true;
    }
    else if (read_value_from_result_field)
    {
      res= result_field->val_real();
      null_value= result_field->is_null();
    }
    else
    {
      res= window_func()->val_real();
      null_value= window_func()->null_value;
    }
    return res;
  }

  longlong val_int()
  {
    longlong res;
    if (force_return_blank)
    {
      res= 0;
      null_value= true;
    }
    else if (read_value_from_result_field)
    {
      res= result_field->val_int();
      null_value= result_field->is_null();
    }
    else
    {
      res= window_func()->val_int();
      null_value= window_func()->null_value;
    }
    return res;
  }

  String* val_str(String* str)
  {
    String *res;
    if (force_return_blank)
    {
      null_value= true;
      res= NULL;
    }
    else if (read_value_from_result_field)
    {
      if ((null_value= result_field->is_null()))
        res= NULL;
      else
        res= result_field->val_str(str);
    }
    else
    {
      res= window_func()->val_str(str);
      null_value= window_func()->null_value;
    }
    return res;
  }

  bool val_native(THD *thd, Native *to)
  {
    if (force_return_blank)
      return null_value= true;
    if (read_value_from_result_field)
      return val_native_from_field(result_field, to);
    return val_native_from_item(thd, window_func(), to);
  }

  my_decimal* val_decimal(my_decimal* dec)
  {
    my_decimal *res;
    if (force_return_blank)
    {
      null_value= true;
      res= NULL;
    }
    else if (read_value_from_result_field)
    {
      if ((null_value= result_field->is_null()))
        res= NULL;
      else
        res= result_field->val_decimal(dec);
    }
    else
    {
      res= window_func()->val_decimal(dec);
      null_value= window_func()->null_value;
    }
    return res;
  }

  bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate)
  {
    bool res;
    if (force_return_blank)
    {
      null_value= true;
      res= true;
    }
    else if (read_value_from_result_field)
    {
      if ((null_value= result_field->is_null()))
        res= true;
      else
        res= result_field->get_date(ltime, fuzzydate);
    }
    else
    {
      res= window_func()->get_date(thd, ltime, fuzzydate);
      null_value= window_func()->null_value;
    }
    return res;
  }

  void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array,
                              List<Item> &fields, uint flags);

  bool fix_length_and_dec()
  {
    Type_std_attributes::set(window_func());
    return FALSE;
  }

  const char* func_name() const { return "WF"; }

  bool fix_fields(THD *thd, Item **ref);

  bool resolve_window_name(THD *thd);
  
  void print(String *str, enum_query_type query_type);

 Item *get_copy(THD *thd) { return 0; }

};

#endif /* ITEM_WINDOWFUNC_INCLUDED */