added tests improved func
This commit is contained in:
parent
dcd35fc3d7
commit
16d0dc739a
@ -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
|
DECLARE
|
||||||
diff numeric;
|
diff numeric;
|
||||||
min_val numeric;
|
min_val numeric;
|
||||||
|
max_val numeric;
|
||||||
tmp_val numeric;
|
tmp_val numeric;
|
||||||
i INT := 1;
|
i INT := 1;
|
||||||
reply numeric[];
|
reply numeric[];
|
||||||
BEGIN
|
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;
|
SELECT min(e), max(e) INTO min_val, max_val FROM ( SELECT unnest(in_array) e ) x WHERE e IS NOT NULL;
|
||||||
RAISE NOTICE 'diff = %, min_val = %', diff, min_val;
|
diff = (max_val - min_val) / breaks::numeric;
|
||||||
LOOP
|
LOOP
|
||||||
IF i < breaks + 1 THEN
|
IF i < breaks THEN
|
||||||
tmp_val = min_val + i::numeric * diff;
|
tmp_val = min_val + i::numeric * diff;
|
||||||
RAISE NOTICE 'tmp_val = %', tmp_val;
|
|
||||||
reply = array_append(reply, tmp_val);
|
reply = array_append(reply, tmp_val);
|
||||||
i := i+1;
|
i := i+1;
|
||||||
ELSE
|
ELSE
|
||||||
|
reply = array_append(reply, max_val);
|
||||||
EXIT;
|
EXIT;
|
||||||
END IF;
|
END IF;
|
||||||
END LOOP;
|
END LOOP;
|
||||||
RETURN reply;
|
RETURN reply;
|
||||||
END;
|
END;
|
||||||
$$ language plpgsql IMMUTABLE;
|
$$ language plpgsql IMMUTABLE;
|
||||||
|
5
test/CDB_EqualIntervalBinsTest.sql
Normal file
5
test/CDB_EqualIntervalBinsTest.sql
Normal file
@ -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
|
7
test/CDB_EqualIntervalBins_expect
Normal file
7
test/CDB_EqualIntervalBins_expect
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
43.57142857
|
||||||
|
86.14285714
|
||||||
|
128.71428571
|
||||||
|
171.28571429
|
||||||
|
213.85714286
|
||||||
|
256.42857143
|
||||||
|
299
|
Loading…
Reference in New Issue
Block a user