Last active
April 8, 2020 09:15
-
-
Save azat/916b98b5ddf9573f7dc9a4dce33b59b5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# reformat each SHOW CREATE query for multiline: | |
# https://github.com/ClickHouse/ClickHouse/pull/10049 | |
import sys | |
import os | |
import subprocess | |
def format_sql(sql, **kwargs): | |
read, write = os.pipe() | |
os.write(write, sql.encode()) | |
os.close(write) | |
sql = subprocess.check_output(['clickhouse-format'], stdin=read, **kwargs) | |
sql = sql.decode()[:-1] | |
return sql | |
def rewrite(line): | |
# - CREATE TABLE | |
# - CREATE MATERIALIZED VIEW | |
if not line.startswith('CREATE'): | |
return line | |
# 01036_no_superfluous_dict_reload_on_create_database* | |
if line == 'CREATE DATABASE': | |
return line | |
# FORMAT TabSeparatedRaw | |
# (and it looks very ugly in one line) | |
if 'test_00751.t_mv_00751' in line: | |
return format_sql(line) | |
try: | |
sql = line.replace("\\'", "'") | |
sql = format_sql(sql, stderr=subprocess.DEVNULL) | |
sql = sql.replace("'", "\\'") | |
sql = sql.replace("\n", "\\n") | |
except subprocess.CalledProcessError: # nested non default format -- 00642_cast.stdout | |
sql = format_sql(line) | |
return sql | |
it = map(lambda x: x.rstrip('\n'), sys.stdin) | |
it = map(rewrite, it) | |
it = map(print, it) | |
any(it) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment