Add PARALLEL and VOLATILE categories to PG functions

This commit is contained in:
Raul Marin
2017-10-31 18:07:10 +01:00
committed by Raúl Marín
parent ddd2b4e3bf
commit 0a15857a54
19 changed files with 49 additions and 51 deletions

View File

@@ -2,11 +2,11 @@
CREATE OR REPLACE FUNCTION cdb_crankshaft_version()
RETURNS text AS $$
SELECT '@@VERSION@@'::text;
$$ language 'sql' STABLE STRICT;
$$ language 'sql' IMMUTABLE STRICT PARALLEL SAFE;
-- Internal identifier of the installed extension instence
-- e.g. 'dev' for current development version
CREATE OR REPLACE FUNCTION _cdb_crankshaft_internal_version()
RETURNS text AS $$
SELECT installed_version FROM pg_available_extensions where name='crankshaft' and pg_available_extensions IS NOT NULL;
$$ language 'sql' STABLE STRICT;
$$ language 'sql' STABLE STRICT PARALLEL SAFE;

View File

@@ -6,4 +6,4 @@ _cdb_random_seeds (seed_value INTEGER) RETURNS VOID
AS $$
from crankshaft import random_seeds
random_seeds.set_random_seeds(seed_value)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;

View File

@@ -8,7 +8,7 @@ CREATE OR REPLACE FUNCTION
end if;
return array_cat(current_state,current_row) ;
END
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
-- Create aggregate if it did not exist
DO $$
@@ -24,6 +24,7 @@ BEGIN
CREATE AGGREGATE CDB_PyAgg(NUMERIC[]) (
SFUNC = CDB_PyAggS,
STYPE = Numeric[],
PARALLEL = SAFE,
INITCOND = "{}"
);
END IF;

View File

@@ -33,7 +33,7 @@ AS $$
target_ids,
model_params)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL RESTRICTED;
CREATE OR REPLACE FUNCTION
CDB_CreateAndPredictSegment (
@@ -50,4 +50,4 @@ AS $$
from crankshaft.segmentation import create_and_predict_segment
model_params = {'n_estimators': n_estimators, 'max_depth':max_depth, 'subsample' : subsample, 'learning_rate': learning_rate, 'min_samples_leaf' : min_samples_leaf}
return create_and_predict_segment(query,variable_name,target_table, model_params)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;

View File

@@ -27,7 +27,7 @@ BEGIN
RETURN QUERY
SELECT g.* FROM t, s, CDB_Gravity(t_id, t_geom, t_weight, s_id, s_geom, s_pop, target, radius, minval) g;
END;
$$ language plpgsql;
$$ language plpgsql VOLATILE PARALLEL UNSAFE;
CREATE OR REPLACE FUNCTION CDB_Gravity(
IN t_id bigint[],
@@ -112,4 +112,4 @@ BEGIN
p.targ_id = target AND
p.sourc_id = d.sourc_id;
END;
$$ language plpgsql;
$$ language plpgsql IMMUTABLE PARALLEL SAFE;

View File

@@ -24,7 +24,7 @@ BEGIN
RETURN output;
END;
$$
language plpgsql IMMUTABLE;
language plpgsql VOLATILE PARALLEL UNSAFE;
CREATE OR REPLACE FUNCTION CDB_SpatialInterpolation(
IN geomin geometry[],
@@ -141,4 +141,4 @@ BEGIN
END;
$$
language plpgsql IMMUTABLE;
language plpgsql IMMUTABLE PARALLEL SAFE;

View File

@@ -167,7 +167,7 @@ BEGIN
clipped_voro;
RETURN geomout;
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql IMMUTABLE PARALLEL SAFE;
/** ----------------------------------------------------------------------------------------
* @function : FindCircle
@@ -181,6 +181,7 @@ $$ language plpgsql IMMUTABLE;
* or NULL if three points do not form a circle.
* @history : Simon Greener - Feb 2012 - Original coding.
* Rafa de la Torre - Aug 2016 - Small fix for type checking
* Raul Marin - Sept 2017 - Remove unnecessary NULL checks and set function categories
* @copyright : Simon Greener @ 2012
* Licensed under a Creative Commons Attribution-Share Alike 2.5 Australia License. (http://creativecommons.org/licenses/by-sa/2.5/au/)
**/
@@ -203,10 +204,6 @@ DECLARE
v_dF NUMERIC;
v_dG NUMERIC;
BEGIN
IF ( p_pt1 IS NULL OR p_pt2 IS NULL OR p_pt3 IS NULL ) THEN
RAISE EXCEPTION 'All supplied points must be not null.';
RETURN NULL;
END IF;
IF ( ST_GeometryType(p_pt1) <> 'ST_Point' OR
ST_GeometryType(p_pt2) <> 'ST_Point' OR
ST_GeometryType(p_pt3) <> 'ST_Point' ) THEN
@@ -232,5 +229,5 @@ BEGIN
RETURN ST_SetSRID(ST_MakePoint(v_CX, v_CY, v_radius),ST_Srid(p_pt1));
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT;
LANGUAGE plpgsql IMMUTABLE STRICT PARALLEL SAFE;

View File

@@ -15,7 +15,7 @@ AS $$
moran = Moran()
return moran.global_stat(subquery, column_name, w_type,
num_ngbrs, permutations, geom_col, id_col)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;
-- Moran's I Local (internal function)
CREATE OR REPLACE FUNCTION
@@ -34,7 +34,7 @@ AS $$
# TODO: use named parameters or a dictionary
return moran.local_stat(subquery, column_name, w_type,
num_ngbrs, permutations, geom_col, id_col)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;
-- Moran's I Local (public-facing function)
CREATE OR REPLACE FUNCTION
@@ -52,7 +52,7 @@ AS $$
SELECT moran, quads, significance, rowid, vals
FROM cdb_crankshaft._CDB_AreasOfInterestLocal(subquery, column_name, w_type, num_ngbrs, permutations, geom_col, id_col);
$$ LANGUAGE SQL;
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
-- Moran's I only for HH and HL (public-facing function)
CREATE OR REPLACE FUNCTION
@@ -71,7 +71,7 @@ AS $$
FROM cdb_crankshaft._CDB_AreasOfInterestLocal(subquery, column_name, w_type, num_ngbrs, permutations, geom_col, id_col)
WHERE quads IN ('HH', 'HL');
$$ LANGUAGE SQL;
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
-- Moran's I only for LL and LH (public-facing function)
CREATE OR REPLACE FUNCTION
@@ -90,7 +90,7 @@ AS $$
FROM cdb_crankshaft._CDB_AreasOfInterestLocal(subquery, attr, w_type, num_ngbrs, permutations, geom_col, id_col)
WHERE quads IN ('LL', 'LH');
$$ LANGUAGE SQL;
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
-- Moran's I only for LH and HL (public-facing function)
CREATE OR REPLACE FUNCTION
@@ -109,7 +109,7 @@ AS $$
FROM cdb_crankshaft._CDB_AreasOfInterestLocal(subquery, attr, w_type, num_ngbrs, permutations, geom_col, id_col)
WHERE quads IN ('HL', 'LH');
$$ LANGUAGE SQL;
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
-- Moran's I Global Rate (public-facing function)
CREATE OR REPLACE FUNCTION
@@ -129,7 +129,7 @@ AS $$
# TODO: use named parameters or a dictionary
return moran.global_rate_stat(subquery, numerator, denominator, w_type,
num_ngbrs, permutations, geom_col, id_col)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;
-- Moran's I Local Rate (internal function)
@@ -150,7 +150,7 @@ AS $$
moran = Moran()
# TODO: use named parameters or a dictionary
return moran.local_rate_stat(subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;
-- Moran's I Local Rate (public-facing function)
CREATE OR REPLACE FUNCTION
@@ -170,7 +170,7 @@ AS $$
SELECT moran, quads, significance, rowid, vals
FROM cdb_crankshaft._CDB_AreasOfInterestLocalRate(subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col);
$$ LANGUAGE SQL;
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
-- Moran's I Local Rate only for HH and HL (public-facing function)
CREATE OR REPLACE FUNCTION
@@ -191,7 +191,7 @@ AS $$
FROM cdb_crankshaft._CDB_AreasOfInterestLocalRate(subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col)
WHERE quads IN ('HH', 'HL');
$$ LANGUAGE SQL;
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
-- Moran's I Local Rate only for LL and LH (public-facing function)
CREATE OR REPLACE FUNCTION
@@ -212,7 +212,7 @@ AS $$
FROM cdb_crankshaft._CDB_AreasOfInterestLocalRate(subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col)
WHERE quads IN ('LL', 'LH');
$$ LANGUAGE SQL;
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;
-- Moran's I Local Rate only for LH and HL (public-facing function)
CREATE OR REPLACE FUNCTION
@@ -233,4 +233,4 @@ AS $$
FROM cdb_crankshaft._CDB_AreasOfInterestLocalRate(subquery, numerator, denominator, w_type, num_ngbrs, permutations, geom_col, id_col)
WHERE quads IN ('HL', 'LH');
$$ LANGUAGE SQL;
$$ LANGUAGE SQL VOLATILE PARALLEL UNSAFE;

View File

@@ -7,7 +7,7 @@ RETURNS table (cartodb_id integer, cluster_no integer) as $$
kmeans = Kmeans()
return kmeans.spatial(query, no_clusters, no_init)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;
CREATE OR REPLACE FUNCTION CDB_WeightedMeanS(state Numeric[],the_geom GEOMETRY(Point, 4326), weight NUMERIC)
@@ -30,7 +30,7 @@ BEGIN
RETURN Array[newX,newY,newW];
END
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
CREATE OR REPLACE FUNCTION CDB_WeightedMeanF(state Numeric[])
RETURNS GEOMETRY AS
@@ -42,7 +42,7 @@ BEGIN
RETURN ST_SETSRID(ST_MakePoint(state[1]/state[3], state[2]/state[3]),4326);
END IF;
END
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
-- Create aggregate if it did not exist
DO $$
@@ -59,6 +59,7 @@ BEGIN
SFUNC = CDB_WeightedMeanS,
FINALFUNC = CDB_WeightedMeanF,
STYPE = Numeric[],
PARALLEL = SAFE,
INITCOND = "{0.0,0.0,0.0}"
);
END IF;

View File

@@ -27,7 +27,7 @@ AS $$
## TODO: use named parameters or a dictionary
return markov.spatial_trend(subquery, time_cols, num_classes, w_type, num_ngbrs, permutations, geom_col, id_col)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;
-- input table format: identical to above but in a predictable format
-- Sample function call:

View File

@@ -91,7 +91,7 @@ BEGIN
RETURN ST_transform(ST_Centroid(best_c), 4326);
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql IMMUTABLE PARALLEL SAFE;
-- signed distance point to polygon with holes
@@ -120,4 +120,4 @@ BEGIN
dist := dist * within::numeric;
RETURN dist;
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql IMMUTABLE PARALLEL SAFE;

View File

@@ -64,4 +64,4 @@ BEGIN
END LOOP;
RETURN QUERY SELECT unnest(geotemp ) as geomout, unnest(coltemp ) as colout;
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql IMMUTABLE PARALLEL SAFE;

View File

@@ -40,4 +40,4 @@ BEGIN
END LOOP;
RETURN QUERY SELECT unnest(gs) as geomout, unnest(coltemp ) as colout;
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql IMMUTABLE PARALLEL SAFE;

View File

@@ -14,6 +14,6 @@ AS $$
from crankshaft.clustering import Getis
getis = Getis()
return getis.getis_ord(subquery, column_name, w_type, num_ngbrs, permutations, geom_col, id_col)
$$ LANGUAGE plpythonu;
$$ LANGUAGE plpythonu VOLATILE PARALLEL UNSAFE;
-- TODO: make a version that accepts the values as arrays

View File

@@ -9,7 +9,7 @@ BEGIN
RETURN column_value > threshold;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE ;
-- Find outliers by a percentage above the threshold
-- TODO: add symmetric option? `is_symmetric boolean DEFAULT false`
@@ -38,7 +38,7 @@ BEGIN
unnest(ids) As rowid;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;
-- Find outliers above a given number of standard deviations from the mean
@@ -72,4 +72,4 @@ BEGIN
SELECT unnest(out_vals) As is_outlier,
unnest(ids) As rowid;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE;

View File

@@ -139,8 +139,7 @@ BEGIN
where final.bin is not null
;
END;
$$ language plpgsql;
$$ language plpgsql VOLATILE PARALLEL RESTRICTED;
-- =====================================================================
@@ -205,4 +204,4 @@ BEGIN
RETURN output;
END;
$$
language plpgsql;
language plpgsql IMMUTABLE PARALLEL SAFE;

View File

@@ -35,4 +35,4 @@ BEGIN
INTO result;
RETURN result;
END;
$$ LANGUAGE plpgsql;
$$ LANGUAGE plpgsql STABLE PARALLEL SAFE;

View File

@@ -51,4 +51,4 @@ BEGIN
RETURN ST_Collect(points);
END;
$$
LANGUAGE plpgsql VOLATILE;
LANGUAGE plpgsql VOLATILE PARALLEL RESTRICTED;

View File

@@ -93,7 +93,7 @@ BEGIN
RETURN;
END
$$ LANGUAGE 'plpgsql' IMMUTABLE;
$$ LANGUAGE 'plpgsql' IMMUTABLE PARALLEL SAFE;
--
-- Calculate the equal interval bins for a given column
@@ -131,7 +131,7 @@ BEGIN
END LOOP;
RETURN reply;
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql IMMUTABLE PARALLEL SAFE;
--
-- Determine the Heads/Tails classifications from a numeric array
@@ -178,7 +178,7 @@ BEGIN
END LOOP;
RETURN reply;
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql IMMUTABLE PARALLEL SAFE;
--
-- Determine the Jenks classifications from a numeric array
@@ -299,7 +299,7 @@ BEGIN
RETURN (best_result)[2:array_upper(best_result, 1)];
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql VOLATILE PARALLEL RESTRICTED;
@@ -399,7 +399,7 @@ BEGIN
RETURN array_prepend(gvf, reply);
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql IMMUTABLE PARALLEL SAFE;
--
@@ -444,4 +444,4 @@ BEGIN
END LOOP;
RETURN reply;
END;
$$ language plpgsql IMMUTABLE;
$$ language plpgsql IMMUTABLE STRICT PARALLEL SAFE;