-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathselect.go
438 lines (386 loc) · 11.4 KB
/
select.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package command
import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/urfave/cli/v2"
errorpkg "github.com/peak/s5cmd/v2/error"
"github.com/peak/s5cmd/v2/log/stat"
"github.com/peak/s5cmd/v2/parallel"
"github.com/peak/s5cmd/v2/storage"
"github.com/peak/s5cmd/v2/storage/url"
)
var selectHelpTemplate = `Name:
{{.HelpName}} - {{.Usage}}
Usage:
{{.HelpName}} [options] argument
Options:
{{range .VisibleFlags}}{{.}}
{{end}}
Examples:
01. Select the average price of the avocado and amount sold, set the output format as csv
> s5cmd select csv -use-header USE --query "SELECT s.avg_price, s.quantity FROM S3Object s WHERE s.item='avocado'" "s3://bucket/prices.csv"
02. Query TSV files
> s5cmd select csv --delimiter=\t --use-header USE --query "SELECT s.avg_price, s.quantity FROM S3Object s WHERE s.item='avocado'" "s3://bucket/prices.tsv"
03. Select a specific field in a JSON document
> s5cmd select json --structure document --query "SELECT s.tracking_id FROM s3object[*]['metadata']['.zattrs'] s" "s3://bucket/metadata.json"
04. Query files that contain lines of JSON objects
> s5cmd select json --query "SELECT s.id FROM s3object s WHERE s.lineNumber = 1"
`
func beforeFunc(c *cli.Context) error {
err := validateSelectCommand(c)
if err != nil {
printError(commandFromContext(c), c.Command.Name, err)
}
return err
}
func buildSelect(c *cli.Context, inputFormat string, inputStructure *string) (cmd *Select, err error) {
defer stat.Collect(c.Command.FullName(), &err)()
fullCommand := commandFromContext(c)
src, err := url.New(
c.Args().Get(0),
url.WithVersion(c.String("version-id")),
url.WithRaw(c.Bool("raw")),
url.WithAllVersions(c.Bool("all-versions")),
)
if err != nil {
printError(fullCommand, c.Command.Name, err)
return nil, err
}
outputFormat := c.String("output-format")
if c.String("output-format") == "" {
outputFormat = inputFormat
}
cmd = &Select{
src: src,
op: c.Command.Name,
fullCommand: fullCommand,
// flags
inputFormat: inputFormat,
outputFormat: outputFormat,
query: c.String("query"),
compressionType: c.String("compression"),
exclude: c.StringSlice("exclude"),
forceGlacierTransfer: c.Bool("force-glacier-transfer"),
ignoreGlacierWarnings: c.Bool("ignore-glacier-warnings"),
storageOpts: NewStorageOpts(c),
}
// parquet files don't have an input structure
if inputStructure != nil {
cmd.inputStructure = *inputStructure
}
return cmd, nil
}
func NewSelectCommand() *cli.Command {
sharedFlags := []cli.Flag{
&cli.StringFlag{
Name: "query",
Aliases: []string{"e"},
Usage: "SQL expression to use to select from the objects",
},
&cli.StringFlag{
Name: "output-format",
Usage: "output format of the result (options: json, csv)",
},
&cli.StringSliceFlag{
Name: "exclude",
Usage: "exclude objects with given pattern",
},
&cli.BoolFlag{
Name: "force-glacier-transfer",
Usage: "force transfer of glacier objects whether they are restored or not",
},
&cli.BoolFlag{
Name: "ignore-glacier-warnings",
Usage: "turns off glacier warnings: ignore errors encountered during selecting objects",
},
&cli.BoolFlag{
Name: "raw",
Usage: "disable the wildcard operations, useful with filenames that contains glob characters",
},
&cli.BoolFlag{
Name: "all-versions",
Usage: "list all versions of object(s)",
},
&cli.StringFlag{
Name: "version-id",
Usage: "use the specified version of the object",
},
}
cmd := &cli.Command{
Name: "select",
HelpName: "select",
Usage: "run SQL queries on objects",
Subcommands: []*cli.Command{
{
Name: "csv",
Usage: "run queries on csv files",
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "delimiter for the csv file",
Usage: "delimiter of the csv file.",
Value: ",",
},
&cli.StringFlag{
Name: "use-header",
Usage: "use header of the csv file. (options for AWS: IGNORE, NONE, USE)",
Value: "NONE",
},
&cli.StringFlag{
Name: "compression",
Usage: "input compression format (options for AWS: GZIP or BZIP2)",
},
}, sharedFlags...),
CustomHelpTemplate: selectHelpTemplate,
Before: beforeFunc,
Action: func(c *cli.Context) (err error) {
delimiter := c.String("delimiter")
// We are doing this because the delimiters that are special characters
// are automatically escaped by go. We quote/unquote to capture the actual
// delimiter. See: https://stackoverflow.com/questions/59952721/how-to-read-t-as-tab-in-golang
quotedDelimiter, err := strconv.Unquote(`"` + delimiter + `"`)
if err != nil {
printError("select csv", c.Command.Name, err)
return err
}
cmd, err := buildSelect(c, "csv", "edDelimiter)
// Since this flag is only specific to csv
// and we set a default value, we tend
// to set it explicitly after building
// the command rather than setting it
// on the shared builder. We also
// show the options, but we don't
// constraint the options that can be
// passed to this flag, since other
// providers might support other options
// that AWS does not support.
cmd.fileHeaderInfo = c.String("use-header")
if err != nil {
printError(cmd.fullCommand, c.Command.Name, err)
return err
}
return cmd.Run(c.Context)
},
},
{
Name: "json",
Usage: "run queries on json files",
Flags: append([]cli.Flag{
&cli.GenericFlag{
Name: "structure",
Usage: "how objects are aligned in the json file, options:(lines, document)",
Value: &EnumValue{
Enum: []string{"lines", "document"},
Default: "lines",
ConditionFunction: func(str, target string) bool {
return strings.ToLower(target) == str
},
},
},
&cli.StringFlag{
Name: "compression",
Usage: "input compression format (options for AWS: GZIP or BZIP2)",
},
}, sharedFlags...),
CustomHelpTemplate: selectHelpTemplate,
Before: beforeFunc,
Action: func(c *cli.Context) (err error) {
structure := c.String("structure")
cmd, err := buildSelect(c, "json", &structure)
if err != nil {
printError(cmd.fullCommand, c.Command.Name, err)
return err
}
return cmd.Run(c.Context)
},
},
{
Name: "parquet",
Usage: "run queries on parquet files",
Flags: sharedFlags,
CustomHelpTemplate: selectHelpTemplate,
Before: beforeFunc,
Action: func(c *cli.Context) (err error) {
cmd, err := buildSelect(c, "parquet", nil)
if err != nil {
printError(cmd.fullCommand, c.Command.Name, err)
return err
}
return cmd.Run(c.Context)
},
},
},
Flags: append([]cli.Flag{
&cli.StringFlag{
Name: "compression",
Usage: "input compression format (options for AWS: GZIP or BZIP2)",
},
}, sharedFlags...),
Before: func(c *cli.Context) (err error) {
if c.Args().Len() == 0 {
err = fmt.Errorf("expected source argument")
printError(commandFromContext(c), c.Command.Name, err)
return err
}
return nil
},
Action: func(c *cli.Context) (err error) {
// default fallback
structure := "lines"
cmd, err := buildSelect(c, "json", &structure)
if err != nil {
printError(cmd.fullCommand, c.Command.Name, err)
return err
}
return cmd.Run(c.Context)
},
CustomHelpTemplate: selectHelpTemplate,
}
cmd.BashComplete = getBashCompleteFn(cmd, true, false)
return cmd
}
// Select holds select operation flags and states.
type Select struct {
src *url.URL
op string
fullCommand string
query string
inputFormat string
compressionType string
inputStructure string
fileHeaderInfo string
outputFormat string
exclude []string
forceGlacierTransfer bool
ignoreGlacierWarnings bool
// s3 options
storageOpts storage.Options
}
// Run starts copying given source objects to destination.
func (s Select) Run(ctx context.Context) error {
client, err := storage.NewRemoteClient(ctx, s.src, s.storageOpts)
if err != nil {
printError(s.fullCommand, s.op, err)
return err
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
objch, err := expandSource(ctx, client, false, s.src)
if err != nil {
printError(s.fullCommand, s.op, err)
return err
}
var (
merrorWaiter error
merrorObjects error
)
waiter := parallel.NewWaiter()
errDoneCh := make(chan struct{})
writeDoneCh := make(chan struct{})
resultCh := make(chan json.RawMessage, 128)
go func() {
defer close(errDoneCh)
for err := range waiter.Err() {
printError(s.fullCommand, s.op, err)
merrorWaiter = multierror.Append(merrorWaiter, err)
}
}()
go func() {
defer close(writeDoneCh)
var fatalError error
for {
record, ok := <-resultCh
if !ok {
break
}
if fatalError != nil {
// Drain the channel.
continue
}
if _, err := os.Stdout.Write(append(record, '\n')); err != nil {
// Stop reading upstream. Notably useful for EPIPE.
cancel()
printError(s.fullCommand, s.op, err)
fatalError = err
}
}
}()
excludePatterns, err := createRegexFromWildcard(s.exclude)
if err != nil {
printError(s.fullCommand, s.op, err)
return err
}
for object := range objch {
if object.Type.IsDir() || errorpkg.IsCancelation(object.Err) {
continue
}
if err := object.Err; err != nil {
merrorObjects = multierror.Append(merrorObjects, err)
printError(s.fullCommand, s.op, err)
continue
}
if object.StorageClass.IsGlacier() && !s.forceGlacierTransfer {
if !s.ignoreGlacierWarnings {
err := fmt.Errorf("object '%v' is on Glacier storage", object)
merrorObjects = multierror.Append(merrorObjects, err)
printError(s.fullCommand, s.op, err)
}
continue
}
if isURLMatched(excludePatterns, object.URL.Path, s.src.Prefix) {
continue
}
task := s.prepareTask(ctx, client, object.URL, resultCh)
parallel.Run(task, waiter)
}
waiter.Wait()
close(resultCh)
<-errDoneCh
<-writeDoneCh
return multierror.Append(merrorWaiter, merrorObjects).ErrorOrNil()
}
func (s Select) prepareTask(ctx context.Context, client *storage.S3, url *url.URL, resultCh chan<- json.RawMessage) func() error {
return func() error {
query := &storage.SelectQuery{
ExpressionType: "SQL",
Expression: s.query,
InputFormat: s.inputFormat,
InputContentStructure: s.inputStructure,
FileHeaderInfo: s.fileHeaderInfo,
OutputFormat: s.outputFormat,
CompressionType: s.compressionType,
}
return client.Select(ctx, url, query, resultCh)
}
}
func validateSelectCommand(c *cli.Context) error {
if c.Args().Len() != 1 {
return fmt.Errorf("expected source argument")
}
if err := checkVersioningFlagCompatibility(c); err != nil {
return err
}
if err := checkVersioningWithGoogleEndpoint(c); err != nil {
return err
}
srcurl, err := url.New(
c.Args().Get(0),
url.WithVersion(c.String("version-id")),
url.WithRaw(c.Bool("raw")),
url.WithAllVersions(c.Bool("all-versions")),
)
if err != nil {
return err
}
if !srcurl.IsRemote() {
return fmt.Errorf("source must be remote")
}
if c.String("query") == "" {
return fmt.Errorf("query must be non-empty")
}
return nil
}