-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsicob_splitsql.sql
More file actions
175 lines (78 loc) · 3.8 KB
/
Copy pathsicob_splitsql.sql
File metadata and controls
175 lines (78 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
SET CLIENT_ENCODING TO 'utf8';
CREATE OR REPLACE FUNCTION public.sicob_splitsql(_query text, _opt json)
RETURNS text[]
LANGUAGE plpgsql
AS $function$
DECLARE
_key text;
_table_to_chunk text;
_filter text;
_txt_chunk_condition text;
_txt_chunk_id text;
_partitions int;
sql text;
r record;
condition text;
listSQL text[];
BEGIN
---------------------------
--PARAMETROS DE ENTRADA
---------------------------
--> query: Consulta SQL para ser particionada. Debe incluir en su texto para la
-- sentencia WHERE la cadena a ser reemplazadas por la condicion logica que
-- separa el bloque que por defecto es "<chunk_condition>" y si es necesario
-- incluir la cadena de identificacion del bloque <chunk_id> donde se requiera
-- diferenciar cada consulta. Por ejemplo:
-- 'SELECT sicob_obtener_predio(''{"lyr_in":"processed.f20170718fagebdcf580ac83_nsi",
-- "condition":"<chunk_condition>","subfix":"_p<chunk_id>","tolerance":"5.3"}'')'
--> _opt: JSON conteniendo los siguientes parametros de opciones:
-- - table_to_chunk: Nombre de la tabla referenciada en la consulta de "query"
-- sobre la cual se aplicara el criterio de particion.
-- - filter (opcional): Condición preestablecida para incluir el WHERE de la
-- tabla a ser particionada.
-- - partitions: cantidad de bloques en la que se divide la tabla a particionar.
-- - txt_chunk_condition (opcional): Cadena a ser reemplazadas por la condicion
-- logica que separa el bloque. Por defecto es "<chunk_condition>".
-- - txt_chunk_id (opcional): Cadena a ser reemplazada con el identificador
-- del bloque. Por defecto es "<chunk_id>".
---------------------------
--VALORES DEVUELTOS
---------------------------
-- Devuelve un array de texto conteniendo las consultas SQL resultantes de la
-- particion.
--_query := 'SELECT sicob_obtener_predio(''{"lyr_in":"processed.f20170718fagebdcf580ac83_nsi","condition":"<chunk_condition>","subfix":"_p<chunk_id>","tolerance":"5.3"}'')';
--_opt := '{"table_to_chunk":"processed.f20170718fagebdcf580ac83_nsi", "partitions":"10"}'::json;
_table_to_chunk := COALESCE( (_opt->>'table_to_chunk')::text, '');
IF _table_to_chunk = '' THEN
SELECT array_append(listSQL, _query::text) INTO listSQL;
RETURN listSQL;
END IF;
_key := COALESCE( (_opt->>'key')::text, 'sicob_id');
_filter := COALESCE( (_opt->>'filter')::text, 'TRUE');
_txt_chunk_condition := COALESCE( (_opt->>'txt_chunk_condition')::text, '<chunk_condition>');
_txt_chunk_id := COALESCE( (_opt->>'txt_chunk_id')::text, '<chunk_id>');
_partitions := COALESCE( (_opt->>'partitions')::int, 1);
-- loop through chunks
FOR r IN
SELECT * FROM sicob_chunk_info(('{"lyr_in":"' || _table_to_chunk || '","condition":"' || _filter || '","num_chunks":"' || _partitions::text || '"}')::json)
LOOP
condition := _filter || ' AND (a.sicob_id ' || CASE WHEN r.chunk_id = 1 OR r.start_id = r.end_id THEN '>=' ELSE '>=' END || ' ' || r.start_id || ' AND a.sicob_id <= ' || r.end_id || ')';
SELECT
REPLACE(
REPLACE(
_query::text,
_txt_chunk_condition::text,
condition::text
),
_txt_chunk_id::text,
r.chunk_id::text
) as ready_query INTO sql;
SELECT array_append(listSQL, sql::text) INTO listSQL;
END LOOP;
RETURN listSQL;
-- error catching to disconnect dblink connections, if error occurs
EXCEPTION WHEN others THEN
RAISE EXCEPTION 'sicob_splitSQL: % (%)', SQLERRM, SQLSTATE;
END;
$function$