-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathtable_function.go
More file actions
206 lines (171 loc) · 6.51 KB
/
Copy pathtable_function.go
File metadata and controls
206 lines (171 loc) · 6.51 KB
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
package connclickhouse
import (
"context"
"fmt"
"slices"
"strings"
"go.temporal.io/sdk/log"
"github.com/PeerDB-io/peerdb/flow/generated/protos"
"github.com/PeerDB-io/peerdb/flow/internal"
"github.com/PeerDB-io/peerdb/flow/internal/clickhouse"
"github.com/PeerDB-io/peerdb/flow/model/qvalue"
peerdb_clickhouse "github.com/PeerDB-io/peerdb/flow/pkg/clickhouse"
"github.com/PeerDB-io/peerdb/flow/pkg/common"
"github.com/PeerDB-io/peerdb/flow/shared"
"github.com/PeerDB-io/peerdb/flow/shared/types"
)
// insertFromTableFunctionConfig contains the configuration for building INSERT queries from table functions
type insertFromTableFunctionConfig struct {
columnNameMap map[string]string
config *protos.QRepConfig
connector *ClickHouseConnector
logger log.Logger
destinationTable string
schema types.QRecordSchema
excludedColumns []string
fieldExpressionConverters []fieldExpressionConverter
}
type fieldExpressionConverter func(
ctx context.Context,
config *insertFromTableFunctionConfig,
sourceFieldIdentifier string,
field types.QField,
) (string, error)
func jsonFieldExpressionConverter(
ctx context.Context,
config *insertFromTableFunctionConfig,
sourceFieldIdentifier string,
field types.QField,
) (string, error) {
if field.Type != types.QValueKindJSON && field.Type != types.QValueKindJSONB {
return sourceFieldIdentifier, nil
}
if !qvalue.ShouldUseNativeJSONType(ctx, config.config.Env, config.connector.chVersion) {
return sourceFieldIdentifier, nil
}
if field.Nullable {
return fmt.Sprintf("CAST(%s, 'Nullable(JSON)')", sourceFieldIdentifier), nil
}
return fmt.Sprintf("CAST(%s, 'JSON')", sourceFieldIdentifier), nil
}
func timeFieldExpressionConverter(
_ context.Context,
config *insertFromTableFunctionConfig,
sourceFieldIdentifier string,
field types.QField,
) (string, error) {
if field.Type != types.QValueKindTime && field.Type != types.QValueKindTimeTZ {
return sourceFieldIdentifier, nil
}
// Handle BigQuery source where TIME is exported as Parquet TIME(MICROS), which
// ClickHouse interprets as DateTime64(6, 'UTC'), so no manual conversion needed
if config.config.SourceType == protos.DBType_BIGQUERY {
return sourceFieldIdentifier, nil
}
// Handle legacy path where TIME was stored as DateTime64, before ClickHouse supported Time64 type
if !slices.Contains(config.config.Flags, shared.Flag_ClickHouseTime64Enabled) {
return sourceFieldIdentifier, nil
}
// QValueTime is stored as time-micro logical type in Avro, toTime64 accepts a
// fractional second, so conversion is necessary. Trip through Decimal64 to preserve precision.
return fmt.Sprintf("toTime64(toDecimal64(%s, 6) / 1000000, 6)", sourceFieldIdentifier), nil
}
var defaultFieldExpressionConverters = []fieldExpressionConverter{
jsonFieldExpressionConverter,
timeFieldExpressionConverter,
}
// buildInsertFromTableFunctionQuery builds a complete INSERT query from a table function expression
// This function handles column mapping, type conversions, and source schema columns
func buildInsertFromTableFunctionQuery(
ctx context.Context,
config *insertFromTableFunctionConfig,
tableFunctionExpr string,
chSettings *clickhouse.CHSettings,
) (string, error) {
fieldExpressionConverters := defaultFieldExpressionConverters
fieldExpressionConverters = append(fieldExpressionConverters, config.fieldExpressionConverters...)
sourceSchemaAsDestinationColumn, err := internal.PeerDBSourceSchemaAsDestinationColumn(ctx, config.config.Env)
if err != nil {
return "", err
}
selectedColumnNames := make([]string, 0, len(config.schema.Fields))
insertedColumnNames := make([]string, 0, len(config.schema.Fields))
for _, field := range config.schema.Fields {
colName := field.Name
// Skip excluded columns
excluded := slices.Contains(config.excludedColumns, colName)
if excluded {
continue
}
sourceFieldName := colName
if config.columnNameMap != nil {
if mappedName, ok := config.columnNameMap[colName]; ok {
sourceFieldName = mappedName
} else {
return "", fmt.Errorf("destination column %s not found in column name map", colName)
}
}
sourceFieldName = peerdb_clickhouse.QuoteIdentifier(sourceFieldName)
for _, converter := range fieldExpressionConverters {
convertedExpr, err := converter(ctx, config, sourceFieldName, field)
if err != nil {
return "", err
}
sourceFieldName = convertedExpr
}
selectedColumnNames = append(selectedColumnNames, sourceFieldName)
insertedColumnNames = append(insertedColumnNames, peerdb_clickhouse.QuoteIdentifier(colName))
}
// Add source schema column if needed
if sourceSchemaAsDestinationColumn {
qualifiedTable, err := common.ParseTableIdentifier(config.config.WatermarkTable)
if err != nil {
return "", err
}
selectedColumnNames = append(selectedColumnNames, peerdb_clickhouse.QuoteLiteral(qualifiedTable.Namespace))
insertedColumnNames = append(insertedColumnNames, sourceSchemaColName)
}
selectorStr := strings.Join(selectedColumnNames, ",")
insertedStr := strings.Join(insertedColumnNames, ",")
settingsStr := ""
if chSettings != nil {
settingsStr = chSettings.String()
}
return fmt.Sprintf("INSERT INTO %s(%s) SELECT %s FROM %s%s",
peerdb_clickhouse.QuoteIdentifier(config.destinationTable), insertedStr, selectorStr, tableFunctionExpr, settingsStr), nil
}
// buildInsertFromTableFunctionQueryWithPartitioning builds an INSERT query with hash-based partitioning
func buildInsertFromTableFunctionQueryWithPartitioning(
ctx context.Context,
config *insertFromTableFunctionConfig,
tableFunctionExpr string,
partitionIndex uint64,
totalPartitions uint64,
chSettings *clickhouse.CHSettings,
) (string, error) {
var query strings.Builder
baseQuery, err := buildInsertFromTableFunctionQuery(ctx, config, tableFunctionExpr, nil)
if err != nil {
return "", err
}
query.WriteString(baseQuery)
if totalPartitions > 1 {
// Get the first field for hash partitioning
if len(config.schema.Fields) == 0 {
return "", fmt.Errorf("schema has no fields for partitioning")
}
hashFieldName := config.schema.Fields[0].Name
if config.columnNameMap != nil {
if mappedName, ok := config.columnNameMap[hashFieldName]; ok {
hashFieldName = mappedName
}
}
whereClause := fmt.Sprintf(" WHERE cityHash64(%s) %% %d = %d",
peerdb_clickhouse.QuoteIdentifier(hashFieldName), totalPartitions, partitionIndex)
query.WriteString(whereClause)
}
if chSettings != nil {
query.WriteString(chSettings.String())
}
return query.String(), nil
}