-
Notifications
You must be signed in to change notification settings - Fork 15
/
dialect_clickhouse.go
271 lines (239 loc) · 8.15 KB
/
dialect_clickhouse.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package db
import (
"context"
"fmt"
"math/big"
"reflect"
"sort"
"strconv"
"strings"
"time"
_ "github.com/ClickHouse/clickhouse-go/v2"
"github.com/streamingfast/cli"
sink "github.com/streamingfast/substreams-sink"
"go.uber.org/zap"
"golang.org/x/exp/maps"
)
type clickhouseDialect struct{}
// Clickhouse should be used to insert a lot of data in batches. The current official clickhouse
// driver doesn't support Transactions for multiple tables. The only way to add in batches is
// creating a transaction for a table, adding all rows and commiting it.
func (d clickhouseDialect) Flush(tx Tx, ctx context.Context, l *Loader, outputModuleHash string, lastFinalBlock uint64) (int, error) {
var entryCount int
for entriesPair := l.entries.Oldest(); entriesPair != nil; entriesPair = entriesPair.Next() {
tableName := entriesPair.Key
entries := entriesPair.Value
tx, err := l.DB.BeginTx(ctx, nil)
if err != nil {
return entryCount, fmt.Errorf("failed to begin db transaction")
}
if l.tracer.Enabled() {
l.logger.Debug("flushing table entries", zap.String("table_name", tableName), zap.Int("entry_count", entries.Len()))
}
info := l.tables[tableName]
columns := make([]string, 0, len(info.columnsByName))
for column := range info.columnsByName {
columns = append(columns, column)
}
sort.Strings(columns)
query := fmt.Sprintf(
"INSERT INTO %s.%s (%s)",
EscapeIdentifier(l.schema),
EscapeIdentifier(tableName),
strings.Join(columns, ","))
batch, err := tx.Prepare(query)
if err != nil {
return entryCount, fmt.Errorf("failed to prepare insert into %q: %w", tableName, err)
}
for entryPair := entries.Oldest(); entryPair != nil; entryPair = entryPair.Next() {
entry := entryPair.Value
if err != nil {
return entryCount, fmt.Errorf("failed to get query: %w", err)
}
if l.tracer.Enabled() {
l.logger.Debug("adding query from operation to transaction", zap.Stringer("op", entry), zap.String("query", query))
}
values, err := convertOpToClickhouseValues(entry)
if err != nil {
return entryCount, fmt.Errorf("failed to get values: %w", err)
}
if _, err := batch.ExecContext(ctx, values...); err != nil {
return entryCount, fmt.Errorf("executing for entry %q: %w", values, err)
}
}
if err := tx.Commit(); err != nil {
return entryCount, fmt.Errorf("failed to commit db transaction: %w", err)
}
entryCount += entries.Len()
}
return entryCount, nil
}
func (d clickhouseDialect) Revert(tx Tx, ctx context.Context, l *Loader, lastValidFinalBlock uint64) error {
return fmt.Errorf("clickhouse driver does not support reorg management.")
}
func (d clickhouseDialect) GetCreateCursorQuery(schema string, withPostgraphile bool) string {
_ = withPostgraphile // TODO: see if this can work
return fmt.Sprintf(cli.Dedent(`
CREATE TABLE IF NOT EXISTS %s.%s
(
id String,
cursor String,
block_num Int64,
block_id String
) Engine = ReplacingMergeTree() ORDER BY id;
`), EscapeIdentifier(schema), EscapeIdentifier(CURSORS_TABLE))
}
func (d clickhouseDialect) GetCreateHistoryQuery(schema string, withPostgraphile bool) string {
panic("clickhouse does not support reorg management")
}
func (d clickhouseDialect) ExecuteSetupScript(ctx context.Context, l *Loader, schemaSql string) error {
for _, query := range strings.Split(schemaSql, ";") {
if len(strings.TrimSpace(query)) == 0 {
continue
}
if _, err := l.ExecContext(ctx, query); err != nil {
return fmt.Errorf("exec schema: %w", err)
}
}
return nil
}
func (d clickhouseDialect) GetUpdateCursorQuery(table, moduleHash string, cursor *sink.Cursor, block_num uint64, block_id string) string {
return query(`
INSERT INTO %s (id, cursor, block_num, block_id) values ('%s', '%s', %d, '%s')
`, table, moduleHash, cursor, block_num, block_id)
}
func (d clickhouseDialect) ParseDatetimeNormalization(value string) string {
return fmt.Sprintf("parseDateTimeBestEffort(%s)", escapeStringValue(value))
}
func (d clickhouseDialect) DriverSupportRowsAffected() bool {
return false
}
func (d clickhouseDialect) OnlyInserts() bool {
return true
}
func (d clickhouseDialect) CreateUser(tx Tx, ctx context.Context, l *Loader, username string, password string, _database string, readOnly bool) error {
user, pass := EscapeIdentifier(username), escapeStringValue(password)
createUserQ := fmt.Sprintf("CREATE USER IF NOT EXISTS %s IDENTIFIED WITH plaintext_password BY %s;", user, pass)
_, err := tx.ExecContext(ctx, createUserQ)
if err != nil {
return fmt.Errorf("executing query %q: %w", createUserQ, err)
}
var grantQ string
if readOnly {
grantQ = fmt.Sprintf(`
GRANT SELECT ON *.* TO %s;
`, user)
} else {
grantQ = fmt.Sprintf(`
GRANT ALL ON *.* TO %s;
`, user)
}
_, err = tx.ExecContext(ctx, grantQ)
if err != nil {
return fmt.Errorf("executing query %q: %w", grantQ, err)
}
return nil
}
func convertOpToClickhouseValues(o *Operation) ([]any, error) {
columns := make([]string, len(o.data))
i := 0
for column := range o.data {
columns[i] = column
i++
}
sort.Strings(columns)
values := make([]any, len(o.data))
for i, v := range columns {
if col, exists := o.table.columnsByName[v]; exists {
convertedType, err := convertToType(o.data[v], col.scanType)
if err != nil {
return nil, fmt.Errorf("converting value %q to type %q in column %q: %w", o.data[v], col.scanType, v, err)
}
values[i] = convertedType
} else {
return nil, fmt.Errorf("cannot find column %q for table %q (valid columns are %q)", v, o.table.identifier, strings.Join(maps.Keys(o.table.columnsByName), ", "))
}
}
return values, nil
}
func convertToType(value string, valueType reflect.Type) (any, error) {
switch valueType.Kind() {
case reflect.String:
return value, nil
case reflect.Slice:
return value, nil
case reflect.Bool:
return strconv.ParseBool(value)
case reflect.Int:
v, err := strconv.ParseInt(value, 10, 0)
return int(v), err
case reflect.Int8:
v, err := strconv.ParseInt(value, 10, 8)
return int8(v), err
case reflect.Int16:
v, err := strconv.ParseInt(value, 10, 16)
return int16(v), err
case reflect.Int32:
v, err := strconv.ParseInt(value, 10, 32)
return int32(v), err
case reflect.Int64:
return strconv.ParseInt(value, 10, 64)
case reflect.Uint:
v, err := strconv.ParseUint(value, 10, 0)
return uint(v), err
case reflect.Uint8:
v, err := strconv.ParseUint(value, 10, 8)
return uint8(v), err
case reflect.Uint16:
v, err := strconv.ParseUint(value, 10, 16)
return uint16(v), err
case reflect.Uint32:
v, err := strconv.ParseUint(value, 10, 32)
return uint32(v), err
case reflect.Uint64:
return strconv.ParseUint(value, 10, 0)
case reflect.Float32, reflect.Float64:
return strconv.ParseFloat(value, 10)
case reflect.Struct:
if valueType == reflectTypeTime {
if integerRegex.MatchString(value) {
i, err := strconv.Atoi(value)
if err != nil {
return "", fmt.Errorf("could not convert %s to int: %w", value, err)
}
return int64(i), nil
}
var v time.Time
var err error
if strings.Contains(value, "T") && strings.HasSuffix(value, "Z") {
v, err = time.Parse("2006-01-02T15:04:05Z", value)
} else {
v, err = time.Parse("2006-01-02 15:04:05", value)
}
if err != nil {
return "", fmt.Errorf("could not convert %s to time: %w", value, err)
}
return v.Unix(), nil
}
return "", fmt.Errorf("unsupported struct type %s", valueType)
case reflect.Ptr:
if valueType.String() == "*big.Int" {
newInt := new(big.Int)
newInt.SetString(value, 10)
return newInt, nil
}
elemType := valueType.Elem()
val, err := convertToType(value, elemType)
if err != nil {
return nil, fmt.Errorf("invalid pointer type: %w", err)
}
// We cannot just return &val here as this will return an *interface{} that the Clickhouse Go client won't be
// able to convert on inserting. Instead, we create a new variable using the type that valueType has been
// pointing to, assign the converted value from convertToType to that and then return a pointer to the new variable.
result := reflect.New(elemType).Elem()
result.Set(reflect.ValueOf(val))
return result.Addr().Interface(), nil
default:
return value, nil
}
}