-
Notifications
You must be signed in to change notification settings - Fork 5
/
load_stage_tests.sql
562 lines (460 loc) · 16.9 KB
/
load_stage_tests.sql
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
--create temp tables
DROP TABLE IF EXISTS concept_tmp_2020_03_04_3;
CREATE TABLE IF NOT EXISTS concept_tmp_2020_03_04_3
as (select * from concept);
DROP TABLE IF EXISTS concept_relationship_tmp_2020_03_04_3;
CREATE TABLE IF NOT EXISTS concept_relationship_tmp_2020_03_04_3
as (select * from concept_relationship);
DROP TABLE IF EXISTS concept_ancestor_tmp_2020_03_04_3;
CREATE TABLE IF NOT EXISTS concept_ancestor_tmp_2020_03_04_3
as (select * from concept_ancestor);
--compare concept between schemas
with first as (
SELECT a.*, 'concept_tmp_2020_03_04_3' as schema
FROM ( SELECT --concept_id,
concept_name,
domain_id,
vocabulary_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
FROM dev_loinc.concept_tmp_2020_03_04_3
EXCEPT
SELECT --concept_id,
concept_name,
domain_id,
vocabulary_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
FROM dev_loinc.concept_tmp_2020_02_25_1
) as a ),
second as (
SELECT b.*, 'concept_tmp_2020_02_25_1' as schema
FROM ( SELECT --concept_id,
concept_name,
domain_id,
vocabulary_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
FROM dev_loinc.concept_tmp_2020_02_25_1
EXCEPT
SELECT --concept_id,
concept_name,
domain_id,
vocabulary_id,
concept_class_id,
standard_concept,
concept_code,
valid_start_date,
valid_end_date,
invalid_reason
FROM dev_loinc.concept_tmp_2020_03_04_3
) as b )
--changed and new concepts
SELECT *
from first
WHERE vocabulary_id = 'LOINC'
UNION ALL
SELECT *
from second
WHERE vocabulary_id = 'LOINC'
--new concepts
--SELECT * from first
--WHERE concept_id NOT IN (SELECT concept_id FROM second)
--changed concepts
/*SELECT first.*, devv5.levenshtein(first.concept_name, second.concept_name)
from first
LEFT JOIN second ON first.concept_id = second.concept_id
WHERE first.concept_id IN (SELECT concept_id FROM second)
UNION ALL
SELECT second.*, devv5.levenshtein(second.concept_name, first.concept_name)
from second
LEFT JOIN first ON first.concept_id = second.concept_id*/
;
--compare CR between schemas
with first as (
SELECT a.concept_id_1,
a.concept_id_2,
a.relationship_id,
a.invalid_reason,
a.valid_start_date,
a.valid_end_date,
'concept_relationship_tmp_2020_02_25_2' as schema,
c.vocabulary_id
FROM (SELECT *--concept_id_1, concept_id_2, relationship_id, invalid_reason
FROM dev_loinc.concept_relationship_tmp_2020_02_25_2
EXCEPT
SELECT *--concept_id_1, concept_id_2, relationship_id, invalid_reason
FROM dev_loinc.concept_relationship_tmp_2020_02_25_1
) as a
LEFT JOIN dev_loinc.concept c
ON concept_id_1 = c.concept_id
),
second as (
SELECT b.concept_id_1,
b.concept_id_2,
b.relationship_id,
b.invalid_reason,
b.valid_start_date,
b.valid_end_date,
'concept_relationship_tmp_2020_02_25_1' as schema,
c.vocabulary_id
FROM (SELECT *--concept_id_1, concept_id_2, relationship_id, invalid_reason
FROM dev_loinc.concept_relationship_tmp_2020_02_25_1
EXCEPT
SELECT *--concept_id_1, concept_id_2, relationship_id, invalid_reason
FROM dev_loinc.concept_relationship_tmp_2020_02_25_2
) as b
LEFT JOIN dev_loinc.concept c
ON concept_id_1 = c.concept_id
)
--changed and new relationships
SELECT *
FROM first
--WHERE vocabulary_id IN ('NDC', 'SPL') AND relationship_id = 'Maps to'
UNION ALL
SELECT *
FROM second
--WHERE vocabulary_id IN ('NDC', 'SPL') AND relationship_id = 'Maps to'
--new mappings
/*SELECT * from first
WHERE concept_id_1 NOT IN (SELECT concept_id_1 FROM second WHERE concept_id_1 IS NOT NULL) AND vocabulary_id IN ('NDC', 'SPL') AND relationship_id = 'Maps to'*/
--changed mappings
/*SELECT f.concept_id_1,
c.concept_code,
c.concept_name,
f.concept_id_2,
cc.concept_name,
f.relationship_id,
f.invalid_reason,
f.valid_start_date,
f.valid_end_date,
f.schema,
f.vocabulary_id
from first f
LEFT JOIN devv5.concept c
ON f.concept_id_1 = c.concept_id
LEFT JOIN devv5.concept cc
ON f.concept_id_2 = cc.concept_id
WHERE concept_id_1 IN (SELECT concept_id_1 FROM second WHERE concept_id_1 IS NOT NULL)
AND f.vocabulary_id IN ('NDC', 'SPL') AND relationship_id = 'Maps to'
UNION ALL
SELECT s.concept_id_1,
c.concept_code,
c.concept_name,
s.concept_id_2,
cc.concept_name,
s.relationship_id,
s.invalid_reason,
s.valid_start_date,
s.valid_end_date,
s.schema,
s.vocabulary_id
from second s
LEFT JOIN devv5.concept c
ON s.concept_id_1 = c.concept_id
LEFT JOIN devv5.concept cc
ON s.concept_id_2 = cc.concept_id
WHERE concept_id_1 IN (SELECT concept_id_1 FROM first WHERE concept_id_1 IS NOT NULL)
AND s.vocabulary_id IN ('NDC', 'SPL') AND relationship_id = 'Maps to'*/
;
--compare CR_stage between schemas
with first as (
SELECT a.concept_code_1,
a.concept_code_2,
a.vocabulary_id_1,
a.vocabulary_id_2,
a.relationship_id,
a.invalid_reason,
--a.valid_start_date,
--a.valid_end_date,
'dev_loinc' as schema
FROM (SELECT concept_id_1,
concept_id_2,
concept_code_1,
concept_code_2,
vocabulary_id_1,
vocabulary_id_2,
relationship_id,
--valid_start_date,
--valid_end_date,
invalid_reason
FROM dev_loinc.concept_relationship_stage
EXCEPT
SELECT concept_id_1,
concept_id_2,
concept_code_1,
concept_code_2,
vocabulary_id_1,
vocabulary_id_2,
relationship_id,
--valid_start_date,
--valid_end_date,
invalid_reason
FROM dev_test.concept_relationship_stage
) as a
),
second as (
SELECT b.concept_code_1,
b.concept_code_2,
b.vocabulary_id_1,
b.vocabulary_id_2,
b.relationship_id,
b.invalid_reason,
--b.valid_start_date,
--b.valid_end_date,
'dev_test' as schema
FROM (SELECT concept_id_1,
concept_id_2,
concept_code_1,
concept_code_2,
vocabulary_id_1,
vocabulary_id_2,
relationship_id,
--valid_start_date,
--valid_end_date,
invalid_reason
FROM dev_test.concept_relationship_stage
EXCEPT
SELECT concept_id_1,
concept_id_2,
concept_code_1,
concept_code_2,
vocabulary_id_1,
vocabulary_id_2,
relationship_id,
--valid_start_date,
--valid_end_date,
invalid_reason
FROM dev_loinc.concept_relationship_stage
) as b
)
--changed and new relationships
SELECT *
FROM first
WHERE vocabulary_id_1 IN ('LOINC')-- AND relationship_id = 'Maps to'
UNION ALL
SELECT *
FROM second
WHERE vocabulary_id_1 IN ('LOINC')-- AND relationship_id = 'Maps to'
--new mappings
/*SELECT * from first
WHERE concept_id_1 NOT IN (SELECT concept_id_1 FROM second WHERE concept_id_1 IS NOT NULL) AND vocabulary_id IN ('NDC', 'SPL') AND relationship_id = 'Maps to'*/
--changed mappings
/*SELECT f.concept_id_1,
c.concept_code,
c.concept_name,
f.concept_id_2,
cc.concept_name,
f.relationship_id,
f.invalid_reason,
f.valid_start_date,
f.valid_end_date,
f.schema,
f.vocabulary_id
from first f
LEFT JOIN devv5.concept c
ON f.concept_id_1 = c.concept_id
LEFT JOIN devv5.concept cc
ON f.concept_id_2 = cc.concept_id
WHERE concept_id_1 IN (SELECT concept_id_1 FROM second WHERE concept_id_1 IS NOT NULL)
AND f.vocabulary_id IN ('NDC', 'SPL') AND relationship_id = 'Maps to'
UNION ALL
SELECT s.concept_id_1,
c.concept_code,
c.concept_name,
s.concept_id_2,
cc.concept_name,
s.relationship_id,
s.invalid_reason,
s.valid_start_date,
s.valid_end_date,
s.schema,
s.vocabulary_id
from second s
LEFT JOIN devv5.concept c
ON s.concept_id_1 = c.concept_id
LEFT JOIN devv5.concept cc
ON s.concept_id_2 = cc.concept_id
WHERE concept_id_1 IN (SELECT concept_id_1 FROM first WHERE concept_id_1 IS NOT NULL)
AND s.vocabulary_id IN ('NDC', 'SPL') AND relationship_id = 'Maps to'*/
;
--compare CA between schemas
with first as (
SELECT a.ancestor_concept_id,
a.descendant_concept_id,
a.min_levels_of_separation,
a.max_levels_of_separation,
'concept_ancestor_tmp_2020_02_19_1' as schema,
anc.vocabulary_id as ancestor_vocabulary_id,
des.vocabulary_id as descendant_vocabulary_id
FROM (SELECT *
FROM dev_loinc.concept_ancestor_tmp_2020_02_19_1
EXCEPT
SELECT *
FROM dev_loinc.concept_ancestor_tmp_2020_02_19_2
) as a
LEFT JOIN dev_loinc.concept anc
ON a.ancestor_concept_id = anc.concept_id
LEFT JOIN dev_loinc.concept des
ON a.descendant_concept_id = des.concept_id
),
second as (
SELECT b.ancestor_concept_id,
b.descendant_concept_id,
b.min_levels_of_separation,
b.max_levels_of_separation,
'concept_ancestor_tmp_2020_02_19_2' as schema,
anc.vocabulary_id as ancestor_vocabulary_id,
des.vocabulary_id as descendant_vocabulary_id
FROM (SELECT *
FROM dev_loinc.concept_ancestor_tmp_2020_02_19_2
EXCEPT
SELECT *
FROM dev_loinc.concept_ancestor_tmp_2020_02_19_1
) as b
LEFT JOIN dev_loinc.concept anc
ON b.ancestor_concept_id = anc.concept_id
LEFT JOIN dev_loinc.concept des
ON b.descendant_concept_id = des.concept_id
)
SELECT *
FROM first
--WHERE (ancestor_vocabulary_id IN ('LOINC', 'SNOMED') AND descendant_vocabulary_id IN ('LOINC')) AND ancestor_concept_id != descendant_concept_id
UNION ALL
SELECT *
FROM second
--WHERE (ancestor_vocabulary_id IN ('LOINC', 'SNOMED') AND descendant_vocabulary_id IN ('LOINC')) AND ancestor_concept_id != descendant_concept_id
;
--compare CR_stage between current and tmp
SELECT a.concept_code_1, a.concept_code_2, a.vocabulary_id_1, a.vocabulary_id_2, a.relationship_id, a.invalid_reason, a.valid_start_date, a.valid_end_date, 'current' as schema, c.vocabulary_id
FROM ( SELECT *--concept_id_1, concept_id_2, relationship_id, invalid_reason
FROM concept_relationship_stage
EXCEPT
SELECT *--concept_id_1, concept_id_2, relationship_id, invalid_reason
FROM concept_relationship_stage_tmp
) as a
LEFT JOIN devv5.concept c
ON concept_code_1 = c.concept_code AND vocabulary_id_1 = c.vocabulary_id
UNION ALL
SELECT b.concept_code_1, b.concept_code_2, b.vocabulary_id_1, b.vocabulary_id_2, b.relationship_id, b.invalid_reason, b.valid_start_date, b.valid_end_date, 'temp' as schema, c.vocabulary_id
FROM ( SELECT *--concept_id_1, concept_id_2, relationship_id, invalid_reason
FROM concept_relationship_stage_tmp
EXCEPT
SELECT *--concept_id_1, concept_id_2, relationship_id, invalid_reason
FROM concept_relationship_stage
) as b
LEFT JOIN devv5.concept c
ON concept_code_1 = c.concept_code AND vocabulary_id_1 = c.vocabulary_id
;
--to check mappings are gone from devv_ndc
with gone as (
SELECT cr.*
FROM devv5.concept_relationship cr
JOIN devv5.concept c
ON cr.concept_id_1 = c.concept_id AND c.vocabulary_id = 'NDC'
JOIN devv5.concept cc
ON cr.concept_id_2 = cc.concept_id AND cc.standard_concept = 'S' AND cc.invalid_reason IS NULL
WHERE cr.invalid_reason IS NULL AND cr.relationship_id = 'Maps to'
AND NOT EXISTS(
SELECT 1
FROM dev_ndc.concept_relationship cr2
WHERE cr2.concept_id_1 = cr.concept_id_1
AND cr2.relationship_id = cr.relationship_id
AND cr2.invalid_reason IS NULL
)
)
SELECT * FROM gone
;
--check if all the mapping was inserted
--check CR
SELECT *
FROM dalex.ndc_concept_relationship_manual_2019_09_16_modifications crm
LEFT JOIN devv5.concept c
ON crm.concept_code_1 = c.concept_code AND crm.vocabulary_id_1 = c.vocabulary_id
LEFT JOIN devv5.concept cc
ON crm.concept_code_2 = cc.concept_code AND crm.vocabulary_id_2 = cc.vocabulary_id
WHERE NOT EXISTS (
SELECT 1
FROM dev_ndc.concept_relationship cr
WHERE cr.concept_id_1 = c.concept_id AND
cr.concept_id_2 = cc.concept_id AND
crm.relationship_id = cr.relationship_id AND
cr.invalid_reason IS NULL
)
AND NOT EXISTS(
SELECT 1
FROM dev_ndc.concept_relationship crr
WHERE crr.concept_id_1 = c.concept_id AND
crm.relationship_id = crr.relationship_id AND
crr.invalid_reason IS NULL
)
;
--check if all the mapping was inserted
--check CR_stage
SELECT *
FROM dalex.ndc_concept_relationship_manual_2019_09_16_modifications crm
LEFT JOIN devv5.concept c
ON crm.concept_code_1 = c.concept_code AND crm.vocabulary_id_1 = c.vocabulary_id
WHERE NOT EXISTS (
SELECT 1
FROM dev_ndc.concept_relationship_stage cr
WHERE cr.concept_code_1 = crm.concept_code_1 AND
cr.concept_code_2 = crm.concept_code_2 AND
cr.vocabulary_id_1 = crm.vocabulary_id_1 AND
cr.vocabulary_id_2 = crm.vocabulary_id_2 AND
crm.relationship_id = cr.relationship_id AND
cr.invalid_reason IS NULL
)
AND NOT EXISTS(
SELECT 1
FROM dev_ndc.concept_relationship crr
WHERE crr.concept_id_1 = c.concept_id AND
crm.relationship_id = crr.relationship_id AND
crr.invalid_reason IS NULL
)
;
--check if 1-to-many mapping originates only from CR_manual
SELECT *
FROM dev_ndc.concept_relationship cr
WHERE concept_id_1 IN (
SELECT concept_id_1
FROM dev_ndc.concept_relationship crr
JOIN devv5.concept c
ON crr.concept_id_1 = c.concept_id
AND c.vocabulary_id = 'NDC'
WHERE crr.invalid_reason IS NULL
AND crr.relationship_id = 'Maps to'
GROUP BY concept_id_1
HAVING COUNT(*) > 1
)
AND cr.invalid_reason IS NULL
AND NOT EXISTS (SELECT 1
FROM dev_ndc.concept_relationship_manual crm
LEFT JOIN devv5.concept cc ON crm.concept_code_1 = cc.concept_code AND crm.vocabulary_id_1 = cc.vocabulary_id
LEFT JOIN devv5.concept ccc ON crm.concept_code_2 = ccc.concept_code AND crm.vocabulary_id_2 = ccc.vocabulary_id
WHERE cr.concept_id_1 = cc.concept_id
AND cr.concept_id_2 = ccc.concept_id
AND cr.relationship_id = crm.relationship_id
AND crm.invalid_reason IS NULL
)
;
SELECT DISTINCT c.*
FROM devv5.concept_relationship cr
JOIN devv5.concept c
ON cr.concept_id_1 = c.concept_id AND c.vocabulary_id = 'NDC'
JOIN devv5.concept cc
ON cr.concept_id_2 = cc.concept_id AND cc.vocabulary_id = 'NDC'
WHERE TRUE
AND cr.relationship_id = 'Maps to' AND cr.invalid_reason IS NULL AND cr.valid_start_date > '2019-11-05'