-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery.go
401 lines (337 loc) · 10 KB
/
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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package cmd
import (
"context"
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/chzyer/readline"
"github.com/spf13/cobra"
"log/slog"
"github.com/rockset/rockset-go-client"
"github.com/rockset/rockset-go-client/openapi"
"github.com/rockset/rockset-go-client/option"
"github.com/rockset/rockset-go-client/paginate"
"github.com/rockset/cli/config"
"github.com/rockset/cli/flag"
"github.com/rockset/cli/format"
"github.com/rockset/cli/lookup"
"github.com/rockset/cli/tui"
)
func newListQueriesCmd() *cobra.Command {
cmd := cobra.Command{
Use: "queries [ID|NAME]",
Aliases: []string{"query", "q"},
Short: "list queries",
Long: "list all actively queued and executing queries, or on a specific virtual instance",
Args: cobra.RangeArgs(0, 1),
Annotations: group("query"), // TODO should this be in the VI group too?
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rs, err := config.Client(cmd, Version)
if err != nil {
return err
}
var list []openapi.QueryInfo
if len(args) == 0 {
list, err = rs.ListActiveQueries(ctx)
} else {
id, err := lookup.VirtualInstanceNameOrIDtoID(ctx, rs, args[0])
if err != nil {
return err
}
list, err = rs.ListVirtualInstanceQueries(ctx, id)
}
if err != nil {
return err
}
return formatList(cmd, format.ToInterfaceArray(list))
},
}
cmd.Flags().Bool(flag.Wide, false, "display more information")
return &cmd
}
func newGetQueryInfoCmd() *cobra.Command {
cmd := cobra.Command{
Use: "info ID",
Aliases: []string{"i"},
Short: "get query info",
Long: "get information about a query",
Args: cobra.ExactArgs(1),
Annotations: group("query"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rs, err := config.Client(cmd, Version)
if err != nil {
return err
}
q, err := rs.GetQueryInfo(ctx, args[0])
if err != nil {
return err
}
return formatOne(cmd, q)
},
}
cmd.Flags().Bool(flag.Wide, false, "display more information")
return &cmd
}
func newGetQueryResultCmd() *cobra.Command {
cmd := cobra.Command{
Use: "results ID",
Aliases: []string{"r"},
Short: "get query results",
Long: fmt.Sprintf("Get query results for a previously executed query. "+
"If --%s isn't specified, all documents are retrieved.", flag.Docs),
Args: cobra.ExactArgs(1),
Annotations: group("query"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rs, err := config.Client(cmd, Version)
if err != nil {
return err
}
// have to get info to get thew query stats
info, err := rs.GetQueryInfo(ctx, args[0])
if err != nil {
return err
}
var options []option.QueryResultOption
if cursor, _ := cmd.Flags().GetString(flag.Cursor); cursor != "" {
options = append(options, option.WithQueryResultCursor(cursor))
}
docs, _ := cmd.Flags().GetInt32(flag.Docs)
if docs != 0 {
options = append(options, option.WithQueryResultDocs(docs))
}
if offset, _ := cmd.Flags().GetInt32(flag.Offset); offset != 0 {
options = append(options, option.WithQueryResultOffset(offset))
}
stats := info.GetStats()
var list []map[string]interface{}
var cursor string
// if we don't have any options, we fetch all documents
if len(options) == 0 {
p := paginate.New(rs)
docCh := make(chan map[string]any, 100)
go func() {
err = p.GetQueryResults(ctx, docCh, args[0])
}()
for doc := range docCh {
list = append(list, doc)
}
} else {
result, err := rs.GetQueryResults(ctx, args[0], options...)
if err != nil {
return err
}
page := result.GetPagination()
cursor = page.GetNextCursor()
list = result.Results
}
return showQueryPaginationResponse(cmd.OutOrStdout(), cursor, stats.GetElapsedTimeMs(), list)
},
}
cmd.Flags().Bool(flag.Wide, false, "display more information")
cmd.Flags().String(flag.Cursor, "", "cursor to current page, defaults to first page")
cmd.Flags().Int32(flag.Docs, 0, "number of documents to fetch, 0 means fetch max")
cmd.Flags().Int32(flag.Offset, 0, "offset from the cursor of the first document to be returned")
return &cmd
}
func newQueryCmd() *cobra.Command {
cmd := cobra.Command{
Use: "query SQL",
Short: "execute SQL query",
Long: "query Rockset collections",
Annotations: group("query"),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
rs, err := config.Client(cmd, Version)
if err != nil {
return err
}
async, _ := cmd.Flags().GetBool(flag.Async)
vi, _ := cmd.Flags().GetString(flag.VI)
file, _ := cmd.Flags().GetString(flag.File)
validate, _ := cmd.Flags().GetBool(flag.Validate)
// TODO handle a parameterized query
var sql string
// start an interactive session
if file == "" && len(args) == 0 {
if validate {
return fmt.Errorf("can't validate interactive commands")
}
return interactiveQuery(ctx, io.NopCloser(cmd.InOrStdin()), cmd.OutOrStdout(), rs)
}
if file != "" && len(args) > 0 {
return fmt.Errorf("you can only specify one of --file or a SQL query")
}
if file != "" {
data, err := os.ReadFile(file)
if err != nil {
return err
}
sql = string(data)
} else {
sql = args[0]
}
if validate {
_, err = rs.ValidateQuery(ctx, sql)
if err != nil {
return err
}
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "SQL is valid\n")
return nil
}
var options []option.QueryOption
if async {
// TODO inform the user that --validate and --async are mutually exclusive?
options = append(options, option.WithAsync())
}
var result openapi.QueryResponse
if vi == "" {
result, err = rs.Query(ctx, sql, options...)
} else {
result, err = rs.ExecuteQueryOnVirtualInstance(ctx, vi, sql, options...)
}
if err != nil {
return err
}
if async {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "query ID is: %s\n", result.GetQueryId())
return nil
}
err = showQueryResponse(cmd.OutOrStdout(), result)
if err != nil {
return err
}
return nil
},
}
cmd.Flags().Bool(flag.Async, false, "execute the query asynchronously")
cmd.Flags().Bool(flag.Validate, false, "validate SQL")
cmd.Flags().String(flag.File, "", "read SQL from file")
cmd.Flags().String(flag.VI, "", "execute query on virtual instance")
_ = cobra.MarkFlagFilename(cmd.Flags(), flag.File, ".sql")
return &cmd
}
func showQueryPaginationResponse(out io.Writer, cursor string, elapsedMs int64, results []map[string]interface{}) error {
if len(results) == 0 {
return errors.New("query returned no rows")
}
var headers []string
for h := range results[0] {
headers = append(headers, h)
}
showQueryResponseTable(out, cursor, elapsedMs, headers, results)
return nil
}
func showQueryResponse(out io.Writer, result openapi.QueryResponse) error {
switch result.GetStatus() {
case "ERROR":
var errs []string
for _, e := range result.GetQueryErrors() {
errs = append(errs, e.GetMessage())
}
_, _ = fmt.Fprintf(out, "query %s failed:\n%s\n", result.GetQueryId(), strings.Join(errs, "\n"))
case "QUEUED", "RUNNING":
_, _ = fmt.Fprintf(out, "your query %s is %s\n", result.GetQueryId(), result.GetStatus())
case "COMPLETED":
var headers []string
if len(result.GetColumnFields()) == 0 {
// in a "SELECT *" query the ColumnFields isn't populated what order should the columns be presented in?
for h := range result.Results[0] {
headers = append(headers, h)
}
} else {
for _, h := range result.GetColumnFields() {
headers = append(headers, h.Name)
}
}
stats := result.GetStats()
showQueryResponseTable(out, "", stats.GetElapsedTimeMs(), headers, result.Results)
default:
return fmt.Errorf("unexpected query status: %s", result.GetStatus())
}
return nil
}
func showQueryResponseTable(out io.Writer, cursor string, elapsedMs int64, headers []string, results []map[string]interface{}) {
t := tui.NewTable(out)
t.Headers(headers...)
for _, row := range results {
var r []string
for _, h := range headers {
r = append(r, fmt.Sprintf("%v", row[h]))
}
t.Row(r...)
}
_, _ = fmt.Fprintln(out, t.Render())
if cursor != "" {
_, _ = fmt.Fprintf(out, "Next cursor: %s\n", cursor)
}
_, _ = fmt.Fprintf(out, "Elapsed time: %d ms\n\n", elapsedMs)
}
func interactiveQuery(ctx context.Context, in io.ReadCloser, out io.Writer, rs *rockset.RockClient) error {
histFile, err := config.HistoryFile()
if err != nil {
return err
}
_, _ = fmt.Fprintf(out, "%s interactive console. End your SQL with ;\n", tui.Rockset)
rl, err := readline.NewEx(&readline.Config{
Prompt: tui.Prompt,
Stdin: in,
Stdout: out,
HistoryFile: histFile,
})
if err != nil {
return err
}
defer func() {
if err := rl.Close(); err != nil {
slog.Error("failed to close readline", "err", err)
}
}()
var cmds []string
for {
line, err := rl.Readline()
if err != nil {
if len(cmds) > 0 {
// in case someone is sending the SQL over a pipe and there isn't a ";" at the end
executeQuery(ctx, out, rs, strings.Join(cmds, " "))
}
break
}
line = strings.TrimSpace(line)
if len(line) == 0 {
continue
}
cmds = append(cmds, line)
if !strings.HasSuffix(line, ";") {
rl.SetPrompt(tui.ContinuationPrompt)
continue
}
sql := strings.Join(cmds, " ")
cmds = cmds[:0]
rl.SetPrompt(tui.Prompt)
if strings.HasPrefix(sql, "help") {
_, _ = fmt.Fprintf(out, "help command TBW\n")
continue
}
if err = rl.SaveHistory(sql); err != nil {
slog.Error("failed to save history", "err", err)
}
executeQuery(ctx, out, rs, sql)
}
return nil
}
func executeQuery(ctx context.Context, out io.Writer, rs *rockset.RockClient, sql string) {
result, err := rs.Query(ctx, sql)
if err != nil {
// TODO should this use tui.ShowError()?
_, _ = fmt.Fprintf(out, "%s\n", tui.ErrorStyle.Render("query failed:", err.Error()))
return
}
if err = showQueryResponse(out, result); err != nil {
slog.Error("failed to show result", "err", err)
}
}