diff --git a/src/pg/sql/04_py_agg.sql b/src/pg/sql/04_py_agg.sql index c38e323..a3e881b 100644 --- a/src/pg/sql/04_py_agg.sql +++ b/src/pg/sql/04_py_agg.sql @@ -10,10 +10,22 @@ CREATE OR REPLACE FUNCTION END $$ LANGUAGE plpgsql; - -CREATE AGGREGATE CDB_PyAgg(NUMERIC[])( - SFUNC = CDB_PyAggS, - STYPE = Numeric[], - INITCOND = "{}" -); - +-- Create aggregate if it did not exist +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT * + FROM pg_catalog.pg_proc p + LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace + WHERE n.nspname = 'cdb_crankshaft' + AND p.proname = 'cdb_pyagg' + AND p.proisagg) + THEN + CREATE AGGREGATE CDB_PyAgg(NUMERIC[]) ( + SFUNC = CDB_PyAggS, + STYPE = Numeric[], + INITCOND = "{}" + ); + END IF; +END +$$ LANGUAGE plpgsql; diff --git a/src/pg/sql/11_kmeans.sql b/src/pg/sql/11_kmeans.sql index 125aac3..f20942f 100644 --- a/src/pg/sql/11_kmeans.sql +++ b/src/pg/sql/11_kmeans.sql @@ -41,9 +41,23 @@ BEGIN END $$ LANGUAGE plpgsql; -CREATE AGGREGATE CDB_WeightedMean(geometry(Point, 4326), NUMERIC)( - SFUNC = CDB_WeightedMeanS, - FINALFUNC = CDB_WeightedMeanF, - STYPE = Numeric[], - INITCOND = "{0.0,0.0,0.0}" -); +-- Create aggregate if it did not exist +DO $$ +BEGIN + IF NOT EXISTS ( + SELECT * + FROM pg_catalog.pg_proc p + LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace + WHERE n.nspname = 'cdb_crankshaft' + AND p.proname = 'cdb_weightedmean' + AND p.proisagg) + THEN + CREATE AGGREGATE CDB_WeightedMean(geometry(Point, 4326), NUMERIC) ( + SFUNC = CDB_WeightedMeanS, + FINALFUNC = CDB_WeightedMeanF, + STYPE = Numeric[], + INITCOND = "{0.0,0.0,0.0}" + ); + END IF; +END +$$ LANGUAGE plpgsql;