-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vdk-trino: optimize trino template implementation (#3380)
Simplified trino template implementation --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
1da6725
commit d7d35de
Showing
44 changed files
with
381 additions
and
594 deletions.
There are no files selected for viewing
1 change: 0 additions & 1 deletion
1
...ns/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/02-drop-backup-target.sql
This file was deleted.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
.../vdk/plugin/trino/templates/load/dimension/scd1/02-handle-quality-checks_and_move_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2023-2024 Broadcom | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import logging | ||
import os | ||
|
||
from vdk.api.job_input import IJobInput | ||
from vdk.plugin.trino.templates.data_quality_exception import DataQualityException | ||
from vdk.plugin.trino.trino_utils import CommonUtilities | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
SQL_FILES_FOLDER = ( | ||
os.path.dirname(os.path.abspath(__file__)) + "/02-requisite-sql-scripts" | ||
) | ||
|
||
""" | ||
This step is intened to handle quality checks if such provided | ||
and stop the data from being populated into the target table if the check has negative outcome. | ||
Otherwise the data will be directly processed according to the used template type | ||
""" | ||
|
||
|
||
def run(job_input: IJobInput): | ||
""" | ||
1. if check, | ||
- create staging table | ||
- Insert source view data to staging table | ||
- send staging table for check validation | ||
- If validated, | ||
- insert staging table data to target table | ||
- else Raise error | ||
2. else, | ||
- Move source view data into target table by replacing | ||
""" | ||
|
||
job_arguments = job_input.get_arguments() | ||
|
||
check = job_arguments.get("check") | ||
source_schema = job_arguments.get("source_schema") | ||
source_view = job_arguments.get("source_view") | ||
target_schema = job_arguments.get("target_schema") | ||
target_table = job_arguments.get("target_table") | ||
|
||
create_table_and_insert_data_query = CommonUtilities.get_file_content( | ||
SQL_FILES_FOLDER, "02-create-table-and-insert-data.sql" | ||
) | ||
drop_table_query = CommonUtilities.get_file_content( | ||
SQL_FILES_FOLDER, "02-drop-table.sql" | ||
) | ||
|
||
if check: | ||
staging_schema = job_arguments.get("staging_schema", target_schema) | ||
staging_table = CommonUtilities.get_staging_table_name( | ||
target_schema, target_table | ||
) | ||
staging_table_full_name = f"{staging_schema}.{staging_table}" | ||
target_table_full_name = f"{target_schema}.{target_table}" | ||
|
||
# drop table if exists | ||
drop_staging_table = drop_table_query.format( | ||
target_schema=staging_schema, target_table=staging_table | ||
) | ||
job_input.execute_query(drop_staging_table) | ||
|
||
# create staging table and insert data into staging table | ||
create_staging_table_and_insert_data = ( | ||
create_table_and_insert_data_query.format( | ||
table_schema=staging_schema, | ||
table_name=staging_table, | ||
target_schema=source_schema, | ||
target_table=source_view, | ||
) | ||
) | ||
job_input.execute_query(create_staging_table_and_insert_data) | ||
|
||
if check(staging_table_full_name): | ||
job_input.execute_query(drop_table_query) | ||
|
||
create_and_insert_into_target_table = ( | ||
create_table_and_insert_data_query.format( | ||
target_schema=target_schema, | ||
target_table=target_table, | ||
source_schema=staging_schema, | ||
source_view=staging_table, | ||
) | ||
) | ||
job_input.execute_query(create_and_insert_into_target_table) | ||
|
||
else: | ||
raise DataQualityException( | ||
checked_object=staging_table_full_name, | ||
source_view=f"{source_schema}.{source_view}", | ||
target_table=target_table_full_name, | ||
) | ||
|
||
else: | ||
job_input.execute_query(drop_table_query) | ||
job_input.execute_query(create_table_and_insert_data_query) |
2 changes: 2 additions & 0 deletions
2
...emplates/load/dimension/scd1/02-requisite-sql-scripts/02-create-table-and-insert-data.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
CREATE TABLE "{target_schema}"."{target_table}" AS | ||
SELECT * FROM "{source_schema}"."{source_view}" |
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion
1
...ugins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/03-drop-tmp-target.sql
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
...ins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/04-create-tmp-target.sql
This file was deleted.
Oops, something went wrong.
134 changes: 0 additions & 134 deletions
134
.../vdk/plugin/trino/templates/load/dimension/scd1/06-handle-quality-checks_and_move_data.py
This file was deleted.
Oops, something went wrong.
1 change: 0 additions & 1 deletion
1
...ugins/vdk-trino/src/vdk/plugin/trino/templates/load/fact/insert/02-drop-backup-target.sql
This file was deleted.
Oops, something went wrong.
130 changes: 130 additions & 0 deletions
130
...src/vdk/plugin/trino/templates/load/fact/insert/02-handle-quality-checks_and_move_data.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# Copyright 2023-2024 Broadcom | ||
# SPDX-License-Identifier: Apache-2.0 | ||
import logging | ||
import os | ||
|
||
from vdk.api.job_input import IJobInput | ||
from vdk.plugin.trino.templates.data_quality_exception import DataQualityException | ||
from vdk.plugin.trino.trino_utils import CommonUtilities | ||
|
||
SQL_FILES_FOLDER = ( | ||
os.path.dirname(os.path.abspath(__file__)) + "/02-requisite-sql-scripts" | ||
) | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
""" | ||
This step is intened to handle quality checks if such provided | ||
and stop the data from being populated into the target table if the check has negative outcome. | ||
Otherwise the data will be directly processed according to the used template type | ||
""" | ||
|
||
|
||
def run(job_input: IJobInput): | ||
""" | ||
1. if check, | ||
- create staging table | ||
- Insert source view data to staging table | ||
- Drop view if exists | ||
- create view from union of staging table data and target table data | ||
- send view for check validation | ||
- If validated, | ||
- Append staging table data to target table data | ||
- drop view | ||
- else Raise error | ||
2. else, | ||
- append source view data to target table using insert query | ||
""" | ||
job_arguments = job_input.get_arguments() | ||
|
||
check = job_arguments.get("check") | ||
source_schema = job_arguments.get("source_schema") | ||
source_view = job_arguments.get("source_view") | ||
target_schema = job_arguments.get("target_schema") | ||
target_table = job_arguments.get("target_table") | ||
create_table_query = CommonUtilities.get_file_content( | ||
SQL_FILES_FOLDER, "02-create-table.sql" | ||
) | ||
create_view_query = CommonUtilities.get_file_content( | ||
SQL_FILES_FOLDER, "02-create-consolidated-view.sql" | ||
) | ||
drop_table_query = CommonUtilities.get_file_content( | ||
SQL_FILES_FOLDER, "02-drop-table.sql" | ||
) | ||
drop_view_query = CommonUtilities.get_file_content( | ||
SQL_FILES_FOLDER, "02-drop-view.sql" | ||
) | ||
insert_query = CommonUtilities.get_file_content( | ||
SQL_FILES_FOLDER, "02-insert-into-target.sql" | ||
) | ||
|
||
if check: | ||
staging_schema = job_arguments.get("staging_schema", target_schema) | ||
staging_table = CommonUtilities.get_staging_table_name( | ||
target_schema, target_table | ||
) | ||
|
||
target_table_full_name = f"{target_schema}.{target_table}" | ||
|
||
# drop table if exists | ||
drop_staging_table = drop_table_query.format( | ||
target_schema=staging_schema, target_table=staging_table | ||
) | ||
job_input.execute_query(drop_staging_table) | ||
|
||
# create staging table | ||
create_staging_table = create_table_query.format( | ||
table_schema=staging_schema, | ||
table_name=staging_table, | ||
target_schema=target_schema, | ||
target_table=target_table, | ||
) | ||
job_input.execute_query(create_staging_table) | ||
|
||
# insert data into staging table | ||
insert_into_staging = insert_query.format( | ||
source_schema=source_schema, | ||
source_view=source_view, | ||
target_schema=staging_schema, | ||
target_table=staging_table, | ||
) | ||
job_input.execute_query(insert_into_staging) | ||
|
||
view_schema = staging_schema | ||
view_name = f"vw_{staging_table}" | ||
# Drop view if already exist | ||
drop_view = drop_view_query.format(view_schema=view_schema, view_name=view_name) | ||
job_input.execute_query(drop_view) | ||
|
||
# create consolidated view of source and target table data using staging table | ||
create_view = create_view_query.format( | ||
view_schema=view_schema, | ||
view_name=view_name, | ||
staging_schema=staging_schema, | ||
staging_table=staging_table, | ||
target_schema=target_schema, | ||
target_table=target_table, | ||
) | ||
job_input.execute_query(create_view) | ||
|
||
view_full_name = f"{view_schema}.{view_name}" | ||
|
||
if check(view_full_name): | ||
insert_into_target = insert_query.format( | ||
source_schema=staging_schema, | ||
source_view=staging_table, | ||
target_schema=target_schema, | ||
target_table=target_table, | ||
) | ||
job_input.execute_query(insert_into_target) | ||
# drop view | ||
job_input.execute_query(drop_view) | ||
|
||
else: | ||
raise DataQualityException( | ||
checked_object=view_full_name, | ||
source_view=f"{source_schema}.{source_view}", | ||
target_table=target_table_full_name, | ||
) | ||
else: | ||
job_input.execute_query(insert_query) |
3 changes: 3 additions & 0 deletions
3
...l-scripts/06-create-consolidated-view.sql → ...l-scripts/02-create-consolidated-view.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
CREATE VIEW {view_schema}.{view_name} | ||
AS | ||
( | ||
SELECT * | ||
FROM {target_schema}.{target_table} | ||
UNION ALL | ||
SELECT * | ||
FROM {staging_schema}.{staging_table} | ||
) |
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ension/scd1/05-insert-into-tmp-target.sql → ...ite-sql-scripts/02-insert-into-target.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/fact/insert/03-drop-tmp-target.sql
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
...lugins/vdk-trino/src/vdk/plugin/trino/templates/load/fact/insert/04-create-tmp-target.sql
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.