Skip to content

Commit

Permalink
Merge pull request #78 from OHDSI/76-inconsistent-keyword-quoting-in-…
Browse files Browse the repository at this point in the history
…staging-models
  • Loading branch information
lawrenceadams authored Oct 7, 2024
2 parents 415130d + 2df584a commit b9010cd
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 38 deletions.
38 changes: 19 additions & 19 deletions macros/create_synthea_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
encounter VARCHAR(1000),
code VARCHAR(100),
"system" VARCHAR(255),
"description" VARCHAR(255),
description VARCHAR(255),
"type" VARCHAR(255),
category VARCHAR(255),
reaction1 VARCHAR(255),
Expand All @@ -30,7 +30,7 @@
patient VARCHAR(1000),
encounter VARCHAR(1000),
code VARCHAR(100),
"description" VARCHAR(255),
description VARCHAR(255),
reasoncode VARCHAR(255),
reasondescription VARCHAR(255)
);
Expand All @@ -56,7 +56,7 @@
payer VARCHAR(1000),
encounterclass VARCHAR(1000),
code VARCHAR(100),
"description" VARCHAR(255),
description VARCHAR(255),
base_encounter_cost NUMERIC,
total_claim_cost NUMERIC,
payer_coverage NUMERIC,
Expand All @@ -70,7 +70,7 @@
patient VARCHAR(1000),
encounter VARCHAR(1000),
code VARCHAR(100),
"description" VARCHAR(255),
description VARCHAR(255),
base_cost NUMERIC
);
{% endif %}
Expand Down Expand Up @@ -99,7 +99,7 @@
payer VARCHAR(1000),
encounter VARCHAR(1000),
code VARCHAR(100),
"description" VARCHAR(1000),
description VARCHAR(1000),
base_cost NUMERIC,
payer_coverage NUMERIC,
dispenses INT,
Expand All @@ -115,7 +115,7 @@
encounter VARCHAR(1000),
category VARCHAR(1000),
code VARCHAR(100),
"description" VARCHAR(255),
description VARCHAR(255),
"value" VARCHAR(1000),
units VARCHAR(100),
"type" VARCHAR(100)
Expand All @@ -125,9 +125,9 @@
CREATE TABLE {{schema}}.organizations (
id VARCHAR(1000),
"name" VARCHAR(1000),
"address" VARCHAR(1000),
address VARCHAR(1000),
city VARCHAR(100),
"state" VARCHAR(100),
state VARCHAR(100),
zip VARCHAR(100),
lat NUMERIC,
lon NUMERIC,
Expand All @@ -154,9 +154,9 @@
ethnicity VARCHAR(100),
gender VARCHAR(100),
birthplace VARCHAR(100),
"address" VARCHAR(100),
address VARCHAR(100),
city VARCHAR(100),
"state" VARCHAR(100),
state VARCHAR(100),
county VARCHAR(100),
zip VARCHAR(100),
lat NUMERIC,
Expand All @@ -172,7 +172,7 @@
patient VARCHAR(1000),
encounter VARCHAR(1000),
code VARCHAR(100),
"description" VARCHAR(255),
description VARCHAR(255),
base_cost NUMERIC,
reasoncode VARCHAR(1000),
reasondescription VARCHAR(1000)
Expand All @@ -182,12 +182,12 @@
CREATE TABLE {{schema}}.providers (
id VARCHAR(1000),
organization VARCHAR(1000),
"name" VARCHAR(100),
name VARCHAR(100),
gender VARCHAR(100),
speciality VARCHAR(100),
"address" VARCHAR(255),
address VARCHAR(255),
city VARCHAR(100),
"state" VARCHAR(100),
state VARCHAR(100),
zip VARCHAR(100),
lat NUMERIC,
lon NUMERIC,
Expand All @@ -201,7 +201,7 @@
patient VARCHAR(1000),
encounter VARCHAR(1000),
code VARCHAR(100),
"description" VARCHAR(255),
description VARCHAR(255),
udi VARCHAR(255)
);
{% endif %}
Expand Down Expand Up @@ -285,15 +285,15 @@
end_year TIMESTAMP,
payer VARCHAR(1000),
secondary_payer VARCHAR(1000),
"ownership" VARCHAR(1000),
ownership VARCHAR(1000),
ownername VARCHAR(1000)
);
{% endif %}
{% if not check_if_exists(database, schema, "payers") %}
CREATE TABLE {{schema}}.payers (
id VARCHAR(1000),
"name" VARCHAR(1000),
"address" VARCHAR(1000),
name VARCHAR(1000),
address VARCHAR(1000),
city VARCHAR(1000),
state_headquartered VARCHAR(1000),
zip VARCHAR(1000),
Expand All @@ -320,7 +320,7 @@
patient VARCHAR(1000),
encounter VARCHAR(1000),
code VARCHAR(1000),
"description" VARCHAR(1000),
description VARCHAR(1000),
quantity NUMERIC
);
{% endif %}
Expand Down
10 changes: 9 additions & 1 deletion macros/lowercase_columns.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{% macro lowercase_columns(column_names) %}
{# List of SQL Keywords in use by dbt-synthea #}
{% set sql_keywords = ["start", "stop", "type", "system", "date", "first", "last", "value", "name"] %}
{% for column_name in column_names %}
"{{ column_name }}" AS {{ column_name | lower }}
{% set lowercase_column = column_name | lower %}
{# If a keyword - quote the output column name so it can be safely used in models #}
{% if column_name | lower in sql_keywords %}
"{{ column_name }}" AS "{{ lowercase_column }}"
{% else %}
"{{ column_name }}" AS {{ lowercase_column }}
{% endif %}
{% if not loop.last %},{% endif %}
{% endfor %}
{% endmacro %}
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__allergies.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ WITH cte_allergies_lower AS (
, encounter AS encounter_id
, code AS allergy_code
, "system" AS allergy_code_system
, "description" AS allergy_description
, description AS allergy_description
, "type" AS allergy_type
, category AS allergy_category
, reaction1 AS reaction_1_code
Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__careplans.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ WITH cte_careplans_lower AS (
, patient AS patient_id
, encounter AS encounter_id
, code AS careplan_code
, "description" AS careplan_description
, description AS careplan_description
, reasoncode AS careplan_reason_code
, reasondescription AS careplan_reason_description
FROM cte_careplans_lower
Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__conditions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ WITH cte_conditions_lower AS (
, patient AS patient_id
, encounter AS encounter_id
, code AS condition_code
, "description" AS condition_description
, description AS condition_description
FROM cte_conditions_lower

)
Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__devices.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ WITH cte_devices_lower AS (
, patient AS patient_id
, encounter AS encounter_id
, code AS device_code
, "description" AS device_description
, description AS device_description
, udi
FROM cte_devices_lower

Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__imaging_studies.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ WITH cte_imaging_studies_lower AS (
, cte_imaging_studies_rename AS (

SELECT
"id" AS imaging_id
id AS imaging_id
, "date" AS imaging_datetime
, patient AS patient_id
, encounter AS encounter_id
Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__immunizations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ WITH cte_immunizations_lower AS (
, patient AS patient_id
, encounter AS encounter_id
, code AS immunization_code
, "description" AS immunization_description
, description AS immunization_description
, {{ dbt.cast("base_cost", api.Column.translate_type("decimal")) }} AS immunization_base_cost
FROM cte_immunizations_lower

Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__medications.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ WITH cte_medications_lower AS (
, payer AS payer_id
, encounter AS encounter_id
, code AS medication_code
, "description" AS medication_description
, description AS medication_description
, {{ dbt.cast("base_cost", api.Column.translate_type("decimal")) }} AS medication_base_cost
, payer_coverage AS medication_payer_coverage
, dispenses
Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__observations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ WITH cte_observations_lower AS (
, encounter AS encounter_id
, category AS observation_category
, code AS observation_code
, "description" AS observation_description
, description AS observation_description
, "value" AS observation_value
, units AS observation_units
, "type" AS observation_value_type
Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__organizations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ WITH cte_organizations_lower AS (

SELECT
id AS organization_id
, name AS organization_name
, "name" AS organization_name
, address AS organization_address
, city AS organization_city
, state AS organization_state
Expand Down
4 changes: 2 additions & 2 deletions models/staging/synthea/stg_synthea__patients.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ WITH cte_patients_lower AS (
, ethnicity
, gender AS patient_gender
, birthplace
, "address" AS patient_address
, address AS patient_address
, city AS patient_city
, "state" AS patient_state
, state AS patient_state
, county AS patient_county
, zip AS patient_zip
, lat AS patient_latitude
Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__payer_transitions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ WITH cte_payer_transitions_lower AS (
, {{ dbt.cast("end_year", api.Column.translate_type("date")) }} AS coverage_end_date
, payer AS payer_id
, secondary_payer AS secondary_payer_id
, "ownership" AS plan_owner_relationship
, ownership AS plan_owner_relationship
, ownername AS plan_owner_name
FROM cte_payer_transitions_lower

Expand Down
6 changes: 3 additions & 3 deletions models/staging/synthea/stg_synthea__procedures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ WITH cte_procedures_lower AS (

SELECT
"start" AS procedure_start_datetime
, {{ dbt.cast("start", api.Column.translate_type("date")) }} AS procedure_start_date
, {{ dbt.cast("\"start\"", api.Column.translate_type("date")) }} AS procedure_start_date
, "stop" AS procedure_stop_datetime
, {{ dbt.cast("stop", api.Column.translate_type("date")) }} AS procedure_stop_date
, {{ dbt.cast("\"stop\"", api.Column.translate_type("date")) }} AS procedure_stop_date
, patient AS patient_id
, encounter AS encounter_id
, code AS procedure_code
, "description" AS procedure_description
, description AS procedure_description
, {{ dbt.cast("base_cost", api.Column.translate_type("decimal")) }} AS procedure_base_cost
, reasoncode AS procedure_reason_code
, reasondescription AS procedure_reason_description
Expand Down
4 changes: 2 additions & 2 deletions models/staging/synthea/stg_synthea__providers.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ WITH cte_providers_lower AS (
, "name" AS provider_name
, gender AS provider_gender
, speciality AS provider_specialty
, "address" AS provider_address
, address AS provider_address
, city AS provider_city
, "state" AS provider_state
, state AS provider_state
, zip AS provider_zip
, lat AS provider_latitude
, lon AS provider_longitude
Expand Down
2 changes: 1 addition & 1 deletion models/staging/synthea/stg_synthea__supplies.sql
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ WITH cte_supplies_lower AS (
, patient AS patient_id
, encounter AS encounter_id
, code AS supply_code
, "description" AS supply_description
, description AS supply_description
, quantity AS supply_quantity
FROM cte_supplies_lower

Expand Down

0 comments on commit b9010cd

Please sign in to comment.