Skip to content

Commit 2d806dc

Browse files
authored
bugfix: deduplicate account map (#417)
Thanks to Andrea Caldarone for the SQL Optimization
1 parent 0aca9cd commit 2d806dc

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CREATE OR REPLACE VIEW ${athena_view_name} AS
2+
SELECT DISTINCT
3+
line_item_usage_account_id account_id,
4+
MAX_BY(bill_payer_account_id, line_item_usage_start_date) parent_account_id,
5+
MAX_BY(line_item_usage_account_id, line_item_usage_start_date) account_name,
6+
MAX_BY(line_item_usage_account_id, line_item_usage_start_date) account_email_id
7+
FROM
8+
"${cur_table_name}"
9+
GROUP BY
10+
line_item_usage_account_id

cid/helpers/account_map.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,24 +156,16 @@ def create(self, name) -> bool:
156156
def get_dummy_account_mapping_sql(self, name) -> list:
157157
"""Create dummy account mapping"""
158158
logger.info(f'Creating dummy account mapping for {name}')
159-
template_str = '''CREATE OR REPLACE VIEW ${athena_view_name} AS SELECT DISTINCT
160-
line_item_usage_account_id account_id, bill_payer_account_id parent_account_id,
161-
line_item_usage_account_id account_name, line_item_usage_account_id account_email_id
162-
FROM
163-
"${cur_table_name}"
164-
'''
165-
template = Template(template_str)
166-
# Fill in TPLs
167-
columns_tpl = dict()
168-
parameters = {
159+
template = Template(resource_string(
160+
package_or_requirement='cid.builtin.core',
161+
resource_name='data/queries/shared/account_map_dummy.sql',
162+
).decode('utf-8'))
163+
columns_tpl = {
169164
'athena_view_name': name,
170165
'cur_table_name': self.cur.tableName
171166
}
172-
columns_tpl.update(**parameters)
173167
compiled_query = template.safe_substitute(columns_tpl)
174-
175168
return compiled_query
176-
177169

178170
def get_organization_accounts(self) -> list:
179171
""" Retreive AWS Organization account """

0 commit comments

Comments
 (0)