Skip to content

Commit

Permalink
Added builder for SOE period
Browse files Browse the repository at this point in the history
  • Loading branch information
dogversioning committed Aug 24, 2023
1 parent be13448 commit 40660e8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions cumulus_library/base_table_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def execute_queries(
table_name = table_name.split(".")[1].replace('"', "")
table_names.append(table_name)
for table_name in table_names:
table_name = table_name.replace('"', "")
cursor.execute(f"DROP TABLE IF EXISTS {table_name}")
with get_progress_bar(disable=verbose) as progress:
task = progress.add_task(
Expand Down
58 changes: 58 additions & 0 deletions cumulus_library/studies/core/builder_soe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
""" Module for generating condition codeableConcept table"""
import re
from cumulus_library.base_table_builder import BaseTableBuilder
from cumulus_library.helper import get_progress_bar, query_console_output
from cumulus_library.template_sql.templates import (
get_column_datatype_query,
get_object_denormalize_query,
)


class SOEBuilder(BaseTableBuilder):
display_text = "Creating condition code table..."

def prepare_queries(self, cursor: object, schema: str):
"""Constructs queries related to condition codeableConcept
:param cursor: A database cursor object
:param schema: the schema/db name, matching the cursor
"""
table = "documentreference"
column = "context"
with get_progress_bar(transient=True) as progress:
task = progress.add_task(
"Detecting SOE...",
total=1,
)

query = get_column_datatype_query(schema, table, column)
cursor.execute(query)
progress.advance(task)
result = str(cursor.fetchone()[0])
field_config = {
"start": {
"present": False,
"type": "varchar",
},
"end": {"present": False, "type": "varchar"},
}
if "period row" in result:
# The following will get all text between parenthesis following
# period row - i.e. the schema of the period object
field_schema_str = re.search(r"period row\(\s*([^\n\r]*)\),", result)[1]
for key in field_config.keys():
if f"{key} {field_config[key]['type']}" in field_schema_str:
field_config[key]["present"] = True

self.queries.append(
get_object_denormalize_query(
schema,
table,
"id",
f"{column}.period",
field_config,
"core__soe_doc_period",
)
)
self.write_queries()
1 change: 1 addition & 0 deletions cumulus_library/studies/core/manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ file_names = [
"builder_encounter_coding.py",
"builder_core_medication.py",
"builder_patient_extension.py",
"builder_soe.py"
]

[sql_config]
Expand Down
5 changes: 3 additions & 2 deletions cumulus_library/studies/core/soe_sequence_of_events.sql
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,17 @@ CREATE TABLE core__soe_document AS
WITH soe_document_rawdata AS (
SELECT DISTINCT
cast(
from_iso8601_timestamp(context.period."start") AS timestamp
from_iso8601_timestamp(sdp."start") AS timestamp
) AS doc_start_datetime,
cast(
from_iso8601_timestamp(context.period."end") AS timestamp
from_iso8601_timestamp(sdp."end") AS timestamp
) AS doc_end_datetime,
doc.subject.reference AS subject_ref,
doc.context,
doc.id AS doc_id,
concat('DocumentReference/', doc.id) AS doc_ref
FROM documentreference AS doc
LEFT JOIN core__soe_doc_period AS sdp ON sdp.id = doc.id
),

document AS (
Expand Down
15 changes: 15 additions & 0 deletions cumulus_library/template_sql/object_denormalize.sql.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
CREATE TABLE "{{schema}}"."{{ target_table }}" AS (
SELECT
{{ source_id }},
{%- for key in field_config.keys() %}
{%- if field_config[key]['present'] %}
{{ field }}."{{ key }}" as "{{ key }}"
{%- else %}
cast(NULL AS {{ field_config[key]['type'] }}) as "{{ key }}"
{%- endif %}
{%- if not loop.last -%}
,
{%- endif -%}
{%- endfor %}
FROM {{ source_table }}
);
20 changes: 20 additions & 0 deletions cumulus_library/template_sql/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,23 @@ def get_show_views(schema_name: str, prefix: str) -> str:
return Template(show_tables.read()).render(
schema_name=schema_name, prefix=prefix
)


def get_object_denormalize_query(
schema: str,
source_table: str,
source_id: str,
field: str,
field_config: dict,
target_table: str,
):
path = Path(__file__).parent
with open(f"{path}/object_denormalize.sql.jinja") as column_datatype:
return Template(column_datatype.read()).render(
schema=schema,
source_table=source_table,
source_id=source_id,
field=field,
field_config=field_config,
target_table=target_table,
)

0 comments on commit 40660e8

Please sign in to comment.