-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathbuiltins.c
More file actions
7267 lines (6974 loc) · 344 KB
/
builtins.c
File metadata and controls
7267 lines (6974 loc) · 344 KB
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
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* 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.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "builtins.h"
#include "builtinsimpl.h"
#include "cJSON.h"
#include "crypt.h"
#include "functionMgt.h"
#include "functionMgtInt.h"
#include "geomFunc.h"
#include "querynodes.h"
#include "scalar.h"
#include "taes.h"
#include "tanalytics.h"
#include "taoserror.h"
#include "tbase64.h"
#include "tglobal.h"
#include "ttypes.h"
static int32_t buildFuncErrMsg(char* pErrBuf, int32_t len, int32_t errCode, const char* pFormat, ...) {
va_list vArgList;
va_start(vArgList, pFormat);
(void)vsnprintf(pErrBuf, len, pFormat, vArgList);
va_end(vArgList);
return errCode;
}
static int32_t invaildFuncParaNumErrMsg(char* pErrBuf, int32_t len, const char* pFuncName) {
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_NUM, "Invalid number of parameters : %s", pFuncName);
}
static int32_t invaildFuncParaTypeErrMsg(char* pErrBuf, int32_t len, const char* pFuncName) {
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_TYPE, "Invalid parameter data type : %s", pFuncName);
}
static int32_t invaildFuncParaValueErrMsg(char* pErrBuf, int32_t len, const char* pFuncName) {
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_VALUE, "Invalid parameter value : %s", pFuncName);
}
static int32_t validateTimeUnitParam(uint8_t dbPrec, const SValueNode* pVal) {
if (!IS_DURATION_VAL(pVal->flag)) {
return TSDB_CODE_FUNC_TIME_UNIT_INVALID;
}
if (TSDB_TIME_PRECISION_MILLI == dbPrec &&
(0 == strcasecmp(pVal->literal, "1u") || 0 == strcasecmp(pVal->literal, "1b"))) {
return TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL;
}
if (TSDB_TIME_PRECISION_MICRO == dbPrec && 0 == strcasecmp(pVal->literal, "1b")) {
return TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL;
}
if (pVal->literal[0] != '1' ||
(pVal->literal[1] != 'u' && pVal->literal[1] != 'a' && pVal->literal[1] != 's' && pVal->literal[1] != 'm' &&
pVal->literal[1] != 'h' && pVal->literal[1] != 'd' && pVal->literal[1] != 'w' && pVal->literal[1] != 'b')) {
return TSDB_CODE_FUNC_TIME_UNIT_INVALID;
}
return TSDB_CODE_SUCCESS;
}
/* Following are valid ISO-8601 timezone format:
* 1 z/Z
* 2 ±hh:mm
* 3 ±hhmm
* 4 ±hh
*
*/
static bool validateHourRange(int8_t hour) {
if (hour < 0 || hour > 12) {
return false;
}
return true;
}
static bool validateMinuteRange(int8_t hour, int8_t minute, char sign) {
if (minute == 0 || (minute == 30 && (hour == 3 || hour == 5) && sign == '+')) {
return true;
}
return false;
}
static bool validateTimezoneFormat(const SValueNode* pVal) {
if (TSDB_DATA_TYPE_BINARY != pVal->node.resType.type) {
return false;
}
char* tz = varDataVal(pVal->datum.p);
int32_t len = varDataLen(pVal->datum.p);
char buf[3] = {0};
int8_t hour = -1, minute = -1;
if (len == 0) {
return false;
} else if (len == 1 && (tz[0] == 'z' || tz[0] == 'Z')) {
return true;
} else if ((tz[0] == '+' || tz[0] == '-')) {
switch (len) {
case 3:
case 5: {
for (int32_t i = 1; i < len; ++i) {
if (!isdigit(tz[i])) {
return false;
}
if (i == 2) {
(void)memcpy(buf, &tz[i - 1], 2);
hour = taosStr2Int8(buf, NULL, 10);
if (!validateHourRange(hour)) {
return false;
}
} else if (i == 4) {
(void)memcpy(buf, &tz[i - 1], 2);
minute = taosStr2Int8(buf, NULL, 10);
if (!validateMinuteRange(hour, minute, tz[0])) {
return false;
}
}
}
break;
}
case 6: {
for (int32_t i = 1; i < len; ++i) {
if (i == 3) {
if (tz[i] != ':') {
return false;
}
continue;
}
if (!isdigit(tz[i])) {
return false;
}
if (i == 2) {
(void)memcpy(buf, &tz[i - 1], 2);
hour = taosStr2Int8(buf, NULL, 10);
if (!validateHourRange(hour)) {
return false;
}
} else if (i == 5) {
(void)memcpy(buf, &tz[i - 1], 2);
minute = taosStr2Int8(buf, NULL, 10);
if (!validateMinuteRange(hour, minute, tz[0])) {
return false;
}
}
}
break;
}
default: {
return false;
}
}
} else {
return false;
}
return true;
}
static int32_t countTrailingSpaces(const SValueNode* pVal, bool isLtrim) {
int32_t numOfSpaces = 0;
int32_t len = varDataLen(pVal->datum.p);
char* str = varDataVal(pVal->datum.p);
int32_t startPos = isLtrim ? 0 : len - 1;
int32_t step = isLtrim ? 1 : -1;
for (int32_t i = startPos; i < len || i >= 0; i += step) {
if (!isspace(str[i])) {
break;
}
numOfSpaces++;
}
return numOfSpaces;
}
static int32_t addTimezoneParam(SNodeList* pList, timezone_t tz) {
char buf[TD_TIME_STR_LEN] = {0};
time_t t;
int32_t code = taosTime(&t);
if (code != 0) {
return code;
}
struct tm tmInfo;
if (taosLocalTime(&t, &tmInfo, buf, sizeof(buf), tz) != NULL) {
(void)taosStrfTime(buf, sizeof(buf), "%z", &tmInfo);
}
int32_t len = (int32_t)strlen(buf);
SValueNode* pVal = NULL;
code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&pVal);
if (pVal == NULL) {
return code;
}
pVal->literal = taosStrndup(buf, len);
if (pVal->literal == NULL) {
nodesDestroyNode((SNode*)pVal);
return terrno;
}
pVal->translate = true;
pVal->node.resType.type = TSDB_DATA_TYPE_BINARY;
pVal->node.resType.bytes = len + VARSTR_HEADER_SIZE;
pVal->node.resType.precision = TSDB_TIME_PRECISION_MILLI;
pVal->datum.p = taosMemoryCalloc(1, len + VARSTR_HEADER_SIZE + 1);
if (pVal->datum.p == NULL) {
nodesDestroyNode((SNode*)pVal);
return terrno;
}
varDataSetLen(pVal->datum.p, len);
tstrncpy(varDataVal(pVal->datum.p), pVal->literal, len + 1);
code = nodesListAppend(pList, (SNode*)pVal);
if (TSDB_CODE_SUCCESS != code) {
nodesDestroyNode((SNode*)pVal);
return code;
}
return TSDB_CODE_SUCCESS;
}
static int32_t addUint8Param(SNodeList** pList, uint8_t param) {
SValueNode* pVal = NULL;
int32_t code = nodesMakeNode(QUERY_NODE_VALUE, (SNode**)&pVal);
if (pVal == NULL) {
return code;
}
pVal->literal = NULL;
pVal->translate = true;
pVal->notReserved = true;
pVal->node.resType.type = TSDB_DATA_TYPE_TINYINT;
pVal->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_TINYINT].bytes;
pVal->node.resType.precision = param;
pVal->datum.i = (int64_t)param;
pVal->typeData = (int64_t)param;
code = nodesListMakeAppend(pList, (SNode*)pVal);
if (TSDB_CODE_SUCCESS != code) {
nodesDestroyNode((SNode*)pVal);
return code;
}
return TSDB_CODE_SUCCESS;
}
static int32_t addPseudoParam(SNodeList** pList) {
SNode* pseudoNode = NULL;
int32_t code = nodesMakeNode(QUERY_NODE_LEFT_VALUE, &pseudoNode);
if (pseudoNode == NULL) {
return code;
}
code = nodesListMakeAppend(pList, pseudoNode);
if (TSDB_CODE_SUCCESS != code) {
nodesDestroyNode(pseudoNode);
return code;
}
return TSDB_CODE_SUCCESS;
}
static SDataType* getSDataTypeFromNode(SNode* pNode) {
if (pNode == NULL) return NULL;
if (nodesIsExprNode(pNode)) {
return &((SExprNode*)pNode)->resType;
} else {
return NULL;
}
}
static bool paramSupportNull(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NULL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE);
}
static bool paramSupportBool(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_BOOL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE);
}
static bool paramSupportTinyint(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_TINYINT_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_INTEGER_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportSmallint(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_SMALLINT_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_INTEGER_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportInt(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_INT_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_INTEGER_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportBigint(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_BIGINT_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_INTEGER_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportFloat(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_FLOAT_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE);
}
static bool paramSupportDouble(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_DOUBLE_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE);
}
static bool paramSupportVarchar(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_VARCHAR_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_STRING_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_VAR_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportTimestamp(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_TIMESTAMP_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportNchar(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NCHAR_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_STRING_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_VAR_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportUTinyInt(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UTINYINT_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_INTEGER_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportUSmallInt(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_USMALLINT_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_INTEGER_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportUInt(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UINT_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_INTEGER_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportUBigInt(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UBIGINT_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NUMERIC_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_INTEGER_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_UNIX_TS_TYPE);
}
static bool paramSupportJSON(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_JSON_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_VAR_TYPE);
}
static bool paramSupportVarBinary(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_VARB_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_VAR_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_STRING_TYPE);
}
static bool paramSupportGeometry(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_GEOMETRY_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_VAR_TYPE);
}
static bool paramSupportDecimal(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_DECIMAL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE);
}
static bool paramSupportBlob(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_BLOB_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_ALL_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_VAR_TYPE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_STRING_TYPE);
}
static bool paramSupportValueNode(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_VALUE_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_EXPR_NODE);
}
static bool paramSupportOperatorNode(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_EXPR_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_OPERATOR_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NOT_VALUE_NODE);
}
static bool paramSupportFunctionNode(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_EXPR_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_FUNCTION_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NOT_VALUE_NODE);
}
static bool paramSupportLogicConNode(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_EXPR_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_LOGIC_CONDITION_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NOT_VALUE_NODE);
}
static bool paramSupportCaseWhenNode(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_EXPR_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_CASE_WHEN_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NOT_VALUE_NODE);
}
static bool paramSupportColumnNode(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_EXPR_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_COLUMN_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NOT_VALUE_NODE);
}
static bool paramSupportRemoteValueNode(uint64_t typeFlag) {
return FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_REMOTE_VALUE_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_EXPR_NODE) ||
FUNC_MGT_TEST_MASK(typeFlag, FUNC_PARAM_SUPPORT_NOT_VALUE_NODE);
}
static bool paramSupportNodeType(SNode* pNode, uint64_t typeFlag) {
switch (pNode->type) {
case QUERY_NODE_VALUE:
return paramSupportValueNode(typeFlag);
case QUERY_NODE_OPERATOR:
return paramSupportOperatorNode(typeFlag);
case QUERY_NODE_FUNCTION:
return paramSupportFunctionNode(typeFlag);
case QUERY_NODE_LOGIC_CONDITION:
return paramSupportLogicConNode(typeFlag);
case QUERY_NODE_CASE_WHEN:
return paramSupportCaseWhenNode(typeFlag);
case QUERY_NODE_COLUMN:
return paramSupportColumnNode(typeFlag);
case QUERY_NODE_REMOTE_VALUE:
return paramSupportRemoteValueNode(typeFlag);
default:
return false;
}
}
static bool paramSupportDataType(SDataType* pDataType, uint64_t typeFlag) {
switch (pDataType->type) {
case TSDB_DATA_TYPE_NULL:
return paramSupportNull(typeFlag);
case TSDB_DATA_TYPE_BOOL:
return paramSupportBool(typeFlag);
case TSDB_DATA_TYPE_TINYINT:
return paramSupportTinyint(typeFlag);
case TSDB_DATA_TYPE_SMALLINT:
return paramSupportSmallint(typeFlag);
case TSDB_DATA_TYPE_INT:
return paramSupportInt(typeFlag);
case TSDB_DATA_TYPE_BIGINT:
return paramSupportBigint(typeFlag);
case TSDB_DATA_TYPE_FLOAT:
return paramSupportFloat(typeFlag);
case TSDB_DATA_TYPE_DOUBLE:
return paramSupportDouble(typeFlag);
case TSDB_DATA_TYPE_VARCHAR:
return paramSupportVarchar(typeFlag);
case TSDB_DATA_TYPE_TIMESTAMP:
return paramSupportTimestamp(typeFlag);
case TSDB_DATA_TYPE_NCHAR:
return paramSupportNchar(typeFlag);
case TSDB_DATA_TYPE_UTINYINT:
return paramSupportUTinyInt(typeFlag);
case TSDB_DATA_TYPE_USMALLINT:
return paramSupportUSmallInt(typeFlag);
case TSDB_DATA_TYPE_UINT:
return paramSupportUInt(typeFlag);
case TSDB_DATA_TYPE_UBIGINT:
return paramSupportUBigInt(typeFlag);
case TSDB_DATA_TYPE_JSON:
return paramSupportJSON(typeFlag);
case TSDB_DATA_TYPE_VARBINARY:
return paramSupportVarBinary(typeFlag);
case TSDB_DATA_TYPE_GEOMETRY:
return paramSupportGeometry(typeFlag);
case TSDB_DATA_TYPE_DECIMAL64:
case TSDB_DATA_TYPE_DECIMAL:
return paramSupportDecimal(typeFlag);
case TSDB_DATA_TYPE_BLOB:
return paramSupportBlob(typeFlag);
default:
return false;
}
}
typedef enum { UNKNOWN_BIN = 0, USER_INPUT_BIN, LINEAR_BIN, LOG_BIN } EHistoBinType;
static int8_t validateHistogramBinType(char* binTypeStr) {
int8_t binType;
if (strcasecmp(binTypeStr, "user_input") == 0) {
binType = USER_INPUT_BIN;
} else if (strcasecmp(binTypeStr, "linear_bin") == 0) {
binType = LINEAR_BIN;
} else if (strcasecmp(binTypeStr, "log_bin") == 0) {
binType = LOG_BIN;
} else {
binType = UNKNOWN_BIN;
}
return binType;
}
static int32_t validateHistogramBinDesc(char* binDescStr, int8_t binType, char* errMsg, int32_t msgLen) {
const char* msg1 = "HISTOGRAM function requires four parameters";
const char* msg3 = "HISTOGRAM function invalid format for binDesc parameter";
const char* msg4 = "HISTOGRAM function binDesc parameter \"count\" should be in range [1, 1000]";
const char* msg5 = "HISTOGRAM function bin/parameter should be in range [-DBL_MAX, DBL_MAX]";
const char* msg6 = "HISTOGRAM function binDesc parameter \"width\" cannot be 0";
const char* msg7 = "HISTOGRAM function binDesc parameter \"start\" cannot be 0 with \"log_bin\" type";
const char* msg8 = "HISTOGRAM function binDesc parameter \"factor\" cannot be negative or equal to 0/1";
const char* msg9 = "HISTOGRAM function out of memory";
cJSON* binDesc = cJSON_Parse(binDescStr);
int32_t numOfBins;
double* intervals;
if (cJSON_IsObject(binDesc)) { /* linaer/log bins */
int32_t numOfParams = cJSON_GetArraySize(binDesc);
int32_t startIndex;
if (numOfParams != 4) {
(void)snprintf(errMsg, msgLen, "%s", msg1);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
cJSON* start = cJSON_GetObjectItem(binDesc, "start");
cJSON* factor = cJSON_GetObjectItem(binDesc, "factor");
cJSON* width = cJSON_GetObjectItem(binDesc, "width");
cJSON* count = cJSON_GetObjectItem(binDesc, "count");
cJSON* infinity = cJSON_GetObjectItem(binDesc, "infinity");
if (!cJSON_IsNumber(start) || !cJSON_IsNumber(count) || !cJSON_IsBool(infinity)) {
(void)snprintf(errMsg, msgLen, "%s", msg3);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
if (count->valueint <= 0 || count->valueint > 1000) { // limit count to 1000
(void)snprintf(errMsg, msgLen, "%s", msg4);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
if (isinf(start->valuedouble) || (width != NULL && isinf(width->valuedouble)) ||
(factor != NULL && isinf(factor->valuedouble)) || (count != NULL && isinf(count->valuedouble))) {
(void)snprintf(errMsg, msgLen, "%s", msg5);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
int32_t counter = (int32_t)count->valueint;
if (infinity->valueint == false) {
startIndex = 0;
numOfBins = counter + 1;
} else {
startIndex = 1;
numOfBins = counter + 3;
}
intervals = taosMemoryCalloc(numOfBins, sizeof(double));
if (intervals == NULL) {
(void)snprintf(errMsg, msgLen, "%s", msg9);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
if (cJSON_IsNumber(width) && factor == NULL && binType == LINEAR_BIN) {
// linear bin process
if (width->valuedouble == 0) {
(void)snprintf(errMsg, msgLen, "%s", msg6);
taosMemoryFree(intervals);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
for (int i = 0; i < counter + 1; ++i) {
intervals[startIndex] = start->valuedouble + i * width->valuedouble;
if (isinf(intervals[startIndex])) {
(void)snprintf(errMsg, msgLen, "%s", msg5);
taosMemoryFree(intervals);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
startIndex++;
}
} else if (cJSON_IsNumber(factor) && width == NULL && binType == LOG_BIN) {
// log bin process
if (start->valuedouble == 0) {
(void)snprintf(errMsg, msgLen, "%s", msg7);
taosMemoryFree(intervals);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
if (factor->valuedouble < 0 || factor->valuedouble == 0 || factor->valuedouble == 1) {
(void)snprintf(errMsg, msgLen, "%s", msg8);
taosMemoryFree(intervals);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
for (int i = 0; i < counter + 1; ++i) {
intervals[startIndex] = start->valuedouble * pow(factor->valuedouble, i * 1.0);
if (isinf(intervals[startIndex])) {
(void)snprintf(errMsg, msgLen, "%s", msg5);
taosMemoryFree(intervals);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
startIndex++;
}
} else {
(void)snprintf(errMsg, msgLen, "%s", msg3);
taosMemoryFree(intervals);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
if (infinity->valueint == true) {
intervals[0] = -INFINITY;
intervals[numOfBins - 1] = INFINITY;
// in case of desc bin orders, -inf/inf should be swapped
if (numOfBins < 4) {
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
if (intervals[1] > intervals[numOfBins - 2]) {
TSWAP(intervals[0], intervals[numOfBins - 1]);
}
}
} else if (cJSON_IsArray(binDesc)) { /* user input bins */
if (binType != USER_INPUT_BIN) {
(void)snprintf(errMsg, msgLen, "%s", msg3);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
numOfBins = cJSON_GetArraySize(binDesc);
intervals = taosMemoryCalloc(numOfBins, sizeof(double));
if (intervals == NULL) {
(void)snprintf(errMsg, msgLen, "%s", msg9);
cJSON_Delete(binDesc);
return terrno;
}
cJSON* bin = binDesc->child;
if (bin == NULL) {
(void)snprintf(errMsg, msgLen, "%s", msg3);
taosMemoryFree(intervals);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
int i = 0;
while (bin) {
intervals[i] = bin->valuedouble;
if (!cJSON_IsNumber(bin)) {
(void)snprintf(errMsg, msgLen, "%s", msg3);
taosMemoryFree(intervals);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
if (i != 0 && intervals[i] <= intervals[i - 1]) {
(void)snprintf(errMsg, msgLen, "%s", msg3);
taosMemoryFree(intervals);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
bin = bin->next;
i++;
}
} else {
(void)snprintf(errMsg, msgLen, "%s", msg3);
cJSON_Delete(binDesc);
return TSDB_CODE_FUNC_HISTOGRAM_ERROR;
}
cJSON_Delete(binDesc);
taosMemoryFree(intervals);
return TSDB_CODE_SUCCESS;
}
static int32_t checkRangeValue(SNode* pNode, SParamRange range, bool* isMatch) {
int32_t code = TSDB_CODE_SUCCESS;
if (pNode->type == QUERY_NODE_VALUE) {
SValueNode* pVal = (SValueNode*)pNode;
if (IS_INTEGER_TYPE(getSDataTypeFromNode(pNode)->type)) {
if (pVal->datum.i < range.iMinVal || pVal->datum.i > range.iMaxVal) {
code = TSDB_CODE_FUNC_FUNTION_PARA_RANGE;
*isMatch = false;
}
} else {
if ((int64_t)pVal->datum.d < range.iMinVal || (int64_t)pVal->datum.d > range.iMaxVal) {
code = TSDB_CODE_FUNC_FUNTION_PARA_RANGE;
*isMatch = false;
}
}
} else {
// for other node type, range check should be done in process function
}
return code;
}
static int32_t checkFixedValue(SNode* pNode, const SParamInfo* paramPattern, int32_t paramIdx, bool* isMatch) {
int32_t code = TSDB_CODE_SUCCESS;
bool checkStr = paramSupportVarBinary(paramPattern->validDataType) ||
paramSupportVarchar(paramPattern->validDataType) || paramSupportNchar(paramPattern->validDataType);
if (pNode->type == QUERY_NODE_VALUE) {
SValueNode* pVal = (SValueNode*)pNode;
if (!checkStr) {
for (int32_t k = 0; k < paramPattern->fixedValueSize; k++) {
if (pVal->datum.i == paramPattern->fixedNumValue[k]) {
code = TSDB_CODE_SUCCESS;
*isMatch = true;
break;
} else {
code = TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
*isMatch = false;
}
}
} else {
for (int32_t k = 0; k < paramPattern->fixedValueSize; k++) {
if (taosStrcasecmp(pVal->literal, paramPattern->fixedStrValue[k]) == 0) {
code = TSDB_CODE_SUCCESS;
*isMatch = true;
break;
} else {
code = TSDB_CODE_FUNC_FUNTION_PARA_VALUE;
*isMatch = false;
}
}
}
} else {
// for other node type, fixed value check should be done in process function
}
return code;
}
static int32_t checkPrimTS(SNode* pNode, bool* isMatch) {
int32_t code = TSDB_CODE_SUCCESS;
if (nodeType(pNode) != QUERY_NODE_COLUMN || !IS_TIMESTAMP_TYPE(getSDataTypeFromNode(pNode)->type) ||
!((SColumnNode*)pNode)->isPrimTs) {
code = TSDB_CODE_FUNC_FUNTION_PARA_PRIMTS;
*isMatch = false;
}
return code;
}
static int32_t checkPrimaryKey(SNode* pNode, bool* isMatch) {
int32_t code = TSDB_CODE_SUCCESS;
if (nodeType(pNode) != QUERY_NODE_COLUMN || !IS_INTEGER_TYPE(getSDataTypeFromNode(pNode)->type) ||
!((SColumnNode*)pNode)->isPk) {
code = TSDB_CODE_FUNC_FUNTION_PARA_PK;
*isMatch = false;
}
return code;
}
static int32_t checkHasColumn(SNode* pNode, bool* isMatch) {
int32_t code = TSDB_CODE_SUCCESS;
if (!nodesExprHasColumn(pNode)) {
code = TSDB_CODE_FUNC_FUNTION_PARA_HAS_COL;
*isMatch = false;
}
return code;
}
static int32_t checkValueNodeNotNull(SNode* pNode, bool* isMatch) {
int32_t code = TSDB_CODE_SUCCESS;
if (IS_NULL_TYPE(getSDataTypeFromNode(pNode)->type) && QUERY_NODE_VALUE == nodeType(pNode)) {
code = TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
*isMatch = false;
}
return code;
}
static int32_t checkTimeUnit(SNode* pNode, int32_t precision, bool* isMatch) {
if (nodeType(pNode) != QUERY_NODE_VALUE || !IS_INTEGER_TYPE(getSDataTypeFromNode(pNode)->type)) {
*isMatch = false;
return TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
}
if (IS_NULL_TYPE(getSDataTypeFromNode(pNode)->type)) {
*isMatch = true;
return TSDB_CODE_SUCCESS;
}
int32_t code = validateTimeUnitParam(precision, (SValueNode*)pNode);
if (TSDB_CODE_SUCCESS != code) {
*isMatch = false;
}
return code;
}
static int32_t validateParam(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
int32_t code = TSDB_CODE_SUCCESS;
SNodeList* paramList = pFunc->pParameterList;
char errMsg[128] = {0};
// no need to check
if (funcMgtBuiltins[pFunc->funcId].parameters.paramInfoPattern == 0) {
return TSDB_CODE_SUCCESS;
}
// check param num
if ((funcMgtBuiltins[pFunc->funcId].parameters.maxParamNum != -1 &&
LIST_LENGTH(paramList) > funcMgtBuiltins[pFunc->funcId].parameters.maxParamNum) ||
LIST_LENGTH(paramList) < funcMgtBuiltins[pFunc->funcId].parameters.minParamNum) {
return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
}
// check each param
for (int32_t i = 0; i < funcMgtBuiltins[pFunc->funcId].parameters.paramInfoPattern; i++) {
bool isMatch = true;
int32_t paramIdx = 0;
const SParamInfo* paramPattern = funcMgtBuiltins[pFunc->funcId].parameters.inputParaInfo[i];
while (1) {
// one table can have at most 4096 columns, int32_t is enough.
for (int32_t j = paramPattern[paramIdx].startParam;
j <= (paramPattern[paramIdx].endParam == -1 ? INT32_MAX - 1 : paramPattern[paramIdx].endParam); j++) {
if (j > LIST_LENGTH(paramList)) {
code = TSDB_CODE_SUCCESS;
isMatch = true;
break;
}
SNode* pNode = nodesListGetNode(paramList, j - 1);
if (NULL == pNode) {
code = TSDB_CODE_FUNC_FUNTION_PARA_NUM;
isMatch = false;
break;
}
// check node type
if (!paramSupportNodeType(pNode, paramPattern[paramIdx].validNodeType)) {
code = TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
isMatch = false;
break;
}
// check data type
if (!paramSupportDataType(getSDataTypeFromNode(pNode), paramPattern[paramIdx].validDataType)) {
code = TSDB_CODE_FUNC_FUNTION_PARA_TYPE;
isMatch = false;
break;
}
if (paramPattern[paramIdx].validNodeType == FUNC_PARAM_SUPPORT_VALUE_NODE) {
SValueNode* pVal = (SValueNode*)pNode;
pVal->notReserved = true;
}
switch (paramPattern[paramIdx].valueRangeFlag) {
case FUNC_PARAM_NO_SPECIFIC_VALUE:
break;
case FUNC_PARAM_HAS_RANGE:
code = checkRangeValue(pNode, paramPattern[paramIdx].range, &isMatch);
break;
case FUNC_PARAM_HAS_FIXED_VALUE:
code = checkFixedValue(pNode, ¶mPattern[paramIdx], paramIdx, &isMatch);
break;
default:
break;
}
if (!isMatch) {
break;
}
switch (paramPattern[paramIdx].paramAttribute) {
case FUNC_PARAM_NO_SPECIFIC_ATTRIBUTE:
break;
case FUNC_PARAM_MUST_BE_PRIMTS:
code = checkPrimTS(pNode, &isMatch);
break;
case FUNC_PARAM_MUST_BE_PK:
code = checkPrimaryKey(pNode, &isMatch);
break;
case FUNC_PARAM_MUST_HAVE_COLUMN:
code = checkHasColumn(pNode, &isMatch);
break;
case FUNC_PARAM_VALUE_NODE_NOT_NULL:
code = checkValueNodeNotNull(pNode, &isMatch);
break;
case FUNC_PARAM_MUST_BE_TIME_UNIT:
code = checkTimeUnit(pNode, pFunc->node.resType.precision, &isMatch);
break;
default:
break;
}
if (!isMatch) {
break;
}
}
if (paramPattern[paramIdx].isLastParam || !isMatch) {
break;
}
paramIdx++;
}
if (isMatch) {
return TSDB_CODE_SUCCESS;
}
}
switch (code) {
case TSDB_CODE_FUNC_FUNTION_PARA_NUM:
return invaildFuncParaNumErrMsg(pErrBuf, len, pFunc->functionName);
case TSDB_CODE_FUNC_FUNTION_PARA_TYPE:
return invaildFuncParaTypeErrMsg(pErrBuf, len, pFunc->functionName);
case TSDB_CODE_FUNC_FUNTION_PARA_VALUE:
return invaildFuncParaValueErrMsg(pErrBuf, len, pFunc->functionName);
case TSDB_CODE_FUNC_FUNTION_PARA_RANGE:
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_RANGE, "Invalid parameter range : %s",
pFunc->functionName);
case TSDB_CODE_FUNC_FUNTION_PARA_PRIMTS:
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_PRIMTS,
"Parameter should be primary timestamp : %s", pFunc->functionName);
case TSDB_CODE_FUNC_FUNTION_PARA_PK:
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_PK, "Parameter should be primary key : %s",
pFunc->functionName);
case TSDB_CODE_FUNC_FUNTION_PARA_HAS_COL:
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_PARA_HAS_COL, "Parameter should have column : %s",
pFunc->functionName);
case TSDB_CODE_FUNC_TIME_UNIT_INVALID:
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_TIME_UNIT_INVALID, "Invalid timzone format : %s",
pFunc->functionName);
case TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL:
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_TIME_UNIT_TOO_SMALL, "Time unit is too small : %s",
pFunc->functionName);
case TSDB_CODE_FUNC_FUNCTION_HISTO_TYPE:
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNCTION_HISTO_TYPE, "Invalid histogram bin type : %s",
pFunc->functionName);
case TSDB_CODE_FUNC_HISTOGRAM_ERROR:
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_HISTOGRAM_ERROR, errMsg, pFunc->functionName);
default:
return buildFuncErrMsg(pErrBuf, len, TSDB_CODE_FUNC_FUNTION_ERROR, "Function check parameter failed : %s",
pFunc->functionName);
}
}
// There is only one parameter of numeric type, and the return type is parameter type
static int32_t translateOutNum(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
FUNC_ERR_RET(validateParam(pFunc, pErrBuf, len));
uint8_t paraType = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 0))->type;
if (IS_NULL_TYPE(paraType)) {
paraType = TSDB_DATA_TYPE_BIGINT;
}
pFunc->node.resType = (SDataType){.bytes = tDataTypes[paraType].bytes, .type = paraType};
return TSDB_CODE_SUCCESS;
}
// There is only one parameter, and the return type is parameter type
static int32_t translateMinMax(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {
FUNC_ERR_RET(validateParam(pFunc, pErrBuf, len));
SDataType* dataType = getSDataTypeFromNode(nodesListGetNode(pFunc->pParameterList, 0));
uint8_t paraType = IS_NULL_TYPE(dataType->type) ? TSDB_DATA_TYPE_BIGINT : dataType->type;
int32_t bytes = IS_STR_DATA_TYPE(paraType) ? dataType->bytes : tDataTypes[paraType].bytes;
uint8_t prec = IS_DECIMAL_TYPE(paraType) ? dataType->precision : pFunc->node.resType.precision;
pFunc->node.resType = (SDataType){.bytes = bytes, .type = paraType, .precision = prec, .scale = dataType->scale};
return TSDB_CODE_SUCCESS;
}
static int32_t translateAvg(SFunctionNode* pFunc, char* pErrBuf, int32_t len) {