From 723a08e8146b9db31ad45d564331463ff6a7a529 Mon Sep 17 00:00:00 2001 From: javi Date: Tue, 19 Aug 2014 18:09:27 +0200 Subject: [PATCH] added CDB_Math_mode --- NEWS | 4 ++++ scripts-available/CDB_Math.sql | 26 ++++++++++++++++++++++++++ scripts-enabled/220-CDB_Math.sql | 1 + test/CDB_MathTest.sql | 4 ++++ test/CDB_MathTest_expect | 3 +++ 5 files changed, 38 insertions(+) create mode 100644 scripts-available/CDB_Math.sql create mode 120000 scripts-enabled/220-CDB_Math.sql create mode 100644 test/CDB_MathTest.sql create mode 100644 test/CDB_MathTest_expect diff --git a/NEWS b/NEWS index d1dc1fc..78594ef 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +0.3.X (2014-XX-XX) +------------------ +Added CDB_Math_Mode function + 0.3.6 (2014-08-11) ------------------ Dummy release to solve some issues with cdb branch/tag diff --git a/scripts-available/CDB_Math.sql b/scripts-available/CDB_Math.sql new file mode 100644 index 0000000..44a1888 --- /dev/null +++ b/scripts-available/CDB_Math.sql @@ -0,0 +1,26 @@ +-- CartoDB Math SQL functions + + +-- Mode +-- https://wiki.postgresql.org/wiki/Aggregate_Mode + +CREATE OR REPLACE FUNCTION cartodb._CDB_Math_final_mode(anyarray) + RETURNS anyelement AS +$BODY$ + SELECT a + FROM unnest($1) a + GROUP BY 1 + ORDER BY COUNT(1) DESC, 1 + LIMIT 1; +$BODY$ +LANGUAGE 'sql' IMMUTABLE; + +DROP AGGREGATE IF EXISTS cartodb.CDB_Math_Mode(anyelement); + +CREATE AGGREGATE cartodb.CDB_Math_Mode(anyelement) ( + SFUNC=array_append, + STYPE=anyarray, + FINALFUNC=_CDB_Math_final_mode, + INITCOND='{}' +); + diff --git a/scripts-enabled/220-CDB_Math.sql b/scripts-enabled/220-CDB_Math.sql new file mode 120000 index 0000000..613e0d8 --- /dev/null +++ b/scripts-enabled/220-CDB_Math.sql @@ -0,0 +1 @@ +../scripts-available/CDB_Math.sql \ No newline at end of file diff --git a/test/CDB_MathTest.sql b/test/CDB_MathTest.sql new file mode 100644 index 0000000..5a6d752 --- /dev/null +++ b/test/CDB_MathTest.sql @@ -0,0 +1,4 @@ + +SELECT cdb_math_mode(a) from unnest(ARRAY[1,2,2,3]) a; +SELECT cdb_math_mode(a) from unnest(ARRAY[1,2,3]) a; +SELECT cdb_math_mode(a) from unnest(ARRAY[1]) a; diff --git a/test/CDB_MathTest_expect b/test/CDB_MathTest_expect new file mode 100644 index 0000000..afe234b --- /dev/null +++ b/test/CDB_MathTest_expect @@ -0,0 +1,3 @@ +2 +1 +1