MADlib
0.7 A newer version is available
User Documentation
|
00001 /* ----------------------------------------------------------------------- *//** 00002 * 00003 * @file utilities.sql_in 00004 * 00005 * @brief SQL functions for carrying out routine tasks 00006 * 00007 * @sa For a brief overview of utility functions, see the 00008 * module description \ref grp_utilities. 00009 * 00010 *//* ----------------------------------------------------------------------- */ 00011 00012 m4_include(`SQLCommon.m4') --' 00013 m4_changequote(<!,!>) 00014 00015 /** 00016 @addtogroup grp_utilities 00017 00018 @about 00019 00020 The utilty module provides functions for routine tasks that need no further 00021 explanation. 00022 00023 @sa File utilities.sql_in documenting the SQL functions. 00024 */ 00025 00026 /** 00027 * @brief Return MADlib build information. 00028 * 00029 * @returns Summary of MADlib build information, consisting of MADlib version, 00030 * git revision, cmake configuration time, build type, build system, 00031 * C compiler, and C++ compiler 00032 */ 00033 CREATE FUNCTION MADLIB_SCHEMA.version() 00034 RETURNS TEXT 00035 LANGUAGE sql 00036 IMMUTABLE 00037 AS $$ 00038 SELECT ( 00039 'MADlib version: __MADLIB_VERSION__, ' 00040 'git revision: __MADLIB_GIT_REVISION__, ' 00041 'cmake configuration time: __MADLIB_BUILD_TIME__, ' 00042 'build type: __MADLIB_BUILD_TYPE__, ' 00043 'build system: __MADLIB_BUILD_SYSTEM__, ' 00044 'C compiler: __MADLIB_C_COMPILER__, ' 00045 'C++ compiler: __MADLIB_CXX_COMPILER__')::TEXT 00046 $$; 00047 00048 00049 /** 00050 * @brief Raise an exception if the given condition is not satisfied. 00051 */ 00052 CREATE FUNCTION MADLIB_SCHEMA.assert(condition BOOLEAN, msg VARCHAR) 00053 RETURNS VOID 00054 LANGUAGE plpgsql 00055 IMMUTABLE 00056 AS $$ 00057 BEGIN 00058 IF NOT condition THEN 00059 RAISE EXCEPTION 'Failed assertion: %', msg; 00060 END IF; 00061 END 00062 $$; 00063 00064 ------------------------------------------------------------------------ 00065 00066 /** 00067 * @brief Compute the relative error of an approximate value 00068 */ 00069 CREATE FUNCTION MADLIB_SCHEMA.relative_error( 00070 approx DOUBLE PRECISION, 00071 value DOUBLE PRECISION 00072 ) RETURNS DOUBLE PRECISION 00073 LANGUAGE sql 00074 AS $$ 00075 SELECT abs(($1 - $2)/$2) 00076 $$; 00077 00078 ------------------------------------------------------------------------ 00079 00080 /** 00081 * @brief Compute the relative error (w.r.t. the 2-norm) of an apprixmate vector 00082 */ 00083 CREATE FUNCTION MADLIB_SCHEMA.relative_error( 00084 approx DOUBLE PRECISION[], 00085 value DOUBLE PRECISION[] 00086 ) RETURNS DOUBLE PRECISION 00087 LANGUAGE sql 00088 AS $$ 00089 SELECT MADLIB_SCHEMA.dist_norm2($1, $2) / MADLIB_SCHEMA.norm2($2) 00090 $$; 00091 00092 ------------------------------------------------------------------------ 00093 00094 /** 00095 * @brief Check if a SQL statement raises an error 00096 * 00097 * @param sql The SQL statement 00098 * @returns \c TRUE if an exception is raised while executing \c sql, \c FALSE 00099 * otherwise. 00100 */ 00101 CREATE FUNCTION MADLIB_SCHEMA.check_if_raises_error( 00102 sql TEXT 00103 ) RETURNS BOOLEAN 00104 LANGUAGE plpgsql 00105 AS $$ 00106 BEGIN 00107 EXECUTE sql; 00108 RETURN FALSE; 00109 EXCEPTION 00110 WHEN OTHERS THEN 00111 RETURN TRUE; 00112 END; 00113 $$; 00114 00115 ------------------------------------------------------------------------ 00116 00117 /** 00118 * @brief Check if a floating-point number is NaN (not a number) 00119 * 00120 * This function exists for portability. Some DBMSs like PostgreSQL treat 00121 * floating-point numbers as fully ordered -- contrary to IEEE 754. (See, e.g., 00122 * the <a href= 00123 * "http://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-FLOAT" 00124 * >PostgreSQL documentation</a>. For portability, MADlib code should not make 00125 * use of such "features" directly, but only use isnan() instead. 00126 * 00127 * @param number 00128 * @returns \c TRUE if \c number is \c NaN, \c FALSE otherwise 00129 */ 00130 CREATE FUNCTION MADLIB_SCHEMA.isnan( 00131 number DOUBLE PRECISION 00132 ) RETURNS BOOLEAN 00133 LANGUAGE sql 00134 AS $$ 00135 SELECT $1 = 'NaN'::DOUBLE PRECISION; 00136 $$; 00137 00138 ------------------------------------------------------------------------ 00139 00140 /** 00141 * @brief Create the temporary schema if it does not exist yet 00142 */ 00143 CREATE FUNCTION MADLIB_SCHEMA.create_schema_pg_temp() 00144 RETURNS VOID 00145 LANGUAGE plpgsql 00146 VOLATILE 00147 AS $$ 00148 BEGIN 00149 -- pg_my_temp_schema() is a built-in function 00150 IF pg_my_temp_schema() = 0 THEN 00151 -- The pg_temp schema does not exist, yet. Creating a temporary table 00152 -- will create it. Note: There is *no* race condition here, because 00153 -- every session has its own temp schema. 00154 EXECUTE 'CREATE TEMPORARY TABLE _madlib_temp_table AS SELECT 1; 00155 DROP TABLE pg_temp._madlib_temp_table CASCADE;'; 00156 END IF; 00157 END; 00158 $$; 00159 00160 ------------------------------------------------------------------------ 00161 00162 /** 00163 * @brief Create volatile noop function 00164 * 00165 * The only use of this function is as an optimization fence when used in the 00166 * SELECT list of a query. See, e.g., 00167 * http://archives.postgresql.org/pgsql-sql/2012-07/msg00030.php 00168 */ 00169 CREATE FUNCTION MADLIB_SCHEMA.noop() 00170 RETURNS VOID 00171 VOLATILE 00172 LANGUAGE c 00173 AS 'MODULE_PATHNAME'; 00174 00175 ------------------------------------------------------------------------ 00176 00177 /* 00178 * Create type bytea8 with 8-byte alignment. 00179 */ 00180 CREATE TYPE MADLIB_SCHEMA.bytea8; 00181 00182 CREATE FUNCTION MADLIB_SCHEMA.bytea8in(cstring) 00183 RETURNS MADLIB_SCHEMA.bytea8 AS 'byteain' 00184 LANGUAGE internal IMMUTABLE STRICT; 00185 00186 CREATE FUNCTION MADLIB_SCHEMA.bytea8out(MADLIB_SCHEMA.bytea8) 00187 RETURNS cstring AS 'byteaout' 00188 LANGUAGE internal IMMUTABLE STRICT; 00189 00190 CREATE FUNCTION MADLIB_SCHEMA.bytea8recv(internal) 00191 RETURNS MADLIB_SCHEMA.bytea8 AS 'bytearecv' 00192 LANGUAGE internal IMMUTABLE STRICT; 00193 00194 CREATE FUNCTION MADLIB_SCHEMA.bytea8send(MADLIB_SCHEMA.bytea8) 00195 RETURNS bytea AS 'byteasend' 00196 LANGUAGE internal IMMUTABLE STRICT; 00197 00198 CREATE TYPE MADLIB_SCHEMA.bytea8( 00199 INPUT = MADLIB_SCHEMA.bytea8in, 00200 OUTPUT = MADLIB_SCHEMA.bytea8out, 00201 RECEIVE = MADLIB_SCHEMA.bytea8recv, 00202 SEND = MADLIB_SCHEMA.bytea8send, 00203 ALIGNMENT = double 00204 ); 00205 00206 ------------------------------------------------------------------------ 00207 /** 00208 * @brief Generate random remporary names for temp table and other names 00209 */ 00210 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.__unique_string () 00211 RETURNS VARCHAR AS $$ 00212 PythonFunction(utilities, utilities, __unique_string) 00213 $$ LANGUAGE plpythonu; 00214 00215 ------------------------------------------------------------------------ 00216 /** 00217 * @brief Cast boolean into text 00218 */ 00219 CREATE OR REPLACE FUNCTION MADLIB_SCHEMA.bool_to_text (BOOLEAN) 00220 RETURNS TEXT 00221 STRICT 00222 LANGUAGE SQL AS ' 00223 SELECT CASE 00224 WHEN $1 THEN ''t'' 00225 ELSE ''f'' 00226 END; 00227 '; 00228 00229 m4_ifdef(`__GREENPLUM__', ` 00230 CREATE CAST (BOOLEAN AS TEXT) 00231 WITH FUNCTION MADLIB_SCHEMA.bool_to_text(BOOLEAN) 00232 AS ASSIGNMENT; 00233 ') 00234 00235 00236 ------------------------------------------------------------------------ 00237 00238 /* 00239 * An array_agg() function is defined in module array_ops (to compatibility with 00240 * GP 4.0. 00241 */ 00242 00243 m4_changequote(<!`!>,<!'!>)