|
139 | 139 | -- Because concepts may or may not have the standard_concept_id, in general. And we prefer to use the standard_concept_id, if it exists. |
140 | 140 | WHERE target_concept.concept_id = COALESCE(temp_existing_concepts.standard_concept_id, temp_existing_concepts.source_concept_id); |
141 | 141 |
|
| 142 | +-- When the scan report table is a death table, override dest_table_id to the death OMOP table so rules show destination table: death, field: cause_concept_id |
| 143 | +UPDATE temp_existing_concepts_%(table_id)s temp_existing_concepts |
| 144 | +SET dest_table_id = (SELECT id FROM mapping_omoptable WHERE "table" = 'death' LIMIT 1) |
| 145 | +WHERE (SELECT death_table FROM mapping_scanreporttable WHERE id = %(table_id)s) = TRUE; |
| 146 | +
|
142 | 147 | -- Update person field ID |
143 | 148 | UPDATE temp_existing_concepts_%(table_id)s temp_existing_concepts |
144 | 149 | SET dest_person_field_id = omop_field.id |
|
204 | 209 | temp_existing_concepts.object_id, |
205 | 210 | target_concept.domain_id, |
206 | 211 | target_concept.concept_id AS concept_id, |
207 | | - -- Use domain-specific source_concept_field_id |
208 | | - (SELECT omop_field.id |
209 | | - FROM mapping_omopfield AS omop_field |
210 | | - WHERE omop_field.table_id = temp_existing_concepts.dest_table_id |
211 | | - AND omop_field.field = |
212 | | - CASE |
213 | | - WHEN target_concept.domain_id = 'Spec Anatomic Site' THEN 'anatomic_site_source_concept_id' -- Not applicable for Specimen table |
| 212 | + -- Source concept field: death table uses cause_source_concept_id, others use domain-based name |
| 213 | + (SELECT omop_field.id |
| 214 | + FROM mapping_omopfield AS omop_field |
| 215 | + JOIN mapping_omoptable AS omop_table ON omop_table.id = omop_field.table_id |
| 216 | + WHERE omop_field.table_id = temp_existing_concepts.dest_table_id |
| 217 | + AND omop_field.field = |
| 218 | + CASE |
| 219 | + WHEN omop_table.table = 'death' THEN 'cause_source_concept_id' |
| 220 | + WHEN target_concept.domain_id = 'Spec Anatomic Site' THEN 'anatomic_site_source_concept_id' |
214 | 221 | ELSE LOWER(target_concept.domain_id) || '_source_concept_id' |
215 | 222 | END |
216 | 223 | LIMIT 1) AS source_concept_field_id, |
217 | 224 |
|
218 | | - -- Use domain-specific source_value_field_id |
219 | | - (SELECT omop_field.id |
220 | | - FROM mapping_omopfield AS omop_field |
221 | | - WHERE omop_field.table_id = temp_existing_concepts.dest_table_id |
222 | | - AND omop_field.field = |
223 | | - CASE |
| 225 | + -- Source value field: death table uses cause_source_value, others use domain-based name |
| 226 | + (SELECT omop_field.id |
| 227 | + FROM mapping_omopfield AS omop_field |
| 228 | + JOIN mapping_omoptable AS omop_table ON omop_table.id = omop_field.table_id |
| 229 | + WHERE omop_field.table_id = temp_existing_concepts.dest_table_id |
| 230 | + AND omop_field.field = |
| 231 | + CASE |
| 232 | + WHEN omop_table.table = 'death' THEN 'cause_source_value' |
224 | 233 | WHEN target_concept.domain_id = 'Spec Anatomic Site' THEN 'anatomic_site_source_value' |
225 | 234 | ELSE LOWER(target_concept.domain_id) || '_source_value' |
226 | 235 | END |
227 | 236 | LIMIT 1) AS source_value_field_id, |
228 | 237 |
|
229 | | - -- Use domain-specific dest_concept_field_id |
230 | | - (SELECT omop_field.id |
231 | | - FROM mapping_omopfield AS omop_field |
232 | | - WHERE omop_field.table_id = temp_existing_concepts.dest_table_id |
233 | | - AND omop_field.field = |
234 | | - CASE |
| 238 | + -- Dest concept field: death table uses cause_concept_id, others use domain-based name |
| 239 | + (SELECT omop_field.id |
| 240 | + FROM mapping_omopfield AS omop_field |
| 241 | + JOIN mapping_omoptable AS omop_table ON omop_table.id = omop_field.table_id |
| 242 | + WHERE omop_field.table_id = temp_existing_concepts.dest_table_id |
| 243 | + AND omop_field.field = |
| 244 | + CASE |
| 245 | + WHEN omop_table.table = 'death' THEN 'cause_concept_id' |
235 | 246 | WHEN target_concept.domain_id = 'Spec Anatomic Site' THEN 'anatomic_site_concept_id' |
236 | 247 | ELSE LOWER(target_concept.domain_id) || '_concept_id' |
237 | 248 | END |
|
293 | 304 | AND map_status.value = 'COMPLETE' |
294 | 305 | AND scan_report.id != %(scan_report_id)s |
295 | 306 | ), |
296 | | - -- Find values in eligible scan reports that have M-type concepts tied |
| 307 | + -- Find values in eligible scan reports that have M-type concepts tied. |
| 308 | + -- Only reuse from tables with the same death_table flag (death tables get death concepts only; non-death never get death concepts). |
297 | 309 | values_with_m_concepts AS ( |
298 | 310 | SELECT |
299 | 311 | sr_value.value AS matching_value_name, |
|
306 | 318 | FROM eligible_reports AS eligible_report |
307 | 319 | JOIN mapping_scanreporttable AS sr_table ON sr_table.scan_report_id = eligible_report.id |
308 | 320 | AND sr_table.name = %(current_table_name)s |
| 321 | + AND (SELECT death_table FROM mapping_scanreporttable WHERE id = %(table_id)s) IS NOT DISTINCT FROM sr_table.death_table |
309 | 322 | JOIN mapping_scanreportfield AS sr_field ON sr_field.scan_report_table_id = sr_table.id |
310 | 323 | JOIN mapping_scanreportvalue AS sr_value ON sr_value.scan_report_field_id = sr_field.id |
311 | 324 | JOIN mapping_scanreportconcept AS sr_concept ON sr_concept.object_id = sr_value.id |
|
345 | 358 | AND map_status.value = 'COMPLETE' |
346 | 359 | AND scan_report.id != %(scan_report_id)s |
347 | 360 | ), |
348 | | - -- Find fields in eligible scan reports that have M-type concepts tied |
| 361 | + -- Find fields in eligible scan reports that have M-type concepts tied. |
| 362 | + -- Match only tables with an identical death_table setting to ensure concepts from death tables are reused only with other death tables, and non-death tables only with non-death. |
349 | 363 | fields_with_m_concepts AS ( |
350 | 364 | SELECT |
351 | 365 | sr_field.name AS matching_field_name, |
|
356 | 370 | FROM eligible_reports AS eligible_report |
357 | 371 | JOIN mapping_scanreporttable AS sr_table ON sr_table.scan_report_id = eligible_report.id |
358 | 372 | AND sr_table.name = %(current_table_name)s |
| 373 | + AND (SELECT death_table FROM mapping_scanreporttable WHERE id = %(table_id)s) IS NOT DISTINCT FROM sr_table.death_table |
359 | 374 | JOIN mapping_scanreportfield AS sr_field ON sr_field.scan_report_table_id = sr_table.id |
360 | 375 | JOIN mapping_scanreportconcept AS sr_concept ON sr_concept.object_id = sr_field.id |
361 | 376 | AND sr_concept.content_type_id = (SELECT id FROM field_content_type) |
|
372 | 387 | FROM fields_with_m_concepts; |
373 | 388 | """ |
374 | 389 |
|
| 390 | +# Only set object_id if the death_table flag for the current table matches the source table (identified by source_scanreport_id and matching_table_name). |
375 | 391 | find_object_id_query = """ |
376 | 392 | UPDATE temp_reuse_concepts_%(table_id)s AS temp_table |
377 | 393 | SET object_id = |
|
405 | 421 | ) |
406 | 422 | LIMIT 1) |
407 | 423 | ELSE NULL |
408 | | - END; |
| 424 | + END |
| 425 | + WHERE (SELECT death_table FROM mapping_scanreporttable WHERE id = %(table_id)s) IS NOT DISTINCT FROM ( |
| 426 | + SELECT sr_table.death_table |
| 427 | + FROM mapping_scanreporttable AS sr_table |
| 428 | + WHERE sr_table.scan_report_id = temp_table.source_scanreport_id |
| 429 | + AND sr_table.name = temp_table.matching_table_name |
| 430 | + LIMIT 1 |
| 431 | + ); |
409 | 432 | """ |
410 | 433 |
|
411 | 434 |
|
|
0 commit comments