/
clickhouse.go
775 lines (671 loc) · 26.3 KB
/
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
package adapters
import (
"context"
"crypto/tls"
"crypto/x509"
"database/sql"
"errors"
"fmt"
"github.com/jitsucom/jitsu/server/drivers/base"
"io/ioutil"
"sort"
"strings"
"time"
"github.com/jitsucom/jitsu/server/errorj"
"github.com/jitsucom/jitsu/server/logging"
"github.com/jitsucom/jitsu/server/typing"
"github.com/mailru/go-clickhouse"
)
const (
tableSchemaCHQuery = `SELECT name, type FROM system.columns WHERE database = ? and table = ? and default_kind not in ('MATERIALIZED', 'ALIAS', 'EPHEMERAL')`
createCHDBTemplate = `CREATE DATABASE IF NOT EXISTS "%s" %s`
alterTableCHTemplate = `ALTER TABLE "%s"."%s" %s %s`
exchangeTableCHTemplate = `EXCHANGE TABLES "%s"."%s" AND "%s"."%s"`
renameTableCHTemplate = `RENAME TABLE "%s"."%s" TO "%s"`
insertCHTemplate = `INSERT INTO "%s"."%s" (%s) VALUES %s`
deleteQueryChTemplate = `ALTER TABLE %s.%s DELETE WHERE %s`
dropTableCHTemplate = `DROP TABLE %s"%s"."%s" %s`
onClusterCHClauseTemplate = ` ON CLUSTER "%s" `
columnCHNullableTemplate = ` Nullable(%s) `
createTableCHTemplate = `CREATE TABLE "%s"."%s" %s (%s) %s %s %s %s`
createDistributedTableCHTemplate = `CREATE TABLE "%s"."dist_%s" %s AS "%s"."%s" ENGINE = Distributed(%s,%s,%s,rand())`
dropDistributedTableCHTemplate = `DROP TABLE IF EXISTS "%s"."dist_%s" %s`
alterDistributedTableCHTemplate = `ALTER TABLE "%s"."dist_%s" %s %s`
exchangeDistributedTableCHTemplate = `EXCHANGE TABLES "%s"."dist_%s" AND "%s"."dist_%s"`
renameDistributedTableCHTemplate = `RENAME TABLE "%s"."dist_%s" TO "dist_%s"`
truncateTableCHTemplate = `TRUNCATE TABLE IF EXISTS "%s"."%s"`
truncateDistributedTableCHTemplate = `TRUNCATE TABLE IF EXISTS "%s"."dist_%s" %s`
defaultPartition = `PARTITION BY (toYYYYMM(_timestamp))`
defaultOrderBy = `ORDER BY (eventn_ctx_event_id)`
defaultPrimaryKey = ``
)
var (
SchemaToClickhouse = map[typing.DataType]string{
typing.STRING: "String",
typing.INT64: "Int64",
typing.FLOAT64: "Float64",
typing.TIMESTAMP: "DateTime",
typing.BOOL: "UInt8",
typing.UNKNOWN: "String",
}
defaultValues = map[string]interface{}{
"int8": 0,
"int16": 0,
"int32": 0,
"int64": 0,
"int128": 0,
"int256": 0,
"float32": 0.0,
"float64": 0.0,
"decimal": 0.0,
"numeric": 0.0,
"datetime": time.Time{},
"uint8": false,
"uint16": 0,
"uint32": 0,
"uint64": 0,
"uint128": 0,
"uint256": 0,
"string": "",
"lowcardinality(int8)": 0,
"lowcardinality(int16)": 0,
"lowcardinality(int32)": 0,
"lowcardinality(int64)": 0,
"lowcardinality(int128)": 0,
"lowcardinality(int256)": 0,
"lowcardinality(float32)": 0,
"lowcardinality(float64)": 0,
"lowcardinality(datetime)": time.Time{},
"lowcardinality(uint8)": false,
"lowcardinality(uint16)": 0,
"lowcardinality(uint32)": 0,
"lowcardinality(uint64)": 0,
"lowcardinality(uint128)": 0,
"lowcardinality(uint256)": 0,
"lowcardinality(string)": "",
"uuid": "00000000-0000-0000-0000-000000000000",
}
)
// ClickHouseConfig dto for deserialized clickhouse config
type ClickHouseConfig struct {
Dsns []string `mapstructure:"dsns,omitempty" json:"dsns,omitempty" yaml:"dsns,omitempty"`
Database string `mapstructure:"db,omitempty" json:"db,omitempty" yaml:"db,omitempty"`
TLS map[string]string `mapstructure:"tls,omitempty" json:"tls,omitempty" yaml:"tls,omitempty"`
Cluster string `mapstructure:"cluster,omitempty" json:"cluster,omitempty" yaml:"cluster,omitempty"`
Engine *EngineConfig `mapstructure:"engine,omitempty" json:"engine,omitempty" yaml:"engine,omitempty"`
}
// EngineConfig dto for deserialized clickhouse engine config
type EngineConfig struct {
RawStatement string `mapstructure:"raw_statement,omitempty" json:"raw_statement,omitempty" yaml:"raw_statement,omitempty"`
NullableFields []string `mapstructure:"nullable_fields,omitempty" json:"nullable_fields,omitempty" yaml:"nullable_fields,omitempty"`
PartitionFields []FieldConfig `mapstructure:"partition_fields,omitempty" json:"partition_fields,omitempty" yaml:"partition_fields,omitempty"`
OrderFields []FieldConfig `mapstructure:"order_fields,omitempty" json:"order_fields,omitempty" yaml:"order_fields,omitempty"`
PrimaryKeys []string `mapstructure:"primary_keys,omitempty" json:"primary_keys,omitempty" yaml:"primary_keys,omitempty"`
}
// FieldConfig dto for deserialized clickhouse engine fields
type FieldConfig struct {
Function string `mapstructure:"function,omitempty" json:"function,omitempty" yaml:"function,omitempty"`
Field string `mapstructure:"field,omitempty" json:"field,omitempty" yaml:"field,omitempty"`
}
// Validate required fields in ClickHouseConfig
func (chc *ClickHouseConfig) Validate() error {
if chc == nil {
return errors.New("ClickHouse config is required")
}
if len(chc.Dsns) == 0 {
return errors.New("dsn is required parameter")
}
for _, dsn := range chc.Dsns {
if dsn == "" {
return errors.New("DSNs values can't be empty")
}
if !strings.HasPrefix(strings.TrimSpace(dsn), "http") {
return errors.New("DSNs must have http:// or https:// prefix")
}
}
if chc.Cluster == "" && len(chc.Dsns) > 1 {
return errors.New("cluster is required parameter when dsns count > 1")
}
if chc.Database == "" {
return errors.New("db is required parameter")
}
return nil
}
// TableStatementFactory is used for creating CREATE TABLE statements depends on config
type TableStatementFactory struct {
engineStatement string
database string
onClusterClause string
partitionClause string
orderByClause string
primaryKeyClause string
engineStatementFormat bool
}
func NewTableStatementFactory(config *ClickHouseConfig) (*TableStatementFactory, error) {
if config == nil {
return nil, errors.New("Clickhouse config can't be nil")
}
var onClusterClause string
if config.Cluster != "" {
onClusterClause = fmt.Sprintf(onClusterCHClauseTemplate, config.Cluster)
}
partitionClause := defaultPartition
orderByClause := defaultOrderBy
primaryKeyClause := defaultPrimaryKey
if config.Engine != nil {
//raw statement overrides all provided config parameters
if config.Engine.RawStatement != "" {
return &TableStatementFactory{
engineStatement: config.Engine.RawStatement,
database: config.Database,
onClusterClause: onClusterClause,
}, nil
}
if len(config.Engine.PartitionFields) > 0 {
partitionClause = "PARTITION BY (" + extractStatement(config.Engine.PartitionFields) + ")"
}
if len(config.Engine.OrderFields) > 0 {
orderByClause = "ORDER BY (" + extractStatement(config.Engine.OrderFields) + ")"
}
if len(config.Engine.PrimaryKeys) > 0 {
primaryKeyClause = "PRIMARY KEY (" + strings.Join(config.Engine.PrimaryKeys, ", ") + ")"
}
}
var engineStatement string
var engineStatementFormat bool
if config.Cluster != "" {
//create engine statement with ReplicatedReplacingMergeTree() engine. We need to replace %s with tableName on creating statement
engineStatement = `ENGINE = ReplicatedReplacingMergeTree('/clickhouse/tables/{shard}/` + config.Database + `/%s', '{replica}')`
engineStatementFormat = true
} else {
//create table template with ReplacingMergeTree() engine
engineStatement = `ENGINE = ReplacingMergeTree()`
}
return &TableStatementFactory{
engineStatement: engineStatement,
database: config.Database,
onClusterClause: onClusterClause,
partitionClause: partitionClause,
orderByClause: orderByClause,
primaryKeyClause: primaryKeyClause,
engineStatementFormat: engineStatementFormat,
}, nil
}
// CreateTableStatement return clickhouse DDL for creating table statement
func (tsf TableStatementFactory) CreateTableStatement(tableName, columnsClause string) string {
engineStatement := tsf.engineStatement
if tsf.engineStatementFormat {
engineStatement = fmt.Sprintf(engineStatement, tableName)
}
return fmt.Sprintf(createTableCHTemplate, tsf.database, tableName, tsf.onClusterClause, columnsClause, engineStatement,
tsf.partitionClause, tsf.orderByClause, tsf.primaryKeyClause)
}
// ClickHouse is adapter for creating,patching (schema or table), inserting data to clickhouse
type ClickHouse struct {
ctx context.Context
database string
cluster string
dataSource *sql.DB
tableStatementFactory *TableStatementFactory
nullableFields map[string]bool
queryLogger *logging.QueryLogger
sqlTypes typing.SQLTypes
}
// NewClickHouse returns configured ClickHouse adapter instance
func NewClickHouse(ctx context.Context, connectionString, database, cluster string, tlsConfig map[string]string,
tableStatementFactory *TableStatementFactory, nullableFields map[string]bool,
queryLogger *logging.QueryLogger, sqlTypes typing.SQLTypes) (*ClickHouse, error) {
connectionString = strings.TrimSpace(connectionString)
//configure tls
if strings.Contains(connectionString, "https://") && tlsConfig != nil {
for tlsName, crtPath := range tlsConfig {
caCert, err := ioutil.ReadFile(crtPath)
if err != nil {
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
if err := clickhouse.RegisterTLSConfig(tlsName, &tls.Config{RootCAs: caCertPool}); err != nil {
return nil, err
}
}
}
//enrich with ?wait_end_of_query=1 (for waiting on cluster execution result)
if strings.Contains(connectionString, "?") {
connectionString += "&"
} else {
connectionString += "?"
}
connectionString += "wait_end_of_query=1"
//connect
dataSource, err := sql.Open("clickhouse", connectionString)
if err != nil {
return nil, err
}
//keep select 1 and don't use Ping() because chproxy doesn't support /ping endpoint.
if _, err := dataSource.Exec("SELECT 1"); err != nil {
dataSource.Close()
return nil, err
}
return &ClickHouse{
ctx: ctx,
database: database,
cluster: cluster,
dataSource: dataSource,
tableStatementFactory: tableStatementFactory,
nullableFields: nullableFields,
queryLogger: queryLogger,
sqlTypes: reformatMappings(sqlTypes, SchemaToClickhouse),
}, nil
}
func (ClickHouse) Type() string {
return "ClickHouse"
}
// CreateDB create database instance if doesn't exist
func (ch *ClickHouse) CreateDB(dbName string) error {
query := fmt.Sprintf(createCHDBTemplate, dbName, ch.getOnClusterClause())
ch.queryLogger.LogDDL(query)
if _, err := ch.dataSource.ExecContext(ch.ctx, query); err != nil {
return errorj.CreateSchemaError.Wrap(err, "failed to create db schema").
WithProperty(errorj.DBInfo, &ErrorPayload{
Database: dbName,
Cluster: ch.cluster,
Statement: query,
})
}
return nil
}
// CreateTable create database table with name,columns provided in Table representation
// New tables will have MergeTree() or ReplicatedMergeTree() engine depends on config.cluster empty or not
func (ch *ClickHouse) CreateTable(table *Table) error {
var columnsDDL []string
for _, columnName := range table.SortedColumnNames() {
column := table.Columns[columnName]
columnTypeDDL := ch.columnDDL(columnName, column)
columnsDDL = append(columnsDDL, columnTypeDDL)
}
//sorting columns asc
sort.Strings(columnsDDL)
statementStr := ch.tableStatementFactory.CreateTableStatement(table.Name, strings.Join(columnsDDL, ","))
ch.queryLogger.LogDDL(statementStr)
if _, err := ch.dataSource.ExecContext(ch.ctx, statementStr); err != nil {
return errorj.CreateTableError.Wrap(err, "failed to create table").
WithProperty(errorj.DBInfo, &ErrorPayload{
Database: ch.database,
Cluster: ch.cluster,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: statementStr,
})
}
//create distributed table if ReplicatedMergeTree engine
if ch.cluster != "" {
ch.createDistributedTableInTransaction(table.Name)
}
return nil
}
// GetTableSchema return table (name,columns with name and types) representation wrapped in Table struct
func (ch *ClickHouse) GetTableSchema(tableName string) (*Table, error) {
table := &Table{Schema: ch.database, Name: tableName, Columns: map[string]typing.SQLColumn{}}
rows, err := ch.dataSource.QueryContext(ch.ctx, tableSchemaCHQuery, ch.database, tableName)
if err != nil {
return nil, errorj.GetTableError.Wrap(err, "failed to get table columns").
WithProperty(errorj.DBInfo, &ErrorPayload{
Database: ch.database,
Cluster: ch.cluster,
Table: tableName,
PrimaryKeys: table.GetPKFields(),
Statement: tableSchemaCHQuery,
Values: []interface{}{ch.database, tableName},
})
}
defer rows.Close()
for rows.Next() {
var columnName, columnClickhouseType string
if err := rows.Scan(&columnName, &columnClickhouseType); err != nil {
return nil, errorj.GetTableError.Wrap(err, "failed to scan result").
WithProperty(errorj.DBInfo, &ErrorPayload{
Database: ch.database,
Cluster: ch.cluster,
Table: tableName,
PrimaryKeys: table.GetPKFields(),
Statement: tableSchemaCHQuery,
Values: []interface{}{ch.database, tableName},
})
}
table.Columns[columnName] = typing.SQLColumn{Type: columnClickhouseType}
}
if err := rows.Err(); err != nil {
return nil, errorj.GetTableError.Wrap(err, "failed read last row").
WithProperty(errorj.DBInfo, &ErrorPayload{
Database: ch.database,
Cluster: ch.cluster,
Table: tableName,
PrimaryKeys: table.GetPKFields(),
Statement: tableSchemaCHQuery,
Values: []interface{}{ch.database, tableName},
})
}
return table, nil
}
// PatchTableSchema add new columns(from provided Table) to existing table
// drop and create distributed table
func (ch *ClickHouse) PatchTableSchema(patchSchema *Table) error {
if len(patchSchema.Columns) == 0 {
return nil
}
addedColumnsDDL := make([]string, 0, len(patchSchema.Columns))
for _, columnName := range patchSchema.SortedColumnNames() {
column := patchSchema.Columns[columnName]
columnDDL := ch.columnDDL(columnName, column)
addedColumnsDDL = append(addedColumnsDDL, "ADD COLUMN "+columnDDL)
}
query := fmt.Sprintf(alterTableCHTemplate, ch.database, patchSchema.Name, ch.getOnClusterClause(), strings.Join(addedColumnsDDL, ", "))
ch.queryLogger.LogDDL(query)
if _, err := ch.dataSource.ExecContext(ch.ctx, query); err != nil {
return errorj.PatchTableError.Wrap(err, "failed to patch table").
WithProperty(errorj.DBInfo, &ErrorPayload{
Database: ch.database,
Cluster: ch.cluster,
Table: patchSchema.Name,
PrimaryKeys: patchSchema.GetPKFields(),
Statement: query,
})
}
//drop and create distributed table if ReplicatedMergeTree engine
if ch.cluster != "" {
query := fmt.Sprintf(alterDistributedTableCHTemplate, ch.database, patchSchema.Name, ch.getOnClusterClause(), strings.Join(addedColumnsDDL, ", "))
ch.queryLogger.LogDDL(query)
_, err := ch.dataSource.ExecContext(ch.ctx, query)
switch err := err.(type) {
case nil:
return nil
case *clickhouse.Error:
if err.Code == 371 {
// fallback for older clickhouse versions
ch.dropDistributedTableInTransaction(patchSchema.Name)
ch.createDistributedTableInTransaction(patchSchema.Name)
return nil
}
}
logging.Errorf("[%s] Error altering distributed table for [%s] with statement [%s]: %v", ch.destinationId(), patchSchema.Name, query, err)
}
return nil
}
// Insert inserts provided object in ClickHouse as a single record or batch
func (ch *ClickHouse) Insert(insertContext *InsertContext) error {
if !insertContext.deleteConditions.IsEmpty() {
if err := ch.delete(insertContext.table, insertContext.deleteConditions); err != nil {
return errorj.Decorate(err, "failed to execute delete in insert")
}
}
return ch.insert(insertContext.table, insertContext.objects...)
}
func (ch *ClickHouse) delete(table *Table, deleteConditions *base.DeleteConditions) error {
deleteCondition, values := ch.toDeleteQuery(table, deleteConditions)
deleteQuery := fmt.Sprintf(deleteQueryChTemplate, ch.database, table.Name, deleteCondition)
ch.queryLogger.LogQueryWithValues(deleteQuery, values)
if _, err := ch.dataSource.ExecContext(ch.ctx, deleteQuery, values...); err != nil {
return errorj.DeleteFromTableError.Wrap(err, "failed to delete data").
WithProperty(errorj.DBInfo, &ErrorPayload{
Database: ch.database,
Cluster: ch.cluster,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: deleteQuery,
Values: values,
})
}
return nil
}
// Truncate deletes all records in tableName table
func (ch *ClickHouse) Truncate(tableName string) error {
sqlParams := SqlParams{
dataSource: ch.dataSource,
queryLogger: ch.queryLogger,
ctx: ch.ctx,
}
statement := fmt.Sprintf(truncateTableCHTemplate, ch.database, tableName)
if err := sqlParams.commonTruncate(statement); err != nil {
return errorj.TruncateError.Wrap(err, "failed to truncate table").
WithProperty(errorj.DBInfo, &ErrorPayload{
Database: ch.database,
Cluster: ch.cluster,
Table: tableName,
Statement: statement,
})
}
if ch.cluster != "" {
statement = fmt.Sprintf(truncateDistributedTableCHTemplate, ch.database, tableName, ch.getOnClusterClause())
if err := sqlParams.commonTruncate(statement); err != nil {
logging.Errorf("[%s] error truncating distributed table with statement [%s]", ch.destinationId(), statement)
}
}
return nil
}
func (ch *ClickHouse) DropTable(table *Table) error {
return ch.dropTable(table, false)
}
// dropTable drops table in transaction
func (ch *ClickHouse) dropTable(table *Table, ifExists bool) error {
ifExs := ""
if ifExists {
ifExs = "IF EXISTS "
}
query := fmt.Sprintf(dropTableCHTemplate, ifExs, ch.database, table.Name, ch.getOnClusterClause())
ch.queryLogger.LogDDL(query)
if _, err := ch.dataSource.ExecContext(ch.ctx, query); err != nil {
return errorj.DropError.Wrap(err, "failed to drop table").
WithProperty(errorj.DBInfo, &ErrorPayload{
Schema: ch.database,
Table: table.Name,
Statement: query,
})
}
if ch.cluster != "" {
ch.dropDistributedTableInTransaction(table.Name)
}
return nil
}
func (ch *ClickHouse) ReplaceTable(originalTable, replacementTable string, dropOldTable bool) error {
query := fmt.Sprintf(exchangeTableCHTemplate, ch.database, originalTable, ch.database, replacementTable)
ch.queryLogger.LogDDL(query)
if _, err := ch.dataSource.ExecContext(ch.ctx, query); err != nil {
if mapError(err) == ErrTableNotExist {
query = fmt.Sprintf(renameTableCHTemplate, ch.database, replacementTable, originalTable)
ch.queryLogger.LogDDL(query)
if _, err := ch.dataSource.ExecContext(ch.ctx, query); err != nil {
return fmt.Errorf("Error renaming [%s] table: %v", replacementTable, err)
}
if ch.cluster != "" {
query := fmt.Sprintf(renameDistributedTableCHTemplate, ch.database, replacementTable, originalTable)
ch.queryLogger.LogDDL(query)
if _, err := ch.dataSource.ExecContext(ch.ctx, query); err != nil {
return fmt.Errorf("Error renaming [%s] distributed table: %v", originalTable, err)
}
}
} else {
return fmt.Errorf("Error replacing [%s] table: %v", originalTable, err)
}
}
if ch.cluster != "" {
query := fmt.Sprintf(exchangeDistributedTableCHTemplate, ch.database, originalTable, ch.database, replacementTable)
ch.queryLogger.LogDDL(query)
if _, err := ch.dataSource.ExecContext(ch.ctx, query); err != nil {
return fmt.Errorf("Error replacing [%s] distributed table: %v", originalTable, err)
}
}
if dropOldTable {
return ch.dropTable(&Table{Name: replacementTable}, true)
} else {
return nil
}
}
func (ch *ClickHouse) toDeleteQuery(table *Table, conditions *base.DeleteConditions) (string, []interface{}) {
var queryConditions []string
var values []interface{}
for _, condition := range conditions.Conditions {
queryConditions = append(queryConditions, condition.Field+" "+condition.Clause+" "+ch.getPlaceholder(condition.Field, table.Columns[condition.Field]))
values = append(values, typing.ReformatValue(condition.Value))
}
return strings.Join(queryConditions, " "+conditions.JoinCondition+" "), values
}
// insert creates statement like insert ... values (), (), ()
// runs executeInsert func
func (ch *ClickHouse) insert(table *Table, objects ...map[string]interface{}) error {
var placeholdersBuilder strings.Builder
var headerWithoutQuotes, headerWithQuotes []string
for _, name := range table.SortedColumnNames() {
headerWithoutQuotes = append(headerWithoutQuotes, name)
headerWithQuotes = append(headerWithQuotes, fmt.Sprintf(`"%s"`, name))
}
maxValues := len(objects) * len(table.Columns)
valueArgs := make([]interface{}, 0, maxValues)
for _, row := range objects {
_, _ = placeholdersBuilder.WriteString("(")
for i, column := range headerWithoutQuotes {
//append value
value, ok := row[column]
if ok {
valueArgs = append(valueArgs, ch.reformatValue(value))
} else {
column, _ := table.Columns[column]
defaultValue, ok := ch.getDefaultValue(column.Type)
if ok {
valueArgs = append(valueArgs, defaultValue)
} else {
valueArgs = append(valueArgs, value)
}
}
//placeholder
placeholder := ch.getPlaceholder(column, table.Columns[column])
_, _ = placeholdersBuilder.WriteString(placeholder)
if i < len(headerWithoutQuotes)-1 {
_, _ = placeholdersBuilder.WriteString(",")
}
}
_, _ = placeholdersBuilder.WriteString("),")
}
if err := ch.executeInsert(table, headerWithQuotes, removeLastComma(placeholdersBuilder.String()), valueArgs, len(objects)); err != nil {
return err
}
return nil
}
// executeInsert execute insert with insertTemplate
func (ch *ClickHouse) executeInsert(table *Table, headerWithQuotes []string, placeholders string, valueArgs []interface{}, objectsCount int) error {
statement := fmt.Sprintf(insertCHTemplate, ch.database, table.Name, strings.Join(headerWithQuotes, ", "), placeholders)
ch.queryLogger.LogQueryWithValues(statement, valueArgs)
if _, err := ch.dataSource.Exec(statement, valueArgs...); err != nil {
return errorj.ExecuteInsertInBatchError.Wrap(err, "failed to execute insert").
WithProperty(errorj.DBInfo, &ErrorPayload{
Database: ch.database,
Cluster: ch.cluster,
Table: table.Name,
PrimaryKeys: table.GetPKFields(),
Statement: statement,
ValuesMapString: ObjectValuesToString(headerWithQuotes, valueArgs),
TotalObjects: objectsCount,
})
}
return nil
}
// Close underlying sql.DB
func (ch *ClickHouse) Close() error {
return ch.dataSource.Close()
}
// return ON CLUSTER name clause or "" if config.cluster is empty
func (ch *ClickHouse) getOnClusterClause() string {
if ch.cluster == "" {
return ""
}
return fmt.Sprintf(onClusterCHClauseTemplate, ch.cluster)
}
// create distributed table, ignore errors
func (ch *ClickHouse) createDistributedTableInTransaction(originTableName string) {
statement := fmt.Sprintf(createDistributedTableCHTemplate,
ch.database, originTableName, ch.getOnClusterClause(), ch.database, originTableName, ch.cluster, ch.database, originTableName)
ch.queryLogger.LogDDL(statement)
if _, err := ch.dataSource.ExecContext(ch.ctx, statement); err != nil {
logging.Errorf("[%s] Error creating distributed table statement with statement [%s] for [%s] : %v", ch.destinationId(), statement, originTableName, err)
}
}
// drop distributed table, ignore errors
func (ch *ClickHouse) dropDistributedTableInTransaction(originTableName string) {
query := fmt.Sprintf(dropDistributedTableCHTemplate, ch.database, originTableName, ch.getOnClusterClause())
ch.queryLogger.LogDDL(query)
if _, err := ch.dataSource.ExecContext(ch.ctx, query); err != nil {
logging.Errorf("[%s] Error dropping distributed table for [%s] with statement [%s]: %v", ch.destinationId(), originTableName, query, err)
}
}
// columnDDL returns column DDL (column name, mapped sql type)
func (ch *ClickHouse) columnDDL(name string, column typing.SQLColumn) string {
//get sql type
columnSQLType := column.DDLType()
overriddenSQLType, ok := ch.sqlTypes[name]
if ok {
columnSQLType = overriddenSQLType.DDLType()
}
//get nullable or plain
var columnTypeDDL string
if _, ok := ch.nullableFields[name]; ok {
columnTypeDDL = fmt.Sprintf(columnCHNullableTemplate, columnSQLType)
} else {
columnTypeDDL = columnSQLType
}
return fmt.Sprintf(`"%s" %s`, name, columnTypeDDL)
}
// getPlaceholder returns "?" placeholder or with typecast
func (ch *ClickHouse) getPlaceholder(columnName string, column typing.SQLColumn) string {
castType, ok := ch.sqlTypes[columnName]
if !ok && column.Override {
castType = column
ok = true
}
if ok {
return fmt.Sprintf("cast(?, '%s')", castType.Type)
}
return "?"
}
// return nil if column type is nullable or default value for input type
func (ch *ClickHouse) getDefaultValue(sqlType string) (interface{}, bool) {
if !strings.Contains(strings.ToLower(sqlType), "nullable") {
//get default value based on type
dv, ok := defaultValues[strings.ToLower(sqlType)]
if ok {
return dv, true
}
logging.SystemErrorf("Unknown clickhouse default value for %s", sqlType)
}
return nil, false
}
// if value is boolean - reformat it [true = 1; false = 0] ClickHouse supports UInt8 instead of boolean
// otherwise return value as is
func (ch *ClickHouse) reformatValue(v interface{}) interface{} {
//reformat boolean
booleanValue, ok := v.(bool)
if ok {
if booleanValue {
return 1
}
return 0
}
return v
}
func (ch *ClickHouse) destinationId() interface{} {
return ch.ctx.Value(CtxDestinationId)
}
func extractStatement(fieldConfigs []FieldConfig) string {
var parameters []string
for _, fieldConfig := range fieldConfigs {
if fieldConfig.Function != "" {
parameters = append(parameters, fieldConfig.Function+"("+fieldConfig.Field+")")
continue
}
parameters = append(parameters, fieldConfig.Field)
}
return strings.Join(parameters, ",")
}
func (ch *ClickHouse) Update(table *Table, object map[string]interface{}, whereKey string, whereValue interface{}) error {
//TODO Add Sign property for CollapcingMergeTree
return ch.insert(table, object)
}