Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
244 lines (189 loc) · 6.61 KB
/
Copy pathapp.py
File metadata and controls
244 lines (189 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import streamlit as st
import pandas as pd
from io import StringIO
st.set_page_config(
page_title="DE Copilot - Enterprise STTM Factory",
page_icon="🛠️",
layout="wide"
)
st.title("🛠️ DE Copilot - Enterprise STTM Factory")
st.subheader("Transform Retail STTM into Snowflake DDL, SQL, Data Dictionary, Technical Specs and DQ Rules")
uploaded_file = st.file_uploader(
"Upload Retail Enterprise STTM",
type=["csv"]
)
REQUIRED_COLUMNS = [
"Source_Table",
"Source_Column",
"Target_Table",
"Target_Column",
"Data_Type",
"Nullable",
"Primary_Key_Flag",
"Audit_Flag",
"Business_Definition",
"Transformation_Rule",
"DQ_Rule"
]
def validate_sttm(df):
missing = [col for col in REQUIRED_COLUMNS if col not in df.columns]
return missing
def generate_ddl(df):
target_table = df["Target_Table"].iloc[0]
lines = [f"CREATE OR REPLACE TABLE {target_table} ("]
column_defs = []
for _, row in df.iterrows():
nullable = "NOT NULL" if str(row["Nullable"]).upper() == "N" else ""
column_defs.append(
f" {row['Target_Column']} {row['Data_Type']} {nullable}".strip()
)
lines.append(",\n".join(column_defs))
lines.append(");")
return "\n".join(lines)
def transform_expression(row):
source_col = row["Source_Column"]
target_col = row["Target_Column"]
rule = str(row["Transformation_Rule"]).lower()
if str(row["Source_Table"]).upper() == "SYSTEM":
return f"CURRENT_TIMESTAMP() AS {target_col}"
if "lowercase" in rule and "trim" in rule:
return f"LOWER(TRIM({source_col})) AS {target_col}"
if "trim" in rule:
return f"TRIM({source_col}) AS {target_col}"
if "uppercase" in rule:
return f"UPPER({source_col}) AS {target_col}"
if "date" in rule:
return f"TO_DATE({source_col}) AS {target_col}"
if "remove spaces and hyphens" in rule:
return f"REPLACE(REPLACE({source_col}, ' ', ''), '-', '') AS {target_col}"
return f"{source_col} AS {target_col}"
def generate_sql(df):
target_table = df["Target_Table"].iloc[0]
source_table = df[df["Source_Table"].str.upper() != "SYSTEM"]["Source_Table"].iloc[0]
expressions = [transform_expression(row) for _, row in df.iterrows()]
sql = f"INSERT INTO {target_table}\nSELECT\n"
sql += ",\n".join([f" {expr}" for expr in expressions])
sql += f"\nFROM {source_table};"
return sql
def generate_data_dictionary(df):
return df[[
"Target_Column",
"Data_Type",
"Nullable",
"Primary_Key_Flag",
"Business_Definition"
]]
def generate_technical_spec(df):
spec = "# Technical Mapping Specification\n\n"
for _, row in df.iterrows():
spec += f"## {row['Target_Column']}\n\n"
spec += f"- Source Table: {row['Source_Table']}\n"
spec += f"- Source Column: {row['Source_Column']}\n"
spec += f"- Target Table: {row['Target_Table']}\n"
spec += f"- Target Column: {row['Target_Column']}\n"
spec += f"- Data Type: {row['Data_Type']}\n"
spec += f"- Nullable: {row['Nullable']}\n"
spec += f"- Primary Key: {row['Primary_Key_Flag']}\n"
spec += f"- Audit Field: {row['Audit_Flag']}\n"
spec += f"- Business Definition: {row['Business_Definition']}\n"
spec += f"- Transformation Rule: {row['Transformation_Rule']}\n"
spec += f"- DQ Rule: {row['DQ_Rule']}\n\n"
return spec
def generate_dq_rules(df):
target_table = df["Target_Table"].iloc[0]
dq_sql = []
dq_sql.append(f"-- Data Quality Rules for {target_table}\n")
for _, row in df.iterrows():
col = row["Target_Column"]
dq_rule = str(row["DQ_Rule"]).upper()
pk = str(row["Primary_Key_Flag"]).upper()
nullable = str(row["Nullable"]).upper()
if nullable == "N" or "NOT NULL" in dq_rule:
dq_sql.append(f"""
-- NOT NULL Check: {col}
SELECT
'{col}' AS column_name,
COUNT(*) AS failed_count
FROM {target_table}
WHERE {col} IS NULL;
""")
if pk == "Y":
dq_sql.append(f"""
-- Duplicate Check: {col}
SELECT
{col},
COUNT(*) AS record_count
FROM {target_table}
GROUP BY {col}
HAVING COUNT(*) > 1;
""")
if "VALID EMAIL" in dq_rule:
dq_sql.append(f"""
-- Email Format Check: {col}
SELECT
{col}
FROM {target_table}
WHERE {col} IS NOT NULL
AND NOT REGEXP_LIKE({col}, '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{{2,}}$');
""")
if "VALID DATE" in dq_rule:
dq_sql.append(f"""
-- Future Date Check: {col}
SELECT
{col}
FROM {target_table}
WHERE {col} > CURRENT_DATE();
""")
if "VALID CODE" in dq_rule:
dq_sql.append(f"""
-- Valid Code Check: {col}
-- Replace values below with approved business reference values
SELECT
{col}
FROM {target_table}
WHERE {col} IS NOT NULL
AND {col} NOT IN ('BRONZE', 'SILVER', 'GOLD', 'PLATINUM');
""")
return "\n".join(dq_sql)
if uploaded_file:
df = pd.read_csv(uploaded_file)
st.success("STTM Uploaded Successfully")
missing_columns = validate_sttm(df)
if missing_columns:
st.error("Missing required columns:")
st.write(missing_columns)
st.stop()
st.subheader("Uploaded STTM")
st.dataframe(df, use_container_width=True)
if st.button("🏗️ Generate Data Product Assets"):
ddl = generate_ddl(df)
sql = generate_sql(df)
dictionary = generate_data_dictionary(df)
tech_spec = generate_technical_spec(df)
dq_rules = generate_dq_rules(df)
tab1, tab2, tab3, tab4, tab5 = st.tabs([
"Snowflake DDL",
"Snowflake SQL",
"Data Dictionary",
"Technical Spec",
"DQ Rules"
])
with tab1:
st.code(ddl, language="sql")
st.download_button("Download DDL", ddl, file_name="snowflake_ddl.sql")
with tab2:
st.code(sql, language="sql")
st.download_button("Download SQL", sql, file_name="snowflake_insert.sql")
with tab3:
st.dataframe(dictionary, use_container_width=True)
csv = dictionary.to_csv(index=False)
st.download_button("Download Data Dictionary", csv, file_name="data_dictionary.csv")
with tab4:
st.markdown(tech_spec)
st.download_button("Download Technical Spec", tech_spec, file_name="technical_spec.md")
with tab5:
st.code(dq_rules, language="sql")
st.download_button("Download DQ Rules", dq_rules, file_name="dq_rules.sql")
st.success("Data Product Assets Generated Successfully")
else:
st.info("Upload an STTM CSV to begin.")