get_query_table_aliases(query) is far from being final
See original GitHub issueWhile I was testing on Oracle SQL:
- missed LEFT JOINs (it was easy to add to the code)
- missed table names starting with schema name (SCHEMA.TABLE)
- missed aliases without AS (this is mentioned in the code but not handled)
- missed recognizing subselects (e.g. FROM (SELECT … ) )
I touched the code to overcome these issues and found solution for them. Tested with a huge SQL but may need to do some more testing.
New code:
def get_query_table_aliases(query: str) -> Dict[str, str]:
"""
Returns tables aliases mapping from a given query
E.g. SELECT a.* FROM users1 AS a JOIN users2 AS b ON a.ip_address = b.ip_address
will give you {'a': 'users1', 'b': 'users2'}
"""
aliases = dict()
last_keyword_token = None
last_table_name = None
for token in get_query_tokens(query):
# print(token.ttype, token, last_table_name)
# handle "FROM foo alias" syntax (i.e, "AS" keyword is missing)
# if last_table_name and token.ttype is Name:
# aliases[token.value] = last_table_name
# last_table_name = False
if last_table_name:
if token.value=='.':
last_table_name = last_table_name + token.value #add the dot
if token.value==',' or token.is_keyword and token.value.upper()!='AS': #there is no alias
aliases[''] = last_table_name
last_table_name = False
if prev_token.value.upper()=='AS': #previous keyword was AS then we found the alias
aliases[token.value] = last_table_name
last_table_name = False
if token.ttype is Name:
if prev_token.value=='.':
last_table_name = last_table_name + token.value #add Name to last_table_name
else: #found alias
aliases[token.value] = last_table_name
last_table_name = False
if last_keyword_token:
if last_keyword_token.value.upper() in ["FROM", "JOIN", "INNER JOIN","LEFT JOIN"] and token.value!='(':
last_table_name = token.value
last_keyword_token = token if token.is_keyword else False
prev_token = token
return aliases
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
SQL Alias: A Guide to the SQL Aliases and the SQL AS Keyword
Table aliases allow you to name your table for use in other parts of your query, such as the SELECT or WHERE clauses....
Read more >How to extract all table names and aliases from Tsql select ...
One suggestion was to create a view or stored procedure containing the query and use sys.sql_dependencies on the new view/SP to get the ......
Read more >How to Use Aliases in SQL Queries - LearnSQL.com
There are several ways to use aliases in your SQL queries. This article shows you how, using simple explanations and helpful examples.
Read more >SQL AS keyword overview and examples - SQLShack
This article explains the SQL AS keyword that is used to assign an alias for columns and table names in SQL Server.
Read more >How to use aliases in SQL - Educative.io
Aliases in SQL are names that you can allocate to a table or table column for use in a single SQL query. In...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
SQL copied in looking awful, sorry. Also tried to copy it in as ‘code’.
So when I ran the modified module on the above SQL I got this dictionary:
{'C': 'EBH_DM_CRM.CRM_CUSTOMER', 'RB_ACCT': 'KMR_FINANCIAL_INDEX_STAGE_01', 'OD_FAC': 'KMR_FINANCIAL_INDEX_STAGE_07', 'CARD': 'KMR_FINANCIAL_INDEX_STAGE_02', 'CL_LOAN': 'KMR_FINANCIAL_INDEX_STAGE_03', 'CL_INV': 'KMR_FINANCIAL_INDEX_STAGE_05', 'BEF_ZRT': 'KMR_FINANCIAL_INDEX_STAGE_04', 'AR': 'SCHEMA.CRM_ARRANGEMENT', 'L': 'SCHEMA.CRM_LOAN', 'CF': 'SCHEMA.CRM_CASH_FLOW', 'CA': 'SCHEMA.CRM_REL_CUSTOMER_ARRANGEMENT', 'LPRE': 'SCHEMA.CRM_LOAN', 'P': 'SCHEMA.CRM_PRODUCT', 'PD': 'SCHEMA.CRM_PAST_DUE', 'DAT': 'SCHEMA.CRM_DATE', 'BLL': 'SCHEMA.CRM_BALANCE_LOAN_LOAN', 'E': 'SCHEMA.CRM_EXCHANGE_RATE', 'LE': 'SCHEMA.CRM_LEASING', 'BLE': 'SCHEMA.CRM_BALANCE_LEASING', 'SCB': 'SCHEMA.CRM_RL_SUBSIDIARY_COMPANIE_BAT', 'SZCH': 'KMR_FINANCIAL_INDEX_STAGE_06'}
Which I checked and looks correct.
this was the test py file:
@macbre , I added a pull request. I don’t know where to share the test sql. Let me paste it here. SELECT *
FROM EBH_DM_CRM.CRM_CUSTOMER C
LEFT JOIN KMR_FINANCIAL_INDEX_STAGE_01 RB_ACCT ON RB_ACCT.CUSTOMER_ID = C.CUSTOMER_ID
LEFT JOIN KMR_FINANCIAL_INDEX_STAGE_07 OD_FAC ON OD_FAC.CUSTOMER_ID = C.CUSTOMER_ID
LEFT JOIN KMR_FINANCIAL_INDEX_STAGE_02 CARD ON CARD.CUSTOMER_ID = C.CUSTOMER_ID
LEFT JOIN KMR_FINANCIAL_INDEX_STAGE_03 CL_LOAN ON CL_LOAN.CUSTOMER_ID = C.CUSTOMER_ID
LEFT JOIN KMR_FINANCIAL_INDEX_STAGE_05 CL_INV ON CL_INV.CUSTOMER_ID = C.CUSTOMER_ID
LEFT JOIN KMR_FINANCIAL_INDEX_STAGE_04 BEF_ZRT ON BEF_ZRT.CUSTOMER_SYMBOLS_SID = C.SYMBOLS_CUSTOMER_SID
LEFT JOIN (SELECT CA.CUSTOMER_ID, SUM(CASE WHEN P.KMR_TYPE_GROUP IN (‘CRED_MORTG’, ‘CRED_HOUSE_NORM’, ‘CRED_HOUSE_SUBV’) AND LPRE.RELATED_LOAN IS NULL AND L.ROTATION_CODE = ‘N’ AND CF.INVOICE_TYPE = ‘PRI’ THEN CF.PAID_AMT ELSE 0 END) AS FIN_CREDIT_MORTGAGE_AP_AMT, SUM(CASE WHEN P.KMR_TYPE_GROUP IN (‘CRED_PERS’, ‘CRED_OTHER_LOMB’, ‘CRED_OTHER_GAR’, ‘CRED_OTHER_GAR_FRPR’, ‘CRED_OTHER_INVEST’, ‘CRED_OTHER_CURR’, ‘CRED_OTHER_SUBVINV’, ‘CRED_OTHER_OTHER’) AND LPRE.RELATED_LOAN IS NULL AND L.ROTATION_CODE = ‘N’ AND CF.INVOICE_TYPE = ‘PRI’ THEN CF.PAID_AMT ELSE 0 END) AS FIN_CREDIT_SHORT_AP_AMT FROM SCHEMA.CRM_ARRANGEMENT AR
LEFT JOIN (SELECT PD.CUSTOMER_ID, CASE WHEN MAX(PD.DAYS_PAST_DUE) > 30 THEN MAX(PD.DAYS_PAST_DUE) - 30 ELSE 0 END AS EXP_CRED_DAYS_OVR_30_DAYS, SUM(CASE WHEN PD.DAYS_PAST_DUE > 30 THEN PD.PAST_DUE_AMOUNT ELSE 0 END) AS EXP_CRED_HUF_OVR_30_DAYS_AMT, CASE WHEN MAX(PD.DAYS_PAST_DUE) > 90 THEN MAX(PD.DAYS_PAST_DUE) - 90 ELSE 0 END AS EXP_CRED_DAYS_OVR_90_DAYS, SUM(CASE WHEN PD.DAYS_PAST_DUE > 90 THEN PD.PAST_DUE_AMOUNT ELSE 0 END) AS EXP_CRED_HUF_OVR_90_DAYS_AMT, MAX(PD.DAYS_PAST_DUE) AS EXP_CRED_DAYS, SUM(PD.PAST_DUE_AMOUNT) AS EXP_CRED_AMT
LEFT JOIN (SELECT BLL.CUSTOMER_ID, SUM(CASE WHEN P.TMO_HIER_LEVEL_1 = ‘HITEL’ AND P.TMO_HIER_LEVEL_2 = ‘MIKROVÃLLALATI HITEL’ AND (P.TMO_HIER_LEVEL_4 = ‘FAKTOR’ OR P.TMO_HIER_LEVEL_3 = ‘KÃNYSZERHITEL’ OR AR.ESTIMATED_END_DATE - L.FIRST_DISBURSEMENT_DATE <= 365) A ND AR.ESTIMATED_END_DATE > DAT.CALENDAR_MONTH_LAST_WORKDAY THEN NVL(BLL.OUTSTANDING_AMT * E.EXCHANGE_RATE_VALUE, 0) ELSE 0 END) AS FIN_CRED_OTH_SHORT_BAL,
LEFT JOIN (SELECT BLE.CUSTOMER_ID, SUM(BLE.FUTURE_CAPITAL) AS FI_CRED_LEASING_BAL, COUNT(LE.LEASING_ID) AS PR_CRED_LEASING_CNT
LEFT JOIN (SELECT SCB.SYMBOLS_ID, COUNT(DISTINCT SCB.CONTRACT_ID) AS BPR_INS_LIFE_CNT, SUM(SCB.AMOUNT) AS FIN_INS_LIFE_BAL FROM SCHEMA.CRM_RL_SUBSIDIARY_COMPANIE_BAT SCB WHERE SCB.SOURCE_TCH = ‘BIZTOSITO’ AND SCB.EFFECTIVE_LOAD_DATE = &GLOBAL_P_EFFECTIVE_LOAD_DATE GROUP BY SCB.SYMBOLS_ID) LIFE_INSUR ON LIFE_INSUR.SYMBOLS_ID = C.SYMBOLS_CUSTOMER_SID
LEFT JOIN (SELECT PD.CUSTOMER_ID, MAX(PD.DAYS_PAST_DUE) AS EXP_CRED_DAYS_MAX, ROUND(SUM(PD.PAST_DUE_AMOUNT)) AS EXP_CRED_AMT_MAX
LEFT JOIN KMR_FINANCIAL_INDEX_STAGE_06 SZCH ON SZCH.CUSTOMER_ID = C.CUSTOMER_ID
WHERE 1 = 1 AND C.START_OF_VALIDITY <= DATE’2020-01-01’ AND C.END_OF_VALIDITY > DATE’2020-01-01’