From 16d0dc739afb1ef2a6b4c9dee94e267b6e20af18 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Thu, 14 May 2015 15:32:58 -0400 Subject: [PATCH] added tests improved func --- scripts-available/CDB_EqualIntervalBins.sql | 13 +++++++------ test/CDB_EqualIntervalBinsTest.sql | 5 +++++ test/CDB_EqualIntervalBins_expect | 7 +++++++ 3 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 test/CDB_EqualIntervalBinsTest.sql create mode 100644 test/CDB_EqualIntervalBins_expect diff --git a/scripts-available/CDB_EqualIntervalBins.sql b/scripts-available/CDB_EqualIntervalBins.sql index 2c2eadc..5f98084 100644 --- a/scripts-available/CDB_EqualIntervalBins.sql +++ b/scripts-available/CDB_EqualIntervalBins.sql @@ -11,26 +11,27 @@ -- -- -CREATE OR REPLACE FUNCTION CDB_EqualIntervalBins ( in_array NUMERIC[], breaks INT) RETURNS NUMERIC[] as $$ +CREATE OR REPLACE FUNCTION CDB_EqualIntervalBins ( in_array NUMERIC[], breaks INT ) RETURNS NUMERIC[] as $$ DECLARE diff numeric; min_val numeric; + max_val numeric; tmp_val numeric; i INT := 1; reply numeric[]; BEGIN - SELECT (max(e) - min(e)) / breaks::numeric, min(e) INTO diff, min_val FROM (SELECT unnest(in_array) e) x WHERE e is not null; - RAISE NOTICE 'diff = %, min_val = %', diff, min_val; + SELECT min(e), max(e) INTO min_val, max_val FROM ( SELECT unnest(in_array) e ) x WHERE e IS NOT NULL; + diff = (max_val - min_val) / breaks::numeric; LOOP - IF i < breaks + 1 THEN + IF i < breaks THEN tmp_val = min_val + i::numeric * diff; - RAISE NOTICE 'tmp_val = %', tmp_val; reply = array_append(reply, tmp_val); i := i+1; ELSE + reply = array_append(reply, max_val); EXIT; END IF; END LOOP; RETURN reply; -END; +END; $$ language plpgsql IMMUTABLE; diff --git a/test/CDB_EqualIntervalBinsTest.sql b/test/CDB_EqualIntervalBinsTest.sql new file mode 100644 index 0000000..2abaef7 --- /dev/null +++ b/test/CDB_EqualIntervalBinsTest.sql @@ -0,0 +1,5 @@ +WITH data AS ( + SELECT array_agg(x) s FROM generate_series(1,300) x + WHERE x % 5 != 0 AND x % 7 != 0 + ) +SELECT unnest(CDB_EqualIntervalBins(s, 7)) FROM data \ No newline at end of file diff --git a/test/CDB_EqualIntervalBins_expect b/test/CDB_EqualIntervalBins_expect new file mode 100644 index 0000000..1e10fb8 --- /dev/null +++ b/test/CDB_EqualIntervalBins_expect @@ -0,0 +1,7 @@ +43.57142857 +86.14285714 +128.71428571 +171.28571429 +213.85714286 +256.42857143 +299