Skip to content

Instantly share code, notes, and snippets.

@azat
Last active April 8, 2020 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save azat/916b98b5ddf9573f7dc9a4dce33b59b5 to your computer and use it in GitHub Desktop.
Save azat/916b98b5ddf9573f7dc9a4dce33b59b5 to your computer and use it in GitHub Desktop.
#!/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