Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,4 @@ seeds:
+post-hook: "{{ load_seed(var('custom_bucket_name','tuva-public-resources') ~ '/versioned_value_sets/0.14.13','surgery_gynecology_cohort.csv',compression=true,null_marker=true) }}"
service_categories:
service_category__service_categories:
+post-hook: "{{ load_seed(var('custom_bucket_name','tuva-public-resources') ~ '/versioned_value_sets/0.14.13','service_category__service_categories.csv',compression=true,null_marker=true) }}"
+post-hook: "{{ load_seed(var('custom_bucket_name','tuva-public-resources') ~ '/versioned_value_sets/0.14.14','service_category__service_categories.csv',compression=true,null_marker=true) }}"
151 changes: 151 additions & 0 deletions macros/cross_database_utils/custom_union_relations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
{#-
This file contains a custom, adapter-aware implementation of union_relations.
- For the Fabric adapter, it uses a custom version to handle datetime2 types.
- For all other adapters, it falls back to the standard dbt_utils.union_relations macro.
-#}

{%- macro custom_union_relations(relations, column_override=none, include=[], exclude=[], source_column_name='_dbt_source_relation', where=none) -%}

{{ return(adapter.dispatch('custom_union_relations')(relations, column_override, include, exclude, source_column_name, where)) }}

{% endmacro %}

{%- macro default__custom_union_relations(relations, column_override=none, include=[], exclude=[], source_column_name='_dbt_source_relation', where=none) -%}

{{ dbt_utils.union_relations(relations, column_override, include, exclude, source_column_name, where) }}

{%- endmacro %}


{%- macro fabric__custom_union_relations(relations, column_override=none, include=[], exclude=[], source_column_name='_dbt_source_relation', where=none) -%}

{%- if exclude and include -%}
{{ exceptions.raise_compiler_error("Both an exclude and include list were provided to the `union` macro. Only one is allowed") }}
{%- endif -%}

{#-- Prevent querying of db in parsing mode. This works because this macro does not create any new refs. -#}
{%- if not execute %}
{{ return('') }}
{% endif -%}

{%- set column_override = column_override if column_override is not none else {} -%}

{%- set relation_columns = {} -%}{%- set column_superset = {} -%}
{%- set all_excludes = [] -%}
{%- set all_includes = [] -%}

{%- if exclude -%}
{%- for exc in exclude -%}
{%- do all_excludes.append(exc | lower) -%}
{%- endfor -%}
{%- endif -%}

{%- if include -%}
{%- for inc in include -%}
{%- do all_includes.append(inc | lower) -%}
{%- endfor -%}
{%- endif -%}

{%- for relation in relations -%}

{%- do relation_columns.update({relation: []}) -%}

{%- do dbt_utils._is_relation(relation, 'union_relations') -%}
{%- do dbt_utils._is_ephemeral(relation, 'union_relations') -%}
{%- set cols = adapter.get_columns_in_relation(relation) -%}
{%- for col in cols -%}

{#- If an exclude list was provided and the column is in the list, do nothing -#}
{%- if exclude and col.column | lower in all_excludes -%}

{#- If an include list was provided and the column is not in the list, do nothing -#}
{%- elif include and col.column | lower not in all_includes -%}

{#- Otherwise add the column to the column superset -#}
{%- else -%}

{#- update the list of columns in this relation -#}
{%- do relation_columns[relation].append(col.column) -%}

{%- if col.column in column_superset -%}

{%- set stored = column_superset[col.column] -%}
{%- if col.is_string() and stored.is_string() and col.string_size() > stored.string_size() -%}

{%- do column_superset.update({col.column: col}) -%}

{%- endif %}

{%- else -%}

{%- do column_superset.update({col.column: col}) -%}

{%- endif -%}

{%- endif -%}

{%- endfor -%}
{%- endfor -%}

{%- set ordered_column_names = column_superset.keys() -%}
{%- set dbt_command = flags.WHICH -%}


{% if dbt_command in ['run', 'build'] %}
{% if (include | length > 0 or exclude | length > 0) and not column_superset.keys() %}
{%- set relations_string -%}
{%- for relation in relations -%}
{{ relation.name }}
{%- if not loop.last %}, {% endif -%}
{%- endfor -%}
{%- endset -%}

{%- set error_message -%}
There were no columns found to union for relations {{ relations_string }}
{%- endset -%}

{{ exceptions.raise_compiler_error(error_message) }}
{%- endif -%}
{%- endif -%}

{%- for relation in relations %}

(
select

{%- if source_column_name is not none %}
cast({{ dbt.string_literal(relation) }} as {{ dbt.type_string() }}) as {{ source_column_name }},
{%- endif %}

{% for col_name in ordered_column_names -%}

{%- set col = column_superset[col_name] %}
{%- set col_type = column_override.get(col.column, col.data_type) %}
{%- set col_name = adapter.quote(col_name) if col_name in relation_columns[relation] else 'null' %}

{#- THIS IS THE CUSTOM LOGIC FOR FABRIC -#}
{%- if col_type.lower().startswith('datetime') %}
{%- set precision = col_type.split('(')[1].split(')')[0] if '(' in col_type else '6' %}
cast({{ col_name }} as datetime2({{ precision }})) as {{ col.quoted }}
{%- else %}
cast({{ col_name }} as {{ col_type }}) as {{ col.quoted }}
{%- endif %}

{% if not loop.last %},{% endif -%}

{%- endfor %}

from {{ relation }}

{% if where -%}
where {{ where }}
{%- endif %}
)

{% if not loop.last -%}
union all
{% endif -%}

{%- endfor -%}

{%- endmacro -%}
171 changes: 171 additions & 0 deletions models/claims_preprocessing/encounters/encounter_models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,131 @@ models:
- name: tuva_last_run
description: '{{ doc("tuva_last_run") }}'

- name: inpatient_long_term__encounter_grain
description: "Encounters for inpatient long term acute care. One row per encounter."
config:
schema: |
{%- if var('tuva_schema_prefix', None) != None -%}
{{ var('tuva_schema_prefix') }}_claims_preprocessing
{%- else -%}
claims_preprocessing
{%- endif -%}
materialized: table
tags:
- claims_preprocessing
- encounters
columns:
- name: encounter_id
description: '{{ doc("encounter_id") }}'
- name: encounter_start_date
description: '{{ doc("encounter_start_date") }}'
- name: encounter_end_date
description: '{{ doc("encounter_end_date") }}'
- name: patient_data_source_id
description: '{{ doc("patient_data_source_id") }}'
- name: encounter_type
description: '{{ doc("encounter_type") }}'
- name: encounter_group
description: '{{ doc("encounter_group") }}'
- name: person_id
description: '{{ doc("person_id") }}'
- name: admit_age
description: '{{ doc("admit_age") }}'
- name: gender
description: '{{ doc("gender") }}'
meta:
terminology: https://github.com/tuva-health/the_tuva_project/blob/main/seeds/terminology/terminology__gender.csv
- name: race
description: '{{ doc("race") }}'
meta:
terminology: https://github.com/tuva-health/the_tuva_project/blob/main/seeds/terminology/terminology__race.csv
- name: primary_diagnosis_code_type
description: '{{ doc("primary_diagnosis_code_type") }}'
- name: primary_diagnosis_code
description: '{{ doc("primary_diagnosis_code") }}'
- name: primary_diagnosis_description
description: '{{ doc("primary_diagnosis_description") }}'
- name: facility_id
description: '{{ doc("facility_id") }}'
- name: observation_flag
description: '{{ doc("observation_flag") }}'
- name: ed_flag
description: '{{ doc("ed_flag") }}'
- name: lab_flag
description: '{{ doc("lab_flag") }}'
- name: dme_flag
description: '{{ doc("dme_flag") }}'
- name: ambulance_flag
description: '{{ doc("ambulance_flag") }}'
- name: pharmacy_flag
description: '{{ doc("pharmacy_flag") }}'
- name: facility_name
description: '{{ doc("facility_name") }}'
- name: facility_type
description: '{{ doc("facility_type") }}'
- name: delivery_flag
description: '{{ doc("delivery_flag") }}'
- name: delivery_type
description: '{{ doc("delivery_type") }}'
- name: newborn_flag
description: '{{ doc("newborn_flag") }}'
- name: nicu_flag
description: '{{ doc("nicu_flag") }}'
- name: facility_npi
description: '{{ doc("facility_npi") }}'
- name: provider_first_name
description: '{{ doc("provider_first_name") }}'
- name: provider_last_name
description: '{{ doc("provider_last_name") }}'
- name: drg_code_type
description: '{{ doc("drg_code_type") }}'
- name: drg_code
description: '{{ doc("drg_code") }}'
- name: drg_description
description: '{{ doc("drg_description") }}'
- name: medical_surgical
description: '{{ doc("medical_surgical") }}'
meta:
terminology: https://github.com/tuva-health/the_tuva_project/blob/main/seeds/terminology/terminology__ms_drg.csv
- name: admit_source_code
description: '{{ doc("admit_source_code") }}'
meta:
terminology: https://github.com/tuva-health/the_tuva_project/blob/main/seeds/terminology/terminology__admit_source.csv
- name: admit_source_description
description: '{{ doc("admit_source_description") }}'
- name: admit_type_code
description: '{{ doc("admit_type_code") }}'
meta:
terminology: https://github.com/tuva-health/the_tuva_project/blob/main/seeds/terminology/terminology__admit_type.csv
- name: admit_type_description
description: '{{ doc("admit_type_description") }}'
- name: discharge_disposition_code
description: '{{ doc("discharge_disposition_code") }}'
meta:
terminology: https://github.com/tuva-health/the_tuva_project/blob/main/seeds/terminology/terminology__discharge_disposition.csv
- name: discharge_disposition_description
description: '{{ doc("discharge_disposition_description") }}'
- name: total_paid_amount
description: '{{ doc("total_paid_amount") }}'
- name: total_allowed_amount
description: '{{ doc("total_allowed_amount") }}'
- name: total_charge_amount
description: '{{ doc("total_charge_amount") }}'
- name: claim_count
description: '{{ doc("claim_count") }}'
- name: inst_claim_count
description: '{{ doc("inst_claim_count") }}'
- name: prof_claim_count
description: '{{ doc("prof_claim_count") }}'
- name: length_of_stay
description: '{{ doc("length_of_stay") }}'
- name: mortality_flag
description: '{{ doc("mortality_flag") }}'
- name: data_source
description: '{{ doc("data_source") }}'
- name: tuva_last_run
description: '{{ doc("tuva_last_run") }}'

- name: inpatient_snf__encounter_grain
description: "Encounters for inpatient rehab. One row per encounter."
config:
Expand Down Expand Up @@ -3350,6 +3475,41 @@ models:
- claims_preprocessing
- encounters

##inpatient long term actue care
- name: inpatient_long_term__generate_encounter_id
description: generate encounter id and stitch inpatient ltac claims together
config:
schema: |
{%- if var('tuva_schema_prefix',None) != None -%}{{var('tuva_schema_prefix')}}_claims_preprocessing
{%- else -%}claims_preprocessing{%- endif -%}
materialized: table
tags:
- claims_preprocessing
- encounters

- name: inpatient_long_term__start_end_dates
description: calculate start and end date of encounter
config:
schema: |
{%- if var('tuva_schema_prefix',None) != None -%}{{var('tuva_schema_prefix')}}_claims_preprocessing
{%- else -%}claims_preprocessing{%- endif -%}
materialized: table
tags:
- claims_preprocessing
- encounters

- name: inpatient_long_term__prof_claims
description: match corresponding prof claims to institutional claims
config:
schema: |
{%- if var('tuva_schema_prefix',None) != None -%}{{var('tuva_schema_prefix')}}_claims_preprocessing
{%- else -%}claims_preprocessing{%- endif -%}
materialized: table
tags:
- claims_preprocessing
- encounters



##inpatient snf
- name: inpatient_snf__generate_encounter_id
Expand Down Expand Up @@ -4131,6 +4291,17 @@ models:
- claims_preprocessing
- encounters

- name: encounters__prof_and_lower_priority
description: adding in lower priority institutional claims in one location with professional for grouping to higher level encounters
config:
schema: |
{%- if var('tuva_schema_prefix',None) != None -%}{{var('tuva_schema_prefix')}}_claims_preprocessing
{%- else -%}claims_preprocessing{%- endif -%}
materialized: table
tags:
- claims_preprocessing
- encounters

- name: encounters__stg_inpatient_institutional
config:
schema: |
Expand Down
Loading