From 2ad3ff547d3c4c0987702efdc40122ba8988136c Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Thu, 7 May 2015 15:49:35 -0400 Subject: [PATCH 1/8] initial --- scripts-available/CDB_EqualIntervalBins.sql | 36 +++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 scripts-available/CDB_EqualIntervalBins.sql diff --git a/scripts-available/CDB_EqualIntervalBins.sql b/scripts-available/CDB_EqualIntervalBins.sql new file mode 100644 index 0000000..2c2eadc --- /dev/null +++ b/scripts-available/CDB_EqualIntervalBins.sql @@ -0,0 +1,36 @@ +-- +-- Calculate the equal interval bins for a given column +-- +-- @param in_array A numeric array of numbers to determine the best +-- to determine the bin boundary +-- +-- @param breaks The number of bins you want to find. +-- +-- +-- Returns: upper edges of bins +-- +-- + +CREATE OR REPLACE FUNCTION CDB_EqualIntervalBins ( in_array NUMERIC[], breaks INT) RETURNS NUMERIC[] as $$ +DECLARE + diff numeric; + min_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; + LOOP + IF i < breaks + 1 THEN + tmp_val = min_val + i::numeric * diff; + RAISE NOTICE 'tmp_val = %', tmp_val; + reply = array_append(reply, tmp_val); + i := i+1; + ELSE + EXIT; + END IF; + END LOOP; + RETURN reply; +END; +$$ language plpgsql IMMUTABLE; From 16d0dc739afb1ef2a6b4c9dee94e267b6e20af18 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Thu, 14 May 2015 15:32:58 -0400 Subject: [PATCH 2/8] 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 From ee1df92561b1420eb0acf0de09351850117316c7 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Thu, 14 May 2015 15:45:57 -0400 Subject: [PATCH 3/8] fixed precision in tests --- test/CDB_EqualIntervalBinsTest.sql | 2 +- test/CDB_EqualIntervalBins_expect | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/test/CDB_EqualIntervalBinsTest.sql b/test/CDB_EqualIntervalBinsTest.sql index 2abaef7..f527b5b 100644 --- a/test/CDB_EqualIntervalBinsTest.sql +++ b/test/CDB_EqualIntervalBinsTest.sql @@ -2,4 +2,4 @@ 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 +SELECT round(unnest(CDB_EqualIntervalBins(s, 7)),7) FROM data \ No newline at end of file diff --git a/test/CDB_EqualIntervalBins_expect b/test/CDB_EqualIntervalBins_expect index 1e10fb8..2ad9fb0 100644 --- a/test/CDB_EqualIntervalBins_expect +++ b/test/CDB_EqualIntervalBins_expect @@ -1,7 +1,7 @@ -43.57142857 -86.14285714 -128.71428571 -171.28571429 -213.85714286 -256.42857143 +43.5714286 +86.1428571 +128.7142857 +171.2857143 +213.8571429 +256.4285714 299 From d0c85855f5ba0492adb2c08cede10a85e8ba079a Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Mon, 18 May 2015 12:00:42 -0400 Subject: [PATCH 4/8] fixed test expectation value --- test/CDB_EqualIntervalBinsTest.sql | 2 +- test/CDB_EqualIntervalBins_expect | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/CDB_EqualIntervalBinsTest.sql b/test/CDB_EqualIntervalBinsTest.sql index f527b5b..1b8426a 100644 --- a/test/CDB_EqualIntervalBinsTest.sql +++ b/test/CDB_EqualIntervalBinsTest.sql @@ -1,5 +1,5 @@ WITH data AS ( - SELECT array_agg(x) s FROM generate_series(1,300) x + SELECT array_agg(x::numeric) s FROM generate_series(1,300) x WHERE x % 5 != 0 AND x % 7 != 0 ) SELECT round(unnest(CDB_EqualIntervalBins(s, 7)),7) FROM data \ No newline at end of file diff --git a/test/CDB_EqualIntervalBins_expect b/test/CDB_EqualIntervalBins_expect index 2ad9fb0..df45bc4 100644 --- a/test/CDB_EqualIntervalBins_expect +++ b/test/CDB_EqualIntervalBins_expect @@ -4,4 +4,4 @@ 171.2857143 213.8571429 256.4285714 -299 +299.0000000 From 737dc1c1f14a39d5cb71e8cc63b11f5fba9c6612 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Mon, 18 May 2015 12:12:55 -0400 Subject: [PATCH 5/8] updated formating of test --- test/CDB_EqualIntervalBins_expect | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/CDB_EqualIntervalBins_expect b/test/CDB_EqualIntervalBins_expect index df45bc4..83bcc2c 100644 --- a/test/CDB_EqualIntervalBins_expect +++ b/test/CDB_EqualIntervalBins_expect @@ -1,5 +1,5 @@ -43.5714286 -86.1428571 + 43.5714286 + 86.1428571 128.7142857 171.2857143 213.8571429 From a3c8d7bce484af27fa8b20ff135198a1b9130d6f Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 19 May 2015 15:28:17 -0400 Subject: [PATCH 6/8] initial commit --- scripts-enabled/240-CDB_EqualIntervalBins.sql | 1 + 1 file changed, 1 insertion(+) create mode 120000 scripts-enabled/240-CDB_EqualIntervalBins.sql diff --git a/scripts-enabled/240-CDB_EqualIntervalBins.sql b/scripts-enabled/240-CDB_EqualIntervalBins.sql new file mode 120000 index 0000000..88c35b5 --- /dev/null +++ b/scripts-enabled/240-CDB_EqualIntervalBins.sql @@ -0,0 +1 @@ +../scripts-available/CDB_EqualIntervalBins.sql \ No newline at end of file From bf4a31842b503cd039bda4a5ae087a2d5feccc8b Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 19 May 2015 15:50:34 -0400 Subject: [PATCH 7/8] new result --- test/CDB_EqualIntervalBins_expect | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CDB_EqualIntervalBins_expect b/test/CDB_EqualIntervalBins_expect index 83bcc2c..fdc3a68 100644 --- a/test/CDB_EqualIntervalBins_expect +++ b/test/CDB_EqualIntervalBins_expect @@ -4,4 +4,4 @@ 171.2857143 213.8571429 256.4285714 -299.0000000 +299 \ No newline at end of file From 42a617e79c3ab77a1e7ee713510ce06696772f9a Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 19 May 2015 16:03:54 -0400 Subject: [PATCH 8/8] ugh bad filename --- ...IntervalBins_expect => CDB_EqualIntervalBinsTest_expect} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename test/{CDB_EqualIntervalBins_expect => CDB_EqualIntervalBinsTest_expect} (58%) diff --git a/test/CDB_EqualIntervalBins_expect b/test/CDB_EqualIntervalBinsTest_expect similarity index 58% rename from test/CDB_EqualIntervalBins_expect rename to test/CDB_EqualIntervalBinsTest_expect index fdc3a68..df45bc4 100644 --- a/test/CDB_EqualIntervalBins_expect +++ b/test/CDB_EqualIntervalBinsTest_expect @@ -1,7 +1,7 @@ - 43.5714286 - 86.1428571 +43.5714286 +86.1428571 128.7142857 171.2857143 213.8571429 256.4285714 -299 \ No newline at end of file +299.0000000