From dfad1469bc0acf8e6c6dc95b9bc2b0a8e62e3deb Mon Sep 17 00:00:00 2001 From: Matt Garber Date: Fri, 25 Aug 2023 09:44:17 -0400 Subject: [PATCH] Commenting, cleanup --- cumulus_library/studies/core/builder_soe.py | 5 +- .../template_sql/object_denormalize.sql.jinja | 2 +- cumulus_library/template_sql/templates.py | 49 +++++++++++-------- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/cumulus_library/studies/core/builder_soe.py b/cumulus_library/studies/core/builder_soe.py index 7a2f69c6..03ab1a36 100644 --- a/cumulus_library/studies/core/builder_soe.py +++ b/cumulus_library/studies/core/builder_soe.py @@ -9,10 +9,10 @@ class SOEBuilder(BaseTableBuilder): - display_text = "Creating condition code table..." + display_text = "Creating SoE support tables..." def prepare_queries(self, cursor: object, schema: str): - """Constructs queries related to condition codeableConcept + """Constructs tables for SOE QA verification :param cursor: A database cursor object :param schema: the schema/db name, matching the cursor @@ -55,4 +55,3 @@ def prepare_queries(self, cursor: object, schema: str): "core__soe_doc_period", ) ) - self.write_queries() diff --git a/cumulus_library/template_sql/object_denormalize.sql.jinja b/cumulus_library/template_sql/object_denormalize.sql.jinja index 2b489770..009c7cc0 100644 --- a/cumulus_library/template_sql/object_denormalize.sql.jinja +++ b/cumulus_library/template_sql/object_denormalize.sql.jinja @@ -1,4 +1,4 @@ -CREATE TABLE "{{schema}}"."{{ target_table }}" AS ( +CREATE TABLE "{{schema_name}}"."{{ target_table }}" AS ( SELECT {{ source_id }}, {%- for key in field_config.keys() %} diff --git a/cumulus_library/template_sql/templates.py b/cumulus_library/template_sql/templates.py index 0a2ce496..3a7943f0 100644 --- a/cumulus_library/template_sql/templates.py +++ b/cumulus_library/template_sql/templates.py @@ -278,6 +278,7 @@ def get_is_table_not_empty_query( unnests: Optional[list[dict]] = [], conditions: Optional[list[str]] = [], ): + """Checks for presence of data, allowing for unnesting or filtering""" path = Path(__file__).parent with open(f"{path}/is_table_not_empty.sql.jinja") as is_table_not_empty: return Template(is_table_not_empty.read()).render( @@ -288,6 +289,34 @@ def get_is_table_not_empty_query( ) +def get_object_denormalize_query( + schema_name: str, + source_table: str, + source_id: str, + field: str, + field_config: dict, + target_table: str, +): + """Generates a table by expanding a specified row element + + :param schema_name: The athena query to create the table in + :param table_name: The name of the athena table to create + :param table_cols: Comma deleniated column names, i.e. ['first','second'] + :param dataset: Array of data arrays to insert, i.e. [['1','3'],['2','4']] + + """ + path = Path(__file__).parent + with open(f"{path}/object_denormalize.sql.jinja") as column_datatype: + return Template(column_datatype.read()).render( + schema_name=schema_name, + source_table=source_table, + source_id=source_id, + field=field, + field_config=field_config, + target_table=target_table, + ) + + def get_show_tables(schema_name: str, prefix: str) -> str: """Generates a show tables query, filtered by prefix @@ -318,23 +347,3 @@ 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, - )