Compare commits

..

13 Commits

Author SHA1 Message Date
Javier Torres
51a669f93c Release 0.23.2 2018-07-20 14:15:05 +02:00
Javier Torres
c8a1119556 Merge pull request #335 from CartoDB/s1669-sql_api_injection_query_tables
Don't rely on regexp to identify non explainable queries
2018-07-20 14:12:59 +02:00
Javier Torres
24a37be1a9 Change querytables expect test 2018-07-20 13:22:02 +02:00
Javier Torres
5659275c0c Don't rely on regexp to identify non explainable queries 2018-07-20 13:01:51 +02:00
Javier Goizueta
7760d6b30d Release 0.23.1 2018-07-19 17:11:56 +02:00
Javier Goizueta
4515c8547e Merge pull request #334 from CartoDB/333-parallel-unsafe
Fix PARALLEL tags
2018-07-19 16:57:22 +02:00
Javier Goizueta
2766bbc83a Fix PARALLEL tags
Fixes #333
2018-07-18 12:13:31 +02:00
Rafa de la Torre
c4980a90f9 Merge pull request #332 from CartoDB/cdb-table-exists
Cdb table exists
2018-07-03 15:45:17 +02:00
Rafa de la Torre
61d2024eb5 Make the code nicer by avoiding IF/THEN/ELSE
As suggested by Algunenano.
2018-07-03 15:35:05 +02:00
Rafa de la Torre
af142306aa Mark _CDB_Table_Exists() as PARALLEL UNSAFE
As pointed out by Algunenano, PL/pgSQL function which establishes an
EXCEPTION block to catch errors must be qualified with it.
2018-07-03 15:26:48 +02:00
Rafa de la Torre
7437a9686b Use a more suitable version number 0.23.0 & NEWS
Use a better version number (as it adds a new function) and update
NEWS.md accordingly.
2018-07-03 15:08:34 +02:00
Rafa de la Torre
82f90e618c Use CREATE OR REPLACE FUNCTION 2018-07-03 13:04:39 +02:00
Rafa de la Torre
55a77b0ef0 Add a new helper function _CDB_Table_Exists 2018-07-03 13:00:24 +02:00
9 changed files with 62 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
# cartodb/Makefile
EXTENSION = cartodb
EXTVERSION = 0.22.2
EXTVERSION = 0.23.2
SED = sed
AWK = awk
@@ -88,6 +88,9 @@ UPGRADABLE = \
0.22.0 \
0.22.1 \
0.22.2 \
0.23.0 \
0.23.1 \
0.23.2 \
$(EXTVERSION)dev \
$(EXTVERSION)next \
$(END)

View File

@@ -1,3 +1,12 @@
0.23.2 (2018-07-19)
* Fix `CDB_QueryTablesText` with parenthesized queries (#335)
0.23.1 (2018-07-19)
* Fix `CDB_EstimateRowCount` parallelizability #333
0.23.0 (2018-07-03)
* Add a new helper function `_CDB_Table_Exists(table_name_with_optional_schema TEXT)` #332
0.22.2 (2018-05-29)
* Fix: Fix hyphenates usernames in 0.22.1 fix (#331)

View File

@@ -12,7 +12,7 @@ BEGIN
EXECUTE Format('ANALYZE %s;', reloid);
END IF;
END
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL RESTRICTED SECURITY DEFINER;
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE SECURITY DEFINER;
-- Return a row count estimate of the result of a query using statistics
CREATE OR REPLACE FUNCTION CDB_EstimateRowCount(query text)
@@ -28,4 +28,4 @@ BEGIN
EXECUTE 'EXPLAIN (FORMAT JSON) ' || query INTO STRICT plan;
RETURN plan->0->'Plan'->'Plan Rows';
END
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL RESTRICTED;
$$ LANGUAGE 'plpgsql' VOLATILE STRICT PARALLEL UNSAFE;

View File

@@ -158,3 +158,20 @@ BEGIN
RETURN left(string, (i - 1));
END;
$$ LANGUAGE 'plpgsql' IMMUTABLE PARALLEL SAFE;
-- Checks if a given text representing a qualified or unqualified table name (relation)
-- actually exists in the database. It is meant to be used as a guard for other function/queries.
CREATE OR REPLACE FUNCTION cartodb._CDB_Table_Exists(table_name_with_optional_schema TEXT)
RETURNS bool
AS $$
DECLARE
table_exists bool := false;
BEGIN
table_exists := EXISTS(SELECT * FROM pg_class WHERE table_name_with_optional_schema::regclass::oid = oid AND relkind = 'r');
RETURN table_exists;
EXCEPTION
WHEN invalid_schema_name OR undefined_table THEN
RETURN false;
END;
$$ LANGUAGE PLPGSQL VOLATILE PARALLEL UNSAFE;

View File

@@ -11,19 +11,16 @@ DECLARE
rec RECORD;
rec2 RECORD;
BEGIN
tables := '{}';
FOR rec IN SELECT CDB_QueryStatements(query) q LOOP
IF NOT ( rec.q ilike 'select%' or rec.q ilike 'with%' ) THEN
--RAISE WARNING 'Skipping %', rec.q;
CONTINUE;
END IF;
BEGIN
EXECUTE 'EXPLAIN (FORMAT XML, VERBOSE) ' || rec.q INTO STRICT exp;
EXCEPTION WHEN others THEN
EXCEPTION WHEN syntax_error THEN
-- We can get a syntax error if the user tries to EXPLAIN a DDL
CONTINUE;
WHEN others THEN
-- TODO: if error is 'relation "xxxxxx" does not exist', take xxxxxx as
-- the affected table ?
RAISE WARNING 'CDB_QueryTables cannot explain query: % (%: %)', rec.q, SQLSTATE, SQLERRM;

View File

@@ -126,3 +126,13 @@ SELECT * FROM cartodb._CDB_Octet_Truncate('piraña', 6);
-- Test _CDB_Octet_Truncate UTF8 case
SELECT * FROM cartodb._CDB_Octet_Truncate('piraña', 7);
-- Test _CDB_Table_Exists
CREATE TABLE public.this_table_exists();
SELECT cartodb._CDB_Table_Exists('this_table_does_not_exist');
SELECT cartodb._CDB_Table_Exists('this_schema_does_not_exist.this_table_does_not_exist');
SELECT cartodb._CDB_Table_Exists('this_table_exists');
SELECT cartodb._CDB_Table_Exists('public.this_table_exists');
SELECT cartodb._CDB_Table_Exists('raster_overviews'); -- view created by postgis
SELECT cartodb._CDB_Table_Exists('public.raster_overviews');
DROP TABLE public.this_table_exists

View File

@@ -57,3 +57,11 @@ DROP TABLE
pira
pirañ
piraña
CREATE TABLE
f
f
t
t
f
f
DROP TABLE

View File

@@ -7,7 +7,7 @@ SELECT 1 as col1; select 2 as col2|{}
WARNING: CDB_QueryTables cannot explain query: select 1 from nonexistant (42P01: relation "nonexistant" does not exist)
ERROR: relation "nonexistant" does not exist
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)
WARNING: CDB_QueryTables cannot explain query: insert into test values (1) (42P01: relation "test" does not exist)
ERROR: relation "test" does not exist
WITH a AS (select * from pg_class) select * from a|{pg_catalog.pg_class}
CREATE SCHEMA

View File

@@ -426,6 +426,12 @@ function test_cdb_querytables_returns_schema_and_table_name() {
sql cdb_testmember_1 "select * from CDB_QueryTables('select * from foo');" should "{cdb_testmember_1.foo}"
}
function test_cdb_querytables_works_with_parentheses() {
load_sql_file scripts-available/CDB_QueryStatements.sql
load_sql_file scripts-available/CDB_QueryTables.sql
sql cdb_testmember_1 "select * from CDB_QueryTables('(select * from foo)');" should "{cdb_testmember_1.foo}"
}
function test_cdb_querytables_returns_schema_and_table_name_for_several_schemas() {
load_sql_file scripts-available/CDB_QueryStatements.sql
load_sql_file scripts-available/CDB_QueryTables.sql