Skip to content

Commit

Permalink
- remove extra backticks from rank
Browse files Browse the repository at this point in the history
- edit fix_ddl_tsql, fix_ddl_sqlite
  • Loading branch information
wendy-aw committed Jun 24, 2024
1 parent e5f2fa1 commit c7e30b1
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion utils_dialects.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def fix_ddl_mysql(translated_ddl):
)
reserved_keywords = [
"long",
"rank",
]
for keyword in reserved_keywords:
translated_ddl = re.sub(
Expand Down Expand Up @@ -178,6 +177,32 @@ def replace_interval(match):
lambda match: f"{int(match.group(1)) * 7} days",
translated_ddl,
)
# replace EXTRACT(YEAR FROM with STRFTIME('%Y',
translated_ddl = re.sub(
r"EXTRACT\(YEAR FROM ([^)]+)\)",
lambda match: f"STRFTIME('%Y', {match.group(1)})",
translated_ddl,
)
# replace STRFTIME('%m', DATE('now', '-x months')) with calculated month using python
# SQLite cannot extract name of month
def replace_strftime_month(translated_ddl):
pattern = r"STRFTIME\('Month', DATE\('now', '-(\d+) months'\)\)"
# get the digits as a list
matches = re.findall(pattern, translated_ddl)
import datetime
from dateutil.relativedelta import relativedelta
current_date = datetime.datetime.now()
current_month_name = current_date.strftime('%B')
for x in matches:
new_current_date = current_date - relativedelta(months=int(x))
new_current_month_name = new_current_date.strftime('%B')
# replace whole pattern with month name
translated_ddl = translated_ddl.replace(
f"STRFTIME('Month', DATE('now', '-{x} months'))", f"'{new_current_month_name}'"
)
return translated_ddl
translated_ddl = replace_strftime_month(translated_ddl)

return translated_ddl


Expand Down Expand Up @@ -241,6 +266,12 @@ def replace_timestamp_trunc(match):
r"DATEDIFF(SECOND, '1970-01-01', \1)",
translated_ddl,
)
# replace 'Month' with 'MMMM' for full month name
translated_ddl = re.sub(
r"'Month'",
r"'MMMM'",
translated_ddl,
)
return translated_ddl


Expand Down

0 comments on commit c7e30b1

Please sign in to comment.