forked from mkabilov/pg2ch
/
ddl.go
198 lines (166 loc) · 5.64 KB
/
ddl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package replicator
import (
"fmt"
"os"
"strings"
"gopkg.in/yaml.v2"
"github.com/mkabilov/pg2ch/pkg/config"
"github.com/mkabilov/pg2ch/pkg/utils/chutils"
"github.com/mkabilov/pg2ch/pkg/utils/dbtypes"
"github.com/mkabilov/pg2ch/pkg/utils/tableinfo"
)
const (
pkColumnsFile = "pk_columns.yaml"
compressedColumnsFile = "compress_columns.yaml"
codecSuffix = " CODEC(Delta, LZ4)"
)
var (
orderByCols = make(map[string][]string)
compressColumns = map[string]struct{}{}
)
//GenerateChDDL generates ClickHouse table DDLs
//TODO: refactor me
func (r *Replicator) GenerateChDDL() error {
if _, err := os.Stat(pkColumnsFile); err == nil {
fp, err := os.Open(pkColumnsFile)
if err == nil {
if err := yaml.NewDecoder(fp).Decode(orderByCols); err != nil {
return fmt.Errorf("could not read primary keys file: %v", err)
}
fp.Close()
}
}
if _, err := os.Stat(compressedColumnsFile); err == nil {
fp, err := os.Open(compressedColumnsFile)
if err == nil {
if err := yaml.NewDecoder(fp).Decode(compressColumns); err != nil {
return fmt.Errorf("could not read primary keys file: %v", err)
}
fp.Close()
}
}
for key, val := range orderByCols {
newKey := r.cfg.ClickHouse.Database + "." + key + "_main"
delete(orderByCols, key)
orderByCols[newKey] = val
}
if err := r.pgDeltaConnect(); err != nil {
return fmt.Errorf("could not connect to pg: %v", err)
}
tx, err := r.pgBegin(r.pgDeltaConn)
if err != nil {
return fmt.Errorf("could not start transaction on pg side: %v", err)
}
processedTables := make(map[config.ChTableName]struct{})
for tblName := range r.cfg.Tables {
var (
pkColumnNumb int
engineParams string
orderBy string
)
tblCfg := r.cfg.Tables[tblName]
tblCfg.TupleColumns, tblCfg.PgColumns, err = tableinfo.TablePgColumns(tx, tblName)
if err != nil {
return fmt.Errorf("could not get columns for %s postgres table: %v", tblName.String(), err)
}
if len(tblCfg.Columns) == 0 {
tblCfg.Columns = make(map[string]string)
for _, pgCol := range tblCfg.TupleColumns {
tblCfg.Columns[pgCol.Name] = pgCol.Name
}
}
chColumnDDLs := make([]string, 0)
for _, tupleColumn := range tblCfg.TupleColumns {
chColName, ok := tblCfg.Columns[tupleColumn.Name]
if !ok {
continue
}
columnCfg, hasColumnCfg := tblCfg.ColumnProperties[tupleColumn.Name]
pgCol := tblCfg.PgColumns[tupleColumn.Name]
if hasColumnCfg && len(columnCfg.Coalesce) > 0 {
pgCol.IsNullable = false
}
chColType, err := chutils.ToClickHouseType(pgCol)
if err != nil {
return fmt.Errorf("could not get clickhouse column definition: %v", err)
}
if pgCol.PkCol > 0 && pgCol.PkCol > pkColumnNumb {
pkColumnNumb = pgCol.PkCol
}
if !hasColumnCfg && (pgCol.BaseType == dbtypes.PgAdjustIstore || pgCol.BaseType == dbtypes.PgAdjustBigIstore) {
hasColumnCfg = true
columnCfg = &config.ColumnProperty{
IstoreKeysSuffix: "keys",
IstoreValuesSuffix: "values",
}
}
if hasColumnCfg && columnCfg.IstoreKeysSuffix != "" {
chColumnDDLs = append(chColumnDDLs, fmt.Sprintf("%s_%s Array(Int32)", chColName, columnCfg.IstoreKeysSuffix))
if pgCol.BaseType == dbtypes.PgAdjustBigIstore {
chColumnDDLs = append(chColumnDDLs, fmt.Sprintf("%s_%s Array(Int64)", chColName, columnCfg.IstoreValuesSuffix))
} else {
chColumnDDLs = append(chColumnDDLs, fmt.Sprintf("%s_%s Array(Int32)", chColName, columnCfg.IstoreValuesSuffix))
}
continue
}
codec := ""
if _, ok := compressColumns[chColName]; ok {
codec = codecSuffix
}
chColumnDDLs = append(chColumnDDLs, fmt.Sprintf("%s %s%s", chColName, chColType, codec))
}
pkColumns := make([]string, pkColumnNumb)
var partitionColumn string
for pgColName := range tblCfg.Columns {
pgCol := tblCfg.PgColumns[pgColName]
if pgCol.PkCol < 1 {
continue
}
if pgCol.IsTime() && partitionColumn == "" {
partitionColumn = pgColName
}
pkColumns[pgCol.PkCol-1] = pgColName
}
if tblCfg.LsnColumnName != "" {
chColumnDDLs = append(chColumnDDLs, fmt.Sprintf("%s UInt64 CODEC(Delta, LZ4)", tblCfg.LsnColumnName))
}
switch tblCfg.Engine {
case config.ReplacingMergeTree:
chColumnDDLs = append(chColumnDDLs, fmt.Sprintf("%s UInt8", tblCfg.IsDeletedColumn))
case config.CollapsingMergeTree:
engineParams = tblCfg.SignColumn
chColumnDDLs = append(chColumnDDLs, fmt.Sprintf("%s Int8", engineParams))
}
tableDDL := fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s) Engine = %s(%s)",
tblCfg.ChMainTable,
strings.Join(chColumnDDLs, ", "),
tblCfg.Engine.String(),
engineParams)
if partitionColumn != "" {
tableDDL += fmt.Sprintf(" PARTITION BY toStartOfMonth(%s)", partitionColumn)
}
if ob, ok := orderByCols[tblCfg.ChMainTable.String()]; ok {
orderBy = fmt.Sprintf(" ORDER BY(%s)", strings.Join(ob, ", "))
} else if len(pkColumns) > 0 {
orderBy = fmt.Sprintf(" ORDER BY(%s)", strings.Join(pkColumns, ", "))
}
tableDDL += orderBy + ";"
if _, ok := processedTables[tblCfg.ChMainTable]; !ok {
fmt.Println(tableDDL)
processedTables[tblCfg.ChMainTable] = struct{}{}
}
if !tblCfg.ChSyncAuxTable.IsEmpty() {
if _, ok := processedTables[tblCfg.ChSyncAuxTable]; !ok {
columns := append(chColumnDDLs, fmt.Sprintf("%s UInt64", tblCfg.RowIDColumnName))
columns = append(columns, fmt.Sprintf("%s String", tblCfg.TableNameColumnName))
fmt.Printf("CREATE TABLE IF NOT EXISTS %s (%s) Engine = MergeTree() ORDER BY (%s) PARTITION BY (%s);\n",
tblCfg.ChSyncAuxTable,
strings.Join(columns, ", "),
tblCfg.LsnColumnName,
tblCfg.TableNameColumnName)
processedTables[tblCfg.ChSyncAuxTable] = struct{}{}
}
}
}
return nil
}