Files
go/conn_http_query.go
zeekay 0c90ebe02e debrand: finish the rename across source, docs and dev infra
Source: the unexported clickhouse struct becomes client, the ch receiver and
chproto import alias follow, error message prefixes read datastore, and the
JSON serializer interface drops the vendor infix. resources' metadata key and
its accessor are renamed together, with a test proving the embedded YAML still
unmarshals — a silent drift there would have made every connection take the
unsupported-server-version branch.

Codegen: all three templates still emitted the upstream module paths, so
regenerating would have quietly undone the rename. Note the templates have
drifted from the files they produce (regenerating today drops QBit and
scanTypeDuration), so the generated files are left as tracked and only the
templates are corrected.

Dead links: an earlier mechanical pass invented URLs that do not resolve —
github.com/ClickHouse/datastore-go, hanzo-ds/driver issue numbers that belong to
upstream, and mangled names of independent projects (mailru, uptrace, nikepan,
EpicStep). Third-party names are not ours to rewrite, so those directories are
removed rather than repaired, and provenance comments now point at real files.

Dev infra: compose runs our own server image at the tag production runs, since
the vendor's alpine tag does not exist in our registry. Cluster hostnames,
nginx upstreams and server configs are renamed as one set.

CHANGELOG.md is upstream's release history for versions this module never
released; rewriting it would fabricate a lineage, so it is dropped and the
upstream base recorded in README instead.

The two long-standing vet findings are fixed by keying the struct literals
rather than suppressed. LICENSE and contributors/list keep the vendor name on
purpose: one is the copyright notice Apache-2.0 requires, the other is real
people's names. CI now enforces build, vet, test, gofmt and a grep gate.
2026-07-26 11:46:39 -07:00

149 lines
3.5 KiB
Go

package datastore
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"log/slog"
"github.com/hanzo-ds/go/lib/proto"
nativeproto "github.com/hanzo-ds/native/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 := nativeproto.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,
}
}