-
Notifications
You must be signed in to change notification settings - Fork 639
Expand file tree
/
Copy pathconn_http_query.go
More file actions
148 lines (134 loc) · 3.51 KB
/
conn_http_query.go
File metadata and controls
148 lines (134 loc) · 3.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
package clickhouse
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"log/slog"
chproto "github.com/ClickHouse/ch-go/proto"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
)
// capturingReader wraps a reader and captures all data that passes through it
type capturingReader struct {
reader io.Reader
buffer bytes.Buffer
}
func (r *capturingReader) Read(p []byte) (n int, err error) {
n, err = r.reader.Read(p)
if n > 0 {
r.buffer.Write(p[:n])
}
return n, err
}
// release is ignored, because http used by std with empty release function
func (h *httpConnect) query(ctx context.Context, release nativeTransportRelease, query string, args ...any) (*rows, error) {
h.logger.Debug("HTTP query", slog.String("sql", query))
options := queryOptions(ctx)
query, err := bindQueryOrAppendParameters(true, &options, query, h.handshake.Timezone, args...)
if err != nil {
err = fmt.Errorf("bindQueryOrAppendParameters: %w", err)
release(h, err)
return nil, err
}
headers := make(map[string]string)
switch h.compression {
case CompressionZSTD, CompressionLZ4:
options.settings["compress"] = "1"
case CompressionGZIP, CompressionDeflate, CompressionBrotli:
// request encoding
headers["Accept-Encoding"] = h.compression.String()
}
res, err := h.sendQuery(ctx, query, &options, headers)
if err != nil {
err = fmt.Errorf("sendQuery: %w", err)
release(h, err)
return nil, err
}
if res.ContentLength == 0 {
discardAndClose(res.Body)
block := proto.NewBlock()
release(h, nil)
return &rows{
block: block,
columns: block.ColumnsNames(),
structMap: &structMap{},
}, nil
}
rw := h.compressionPool.Get()
// The HTTPReaderWriter.NewReader will create a reader that will decompress it if needed,
reader, err := rw.NewReader(res)
if err != nil {
err = fmt.Errorf("NewReader: %w", err)
discardAndClose(res.Body)
h.compressionPool.Put(rw)
release(h, err)
return nil, err
}
// Wrap reader with capturing reader to detect exceptions
capturingRdr := &capturingReader{reader: reader}
bufferedReader := bufio.NewReader(capturingRdr)
chReader := chproto.NewReader(bufferedReader)
block, err := h.readData(chReader, options.userLocation, &capturingRdr.buffer)
if err != nil && !errors.Is(err, io.EOF) {
err = fmt.Errorf("readData: %w", err)
discardAndClose(res.Body)
h.compressionPool.Put(rw)
release(h, err)
return nil, err
}
bufferSize := h.blockBufferSize
if options.blockBufferSize > 0 {
// allow block buffer size to be overridden per query
bufferSize = options.blockBufferSize
}
var (
errCh = make(chan error)
stream = make(chan *proto.Block, bufferSize)
)
go func() {
for {
block, err := h.readData(chReader, options.userLocation, &capturingRdr.buffer)
if err != nil {
// ch-go wraps EOF errors
if !errors.Is(err, io.EOF) {
errCh <- fmt.Errorf("readData stream: %w", err)
}
break
}
select {
case <-ctx.Done():
errCh <- ctx.Err()
break
case stream <- block:
}
}
discardAndClose(res.Body)
h.compressionPool.Put(rw)
close(stream)
close(errCh)
release(h, nil)
}()
if block == nil {
block = proto.NewBlock()
}
return &rows{
block: block,
stream: stream,
errors: errCh,
columns: block.ColumnsNames(),
structMap: &structMap{},
}, nil
}
func (h *httpConnect) queryRow(ctx context.Context, release nativeTransportRelease, query string, args ...any) *row {
rows, err := h.query(ctx, release, query, args...)
if err != nil {
return &row{
err: err,
}
}
return &row{
rows: rows,
}
}