-
Notifications
You must be signed in to change notification settings - Fork 639
Expand file tree
/
Copy pathconn_http_batch.go
More file actions
290 lines (250 loc) · 6.86 KB
/
conn_http_batch.go
File metadata and controls
290 lines (250 loc) · 6.86 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
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
package clickhouse
import (
"context"
"fmt"
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
"io"
"log/slog"
"os"
"slices"
)
func fetchColumnNamesAndTypesForInsert(h *httpConnect, release nativeTransportRelease, ctx context.Context, tableName string, requestedColumnNames []string) ([]ColumnNameAndType, error) {
describeTableQuery := fmt.Sprintf("DESCRIBE TABLE %s", tableName)
r, err := h.query(ctx, release, describeTableQuery)
if err != nil {
return nil, err
}
defer r.Close()
columnsToTypes := make(map[string]string)
var allColumns []string
for r.Next() {
var (
colName string
colType string
defaultType string
ignore string
)
if err = r.Scan(&colName, &colType, &defaultType, &ignore, &ignore, &ignore, &ignore); err != nil {
return nil, err
}
// these column types cannot be specified in INSERT queries
if defaultType == "MATERIALIZED" || defaultType == "ALIAS" {
continue
}
columnsToTypes[colName] = colType
allColumns = append(allColumns, colName)
}
// The order of the columns must match the INSERT list, or the DESC table if no insert list was provided
insertColumns := make([]ColumnNameAndType, 0, len(allColumns))
if len(requestedColumnNames) > 0 {
// Validate requested columns present
for _, colName := range requestedColumnNames {
colType, ok := columnsToTypes[colName]
if !ok {
return nil, fmt.Errorf("column %s is not present in the table %s", colName, tableName)
}
insertColumns = append(insertColumns, ColumnNameAndType{
Name: colName,
Type: colType,
})
}
} else {
// Use all columns
for _, colName := range allColumns {
colType := columnsToTypes[colName]
insertColumns = append(insertColumns, ColumnNameAndType{
Name: colName,
Type: colType,
})
}
}
return insertColumns, nil
}
func newBlock(h *httpConnect, release nativeTransportRelease, ctx context.Context, query string) (string, *proto.Block, error) {
normalizedQuery, tableName, requestedColumnNames, err := extractNormalizedInsertQueryAndColumns(query)
if err != nil {
return "", nil, err
}
opt := queryOptions(ctx)
columns := opt.columnNamesAndTypes
// If the user didn't supply known column names/types, do expensive DESC TABLE logic
if opt.columnNamesAndTypes == nil {
fetchedColumns, err := fetchColumnNamesAndTypesForInsert(h, release, ctx, tableName, requestedColumnNames)
if err != nil {
return "", nil, fmt.Errorf("failed to determine columns for HTTP insert: %w", err)
}
columns = fetchedColumns
}
var block proto.Block
serverContext := serverVersionToContext(h.handshake)
block.ServerContext = &serverContext
for _, col := range columns {
if err := block.AddColumn(col.Name, column.Type(col.Type)); err != nil {
return "", nil, err
}
}
return normalizedQuery, &block, nil
}
func (h *httpConnect) prepareBatch(ctx context.Context, release nativeTransportRelease, acquire nativeTransportAcquire, query string, opts driver.PrepareBatchOptions) (driver.Batch, error) {
// release is not used within newBlock since the connection is held for the batch.
query, block, err := newBlock(h, func(nativeTransport, error) {}, ctx, query)
if err != nil {
err = fmt.Errorf("failed to init block for HTTP batch: %w", err)
release(h, err)
return nil, err
}
return &httpBatch{
ctx: ctx,
conn: h,
connRelease: release,
structMap: &structMap{},
block: block,
query: query,
}, nil
}
type httpBatch struct {
query string
err error
ctx context.Context
conn *httpConnect
released bool
connRelease nativeTransportRelease
structMap *structMap
sent bool
block *proto.Block
}
func (b *httpBatch) release(err error) {
if !b.released {
b.released = true
b.connRelease(b.conn, err)
}
}
func (b *httpBatch) Flush() error {
// Flush and Send are effectively the same for HTTP, but users should just use Send until we
// figure out a way to do proper streaming.
return nil
}
func (b *httpBatch) Close() error {
if b.sent || b.released {
return nil
}
b.sent = true
b.release(nil)
return nil
}
func (b *httpBatch) Abort() error {
defer func() {
b.sent = true
b.release(os.ErrProcessDone)
}()
if b.sent {
return ErrBatchAlreadySent
}
return nil
}
func (b *httpBatch) Append(v ...any) error {
if b.sent {
return ErrBatchAlreadySent
}
if b.err != nil {
return b.err
}
if err := b.block.Append(v...); err != nil {
b.err = fmt.Errorf("%w: %w", ErrBatchInvalid, err)
b.release(err)
return err
}
return nil
}
func (b *httpBatch) AppendStruct(v any) error {
if b.err != nil {
return b.err
}
values, err := b.structMap.Map("AppendStruct", b.block.ColumnsNames(), v, false)
if err != nil {
return err
}
return b.Append(values...)
}
func (b *httpBatch) Column(idx int) driver.BatchColumn {
if len(b.block.Columns) <= idx {
return &batchColumn{
err: &OpError{
Op: "batch.Column",
Err: fmt.Errorf("invalid column index %d", idx),
},
}
}
return &batchColumn{
batch: b,
column: b.block.Columns[idx],
release: func(err error) {
b.err = err
},
}
}
func (b *httpBatch) IsSent() bool {
return b.sent
}
func (b *httpBatch) Send() (err error) {
defer func() {
b.sent = true
b.release(err)
}()
if b.sent {
return ErrBatchAlreadySent
}
if b.err != nil {
return b.err
}
if b.block.Rows() == 0 {
return nil
}
options := queryOptions(b.ctx)
headers := make(map[string]string)
switch b.conn.compression {
case CompressionGZIP, CompressionDeflate, CompressionBrotli:
headers["Content-Encoding"] = b.conn.compression.String()
case CompressionZSTD, CompressionLZ4:
options.settings["decompress"] = "1"
options.settings["compress"] = "1"
}
compressionWriter := b.conn.compressionPool.Get()
defer b.conn.compressionPool.Put(compressionWriter)
pipeReader, pipeWriter := io.Pipe()
connWriter := compressionWriter.reset(pipeWriter)
go func() {
var err error
defer pipeWriter.CloseWithError(err)
defer connWriter.Close()
b.conn.buffer.Reset()
if err = b.conn.writeData(b.block); err != nil {
return
}
if _, err = connWriter.Write(b.conn.buffer.Buf); err != nil {
return
}
}()
options.settings["query"] = b.query
headers["Content-Type"] = "application/octet-stream"
b.conn.logger.Debug("batch: sending via HTTP",
slog.Int("columns", len(b.block.Columns)),
slog.Int("rows", b.block.Rows()))
res, err := b.conn.sendStreamQuery(b.ctx, pipeReader, &options, headers)
if err != nil {
return fmt.Errorf("batch sendStreamQuery: %w", err)
}
discardAndClose(res.Body)
b.conn.logger.Debug("batch: send complete")
b.block.Reset()
return nil
}
func (b *httpBatch) Rows() int {
return b.block.Rows()
}
func (b *httpBatch) Columns() []column.Interface {
return slices.Clone(b.block.Columns)
}
var _ driver.Batch = (*httpBatch)(nil)