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
|
#ifndef __S3SELECT_FUNCTIONS__
#define __S3SELECT_FUNCTIONS__
#include "s3select_oper.h"
#define BOOST_BIND_ACTION_PARAM( push_name ,param ) boost::bind( &push_name::operator(), g_ ## push_name , _1 ,_2, param)
namespace s3selectEngine
{
struct push_2dig
{
void operator()(const char* a, const char* b, uint32_t* n) const
{
*n = ((char)(*a) - 48) *10 + ((char)*(a+1)-48) ;
}
};
static push_2dig g_push_2dig;
struct push_4dig
{
void operator()(const char* a, const char* b, uint32_t* n) const
{
*n = ((char)(*a) - 48) *1000 + ((char)*(a+1)-48)*100 + ((char)*(a+2)-48)*10 + ((char)*(a+3)-48);
}
};
static push_4dig g_push_4dig;
enum class s3select_func_En_t {ADD,
SUM,
MIN,
MAX,
COUNT,
TO_INT,
TO_FLOAT,
TO_TIMESTAMP,
SUBSTR,
EXTRACT,
DATE_ADD,
DATE_DIFF,
UTCNOW
};
class s3select_functions : public __clt_allocator
{
private:
using FunctionLibrary = std::map<std::string, s3select_func_En_t>;
const FunctionLibrary m_functions_library =
{
{"add", s3select_func_En_t::ADD},
{"sum", s3select_func_En_t::SUM},
{"count", s3select_func_En_t::COUNT},
{"min", s3select_func_En_t::MIN},
{"max", s3select_func_En_t::MAX},
{"int", s3select_func_En_t::TO_INT},
{"float", s3select_func_En_t::TO_FLOAT},
{"substr", s3select_func_En_t::SUBSTR},
{"timestamp", s3select_func_En_t::TO_TIMESTAMP},
{"extract", s3select_func_En_t::EXTRACT},
{"dateadd", s3select_func_En_t::DATE_ADD},
{"datediff", s3select_func_En_t::DATE_DIFF},
{"utcnow", s3select_func_En_t::UTCNOW}
};
public:
base_function* create(std::string fn_name);
};
class __function : public base_statement
{
private:
std::vector<base_statement*> arguments;
std::string name;
base_function* m_func_impl;
s3select_functions* m_s3select_functions;
variable m_result;
void _resolve_name()
{
if (m_func_impl)
{
return;
}
base_function* f = m_s3select_functions->create(name);
if (!f)
{
throw base_s3select_exception("function not found", base_s3select_exception::s3select_exp_en_t::FATAL); //should abort query
}
m_func_impl = f;
}
public:
virtual void traverse_and_apply(scratch_area* sa, projection_alias* pa)
{
m_scratch = sa;
m_aliases = pa;
for (base_statement* ba : arguments)
{
ba->traverse_and_apply(sa, pa);
}
}
virtual bool is_aggregate() // TODO under semantic flow
{
_resolve_name();
return m_func_impl->is_aggregate();
}
virtual bool semantic()
{
return true;
}
__function(const char* fname, s3select_functions* s3f) : name(fname), m_func_impl(0), m_s3select_functions(s3f) {}
virtual value& eval()
{
_resolve_name();
if (is_last_call == false)
{
(*m_func_impl)(&arguments, &m_result);
}
else
{
(*m_func_impl).get_aggregate_result(&m_result);
}
return m_result.get_value();
}
virtual std::string print(int ident)
{
return std::string(0);
}
void push_argument(base_statement* arg)
{
arguments.push_back(arg);
}
std::vector<base_statement*> get_arguments()
{
return arguments;
}
virtual ~__function()
{
arguments.clear();
}
};
/*
s3-select function defintions
*/
struct _fn_add : public base_function
{
value var_result;
bool operator()(std::vector<base_statement*>* args, variable* result)
{
std::vector<base_statement*>::iterator iter = args->begin();
base_statement* x = *iter;
iter++;
base_statement* y = *iter;
var_result = x->eval() + y->eval();
*result = var_result;
return true;
}
};
struct _fn_sum : public base_function
{
value sum;
_fn_sum() : sum(0)
{
aggregate = true;
}
bool operator()(std::vector<base_statement*>* args, variable* result)
{
std::vector<base_statement*>::iterator iter = args->begin();
base_statement* x = *iter;
try
{
sum = sum + x->eval();
}
catch (base_s3select_exception& e)
{
std::cout << "illegal value for aggregation(sum). skipping." << std::endl;
if (e.severity() == base_s3select_exception::s3select_exp_en_t::FATAL)
{
throw;
}
}
return true;
}
virtual void get_aggregate_result(variable* result)
{
*result = sum ;
}
};
struct _fn_count : public base_function
{
int64_t count;
_fn_count():count(0)
{
aggregate=true;
}
bool operator()(std::vector<base_statement*>* args, variable* result)
{
count += 1;
return true;
}
virtual void get_aggregate_result(variable* result)
{
result->set_value(count);
}
};
struct _fn_min : public base_function
{
value min;
_fn_min():min(__INT64_MAX__)
{
aggregate=true;
}
bool operator()(std::vector<base_statement*>* args, variable* result)
{
std::vector<base_statement*>::iterator iter = args->begin();
base_statement* x = *iter;
if(min > x->eval())
{
min=x->eval();
}
return true;
}
virtual void get_aggregate_result(variable* result)
{
*result = min;
}
};
struct _fn_max : public base_function
{
value max;
_fn_max():max(-__INT64_MAX__)
{
aggregate=true;
}
bool operator()(std::vector<base_statement*>* args, variable* result)
{
std::vector<base_statement*>::iterator iter = args->begin();
base_statement* x = *iter;
if(max < x->eval())
{
max=x->eval();
}
return true;
}
virtual void get_aggregate_result(variable* result)
{
*result = max;
}
};
struct _fn_to_int : public base_function
{
value var_result;
value func_arg;
bool operator()(std::vector<base_statement*>* args, variable* result)
{
char* perr;
int64_t i=0;
func_arg = (*args->begin())->eval();
if (func_arg.type == value::value_En_t::STRING)
{
i = strtol(func_arg.str(), &perr, 10) ; //TODO check error before constructor
}
else if (func_arg.type == value::value_En_t::FLOAT)
{
i = func_arg.dbl();
}
else
{
i = func_arg.i64();
}
var_result = i ;
*result = var_result;
return true;
}
};
struct _fn_to_float : public base_function
{
value var_result;
value v_from;
bool operator()(std::vector<base_statement*>* args, variable* result)
{
char* perr;
double d=0;
value v = (*args->begin())->eval();
if (v.type == value::value_En_t::STRING)
{
d = strtod(v.str(), &perr) ; //TODO check error before constructor
}
else if (v.type == value::value_En_t::FLOAT)
{
d = v.dbl();
}
else
{
d = v.i64();
}
var_result = d;
*result = var_result;
return true;
}
};
struct _fn_to_timestamp : public base_function
{
bsc::rule<> separator = bsc::ch_p(":") | bsc::ch_p("-");
uint32_t yr = 1700, mo = 1, dy = 1;
bsc::rule<> dig4 = bsc::lexeme_d[bsc::digit_p >> bsc::digit_p >> bsc::digit_p >> bsc::digit_p];
bsc::rule<> dig2 = bsc::lexeme_d[bsc::digit_p >> bsc::digit_p];
bsc::rule<> d_yyyymmdd_dig = ((dig4[BOOST_BIND_ACTION_PARAM(push_4dig, &yr)]) >> *(separator)
>> (dig2[BOOST_BIND_ACTION_PARAM(push_2dig, &mo)]) >> *(separator)
>> (dig2[BOOST_BIND_ACTION_PARAM(push_2dig, &dy)]) >> *(separator));
uint32_t hr = 0, mn = 0, sc = 0;
bsc::rule<> d_time_dig = ((dig2[BOOST_BIND_ACTION_PARAM(push_2dig, &hr)]) >> *(separator)
>> (dig2[BOOST_BIND_ACTION_PARAM(push_2dig, &mn)]) >> *(separator)
>> (dig2[BOOST_BIND_ACTION_PARAM(push_2dig, &sc)]) >> *(separator));
boost::posix_time::ptime new_ptime;
value v_str;
bool datetime_validation()
{
//TODO temporary , should check for leap year
if(yr<1700 || yr>2050)
{
return false;
}
if (mo<1 || mo>12)
{
return false;
}
if (dy<1 || dy>31)
{
return false;
}
if (hr>23)
{
return false;
}
if (dy>59)
{
return false;
}
if (sc>59)
{
return false;
}
return true;
}
bool operator()(std::vector<base_statement*>* args, variable* result)
{
hr = 0;
mn = 0;
sc = 0;
std::vector<base_statement*>::iterator iter = args->begin();
int args_size = args->size();
if (args_size != 1)
{
throw base_s3select_exception("to_timestamp should have one parameter");
}
base_statement* str = *iter;
v_str = str->eval();
if (v_str.type != value::value_En_t::STRING)
{
throw base_s3select_exception("to_timestamp first argument must be string"); //can skip current row
}
bsc::parse_info<> info_dig = bsc::parse(v_str.str(), d_yyyymmdd_dig >> *(separator) >> d_time_dig);
if(datetime_validation()==false or !info_dig.full)
{
throw base_s3select_exception("input date-time is illegal");
}
new_ptime = boost::posix_time::ptime(boost::gregorian::date(yr, mo, dy),
boost::posix_time::hours(hr) + boost::posix_time::minutes(mn) + boost::posix_time::seconds(sc));
result->set_value(&new_ptime);
return true;
}
};
struct _fn_extact_from_timestamp : public base_function
{
boost::posix_time::ptime new_ptime;
value val_date_part;
bool operator()(std::vector<base_statement*>* args, variable* result)
{
std::vector<base_statement*>::iterator iter = args->begin();
int args_size = args->size();
if (args_size < 2)
{
throw base_s3select_exception("to_timestamp should have 2 parameters");
}
base_statement* date_part = *iter;
val_date_part = date_part->eval();//TODO could be done once?
if(val_date_part.is_string()== false)
{
throw base_s3select_exception("first parameter should be string");
}
iter++;
base_statement* ts = *iter;
if(ts->eval().is_timestamp()== false)
{
throw base_s3select_exception("second parameter is not timestamp");
}
new_ptime = *ts->eval().timestamp();
if( strcmp(val_date_part.str(), "year")==0 )
{
result->set_value( (int64_t)new_ptime.date().year() );
}
else if( strcmp(val_date_part.str(), "month")==0 )
{
result->set_value( (int64_t)new_ptime.date().month() );
}
else if( strcmp(val_date_part.str(), "day")==0 )
{
result->set_value( (int64_t)new_ptime.date().day_of_year() );
}
else if( strcmp(val_date_part.str(), "week")==0 )
{
result->set_value( (int64_t)new_ptime.date().week_number() );
}
else
{
throw base_s3select_exception(std::string( val_date_part.str() + std::string(" is not supported ") ).c_str() );
}
return true;
}
};
struct _fn_diff_timestamp : public base_function
{
value val_date_part;
value val_dt1;
value val_dt2;
bool operator()(std::vector<base_statement*>* args, variable* result)
{
std::vector<base_statement*>::iterator iter = args->begin();
int args_size = args->size();
if (args_size < 3)
{
throw base_s3select_exception("datediff need 3 parameters");
}
base_statement* date_part = *iter;
val_date_part = date_part->eval();
iter++;
base_statement* dt1_param = *iter;
val_dt1 = dt1_param->eval();
if (val_dt1.is_timestamp() == false)
{
throw base_s3select_exception("second parameter should be timestamp");
}
iter++;
base_statement* dt2_param = *iter;
val_dt2 = dt2_param->eval();
if (val_dt2.is_timestamp() == false)
{
throw base_s3select_exception("third parameter should be timestamp");
}
if (strcmp(val_date_part.str(), "year") == 0)
{
int64_t yr = val_dt2.timestamp()->date().year() - val_dt1.timestamp()->date().year() ;
result->set_value( yr );
}
else if (strcmp(val_date_part.str(), "month") == 0)
{
int64_t yr = val_dt2.timestamp()->date().year() - val_dt1.timestamp()->date().year() ;
int64_t mon = val_dt2.timestamp()->date().month() - val_dt1.timestamp()->date().month() ;
result->set_value( yr*12 + mon );
}
else if (strcmp(val_date_part.str(), "day") == 0)
{
boost::gregorian::date_period dp =
boost::gregorian::date_period( val_dt1.timestamp()->date(), val_dt2.timestamp()->date());
result->set_value( dp.length().days() );
}
else if (strcmp(val_date_part.str(), "hours") == 0)
{
boost::posix_time::time_duration td_res = (*val_dt2.timestamp() - *val_dt1.timestamp());
result->set_value( td_res.hours());
}
else
{
throw base_s3select_exception("first parameter should be string: year,month,hours,day");
}
return true;
}
};
struct _fn_add_to_timestamp : public base_function
{
boost::posix_time::ptime new_ptime;
value val_date_part;
value val_quantity;
value val_timestamp;
bool operator()(std::vector<base_statement*>* args, variable* result)
{
std::vector<base_statement*>::iterator iter = args->begin();
int args_size = args->size();
if (args_size < 3)
{
throw base_s3select_exception("add_to_timestamp should have 3 parameters");
}
base_statement* date_part = *iter;
val_date_part = date_part->eval();//TODO could be done once?
if(val_date_part.is_string()== false)
{
throw base_s3select_exception("first parameter should be string");
}
iter++;
base_statement* quan = *iter;
val_quantity = quan->eval();
if (val_quantity.is_number() == false)
{
throw base_s3select_exception("second parameter should be number"); //TODO what about double?
}
iter++;
base_statement* ts = *iter;
val_timestamp = ts->eval();
if(val_timestamp.is_timestamp() == false)
{
throw base_s3select_exception("third parameter should be time-stamp");
}
new_ptime = *val_timestamp.timestamp();
if( strcmp(val_date_part.str(), "year")==0 )
{
new_ptime += boost::gregorian::years( val_quantity.i64() );
result->set_value( &new_ptime );
}
else if( strcmp(val_date_part.str(), "month")==0 )
{
new_ptime += boost::gregorian::months( val_quantity.i64() );
result->set_value( &new_ptime );
}
else if( strcmp(val_date_part.str(), "day")==0 )
{
new_ptime += boost::gregorian::days( val_quantity.i64() );
result->set_value( &new_ptime );
}
else
{
throw base_s3select_exception( std::string(val_date_part.str() + std::string(" is not supported for add")).c_str());
}
return true;
}
};
struct _fn_utcnow : public base_function
{
boost::posix_time::ptime now_ptime;
bool operator()(std::vector<base_statement*>* args, variable* result)
{
int args_size = args->size();
if (args_size != 0)
{
throw base_s3select_exception("utcnow does not expect any parameters");
}
now_ptime = boost::posix_time::ptime( boost::posix_time::second_clock::universal_time());
result->set_value( &now_ptime );
return true;
}
};
struct _fn_substr : public base_function
{
char buff[4096];// this buffer is persist for the query life time, it use for the results per row(only for the specific function call)
//it prevent from intensive use of malloc/free (fragmentation).
//should validate result length.
//TODO may replace by std::string (dynamic) , or to replace with global allocator , in query scope.
value v_str;
value v_from;
value v_to;
bool operator()(std::vector<base_statement*>* args, variable* result)
{
std::vector<base_statement*>::iterator iter = args->begin();
int args_size = args->size();
if (args_size<2)
{
throw base_s3select_exception("substr accept 2 arguments or 3");
}
base_statement* str = *iter;
iter++;
base_statement* from = *iter;
base_statement* to;
if (args_size == 3)
{
iter++;
to = *iter;
}
v_str = str->eval();
if(v_str.type != value::value_En_t::STRING)
{
throw base_s3select_exception("substr first argument must be string"); //can skip current row
}
int str_length = strlen(v_str.str());
v_from = from->eval();
if(v_from.is_string())
{
throw base_s3select_exception("substr second argument must be number"); //can skip current row
}
int64_t f;
int64_t t;
if (args_size==3)
{
v_to = to->eval();
if (v_to.is_string())
{
throw base_s3select_exception("substr third argument must be number"); //can skip row
}
}
if (v_from.type == value::value_En_t::FLOAT)
{
f=v_from.dbl();
}
else
{
f=v_from.i64();
}
if (f>str_length)
{
throw base_s3select_exception("substr start position is too far"); //can skip row
}
if (str_length>(int)sizeof(buff))
{
throw base_s3select_exception("string too long for internal buffer"); //can skip row
}
if (args_size == 3)
{
if (v_from.type == value::value_En_t::FLOAT)
{
t = v_to.dbl();
}
else
{
t = v_to.i64();
}
if( (str_length-(f-1)-t) <0)
{
throw base_s3select_exception("substr length parameter beyond bounderies"); //can skip row
}
strncpy(buff, v_str.str()+f-1, t);
}
else
{
strcpy(buff, v_str.str()+f-1);
}
result->set_value(buff);
return true;
}
};
base_function* s3select_functions::create(std::string fn_name)
{
const FunctionLibrary::const_iterator iter = m_functions_library.find(fn_name);
if (iter == m_functions_library.end())
{
std::string msg;
msg = fn_name + " " + " function not found";
throw base_s3select_exception(msg, base_s3select_exception::s3select_exp_en_t::FATAL);
}
switch (iter->second)
{
case s3select_func_En_t::ADD:
return S3SELECT_NEW(_fn_add);
break;
case s3select_func_En_t::SUM:
return S3SELECT_NEW(_fn_sum);
break;
case s3select_func_En_t::COUNT:
return S3SELECT_NEW(_fn_count);
break;
case s3select_func_En_t::MIN:
return S3SELECT_NEW(_fn_min);
break;
case s3select_func_En_t::MAX:
return S3SELECT_NEW(_fn_max);
break;
case s3select_func_En_t::TO_INT:
return S3SELECT_NEW(_fn_to_int);
break;
case s3select_func_En_t::TO_FLOAT:
return S3SELECT_NEW(_fn_to_float);
break;
case s3select_func_En_t::SUBSTR:
return S3SELECT_NEW(_fn_substr);
break;
case s3select_func_En_t::TO_TIMESTAMP:
return S3SELECT_NEW(_fn_to_timestamp);
break;
case s3select_func_En_t::EXTRACT:
return S3SELECT_NEW(_fn_extact_from_timestamp);
break;
case s3select_func_En_t::DATE_ADD:
return S3SELECT_NEW(_fn_add_to_timestamp);
break;
case s3select_func_En_t::DATE_DIFF:
return S3SELECT_NEW(_fn_diff_timestamp);
break;
case s3select_func_En_t::UTCNOW:
return S3SELECT_NEW(_fn_utcnow);
break;
default:
throw base_s3select_exception("internal error while resolving function-name");
break;
}
}
bool base_statement::is_function()
{
if (dynamic_cast<__function*>(this))
{
return true;
}
else
{
return false;
}
}
bool base_statement::is_aggregate_exist_in_expression(base_statement* e) //TODO obsolete ?
{
if (e->is_aggregate())
{
return true;
}
if (e->left() && e->left()->is_aggregate_exist_in_expression(e->left()))
{
return true;
}
if (e->right() && e->right()->is_aggregate_exist_in_expression(e->right()))
{
return true;
}
if (e->is_function())
{
for (auto i : dynamic_cast<__function*>(e)->get_arguments())
if (e->is_aggregate_exist_in_expression(i))
{
return true;
}
}
return false;
}
base_statement* base_statement::get_aggregate()
{
//search for aggregation function in AST
base_statement* res = 0;
if (is_aggregate())
{
return this;
}
if (left() && (res=left()->get_aggregate())!=0)
{
return res;
}
if (right() && (res=right()->get_aggregate())!=0)
{
return res;
}
if (is_function())
{
for (auto i : dynamic_cast<__function*>(this)->get_arguments())
{
base_statement* b=i->get_aggregate();
if (b)
{
return b;
}
}
}
return 0;
}
bool base_statement::is_nested_aggregate(base_statement* e)
{
//validate for non nested calls for aggregation function, i.e. sum ( min ( ))
if (e->is_aggregate())
{
if (e->left())
{
if (e->left()->is_aggregate_exist_in_expression(e->left()))
{
return true;
}
}
else if (e->right())
{
if (e->right()->is_aggregate_exist_in_expression(e->right()))
{
return true;
}
}
else if (e->is_function())
{
for (auto i : dynamic_cast<__function*>(e)->get_arguments())
{
if (i->is_aggregate_exist_in_expression(i))
{
return true;
}
}
}
return false;
}
return false;
}
// select sum(c2) ... + c1 ... is not allowed. a binary operation with scalar is OK. i.e. select sum() + 1
bool base_statement::is_binop_aggregate_and_column(base_statement* skip_expression)
{
if (left() && left() != skip_expression) //can traverse to left
{
if (left()->is_column())
{
return true;
}
else if (left()->is_binop_aggregate_and_column(skip_expression) == true)
{
return true;
}
}
if (right() && right() != skip_expression) //can traverse right
{
if (right()->is_column())
{
return true;
}
else if (right()->is_binop_aggregate_and_column(skip_expression) == true)
{
return true;
}
}
if (this != skip_expression && is_function())
{
__function* f = (dynamic_cast<__function*>(this));
std::vector<base_statement*> l = f->get_arguments();
for (auto i : l)
{
if (i!=skip_expression && i->is_column())
{
return true;
}
if (i->is_binop_aggregate_and_column(skip_expression) == true)
{
return true;
}
}
}
return false;
}
} //namespace s3selectEngine
#endif
|