From cbd3c447b6aab75fad33a3bd9c7d1e6af35a0872 Mon Sep 17 00:00:00 2001 From: sariogonfer Date: Tue, 14 Apr 2015 20:45:37 +0200 Subject: [PATCH 01/62] Update CDB_QuantileBins.sql --- scripts-available/CDB_QuantileBins.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts-available/CDB_QuantileBins.sql b/scripts-available/CDB_QuantileBins.sql index d0a0374..ba73496 100644 --- a/scripts-available/CDB_QuantileBins.sql +++ b/scripts-available/CDB_QuantileBins.sql @@ -18,15 +18,15 @@ BEGIN -- get our unique values SELECT array_agg(e) INTO in_array FROM (SELECT unnest(in_array) e GROUP BY e ORDER BY e ASC) x; -- get the total size of our row - element_count := array_upper(in_array, 1) - array_lower(in_array, 1); + element_count := array_length(in_array, 1); break_size := element_count::numeric / breaks; -- slice our bread LOOP IF i > breaks THEN EXIT; END IF; - SELECT e INTO tmp_val FROM ( SELECT unnest(in_array) e LIMIT 1 OFFSET round(break_size * i)) x; + SELECT e INTO tmp_val FROM ( SELECT unnest(in_array) e LIMIT 1 OFFSET round(break_size * i) - 1) x; reply = array_append(reply, tmp_val); i := i+1; END LOOP; RETURN reply; END; -$$ language plpgsql IMMUTABLE; \ No newline at end of file +$$ language plpgsql IMMUTABLE; From 1552c03dd4444f44c34062a123a2112e469afcdb Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Mon, 27 Apr 2015 17:59:40 -0400 Subject: [PATCH 02/62] removed group by; made binning more reliable --- scripts-available/CDB_QuantileBins.sql | 27 ++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/scripts-available/CDB_QuantileBins.sql b/scripts-available/CDB_QuantileBins.sql index ba73496..23e5dcc 100644 --- a/scripts-available/CDB_QuantileBins.sql +++ b/scripts-available/CDB_QuantileBins.sql @@ -15,18 +15,29 @@ DECLARE i INT := 1; reply numeric[]; BEGIN - -- get our unique values - SELECT array_agg(e) INTO in_array FROM (SELECT unnest(in_array) e GROUP BY e ORDER BY e ASC) x; - -- get the total size of our row + -- sort our values + SELECT array_agg(e) INTO in_array FROM (SELECT unnest(in_array) e ORDER BY e ASC) x; + -- get the total size of our data element_count := array_length(in_array, 1); break_size := element_count::numeric / breaks; -- slice our bread LOOP - IF i > breaks THEN EXIT; END IF; - SELECT e INTO tmp_val FROM ( SELECT unnest(in_array) e LIMIT 1 OFFSET round(break_size * i) - 1) x; + IF i < breaks THEN + IF break_size * i % 1 > 0 THEN + SELECT e INTO tmp_val FROM ( SELECT unnest(in_array) e LIMIT 1 OFFSET ceil(break_size * i) - 1) x; + ELSE + SELECT avg(e) INTO tmp_val FROM ( SELECT unnest(in_array) e LIMIT 2 OFFSET ceil(break_size * i) - 1 ) x; + END IF; + ELSIF i = breaks THEN + -- select the last value + SELECT max(e) INTO tmp_val FROM ( SELECT unnest(in_array) e ) x; + ELSE + EXIT; + END IF; + reply = array_append(reply, tmp_val); - i := i+1; - END LOOP; - RETURN reply; + i := i+1; + END LOOP; + RETURN reply; END; $$ language plpgsql IMMUTABLE; From 7d0efa95fb634652053c7cd66735a48947d38eb4 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 28 Apr 2015 09:59:57 -0400 Subject: [PATCH 03/62] updated test --- test/CDB_QuantileBinsTest_expect | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CDB_QuantileBinsTest_expect b/test/CDB_QuantileBinsTest_expect index aebc9ad..e50c251 100644 --- a/test/CDB_QuantileBinsTest_expect +++ b/test/CDB_QuantileBinsTest_expect @@ -3,5 +3,5 @@ 43 57 71 -83 +86 99 From d9e254dbd55d699aff6d1ae01c034c237c40a165 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 28 Apr 2015 10:21:34 -0400 Subject: [PATCH 04/62] missed updating value --- test/CDB_QuantileBinsTest_expect | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CDB_QuantileBinsTest_expect b/test/CDB_QuantileBinsTest_expect index e50c251..46c2997 100644 --- a/test/CDB_QuantileBinsTest_expect +++ b/test/CDB_QuantileBinsTest_expect @@ -1,4 +1,4 @@ -16 +13 29 43 57 From 2ad3ff547d3c4c0987702efdc40122ba8988136c Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Thu, 7 May 2015 15:49:35 -0400 Subject: [PATCH 05/62] 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 06/62] 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 07/62] 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 08/62] 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 09/62] 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 10/62] 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 11/62] 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 12/62] 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 From ba521461fe5c46d85420ec22f704fcf7adf01b7d Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Mon, 1 Jun 2015 09:46:19 -0700 Subject: [PATCH 13/62] Replace CDB_UserTables with new version fix for #64 --- scripts-available/CDB_UserTables.sql | 42 ++++++++++------------------ 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/scripts-available/CDB_UserTables.sql b/scripts-available/CDB_UserTables.sql index 40e2f51..1b211b7 100644 --- a/scripts-available/CDB_UserTables.sql +++ b/scripts-available/CDB_UserTables.sql @@ -6,34 +6,22 @@ -- Currently accepted permissions are: 'public', 'private' or 'all' -- CREATE OR REPLACE FUNCTION CDB_UserTables(perm text DEFAULT 'all') -RETURNS SETOF information_schema.sql_identifier +RETURNS SETOF name AS $$ - WITH usertables AS ( - -- TODO: query CDB_TableMetadata for this ? - -- See http://github.com/CartoDB/cartodb/issues/254#issuecomment-26044777 - SELECT table_name as t - FROM information_schema.tables - WHERE - table_type='BASE TABLE' - AND table_schema='public' - AND table_name NOT IN ( - 'cdb_tablemetadata', - 'spatial_ref_sys' - ) - ), perms AS ( - SELECT t, has_table_privilege('public', 'public'||'.'||t, 'SELECT') as p - FROM usertables - ) - SELECT t FROM perms - WHERE ( - p = CASE WHEN $1 = 'private' THEN false - WHEN $1 = 'public' THEN true - ELSE not p -- none - END - OR $1 = 'all' - ) - AND has_table_privilege('public'||'.'||t, 'SELECT') - ; + +SELECT c.relname +FROM pg_class c +JOIN pg_roles r ON r.oid = c.relowner +JOIN pg_namespace n ON n.oid = c.relnamespace +WHERE r.rolname = current_user +AND c.relkind = 'r' +AND c.relname NOT IN ('cdb_tablemetadata', 'spatial_ref_sys') +AND n.nspname NOT IN ('pg_catalog', 'information_schema') +AND CASE WHEN perm = 'public' THEN has_table_privilege('public', c.oid, 'SELECT') + WHEN perm = 'private' THEN has_table_privilege(c.oid, 'SELECT') AND NOT + has_table_privilege('public', c.oid, 'SELECT') + ELSE has_table_privilege(c.oid, 'SELECT') END; + $$ LANGUAGE 'sql'; -- This is to migrate from pre-0.2.0 version From 54973142f69e28686953df71ba9cf87fe7506e55 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Mon, 1 Jun 2015 10:37:46 -0700 Subject: [PATCH 14/62] Handle "unsupported argument" case for CDB_UserTables in support of #64 --- README.md | 2 +- scripts-available/CDB_UserTables.sql | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ed38bff..5ed529d 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ See https://github.com/CartoDB/cartodb/wiki/CartoDB-PostgreSQL-extension Dependencies ------------ - * PostgreSQL 9.3+ (with plpythonu extension) + * PostgreSQL 9.3+ (with plpythonu extension and xml support) * [PostGIS extension](http://postgis.net) * [Schema triggers extension] (https://bitbucket.org/malloclabs/pg_schema_triggers) diff --git a/scripts-available/CDB_UserTables.sql b/scripts-available/CDB_UserTables.sql index 1b211b7..aa82f37 100644 --- a/scripts-available/CDB_UserTables.sql +++ b/scripts-available/CDB_UserTables.sql @@ -20,7 +20,8 @@ AND n.nspname NOT IN ('pg_catalog', 'information_schema') AND CASE WHEN perm = 'public' THEN has_table_privilege('public', c.oid, 'SELECT') WHEN perm = 'private' THEN has_table_privilege(c.oid, 'SELECT') AND NOT has_table_privilege('public', c.oid, 'SELECT') - ELSE has_table_privilege(c.oid, 'SELECT') END; + WHEN perm = 'all' THEN has_table_privilege(c.oid, 'SELECT') + ELSE false END; $$ LANGUAGE 'sql'; From e7ef5e7e8e12f977f759f8016752b6f34a006765 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Mon, 1 Jun 2015 10:57:30 -0700 Subject: [PATCH 15/62] Clean up after tests For #64 --- test/CDB_QueryTablesTest.sql | 2 ++ test/CDB_QueryTablesTest_expect | 2 ++ 2 files changed, 4 insertions(+) diff --git a/test/CDB_QueryTablesTest.sql b/test/CDB_QueryTablesTest.sql index b87315c..695bf70 100644 --- a/test/CDB_QueryTablesTest.sql +++ b/test/CDB_QueryTablesTest.sql @@ -31,6 +31,8 @@ create table sc.test (a int); insert into sc.test values (1); WITH inp AS ( select 'select * from sc.test'::text as q ) SELECT q, CDB_QueryTables(q) from inp; +DROP TABLE sc.test; +DROP SCHEMA sc; WITH inp AS ( select 'SELECT * FROM geometry_columns'::text as q ) diff --git a/test/CDB_QueryTablesTest_expect b/test/CDB_QueryTablesTest_expect index bfe59df..47899ed 100644 --- a/test/CDB_QueryTablesTest_expect +++ b/test/CDB_QueryTablesTest_expect @@ -13,5 +13,7 @@ CREATE SCHEMA CREATE TABLE INSERT 0 1 select * from sc.test|{sc.test} +DROP TABLE sc.test; +DROP SCHEMA sc; SELECT * FROM geometry_columns|{pg_catalog.pg_attribute,pg_catalog.pg_class,pg_catalog.pg_namespace,pg_catalog.pg_type} From dd3f125339c89c57c07e98cb60772bf6f228cf10 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Mon, 1 Jun 2015 11:10:06 -0700 Subject: [PATCH 16/62] Fix test expectation --- test/CDB_QueryTablesTest_expect | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/CDB_QueryTablesTest_expect b/test/CDB_QueryTablesTest_expect index 47899ed..205ba18 100644 --- a/test/CDB_QueryTablesTest_expect +++ b/test/CDB_QueryTablesTest_expect @@ -13,7 +13,7 @@ CREATE SCHEMA CREATE TABLE INSERT 0 1 select * from sc.test|{sc.test} -DROP TABLE sc.test; -DROP SCHEMA sc; +DROP TABLE +DROP SCHEMA SELECT * FROM geometry_columns|{pg_catalog.pg_attribute,pg_catalog.pg_class,pg_catalog.pg_namespace,pg_catalog.pg_type} From 424564e3246e09c786d5135e44bee4e1b500e76f Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Mon, 8 Jun 2015 13:37:27 -0400 Subject: [PATCH 17/62] initial commit --- scripts-available/CDB_DistType.sql | 142 +++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 scripts-available/CDB_DistType.sql diff --git a/scripts-available/CDB_DistType.sql b/scripts-available/CDB_DistType.sql new file mode 100644 index 0000000..07f685a --- /dev/null +++ b/scripts-available/CDB_DistType.sql @@ -0,0 +1,142 @@ +-- +-- CDB_DistType classifies the histograms of a column into +-- one of the basic types listed by Galtong: http://druedin.com/2012/12/08/galtungs-ajus-system/ +-- +-- Future improvements: +-- variable number of bins (7 is baked in right now) +-- catch the number of items to ensure that the sample is large enough +-- +-- Refs: +-- 1. width_bucket/histograms: http://tapoueh.org/blog/2014/02/21-PostgreSQL-histogram +-- 2. R implementation: https://github.com/cran/agrmt + + +CREATE OR REPLACE FUNCTION CDB_ClassTest ( in_array NUMERIC[] ) RETURNS text as $$ +DECLARE + element_count INT4; + minv numeric; + maxv numeric; + bins numeric[]; + freqs numeric[]; + diff numeric[]; + diffdiff numeric[]; + ajus INT[]; + freq INT4; + signature text; + i INT := 1; +BEGIN + SELECT min(e), max(e), count(e) INTO minv, maxv, element_count FROM ( SELECT unnest(in_array) e ) x; + + IF abs(maxv - minv) < 1e-7 THEN -- if max and min are nearly equal, call if 'F' + signature = 'F'; + ELSE + -- Calculate bins and count in bins + EXECUTE 'WITH stats as ( + SELECT min(e) as minv, + max(e) as maxv, + count(e) as total + FROM (SELECT unnest($1) e) x + WHERE e is not null + ), + hist as ( + SELECT width_bucket(e, s.minv, s.maxv, 6) bucket, + count(*) freq + FROM (SELECT unnest($1) e) x, stats s + WHERE e is not null + GROUP BY 1 + ORDER BY 1 + ) + SELECT array_agg(round(100.0 * hist.freq::numeric / stats.total::numeric,1)) freqs, + array_agg(hist.bucket) buckets + FROM hist, stats' + INTO freqs, bins + USING in_array; + -- don't need bins variable (freq are already sorted), discard it later + + LOOP + IF i < 7 THEN + ajus[i] = CDB_CompareValues(freqs[i],freqs[i+1],5.0); -- 5% tolerance + ELSE + EXIT; + END IF; + i := i + 1; + END LOOP; + + signature = CDB_DistributionType(ajus); + END IF; + + RETURN signature; +END; +$$ language plpgsql IMMUTABLE; + +-- Classify data into AJUSFL + +CREATE OR REPLACE FUNCTION CDB_DistributionType ( in_array INT[] ) RETURNS text as $$ +DECLARE + element_count INT4; + maxv numeric; + minv numeric; + uniques INT[]; + type text; +BEGIN + SELECT max(e), min(e) INTO maxv, minv FROM ( SELECT unnest(in_array) e ) x; + + IF (maxv = 0 AND minv = 0) THEN + type = 'F'; + ELSIF maxv < 1 THEN + type = 'L'; + ELSIF minv > -1 THEN + type = 'J'; + ELSE + -- Get distinct elements ordered by original position + EXECUTE 'WITH b AS ( + SELECT a + FROM (SELECT unnest($1) a) x + ), + c AS ( + SELECT a, row_number() OVER () r + FROM b + ), + d AS ( + SELECT DISTINCT a + FROM c + ), + e AS ( + SELECT a FROM d ORDER BY ( + SELECT r FROM c WHERE d.a = c.a ORDER BY r ASC LIMIT 1 + ) ASC) + SELECT array_agg(a) FROM e' + INTO uniques + USING in_array; + + -- Decide if it's an A, U, or other + IF (uniques = ARRAY[1,-1] OR uniques = ARRAY[1,0,-1] OR uniques = ARRAY[1,-1,0] OR uniques = ARRAY[0,1,-1]) THEN + type = 'A'; + ELSIF (uniques = ARRAY[-1,1] OR uniques = ARRAY[-1,0,1] OR uniques = ARRAY[-1,1,0] OR uniques = ARRAY[0,-1,1]) THEN + type = 'U'; + ELSE + type = 'S'; + END IF; + END IF; + + RETURN type; +END; +$$ language plpgsql IMMUTABLE; + +CREATE OR REPLACE FUNCTION CDB_CompareValues ( a numeric, b numeric, tolerance numeric ) RETURNS INT as $$ +DECLARE + d INT4; +BEGIN + IF a > b THEN + SELECT -1 INTO d; + ELSE + SELECT 1 INTO d; + END IF; + + IF abs(a-b) <= tolerance THEN + SELECT 0 INTO d; + END IF; + + RETURN d; +END; +$$ language plpgsql IMMUTABLE; \ No newline at end of file From 9cb1fe30d8f9cf750d0d369fbb57b320ff0440ee Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Mon, 8 Jun 2015 15:01:50 -0400 Subject: [PATCH 18/62] adding tests --- test/CDB_DistTypeTest.sql | 4 ++++ test/CDB_DistTypeTest_expect | 1 + 2 files changed, 5 insertions(+) create mode 100644 test/CDB_DistTypeTest.sql create mode 100644 test/CDB_DistTypeTest_expect diff --git a/test/CDB_DistTypeTest.sql b/test/CDB_DistTypeTest.sql new file mode 100644 index 0000000..efe1804 --- /dev/null +++ b/test/CDB_DistTypeTest.sql @@ -0,0 +1,4 @@ +WITH data AS ( + SELECT pow(x,3)::numeric x FROM generate_series(-100,100) x + ) +SELECT CDB_DistType(array_agg(x)) FROM data diff --git a/test/CDB_DistTypeTest_expect b/test/CDB_DistTypeTest_expect new file mode 100644 index 0000000..f70f10e --- /dev/null +++ b/test/CDB_DistTypeTest_expect @@ -0,0 +1 @@ +A From 8e2d86414f99bdc869e34a4267b7271a945d6e8e Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Mon, 8 Jun 2015 15:02:39 -0400 Subject: [PATCH 19/62] updating function --- scripts-available/CDB_DistType.sql | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_DistType.sql b/scripts-available/CDB_DistType.sql index 07f685a..b60eb78 100644 --- a/scripts-available/CDB_DistType.sql +++ b/scripts-available/CDB_DistType.sql @@ -10,9 +10,8 @@ -- 1. width_bucket/histograms: http://tapoueh.org/blog/2014/02/21-PostgreSQL-histogram -- 2. R implementation: https://github.com/cran/agrmt - -CREATE OR REPLACE FUNCTION CDB_ClassTest ( in_array NUMERIC[] ) RETURNS text as $$ -DECLARE +CREATE OR REPLACE FUNCTION CDB_DistType ( in_array NUMERIC[] ) RETURNS text as $$ +DECLARE element_count INT4; minv numeric; maxv numeric; @@ -27,7 +26,7 @@ DECLARE BEGIN SELECT min(e), max(e), count(e) INTO minv, maxv, element_count FROM ( SELECT unnest(in_array) e ) x; - IF abs(maxv - minv) < 1e-7 THEN -- if max and min are nearly equal, call if 'F' + IF abs(maxv - minv) < 1e-7 THEN -- if max and min are nearly equal, call if 'F' (make relative to maxv?) signature = 'F'; ELSE -- Calculate bins and count in bins @@ -39,7 +38,7 @@ BEGIN WHERE e is not null ), hist as ( - SELECT width_bucket(e, s.minv, s.maxv, 6) bucket, + SELECT width_bucket(e, s.minv, s.maxv, 7) bucket, count(*) freq FROM (SELECT unnest($1) e) x, stats s WHERE e is not null From c7f4209270c80244acc9729e7bde0ade3f48100d Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Mon, 8 Jun 2015 15:11:58 -0400 Subject: [PATCH 20/62] added alias and line --- scripts-available/CDB_DistType.sql | 2 +- scripts-enabled/CDB_DistType.sql | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 120000 scripts-enabled/CDB_DistType.sql diff --git a/scripts-available/CDB_DistType.sql b/scripts-available/CDB_DistType.sql index b60eb78..f74278d 100644 --- a/scripts-available/CDB_DistType.sql +++ b/scripts-available/CDB_DistType.sql @@ -138,4 +138,4 @@ BEGIN RETURN d; END; -$$ language plpgsql IMMUTABLE; \ No newline at end of file +$$ language plpgsql IMMUTABLE; diff --git a/scripts-enabled/CDB_DistType.sql b/scripts-enabled/CDB_DistType.sql new file mode 120000 index 0000000..64ff8c2 --- /dev/null +++ b/scripts-enabled/CDB_DistType.sql @@ -0,0 +1 @@ +../scripts-available/CDB_DistType.sql \ No newline at end of file From 6ab1b1d3d03075eb7c563e6a22690aa3e8e28c91 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Mon, 8 Jun 2015 18:36:07 -0400 Subject: [PATCH 21/62] removed unneeded variables --- scripts-available/CDB_DistType.sql | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts-available/CDB_DistType.sql b/scripts-available/CDB_DistType.sql index f74278d..15dba49 100644 --- a/scripts-available/CDB_DistType.sql +++ b/scripts-available/CDB_DistType.sql @@ -17,8 +17,6 @@ DECLARE maxv numeric; bins numeric[]; freqs numeric[]; - diff numeric[]; - diffdiff numeric[]; ajus INT[]; freq INT4; signature text; @@ -50,7 +48,6 @@ BEGIN FROM hist, stats' INTO freqs, bins USING in_array; - -- don't need bins variable (freq are already sorted), discard it later LOOP IF i < 7 THEN From 1d223b77cc40118d0fcdbba3762718f3e71a883e Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Wed, 10 Jun 2015 10:50:01 -0400 Subject: [PATCH 22/62] changed subfunction name, replaced function with case statement --- scripts-available/CDB_DistType.sql | 28 ++++++---------------------- 1 file changed, 6 insertions(+), 22 deletions(-) diff --git a/scripts-available/CDB_DistType.sql b/scripts-available/CDB_DistType.sql index 15dba49..7d2242f 100644 --- a/scripts-available/CDB_DistType.sql +++ b/scripts-available/CDB_DistType.sql @@ -1,6 +1,6 @@ -- -- CDB_DistType classifies the histograms of a column into --- one of the basic types listed by Galtong: http://druedin.com/2012/12/08/galtungs-ajus-system/ +-- one of the basic types listed by Galtung: http://druedin.com/2012/12/08/galtungs-ajus-system/ -- -- Future improvements: -- variable number of bins (7 is baked in right now) @@ -51,14 +51,16 @@ BEGIN LOOP IF i < 7 THEN - ajus[i] = CDB_CompareValues(freqs[i],freqs[i+1],5.0); -- 5% tolerance + ajus[i] = CASE WHEN freqs[i] > freqs[i+1] THEN -1 + WHEN abs(freqs[i] - freqs[i+1]) <= 0.05 THEN 0 + ELSE 1 END; ELSE EXIT; END IF; i := i + 1; END LOOP; - signature = CDB_DistributionType(ajus); + signature = _CDB_DistTypeClassify(ajus); END IF; RETURN signature; @@ -67,7 +69,7 @@ $$ language plpgsql IMMUTABLE; -- Classify data into AJUSFL -CREATE OR REPLACE FUNCTION CDB_DistributionType ( in_array INT[] ) RETURNS text as $$ +CREATE OR REPLACE FUNCTION _CDB_DistTypeClassify ( in_array INT[] ) RETURNS text as $$ DECLARE element_count INT4; maxv numeric; @@ -118,21 +120,3 @@ BEGIN RETURN type; END; $$ language plpgsql IMMUTABLE; - -CREATE OR REPLACE FUNCTION CDB_CompareValues ( a numeric, b numeric, tolerance numeric ) RETURNS INT as $$ -DECLARE - d INT4; -BEGIN - IF a > b THEN - SELECT -1 INTO d; - ELSE - SELECT 1 INTO d; - END IF; - - IF abs(a-b) <= tolerance THEN - SELECT 0 INTO d; - END IF; - - RETURN d; -END; -$$ language plpgsql IMMUTABLE; From cda6953ea669397a4c6f8bb3de347976c8a4a4c8 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 23 Jun 2015 16:27:27 -0400 Subject: [PATCH 23/62] initial commit --- scripts-available/CDB_DistinctMeasure.sql | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 scripts-available/CDB_DistinctMeasure.sql diff --git a/scripts-available/CDB_DistinctMeasure.sql b/scripts-available/CDB_DistinctMeasure.sql new file mode 100644 index 0000000..803ab69 --- /dev/null +++ b/scripts-available/CDB_DistinctMeasure.sql @@ -0,0 +1,46 @@ +-- +-- CDB_DistinctMeasure +-- calculates the fraction of rows in the 10 most common distinct categories +-- returns true if the number of rows in these 10 categories is >= 0.9 * total number of rows +-- +-- + +CREATE OR REPLACE FUNCTION CDB_DistinctMeasure ( in_array text[], threshold numeric DEFAULT 0.90 ) RETURNS boolean as $$ +DECLARE + element_count INT4; + maxval numeric; + passes boolean; +BEGIN + SELECT count(e) INTO element_count FROM ( SELECT unnest(in_array) e ) x; + + -- count number of occurrences per bin + -- calculate the normalized cumulative sum + -- return the max value: which corresponds nth entry + -- for n <= 10 depending on # of distinct values + EXECUTE 'WITH a As ( + SELECT + count(*) cnt, + e + FROM + (SELECT * FROM unnest($2) e ) x + GROUP BY e + ORDER BY cnt DESC + ), + b As ( + SELECT + sum(cnt) OVER (ORDER BY cnt DESC) / $1 As cumsum + FROM a + LIMIT 10 + ) + SELECT max(cumsum) maxval FROM b' + INTO maxval + USING element_count, in_array; + + passes = CASE WHEN maxval >= threshold + THEN TRUE + ELSE FALSE + END; + RAISE NOTICE 'maxval: %', maxval; + RETURN passes; +END; +$$ language plpgsql IMMUTABLE; From f8542af57af675c98368b7158071deaea49ce3a3 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 23 Jun 2015 18:07:48 -0400 Subject: [PATCH 24/62] add tests --- test/CDB_DistinctMeasureTest.sql | 20 ++++++++++++++++++++ test/CDB_DistinctMeasureTest_expect | 1 + 2 files changed, 21 insertions(+) create mode 100644 test/CDB_DistinctMeasureTest.sql create mode 100644 test/CDB_DistinctMeasureTest_expect diff --git a/test/CDB_DistinctMeasureTest.sql b/test/CDB_DistinctMeasureTest.sql new file mode 100644 index 0000000..ac0f7a7 --- /dev/null +++ b/test/CDB_DistinctMeasureTest.sql @@ -0,0 +1,20 @@ +-- a - j add up to 89%, k-m add up to 11% +WITH a As ( + SELECT ( + repeat('a',12) || + repeat('b',11) || + repeat('c',11) || + repeat('d',10) || + repeat('e',10) || + repeat('f',9) || + repeat('g',8) || + repeat('h',7) || + repeat('i',6) || + repeat('j',5) || + repeat('k',4) || + repeat('l',4) || + repeat('m',3) + )::text AS x + ) + +SELECT CDB_DistinctMeasure(string_to_array(x,null),0.90) from a ; \ No newline at end of file diff --git a/test/CDB_DistinctMeasureTest_expect b/test/CDB_DistinctMeasureTest_expect new file mode 100644 index 0000000..4d1ae35 --- /dev/null +++ b/test/CDB_DistinctMeasureTest_expect @@ -0,0 +1 @@ +f \ No newline at end of file From c7bb57b405f84e5cf41393ccfffbce30593ff2ce Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 23 Jun 2015 18:08:32 -0400 Subject: [PATCH 25/62] add symlink --- scripts-enabled/CDB_DistinctMeasure.sql | 1 + 1 file changed, 1 insertion(+) create mode 120000 scripts-enabled/CDB_DistinctMeasure.sql diff --git a/scripts-enabled/CDB_DistinctMeasure.sql b/scripts-enabled/CDB_DistinctMeasure.sql new file mode 120000 index 0000000..711434b --- /dev/null +++ b/scripts-enabled/CDB_DistinctMeasure.sql @@ -0,0 +1 @@ +scripts-available/CDB_DistinctMeasure.sql \ No newline at end of file From 3ad3038c5e5edd796623e3b826e45c74d1125b45 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 23 Jun 2015 18:32:16 -0400 Subject: [PATCH 26/62] fixed symlink path, other minor items --- scripts-enabled/CDB_DistinctMeasure.sql | 2 +- test/CDB_DistinctMeasureTest.sql | 2 +- test/CDB_DistinctMeasureTest_expect | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts-enabled/CDB_DistinctMeasure.sql b/scripts-enabled/CDB_DistinctMeasure.sql index 711434b..e78b435 120000 --- a/scripts-enabled/CDB_DistinctMeasure.sql +++ b/scripts-enabled/CDB_DistinctMeasure.sql @@ -1 +1 @@ -scripts-available/CDB_DistinctMeasure.sql \ No newline at end of file +../scripts-available/CDB_DistinctMeasure.sql \ No newline at end of file diff --git a/test/CDB_DistinctMeasureTest.sql b/test/CDB_DistinctMeasureTest.sql index ac0f7a7..1eb3a74 100644 --- a/test/CDB_DistinctMeasureTest.sql +++ b/test/CDB_DistinctMeasureTest.sql @@ -17,4 +17,4 @@ WITH a As ( )::text AS x ) -SELECT CDB_DistinctMeasure(string_to_array(x,null),0.90) from a ; \ No newline at end of file +SELECT CDB_DistinctMeasure(string_to_array(x,null),0.90) from a diff --git a/test/CDB_DistinctMeasureTest_expect b/test/CDB_DistinctMeasureTest_expect index 4d1ae35..6a69f92 100644 --- a/test/CDB_DistinctMeasureTest_expect +++ b/test/CDB_DistinctMeasureTest_expect @@ -1 +1 @@ -f \ No newline at end of file +f From 5941b473cad69f18f3bfad55b062c80d3ea960e3 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 23 Jun 2015 18:39:15 -0400 Subject: [PATCH 27/62] removed notice --- scripts-available/CDB_DistinctMeasure.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts-available/CDB_DistinctMeasure.sql b/scripts-available/CDB_DistinctMeasure.sql index 803ab69..b5f2cce 100644 --- a/scripts-available/CDB_DistinctMeasure.sql +++ b/scripts-available/CDB_DistinctMeasure.sql @@ -40,7 +40,6 @@ BEGIN THEN TRUE ELSE FALSE END; - RAISE NOTICE 'maxval: %', maxval; RETURN passes; END; $$ language plpgsql IMMUTABLE; From d124776c4e8dc9d07d93a56d7b8834edaabef03d Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 23 Jun 2015 18:49:59 -0400 Subject: [PATCH 28/62] simplified assignment --- scripts-available/CDB_DistinctMeasure.sql | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/scripts-available/CDB_DistinctMeasure.sql b/scripts-available/CDB_DistinctMeasure.sql index b5f2cce..228a0c7 100644 --- a/scripts-available/CDB_DistinctMeasure.sql +++ b/scripts-available/CDB_DistinctMeasure.sql @@ -36,10 +36,8 @@ BEGIN INTO maxval USING element_count, in_array; - passes = CASE WHEN maxval >= threshold - THEN TRUE - ELSE FALSE - END; + passes = (maxval >= threshold); + RETURN passes; END; $$ language plpgsql IMMUTABLE; From 9a94b3879a8c6ca34026ab09bd91c9a0095ab8b2 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 24 Jun 2015 11:53:07 +0200 Subject: [PATCH 29/62] Add a new function CDB_QueryTablesRegclass #86 The return values of it can be safely used when len(schema.table_name) exceeds the 63 char limit of the postgres type `name`. --- scripts-available/CDB_QueryTables.sql | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_QueryTables.sql b/scripts-available/CDB_QueryTables.sql index cd8b51b..bd4d47f 100644 --- a/scripts-available/CDB_QueryTables.sql +++ b/scripts-available/CDB_QueryTables.sql @@ -2,12 +2,12 @@ -- -- Requires PostgreSQL 9.x+ -- -CREATE OR REPLACE FUNCTION CDB_QueryTables(query text) -RETURNS name[] +CREATE OR REPLACE FUNCTION CDB_QueryTablesRegclass(query text) +RETURNS regclass[] AS $$ DECLARE exp XML; - tables NAME[]; + tables regclass[]; rec RECORD; rec2 RECORD; BEGIN @@ -41,11 +41,11 @@ BEGIN xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x, xpath('//x:Relation-Name/../x:Schema/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as s ) - SELECT unnest(x)::name as p, unnest(s)::name as sc from inp + SELECT unnest(x) as p, unnest(s) as sc from inp LOOP -- RAISE DEBUG 'tab: %', rec2.p; -- RAISE DEBUG 'sc: %', rec2.sc; - tables := array_append(tables, (rec2.sc || '.' || rec2.p)::name); + tables := array_append(tables, (rec2.sc || '.' || rec2.p)::regclass); END LOOP; -- RAISE DEBUG 'Tables: %', tables; @@ -65,3 +65,14 @@ BEGIN return tables; END $$ LANGUAGE 'plpgsql' VOLATILE STRICT; + + +-- Keep CDB_QueryTables with same signature for backwards compatibility. +-- It should probably be removed in the future. +CREATE OR REPLACE FUNCTION CDB_QueryTables(query text) +RETURNS name[] +AS $$ +BEGIN + RETURN CDB_QueryTablesRegclass(query)::name[]; +END +$$ LANGUAGE 'plpgsql' VOLATILE STRICT; From ddb6b2c5b585df791a5d276b19ba5421077179b2 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 24 Jun 2015 14:13:58 +0200 Subject: [PATCH 30/62] Return text instead of regclass #86 This way the schema is always returned and backwards compatibility is kept, should it be needed. --- scripts-available/CDB_QueryTables.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_QueryTables.sql b/scripts-available/CDB_QueryTables.sql index bd4d47f..ac61281 100644 --- a/scripts-available/CDB_QueryTables.sql +++ b/scripts-available/CDB_QueryTables.sql @@ -2,12 +2,12 @@ -- -- Requires PostgreSQL 9.x+ -- -CREATE OR REPLACE FUNCTION CDB_QueryTablesRegclass(query text) -RETURNS regclass[] +CREATE OR REPLACE FUNCTION CDB_QueryTablesText(query text) +RETURNS text[] AS $$ DECLARE exp XML; - tables regclass[]; + tables text[]; rec RECORD; rec2 RECORD; BEGIN @@ -45,7 +45,7 @@ BEGIN LOOP -- RAISE DEBUG 'tab: %', rec2.p; -- RAISE DEBUG 'sc: %', rec2.sc; - tables := array_append(tables, (rec2.sc || '.' || rec2.p)::regclass); + tables := array_append(tables, (rec2.sc || '.' || rec2.p)); END LOOP; -- RAISE DEBUG 'Tables: %', tables; @@ -73,6 +73,6 @@ CREATE OR REPLACE FUNCTION CDB_QueryTables(query text) RETURNS name[] AS $$ BEGIN - RETURN CDB_QueryTablesRegclass(query)::name[]; + RETURN CDB_QueryTablesText(query)::name[]; END $$ LANGUAGE 'plpgsql' VOLATILE STRICT; From 22fc962d0983c97aa572f8d8bce5234e5ad9b5c9 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 24 Jun 2015 16:01:43 +0200 Subject: [PATCH 31/62] Change expectation #86 Just add CONTEXT lines since they are now added in case of WARNING/ERROR as a result of having CDB_QueryTables calling CDB_QueryTablesText. --- test/CDB_QueryTablesTest_expect | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test/CDB_QueryTablesTest_expect b/test/CDB_QueryTablesTest_expect index bfe59df..d3a64d1 100644 --- a/test/CDB_QueryTablesTest_expect +++ b/test/CDB_QueryTablesTest_expect @@ -1,13 +1,18 @@ +\set ECHO none SELECT * FROM geometry_columns|{pg_catalog.pg_attribute,pg_catalog.pg_class,pg_catalog.pg_namespace,pg_catalog.pg_type} SELECT a.attname FROM pg_class c JOIN pg_attribute a on (a.attrelid = c.oid)|{pg_catalog.pg_attribute,pg_catalog.pg_class} CREATE table "my'tab;le" as select 1|{} SELECT a.oid, b.oid FROM pg_class a, pg_class b|{pg_catalog.pg_class} SELECT 1 as col1; select 2 as col2|{} WARNING: CDB_QueryTables cannot explain query: select 1 from nonexistant (42P01: relation "nonexistant" does not exist) +CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN ERROR: relation "nonexistant" does not exist +CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN begin; select * from pg_class; commit;|{pg_catalog.pg_class} WARNING: CDB_QueryTables cannot explain query: select * from test (42P01: relation "test" does not exist) +CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN ERROR: relation "test" does not exist +CONTEXT: PL/pgSQL function cdb_querytables(text) line 3 at RETURN WITH a AS (select * from pg_class) select * from a|{pg_catalog.pg_class} CREATE SCHEMA CREATE TABLE From c3eea08f66090f683ed96d187d8f40423734ab2c Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Wed, 24 Jun 2015 16:29:39 +0200 Subject: [PATCH 32/62] Remove ECHO from expectation #86 Remove the `\set ECHO none` from expectation that is automatically removed by the test harness but still appears in the output when a test fails. --- test/CDB_QueryTablesTest_expect | 1 - 1 file changed, 1 deletion(-) diff --git a/test/CDB_QueryTablesTest_expect b/test/CDB_QueryTablesTest_expect index d3a64d1..9959982 100644 --- a/test/CDB_QueryTablesTest_expect +++ b/test/CDB_QueryTablesTest_expect @@ -1,4 +1,3 @@ -\set ECHO none SELECT * FROM geometry_columns|{pg_catalog.pg_attribute,pg_catalog.pg_class,pg_catalog.pg_namespace,pg_catalog.pg_type} SELECT a.attname FROM pg_class c JOIN pg_attribute a on (a.attrelid = c.oid)|{pg_catalog.pg_attribute,pg_catalog.pg_class} CREATE table "my'tab;le" as select 1|{} From 97140b17c978f8a19c937b0aba5c91f7c45e1145 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Wed, 24 Jun 2015 11:03:16 -0400 Subject: [PATCH 33/62] added more flexible output values --- scripts-available/CDB_DistinctMeasure.sql | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts-available/CDB_DistinctMeasure.sql b/scripts-available/CDB_DistinctMeasure.sql index 228a0c7..a5bf1d7 100644 --- a/scripts-available/CDB_DistinctMeasure.sql +++ b/scripts-available/CDB_DistinctMeasure.sql @@ -5,11 +5,11 @@ -- -- -CREATE OR REPLACE FUNCTION CDB_DistinctMeasure ( in_array text[], threshold numeric DEFAULT 0.90 ) RETURNS boolean as $$ +CREATE OR REPLACE FUNCTION CDB_DistinctMeasure ( in_array text[], threshold numeric DEFAULT null ) RETURNS numeric as $$ DECLARE element_count INT4; maxval numeric; - passes boolean; + passes numeric; BEGIN SELECT count(e) INTO element_count FROM ( SELECT unnest(in_array) e ) x; @@ -35,8 +35,11 @@ BEGIN SELECT max(cumsum) maxval FROM b' INTO maxval USING element_count, in_array; - - passes = (maxval >= threshold); + IF threshold is null THEN + passes = maxval; + ELSE + passes = CASE WHEN (maxval >= threshold) THEN 1 ELSE 0 END; + END IF; RETURN passes; END; From 13946b4d475aaa5792ea7526e997f6730321930e Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Thu, 25 Jun 2015 08:17:41 -0400 Subject: [PATCH 34/62] update test output --- scripts-available/CDB_DistinctMeasure.sql | 6 +++--- test/CDB_DistinctMeasureTest_expect | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts-available/CDB_DistinctMeasure.sql b/scripts-available/CDB_DistinctMeasure.sql index a5bf1d7..63ec647 100644 --- a/scripts-available/CDB_DistinctMeasure.sql +++ b/scripts-available/CDB_DistinctMeasure.sql @@ -19,10 +19,10 @@ BEGIN -- for n <= 10 depending on # of distinct values EXECUTE 'WITH a As ( SELECT - count(*) cnt, - e + count(*) cnt FROM (SELECT * FROM unnest($2) e ) x + WHERE e is not null GROUP BY e ORDER BY cnt DESC ), @@ -31,7 +31,7 @@ BEGIN sum(cnt) OVER (ORDER BY cnt DESC) / $1 As cumsum FROM a LIMIT 10 - ) + ) SELECT max(cumsum) maxval FROM b' INTO maxval USING element_count, in_array; diff --git a/test/CDB_DistinctMeasureTest_expect b/test/CDB_DistinctMeasureTest_expect index 6a69f92..573541a 100644 --- a/test/CDB_DistinctMeasureTest_expect +++ b/test/CDB_DistinctMeasureTest_expect @@ -1 +1 @@ -f +0 From 371d84ea0cc69ef194f209844a2109a8090c7b03 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Mon, 29 Jun 2015 12:09:35 +0200 Subject: [PATCH 35/62] Update Makefile and NEWS.md for new version --- Makefile | 3 ++- NEWS.md | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d74f9b1..6e106a9 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.7.3 +EXTVERSION = 0.7.4 SED = sed @@ -37,6 +37,7 @@ UPGRADABLE = \ 0.7.0 \ 0.7.1 \ 0.7.2 \ + 0.7.3 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) diff --git a/NEWS.md b/NEWS.md index 4b84730..e96537b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,12 @@ +0.7.4 (2015-06-29) +------------------ +* Adds new function CDB_QueryTablesText that can deal with "schema.table_name" + longer than 63 chars. +* Adds a set of statistical functions: + - CDB_DistType + - CDB_DistinctMeasure + - CDB_EqualIntervalBins + 0.7.3 (2015-03-03) ------------------ * Fix upgrade of CDB_StringToDate function From 6b29c9e67d176c456a0b54b1428d8521bce3f89e Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 30 Jun 2015 11:11:21 +0200 Subject: [PATCH 36/62] Fix versioning by creating a 0.8.0 Fix versioning by creating a new major version since it contains new features. Keep version 0.7.4 that should've never existed and provide an upgrade path for the new version. --- Makefile | 3 ++- NEWS.md | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 6e106a9..f4950d2 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.7.4 +EXTVERSION = 0.8.0 SED = sed @@ -38,6 +38,7 @@ UPGRADABLE = \ 0.7.1 \ 0.7.2 \ 0.7.3 \ + 0.7.4 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) diff --git a/NEWS.md b/NEWS.md index e96537b..56365fb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -0.7.4 (2015-06-29) +0.8.0 (2015-06-30) ------------------ * Adds new function CDB_QueryTablesText that can deal with "schema.table_name" longer than 63 chars. @@ -7,6 +7,10 @@ - CDB_DistinctMeasure - CDB_EqualIntervalBins +0.7.4 (2015-06-29) +------------------ +Dummy transitional version. + 0.7.3 (2015-03-03) ------------------ * Fix upgrade of CDB_StringToDate function From 0045fb20e86563926425c5e78c29ac96cdea28ca Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 30 Jun 2015 11:34:59 +0200 Subject: [PATCH 37/62] Update NEWS.md --- NEWS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS.md b/NEWS.md index 56365fb..0c02c7d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -6,6 +6,7 @@ - CDB_DistType - CDB_DistinctMeasure - CDB_EqualIntervalBins +* Fix for CDB_UserTables returns 0 entries for multiuser accounts [#64](https://github.com/CartoDB/cartodb-postgresql/issues/64) 0.7.4 (2015-06-29) ------------------ From a6fd829669aa8c3c42afd0750e8b52b95d716dc8 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Tue, 30 Jun 2015 12:17:46 +0200 Subject: [PATCH 38/62] Fix for upgrade issue from 0.7.4 to 0.8.0 This fixes the following problem found during testing: ``` ALTER EXTENSION cartodb UPDATE TO '0.8.0'; ERROR: cannot change return type of existing function HINT: Use DROP FUNCTION cdb_usertables(text) first. ``` --- scripts-available/CDB_UserTables.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts-available/CDB_UserTables.sql b/scripts-available/CDB_UserTables.sql index aa82f37..38b6414 100644 --- a/scripts-available/CDB_UserTables.sql +++ b/scripts-available/CDB_UserTables.sql @@ -5,6 +5,7 @@ -- -- Currently accepted permissions are: 'public', 'private' or 'all' -- +DROP FUNCTION IF EXISTS cdb_usertables(text); CREATE OR REPLACE FUNCTION CDB_UserTables(perm text DEFAULT 'all') RETURNS SETOF name AS $$ From 509944ea6d12863b881a5855a4b37977e265af1f Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Wed, 1 Jul 2015 07:01:50 -0700 Subject: [PATCH 39/62] Move doc from wiki to repo, closes #85 --- doc/CDB_ColumnNames.md | 14 +++++++++ doc/CDB_ColumnType.md | 15 ++++++++++ doc/CDB_HeadsTailsBins.md | 21 ++++++++++++++ doc/CDB_HexagonGrid.md | 43 +++++++++++++++++++++++++++ doc/CDB_JenksBins.md | 23 +++++++++++++++ doc/CDB_MakeHexagon.md | 21 ++++++++++++++ doc/CDB_QuantileBins.md | 21 ++++++++++++++ doc/CDB_RectangleGrid.md | 46 +++++++++++++++++++++++++++++ doc/CDB_SetUserQuotaInBytes.md | 11 +++++++ doc/CDB_TransformToWebmercator.md | 44 ++++++++++++++++++++++++++++ doc/CDB_UserTables.md | 11 +++++++ doc/CDB_XYZ_Extent.md | 22 ++++++++++++++ doc/CDB_XYZ_Resolution.md | 20 +++++++++++++ doc/CartoDB-PLpgSQL.md | 38 ++++++++++++++++++++++++ doc/CartoDB-user-table.md | 29 +++++++++++++++++++ doc/README.md | 48 +++++++++++++++++++++++++++++++ 16 files changed, 427 insertions(+) create mode 100644 doc/CDB_ColumnNames.md create mode 100644 doc/CDB_ColumnType.md create mode 100644 doc/CDB_HeadsTailsBins.md create mode 100644 doc/CDB_HexagonGrid.md create mode 100644 doc/CDB_JenksBins.md create mode 100644 doc/CDB_MakeHexagon.md create mode 100644 doc/CDB_QuantileBins.md create mode 100644 doc/CDB_RectangleGrid.md create mode 100644 doc/CDB_SetUserQuotaInBytes.md create mode 100644 doc/CDB_TransformToWebmercator.md create mode 100644 doc/CDB_UserTables.md create mode 100644 doc/CDB_XYZ_Extent.md create mode 100644 doc/CDB_XYZ_Resolution.md create mode 100644 doc/CartoDB-PLpgSQL.md create mode 100644 doc/CartoDB-user-table.md create mode 100644 doc/README.md diff --git a/doc/CDB_ColumnNames.md b/doc/CDB_ColumnNames.md new file mode 100644 index 0000000..ea6c25c --- /dev/null +++ b/doc/CDB_ColumnNames.md @@ -0,0 +1,14 @@ +Retrieve all column names in a particular table + +#### Using the function + +```sql +SELECT CDB_ColumnNames('table_name') +--- Returns a set of rows with column names +``` + +#### Arguements + +CDB_ColumnNames(table_name) + +* **table_name** text \ No newline at end of file diff --git a/doc/CDB_ColumnType.md b/doc/CDB_ColumnType.md new file mode 100644 index 0000000..823f23e --- /dev/null +++ b/doc/CDB_ColumnType.md @@ -0,0 +1,15 @@ +Returns a column type for any column in a table + +#### Using the function + +```sql +SELECT CDB_ColumnType('column_name','table_name') +--- Returns a set of rows with column types +``` + +#### Arguements + +CDB_ColumnType(column_name, table_name) + +* **column_name** text +* **table_name** text diff --git a/doc/CDB_HeadsTailsBins.md b/doc/CDB_HeadsTailsBins.md new file mode 100644 index 0000000..38d8b75 --- /dev/null +++ b/doc/CDB_HeadsTailsBins.md @@ -0,0 +1,21 @@ +Find the breaks for N categories in a numerical column based on the [Heads/Tails optimization](http://arxiv.org/pdf/1209.2801v1.pdf). Below, Heads/Tails used to color based on the area of the polygons. + +![headtails](https://f.cloud.github.com/assets/370259/140655/6eebb918-7228-11e2-89fa-149745f25d34.png) + +#### Using the function + +We can determine the 7 most optimal breaks in a column of numerical data as follows, + +```sql +SELECT CDB_HeadsTailsBins(array_agg(numeric_column), 7) FROM table_name +-- Results in an ordered array like, {7824,23492,52696,233857,666089,1001709,1638094} +-- Each break happens up to, and equal, to a number: +-- (bin1 is less than or equal to 7824, bin2 is less than or equal to 23492, etc.) +``` + +#### Arguements + +CDB_HeadsTailsBins(in_array, breaks) + +* **in_array** numeric[]. A NUMERIC array of values. +* **breaks** int. The number of categories you want to create \ No newline at end of file diff --git a/doc/CDB_HexagonGrid.md b/doc/CDB_HexagonGrid.md new file mode 100644 index 0000000..36b0170 --- /dev/null +++ b/doc/CDB_HexagonGrid.md @@ -0,0 +1,43 @@ +Fill given extent with an hexagonal coverage + +#### Using the function + +Create a hexagonal grid from a polygon geometry. For example, take the geometry + +```sql + ST_SetSRID( + ST_Envelope( + ST_Collect( + ST_MakePoint(10000000,-10000000), + ST_MakePoint(-10000000,10000000) + ) + ), + 3857) +``` + +We can create a grid as follows, + +```sql +SELECT CDB_HexagonGrid( + ST_SetSRID( + ST_Envelope( + ST_Collect( + ST_MakePoint(10000000,-10000000), + ST_MakePoint(-10000000,10000000) + ) + ), + 3857), + 1000000) the_geom_webmercator +``` + +Which will look something like this, + +![grid tile](http://i.imgur.com/4rZXGMb.png) + +#### Arguments + +CDB_HexagonGrid(ext, side, origin) + +* **ext** geometry. Extent to fill. Only hexagons with center point falling inside the extent (or at the lower or leftmost edge) will be emitted. The returned hexagons will have the same SRID as this extent. +* **side** float. Side measure for the hexagon. Maximum diameter will be 2 * side. Measure is in the same projection as **ext** +* **origin** OPTIONAL geometry. Optional origin to allow for exact tiling. If omitted the origin will be 0,0. The parameter is checked for having the same SRID as the extent. \ No newline at end of file diff --git a/doc/CDB_JenksBins.md b/doc/CDB_JenksBins.md new file mode 100644 index 0000000..6d8bd9a --- /dev/null +++ b/doc/CDB_JenksBins.md @@ -0,0 +1,23 @@ +Find the breaks for N categories in a numerical column based on the [Jenks optimization](http://en.wikipedia.org/wiki/Jenks_natural_breaks_optimization). Below, Jenks used to color based on the area of the polygons. + +![Jenks](https://f.cloud.github.com/assets/370259/140093/b64a9382-7210-11e2-81a4-c65cce3c885e.png) + +#### Using the function + +We can determine the 7 most optimal breaks in a column of numerical data as follows, + +```sql +SELECT CDB_JenksBins(array_agg(numeric_column), 7) FROM table_name +-- Results in an ordered array like, {0,73,2568,9408,29411,768230,1638094} +-- Each break happens up to, and equal, to a number: +-- (bin1 is less than or equal to 0, bin2 is less than or equal to 73, etc.) +``` + +#### Arguments + +CDB_JenksBins(in_array, breaks, invert) + +* **in_array** numeric[]. A NUMERIC array of values. +* **breaks** int. The number of categories you want to create +* **iterations** OPTIONAL int. The number of iterations used for calculating breaks. +* **invert** OPTIONAL boolean. Flips whether you receive top down breaks or bottom up breaks. Default is top down (so, <=). Bottom up would give you values that define the lower-end start of a bin (so >=). \ No newline at end of file diff --git a/doc/CDB_MakeHexagon.md b/doc/CDB_MakeHexagon.md new file mode 100644 index 0000000..6b4364d --- /dev/null +++ b/doc/CDB_MakeHexagon.md @@ -0,0 +1,21 @@ +Return an Hexagon with given center and side (or maximal radius) + +#### Using the function + +Running the following SQL + +```sql +SELECT CDB_MakeHexagon(ST_MakePoint(0,0),10000000) +``` + +Would give you back a single hexagon geometry, + +![hexagon](http://i.imgur.com/6jeGStb.png) + + +#### Arguements + +CDB_MakeHexagon(center, radius) + +* **center** geometry +* **radius** float. Radius of hexagon measured in same projection as **center** diff --git a/doc/CDB_QuantileBins.md b/doc/CDB_QuantileBins.md new file mode 100644 index 0000000..ad89c5d --- /dev/null +++ b/doc/CDB_QuantileBins.md @@ -0,0 +1,21 @@ +Find the breaks for N categories in a numerical column based on the [Quantile bins]. Below, the quantile method is used to determine color based on the area of the polygons. + +![qunatile](https://f.cloud.github.com/assets/370259/140714/932ed0e6-722b-11e2-9807-ffbd0fddb9ac.png) + +#### Using the function + +We can determine the 7 most optimal breaks in a column of numerical data as follows, + +```sql +SELECT CDB_QuantileBins(array_agg(numeric_column), 7) FROM table_name +-- Results in an ordered array like, {80,2281,7162,17652,39730,91077,1638094} +-- Each break happens up to, and equal, to a number: +-- (bin1 is less than or equal to 80, bin2 is less than or equal to 2281, etc.) +``` + +#### Arguements + +CDB_QuantileBins(in_array, breaks) + +* **in_array** numeric[]. A NUMERIC array of values. +* **breaks** int. The number of categories you want to create \ No newline at end of file diff --git a/doc/CDB_RectangleGrid.md b/doc/CDB_RectangleGrid.md new file mode 100644 index 0000000..6a71014 --- /dev/null +++ b/doc/CDB_RectangleGrid.md @@ -0,0 +1,46 @@ +Fill given extent with a rectangular coverage + +#### Using the function + +Create a rectangular grid from a polygon geometry. For example, take the geometry + +```sql + ST_SetSRID( + ST_Envelope( + ST_Collect( + ST_MakePoint(10000000,-10000000), + ST_MakePoint(-10000000,10000000) + ) + ), + 3857) +``` + +We can create a grid as follows, + +```sql +SELECT CDB_RectangleGrid( + ST_SetSRID( + ST_Envelope( + ST_Collect( + ST_MakePoint(10000000,-10000000), + ST_MakePoint(-10000000,10000000) + ) + ), + 3857), + 1000000, + 1000000 +) the_geom_webmercator +``` + +Which will look something like this, + +![rect grid](http://i.imgur.com/HuhOJRs.png) + +#### Arguements + +CDB_RectangleGrid(ext, width, height, origin) + +* **ext** geometry. Extent to fill. Only rectangles with center point falling inside the extent (or at the lower or leftmost edge) will be emitted. The returned hexagons will have the same SRID as this extent. +* **width** float. Width of each rectangle. Measure is in the same projection as **ext** +* **height** float. Height of each rectangle. Measure is in the same projection as **ext** +* **origin** OPTIONAL geometry. Optional origin to allow for exact tiling. If omitted the origin will be 0,0. The parameter is checked for having the same SRID as the extent. \ No newline at end of file diff --git a/doc/CDB_SetUserQuotaInBytes.md b/doc/CDB_SetUserQuotaInBytes.md new file mode 100644 index 0000000..b43f3b8 --- /dev/null +++ b/doc/CDB_SetUserQuotaInBytes.md @@ -0,0 +1,11 @@ +Sets user quota in bytes (superuser only) + +#### Using the function + +```sql +SELECT CDB_SetUserQuotaInBytes(10485760); +--- Returns the previously set quota. +--- Use 0 to disable quota. +``` + +REF: https://github.com/CartoDB/cartodb-postgresql/blob/master/scripts-available/CDB_Quota.sql diff --git a/doc/CDB_TransformToWebmercator.md b/doc/CDB_TransformToWebmercator.md new file mode 100644 index 0000000..b4ebb00 --- /dev/null +++ b/doc/CDB_TransformToWebmercator.md @@ -0,0 +1,44 @@ +Function to "safely" transform to webmercator. This function is most useful for rendering custom geometries using the CartoDB tiler. Often, transforming a projection like WGS84 can cause issues with extents beyond what are actually valid in webmercator, this attempts to fix those issues. + +#### Using the function + +Using a box that is nearly the full globe, + +```sql +ST_SetSRID( + ST_Envelope( + ST_Collect( + ST_MakePoint(-180,60), + ST_MakePoint(180,-60) + ) + ), + 4326 +) +``` + +We can then convert it to a renderable webmercator geometry. + +```sql +SELECT CDB_TransformToWebmercator( + ST_SetSRID( + ST_Envelope( + ST_Collect( + ST_MakePoint(-10,60), + ST_MakePoint(300,-60) + ) + ), + 4326 + ) +) +``` + +Would give you back a single valid rectangle in webmercator. Since a longitude of 300 would convert to an unallowed webmercator coordinate, it gets clipped first. Valid extent is WGS84 (-180, -89, 180, 89) + +![valid geom](http://i.imgur.com/EFdXiqt.png) + + +#### Arguements + +CDB_TransformToWebmercator(geom) + +* **geom** geometry \ No newline at end of file diff --git a/doc/CDB_UserTables.md b/doc/CDB_UserTables.md new file mode 100644 index 0000000..6c918ee --- /dev/null +++ b/doc/CDB_UserTables.md @@ -0,0 +1,11 @@ +List the name of available tables (only the usable ones) + +#### Using the function + +```sql +--- Returns a row for each table having given permission with the table name +--- Currently accepted permissions are: 'public', 'private' or 'all' +SELECT CDB_UserTables(perms) +``` + +REF: https://github.com/CartoDB/cartodb-postgresql/blob/master/scripts-available/CDB_UserTables.sql diff --git a/doc/CDB_XYZ_Extent.md b/doc/CDB_XYZ_Extent.md new file mode 100644 index 0000000..dcefde2 --- /dev/null +++ b/doc/CDB_XYZ_Extent.md @@ -0,0 +1,22 @@ +Determine the spatial extent of a tile based on the tile's XYZ coordinate. + +#### Using the function + +Take a common tile with coordinates x=3, y=2, z=2, + +![2/3/2](https://viz2.cartodb.com/tiles/quantile_breaks/2/3/2.png) + +To determine its extent you would run, + +```sql +SELECT CDB_XYZ_Extent(3,2,2) +--- Returns a WKB polygon in Webmercator (SRID 3857) +``` + +#### Arguements + +CDB_XYZ_Extent(x,y,z) + +* **x** integer +* **y** integer +* **z** integer \ No newline at end of file diff --git a/doc/CDB_XYZ_Resolution.md b/doc/CDB_XYZ_Resolution.md new file mode 100644 index 0000000..c47b370 --- /dev/null +++ b/doc/CDB_XYZ_Resolution.md @@ -0,0 +1,20 @@ +Return pixel resolution of tiles at a given zoom level + +#### Using the function + +Take a common tile with zoom, z=2, + +![2/3/2](https://viz2.cartodb.com/tiles/quantile_breaks/2/3/2.png) + +To determine the resolution of these pixels, + +```sql +SELECT CDB_XYZ_Resolution(2) +--- Returns a float, 39135.7587890625 +``` + +#### Arguements + +CDB_XYZ_Resolution(z) + +* **z** integer \ No newline at end of file diff --git a/doc/CartoDB-PLpgSQL.md b/doc/CartoDB-PLpgSQL.md new file mode 100644 index 0000000..9c5976e --- /dev/null +++ b/doc/CartoDB-PLpgSQL.md @@ -0,0 +1,38 @@ +INTRODUCTION +============ + +CartoDB uses a number of custom [PLpgSQL](http://www.postgresql.org/docs/8.3/static/plpgsql.html) functions to perform a few magical things. Those functions are accessible to users on CartoDB as well, so we would like to document what they are and what they do here. + +## Spatial functions + +[CDB_HexagonGrid](CDB_HexagonGrid) - create hexagonal grid from extent and size + +[CDB_MakeHexagon](CDB_MakeHexagon) - make a hexagon with given center and side + +[CDB_RectangleGrid](CDB_RectangleGrid) - fill given extent with a rectangular coverage + +##### Tile based + +[CDB_XYZ_Extent](CDB_XYZ_Extent) - Find the extent of a tile by XYZ + +[CDB_XYZ_Resolution](CDB_XYZ_Resolution) - Find the pixel resolution of tiles + +[CDB_TransformToWebmercator](CDB_TransformToWebmercator) - Convert a geometry to valid webmercator + +## Statistical functions + +[CDB_JenksBins](CDB_JenksBins) - Find breaks in an array of numbers using Jenks method + +[CDB_HeadsTailsBins](CDB_HeadsTailsBins) - Find breaks in an array of numbers using Heads/Tails method + +[CDB_QuantileBins](CDB_QuantileBins) - Find quantile breaks in an array of numbers + +## System functions + +[CDB_UserTables](CDB_UserTables) - Get a list of all tables in your account + +[[CDB_SetUserQuotaInBytes]] - Set maximum user quota in bytes + +column names - now returned in JSON response + +column types - now returned in JSON response diff --git a/doc/CartoDB-user-table.md b/doc/CartoDB-user-table.md new file mode 100644 index 0000000..88d84cc --- /dev/null +++ b/doc/CartoDB-user-table.md @@ -0,0 +1,29 @@ +A "cartodb" user table is a table with a well-known set of fields and a well-known set of triggers attached on. + +The fields are: + + - `cartodb_id`, a numerical primary key of serial type + - `created_at`, timestamp with timezone not null default now() + - `updated_at`, timestamp with timezone not null default now() + - `the_geom`, geometry, GiST indexed, constrained (see below) + - `the_geom_webmercator`, geometry, GiST indexed, constrained (see below) + +The values of "the_geom" and "the_geom_webmercator" must match these constraints: + + - Only POINT, MULTILINE, MULTIPOLYGON types ? Maybe UNCONSTRAINED + - Only 2 dimensions ? Maybe UNCONSTRAINED + - SRID=4326 for the_geom and SRID=3857 for the_geom_webmercator + +The triggers are: + + - `track_updates` after modifying statement updates cdb_tablemetadata + - `test_quota` before changing statement to forbid if overquota + - `test_quota_per_row` before changing row to forbod if overquota (checked on a probabilistic basis) + - `update_the_geom_webmercator` before insert or update row to maintain the_geom_webmercator + - `update_updated_at_trigger` before update row to maintain updated_at + +Some conversions will be attempted to perform upon cartodbfication when certain fields appear: + + - `cartodb_id`: If found type TEXT will be attempted to cast + - `created_at`: If found type TEXT will be attempted to cast + - `updated_at`: If found type TEXT will be attempted to cast \ No newline at end of file diff --git a/doc/README.md b/doc/README.md new file mode 100644 index 0000000..a76d615 --- /dev/null +++ b/doc/README.md @@ -0,0 +1,48 @@ +# Contents + +* [CartoDB-user-table](CartoDB-user-table.md) +* [CartoDB-PLpgSQL](CartoDB-PLpgSQL.md) +* [CDB_ColumnNames](CDB_ColumnNames.md) +* [CDB_ColumnType](CDB_ColumnType.md) +* [CDB_HeadsTailsBins](CDB_HeadsTailsBins.md) +* [CDB_HexagonGrid](CDB_HexagonGrid.md) +* [CDB_JenksBins](CDB_JenksBins.md) +* [CDB_MakeHexagon](CDB_MakeHexagon.md) +* [CDB_QuantileBins](CDB_QuantileBins.md) +* [CDB_RectangleGrid](CDB_RectangleGrid.md) +* [CDB_SetUserQuotaInBytes](CDB_SetUserQuotaInBytes.md) +* [CDB_TransformToWebmercator](CDB_TransformToWebmercator.md) +* [CDB_UserTables](CDB_UserTables.md) +* [CDB_XYZ_Extent](CDB_XYZ_Extent.md) +* [CDB_XYZ_Resolution](CDB_XYZ_Resolution.md) + +The CartoDB PostgreSQL extension is a module to load into each CartoDB user database to perform cartodb-specific security and functionality checks. + +# Checks + +No user other than the superuser should be allowed to change `statement_timeout` for the session (SET statement_timeout disallowed). + +User tables need to match certain structure criteria (See [[CartoDB-user-table]]) so the extension should provide a mean to enforce such structure everytime an attempt to change structure is encountered. + +# Events + +The events we want some function to be called upon are: + +| event | arguments to handler function | function duty | OK* | +|------------------------|--------------------------------------|----------------------------------|-----| +| SET variable | name of variable | forbid changing some vars | | +| RENAME table | old and new name + oid of the table | flush cache | | +| ADD/DROP/ALTER column | oid of the table | flush cache, cartodbfy, upd meta | Y | +| DISABLE/DROP trigger | oid of table, name of trigger | cartodbfy or forbid | | +| DROP table | oid of the table | flush cache and metadata | Y | +| CREATE table | oid of the table | cartodby, upd metadata | Y | +| GRANT | oid of table, privilege, role | flush cache, upd metadata | +| REVOKE | oid of table, privilege, role | flush cache, upd metadata | + + +* event available by installing https://github.com/CartoDB/pg_schema_triggers + +At this stage we don't need more than this, but the number of events and the number of arguments passed to the handler function may expand in the future, so the extension should be written in a way to easily allow that. + +Some of the handler will need to act _after_ the statement is completed (CREATE TABLE, for example). + From 8516cbd4c3debebb27b7e115b0312e9cf4479af3 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Wed, 1 Jul 2015 07:03:13 -0700 Subject: [PATCH 40/62] Splling fix --- doc/CDB_ColumnNames.md | 2 +- doc/CDB_ColumnType.md | 2 +- doc/CDB_HeadsTailsBins.md | 2 +- doc/CDB_MakeHexagon.md | 2 +- doc/CDB_QuantileBins.md | 2 +- doc/CDB_RectangleGrid.md | 2 +- doc/CDB_TransformToWebmercator.md | 2 +- doc/CDB_XYZ_Extent.md | 2 +- doc/CDB_XYZ_Resolution.md | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/doc/CDB_ColumnNames.md b/doc/CDB_ColumnNames.md index ea6c25c..5562440 100644 --- a/doc/CDB_ColumnNames.md +++ b/doc/CDB_ColumnNames.md @@ -7,7 +7,7 @@ SELECT CDB_ColumnNames('table_name') --- Returns a set of rows with column names ``` -#### Arguements +#### Arguments CDB_ColumnNames(table_name) diff --git a/doc/CDB_ColumnType.md b/doc/CDB_ColumnType.md index 823f23e..b16fd78 100644 --- a/doc/CDB_ColumnType.md +++ b/doc/CDB_ColumnType.md @@ -7,7 +7,7 @@ SELECT CDB_ColumnType('column_name','table_name') --- Returns a set of rows with column types ``` -#### Arguements +#### Arguments CDB_ColumnType(column_name, table_name) diff --git a/doc/CDB_HeadsTailsBins.md b/doc/CDB_HeadsTailsBins.md index 38d8b75..305ecc2 100644 --- a/doc/CDB_HeadsTailsBins.md +++ b/doc/CDB_HeadsTailsBins.md @@ -13,7 +13,7 @@ SELECT CDB_HeadsTailsBins(array_agg(numeric_column), 7) FROM table_name -- (bin1 is less than or equal to 7824, bin2 is less than or equal to 23492, etc.) ``` -#### Arguements +#### Arguments CDB_HeadsTailsBins(in_array, breaks) diff --git a/doc/CDB_MakeHexagon.md b/doc/CDB_MakeHexagon.md index 6b4364d..7e76798 100644 --- a/doc/CDB_MakeHexagon.md +++ b/doc/CDB_MakeHexagon.md @@ -13,7 +13,7 @@ Would give you back a single hexagon geometry, ![hexagon](http://i.imgur.com/6jeGStb.png) -#### Arguements +#### Arguments CDB_MakeHexagon(center, radius) diff --git a/doc/CDB_QuantileBins.md b/doc/CDB_QuantileBins.md index ad89c5d..26fd855 100644 --- a/doc/CDB_QuantileBins.md +++ b/doc/CDB_QuantileBins.md @@ -13,7 +13,7 @@ SELECT CDB_QuantileBins(array_agg(numeric_column), 7) FROM table_name -- (bin1 is less than or equal to 80, bin2 is less than or equal to 2281, etc.) ``` -#### Arguements +#### Arguments CDB_QuantileBins(in_array, breaks) diff --git a/doc/CDB_RectangleGrid.md b/doc/CDB_RectangleGrid.md index 6a71014..221ce87 100644 --- a/doc/CDB_RectangleGrid.md +++ b/doc/CDB_RectangleGrid.md @@ -36,7 +36,7 @@ Which will look something like this, ![rect grid](http://i.imgur.com/HuhOJRs.png) -#### Arguements +#### Arguments CDB_RectangleGrid(ext, width, height, origin) diff --git a/doc/CDB_TransformToWebmercator.md b/doc/CDB_TransformToWebmercator.md index b4ebb00..99eab64 100644 --- a/doc/CDB_TransformToWebmercator.md +++ b/doc/CDB_TransformToWebmercator.md @@ -37,7 +37,7 @@ Would give you back a single valid rectangle in webmercator. Since a longitude o ![valid geom](http://i.imgur.com/EFdXiqt.png) -#### Arguements +#### Arguments CDB_TransformToWebmercator(geom) diff --git a/doc/CDB_XYZ_Extent.md b/doc/CDB_XYZ_Extent.md index dcefde2..df022f2 100644 --- a/doc/CDB_XYZ_Extent.md +++ b/doc/CDB_XYZ_Extent.md @@ -13,7 +13,7 @@ SELECT CDB_XYZ_Extent(3,2,2) --- Returns a WKB polygon in Webmercator (SRID 3857) ``` -#### Arguements +#### Arguments CDB_XYZ_Extent(x,y,z) diff --git a/doc/CDB_XYZ_Resolution.md b/doc/CDB_XYZ_Resolution.md index c47b370..7bbf75c 100644 --- a/doc/CDB_XYZ_Resolution.md +++ b/doc/CDB_XYZ_Resolution.md @@ -13,7 +13,7 @@ SELECT CDB_XYZ_Resolution(2) --- Returns a float, 39135.7587890625 ``` -#### Arguements +#### Arguments CDB_XYZ_Resolution(z) From 734561de4cbe44b3713f5f13713a6114cbd4c2e2 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Tue, 7 Jul 2015 05:49:28 -0700 Subject: [PATCH 41/62] Use 'publicuser' as public role, not 'public', closes #95. This is consistent with cartodb behaviour, but not exactly the same as the contract that the 'public' role guarantees access to public resources. Possibly a better fix would be to audit (ug) everything and make sure that it's really using the public role to mean public, rather than the 'publicuser' connection role. That CDB creates. --- scripts-available/CDB_UserTables.sql | 14 ++++++-------- test/CDB_UserTablesTest.sql | 14 ++++++++------ test/CDB_UserTablesTest_expect | 2 ++ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/scripts-available/CDB_UserTables.sql b/scripts-available/CDB_UserTables.sql index 38b6414..9f636a5 100644 --- a/scripts-available/CDB_UserTables.sql +++ b/scripts-available/CDB_UserTables.sql @@ -12,16 +12,14 @@ AS $$ SELECT c.relname FROM pg_class c -JOIN pg_roles r ON r.oid = c.relowner JOIN pg_namespace n ON n.oid = c.relnamespace -WHERE r.rolname = current_user -AND c.relkind = 'r' +WHERE c.relkind = 'r' AND c.relname NOT IN ('cdb_tablemetadata', 'spatial_ref_sys') -AND n.nspname NOT IN ('pg_catalog', 'information_schema') -AND CASE WHEN perm = 'public' THEN has_table_privilege('public', c.oid, 'SELECT') - WHEN perm = 'private' THEN has_table_privilege(c.oid, 'SELECT') AND NOT - has_table_privilege('public', c.oid, 'SELECT') - WHEN perm = 'all' THEN has_table_privilege(c.oid, 'SELECT') +AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'topology') +AND CASE WHEN perm = 'public' THEN has_table_privilege('publicuser', c.oid, 'SELECT') + WHEN perm = 'private' THEN (has_table_privilege(c.relowner, c.oid, 'SELECT') OR has_table_privilege(current_user, c.oid, 'SELECT')) + AND NOT has_table_privilege('publicuser', c.oid, 'SELECT') + WHEN perm = 'all' THEN has_table_privilege(c.relowner, c.oid, 'SELECT') OR has_table_privilege('publicuser', c.oid, 'SELECT') ELSE false END; $$ LANGUAGE 'sql'; diff --git a/test/CDB_UserTablesTest.sql b/test/CDB_UserTablesTest.sql index 8017e03..25e1029 100644 --- a/test/CDB_UserTablesTest.sql +++ b/test/CDB_UserTablesTest.sql @@ -1,11 +1,13 @@ -create table pub(a int); -create table prv(a int); -GRANT SELECT ON TABLE pub TO public; -REVOKE SELECT ON TABLE prv FROM public; +CREATE ROLE publicuser; +CREATE TABLE pub(a int); +CREATE TABLE prv(a int); +GRANT SELECT ON TABLE pub TO publicuser; +REVOKE SELECT ON TABLE prv FROM publicuser; SELECT CDB_UserTables() ORDER BY 1; SELECT 'all',CDB_UserTables('all') ORDER BY 2; SELECT 'public',CDB_UserTables('public') ORDER BY 2; SELECT 'private',CDB_UserTables('private') ORDER BY 2; SELECT '--unsupported--',CDB_UserTables('--unsupported--') ORDER BY 2; -drop table pub; -drop table prv; +DROP TABLE pub; +DROP TABLE prv; +DROP ROLE publicuser; diff --git a/test/CDB_UserTablesTest_expect b/test/CDB_UserTablesTest_expect index 16bd19a..0eb6786 100644 --- a/test/CDB_UserTablesTest_expect +++ b/test/CDB_UserTablesTest_expect @@ -1,3 +1,4 @@ +CREATE ROLE CREATE TABLE CREATE TABLE GRANT @@ -10,3 +11,4 @@ public|pub private|prv DROP TABLE DROP TABLE +DROP ROLE From 08cdb3873056315e6baa3cb16c647f81308499cc Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Tue, 7 Jul 2015 06:07:41 -0700 Subject: [PATCH 42/62] Prepare for 0.8.1 release --- Makefile | 3 ++- NEWS.md | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index f4950d2..e0778df 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.8.0 +EXTVERSION = 0.8.1 SED = sed @@ -39,6 +39,7 @@ UPGRADABLE = \ 0.7.2 \ 0.7.3 \ 0.7.4 \ + 0.8.0 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) diff --git a/NEWS.md b/NEWS.md index 0c02c7d..5541b2e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +0.8.1 (2015-06-30) +------------------ +* Fix for [#95](https://github.com/CartoDB/cartodb-postgresql/issues/95) *cdb_usertables should return public tables when the user is publicuser* + 0.8.0 (2015-06-30) ------------------ * Adds new function CDB_QueryTablesText that can deal with "schema.table_name" From 011a007f04866ba1e2a38c5c1bce797b5ef765f3 Mon Sep 17 00:00:00 2001 From: Paul Ramsey Date: Tue, 7 Jul 2015 06:13:02 -0700 Subject: [PATCH 43/62] Return test results in deterministic order (closes #96) --- test/CDB_XYZ_ExtentTest.sql | 2 +- test/CDB_XYZ_ExtentTest_expect | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/CDB_XYZ_ExtentTest.sql b/test/CDB_XYZ_ExtentTest.sql index 6e01545..2202c3d 100644 --- a/test/CDB_XYZ_ExtentTest.sql +++ b/test/CDB_XYZ_ExtentTest.sql @@ -5,5 +5,5 @@ inp AS ( select z0.z, r1.r as x, r2.r as y FROM zoom z0, range r1, range r2 WHER ext AS ( select x,y,z,CDB_XYZ_Extent(x,y,z) as g from inp ) select X::text || ',' || Y::text || ',' || Z::text as xyz, st_xmin(g), st_xmax(g), st_ymin(g), st_ymax(g) - from ext; + from ext order by xyz; diff --git a/test/CDB_XYZ_ExtentTest_expect b/test/CDB_XYZ_ExtentTest_expect index ba34b4c..48c8759 100644 --- a/test/CDB_XYZ_ExtentTest_expect +++ b/test/CDB_XYZ_ExtentTest_expect @@ -1,13 +1,13 @@ 0,0,0|-20037508.5|20037508.5|-20037508.5|20037508.5 0,0,1|-20037508.5|0|0|20037508.5 -0,1,1|-20037508.5|0|-20037508.5|0 -1,0,1|0|20037508.5|0|20037508.5 -1,1,1|0|20037508.5|-20037508.5|0 0,0,2|-20037508.5|-10018754.25|10018754.25|20037508.5 +0,1,1|-20037508.5|0|-20037508.5|0 0,1,2|-20037508.5|-10018754.25|0|10018754.25 0,2,2|-20037508.5|-10018754.25|-10018754.25|0 0,3,2|-20037508.5|-10018754.25|-20037508.5|-10018754.25 +1,0,1|0|20037508.5|0|20037508.5 1,0,2|-10018754.25|0|10018754.25|20037508.5 +1,1,1|0|20037508.5|-20037508.5|0 1,1,2|-10018754.25|0|0|10018754.25 1,2,2|-10018754.25|0|-10018754.25|0 1,3,2|-10018754.25|0|-20037508.5|-10018754.25 From e05613d5c4b1dba4fa7fddd71a3b4e00b14819e9 Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 10:25:44 +0200 Subject: [PATCH 44/62] added tests for usertables to check private tables are not returned --- test/CDB_UserTablesTest.sql | 6 ++++++ test/CDB_UserTablesTest_expect | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/test/CDB_UserTablesTest.sql b/test/CDB_UserTablesTest.sql index 25e1029..836a748 100644 --- a/test/CDB_UserTablesTest.sql +++ b/test/CDB_UserTablesTest.sql @@ -8,6 +8,12 @@ SELECT 'all',CDB_UserTables('all') ORDER BY 2; SELECT 'public',CDB_UserTables('public') ORDER BY 2; SELECT 'private',CDB_UserTables('private') ORDER BY 2; SELECT '--unsupported--',CDB_UserTables('--unsupported--') ORDER BY 2; +-- now tests with public user +\c contrib_regression publicuser +SELECT 'all_publicuser',CDB_UserTables('all') ORDER BY 2; +SELECT 'public_publicuser',CDB_UserTables('public') ORDER BY 2; +SELECT 'private_publicuser',CDB_UserTables('private') ORDER BY 2; +\c contrib_regression postgres DROP TABLE pub; DROP TABLE prv; DROP ROLE publicuser; diff --git a/test/CDB_UserTablesTest_expect b/test/CDB_UserTablesTest_expect index 0eb6786..73f01a4 100644 --- a/test/CDB_UserTablesTest_expect +++ b/test/CDB_UserTablesTest_expect @@ -9,6 +9,10 @@ all|prv all|pub public|pub private|prv +You are now connected to database "contrib_regression" as user "publicuser" +all_publicuser|pub +public_publicuser|pub +You are now connected to database "contrib_regression" as user "postgres" DROP TABLE DROP TABLE DROP ROLE From df36e83cb552bb8cb287a12b53a562730003807f Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 10:31:55 +0200 Subject: [PATCH 45/62] revoke permissions to list private tables to public user --- scripts-available/CDB_UserTables.sql | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/scripts-available/CDB_UserTables.sql b/scripts-available/CDB_UserTables.sql index 9f636a5..bfa5165 100644 --- a/scripts-available/CDB_UserTables.sql +++ b/scripts-available/CDB_UserTables.sql @@ -17,9 +17,8 @@ WHERE c.relkind = 'r' AND c.relname NOT IN ('cdb_tablemetadata', 'spatial_ref_sys') AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'topology') AND CASE WHEN perm = 'public' THEN has_table_privilege('publicuser', c.oid, 'SELECT') - WHEN perm = 'private' THEN (has_table_privilege(c.relowner, c.oid, 'SELECT') OR has_table_privilege(current_user, c.oid, 'SELECT')) - AND NOT has_table_privilege('publicuser', c.oid, 'SELECT') - WHEN perm = 'all' THEN has_table_privilege(c.relowner, c.oid, 'SELECT') OR has_table_privilege('publicuser', c.oid, 'SELECT') + WHEN perm = 'private' THEN has_table_privilege(current_user, c.oid, 'SELECT') AND NOT has_table_privilege('publicuser', c.oid, 'SELECT') + WHEN perm = 'all' THEN has_table_privilege(current_user, c.oid, 'SELECT') OR has_table_privilege('publicuser', c.oid, 'SELECT') ELSE false END; $$ LANGUAGE 'sql'; From 5d6c2111bf9e4f4e1029114d629a5f157d6d4fe6 Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 10:37:07 +0200 Subject: [PATCH 46/62] updates tests --- test/CDB_UserTablesTest.sql | 2 +- test/CDB_UserTablesTest_expect | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/CDB_UserTablesTest.sql b/test/CDB_UserTablesTest.sql index 836a748..97821e7 100644 --- a/test/CDB_UserTablesTest.sql +++ b/test/CDB_UserTablesTest.sql @@ -1,4 +1,4 @@ -CREATE ROLE publicuser; +CREATE ROLE publicuser LOGIN; CREATE TABLE pub(a int); CREATE TABLE prv(a int); GRANT SELECT ON TABLE pub TO publicuser; diff --git a/test/CDB_UserTablesTest_expect b/test/CDB_UserTablesTest_expect index 73f01a4..2325bab 100644 --- a/test/CDB_UserTablesTest_expect +++ b/test/CDB_UserTablesTest_expect @@ -9,10 +9,10 @@ all|prv all|pub public|pub private|prv -You are now connected to database "contrib_regression" as user "publicuser" +You are now connected to database "contrib_regression" as user "publicuser". all_publicuser|pub public_publicuser|pub -You are now connected to database "contrib_regression" as user "postgres" +You are now connected to database "contrib_regression" as user "postgres". DROP TABLE DROP TABLE DROP ROLE From 1b3db28a741582fc0b6d367bcffd8f0850e92acb Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 10:51:20 +0200 Subject: [PATCH 47/62] added tests for organization --- test/organization/test.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/organization/test.sh b/test/organization/test.sh index c713afa..c1a65f3 100644 --- a/test/organization/test.sh +++ b/test/organization/test.sh @@ -346,6 +346,23 @@ function test_cdb_querytables_does_not_return_functions_as_part_of_the_resultset sql postgres "select * from CDB_QueryTables('select * from cdb_testmember_1.foo, cdb_testmember_2.bar, plainto_tsquery(''foo'')');" should "{cdb_testmember_1.foo,cdb_testmember_2.bar}" } +function test_cdb_usertables_should_work_with_orgusers() { + sql "CREATE ROLE publicuser LOGIN" + sql "GRANT USAGE ON SCHEMA cartodb TO publicuser;" + ${CMD} -d ${DATABASE} -f scripts-available/CDB_UserTables.sql + sql cdb_testmember_1 "CREATE TABLE test_perms_pub (a int)" + sql cdb_testmember_1 "CREATE TABLE test_perms_priv (a int)" + sql cdb_testmember_1 "GRANT SELECT ON TABLE test_perms_pub TO publicuser" + sql publicuser "SELECT count(*) FROM CDB_UserTables('all')" should 1 + sql publicuser "SELECT count(*) FROM CDB_UserTables('public')" should 1 + sql publicuser "SELECT count(*) FROM CDB_UserTables('private')" should 0 + # the following tests are for https://github.com/CartoDB/cartodb-postgresql/issues/98 + #sql cdb_testmember_2 "SELECT count(*) FROM CDB_UserTables('all')" should 1 + #sql cdb_testmember_2 "SELECT count(*) FROM CDB_UserTables('public')" should 1 + #sql cdb_testmember_2 "SELECT count(*) FROM CDB_UserTables('private')" should 0 +} + + #################################################### TESTS END HERE #################################################### From 12260b9fc3537a029b95ea26864af2778b9b9d64 Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 10:54:43 +0200 Subject: [PATCH 48/62] test cleanup --- test/organization/test.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/organization/test.sh b/test/organization/test.sh index c1a65f3..3ada56b 100644 --- a/test/organization/test.sh +++ b/test/organization/test.sh @@ -360,6 +360,9 @@ function test_cdb_usertables_should_work_with_orgusers() { #sql cdb_testmember_2 "SELECT count(*) FROM CDB_UserTables('all')" should 1 #sql cdb_testmember_2 "SELECT count(*) FROM CDB_UserTables('public')" should 1 #sql cdb_testmember_2 "SELECT count(*) FROM CDB_UserTables('private')" should 0 + + sql cdb_testmember_1 "DROP TABLE test_perms_pub" + sql cdb_testmember_1 "DROP TABLE test_perms_priv" } From c71faf21e27cddc25a24a4bf0d80b287c0b795d7 Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 10:57:38 +0200 Subject: [PATCH 49/62] do not create publicuser since a previous tests is doing it --- test/organization/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/organization/test.sh b/test/organization/test.sh index 3ada56b..bc70ef1 100644 --- a/test/organization/test.sh +++ b/test/organization/test.sh @@ -347,7 +347,7 @@ function test_cdb_querytables_does_not_return_functions_as_part_of_the_resultset } function test_cdb_usertables_should_work_with_orgusers() { - sql "CREATE ROLE publicuser LOGIN" + #sql "CREATE ROLE publicuser LOGIN" sql "GRANT USAGE ON SCHEMA cartodb TO publicuser;" ${CMD} -d ${DATABASE} -f scripts-available/CDB_UserTables.sql sql cdb_testmember_1 "CREATE TABLE test_perms_pub (a int)" From 53e6b38c325c4daabfcb485abaffd97a97ef39c4 Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 11:03:21 +0200 Subject: [PATCH 50/62] enabling user again (no sense) --- test/organization/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/organization/test.sh b/test/organization/test.sh index bc70ef1..1dbabeb 100644 --- a/test/organization/test.sh +++ b/test/organization/test.sh @@ -347,7 +347,7 @@ function test_cdb_querytables_does_not_return_functions_as_part_of_the_resultset } function test_cdb_usertables_should_work_with_orgusers() { - #sql "CREATE ROLE publicuser LOGIN" + sql "CREATE ROLE publicuser" sql "GRANT USAGE ON SCHEMA cartodb TO publicuser;" ${CMD} -d ${DATABASE} -f scripts-available/CDB_UserTables.sql sql cdb_testmember_1 "CREATE TABLE test_perms_pub (a int)" From 68fdd9ce33afa98d8296e595b2f0b08cb56debae Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 11:07:46 +0200 Subject: [PATCH 51/62] just add login permissions to public user --- test/organization/test.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/organization/test.sh b/test/organization/test.sh index 1dbabeb..431c2ba 100644 --- a/test/organization/test.sh +++ b/test/organization/test.sh @@ -347,7 +347,7 @@ function test_cdb_querytables_does_not_return_functions_as_part_of_the_resultset } function test_cdb_usertables_should_work_with_orgusers() { - sql "CREATE ROLE publicuser" + sql "ALTER USER publicuser LOGIN" sql "GRANT USAGE ON SCHEMA cartodb TO publicuser;" ${CMD} -d ${DATABASE} -f scripts-available/CDB_UserTables.sql sql cdb_testmember_1 "CREATE TABLE test_perms_pub (a int)" @@ -363,6 +363,7 @@ function test_cdb_usertables_should_work_with_orgusers() { sql cdb_testmember_1 "DROP TABLE test_perms_pub" sql cdb_testmember_1 "DROP TABLE test_perms_priv" + sql "ALTER USER publicuser NOLOGIN" } From 92b5d1f8f4c91c65cbccf10494e1789ea8aa07e8 Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 11:15:14 +0200 Subject: [PATCH 52/62] creating in setup --- test/organization/test.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/organization/test.sh b/test/organization/test.sh index 431c2ba..75e5b5c 100644 --- a/test/organization/test.sh +++ b/test/organization/test.sh @@ -142,6 +142,8 @@ function setup() { log_info "############################# SETUP #############################" create_role_and_schema cdb_testmember_1 create_role_and_schema cdb_testmember_2 + sql "CREATE ROLE publicuser LOGIN;" + sql "GRANT CONNECT ON DATABASE \"${DATABASE}\" TO publicuser;" create_table cdb_testmember_1 foo sql cdb_testmember_1 'INSERT INTO cdb_testmember_1.foo VALUES (1), (2), (3), (4), (5);' @@ -171,6 +173,7 @@ function tear_down() { sql 'DROP ROLE cdb_testmember_1;' sql 'DROP ROLE cdb_testmember_2;' + sql 'DROP ROLE publicuser;' ${CMD} -c "DROP DATABASE ${DATABASE}" } @@ -347,7 +350,6 @@ function test_cdb_querytables_does_not_return_functions_as_part_of_the_resultset } function test_cdb_usertables_should_work_with_orgusers() { - sql "ALTER USER publicuser LOGIN" sql "GRANT USAGE ON SCHEMA cartodb TO publicuser;" ${CMD} -d ${DATABASE} -f scripts-available/CDB_UserTables.sql sql cdb_testmember_1 "CREATE TABLE test_perms_pub (a int)" @@ -363,7 +365,6 @@ function test_cdb_usertables_should_work_with_orgusers() { sql cdb_testmember_1 "DROP TABLE test_perms_pub" sql cdb_testmember_1 "DROP TABLE test_perms_priv" - sql "ALTER USER publicuser NOLOGIN" } From 400248cd5d1f48def6269b7a14898d4d9b160357 Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 11:20:55 +0200 Subject: [PATCH 53/62] remove connection permissions to public user --- test/organization/test.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/test/organization/test.sh b/test/organization/test.sh index 75e5b5c..40d1a41 100644 --- a/test/organization/test.sh +++ b/test/organization/test.sh @@ -170,6 +170,7 @@ function tear_down() { sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM cdb_testmember_1;" sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM cdb_testmember_2;" + sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM publicuser;" sql 'DROP ROLE cdb_testmember_1;' sql 'DROP ROLE cdb_testmember_2;' From 189a73ce4d644b31853b6f1971613eda0785ca0c Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 16:12:15 +0200 Subject: [PATCH 54/62] fixed tests --- test/CDB_QuotaTest_expect | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CDB_QuotaTest_expect b/test/CDB_QuotaTest_expect index ba04208..d7f289d 100644 --- a/test/CDB_QuotaTest_expect +++ b/test/CDB_QuotaTest_expect @@ -9,7 +9,7 @@ INSERT 0 2048 INSERT 0 2048 INSERT 0 2048 2 -ERROR: Quota exceeded by 567.998046875KB +ERROR: Quota exceeded by 559.998046875KB 0 INSERT 0 1 DROP TABLE From 788d2a7f1052a54ae01ab131c115334e647bed6b Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 16:12:42 +0200 Subject: [PATCH 55/62] ready for 0.8.2 --- Makefile | 5 +++-- NEWS.md | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index e0778df..ce14fd7 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.8.1 +EXTVERSION = 0.8.2 SED = sed @@ -40,6 +40,7 @@ UPGRADABLE = \ 0.7.3 \ 0.7.4 \ 0.8.0 \ + 0.8.1 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) @@ -119,5 +120,5 @@ test_extension_new: legacy_tests: legacy_regress -installcheck: legacy_tests test_extension_new test_organization +installcheck: test_organization diff --git a/NEWS.md b/NEWS.md index 5541b2e..04df901 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +0.8.2 (2015-07-27) +------------------ +* Fix for CDB_UserTables returning wrong listings when publicuser is used + 0.8.1 (2015-06-30) ------------------ * Fix for [#95](https://github.com/CartoDB/cartodb-postgresql/issues/95) *cdb_usertables should return public tables when the user is publicuser* From 7921cad4f5b45bb979687d383096c215a4da0373 Mon Sep 17 00:00:00 2001 From: javi Date: Mon, 27 Jul 2015 16:49:59 +0200 Subject: [PATCH 56/62] readded removed tests by error --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ce14fd7..bd04084 100644 --- a/Makefile +++ b/Makefile @@ -120,5 +120,5 @@ test_extension_new: legacy_tests: legacy_regress -installcheck: test_organization +installcheck: legacy_tests test_extension_new test_organization From 513488da0ef0cc5516a774f2a8dc7022675ad303 Mon Sep 17 00:00:00 2001 From: javi Date: Tue, 28 Jul 2015 09:05:03 +0200 Subject: [PATCH 57/62] change random value for quota test again --- test/CDB_QuotaTest_expect | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/CDB_QuotaTest_expect b/test/CDB_QuotaTest_expect index d7f289d..ba04208 100644 --- a/test/CDB_QuotaTest_expect +++ b/test/CDB_QuotaTest_expect @@ -9,7 +9,7 @@ INSERT 0 2048 INSERT 0 2048 INSERT 0 2048 2 -ERROR: Quota exceeded by 559.998046875KB +ERROR: Quota exceeded by 567.998046875KB 0 INSERT 0 1 DROP TABLE From cb57af9074b7dd7a83a8ff2bdb0f7df531545cbb Mon Sep 17 00:00:00 2001 From: javi santana Date: Mon, 10 Aug 2015 11:01:28 +0200 Subject: [PATCH 58/62] added requirements for test --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 5ed529d..771fe12 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ NOTE: if ``test_ddl_triggers`` fails it's likely due to an incomplete NOTE: you need to run the installcheck as a superuser, use PGUSER env variable if needed, like: PGUSER=postgres make installcheck + +NOTE: the tests need to run against a **clean postgres instance**, if you have some roles already created test will likely fail due `publicuser` not being dropped. Enable database --------------- From 14213c5d6af9122054497dc19e6b675b1f22df3b Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 12 Aug 2015 10:10:21 +0200 Subject: [PATCH 59/62] Test scenario to show how public tables can be accessed by other roles This will require changes in cartodb/cartodb rails app because user/role creation happens over there. So it's not possible to fix all the problems within the extension right now. --- test/organization/test.sh | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/test/organization/test.sh b/test/organization/test.sh index 40d1a41..66ce7fe 100644 --- a/test/organization/test.sh +++ b/test/organization/test.sh @@ -351,18 +351,40 @@ function test_cdb_querytables_does_not_return_functions_as_part_of_the_resultset } function test_cdb_usertables_should_work_with_orgusers() { - sql "GRANT USAGE ON SCHEMA cartodb TO publicuser;" - ${CMD} -d ${DATABASE} -f scripts-available/CDB_UserTables.sql + # create tables sql cdb_testmember_1 "CREATE TABLE test_perms_pub (a int)" - sql cdb_testmember_1 "CREATE TABLE test_perms_priv (a int)" + sql cdb_testmember_1 "INSERT INTO test_perms_pub (a) values (1);" sql cdb_testmember_1 "GRANT SELECT ON TABLE test_perms_pub TO publicuser" + + sql cdb_testmember_1 "CREATE TABLE test_perms_priv (a int)" + + + # this is what we need to make public tables available in CDB_UserTables + sql postgres "grant publicuser to cdb_testmember_1;" + sql postgres "grant publicuser to cdb_testmember_2;" + + + # this is required to enable select from other schema + sql postgres "GRANT USAGE ON SCHEMA cdb_testmember_1 TO cdb_testmember_2"; + + + # test CDB_UserTables with publicuser + ${CMD} -d ${DATABASE} -f scripts-available/CDB_UserTables.sql + sql publicuser "SELECT count(*) FROM CDB_UserTables('all')" should 1 sql publicuser "SELECT count(*) FROM CDB_UserTables('public')" should 1 sql publicuser "SELECT count(*) FROM CDB_UserTables('private')" should 0 + sql publicuser "SELECT * FROM CDB_UserTables('all')" should "test_perms_pub" + sql publicuser "SELECT * FROM CDB_UserTables('public')" should "test_perms_pub" + sql publicuser "SELECT * FROM CDB_UserTables('private')" should "" # the following tests are for https://github.com/CartoDB/cartodb-postgresql/issues/98 - #sql cdb_testmember_2 "SELECT count(*) FROM CDB_UserTables('all')" should 1 - #sql cdb_testmember_2 "SELECT count(*) FROM CDB_UserTables('public')" should 1 - #sql cdb_testmember_2 "SELECT count(*) FROM CDB_UserTables('private')" should 0 + # cdb_testmember_2 is already owner of `bar` table + sql cdb_testmember_2 "select string_agg(t,',') from (select cdb_usertables('all') t order by t) as s" should "bar,test_perms_pub" + sql cdb_testmember_2 "SELECT * FROM CDB_UserTables('public')" should "test_perms_pub" + sql cdb_testmember_2 "SELECT * FROM CDB_UserTables('private')" should "bar" + + # test cdb_testmember_2 can select from cdb_testmember_1's public table + sql cdb_testmember_2 "SELECT * FROM cdb_testmember_1.test_perms_pub" should 1 sql cdb_testmember_1 "DROP TABLE test_perms_pub" sql cdb_testmember_1 "DROP TABLE test_perms_priv" From 4e31d3a37e30e92af9d62e268a8563e26eecdc75 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 13 Aug 2015 13:23:35 +0200 Subject: [PATCH 60/62] Fixes CDB_UserDataSize failing due `ERROR: relation "*" does not exist.` Adds new _CDB_total_relation_size function that handles nonexistent tables and does fallback to size=0. That function could be used to cache total relation size or query another table view with a cached total relation size. Fixes #108 --- Makefile | 5 +++-- NEWS.md | 4 ++++ scripts-available/CDB_Quota.sql | 21 ++++++++++++++++++++- test/CDB_QuotaTest.sql | 5 +++++ test/CDB_QuotaTest_expect | 3 +++ 5 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index bd04084..e01d865 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.8.2 +EXTVERSION = 0.8.3 SED = sed @@ -41,6 +41,7 @@ UPGRADABLE = \ 0.7.4 \ 0.8.0 \ 0.8.1 \ + 0.8.2 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) @@ -120,5 +121,5 @@ test_extension_new: legacy_tests: legacy_regress -installcheck: legacy_tests test_extension_new test_organization +installcheck: legacy_tests diff --git a/NEWS.md b/NEWS.md index 04df901..6d68994 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +0.8.3 (2015-mm-dd) +------------------ +* Fixes CDB_UserDataSize failing due `ERROR: relation "*" does not exist.` [#108](https://github.com/CartoDB/cartodb-postgresql/issues/108) + 0.8.2 (2015-07-27) ------------------ * Fix for CDB_UserTables returning wrong listings when publicuser is used diff --git a/scripts-available/CDB_Quota.sql b/scripts-available/CDB_Quota.sql index 9d53d2a..3474f48 100644 --- a/scripts-available/CDB_Quota.sql +++ b/scripts-available/CDB_Quota.sql @@ -1,3 +1,22 @@ +CREATE OR REPLACE FUNCTION cartodb._CDB_total_relation_size(_schema_name TEXT, _table_name TEXT) +RETURNS bigint AS +$$ +BEGIN + IF EXISTS ( + SELECT 1 FROM information_schema.tables + WHERE table_catalog = current_database() + AND table_schema = _schema_name + AND table_name = _table_name + ) + THEN + RETURN pg_total_relation_size(format('"%s"."%s"', _schema_name, _table_name)); + ELSE + RETURN 0; + END IF; +END; +$$ +LANGUAGE 'plpgsql' VOLATILE; + -- Return the estimated size of user data. Used for quota checking. CREATE OR REPLACE FUNCTION CDB_UserDataSize(schema_name TEXT) RETURNS bigint AS @@ -24,7 +43,7 @@ BEGIN FROM user_tables ), sizes AS ( - SELECT COALESCE(INT8(SUM(pg_total_relation_size('"' || schema_name || '"."' || table_name || '"')))) table_size, + SELECT COALESCE(INT8(SUM(cartodb._CDB_total_relation_size(schema_name, table_name)))) table_size, CASE WHEN is_overview THEN 0 WHEN is_raster THEN 1 diff --git a/test/CDB_QuotaTest.sql b/test/CDB_QuotaTest.sql index e54a74d..69cd84b 100644 --- a/test/CDB_QuotaTest.sql +++ b/test/CDB_QuotaTest.sql @@ -13,6 +13,11 @@ SELECT CDB_CartodbfyTable('big'); INSERT INTO big SELECT generate_series(1,2048); INSERT INTO big SELECT generate_series(1,2048); INSERT INTO big SELECT generate_series(1,2048); +-- Test for #108: https://github.com/CartoDB/cartodb-postgresql/issues/108 +SELECT CDB_UserDataSize(); +SELECT cartodb._CDB_total_relation_size('public', 'big'); +SELECT cartodb._CDB_total_relation_size('public', 'nonexistent_table_name'); +-- END Test for #108 SELECT CDB_SetUserQuotaInBytes(2); INSERT INTO big VALUES (1); SELECT CDB_SetUserQuotaInBytes(0); diff --git a/test/CDB_QuotaTest_expect b/test/CDB_QuotaTest_expect index ba04208..ebe7156 100644 --- a/test/CDB_QuotaTest_expect +++ b/test/CDB_QuotaTest_expect @@ -8,6 +8,9 @@ ERROR: Quota exceeded by 3.9990234375KB INSERT 0 2048 INSERT 0 2048 INSERT 0 2048 +581632 +1163264 +0 2 ERROR: Quota exceeded by 567.998046875KB 0 From 714ba9d0dc85d44b1e422f69e97a4de549949e90 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 13 Aug 2015 13:27:27 +0200 Subject: [PATCH 61/62] Keep all test running :D --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e01d865..b0241fd 100644 --- a/Makefile +++ b/Makefile @@ -121,5 +121,5 @@ test_extension_new: legacy_tests: legacy_regress -installcheck: legacy_tests +installcheck: legacy_tests test_extension_new test_organization From e959bba335870bad0c8483307260a021e786a9c9 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 13 Aug 2015 18:53:41 +0200 Subject: [PATCH 62/62] Validates it's enought to grant publicuser to user. Reference to PR where it will be actually fixed for now. --- test/organization/test.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/organization/test.sh b/test/organization/test.sh index 66ce7fe..60e577e 100644 --- a/test/organization/test.sh +++ b/test/organization/test.sh @@ -351,6 +351,9 @@ function test_cdb_querytables_does_not_return_functions_as_part_of_the_resultset } function test_cdb_usertables_should_work_with_orgusers() { + + # This test validates the changes proposed in https://github.com/CartoDB/cartodb/pull/5021 + # create tables sql cdb_testmember_1 "CREATE TABLE test_perms_pub (a int)" sql cdb_testmember_1 "INSERT INTO test_perms_pub (a) values (1);" @@ -365,7 +368,7 @@ function test_cdb_usertables_should_work_with_orgusers() { # this is required to enable select from other schema - sql postgres "GRANT USAGE ON SCHEMA cdb_testmember_1 TO cdb_testmember_2"; + sql postgres "GRANT USAGE ON SCHEMA cdb_testmember_1 TO publicuser"; # test CDB_UserTables with publicuser