Skip to content

Instantly share code, notes, and snippets.

@Hosuke
Created June 13, 2023 11:41
Show Gist options
  • Save Hosuke/dfdab9cd571ee133afe54ec84ea8a4f3 to your computer and use it in GitHub Desktop.
Save Hosuke/dfdab9cd571ee133afe54ec84ea8a4f3 to your computer and use it in GitHub Desktop.
import os
import re
# Get the current directory
dir_path = os.getcwd()
pattern = re.compile(r'LEFT ANTI JOIN (\w+) t ON (t.\w+) = (\w+.\w+)')
def replace_anti_join(match):
table_name = match.group(1)
join_col = match.group(2)
target_col = match.group(3)
return f'LEFT JOIN {table_name} t ON {join_col} = {target_col} WHERE {join_col} IS NULL'
# Traverse all files and subdirectories in the current directory
for root, dirs, files in os.walk(dir_path):
for file_name in files:
# Rewrite TIMESTAMP()/timestamp() in the file content
with open(os.path.join(root, file_name), 'r') as f:
content = f.read()
updated_query = pattern.sub(replace_anti_join, content)
with open(os.path.join(root, file_name), 'w') as f:
f.write(updated_query)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment