-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathquery.go
More file actions
183 lines (172 loc) · 5.99 KB
/
Copy pathquery.go
File metadata and controls
183 lines (172 loc) · 5.99 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
package presto
import (
"context"
"fmt"
"net/http"
"net/url"
)
// requestQueryResults executes an HTTP request and processes the response as a QueryResults object.
// This is an internal helper method used by the various query execution methods.
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - req: The prepared HTTP request to execute
//
// Returns:
// - The QueryResults containing the query response
// - The HTTP response
// - An error if the request fails or the query returns an error
func (s *Session) requestQueryResults(ctx context.Context, req *http.Request) (*QueryResults, *http.Response, error) {
qr := new(QueryResults)
resp, err := s.Do(ctx, req, qr)
if err != nil {
return nil, resp, err
}
// Maintain the link to the session for the QueryResults object
qr.session = s
if qr.Error != nil {
return qr, resp, qr.Error
}
return qr, resp, nil
}
// Query executes a SQL query on the Presto/Trino server.
// This is the primary method for executing queries and returns the initial query results.
// For large result sets, you'll need to fetch additional batches using the NextUri.
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - query: The SQL query to execute
// - opts: Optional request modifiers
//
// Returns:
// - The initial QueryResults containing schema information and possibly the first batch of data
// - The HTTP response
// - An error if the request fails or the query returns an error
//
// Example:
//
// results, _, err := session.Query(ctx, "SELECT * FROM my_table LIMIT 100")
// if err != nil {
// return err
// }
// // Process results...
func (s *Session) Query(ctx context.Context, query string, opts ...RequestOption) (*QueryResults, *http.Response, error) {
req, err := s.NewRequest("POST", "v1/statement", query, opts...)
if err != nil {
return nil, nil, err
}
return s.requestQueryResults(ctx, req)
}
// QueryWithPreMintedID executes a SQL query using a pre-assigned query ID.
// This is useful when you want to control the query ID for tracking or management purposes.
// If queryId is empty, this method falls back to the standard Query method.
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - query: The SQL query to execute
// - queryId: The pre-assigned query ID
// - slug: A unique identifier for this query execution
// - opts: Optional request modifiers
//
// Returns:
// - The initial QueryResults containing schema information and possibly the first batch of data
// - The HTTP response
// - An error if the request fails or the query returns an error
//
// Example:
//
// results, _, err := session.QueryWithPreMintedID(ctx, "SELECT * FROM my_table", "my-custom-id-123", "my-slug")
// if err != nil {
// return err
// }
// // Process results...
func (s *Session) QueryWithPreMintedID(ctx context.Context, query, queryId, slug string, opts ...RequestOption) (*QueryResults, *http.Response, error) {
if queryId == "" {
return s.Query(ctx, query, opts...)
}
req, err := s.NewRequest("PUT",
fmt.Sprintf("v1/statement/%s?slug=%s", url.PathEscape(queryId), url.QueryEscape(slug)), query, opts...)
if err != nil {
return nil, nil, err
}
return s.requestQueryResults(ctx, req)
}
// FetchNextBatch retrieves the next batch of results for a query.
// This method should be called when a QueryResults has a non-nil NextUri,
// indicating that more data is available.
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - nextUri: The URI for the next batch of results (from QueryResults.NextUri)
// - opts: Optional request modifiers
//
// Returns:
// - The next batch of QueryResults
// - The HTTP response
// - An error if the request fails or the query returns an error
//
// Example:
//
// // Assuming 'results' is a QueryResults with NextUri != nil
// nextResults, _, err := session.FetchNextBatch(ctx, *results.NextUri)
// if err != nil {
// return err
// }
// // Process next batch...
func (s *Session) FetchNextBatch(ctx context.Context, nextUri string, opts ...RequestOption) (*QueryResults, *http.Response, error) {
req, err := s.NewRequest("GET", nextUri, nil, opts...)
if err != nil {
return nil, nil, err
}
return s.requestQueryResults(ctx, req)
}
// CancelQuery cancels a running query.
// This method should be called when you want to stop a query before it completes.
//
// Parameters:
// - ctx: Context for cancellation and timeout
// - nextUri: The URI for the query to cancel (from QueryResults.NextUri)
// - opts: Optional request modifiers
//
// Returns:
// - The final QueryResults for the canceled query
// - The HTTP response
// - An error if the request fails
//
// Example:
//
// // Assuming 'results' is a QueryResults for a running query
// _, _, err := session.CancelQuery(ctx, *results.NextUri)
// if err != nil {
// return err
// }
// // Query has been canceled
func (s *Session) CancelQuery(ctx context.Context, nextUri string, opts ...RequestOption) (*QueryResults, *http.Response, error) {
req, err := s.NewRequest("DELETE", nextUri, nil, opts...)
if err != nil {
return nil, nil, err
}
return s.requestQueryResults(ctx, req)
}
// GetQueryInfo retrieves query execution details from /v1/query/{queryId}.
// The v parameter controls how the response is handled:
// - Pass a struct pointer (e.g., &QueryInfo{}) to decode the JSON response into it
// - Pass an io.Writer (e.g., *os.File) to write the raw JSON response
// - Pass nil to discard the response body
//
// Example:
//
// // Decode into a struct
// var info queryjson.QueryInfo
// resp, err := session.GetQueryInfo(ctx, "20231001_123456_00001_xxxxx", &info)
//
// // Write raw JSON to a file
// file, _ := os.Create("query.json")
// resp, err := session.GetQueryInfo(ctx, "20231001_123456_00001_xxxxx", file)
func (s *Session) GetQueryInfo(ctx context.Context, queryId string, v any, opts ...RequestOption) (*http.Response, error) {
req, err := s.NewRequest("GET", "v1/query/"+url.PathEscape(queryId), nil, opts...)
if err != nil {
return nil, err
}
return s.Do(ctx, req, v)
}