User Documentation
 All Files Functions Groups
array_ops.sql_in
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------- *//**
2  *
3  * @file array_ops.sql_in
4  *
5  * @brief implementation of array operations in SQL
6  * @date April 2011
7  *
8  *
9  *//* ----------------------------------------------------------------------- */
10 
11 m4_include(`SQLCommon.m4')
12 
13 /**
14 @addtogroup grp_array
15 
16 
17 @about
18 
19 This module provide a set of basic array operations implemented in C. It is a support module for several machine learning algorithms that
20 require fast array operations.
21 
22 @implementation
23 
24 -# At present these functions support several numeric types:
25  - SHORTINT
26  - INT
27  - BIGINT
28  - REAL
29  - FLOAT
30 As of now they do not support variable size NUMERIC input.
31 -# Also several of them may require NO NULL VALUES, while others omit NULLs and
32 return results.
33 
34 @sa File array_ops.sql_in for list of functions and usage.
35 */
36 
37 /**
38  * @brief Adds two arrays. It requires that all the values are NON-NULL. Return type is the same as the input type.
39  *
40  * @param x Array x
41  * @param y Array y
42  * @returns Sum of x and y.
43  *
44  */
45 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_add(x anyarray, y anyarray) RETURNS anyarray
46 AS 'MODULE_PATHNAME', 'array_add'
47 LANGUAGE C IMMUTABLE;
48 
49 /**
50  * @brief Subtracts two arrays. It requires that all the values are NON-NULL. Return type is the same as the input type.
51  *
52  * @param x Array x
53  * @param y Array y
54  * @returns x-y.
55  *
56  */
57 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_sub(x anyarray, y anyarray) RETURNS anyarray
58 AS 'MODULE_PATHNAME', 'array_sub'
59 LANGUAGE C IMMUTABLE;
60 
61 /**
62  * @brief Element-wise product of two arrays. It requires that all the values are NON-NULL. Return type is the same as the input type.
63  *
64  * @param x Array x
65  * @param y Array y
66  * @returns Element-wise product of x and y.
67  *
68  */
69 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_mult(x anyarray, y anyarray) RETURNS anyarray
70 AS 'MODULE_PATHNAME', 'array_mult'
71 LANGUAGE C IMMUTABLE;
72 
73 /**
74  * @brief Element-wise division of two arrays. It requires that all the values are NON-NULL. Return type is the same as the input type.
75  *
76  * @param x Array x
77  * @param y Array y
78  * @returns Element-wise division of x and y.
79  *
80  */
81 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_div(x anyarray, y anyarray) RETURNS anyarray
82 AS 'MODULE_PATHNAME', 'array_div'
83 LANGUAGE C IMMUTABLE;
84 
85 /**
86  * @brief Dot-product of two arrays. It requires that all the values are NON-NULL. Return type is the same as the input type.
87  *
88  * @param x Array x
89  * @param y Array y
90  * @returns Dot-product of x and y.
91  *
92  */
93 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_dot(x anyarray, y anyarray) RETURNS FLOAT8
94 AS 'MODULE_PATHNAME', 'array_dot'
95 LANGUAGE C IMMUTABLE;
96 
97 /**
98  * @brief Checks whether one array contains the other.
99  *
100  * @param x Array x
101  * @param y Array y
102  * @returns Returns true if x contains y.
103  *
104  */
105 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_contains(x anyarray, y anyarray) RETURNS BOOL
106 AS 'MODULE_PATHNAME', 'array_contains'
107 LANGUAGE C IMMUTABLE;
108 
109 /**
110  * @brief This function finds the maximum value in the array. NULLs are ignored. Return type is the same as the input type.
111  *
112  * @param x Array x
113  * @returns Max of x.
114  *
115  */
116 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_max(x anyarray) RETURNS anyelement
117 AS 'MODULE_PATHNAME', 'array_max'
118 LANGUAGE C IMMUTABLE;
119 
120 /**
121  * @brief This function finds the minimum value in the array. NULLs are ignored. Return type is the same as the input type.
122  *
123  * @param x Array x
124  * @returns Min of x.
125  *
126  */
127 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_min(x anyarray) RETURNS anyelement
128 AS 'MODULE_PATHNAME', 'array_min'
129 LANGUAGE C IMMUTABLE;
130 
131 /**
132  * @brief This function finds the sum of the values in the array. NULLs are ignored. Return type is the same as the input type.
133  *
134  * @param x Array x
135  * @returns Sum of x.
136  *
137  */
138 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_sum(x anyarray) RETURNS anyelement
139 AS 'MODULE_PATHNAME', 'array_sum'
140 LANGUAGE C IMMUTABLE;
141 
142 /**
143  * @brief This function finds the sum of the values in the array. NULLs are ignored. Return type is always FLOAT8 regardless of input. This function is meant to replace array_sum in the cases when sum may overflow the array type.
144  *
145  * @param x Array x
146  * @returns Sum of x.
147  *
148  */
149 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_sum_big(x anyarray) RETURNS FLOAT8
150 AS 'MODULE_PATHNAME', 'array_sum_big'
151 LANGUAGE C IMMUTABLE;
152 
153 /**
154  * @brief TThis function finds the mean of the values in the array. NULLs are ignored. Return type is the same as the input type.
155  *
156  * @param x Array x
157  * @returns Mean of x.
158  *
159  */
160 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_mean(x anyarray) RETURNS FLOAT8
161 AS 'MODULE_PATHNAME', 'array_mean'
162 LANGUAGE C IMMUTABLE;
163 
164 /**
165  * @brief This function finds the standard deviation of the values in the array. NULLs are ignored. Return type is the same as the input type.
166  *
167  * @param x Array x
168  * @returns Standard deviation of x.
169  *
170  */
171 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_stddev(x anyarray) RETURNS FLOAT8
172 AS 'MODULE_PATHNAME', 'array_stddev'
173 LANGUAGE C IMMUTABLE;
174 
175 /**
176  * @brief This function creates an array of set size (the argument value) of FLOAT8, initializing the values to 0.0;
177  *
178  * @param k Array size
179  * @returns Array of size k.
180  *
181  */
182 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_of_float(k INT4) RETURNS FLOAT8[]
183 AS 'MODULE_PATHNAME', 'array_of_float'
184 LANGUAGE C IMMUTABLE;
185 
186 /**
187  * @brief This function creates an array of set size (the argument value) of BIGINT, initializing the values to 0;
188  *
189  * @param k Array size.
190  * @returns Array of size k.
191  *
192  */
193 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_of_bigint(k INT4) RETURNS INT8[]
194 AS 'MODULE_PATHNAME', 'array_of_bigint'
195 LANGUAGE C IMMUTABLE;
196 
197 /**
198  * @brief This functions set every values in the array to some desired value (provided as the argument).
199  *
200  * @param x Some array
201  * @param k Desired value
202  * @returns Fills array with desired value.
203  *
204  */
205 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_fill(x anyarray, k anyelement) RETURNS anyarray
206 AS 'MODULE_PATHNAME', 'array_fill'
207 LANGUAGE C IMMUTABLE;
208 
209 /**
210  * @brief This function takes an array as the input and executes element with multiplication by the scalar provided as the second argument, returning the resulting array. It requires that all the values are NON-NULL. Return type is the same as the input type.
211  * @param x Array x
212  * @param k Scalar
213  * @returns Array with each element of x multiplied by scalar.
214  *
215  */
216 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_scalar_mult(x anyarray, k anyelement) RETURNS anyarray
217 AS 'MODULE_PATHNAME', 'array_scalar_mult'
218 LANGUAGE C IMMUTABLE;
219 
220 /**
221  * @brief This function takes an array as the input and finds square root of each element in the array, returning the resulting array. It requires that all the values are NON-NULL. Return type is the same as the input type. This means that if the input if of the size INT, the results would also be rounded.
222  *
223  * @param x Array x
224  * @returns Square root of all elements of x.
225  *
226  */
227 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_sqrt(x anyarray) RETURNS anyarray
228 AS 'MODULE_PATHNAME', 'array_sqrt'
229 LANGUAGE C IMMUTABLE;
230 
231 
232 /**
233  * @brief Array normalization function.
234  *
235  * @param x Array x.
236  * @return Array normalized by its norm.
237  *
238  */
239 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.normalize(x float8[]) RETURNS float8[]
240 AS 'MODULE_PATHNAME', 'array_normalize' LANGUAGE C IMMUTABLE STRICT;
241 
242 /**
243  * @brief ARRAY_AGG aggregate for compatibility with GPDB < 4.1 and Postgres < 9.0
244  * This is a slower solution than the built in array_agg that appears
245  * in later GPDB and Postgres versions
246  */
247 CREATE AGGREGATE MADLIB_SCHEMA.array_agg( anyelement) (
248  SFUNC = array_append,
249  STYPE = anyarray
250  m4_ifdef( `__GREENPLUM__',`, PREFUNC = array_cat')
251 );