forked from influxdata/influxdb-iox-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iox_client_query.go
125 lines (115 loc) · 3.47 KB
/
iox_client_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
125
package influxdbiox
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
"github.com/apache/arrow/go/v10/arrow/flight"
"github.com/apache/arrow/go/v10/arrow/ipc"
"github.com/apache/arrow/go/v10/arrow/memory"
"google.golang.org/grpc"
)
// Handshake the InfluxDB/IOx service, possibly (re-)connecting to the gRPC
// service in the process.
func (c *Client) Handshake(ctx context.Context) error {
response, err := c.flightClient.Handshake(ctx)
if err != nil {
return err
}
payload := make([]byte, 16)
if _, err = rand.Read(payload); err != nil {
return err
}
if err = response.Send(&flight.HandshakeRequest{Payload: payload}); err != nil {
return err
}
resp, err := response.Recv()
if err != nil {
return err
}
if !bytes.Equal(resp.Payload, payload) && resp.Payload != nil {
return errors.New("handshake payload response does not match request")
}
return nil
}
type ticketReadInfo struct {
NamespaceName string `json:"namespace_name"`
SQLQuery string `json:"sql_query"`
}
// PrepareQuery prepares a query request.
//
// If database is "" then the configured default is used.
func (c *Client) PrepareQuery(ctx context.Context, database, query string) (*QueryRequest, error) {
if database == "" {
database = c.config.Namespace
}
return newRequest(c, database, query), nil
}
// QueryRequest represents a prepared query.
type QueryRequest struct {
client *Client
database string
query string
grpcCallOptions []grpc.CallOption
allocator memory.Allocator
}
func newRequest(client *Client, database, query string) *QueryRequest {
return &QueryRequest{
client: client,
database: database,
query: query,
allocator: memory.DefaultAllocator,
}
}
// WithCallOption adds a grpc.CallOption to be included when the gRPC service
// is called.
func (r *QueryRequest) WithCallOption(grpcCallOption grpc.CallOption) *QueryRequest {
return &QueryRequest{
client: r.client,
database: r.database,
query: r.query,
grpcCallOptions: append(r.grpcCallOptions, grpcCallOption),
allocator: r.allocator,
}
}
// WithAllocator provides an Arrow allocator the that flight.Reader will use to
// account for memory allocated for record batches pulled off the wire.
func (r *QueryRequest) WithAllocator(alloc memory.Allocator) *QueryRequest {
return &QueryRequest{
client: r.client,
database: r.database,
query: r.query,
grpcCallOptions: r.grpcCallOptions,
allocator: alloc,
}
}
// Query sends a query via the Flight RPC DoGet.
//
// The returned *flight.Reader must be released when the caller is done with it.
//
// reader, err := request.Query(ctx)
// defer reader.Release()
// ...
func (r *QueryRequest) Query(ctx context.Context, args ...interface{}) (*flight.Reader, error) {
if len(args) > 0 {
return nil, errors.New("query arguments are not supported")
}
ticket, err := json.Marshal(ticketReadInfo{
NamespaceName: r.database,
SQLQuery: r.query,
})
if err != nil {
return nil, fmt.Errorf("failed to marshal Arrow DoGet ticket: %w", err)
}
doGetClient, err := r.client.flightClient.DoGet(ctx, &flight.Ticket{Ticket: ticket}, r.grpcCallOptions...)
if err != nil {
return nil, fmt.Errorf("arrow Flight DoGet request failed: %w", err)
}
flightReader, err := flight.NewRecordReader(doGetClient, ipc.WithAllocator(r.allocator))
if err != nil {
return nil, fmt.Errorf("failed to create Flight record reader: %w", err)
}
return flightReader, nil
}