From 49c4cea4e7736bde316e4c921689d6dbe2735d73 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 25 Aug 2015 22:45:55 -0400 Subject: [PATCH 01/22] adding kurtosis --- scripts-available/CDB_Stats.sql | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 scripts-available/CDB_Stats.sql diff --git a/scripts-available/CDB_Stats.sql b/scripts-available/CDB_Stats.sql new file mode 100644 index 0000000..f0a47ba --- /dev/null +++ b/scripts-available/CDB_Stats.sql @@ -0,0 +1,33 @@ +-- +-- Calculate the Pearson kurtosis of the input data +-- +-- @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_Kurtosis ( in_array NUMERIC[] ) RETURNS NUMERIC as $$ +DECLARE + a numeric; + c numeric; + s numeric; + k numeric; +BEGIN + SELECT AVG(e), COUNT(e)::numeric, stddev(e) INTO a, c, s FROM ( SELECT unnest(in_array) e ) x; + + RAISE NOTICE 'avg: %, cnt: %, std: %', a, c, s; + + EXECUTE ' + SELECT sum(power($1 - e,4)) / ( $2 * power($3, 4)) + FROM (SELECT unnest($4) e ) x' + INTO k + USING a, c, s, in_array; + + RETURN k; +END; +$$ language plpgsql IMMUTABLE; From db323f3e13574cf700c62e5a2001d669f1152960 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 25 Aug 2015 22:58:31 -0400 Subject: [PATCH 02/22] adding skewness --- scripts-available/CDB_Stats.sql | 39 +++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/scripts-available/CDB_Stats.sql b/scripts-available/CDB_Stats.sql index f0a47ba..b9b765b 100644 --- a/scripts-available/CDB_Stats.sql +++ b/scripts-available/CDB_Stats.sql @@ -1,18 +1,15 @@ -- --- Calculate the Pearson kurtosis of the input data +-- Calculate basic statistics of a given dataset -- --- @param in_array A numeric array of numbers to determine the best --- to determine the bin boundary +-- @param in_array A numeric array of numbers -- --- @param breaks The number of bins you want to find. --- --- --- Returns: upper edges of bins +-- Returns: statistical quantity chosen -- -- +-- Calculate Pearson's moment coefficient of kurtosis CREATE OR REPLACE FUNCTION CDB_Kurtosis ( in_array NUMERIC[] ) RETURNS NUMERIC as $$ -DECLARE +DECLARE a numeric; c numeric; s numeric; @@ -22,12 +19,32 @@ BEGIN RAISE NOTICE 'avg: %, cnt: %, std: %', a, c, s; - EXECUTE ' - SELECT sum(power($1 - e,4)) / ( $2 * power($3, 4)) - FROM (SELECT unnest($4) e ) x' + EXECUTE 'SELECT sum(power($1 - e, 4)) / ( $2 * power($3, 4)) + FROM (SELECT unnest($4) e ) x' INTO k USING a, c, s, in_array; RETURN k; END; $$ language plpgsql IMMUTABLE; + +-- Calculate Pearson's moment coefficient of skewness +CREATE OR REPLACE FUNCTION CDB_Skewness ( in_array NUMERIC[] ) RETURNS NUMERIC as $$ +DECLARE + a numeric; + c numeric; + s numeric; + sk numeric; +BEGIN + SELECT AVG(e), COUNT(e)::numeric, stddev(e) INTO a, c, s FROM ( SELECT unnest(in_array) e ) x; + + RAISE NOTICE 'avg: %, cnt: %, std: %', a, c, s; + + EXECUTE 'SELECT sum(power($1 - e, 3)) / ( $2 * power($3, 3)) + FROM (SELECT unnest($4) e ) x' + INTO sk + USING a, c, s, in_array; + + RETURN sk; +END; +$$ language plpgsql IMMUTABLE; From d723487f67811f1aa3ee13ad8d49a826b6867d41 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 25 Aug 2015 23:10:26 -0400 Subject: [PATCH 03/22] updated definition --- scripts-available/CDB_Stats.sql | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/scripts-available/CDB_Stats.sql b/scripts-available/CDB_Stats.sql index b9b765b..543b3ee 100644 --- a/scripts-available/CDB_Stats.sql +++ b/scripts-available/CDB_Stats.sql @@ -5,9 +5,10 @@ -- -- Returns: statistical quantity chosen -- +-- References: http://www.itl.nist.gov/div898/handbook/eda/section3/eda35b.htm -- --- Calculate Pearson's moment coefficient of kurtosis +-- Calculate kurtosis CREATE OR REPLACE FUNCTION CDB_Kurtosis ( in_array NUMERIC[] ) RETURNS NUMERIC as $$ DECLARE a numeric; @@ -19,7 +20,7 @@ BEGIN RAISE NOTICE 'avg: %, cnt: %, std: %', a, c, s; - EXECUTE 'SELECT sum(power($1 - e, 4)) / ( $2 * power($3, 4)) + EXECUTE 'SELECT sum(power($1 - e, 4)) / ( $2 * power($3, 4)) - 3 FROM (SELECT unnest($4) e ) x' INTO k USING a, c, s, in_array; @@ -28,7 +29,7 @@ BEGIN END; $$ language plpgsql IMMUTABLE; --- Calculate Pearson's moment coefficient of skewness +-- Calculate skewness CREATE OR REPLACE FUNCTION CDB_Skewness ( in_array NUMERIC[] ) RETURNS NUMERIC as $$ DECLARE a numeric; From 14e2a65523f989e3879e0cef64e1b9479df4f199 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Tue, 25 Aug 2015 23:11:09 -0400 Subject: [PATCH 04/22] adding symlink --- scripts-enabled/CDB_Stats.sql | 1 + 1 file changed, 1 insertion(+) create mode 120000 scripts-enabled/CDB_Stats.sql diff --git a/scripts-enabled/CDB_Stats.sql b/scripts-enabled/CDB_Stats.sql new file mode 120000 index 0000000..0d7fa50 --- /dev/null +++ b/scripts-enabled/CDB_Stats.sql @@ -0,0 +1 @@ +scripts-available/CDB_Stats.sql \ No newline at end of file From 0ba57f436ad0b46122ddb5a080fdbefad99b68d6 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Mon, 31 Aug 2015 12:01:01 +0200 Subject: [PATCH 05/22] Do not remove old function #120 The `DROP FUNCTION IF EXISTS` was added as transient code and not needed anymore. See the ticket #120 for more information on this. --- scripts-available/CDB_UserTables.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts-available/CDB_UserTables.sql b/scripts-available/CDB_UserTables.sql index bfa5165..7d65965 100644 --- a/scripts-available/CDB_UserTables.sql +++ b/scripts-available/CDB_UserTables.sql @@ -5,7 +5,6 @@ -- -- 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 2269dc0cb5de1e7f4024f1dc8119715957e8f829 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Mon, 31 Aug 2015 12:56:12 +0200 Subject: [PATCH 06/22] Add explanation in NEWS file #120 as suggested in PR. --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index 5406364..332c72b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,7 @@ +next (2015-??-??) +----------------- +* Removed `DROP FUNCTION IF EXISTS cdb_usertables(text);` [#129](https://github.com/CartoDB/cartodb-postgresql/pull/129). This was needed for upgrading between 0.7.4 to 0.8.0 but is no longer needed. + 0.9.4 (2015-08-28) ------------------ * Fixed issue with indices when renaming tables [#123](https://github.com/CartoDB/cartodb-postgresql/issues/123) From eb6fc4fefb5e6b95467aa83d0b831d9921e9c9f9 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 2 Sep 2015 12:01:43 +0200 Subject: [PATCH 07/22] Use CDB_ColumnType and CDB_ColumnNames in bash tests --- test/extension/run_at_cartodb_schema.sql | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/extension/run_at_cartodb_schema.sql b/test/extension/run_at_cartodb_schema.sql index c3792ff..0c09e5f 100644 --- a/test/extension/run_at_cartodb_schema.sql +++ b/test/extension/run_at_cartodb_schema.sql @@ -1,4 +1,6 @@ SET SCHEMA 'cartodb'; \i scripts-available/CDB_Quota.sql \i scripts-available/CDB_TableMetadata.sql +\i scripts-available/CDB_ColumnNames.sql +\i scripts-available/CDB_ColumnType.sql SET SCHEMA 'public'; \ No newline at end of file From 4b5c5dd275ed36b912adb275ed632eb62e747777 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 2 Sep 2015 12:04:52 +0200 Subject: [PATCH 08/22] CDB_ColumnNames uses schema and table name from regclass Fixes #122 --- scripts-available/CDB_ColumnNames.sql | 11 ++++++----- test/extension/test.sh | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/scripts-available/CDB_ColumnNames.sql b/scripts-available/CDB_ColumnNames.sql index 693ba3d..51e0244 100644 --- a/scripts-available/CDB_ColumnNames.sql +++ b/scripts-available/CDB_ColumnNames.sql @@ -3,11 +3,12 @@ CREATE OR REPLACE FUNCTION CDB_ColumnNames(REGCLASS) RETURNS SETOF information_schema.sql_identifier AS $$ - SELECT column_name - FROM information_schema.columns - WHERE - table_name IN (SELECT CDB_UserTables()) - AND table_name = '' || $1 || ''; + SELECT c.column_name + FROM information_schema.columns c, pg_class _tn, pg_namespace _sn + WHERE table_name = _tn.relname + AND table_schema = _sn.nspname + AND _tn.oid = $1::regclass::oid + AND _sn.oid = _tn.relnamespace; $$ LANGUAGE SQL; diff --git a/test/extension/test.sh b/test/extension/test.sh index ff9de57..f381232 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -337,6 +337,20 @@ function test_cdb_tablemetadatatouch_fails_from_user_without_permission() { sql postgres "REVOKE ALL ON CDB_TableMetadata FROM cdb_testmember_1;" } +function test_cdb_column_names() { + sql cdb_testmember_1 'CREATE TABLE cdb_testmember_1.table_cnames(c int, a int, r int, t int, o int);' + sql cdb_testmember_2 'CREATE TABLE cdb_testmember_2.table_cnames(d int, b int);' + + sql cdb_testmember_1 "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('table_cnames') c) as s" should "carto" + sql cdb_testmember_2 "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('table_cnames') c) as s" should "db" + + sql postgres "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('cdb_testmember_1.table_cnames'::regclass) c) as s" should "carto" + sql postgres "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('cdb_testmember_2.table_cnames') c) as s" should "db" + + sql cdb_testmember_1 'DROP TABLE cdb_testmember_1.table_cnames' + sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.table_cnames' +} + #################################################### TESTS END HERE #################################################### run_tests $@ From 7582f2cbc59d01bd975a37bd220d5030327759a4 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 2 Sep 2015 12:06:04 +0200 Subject: [PATCH 09/22] CDB_ColumnType uses schema and table from regclass Fixes #130 --- scripts-available/CDB_ColumnType.sql | 13 +++++++------ test/extension/test.sh | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/scripts-available/CDB_ColumnType.sql b/scripts-available/CDB_ColumnType.sql index f887771..7793c0b 100644 --- a/scripts-available/CDB_ColumnType.sql +++ b/scripts-available/CDB_ColumnType.sql @@ -3,12 +3,13 @@ CREATE OR REPLACE FUNCTION CDB_ColumnType(REGCLASS, TEXT) RETURNS information_schema.character_data AS $$ - SELECT data_type - FROM information_schema.columns - WHERE - table_name IN (SELECT CDB_UserTables()) - AND table_name = '' || $1 || '' - AND column_name = '' || quote_ident($2) || ''; + SELECT c.data_type + FROM information_schema.columns c, pg_class _tn, pg_namespace _sn + WHERE table_name = _tn.relname + AND table_schema = _sn.nspname + AND column_name = $2 + AND _tn.oid = $1::regclass::oid + AND _sn.oid = _tn.relnamespace; $$ LANGUAGE SQL; diff --git a/test/extension/test.sh b/test/extension/test.sh index f381232..f9ed2ec 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -351,6 +351,20 @@ function test_cdb_column_names() { sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.table_cnames' } +function test_cdb_column_type() { + sql cdb_testmember_1 'CREATE TABLE cdb_testmember_1.table_ctype(c int, a int, r int, t int, o int);' + sql cdb_testmember_2 'CREATE TABLE cdb_testmember_2.table_ctype(c text, a text, r text, t text, o text);' + + sql cdb_testmember_1 "SELECT cartodb.CDB_ColumnType('table_ctype', 'c')" should "integer" + sql cdb_testmember_2 "SELECT cartodb.CDB_ColumnType('table_ctype', 'c')" should "text" + + sql postgres "SELECT cartodb.CDB_ColumnType('cdb_testmember_1.table_ctype', 'c')" should "integer" + sql postgres "SELECT cartodb.CDB_ColumnType('cdb_testmember_2.table_ctype', 'c')" should "text" + + sql cdb_testmember_1 'DROP TABLE cdb_testmember_1.table_ctype' + sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.table_ctype' +} + #################################################### TESTS END HERE #################################################### run_tests $@ From afecef0e31569f044e5f0df594a5f7c51b52032b Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 2 Sep 2015 12:25:44 +0200 Subject: [PATCH 10/22] Removing redundant ::regclass casting --- scripts-available/CDB_ColumnNames.sql | 2 +- scripts-available/CDB_ColumnType.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts-available/CDB_ColumnNames.sql b/scripts-available/CDB_ColumnNames.sql index 51e0244..ebaf0b7 100644 --- a/scripts-available/CDB_ColumnNames.sql +++ b/scripts-available/CDB_ColumnNames.sql @@ -7,7 +7,7 @@ AS $$ FROM information_schema.columns c, pg_class _tn, pg_namespace _sn WHERE table_name = _tn.relname AND table_schema = _sn.nspname - AND _tn.oid = $1::regclass::oid + AND _tn.oid = $1::oid AND _sn.oid = _tn.relnamespace; $$ LANGUAGE SQL; diff --git a/scripts-available/CDB_ColumnType.sql b/scripts-available/CDB_ColumnType.sql index 7793c0b..8ddc36b 100644 --- a/scripts-available/CDB_ColumnType.sql +++ b/scripts-available/CDB_ColumnType.sql @@ -8,7 +8,7 @@ AS $$ WHERE table_name = _tn.relname AND table_schema = _sn.nspname AND column_name = $2 - AND _tn.oid = $1::regclass::oid + AND _tn.oid = $1::oid AND _sn.oid = _tn.relnamespace; $$ LANGUAGE SQL; From 2867a6fbad59c3e339a30c4ac74c0c9ea3eb3918 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 2 Sep 2015 12:32:34 +0200 Subject: [PATCH 11/22] Assert user can use its schema to retrieve column names --- test/extension/test.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/extension/test.sh b/test/extension/test.sh index f9ed2ec..0a59630 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -347,6 +347,9 @@ function test_cdb_column_names() { sql postgres "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('cdb_testmember_1.table_cnames'::regclass) c) as s" should "carto" sql postgres "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('cdb_testmember_2.table_cnames') c) as s" should "db" + # Using schema from owner + sql cdb_testmember_1 "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('cdb_testmember_1.table_cnames') c) as s" should "carto" + sql cdb_testmember_1 'DROP TABLE cdb_testmember_1.table_cnames' sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.table_cnames' } From e28b6344aa272f0d7940f5054525098a7794231c Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Wed, 2 Sep 2015 12:32:43 +0200 Subject: [PATCH 12/22] Assert it's not possible to get column names from a table without permissions --- test/extension/test.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/extension/test.sh b/test/extension/test.sh index 0a59630..d86a687 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -350,6 +350,9 @@ function test_cdb_column_names() { # Using schema from owner sql cdb_testmember_1 "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('cdb_testmember_1.table_cnames') c) as s" should "carto" + ## it's not possible to get column names from a table where you don't have permissions + sql cdb_testmember_2 "SELECT string_agg(c,'') from (SELECT cartodb.CDB_ColumnNames('cdb_testmember_1.table_cnames') c) as s" fails + sql cdb_testmember_1 'DROP TABLE cdb_testmember_1.table_cnames' sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.table_cnames' } From 07280321ab4f99a1314550806c1c3f9879721cda Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Wed, 2 Sep 2015 22:19:07 -0400 Subject: [PATCH 13/22] adding tests --- test/CDB_StatsTest.sql | 17 +++++++++++++++++ test/CDB_StatsTest_expect | 1 + 2 files changed, 18 insertions(+) create mode 100644 test/CDB_StatsTest.sql create mode 100644 test/CDB_StatsTest_expect diff --git a/test/CDB_StatsTest.sql b/test/CDB_StatsTest.sql new file mode 100644 index 0000000..2bcbf69 --- /dev/null +++ b/test/CDB_StatsTest.sql @@ -0,0 +1,17 @@ +-- continuous uniform distribution has kurtosis = -6/5, skewness = 0.0 +-- http://mathworld.wolfram.com/UniformDistribution.html + +With dist As ( + SELECT random() As val + FROM generate_series(1,5000000) t +), +m As ( + SELECT avg(val) mn, count(*) cnt, stddev(val) s + FROM dist + ) + +SELECT + abs(sum(power(mn - val,4)) / ( cnt * power(s,4)) - 3 + 1.20) < 1e-3 As kurtosis, + abs(sum(power(mn - val,3)) / ( cnt * power(s,3))) < 1e-3 As skewness +FROM dist, m +GROUP BY m.cnt, m.mn, m.s \ No newline at end of file diff --git a/test/CDB_StatsTest_expect b/test/CDB_StatsTest_expect new file mode 100644 index 0000000..c1a8116 --- /dev/null +++ b/test/CDB_StatsTest_expect @@ -0,0 +1 @@ +true|true From d00e71309d62f4f8353e4bcba73e2c2f7e8d33e5 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Wed, 2 Sep 2015 22:35:03 -0400 Subject: [PATCH 14/22] really add tests --- test/CDB_StatsTest.sql | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/test/CDB_StatsTest.sql b/test/CDB_StatsTest.sql index 2bcbf69..5bfb5dc 100644 --- a/test/CDB_StatsTest.sql +++ b/test/CDB_StatsTest.sql @@ -2,16 +2,12 @@ -- http://mathworld.wolfram.com/UniformDistribution.html With dist As ( - SELECT random() As val + SELECT random()::numeric As val FROM generate_series(1,5000000) t -), -m As ( - SELECT avg(val) mn, count(*) cnt, stddev(val) s - FROM dist - ) +) SELECT - abs(sum(power(mn - val,4)) / ( cnt * power(s,4)) - 3 + 1.20) < 1e-3 As kurtosis, - abs(sum(power(mn - val,3)) / ( cnt * power(s,3))) < 1e-3 As skewness -FROM dist, m -GROUP BY m.cnt, m.mn, m.s \ No newline at end of file + -- does random dist values match within 1% of known values + abs(CDB_Kurtosis(array_agg(val)) + 1.20) < 1e-2 As kurtosis, + abs(CDB_Skewness(array_agg(val)) - 0) < 1e-2 As skewness +FROM dist \ No newline at end of file From 350c76f847caff3dd557e07b46ae6402f8d363ee Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 3 Sep 2015 12:59:20 +0200 Subject: [PATCH 15/22] Add option to run tests by prefix `bash test/extension/test.sh test_cdb_querytables` will run all tests that start with test_cdb_querytables --- test/extension/test.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/extension/test.sh b/test/extension/test.sh index ff9de57..ceeb73b 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -227,7 +227,12 @@ function run_tests() { local TESTS if [[ $# -ge 1 ]] then - TESTS="$@" + if [[ $# -eq 1 ]] + then + TESTS=`cat $0 | grep -o "$1[^\(]*"` + else + TESTS="$@" + fi else TESTS=`cat $0 | perl -n -e'/function (test.*)\(\)/ && print "$1\n"'` fi From 4be7d4a497e8d1ebabc453a2e94e55e8ce20ea99 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Thu, 3 Sep 2015 13:12:29 +0200 Subject: [PATCH 16/22] Use quote_ident to quote schema and table names when necessary Fixes #133 --- scripts-available/CDB_QueryTables.sql | 4 +- test/extension/test.sh | 53 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/scripts-available/CDB_QueryTables.sql b/scripts-available/CDB_QueryTables.sql index ac61281..c7cfa64 100644 --- a/scripts-available/CDB_QueryTables.sql +++ b/scripts-available/CDB_QueryTables.sql @@ -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) as p, unnest(s) as sc from inp + SELECT unnest(x)::text as p, unnest(s)::text as sc from inp LOOP -- RAISE DEBUG 'tab: %', rec2.p; -- RAISE DEBUG 'sc: %', rec2.sc; - tables := array_append(tables, (rec2.sc || '.' || rec2.p)); + tables := array_append(tables, format('%s.%s', quote_ident(rec2.sc), quote_ident(rec2.p))); END LOOP; -- RAISE DEBUG 'Tables: %', tables; diff --git a/test/extension/test.sh b/test/extension/test.sh index ceeb73b..0eb4977 100755 --- a/test/extension/test.sh +++ b/test/extension/test.sh @@ -178,6 +178,7 @@ function setup() { sql "CREATE SCHEMA cartodb;" sql "GRANT USAGE ON SCHEMA cartodb TO public;" sql "CREATE EXTENSION postgis;" + sql "CREATE EXTENSION plpythonu;" log_info "########################### BOOTSTRAP ###########################" ${CMD} -d ${DATABASE} -f scripts-available/CDB_Organizations.sql @@ -342,6 +343,58 @@ function test_cdb_tablemetadatatouch_fails_from_user_without_permission() { sql postgres "REVOKE ALL ON CDB_TableMetadata FROM cdb_testmember_1;" } +function test_cdb_querytables_schema_and_table_names_with_dots() { + ${CMD} -d ${DATABASE} -f scripts-available/CDB_QueryStatements.sql + ${CMD} -d ${DATABASE} -f scripts-available/CDB_QueryTables.sql + + sql postgres 'CREATE SCHEMA "foo.bar";' + sql postgres 'CREATE TABLE "foo.bar"."c.a.r.t.o.d.b" (a int);' + sql postgres 'INSERT INTO "foo.bar"."c.a.r.t.o.d.b" values (1);' + sql postgres 'SELECT a FROM "foo.bar"."c.a.r.t.o.d.b";' should 1 + + sql postgres 'SELECT CDB_QueryTablesText($q$select * from "foo.bar"."c.a.r.t.o.d.b"$q$);' should '{"\"foo.bar\".\"c.a.r.t.o.d.b\""}' + sql postgres 'SELECT CDB_QueryTables($q$select * from "foo.bar"."c.a.r.t.o.d.b"$q$);' should '{"\"foo.bar\".\"c.a.r.t.o.d.b\""}' + + sql postgres 'DROP TABLE "foo.bar"."c.a.r.t.o.d.b";' + sql postgres 'DROP SCHEMA "foo.bar";' +} + +function test_cdb_querytables_table_name_with_dots() { + ${CMD} -d ${DATABASE} -f scripts-available/CDB_QueryStatements.sql + ${CMD} -d ${DATABASE} -f scripts-available/CDB_QueryTables.sql + + sql postgres 'CREATE TABLE "w.a.d.u.s" (a int);'; + + sql postgres 'SELECT CDB_QueryTablesText($q$select * from "w.a.d.u.s"$q$);' should '{"public.\"w.a.d.u.s\""}' + sql postgres 'SELECT CDB_QueryTables($q$select * from "w.a.d.u.s"$q$);' should '{"public.\"w.a.d.u.s\""}' + + sql postgres 'DROP TABLE "w.a.d.u.s";'; +} + +function test_cdb_querytables_happy_cases() { + ${CMD} -d ${DATABASE} -f scripts-available/CDB_QueryStatements.sql + ${CMD} -d ${DATABASE} -f scripts-available/CDB_QueryTables.sql + + sql postgres 'CREATE TABLE wadus (a int);'; + sql postgres 'CREATE TABLE "FOOBAR" (a int);'; + sql postgres 'CREATE SCHEMA foo;' + sql postgres 'CREATE TABLE foo.wadus (a int);'; + + ## See how it does NOT quote anything here + sql postgres 'SELECT CDB_QueryTablesText($q$select * from wadus$q$);' should '{public.wadus}' + sql postgres 'SELECT CDB_QueryTablesText($q$select * from foo.wadus$q$);' should '{foo.wadus}' + sql postgres 'SELECT CDB_QueryTables($q$select * from wadus$q$);' should '{public.wadus}' + sql postgres 'SELECT CDB_QueryTables($q$select * from foo.wadus$q$);' should '{foo.wadus}' + + ## But it quotes when it's needed even if table name has no dots but was created with quotes + sql postgres 'SELECT CDB_QueryTablesText($q$select * from "FOOBAR"$q$);' should '{"public.\"FOOBAR\""}' + + sql postgres 'DROP TABLE wadus;' + sql postgres 'DROP TABLE "FOOBAR";' + sql postgres 'DROP TABLE foo.wadus;' + sql postgres 'DROP SCHEMA foo;' +} + #################################################### TESTS END HERE #################################################### run_tests $@ From 29efdf2ee726bdbc7c8a82242a80a5b452ed7a11 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 4 Sep 2015 00:00:30 +0200 Subject: [PATCH 17/22] Fix symlink for CDB_Stats.sql --- scripts-enabled/CDB_Stats.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts-enabled/CDB_Stats.sql b/scripts-enabled/CDB_Stats.sql index 0d7fa50..37abd7b 120000 --- a/scripts-enabled/CDB_Stats.sql +++ b/scripts-enabled/CDB_Stats.sql @@ -1 +1 @@ -scripts-available/CDB_Stats.sql \ No newline at end of file +../scripts-available/CDB_Stats.sql \ No newline at end of file From 25cf48d4a491bdd8b6a6fe14676c4822d71a56ae Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Fri, 4 Sep 2015 00:02:30 +0200 Subject: [PATCH 18/22] Raise min message so we don't have to validate notices --- test/CDB_StatsTest.sql | 5 ++++- test/CDB_StatsTest_expect | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/test/CDB_StatsTest.sql b/test/CDB_StatsTest.sql index 5bfb5dc..9cb6bc2 100644 --- a/test/CDB_StatsTest.sql +++ b/test/CDB_StatsTest.sql @@ -1,5 +1,6 @@ -- continuous uniform distribution has kurtosis = -6/5, skewness = 0.0 -- http://mathworld.wolfram.com/UniformDistribution.html +set client_min_messages to ERROR; With dist As ( SELECT random()::numeric As val @@ -10,4 +11,6 @@ SELECT -- does random dist values match within 1% of known values abs(CDB_Kurtosis(array_agg(val)) + 1.20) < 1e-2 As kurtosis, abs(CDB_Skewness(array_agg(val)) - 0) < 1e-2 As skewness -FROM dist \ No newline at end of file +FROM dist; + +set client_min_messages to NOTICE; diff --git a/test/CDB_StatsTest_expect b/test/CDB_StatsTest_expect index c1a8116..fdc125d 100644 --- a/test/CDB_StatsTest_expect +++ b/test/CDB_StatsTest_expect @@ -1 +1,3 @@ -true|true +SET +t|t +SET From 83b7f47617b900d62cf21281c3f12cc7a422ea56 Mon Sep 17 00:00:00 2001 From: Andy Eschbacher Date: Thu, 3 Sep 2015 22:43:25 -0400 Subject: [PATCH 19/22] removing raise notices and lower test bounds --- scripts-available/CDB_Stats.sql | 4 ---- test/CDB_StatsTest.sql | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/scripts-available/CDB_Stats.sql b/scripts-available/CDB_Stats.sql index 543b3ee..e16748c 100644 --- a/scripts-available/CDB_Stats.sql +++ b/scripts-available/CDB_Stats.sql @@ -18,8 +18,6 @@ DECLARE BEGIN SELECT AVG(e), COUNT(e)::numeric, stddev(e) INTO a, c, s FROM ( SELECT unnest(in_array) e ) x; - RAISE NOTICE 'avg: %, cnt: %, std: %', a, c, s; - EXECUTE 'SELECT sum(power($1 - e, 4)) / ( $2 * power($3, 4)) - 3 FROM (SELECT unnest($4) e ) x' INTO k @@ -39,8 +37,6 @@ DECLARE BEGIN SELECT AVG(e), COUNT(e)::numeric, stddev(e) INTO a, c, s FROM ( SELECT unnest(in_array) e ) x; - RAISE NOTICE 'avg: %, cnt: %, std: %', a, c, s; - EXECUTE 'SELECT sum(power($1 - e, 3)) / ( $2 * power($3, 3)) FROM (SELECT unnest($4) e ) x' INTO sk diff --git a/test/CDB_StatsTest.sql b/test/CDB_StatsTest.sql index 5bfb5dc..06ef647 100644 --- a/test/CDB_StatsTest.sql +++ b/test/CDB_StatsTest.sql @@ -3,7 +3,7 @@ With dist As ( SELECT random()::numeric As val - FROM generate_series(1,5000000) t + FROM generate_series(1,50000) t ) SELECT From 03f42e36c9e6e134cca720ecb5eebf095cec4ba8 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 7 Sep 2015 13:02:05 +0200 Subject: [PATCH 20/22] Update news and bump version --- Makefile | 3 ++- NEWS.md | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index a125400..6a803ba 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # cartodb/Makefile EXTENSION = cartodb -EXTVERSION = 0.9.4 +EXTVERSION = 0.10.0 SED = sed @@ -47,6 +47,7 @@ UPGRADABLE = \ 0.9.2 \ 0.9.3 \ 0.9.4 \ + 0.10.0 \ $(EXTVERSION)dev \ $(EXTVERSION)next \ $(END) diff --git a/NEWS.md b/NEWS.md index 332c72b..7e70c9e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ -next (2015-??-??) +0.10.0 (2015-??-??) ----------------- +* Quote schema and table names returned by CDB_QueryTables [#134](https://github.com/CartoDB/cartodb-postgresql/pull/134). Use quote_ident to quote schema and table names when necessary. +* Fixed CDB_ColumnNames [#122](https://github.com/CartoDB/cartodb-postgresql/issues/122) and CDB_ColumnType [#130](https://github.com/CartoDB/cartodb-postgresql/issues/130) should honor regclass, returning columns for just the table in the schema and not in any other one [#131](https://github.com/CartoDB/cartodb-postgresql/pull/131). +* Add kurtosis and skewness [#124](https://github.com/CartoDB/cartodb-postgresql/pull/124). * Removed `DROP FUNCTION IF EXISTS cdb_usertables(text);` [#129](https://github.com/CartoDB/cartodb-postgresql/pull/129). This was needed for upgrading between 0.7.4 to 0.8.0 but is no longer needed. 0.9.4 (2015-08-28) From c6f29032211c74958eb151dcdde256fc66c24a4a Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 7 Sep 2015 13:16:27 +0200 Subject: [PATCH 21/22] Release 0.10.0 --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 7e70c9e..11f4244 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -0.10.0 (2015-??-??) +0.10.0 (2015-09-07) ----------------- * Quote schema and table names returned by CDB_QueryTables [#134](https://github.com/CartoDB/cartodb-postgresql/pull/134). Use quote_ident to quote schema and table names when necessary. * Fixed CDB_ColumnNames [#122](https://github.com/CartoDB/cartodb-postgresql/issues/122) and CDB_ColumnType [#130](https://github.com/CartoDB/cartodb-postgresql/issues/130) should honor regclass, returning columns for just the table in the schema and not in any other one [#131](https://github.com/CartoDB/cartodb-postgresql/pull/131). From 0ec579984b1c579f39ac172e3cf0ae4289b25845 Mon Sep 17 00:00:00 2001 From: Raul Ochoa Date: Mon, 7 Sep 2015 13:17:22 +0200 Subject: [PATCH 22/22] Stubs next version --- NEWS.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/NEWS.md b/NEWS.md index 11f4244..c69d430 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +next (2015-mm-dd) +----------------- + 0.10.0 (2015-09-07) ----------------- * Quote schema and table names returned by CDB_QueryTables [#134](https://github.com/CartoDB/cartodb-postgresql/pull/134). Use quote_ident to quote schema and table names when necessary.