User Documentation
array_ops.sql_in
Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------- *//** 
00002  *
00003  * @file array_ops.sql_in
00004  *
00005  * @brief implementation of array operations in SQL
00006  * @date   April 2011
00007  *
00008  *
00009  *//* ----------------------------------------------------------------------- */
00010 
00011 m4_include(`SQLCommon.m4')
00012 
00013 /**
00014 @addtogroup grp_array
00015 
00016 
00017 @about
00018 
00019 This module provide a set of basic array operations implemented in C. It is a support module for several machine learning algorithms that
00020 require fast array operations. 
00021 
00022 @implementation
00023 
00024 -# At present these functions support several numeric types:
00025     - SHORTINT
00026     - INT
00027     - BIGINT
00028     - REAL
00029     - FLOAT
00030 As of now they do not support variable size NUMERIC input. 
00031 -# Also several of them may require NO NULL VALUES, while others omit NULLs and 
00032 return results.
00033 
00034 @sa File array_ops.sql_in for list of functions and usage.
00035 */
00036 
00037 /**
00038  * @brief Adds two arrays. It requires that all the values are NON-NULL. Return type is the same as the input type.
00039  *
00040  * @param x Array x
00041  * @param y Array y
00042  * @returns Sum of x and y.
00043  *
00044  */
00045 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_add(x anyarray, y anyarray) RETURNS anyarray 
00046 AS 'MODULE_PATHNAME', 'array_add'
00047 LANGUAGE C IMMUTABLE;
00048 
00049 /**
00050  * @brief Subtracts two arrays. It requires that all the values are NON-NULL. Return type is the same as the input type.
00051  *
00052  * @param x Array x
00053  * @param y Array y
00054  * @returns x-y.
00055  *
00056  */
00057 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_sub(x anyarray, y anyarray) RETURNS anyarray 
00058 AS 'MODULE_PATHNAME', 'array_sub'
00059 LANGUAGE C IMMUTABLE;
00060 
00061 /**
00062  * @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.
00063  *
00064  * @param x Array x
00065  * @param y Array y
00066  * @returns Element-wise product of x and y.
00067  *
00068  */
00069 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_mult(x anyarray, y anyarray) RETURNS anyarray 
00070 AS 'MODULE_PATHNAME', 'array_mult'
00071 LANGUAGE C IMMUTABLE;
00072 
00073 /**
00074  * @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.
00075  *
00076  * @param x Array x
00077  * @param y Array y
00078  * @returns Element-wise division of x and y.
00079  *
00080  */
00081 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_div(x anyarray, y anyarray) RETURNS anyarray 
00082 AS 'MODULE_PATHNAME', 'array_div'
00083 LANGUAGE C IMMUTABLE;
00084 
00085 /**
00086  * @brief Dot-product of two arrays. It requires that all the values are NON-NULL. Return type is the same as the input type.
00087  *
00088  * @param x Array x
00089  * @param y Array y
00090  * @returns Dot-product of x and y.
00091  *
00092  */
00093 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_dot(x anyarray, y anyarray) RETURNS FLOAT8 
00094 AS 'MODULE_PATHNAME', 'array_dot'
00095 LANGUAGE C IMMUTABLE;
00096 
00097 /**
00098  * @brief Checks whether one array contains the other.
00099  *
00100  * @param x Array x
00101  * @param y Array y
00102  * @returns Returns true if x contains y.
00103  *
00104  */
00105 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_contains(x anyarray, y anyarray) RETURNS BOOL 
00106 AS 'MODULE_PATHNAME', 'array_contains'
00107 LANGUAGE C IMMUTABLE;
00108 
00109 /**
00110  * @brief This function finds the maximum value in the array. NULLs are ignored. Return type is the same as the input type.
00111  *
00112  * @param x Array x
00113  * @returns Max of x.
00114  *
00115  */
00116 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_max(x anyarray) RETURNS anyelement 
00117 AS 'MODULE_PATHNAME', 'array_max'
00118 LANGUAGE C IMMUTABLE;
00119 
00120 /**
00121  * @brief This function finds the minimum value in the array. NULLs are ignored. Return type is the same as the input type.
00122  *
00123  * @param x Array x
00124  * @returns Min of x.
00125  *
00126  */
00127 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_min(x anyarray) RETURNS anyelement 
00128 AS 'MODULE_PATHNAME', 'array_min'
00129 LANGUAGE C IMMUTABLE;
00130 
00131 /**
00132  * @brief This function finds the sum of the values in the array. NULLs are ignored. Return type is the same as the input type.
00133  *
00134  * @param x Array x
00135  * @returns Sum of x.
00136  *
00137  */
00138 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_sum(x anyarray) RETURNS anyelement 
00139 AS 'MODULE_PATHNAME', 'array_sum'
00140 LANGUAGE C IMMUTABLE;
00141 
00142 /**
00143  * @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.
00144  *
00145  * @param x Array x
00146  * @returns Sum of x.
00147  *
00148  */
00149 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_sum_big(x anyarray) RETURNS FLOAT8 
00150 AS 'MODULE_PATHNAME', 'array_sum_big'
00151 LANGUAGE C IMMUTABLE;
00152 
00153 /**
00154  * @brief TThis function finds the mean of the values in the array. NULLs are ignored. Return type is the same as the input type.
00155  *
00156  * @param x Array x
00157  * @returns Mean of x.
00158  *
00159  */
00160 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_mean(x anyarray) RETURNS FLOAT8 
00161 AS 'MODULE_PATHNAME', 'array_mean'
00162 LANGUAGE C IMMUTABLE;
00163 
00164 /**
00165  * @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.
00166  *
00167  * @param x Array x
00168  * @returns Standard deviation of x.
00169  *
00170  */
00171 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_stddev(x anyarray) RETURNS FLOAT8 
00172 AS 'MODULE_PATHNAME', 'array_stddev'
00173 LANGUAGE C IMMUTABLE;
00174 
00175 /**
00176  * @brief This function creates an array of set size (the argument value) of FLOAT8, initializing the values to 0.0;
00177  *
00178  * @param k Array size
00179  * @returns Array of size k.
00180  *
00181  */
00182 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_of_float(k INT4) RETURNS FLOAT8[] 
00183 AS 'MODULE_PATHNAME', 'array_of_float'
00184 LANGUAGE C IMMUTABLE;
00185 
00186 /**
00187  * @brief This function creates an array of set size (the argument value) of BIGINT, initializing the values to 0;
00188  *
00189  * @param k Array size.
00190  * @returns Array of size k.
00191  *
00192  */
00193 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_of_bigint(k INT4) RETURNS INT8[] 
00194 AS 'MODULE_PATHNAME', 'array_of_bigint'
00195 LANGUAGE C IMMUTABLE;
00196 
00197 /**
00198  * @brief This functions set every values in the array to some desired value (provided as the argument).
00199  *
00200  * @param x Some array
00201  * @param k Desired value
00202  * @returns Fills array with desired value.
00203  *
00204  */
00205 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_fill(x anyarray, k anyelement) RETURNS anyarray 
00206 AS 'MODULE_PATHNAME', 'array_fill'
00207 LANGUAGE C IMMUTABLE;
00208 
00209 /**
00210  * @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.
00211  * @param x Array x
00212  * @param k Scalar
00213  * @returns Array with each element of x multiplied by scalar.
00214  *
00215  */
00216 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_scalar_mult(x anyarray, k anyelement) RETURNS anyarray 
00217 AS 'MODULE_PATHNAME', 'array_scalar_mult'
00218 LANGUAGE C IMMUTABLE;
00219 
00220 /**
00221  * @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.
00222  *
00223  * @param x Array x
00224  * @returns Square root of all elements of x.
00225  *
00226  */
00227 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.array_sqrt(x anyarray) RETURNS anyarray 
00228 AS 'MODULE_PATHNAME', 'array_sqrt'
00229 LANGUAGE C IMMUTABLE;
00230 
00231 
00232 /**
00233  * @brief Array normalization function.
00234  *
00235  * @param x Array x.
00236  * @return  Array normalized by its norm.
00237  *      
00238  */
00239 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.normalize(x float8[]) RETURNS float8[]
00240 AS 'MODULE_PATHNAME', 'array_normalize' LANGUAGE C IMMUTABLE STRICT;
00241 
00242 /**
00243  * @brief ARRAY_AGG aggregate for compatibility with GPDB < 4.1 and Postgres < 9.0
00244  *        This is a slower solution than the built in array_agg that appears
00245  *        in later GPDB and Postgres versions
00246  */
00247 CREATE AGGREGATE MADLIB_SCHEMA.array_agg( anyelement) ( 
00248    SFUNC     = array_append, 
00249    STYPE     = anyarray 
00250    m4_ifdef( `__GREENPLUM__',`, PREFUNC   = array_cat')
00251 );