Loading...
Searching...
No Matches
mpe_derived_postedit.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Post Edit Hook script for the MPE_DERIVED table
4 @details Post edit hooks provide additional backend validation for user
5 provided data. The incoming dataset is named `work.staging_ds` and is
6 provided in mpe_loader.sas.
7
8 Available macro variables:
9 @li DC_LIBREF - The DC control library
10 @li LIBREF - The library of the dataset being edited (is assigned)
11 @li DS - The dataset being edited
12
13 This hook normalises the incoming rows and applies the configuration rules
14 at submit time, so a bad derived variable is rejected while the submitter is
15 still watching - rather than breaking the EDIT screen later, when the view
16 is built.
17
18 VAR_DEPS must list every input the expression uses, physical columns
19 and derived variables alike. The hook checks the part that does not need
20 the target library: a derived variable referenced by an expression must be
21 declared, a variable may not depend on itself, and the resulting graph must
22 be acyclic - across the incoming rows and the current rows they do not
23 replace.
24
25 The target table is not assigned in this context, so the collision between
26 VAR_NM and a physical column, and the resolution of a physical dependency,
27 are left to %mpe_derived, which runs with the target library assigned.
28 Uniqueness on the business key (LIBREF DSN VAR_NM) is checked by mpe_loader
29 before this hook runs, but on the raw submitted values, so it is case
30 sensitive: 'Region' and 'REGION' for the same table both pass the loader and
31 collide only at approve time. The post-normalisation check below rejects
32 the second one at submit time, naming the variable.
33
34 <h4> SAS Macros </h4>
35 @li mf_nobs.sas
36 @li mp_abort.sas
37 @li mp_derivedvarcheck.sas
38
39 <h4> Related Macros </h4>
40 @li mpe_derived.sas
41 @li mpe_loader.sas
42
43 @author 4GL Apps Ltd
44 @copyright 4GL Apps Ltd. This code may only be used within Data Controller
45 and may not be re-distributed or re-sold without the express permission of
46 4GL Apps Ltd.
47
48**/
49
50%let der_errflag=0;
51%let der_errmsg=;
52
53/* a hook is included at execution time, so this is open code - the macro
54 language has to be inside a macro definition for %local to be valid. The
55 control variables are set here, in the including scope (which %mp_include
56 makes local): a %global inside the macro would conflict with them and abort
57 the step. */
58%macro _mpe_derived_postedit();
59 %local left nready cyc_start cyc_cur cyc_next cyc_path cyc_seen cyc_i
60 cyc_n cyc_found;
61
62/* normalise the incoming values */
63data work.staging_ds;
64 set work.staging_ds;
65 if upcase(_____DELETE__THIS__RECORD_____) ne 'YES' then do;
66 libref=upcase(strip(libref));
67 dsn=upcase(strip(dsn));
68 var_nm=upcase(strip(var_nm));
69 var_type=upcase(strip(var_type));
70 var_fmt=upcase(strip(var_fmt));
71 var_label=strip(var_label);
72 var_deps=upcase(strip(var_deps));
73 var_logic=strip(var_logic);
74 end;
75run;
76
77/**
78 * Duplicate names after normalisation - mpe_loader compares the raw values,
79 * so a case variant reaches us here
80 */
81proc sql;
82create table work.derived_dup as
83 select libref, dsn, var_nm, count(*) as n
84 from work.staging_ds
85 where upcase(_____DELETE__THIS__RECORD_____) ne 'YES'
86 group by libref, dsn, var_nm
87 having count(*)>1;
88quit;
89
90data _null_;
91 set work.derived_dup(obs=1);
92 call symputx('der_errmsg'
93 ,cats('MPE_DERIVED: ',var_nm,' is declared more than once for '
94 ,libref,'.',dsn));
95run;
96
97%mp_abort(iftrue= (0 < %mf_nobs(work.derived_dup))
98 ,mac=mpe_derived_postedit
99 ,msg=%superq(der_errmsg)
100)
101
102/**
103 * Field level checks. The rules for the derived variable itself - the name,
104 * the type, length, format, label and the expression - are core's, in
105 * mp_derivedvarcheck, so this hook and the read time engine (%mpe_derived ->
106 * mp_derivedvars) scan a row with the same rules and report the same reasons.
107 * What is added here are the two fields that belong to Data Controller rather
108 * than to the expression: the control table key, which the loader normalises
109 * but does not require, and ACTIVE, which the EDIT screen offers as a picker
110 * over the MPE_SELECTBOX pair - so a submission that bypasses the grid cannot
111 * land a value the screen would never offer.
112 */
113data work.derived_dcfield(keep=name reason);
114 set work.staging_ds;
115 if upcase(_____DELETE__THIS__RECORD_____)='YES' then delete;
116 length name $32 reason $200;
117 call missing(reason);
118 name=upcase(strip(var_nm));
119 if missing(libref) or missing(dsn) then reason='LIBREF and DSN are required';
120 else if missing(active) or active not in (0,1)
121 then reason='ACTIVE must be 1 or 0';
122 if not missing(reason) then output;
123run;
124
125%mp_derivedvarcheck(inds=work.staging_ds
126 ,outds=work.derived_field
127 ,reserved=%str(_____DELETE__THIS__RECORD_____)
128)
129
130/* the control table fields are reported before the expression's own rules, so
131 which of the two passes found a row does not change the message it gets */
132data work.derived_check;
133 set work.derived_dcfield work.derived_field;
134run;
135
136%if %mf_nobs(work.derived_check)>0 %then %do;
137 data _null_;
138 set work.derived_check(obs=1);
139 call symputx('der_errmsg',cats('MPE_DERIVED: ',name,': ',reason));
140 call symputx('der_errflag',1);
141 run;
142%end;
143
144%mp_abort(iftrue= (&der_errflag=1)
145 ,mac=mpe_derived_postedit
146 ,msg=%superq(der_errmsg)
147)
148
149/**
150 * The graph has to hold for the resulting configuration: the incoming rows
151 * plus the current rows of the same tables that this submission does not
152 * replace. Only active rows are part of it - the engine reads nothing else
153 * (mpe_derived.sas), so an inactive row can neither resolve a dependency nor
154 * be resolved by one.
155 */
156proc sql;
157create table work.derived_rows as
158 select libref, dsn, upcase(strip(var_nm)) as name
159 ,upcase(strip(var_deps)) as deps
160 ,strip(var_logic) as logic
161 from work.staging_ds
162 where upcase(_____DELETE__THIS__RECORD_____) ne 'YES'
163 and active=1
164 union all
165 select libref, dsn, upcase(strip(var_nm)) as name
166 ,upcase(strip(var_deps)) as deps
167 ,strip(var_logic) as logic
168 from &DC_LIBREF..mpe_derived
169 where &dc_dttmtfmt. lt tx_to
170 and active=1
171 and catx('.',libref,dsn,var_nm) not in
172 (select catx('.',libref,dsn,var_nm) from work.staging_ds)
173 and catx('.',libref,dsn) in
174 (select catx('.',libref,dsn) from work.staging_ds);
175quit;
176
177data work.derived_dep;
178 set work.derived_rows;
179 length dep_nm $32;
180 do i=1 to countw(deps,' ');
181 dep_nm=scan(deps,i,' ');
182 if not missing(dep_nm) then output;
183 end;
184 keep libref dsn name dep_nm;
185run;
186
187/* a dependency on a row the engine will not read cannot resolve: it aborts
188 every read of the table with "VAR_DEPS lists X, which is not a column of the
189 table or a derived variable". Catch it here, covering a row this submission
190 deactivates or deletes - neither shows in the control table yet. A name the
191 same submission also stages as a live active row is not inactive after the
192 load, so it is excluded: a delete and re-add of one variable under a different
193 case is two PKs to the loader (its duplicate check is case sensitive) and the
194 re-added row resolves the dependency. */
195proc sql;
196create table work.derived_baddep as
197 select d.libref, d.dsn, d.name, d.dep_nm
198 from work.derived_dep d
199 inner join (
200 select upcase(strip(libref)) as libref
201 ,upcase(strip(dsn)) as dsn
202 ,upcase(strip(var_nm)) as name
203 from work.staging_ds
204 where (upcase(_____DELETE__THIS__RECORD_____) = 'YES'
205 or active ne 1)
206 and catx('.',upcase(strip(libref)),upcase(strip(dsn))
207 ,upcase(strip(var_nm))) not in
208 (select catx('.',upcase(strip(libref)),upcase(strip(dsn))
209 ,upcase(strip(var_nm)))
210 from work.staging_ds
211 where upcase(_____DELETE__THIS__RECORD_____) ne 'YES'
212 and active=1)
213 union all
214 select upcase(strip(libref)) as libref
215 ,upcase(strip(dsn)) as dsn
216 ,upcase(strip(var_nm)) as name
217 from &DC_LIBREF..mpe_derived
218 where &dc_dttmtfmt. lt tx_to
219 and active ne 1
220 and catx('.',upcase(strip(libref)),upcase(strip(dsn))
221 ,upcase(strip(var_nm))) not in
222 (select catx('.',upcase(strip(libref)),upcase(strip(dsn))
223 ,upcase(strip(var_nm)))
224 from work.staging_ds
225 where upcase(_____DELETE__THIS__RECORD_____) ne 'YES'
226 and active=1)
227 ) inactive
228 on inactive.libref=d.libref
229 and inactive.dsn=d.dsn
230 and inactive.name=d.dep_nm;
231quit;
232
233data _null_;
234 set work.derived_baddep(obs=1);
235 call symputx('der_errmsg'
236 ,cats('MPE_DERIVED: ',name,': VAR_DEPS lists ',dep_nm
237 ,', which is not active after this load - the engine cannot resolve it'));
238run;
239
240%mp_abort(iftrue= (0 < %mf_nobs(work.derived_baddep))
241 ,mac=mpe_derived_postedit
242 ,msg=%superq(der_errmsg)
243)
244
245data work.derived_ref;
246 set work.derived_rows;
247 length l $4000 tok $32;
248 l=prxchange("s/'([^']|'')*'//",-1,logic);
249 l=prxchange('s/"([^"]|"")*"//',-1,l);
250 l=prxchange('s/[^A-Za-z0-9_]/ /',-1,upcase(l));
251 do i=1 to countw(l,' ');
252 tok=scan(l,i,' ');
253 if not missing(tok) then output;
254 end;
255 keep libref dsn name tok;
256run;
257
258proc sql;
259create table work.derived_self as
260 select libref, dsn, name, dep_nm
261 from work.derived_dep
262 where name=dep_nm;
263
264create table work.derived_undeclared as
265 select r.libref, r.dsn, r.name, r.tok as dep_nm
266 from work.derived_ref r
267 where exists (
268 select 1 from work.derived_rows n
269 where n.libref=r.libref and n.dsn=r.dsn and n.name=r.tok
270 )
271 and r.tok ne r.name
272 and not exists (
273 select 1 from work.derived_dep d
274 where d.libref=r.libref and d.dsn=r.dsn and d.name=r.name
275 and d.dep_nm=r.tok
276 );
277quit;
278
279/* the message is always prepared, the abort is driven by the count - an
280 iftrue=1 inside a wrapping %if leaves the step alive on some engines */
281data _null_;
282 set work.derived_self(obs=1);
283 call symputx('der_errmsg'
284 ,cats('MPE_DERIVED: ',name,': VAR_DEPS may not list itself'));
285run;
286
287%mp_abort(iftrue= (0 < %mf_nobs(work.derived_self))
288 ,mac=mpe_derived_postedit
289 ,msg=%superq(der_errmsg)
290)
291
292data _null_;
293 set work.derived_undeclared(obs=1);
294 call symputx('der_errmsg'
295 ,cats('MPE_DERIVED: ',name,': VAR_LOGIC references ',dep_nm
296 ,', which is not in VAR_DEPS'));
297run;
298
299%mp_abort(iftrue= (0 < %mf_nobs(work.derived_undeclared))
300 ,mac=mpe_derived_postedit
301 ,msg=%superq(der_errmsg)
302)
303
304/* the inverse of the check above: a declared name the expression never uses
305 is stale, and the read-time engine rejects it too */
306proc sql;
307create table work.derived_stale as
308 select d.libref, d.dsn, d.name, d.dep_nm
309 from work.derived_dep d
310 where not exists (
311 select 1 from work.derived_ref r
312 where r.libref=d.libref and r.dsn=d.dsn and r.name=d.name
313 and r.tok=d.dep_nm
314 );
315quit;
316
317data _null_;
318 set work.derived_stale(obs=1);
319 call symputx('der_errmsg'
320 ,cats('MPE_DERIVED: ',name,': VAR_DEPS lists ',dep_nm
321 ,', which VAR_LOGIC does not reference'));
322run;
323
324%mp_abort(iftrue= (0 < %mf_nobs(work.derived_stale))
325 ,mac=mpe_derived_postedit
326 ,msg=%superq(der_errmsg)
327)
328
329/* the same invariant the RLS hook enforces from its side: a derived variable
330 cannot be named by an active row level security rule of any scope. Only the
331 VIEW scope could evaluate it (on the view the EDIT screen reads), but the RLS
332 hook validates every rule against the physical table, where the column does
333 not exist, so the pairing is refused up front rather than failing there. */
334proc sql;
335create table work.derived_rls as
336 select d.libref, d.dsn, d.name
337 from work.derived_rows d
338 inner join &DC_LIBREF..mpe_row_level_security r
339 on upcase(r.rls_libref)=upcase(d.libref)
340 and upcase(r.rls_table)=upcase(d.dsn)
341 and upcase(r.rls_variable_nm)=upcase(d.name)
342 where r.rls_active=1
343 and &dc_dttmtfmt lt r.tx_to;
344quit;
345
346data _null_;
347 set work.derived_rls(obs=1);
348 call symputx('der_errmsg'
349 ,cats('MPE_DERIVED: ',name,' is named by a row level security rule on '
350 ,libref,'.',dsn,' - derived variables cannot be used there'));
351run;
352
353%mp_abort(iftrue= (0 < %mf_nobs(work.derived_rls))
354 ,mac=mpe_derived_postedit
355 ,msg=%superq(der_errmsg)
356)
357
358/**
359 * Cycle check - repeatedly take the variables whose derived inputs have all
360 * been placed; a round that places nothing leaves a cycle
361 */
362proc sql;
363create table work.derived_edge as
364 select d.libref, d.dsn, d.name as child, d.dep_nm as parent
365 from work.derived_dep d
366 where d.dep_nm in (select name from work.derived_rows);
367quit;
368
369data work.derived_todo;
370 set work.derived_rows(keep=libref dsn name);
371run;
372
373%let left=%mf_nobs(work.derived_todo);
374%let cyc_found=0;
375
376%do %while (&left>0);
377 proc sql;
378 create table work.derived_ready as
379 select t.libref, t.dsn, t.name
380 from work.derived_todo t
381 where not exists (
382 select 1 from work.derived_edge e
383 inner join work.derived_todo t2
384 on e.libref=t2.libref and e.dsn=t2.dsn and e.parent=t2.name
385 where e.libref=t.libref and e.dsn=t.dsn and e.child=t.name
386 );
387 quit;
388
389 %let nready=%mf_nobs(work.derived_ready);
390 %if &nready=0 %then %do;
391 /* walk the edges, so the message names the cycle rather than just the
392 names that could not be placed */
393 %let cyc_n=%mf_nobs(work.derived_todo);
394 data _null_;
395 set work.derived_todo(obs=1);
396 call symputx('cyc_start',name);
397 run;
398 %let cyc_path=&cyc_start;
399 %let cyc_cur=&cyc_start;
400 %let cyc_seen=%str(.&cyc_start..);
401 %do cyc_i=1 %to &cyc_n;
402 %let cyc_next=;
403 proc sql noprint;
404 select e.parent into: cyc_next
405 from work.derived_edge e
406 where e.child="&cyc_cur"
407 and catx('.',e.libref,e.dsn,e.parent) in
408 (select catx('.',libref,dsn,name) from work.derived_todo);
409 quit;
410 %if %length(&cyc_next)=0 %then %let cyc_i=&cyc_n;
411 %else %do;
412 %let cyc_path=&cyc_path. -> &cyc_next;
413 %if %index(&cyc_seen,%str(.&cyc_next..))>0 %then %let cyc_i=&cyc_n;
414 %else %do;
415 %let cyc_seen=&cyc_seen.&cyc_next..;
416 %let cyc_cur=&cyc_next;
417 %end;
418 %end;
419 %end;
420 /* leave the loop before aborting - an abort that only cancels the step
421 would re-enter it with the ready count still zero */
422 %let cyc_found=1;
423 %let left=0;
424 %end;
425
426 %if &cyc_found=0 %then %do;
427 proc sql;
428 delete from work.derived_todo
429 where catx('.',libref,dsn,name) in
430 (select catx('.',libref,dsn,name) from work.derived_ready);
431 quit;
432
433 %let left=%mf_nobs(work.derived_todo);
434 %end;
435%end;
436
437%mp_abort(iftrue= (&cyc_found=1)
438 ,mac=mpe_derived_postedit
439 ,msg=%str(MPE_DERIVED: VAR_DEPS contains a cycle: &cyc_path)
440)
441
442%mend _mpe_derived_postedit;
443
444%_mpe_derived_postedit()