forked from ClickHouse/clickhouse-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn_http_query.go
124 lines (115 loc) · 3.37 KB
/
conn_http_query.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
// Licensed to ClickHouse, Inc. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. ClickHouse, Inc. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package clickhouse
import (
"bytes"
"context"
"errors"
chproto "github.com/ClickHouse/ch-go/proto"
"github.com/ClickHouse/clickhouse-go/v2/lib/proto"
"io"
)
// release is ignored, because http used by std with empty release function
func (h *httpConnect) query(ctx context.Context, release func(*connect, error), query string, args ...any) (*rows, error) {
options := queryOptions(ctx)
query, err := bindQueryOrAppendParameters(true, &options, query, h.location, args...)
if err != nil {
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()
}
for k, v := range h.headers {
headers[k] = v
}
res, err := h.sendQuery(ctx, query, &options, headers)
if err != nil {
return nil, err
}
defer res.Body.Close()
// detect compression from http Content-Encoding header - note user will need to have set enable_http_compression
// for CH to respond with compressed data - we don't set this automatically as they might not have permissions
var body []byte
//adding Accept-Encoding:gzip on your request means response won’t be automatically decompressed per https://github.com/golang/go/blob/master/src/net/http/transport.go#L182-L190
rw := h.compressionPool.Get()
body, err = rw.read(res)
bufferSize := h.blockBufferSize
if options.blockBufferSize > 0 {
// allow block buffer sze to be overridden per query
bufferSize = options.blockBufferSize
}
var (
errCh = make(chan error)
stream = make(chan *proto.Block, bufferSize)
)
if len(body) == 0 {
// queries with no results can get an empty body
go func() {
close(stream)
close(errCh)
}()
return &rows{
err: nil,
stream: stream,
errors: errCh,
block: &proto.Block{},
columns: []string{},
structMap: &structMap{},
}, nil
}
if err != nil {
return nil, err
}
h.compressionPool.Put(rw)
reader := chproto.NewReader(bytes.NewReader(body))
block, err := h.readData(ctx, reader)
if err != nil {
return nil, err
}
go func() {
for {
block, err := h.readData(ctx, reader)
if err != nil {
// ch-go wraps EOF errors
if !errors.Is(err, io.EOF) {
errCh <- err
}
break
}
select {
case <-ctx.Done():
errCh <- ctx.Err()
break
case stream <- block:
}
}
close(stream)
close(errCh)
}()
return &rows{
block: block,
stream: stream,
errors: errCh,
columns: block.ColumnsNames(),
structMap: &structMap{},
}, nil
}