Skip to content

Commit d7d35de

Browse files
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>
1 parent 1da6725 commit d7d35de

44 files changed

Lines changed: 381 additions & 594 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

projects/vdk-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/02-drop-backup-target.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Copyright 2023-2024 Broadcom
2+
# SPDX-License-Identifier: Apache-2.0
3+
import logging
4+
import os
5+
6+
from vdk.api.job_input import IJobInput
7+
from vdk.plugin.trino.templates.data_quality_exception import DataQualityException
8+
from vdk.plugin.trino.trino_utils import CommonUtilities
9+
10+
log = logging.getLogger(__name__)
11+
12+
SQL_FILES_FOLDER = (
13+
os.path.dirname(os.path.abspath(__file__)) + "/02-requisite-sql-scripts"
14+
)
15+
16+
"""
17+
This step is intened to handle quality checks if such provided
18+
and stop the data from being populated into the target table if the check has negative outcome.
19+
Otherwise the data will be directly processed according to the used template type
20+
"""
21+
22+
23+
def run(job_input: IJobInput):
24+
"""
25+
1. if check,
26+
- create staging table
27+
- Insert source view data to staging table
28+
- send staging table for check validation
29+
- If validated,
30+
- insert staging table data to target table
31+
- else Raise error
32+
2. else,
33+
- Move source view data into target table by replacing
34+
"""
35+
36+
job_arguments = job_input.get_arguments()
37+
38+
check = job_arguments.get("check")
39+
source_schema = job_arguments.get("source_schema")
40+
source_view = job_arguments.get("source_view")
41+
target_schema = job_arguments.get("target_schema")
42+
target_table = job_arguments.get("target_table")
43+
44+
create_table_and_insert_data_query = CommonUtilities.get_file_content(
45+
SQL_FILES_FOLDER, "02-create-table-and-insert-data.sql"
46+
)
47+
drop_table_query = CommonUtilities.get_file_content(
48+
SQL_FILES_FOLDER, "02-drop-table.sql"
49+
)
50+
51+
if check:
52+
staging_schema = job_arguments.get("staging_schema", target_schema)
53+
staging_table = CommonUtilities.get_staging_table_name(
54+
target_schema, target_table
55+
)
56+
staging_table_full_name = f"{staging_schema}.{staging_table}"
57+
target_table_full_name = f"{target_schema}.{target_table}"
58+
59+
# drop table if exists
60+
drop_staging_table = drop_table_query.format(
61+
target_schema=staging_schema, target_table=staging_table
62+
)
63+
job_input.execute_query(drop_staging_table)
64+
65+
# create staging table and insert data into staging table
66+
create_staging_table_and_insert_data = (
67+
create_table_and_insert_data_query.format(
68+
table_schema=staging_schema,
69+
table_name=staging_table,
70+
target_schema=source_schema,
71+
target_table=source_view,
72+
)
73+
)
74+
job_input.execute_query(create_staging_table_and_insert_data)
75+
76+
if check(staging_table_full_name):
77+
job_input.execute_query(drop_table_query)
78+
79+
create_and_insert_into_target_table = (
80+
create_table_and_insert_data_query.format(
81+
target_schema=target_schema,
82+
target_table=target_table,
83+
source_schema=staging_schema,
84+
source_view=staging_table,
85+
)
86+
)
87+
job_input.execute_query(create_and_insert_into_target_table)
88+
89+
else:
90+
raise DataQualityException(
91+
checked_object=staging_table_full_name,
92+
source_view=f"{source_schema}.{source_view}",
93+
target_table=target_table_full_name,
94+
)
95+
96+
else:
97+
job_input.execute_query(drop_table_query)
98+
job_input.execute_query(create_table_and_insert_data_query)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CREATE TABLE "{target_schema}"."{target_table}" AS
2+
SELECT * FROM "{source_schema}"."{source_view}"

projects/vdk-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/06-requisite-sql-scripts/06-create-table.sql renamed to projects/vdk-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/02-requisite-sql-scripts/02-create-table.sql

File renamed without changes.

projects/vdk-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/06-requisite-sql-scripts/06-drop-table.sql renamed to projects/vdk-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/02-requisite-sql-scripts/02-drop-table.sql

File renamed without changes.

projects/vdk-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/03-drop-tmp-target.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

projects/vdk-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/04-create-tmp-target.sql

Lines changed: 0 additions & 3 deletions
This file was deleted.

projects/vdk-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/dimension/scd1/06-handle-quality-checks_and_move_data.py

Lines changed: 0 additions & 134 deletions
This file was deleted.

projects/vdk-plugins/vdk-trino/src/vdk/plugin/trino/templates/load/fact/insert/02-drop-backup-target.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Copyright 2023-2024 Broadcom
2+
# SPDX-License-Identifier: Apache-2.0
3+
import logging
4+
import os
5+
6+
from vdk.api.job_input import IJobInput
7+
from vdk.plugin.trino.templates.data_quality_exception import DataQualityException
8+
from vdk.plugin.trino.trino_utils import CommonUtilities
9+
10+
SQL_FILES_FOLDER = (
11+
os.path.dirname(os.path.abspath(__file__)) + "/02-requisite-sql-scripts"
12+
)
13+
14+
log = logging.getLogger(__name__)
15+
16+
"""
17+
This step is intened to handle quality checks if such provided
18+
and stop the data from being populated into the target table if the check has negative outcome.
19+
Otherwise the data will be directly processed according to the used template type
20+
"""
21+
22+
23+
def run(job_input: IJobInput):
24+
"""
25+
1. if check,
26+
- create staging table
27+
- Insert source view data to staging table
28+
- Drop view if exists
29+
- create view from union of staging table data and target table data
30+
- send view for check validation
31+
- If validated,
32+
- Append staging table data to target table data
33+
- drop view
34+
- else Raise error
35+
2. else,
36+
- append source view data to target table using insert query
37+
"""
38+
job_arguments = job_input.get_arguments()
39+
40+
check = job_arguments.get("check")
41+
source_schema = job_arguments.get("source_schema")
42+
source_view = job_arguments.get("source_view")
43+
target_schema = job_arguments.get("target_schema")
44+
target_table = job_arguments.get("target_table")
45+
create_table_query = CommonUtilities.get_file_content(
46+
SQL_FILES_FOLDER, "02-create-table.sql"
47+
)
48+
create_view_query = CommonUtilities.get_file_content(
49+
SQL_FILES_FOLDER, "02-create-consolidated-view.sql"
50+
)
51+
drop_table_query = CommonUtilities.get_file_content(
52+
SQL_FILES_FOLDER, "02-drop-table.sql"
53+
)
54+
drop_view_query = CommonUtilities.get_file_content(
55+
SQL_FILES_FOLDER, "02-drop-view.sql"
56+
)
57+
insert_query = CommonUtilities.get_file_content(
58+
SQL_FILES_FOLDER, "02-insert-into-target.sql"
59+
)
60+
61+
if check:
62+
staging_schema = job_arguments.get("staging_schema", target_schema)
63+
staging_table = CommonUtilities.get_staging_table_name(
64+
target_schema, target_table
65+
)
66+
67+
target_table_full_name = f"{target_schema}.{target_table}"
68+
69+
# drop table if exists
70+
drop_staging_table = drop_table_query.format(
71+
target_schema=staging_schema, target_table=staging_table
72+
)
73+
job_input.execute_query(drop_staging_table)
74+
75+
# create staging table
76+
create_staging_table = create_table_query.format(
77+
table_schema=staging_schema,
78+
table_name=staging_table,
79+
target_schema=target_schema,
80+
target_table=target_table,
81+
)
82+
job_input.execute_query(create_staging_table)
83+
84+
# insert data into staging table
85+
insert_into_staging = insert_query.format(
86+
source_schema=source_schema,
87+
source_view=source_view,
88+
target_schema=staging_schema,
89+
target_table=staging_table,
90+
)
91+
job_input.execute_query(insert_into_staging)
92+
93+
view_schema = staging_schema
94+
view_name = f"vw_{staging_table}"
95+
# Drop view if already exist
96+
drop_view = drop_view_query.format(view_schema=view_schema, view_name=view_name)
97+
job_input.execute_query(drop_view)
98+
99+
# create consolidated view of source and target table data using staging table
100+
create_view = create_view_query.format(
101+
view_schema=view_schema,
102+
view_name=view_name,
103+
staging_schema=staging_schema,
104+
staging_table=staging_table,
105+
target_schema=target_schema,
106+
target_table=target_table,
107+
)
108+
job_input.execute_query(create_view)
109+
110+
view_full_name = f"{view_schema}.{view_name}"
111+
112+
if check(view_full_name):
113+
insert_into_target = insert_query.format(
114+
source_schema=staging_schema,
115+
source_view=staging_table,
116+
target_schema=target_schema,
117+
target_table=target_table,
118+
)
119+
job_input.execute_query(insert_into_target)
120+
# drop view
121+
job_input.execute_query(drop_view)
122+
123+
else:
124+
raise DataQualityException(
125+
checked_object=view_full_name,
126+
source_view=f"{source_schema}.{source_view}",
127+
target_table=target_table_full_name,
128+
)
129+
else:
130+
job_input.execute_query(insert_query)

0 commit comments

Comments
 (0)