-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to normalize only utility statements
- Loading branch information
1 parent
c3ed78b
commit c492b8f
Showing
7 changed files
with
148 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <pg_query.h> | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <stdbool.h> | ||
|
||
#include "normalize_utility_tests.c" | ||
|
||
int main() { | ||
size_t i; | ||
bool ret_code = 0; | ||
|
||
for (i = 0; i < testsLength; i += 2) { | ||
PgQueryNormalizeResult result = pg_query_normalize_utility(tests[i]); | ||
|
||
if (result.error) { | ||
ret_code = -1; | ||
printf("%s\n", result.error->message); | ||
} else if (strcmp(result.normalized_query, tests[i + 1]) == 0) { | ||
printf("."); | ||
} else { | ||
ret_code = -1; | ||
printf("INVALID result for \"%s\"\nexpected: %s\nactual: %s\n", tests[i], tests[i + 1], result.normalized_query); | ||
} | ||
|
||
pg_query_free_normalize_result(result); | ||
} | ||
|
||
printf("\n"); | ||
|
||
pg_query_exit(); | ||
|
||
return ret_code; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// This contains all the same tests as normalize_tests.c, but only expecting that | ||
// utility statments are normalized. | ||
const char* tests[] = { | ||
"SELECT 1", | ||
"SELECT 1", | ||
"SELECT $1, 1", | ||
"SELECT $1, 1", | ||
"CREATE ROLE postgres PASSWORD 'xyz'", | ||
"CREATE ROLE postgres PASSWORD $1", | ||
"CREATE ROLE postgres ENCRYPTED PASSWORD 'xyz'", | ||
"CREATE ROLE postgres ENCRYPTED PASSWORD $1", | ||
"ALTER ROLE foo WITH PASSWORD 'bar' VALID UNTIL 'infinity'", | ||
"ALTER ROLE foo WITH PASSWORD $1 VALID UNTIL $2", | ||
"ALTER ROLE postgres LOGIN SUPERUSER ENCRYPTED PASSWORD 'xyz'", | ||
"ALTER ROLE postgres LOGIN SUPERUSER ENCRYPTED PASSWORD $1", | ||
"SELECT a, SUM(b) FROM tbl WHERE c = 'foo' GROUP BY 1, 'bar' ORDER BY 1, 'cafe'", | ||
"SELECT a, SUM(b) FROM tbl WHERE c = 'foo' GROUP BY 1, 'bar' ORDER BY 1, 'cafe'", | ||
"select date_trunc($1, created_at at time zone $2), count(*) from users group by date_trunc('day', created_at at time zone 'US/Pacific')", | ||
"select date_trunc($1, created_at at time zone $2), count(*) from users group by date_trunc('day', created_at at time zone 'US/Pacific')", | ||
"select count(1), date_trunc('day', created_at at time zone 'US/Pacific'), 'something', 'somethingelse' from users group by date_trunc('day', created_at at time zone 'US/Pacific'), date_trunc('day', created_at), 'foobar', 'abcdef'", | ||
"select count(1), date_trunc('day', created_at at time zone 'US/Pacific'), 'something', 'somethingelse' from users group by date_trunc('day', created_at at time zone 'US/Pacific'), date_trunc('day', created_at), 'foobar', 'abcdef'", | ||
"SELECT CAST('abc' as varchar(50))", | ||
"SELECT CAST('abc' as varchar(50))", | ||
"CREATE OR REPLACE FUNCTION pg_temp.testfunc(OUT response \"mytable\", OUT sequelize_caught_exception text) RETURNS RECORD AS $func_12345$ BEGIN INSERT INTO \"mytable\" (\"mycolumn\") VALUES ('myvalue') RETURNING * INTO response; EXCEPTION WHEN unique_violation THEN GET STACKED DIAGNOSTICS sequelize_caught_exception = PG_EXCEPTION_DETAIL; END $func_12345$ LANGUAGE plpgsql; SELECT (testfunc.response).\"mycolumn\", testfunc.sequelize_caught_exception FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc();", | ||
"CREATE OR REPLACE FUNCTION pg_temp.testfunc(OUT response \"mytable\", OUT sequelize_caught_exception text) RETURNS RECORD AS $func_12345$ BEGIN INSERT INTO \"mytable\" (\"mycolumn\") VALUES ('myvalue') RETURNING * INTO response; EXCEPTION WHEN unique_violation THEN GET STACKED DIAGNOSTICS sequelize_caught_exception = PG_EXCEPTION_DETAIL; END $func_12345$ LANGUAGE plpgsql; SELECT (testfunc.response).\"mycolumn\", testfunc.sequelize_caught_exception FROM pg_temp.testfunc(); DROP FUNCTION IF EXISTS pg_temp.testfunc();", | ||
"CREATE PROCEDURE insert_data(a integer, b integer) LANGUAGE SQL AS $$ INSERT INTO tbl VALUES (a); INSERT INTO tbl VALUES (b); $$", | ||
"CREATE PROCEDURE insert_data(a integer, b integer) LANGUAGE SQL AS $$ INSERT INTO tbl VALUES (a); INSERT INTO tbl VALUES (b); $$", | ||
"DO $$DECLARE r record; BEGIN FOR r IN SELECT table_schema, table_name FROM information_schema.tables WHERE table_type = 'VIEW' AND table_schema = 'public' LOOP EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser'; END LOOP; END$$", | ||
"DO $$DECLARE r record; BEGIN FOR r IN SELECT table_schema, table_name FROM information_schema.tables WHERE table_type = 'VIEW' AND table_schema = 'public' LOOP EXECUTE 'GRANT ALL ON ' || quote_ident(r.table_schema) || '.' || quote_ident(r.table_name) || ' TO webuser'; END LOOP; END$$", | ||
"CREATE SUBSCRIPTION mysub CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb' PUBLICATION mypublication, insert_only", | ||
"CREATE SUBSCRIPTION mysub CONNECTION $1 PUBLICATION mypublication, insert_only", | ||
"ALTER SUBSCRIPTION mysub SET PUBLICATION insert_only", | ||
"ALTER SUBSCRIPTION mysub SET PUBLICATION insert_only", | ||
"ALTER SUBSCRIPTION mysub CONNECTION 'host=192.168.1.50 port=5432 user=foo dbname=foodb'", | ||
"ALTER SUBSCRIPTION mysub CONNECTION $1", | ||
"CREATE USER MAPPING FOR bob SERVER foo OPTIONS (user 'bob', password 'secret')", | ||
"CREATE USER MAPPING FOR bob SERVER foo OPTIONS (user $1, password $2)", | ||
"ALTER USER MAPPING FOR bob SERVER foo OPTIONS (SET password 'public')", | ||
"ALTER USER MAPPING FOR bob SERVER foo OPTIONS (SET password $1)", | ||
"MERGE into measurement m USING new_measurement nm ON (m.city_id = nm.city_id and m.logdate=nm.logdate) WHEN MATCHED AND nm.peaktemp IS NULL THEN DELETE WHEN MATCHED THEN UPDATE SET peaktemp = greatest(m.peaktemp, nm.peaktemp), unitsales = m.unitsales + coalesce(nm.unitsales, 0) WHEN NOT MATCHED THEN INSERT (city_id, logdate, peaktemp, unitsales) VALUES (city_id, logdate, peaktemp, unitsales)", | ||
"MERGE into measurement m USING new_measurement nm ON (m.city_id = nm.city_id and m.logdate=nm.logdate) WHEN MATCHED AND nm.peaktemp IS NULL THEN DELETE WHEN MATCHED THEN UPDATE SET peaktemp = greatest(m.peaktemp, nm.peaktemp), unitsales = m.unitsales + coalesce(nm.unitsales, 0) WHEN NOT MATCHED THEN INSERT (city_id, logdate, peaktemp, unitsales) VALUES (city_id, logdate, peaktemp, unitsales)", | ||
// These below are as expected, though questionable if upstream shouldn't be | ||
// fixed as this could bloat pg_stat_statements | ||
"DECLARE cursor_b CURSOR FOR SELECT * FROM x WHERE id = 123", | ||
"DECLARE cursor_b CURSOR FOR SELECT * FROM x WHERE id = 123", | ||
"FETCH 1000 FROM cursor_a", | ||
"FETCH 1000 FROM cursor_a", | ||
"CLOSE cursor_a", | ||
"CLOSE cursor_a", | ||
"SELECT 1; ALTER USER a WITH PASSWORD 'b'", | ||
"SELECT 1; ALTER USER a WITH PASSWORD $1", | ||
}; | ||
|
||
size_t testsLength = __LINE__ - 8; |