Last active
October 23, 2024 10:50
-
-
Save AlexeyKupershtokh/82d04095a275e741785dcd6e6436643f to your computer and use it in GitHub Desktop.
Postgres DDL to Clickhouse converter
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
select | |
concat( | |
'create table ', | |
table_name, | |
'(', | |
string_agg( | |
concat( | |
column_name, | |
' ', | |
CASE when is_nullable = 'YES' THEN 'Nullable(' END, | |
CASE | |
when data_type = 'integer' then 'Int32' | |
when data_type = 'smallint' then 'Int16' | |
when data_type = 'bigint' then 'Int64' | |
when data_type = 'numeric' | |
then concat('Decimal(', numeric_precision, ',', numeric_scale, ')') | |
when data_type = 'double precision' then 'Float64' | |
when data_type = 'character varying' then 'String' | |
when data_type = 'character' then 'String' | |
when data_type = 'text' then 'String' | |
when data_type = 'boolean' then 'UInt8' | |
when data_type = 'date' then 'Date' | |
when data_type = 'timestamp without time zone' then 'Datetime' | |
when data_type = 'time without time zone' then 'String' | |
else concat('###################', data_type) | |
END, | |
CASE when is_nullable = 'YES' THEN ')' END | |
), | |
', ' order by ordinal_position | |
), | |
') ', | |
'ENGINE = MergeTree() ' | |
'PARTITION BY id ' | |
'ORDER BY [id] ' | |
) as table_ddl | |
from information_schema.columns | |
where true | |
and table_catalog = 'unbiased_accounts' | |
and table_schema = 'public' | |
group by table_name; | |
select * from information_schema.columns | |
where true | |
and table_catalog = 'unbiased_accounts' | |
and table_schema = 'public' | |
--and data_type = 'time without time zone' | |
; | |
select distinct data_type from information_schema.columns | |
where true | |
and table_catalog = 'unbiased_accounts' | |
and table_schema = 'public'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment