-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy patharray_ops.c
More file actions
2146 lines (1860 loc) · 64.1 KB
/
array_ops.c
File metadata and controls
2146 lines (1860 loc) · 64.1 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
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "parser/parse_coerce.h"
#include "catalog/pg_type.h"
#include "utils/array.h"
#include "utils/numeric.h"
#include "utils/builtins.h"
#include "utils/memutils.h"
#if GP_VERSION_NUM >= 70000 || PG_VERSION_NUM >= 130000
#include "utils/fmgrprotos.h"
#else
#include "utils/int8.h"
#endif
#if PG_VERSION_NUM >= 160000
#include <varatt.h>
#endif
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
#include "access/hash.h"
#include <math.h>
#include <float.h>
#ifndef NO_PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
/*
This module provides c implementations for several postgres array operations.
There is a 3 tier structure to each function calls. This is done to take
advantage of some common type checking. All the functions are classified into
4 types based on the input/output types:
function(array,array)->array (calls: General_2Array_to_Array)
function(array,scalar)->array (calls: General_Array_to_Array)
function(array,array)->scalar (calls: General_2Array_to_Element)
function(array,scalar)->scalar (calls: General_Array_to_Element)
function(array,scalar)->struct (calls: General_Array_to_Struct)
Assuming that this input is flexible enough for some new function, implementer
needs to provide 2 functions. First is the top level function that is being
exposed to SQL and takes the necessary parameters. This function makes a call
to one of the 4 intermediate functions, passing pointer to the low level
function (or functions) as the argument. In case of the single function, this
low level function, it will specify the operations to be executed against each
cell in the array. If two functions are to be passed the second is the
finalization function that takes the result of the execution on each cell and
produces final result. In case not final functions is necessary a generic
'noop_finalize' can be used - which does nothing to the intermediate result.
*/
static ArrayType *General_2Array_to_Array(ArrayType *v1, ArrayType *v2,
Datum(*element_function)(Datum,Oid,Datum,Oid,Datum,Oid));
static ArrayType *General_Array_to_Array(ArrayType *v1, Datum value,
Datum(*element_function)(Datum,Oid,Datum,Oid,Datum,Oid));
static ArrayType *General_Array_to_Cumulative_Array(ArrayType *v1, Datum value,
Datum(*element_function)(Datum,Oid,Datum,Oid,Datum,Oid));
static Datum General_2Array_to_Element(ArrayType *v1, ArrayType *v2,
Datum(*element_function)(Datum,Oid,Datum,Oid,Datum,Oid),
Datum(*finalize_function)(Datum,int,Oid));
static Datum General_Array_to_Element(ArrayType *v, Datum exta_val, float8 init_val,
Datum(*element_function)(Datum,Oid,Datum,Oid,Datum,Oid),
Datum(*finalize_function)(Datum,int,Oid));
static Datum General_Array_to_Struct(ArrayType *v, void *init_val,
void*(*element_function)(Datum,Oid,int,void*),
Datum(*finalize_function)(void*,int,Oid));
static inline Datum noop_finalize(Datum elt,int size,Oid element_type);
static inline Datum average_finalize(Datum elt,int size,Oid element_type);
static inline Datum average_root_finalize(Datum elt,int size,Oid element_type);
static inline Datum value_index_finalize(void *mid_result,int size,Oid element_type);
static inline Datum element_cos(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_add(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_sub(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_mult(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_div(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_set(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_abs(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_sqrt(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_pow(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_square(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_dot(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_contains(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_max(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_min(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline void* element_argmax(Datum element, Oid elt_type, int elt_index, void *result);
static inline void* element_argmin(Datum element, Oid elt_type, int elt_index, void *result);
static inline Datum element_sum(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_abs_sum(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_diff(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static inline Datum element_sum_sqr(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type);
static ArrayType* array_to_float8_array(ArrayType *a);
static inline int64 datum_int64_cast(Datum elt, Oid element_type);
static inline Datum int64_datum_cast(int64 result, Oid result_type);
static inline float8 datum_float8_cast(Datum elt, Oid element_type);
static inline Datum float8_datum_cast(float8 result, Oid result_type);
static inline Datum element_op(Datum element, Oid elt_type, Datum result,
Oid result_type, Datum opt_elt, Oid opt_type,
float8 (*op)(float8, float8, float8));
static inline float8 float8_cos(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_add(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_sub(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_mult(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_div(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_set(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_abs(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_sqrt(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_pow(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_square(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_dot(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_contains(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_max(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_min(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_sum(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_abs_sum(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_diff(float8 op1, float8 op2, float8 opt_op);
static inline float8 float8_sum_sqr(float8 op1, float8 op2, float8 opt_op);
/*
* Implementation of operations on float8 type
*/
static
inline
float8
float8_cos(float8 op1, float8 op2, float8 opt_op){
(void) op2;
(void) opt_op;
return cos(op1);
}
static
inline
float8
float8_add(float8 op1, float8 op2, float8 opt_op){
(void) op2;
return op1 + opt_op;
}
static
inline
float8
float8_sub(float8 op1, float8 op2, float8 opt_op){
(void) op2;
return op1 - opt_op;
}
static
inline
float8
float8_mult(float8 op1, float8 op2, float8 opt_op){
(void) op2;
return op1 * opt_op;
}
static
inline
int64
int64_div(int64 num, int64 denom){
if (denom == 0) {
ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero is not allowed"),
errdetail("Arrays with element 0 can not be use in the denominator")));
}
return num / denom;
}
static
inline
float8
float8_div(float8 op1, float8 op2, float8 opt_op){
(void) op2;
if (opt_op == 0) {
ereport(ERROR, (errcode(ERRCODE_DIVISION_BY_ZERO),
errmsg("division by zero is not allowed"),
errdetail("Arrays with element 0 can not be use in the denominator")));
}
return op1 / opt_op;
}
static
inline
float8
float8_set(float8 op1, float8 op2, float8 opt_op){
(void) op2;
(void) op1;
return opt_op;
}
static
inline
float8
float8_abs(float8 op1, float8 op2, float8 opt_op){
(void) op2;
(void) opt_op;
return fabs(op1);
}
static
inline
float8
float8_sqrt(float8 op1, float8 op2, float8 opt_op){
(void) op2;
(void) opt_op;
if (op1 < 0) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("square root of negative values is not allowed"),
errdetail("Arrays with negative values can not be input of array_sqrt")));
}
return sqrt(op1);
}
static
inline
float8
float8_pow(float8 op1, float8 op2, float8 opt_op){
(void) op2;
return pow(op1, opt_op);
}
static
inline
float8
float8_square(float8 op1, float8 op2, float8 opt_op){
(void) op2;
(void) opt_op;
return op1*op1;
}
static
inline
float8
float8_dot(float8 op1, float8 op2, float8 opt_op){
return op2 + op1 * opt_op;
}
static
inline
float8
float8_contains(float8 op1, float8 op2, float8 opt_op){
return op2 + (((op1 == opt_op) || (opt_op == 0))? 0 : 1);
}
static
inline
float8
float8_max(float8 op1, float8 op2, float8 opt_op) {
(void) opt_op;
return (op1 > op2) ? op1:op2;
}
static
inline
float8
float8_min(float8 op1, float8 op2, float8 opt_op) {
(void) opt_op;
return (op1 < op2) ? op1: op2;
}
static
inline
float8
float8_sum(float8 op1, float8 op2, float8 opt_op){
(void) opt_op;
return op1 + op2;
}
static
inline
float8
float8_abs_sum(float8 op1, float8 op2, float8 opt_op){
(void) opt_op;
return fabs(op1) + op2;
}
static
inline
float8
float8_diff(float8 op1, float8 op2, float8 opt_op){
return op2 + (op1 - opt_op) * (op1 - opt_op);
}
static
inline
float8
float8_sum_sqr(float8 op1, float8 op2, float8 opt_op){
(void) opt_op;
return op2 + op1 * op1;
}
/*
* Assume the input array is of type numeric[].
*/
ArrayType*
array_to_float8_array(ArrayType *x) {
Oid element_type = ARR_ELEMTYPE(x);
if (element_type == FLOAT8OID) {
// this does not returning a copy, the caller needs to pay attention
return x;
}
// deconstruct
TypeCacheEntry * TI = lookup_type_cache(element_type,
TYPECACHE_CMP_PROC_FINFO);
bool *nulls = NULL;
int len = 0;
Datum *array = NULL;
deconstruct_array(x,
element_type,
TI->typlen,
TI->typbyval,
TI->typalign,
&array,
&nulls,
&len);
// casting
Datum *float8_array = (Datum *)palloc(len * sizeof(Datum));
int i = 0;
for (i = 0; i < len; i ++) {
if (nulls[i]) { float8_array[i] = Float8GetDatum(0); }
else {
float8_array[i] = Float8GetDatum(
datum_float8_cast(array[i], element_type));
}
}
// reconstruct
TypeCacheEntry * FLOAT8TI = lookup_type_cache(FLOAT8OID,
TYPECACHE_CMP_PROC_FINFO);
ArrayType *ret = construct_md_array(float8_array,
nulls,
ARR_NDIM(x),
ARR_DIMS(x),
ARR_LBOUND(x),
FLOAT8OID,
FLOAT8TI->typlen,
FLOAT8TI->typbyval,
FLOAT8TI->typalign);
pfree(array);
pfree(float8_array);
pfree(nulls);
return ret;
}
// ------------------------------------------------------------------------
// exported functions
// ------------------------------------------------------------------------
/*
* This function returns an float array with specified size.
*/
PG_FUNCTION_INFO_V1(array_of_float);
Datum
array_of_float(PG_FUNCTION_ARGS){
int size = PG_GETARG_INT32(0);
if (size <= 0 || size > 10000000) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid array length"),
errdetail("array_of_float: Size should be in [1, 1e7], %d given", size)));
}
Datum* array = palloc (sizeof(Datum)*size);
for(int i = 0; i < size; ++i) {
array[i] = Float8GetDatum(0);
}
TypeCacheEntry *typentry = lookup_type_cache(FLOAT8OID,TYPECACHE_CMP_PROC_FINFO);
int type_size = typentry->typlen;
bool typbyval = typentry->typbyval;
char typalign = typentry->typalign;
ArrayType *pgarray = construct_array(array,size,FLOAT8OID,type_size,typbyval,typalign);
PG_RETURN_ARRAYTYPE_P(pgarray);
}
/*
* This function returns an integer array with specified size.
*/
PG_FUNCTION_INFO_V1(array_of_bigint);
Datum
array_of_bigint(PG_FUNCTION_ARGS){
int size = PG_GETARG_INT32(0);
if (size <= 0 || size > 10000000) {
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid array length"),
errdetail("array_of_bigint: Size should be in [1, 1e7], %d given", size)));
}
Datum* array = palloc (sizeof(Datum)*size);
for(int i = 0; i < size; ++i) {
array[i] = Int64GetDatum(0);
}
TypeCacheEntry *typentry = lookup_type_cache(INT8OID,TYPECACHE_CMP_PROC_FINFO);
int type_size = typentry->typlen;
bool typbyval = typentry->typbyval;
char typalign = typentry->typalign;
ArrayType *pgarray = construct_array(array,size,INT8OID,type_size,typbyval,typalign);
PG_RETURN_ARRAYTYPE_P(pgarray);
}
/*
* This function returns standard deviation of the array elements.
*/
PG_FUNCTION_INFO_V1(array_stddev);
Datum
array_stddev(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *x = PG_GETARG_ARRAYTYPE_P(0);
Datum mean = General_Array_to_Element(x, Float8GetDatum(0), 0.0,
element_sum, average_finalize);
Datum res = General_Array_to_Element(x, mean, 0.0,
element_diff, average_root_finalize);
PG_FREE_IF_COPY(x, 0);
return(res);
}
/*
* This function returns mean of the array elements.
*/
PG_FUNCTION_INFO_V1(array_mean);
Datum
array_mean(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
Datum res = General_Array_to_Element(v, Float8GetDatum(0), 0.0,
element_sum, average_finalize);
PG_FREE_IF_COPY(v, 0);
return(res);
}
/*
* This function returns sum of the array elements, typed float8.
*/
PG_FUNCTION_INFO_V1(array_sum_big);
Datum
array_sum_big(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
Datum res = General_Array_to_Element(v, Float8GetDatum(0), 0.0,
element_sum, noop_finalize);
PG_FREE_IF_COPY(v, 0);
return(res);
}
/*
* This function returns sum of the array elements.
*/
PG_FUNCTION_INFO_V1(array_sum);
Datum
array_sum(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
Oid element_type = ARR_ELEMTYPE(v);
Datum res = General_Array_to_Element(v, Float8GetDatum(0), 0.0,
element_sum, noop_finalize);
PG_FREE_IF_COPY(v, 0);
return float8_datum_cast(DatumGetFloat8(res), element_type);
}
/*
* This function returns sum of the array elements' absolute value.
*/
PG_FUNCTION_INFO_V1(array_abs_sum);
Datum
array_abs_sum(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
Oid element_type = ARR_ELEMTYPE(v);
Datum res = General_Array_to_Element(v, Float8GetDatum(0), 0.0,
element_abs_sum, noop_finalize);
PG_FREE_IF_COPY(v, 0);
return float8_datum_cast(DatumGetFloat8(res), element_type);
}
/*
* This function returns minimum of the array elements.
*/
PG_FUNCTION_INFO_V1(array_min);
Datum
array_min(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
Oid element_type = ARR_ELEMTYPE(v);
Datum res = General_Array_to_Element(v, Float8GetDatum(0), FLT_MAX,
element_min, noop_finalize);
PG_FREE_IF_COPY(v, 0);
return float8_datum_cast(DatumGetFloat8(res), element_type);
}
/*
* This function returns maximum of the array elements.
*/
PG_FUNCTION_INFO_V1(array_max);
Datum
array_max(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
Oid element_type = ARR_ELEMTYPE(v);
Datum res = General_Array_to_Element(v, Float8GetDatum(0), -FLT_MAX,
element_max, noop_finalize);
PG_FREE_IF_COPY(v, 0);
return float8_datum_cast(DatumGetFloat8(res), element_type);
}
typedef struct
{
float8 value;
int64 index;
} value_index;
/*
* This function returns maximum and corresponding index of the array elements.
*/
PG_FUNCTION_INFO_V1(array_max_index);
Datum
array_max_index(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
if (ARR_NDIM(v) != 1) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Input array with multiple dimensions is not allowed!")));
}
value_index *result = (value_index *)palloc(sizeof(value_index));
result->value = -FLT_MAX;
result->index = 0;
Datum res = General_Array_to_Struct(v, result, element_argmax, value_index_finalize);
pfree(result);
PG_FREE_IF_COPY(v, 0);
return res;
}
/*
* This function returns minimum and corresponding index of the array elements.
*/
PG_FUNCTION_INFO_V1(array_min_index);
Datum
array_min_index(PG_FUNCTION_ARGS) {
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
if (ARR_NDIM(v) != 1) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Input array with multiple dimensions is not allowed!")));
}
value_index *result = (value_index *)palloc(sizeof(value_index));
result->value = FLT_MAX;
result->index = 0;
Datum res = General_Array_to_Struct(v, result, element_argmin, value_index_finalize);
PG_FREE_IF_COPY(v, 0);
pfree(result);
return res;
}
/*
* This function returns dot product of two arrays as vectors.
*/
PG_FUNCTION_INFO_V1(array_dot);
Datum
array_dot(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *v2 = PG_GETARG_ARRAYTYPE_P(1);
Datum res = General_2Array_to_Element(v1, v2, element_dot, noop_finalize);
PG_FREE_IF_COPY(v1, 0);
PG_FREE_IF_COPY(v2, 1);
return(res);
}
/*
* This function checks if each non-zero element in the right array equals to
* the element with the same index in the left array.
*/
PG_FUNCTION_INFO_V1(array_contains);
Datum
array_contains(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *v2 = PG_GETARG_ARRAYTYPE_P(1);
Datum res = General_2Array_to_Element(v1, v2, element_contains, noop_finalize);
PG_FREE_IF_COPY(v1, 0);
PG_FREE_IF_COPY(v2, 1);
if (DatumGetFloat8(res) == 0.) {
PG_RETURN_BOOL(true);
} else {
PG_RETURN_BOOL(false);
}
}
/*
* This function returns the element-wise sum of two arrays.
*/
PG_FUNCTION_INFO_V1(array_add);
Datum
array_add(PG_FUNCTION_ARGS){
// special handling for madlib.sum()
if (PG_ARGISNULL(0) && PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(0)) {
PG_RETURN_ARRAYTYPE_P(PG_GETARG_ARRAYTYPE_P(1));
}
if (PG_ARGISNULL(1)) {
PG_RETURN_ARRAYTYPE_P(PG_GETARG_ARRAYTYPE_P(0));
}
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *v2 = PG_GETARG_ARRAYTYPE_P(1);
ArrayType *res = General_2Array_to_Array(v1, v2, element_add);
PG_FREE_IF_COPY(v1, 0);
PG_FREE_IF_COPY(v2, 1);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function returns the element-wise difference of two arrays.
*/
PG_FUNCTION_INFO_V1(array_sub);
Datum
array_sub(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *v2 = PG_GETARG_ARRAYTYPE_P(1);
ArrayType *res = General_2Array_to_Array(v1, v2, element_sub);
PG_FREE_IF_COPY(v1, 0);
PG_FREE_IF_COPY(v2, 1);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function returns the element-wise product of two arrays.
*/
PG_FUNCTION_INFO_V1(array_mult);
Datum
array_mult(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *v2 = PG_GETARG_ARRAYTYPE_P(1);
ArrayType *res = General_2Array_to_Array(v1, v2, element_mult);
PG_FREE_IF_COPY(v1, 0);
PG_FREE_IF_COPY(v2, 1);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function returns result of element-wise division of two arrays.
*/
PG_FUNCTION_INFO_V1(array_div);
Datum
array_div(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *v2 = PG_GETARG_ARRAYTYPE_P(1);
ArrayType *res = General_2Array_to_Array(v1, v2, element_div);
PG_FREE_IF_COPY(v1, 0);
PG_FREE_IF_COPY(v2, 1);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function takes the absolute value for each element.
*/
PG_FUNCTION_INFO_V1(array_abs);
Datum
array_abs(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
Oid element_type = ARR_ELEMTYPE(v1);
Datum v2 = float8_datum_cast(0, element_type);
ArrayType *res = General_Array_to_Array(v1, v2, element_abs);
PG_FREE_IF_COPY(v1, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function takes the square root for each element.
*/
PG_FUNCTION_INFO_V1(array_sqrt);
Datum
array_sqrt(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *x = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *v1 = array_to_float8_array(x);
Datum v2 = 0;
ArrayType *res = General_Array_to_Array(v1, v2, element_sqrt);
if (v1 != x) { pfree(v1); }
PG_FREE_IF_COPY(x, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function takes the power for each element.
*/
PG_FUNCTION_INFO_V1(array_pow);
Datum
array_pow(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
Datum v2 = PG_GETARG_DATUM(1);
ArrayType *res = General_Array_to_Array(v1, v2, element_pow);
PG_FREE_IF_COPY(v1, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function takes the square for each element.
*/
PG_FUNCTION_INFO_V1(array_square);
Datum
array_square(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *x = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *v1 = array_to_float8_array(x);
Datum v2 = 0;
ArrayType *res = General_Array_to_Array(v1, v2, element_square);
if (v1 != x) { pfree(v1); }
PG_FREE_IF_COPY(x, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function sets all elements to be the specified value.
*/
PG_FUNCTION_INFO_V1(array_fill);
Datum
array_fill(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
Datum v2 = PG_GETARG_DATUM(1);
ArrayType *res = General_Array_to_Array(v1, v2, element_set);
PG_FREE_IF_COPY(v1, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function apply cos function to each element.
*/
PG_FUNCTION_INFO_V1(array_cos);
Datum
array_cos(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
Oid element_type = ARR_ELEMTYPE(v1);
Datum v2 = float8_datum_cast(0, element_type);
ArrayType *res = General_Array_to_Array(v1, v2, element_cos);
PG_FREE_IF_COPY(v1, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function multiplies the specified value to each element.
*/
PG_FUNCTION_INFO_V1(array_scalar_mult);
Datum
array_scalar_mult(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
Datum v2 = PG_GETARG_DATUM(1);
ArrayType *res = General_Array_to_Array(v1, v2, element_mult);
PG_FREE_IF_COPY(v1, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function addes the specified value to each element.
*/
PG_FUNCTION_INFO_V1(array_scalar_add);
Datum
array_scalar_add(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
if (PG_ARGISNULL(1)) { PG_RETURN_NULL(); }
ArrayType *v1 = PG_GETARG_ARRAYTYPE_P(0);
Datum v2 = PG_GETARG_DATUM(1);
ArrayType *res = General_Array_to_Array(v1, v2, element_add);
PG_FREE_IF_COPY(v1, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function returns the cumulative sum of the array elements.
*/
PG_FUNCTION_INFO_V1(array_cum_sum);
Datum
array_cum_sum(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *res = General_Array_to_Cumulative_Array(v, Float8GetDatum(0.0), element_add);
PG_FREE_IF_COPY(v, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function returns the cumulative product of the array elements.
*/
PG_FUNCTION_INFO_V1(array_cum_prod);
Datum
array_cum_prod(PG_FUNCTION_ARGS){
if (PG_ARGISNULL(0)) { PG_RETURN_NULL(); }
ArrayType *v = PG_GETARG_ARRAYTYPE_P(0);
ArrayType *res = General_Array_to_Cumulative_Array(v, Float8GetDatum(1.0), element_mult);
PG_FREE_IF_COPY(v, 0);
PG_RETURN_ARRAYTYPE_P(res);
}
/*
* This function removes elements with specified value from an array.
*/
PG_FUNCTION_INFO_V1(array_filter);
Datum
array_filter(PG_FUNCTION_ARGS) {
ArrayType * arr = PG_GETARG_ARRAYTYPE_P(0);
if (ARR_NDIM(arr) != 1) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Input array with multiple dimensions is not allowed!")));
}
if (ARR_HASNULL(arr)) {
ereport(ERROR, (errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("Input array with nulls is not allowed!")));
}
// value to be filtered
Oid element_type = ARR_ELEMTYPE(arr);
Datum val = float8_datum_cast(0., element_type);
char op[3] = "!=";
if (PG_NARGS() > 1) { val = PG_GETARG_DATUM(1); }
if (PG_NARGS() > 2) {
text *op_text = PG_GETARG_TEXT_P(2);
int op_len = VARSIZE(op_text) - VARHDRSZ;
strncpy(op, VARDATA(op_text), VARSIZE(op_text) - VARHDRSZ);
op[op_len] = 0;
}
// deconstruct
TypeCacheEntry * TI = lookup_type_cache(element_type,
TYPECACHE_CMP_PROC_FINFO);
bool *nulls = NULL;
int len = 0;
Datum *array = NULL;
deconstruct_array(arr,
element_type,
TI->typlen,
TI->typbyval,
TI->typalign,
&array,
&nulls,
&len);
// filtering
Datum *ret_array = (Datum *)palloc(len * sizeof(Datum));
int count = 0;
for (int i = 0; i < len; i ++) {
float8 left = datum_float8_cast(array[i], element_type);
float8 right = datum_float8_cast(val , element_type);
bool pass = true;
if (strcmp(op, "!=") == 0 || strcmp(op, "<>") == 0) {
if (isnan(left) || isnan(right)) {
pass = !(isnan(left) && isnan(right));
} else { pass = (left != right); }
} else if (strcmp(op, "=") == 0 || strcmp(op, "==") == 0) {
if (isnan(left) || isnan(right)) {
pass = (isnan(left) && isnan(right));
} else { pass = (left == right); }
} else if (strcmp(op, ">") == 0) {
pass = (left > right);
} else if (strcmp(op, ">=") == 0) {
pass = (left >= right);
} else if (strcmp(op, "<") == 0) {
pass = (left < right);