Skip to content

Commit

Permalink
feat(Common): raw_parse test (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Kulikov committed Nov 22, 2022
1 parent b3a61b6 commit 471535a
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 23 deletions.
31 changes: 25 additions & 6 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ init: volume
docker-compose up -d
sleep 2

reset: prune init
reset: build prune init

volume:
docker volume create --name rvkulikov.pg-deps-management.pgsql.data || true

prune:
docker-compose rm -fsv
docker volume rm rvkulikov.pg-deps-management.pgsql.data || true
docker volume rm rvkulikov.pg-deps-management.pgsql.data || true

build:
docker build -f "./docker/Dockerfile" -t rvkulikov.pg-deps-management.pgsql:latest .
2 changes: 2 additions & 0 deletions ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ begin
on rwr.ev_class = rwr_cl.oid
join pg_namespace rwr_nsp
on rwr_cl.relnamespace = rwr_nsp.oid
join pg_proc ref_prc
on dep.refobjid = ref_prc.oid
where
dep.deptype = 'n' and
dep.classid = 'pg_rewrite'::regclass
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3.7'

services:
rvkulikov.pg-deps-management.pgsql:
image: postgres:13-alpine
image: rvkulikov.pg-deps-management.pgsql:latest
environment:
- PGDATA=/var/lib/postgresql/data
- POSTGRES_DB=database
Expand Down
22 changes: 22 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM postgres:13-alpine as build

RUN echo https://dl-2.alpinelinux.org/alpine/edge/community/ >> /etc/apk/repositories \
&& apk --no-cache add git make cmake libc-dev flex bison gcc postgresql-dev

COPY ./extension /tmp/annotate_query

RUN cd /tmp/annotate_query \
&& make USE_PGXS=1 \
&& make USE_PGXS=1 install

RUN echo 1 \
&& ls -la /tmp/annotate_query \
&& cat /tmp/annotate_query/annotate_query--1.0.0.sql \
&& cat /tmp/annotate_query/annotate_query--1.0.0.sql.in

FROM postgres:13-alpine as result

COPY --from=build /usr/local/share/postgresql/extension/annotate_query* /usr/local/share/postgresql/extension/
COPY --from=build /usr/local/lib/postgresql/annotate_query* /usr/local/lib/postgresql/
COPY --from=build /usr/local/lib/postgresql/bitcode/annotate_query/ /usr/local/lib/postgresql/bitcode/annotate_query/
COPY --from=build /usr/local/lib/postgresql/bitcode/annotate_query.index.bc /usr/local/lib/postgresql/bitcode/annotate_query.index.bc
7 changes: 7 additions & 0 deletions extension/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MODULES = annotate_query
EXTENSION = annotate_query
DATA_built = annotate_query--1.0.0.sql

PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
7 changes: 7 additions & 0 deletions extension/annotate_query--1.0.0.sql.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION annotate_query" to load this file. \quit

CREATE OR REPLACE FUNCTION annotate_query(query text)
RETURNS text
AS '$libdir/annotate_query'
LANGUAGE C IMMUTABLE STRICT;
27 changes: 27 additions & 0 deletions extension/annotate_query.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "parser/parser.h"
#include "nodes/print.h"

PG_MODULE_MAGIC;


PG_FUNCTION_INFO_V1(annotate_query);

Datum
annotate_query(PG_FUNCTION_ARGS)
{
text *sql_t = PG_GETARG_TEXT_P(0);
text *out_t;
char *sql, *out;
List *tree;

sql = text_to_cstring(sql_t);
tree = raw_parser(sql);

out = nodeToString(tree);

out_t = cstring_to_text(out);
PG_RETURN_TEXT_P(out_t);
}
5 changes: 5 additions & 0 deletions extension/annotate_query.control
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# annotate_query extension
comment = 'annotate_query'
default_version = '1.0.0'
module_pathname = '$libdir/annotate_query'
relocatable = true
2 changes: 2 additions & 0 deletions pgsql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
docker-compose exec -u 0 rvkulikov.pg-deps-management.pgsql sh $@
79 changes: 65 additions & 14 deletions tests/feature-10.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
--- dependency tree
--- table1
--- view1
--- view3
--- rule1
--- rule2
--- fn1
Expand Down Expand Up @@ -33,6 +34,7 @@ create table feature10._table1(
);

create view feature10._view1 as select * from feature10._table1;
create view feature10._view3 as select * from feature10._view1;

create function feature10._fn1() returns setof feature10._view1 language plpgsql as $$ begin
return query select * from feature10._view1;
Expand Down Expand Up @@ -65,18 +67,67 @@ create rule _rule2 as on insert to feature10._view1

insert into feature10._view1 values (10);

select public.deps_save_and_drop_dependencies(
'feature10',
'_table1',
'{"dry_run": true, "verbose": true, "populate_materialized_view": true}'::jsonb
);

select * from feature10._table1;

select * from public.deps_saved_ddl;

-- select
-- util.__assert(
-- (select count(ctid) from public.deps_saved_ddl where ddl_statement ~* 'create view') = 1,
-- 'There is only 1 create view statement'::text
-- );
-- obj_schema,
-- obj_name,
-- obj_type
-- from (
-- with recursive
-- deps as (
-- select
-- n.nspname ref_schema,
-- c.relname ref_name,
-- rc.relkind dep_type,
-- rn.nspname dep_schema,
-- rc.relname dep_name
-- from pg_depend dep
-- join pg_class c on dep.refobjid = c.oid
-- join pg_namespace n on c.relnamespace = n.oid
-- join pg_rewrite r on dep.objid = r.oid
-- join pg_class rc on r.ev_class = rc.oid
-- join pg_namespace rn on rc.relnamespace = rn.oid
-- ),
-- recursive_deps(obj_schema, obj_name, obj_type, depth) as (
-- select
-- 'feature10'::name,
-- '_table1'::name,
-- null::char,
-- 0
-- union
-- select
-- dep_schema::name,
-- dep_name::name,
-- dep_type::char,
-- recursive_deps.depth + 1
-- from deps
-- join recursive_deps
-- on deps.ref_schema = recursive_deps.obj_schema and
-- deps.ref_name = recursive_deps.obj_name
-- where
-- deps.ref_schema != deps.dep_schema or
-- deps.ref_name != deps.dep_name
-- )
-- select
-- obj_schema,
-- obj_name,
-- obj_type,
-- depth
-- from
-- recursive_deps
-- where
-- depth > 0
-- ) t
-- group by
-- obj_schema,
-- obj_name,
-- obj_type
-- order by
-- max(depth) desc;

create function feature10._fn3(routine_namespace name, routine_name name) returns table (plan jsonb) language plpgsql as $$ begin
return execute 'explain (format json) select true';
end; $$;

explain do $$ begin
execute 'explain (format json) select true';
end $$;

0 comments on commit 471535a

Please sign in to comment.