-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
559 lines (471 loc) · 15.5 KB
/
main.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package main
import (
"bytes"
"context"
"encoding/csv"
"errors"
"github.com/apstndb/execspansql/internal"
"github.com/apstndb/execspansql/params"
"google.golang.org/api/iterator"
"io"
"iter"
"regexp"
"slices"
"spheric.cloud/xiter"
"time"
"fmt"
"log"
"os"
"encoding/json"
texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
oteloc "go.opentelemetry.io/otel/bridge/opencensus"
"go.opentelemetry.io/otel"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"google.golang.org/protobuf/proto"
"gopkg.in/yaml.v3"
"google.golang.org/api/option"
"google.golang.org/grpc"
grpczap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
"go.uber.org/zap"
"cloud.google.com/go/spanner"
sppb "cloud.google.com/go/spanner/apiv1/spannerpb"
"github.com/apstndb/spannerotel/interceptor"
"github.com/itchyny/gojq"
"github.com/jessevdk/go-flags"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"
)
func main() {
if err := _main(); err != nil {
log.Fatalln(err)
}
}
var dmlRe = regexp.MustCompile(`(?is)^\s*(INSERT|UPDATE|DELETE)\s+.+$`)
var debuglog *log.Logger
func init() {
if os.Getenv("DEBUG") != "" {
debuglog = log.New(os.Stderr, "", log.LstdFlags)
} else {
debuglog = log.New(io.Discard, "", log.LstdFlags)
}
// suppress
_ = debuglog
}
type opts struct {
Positional struct {
Database string `positional-arg-name:"database" description:"(required) ID of the database." required:"true"`
} `positional-args:"yes"`
Sql string `long:"sql" description:"SQL query text; exclusive with --sql-file."`
SqlFile string `long:"sql-file" description:"File name contains SQL query; exclusive with --sql"`
Project string `long:"project" short:"p" description:"(required) ID of the project." required:"true" env:"CLOUDSDK_CORE_PROJECT"`
Instance string `long:"instance" short:"i" description:"(required) ID of the instance." required:"true" env:"CLOUDSDK_SPANNER_INSTANCE"`
QueryMode string `long:"query-mode" description:"Query mode." default:"NORMAL" choice:"NORMAL" choice:"PLAN" choice:"PROFILE" choice:"WITH_STATS" choice:"WITH_PLAN_AND_STATS"`
Format string `long:"format" description:"Output format." default:"json" choice:"json" choice:"yaml" choice:"experimental_csv"`
RedactRows bool `long:"redact-rows" description:"Redact result rows from output"`
CompactOutput bool `long:"compact-output" short:"c" description:"Compact JSON output(--compact-output of jq)"`
JqFilter string `long:"filter" description:"jq filter"`
JqRawOutput bool `long:"raw-output" short:"r" description:"(--raw-output of jq)"`
JqFromFile string `long:"filter-file" description:"(--from-file of jq)"`
Param map[string]string `long:"param" description:"[name]:[Cloud Spanner type(PLAN only) or literal]"`
LogGrpc bool `long:"log-grpc" description:"Show gRPC logs"`
TraceProject string `long:"experimental-trace-project"`
EnablePartitionedDML bool `long:"enable-partitioned-dml" description:"Execute DML statement using Partitioned DML"`
Timeout time.Duration `long:"timeout" default:"10m" description:"Maximum time to wait for the SQL query to complete"`
TryPartitionQuery bool `long:"try-partition-query" description:"(Experimental) Check whether the query can be executed as partition query or not"`
TimestampBound struct {
Strong bool `long:"strong" description:"Perform a strong query."`
ReadTimestamp string `long:"read-timestamp" description:"Perform a query at the given timestamp. (micro-seconds precision)" value-name:"TIMESTAMP"`
} `group:"Timestamp Bound"`
}
func processFlags() (o opts, err error) {
flagParser := flags.NewParser(&o, flags.Default)
defer func() {
if err == nil {
return
}
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
return
}
log.Print(err)
flagParser.WriteHelp(os.Stderr)
}()
_, err = flagParser.Parse()
if err != nil {
return o, err
}
if _, err := time.Parse(time.RFC3339Nano, o.TimestampBound.ReadTimestamp); o.TimestampBound.ReadTimestamp != "" && err != nil {
return o, fmt.Errorf("--read-timestamp is supplied but wrong: %w", err)
}
if o.TimestampBound.Strong && o.TimestampBound.ReadTimestamp != "" {
return o, errors.New("--strong and --read-timestamp are exclusive")
}
if o.Sql != "" && o.SqlFile != "" {
return o, errors.New("--sql and --sql-file are exclusive")
}
if o.Sql == "" && o.SqlFile == "" {
return o, errors.New("--sql or --sql-file is required")
}
if o.JqFilter != "" && o.JqFromFile != "" {
return o, errors.New("--jq-filter and --jq-from-file are exclusive")
}
return o, nil
}
// readFileOrDefault returns content of filename or s if filename is empty
func readFileOrDefault(filename, s string) (string, error) {
if filename == "" {
return s, nil
}
b, err := os.ReadFile(filename)
if err != nil {
return "", err
}
return string(b), nil
}
func logGrpcClientOptions() []option.ClientOption {
zapDevelopmentConfig := zap.NewDevelopmentConfig()
zapDevelopmentConfig.DisableCaller = true
zapLogger, _ := zapDevelopmentConfig.Build(zap.Fields())
return []option.ClientOption{
option.WithGRPCDialOption(grpc.WithChainUnaryInterceptor(
grpczap.PayloadUnaryClientInterceptor(zapLogger, func(ctx context.Context, fullMethodName string) bool {
return true
}),
grpczap.UnaryClientInterceptor(zapLogger),
)),
option.WithGRPCDialOption(grpc.WithChainStreamInterceptor(
grpczap.PayloadStreamClientInterceptor(zapLogger, func(ctx context.Context, fullMethodName string) bool {
return true
}),
grpczap.StreamClientInterceptor(zapLogger),
)),
}
}
type queryMode interface{ isQueryMode() }
type single struct{ spanner.TimestampBound }
type readWrite struct{}
type partitionedDML struct{}
func (s single) isQueryMode() {}
func (r readWrite) isQueryMode() {}
func (p partitionedDML) isQueryMode() {}
func runInNewTransaction(ctx context.Context, client *spanner.Client, stmt spanner.Statement, opts spanner.QueryOptions, mode queryMode, reductRows bool) (*sppb.ResultSet, error) {
var rs *sppb.ResultSet
switch mode := mode.(type) {
case readWrite:
_, err := client.ReadWriteTransaction(ctx, func(ctx context.Context, tx *spanner.ReadWriteTransaction) (err error) {
rs, err = consumeRowIterIntoResultSet(tx.QueryWithOptions(ctx, stmt, opts), reductRows)
return err
})
return rs, err
case single:
return consumeRowIterIntoResultSet(client.Single().WithTimestampBound(mode.TimestampBound).QueryWithOptions(ctx, stmt, opts), reductRows)
case partitionedDML:
count, err := client.PartitionedUpdateWithOptions(ctx, stmt, opts)
return &sppb.ResultSet{
Metadata: &sppb.ResultSetMetadata{
RowType: &sppb.StructType{},
},
Stats: &sppb.ResultSetStats{
RowCount: &sppb.ResultSetStats_RowCountLowerBound{RowCountLowerBound: count},
},
}, err
default:
panic(fmt.Sprintf("unknown mode: %d", mode))
}
}
func cloudOperationsExporter(project string) (sdktrace.SpanExporter, error) {
exporter, err := texporter.New(texporter.WithProjectID(project))
if err != nil {
return nil, fmt.Errorf("texporter.New: %v", err)
}
return exporter, err
}
func TracerProvider(project string) (*sdktrace.TracerProvider, error) {
exp, err := cloudOperationsExporter(project)
if err != nil {
return nil, err
}
tp := sdktrace.NewTracerProvider(
// Always be sure to batch in production.
sdktrace.WithBatcher(exp),
)
return tp, nil
}
func _main() error {
o, err := processFlags()
if err != nil {
os.Exit(1)
}
ctx, cancel := context.WithTimeout(context.Background(), o.Timeout)
defer cancel()
// Use jqQuery even if empty filter
jqFilter, err := readFileOrDefault(o.JqFromFile, o.JqFilter)
if err != nil {
return err
}
// Overwrite to "." because gojq don't support empty query
if jqFilter == "" {
jqFilter = "."
}
jqQuery, err := gojq.Parse(jqFilter)
if err != nil {
return err
}
mode := sppb.ExecuteSqlRequest_QueryMode(sppb.ExecuteSqlRequest_QueryMode_value[o.QueryMode])
query, err := readFileOrDefault(o.SqlFile, o.Sql)
if err != nil {
return err
}
doTrace := o.TraceProject != ""
if doTrace {
tp, err := TracerProvider(o.TraceProject)
if err != nil {
return err
}
otel.SetTracerProvider(tp)
oteloc.InstallTraceBridge()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Cleanly shutdown and flush telemetry when the application exits.
defer func(ctx context.Context) {
// Do not make the application hang when it is shutdown.
ctx, cancel = context.WithTimeout(ctx, time.Second*5)
defer cancel()
if err := tp.Shutdown(ctx); err != nil {
log.Fatal(err)
}
}(ctx)
}
logGrpc := o.LogGrpc
client, err := newClient(ctx, o.Project, o.Instance, o.Positional.Database, logGrpc, doTrace)
if err != nil {
return err
}
defer client.Close()
paramMap, err := params.GenerateParams(o.Param, mode == sppb.ExecuteSqlRequest_PLAN)
if err != nil {
return err
}
var tb spanner.TimestampBound
if o.TimestampBound.ReadTimestamp != "" {
ts, err := time.Parse(time.RFC3339Nano, o.TimestampBound.ReadTimestamp)
if err != nil {
return err
}
tb = spanner.ReadTimestamp(ts)
} else {
tb = spanner.StrongRead()
}
var m queryMode
switch {
case o.EnablePartitionedDML:
m = partitionedDML{}
case dmlRe.MatchString(query):
m = readWrite{}
default:
m = single{tb}
}
stmt := spanner.Statement{SQL: query, Params: paramMap}
if o.TryPartitionQuery {
bt, err := client.BatchReadOnlyTransaction(ctx, spanner.StrongRead())
if err != nil {
return err
}
defer bt.Close()
_, err = bt.PartitionQuery(ctx, stmt, spanner.PartitionOptions{})
if err != nil {
return err
}
bt.Cleanup(ctx)
fmt.Println("success")
return nil
}
rs, err := runInNewTransaction(ctx, client, stmt, spanner.QueryOptions{Mode: &mode}, m, o.RedactRows)
if err != nil {
return err
}
if o.Format == "experimental_csv" {
return writeCsv(os.Stdout, rs)
}
object, err := toProtojsonObject(rs)
if err != nil {
return err
}
enc, err := newEncoder(os.Stdout, o.Format, o.CompactOutput, o.JqRawOutput)
if err != nil {
return err
}
return printResult(enc, jqQuery.Run(object))
}
func writeCsv(writer io.Writer, rs *sppb.ResultSet) error {
fields := rs.GetMetadata().GetRowType().GetFields()
types := slices.Collect(xiter.Map(slices.Values(fields), (*sppb.StructType_Field).GetType))
records := slices.Collect(xiter.Map(slices.Values(rs.GetRows()), func(row *structpb.ListValue) []string {
return slices.Collect(
xiter.Map(
xiter.Zip(slices.Values(types), slices.Values(row.Values)),
internal.Tupled(internal.Must2(typeValueToStringExperimental)),
),
)
}))
csvWriter := csv.NewWriter(writer)
defer csvWriter.Flush()
header := slices.Collect(xiter.Map(slices.Values(fields), (*sppb.StructType_Field).GetName))
err := csvWriter.Write(header)
if err != nil {
return err
}
err = csvWriter.WriteAll(records)
if err != nil {
return err
}
return nil
}
func newClient(ctx context.Context, project, instance, database string, logGrpc bool, doTrace bool) (*spanner.Client, error) {
name := fmt.Sprintf("projects/%s/instances/%s/databases/%s", project, instance, database)
var copts []option.ClientOption
if logGrpc {
copts = logGrpcClientOptions()
}
if doTrace {
copts = append(copts, option.WithGRPCDialOption(grpc.WithChainStreamInterceptor(interceptor.StreamInterceptor(interceptor.WithDefaultDecorators()))))
}
return spanner.NewClientWithConfig(ctx, name, spanner.ClientConfig{
SessionPoolConfig: spanner.SessionPoolConfig{
MaxOpened: 1,
MinOpened: 1,
TrackSessionHandles: true,
},
}, copts...)
}
func toProtojsonObject(m proto.Message) (map[string]interface{}, error) {
b, err := protojson.Marshal(m)
if err != nil {
return nil, err
}
dec := json.NewDecoder(bytes.NewReader(b))
dec.UseNumber()
var object map[string]interface{}
err = dec.Decode(&object)
if err != nil {
return nil, err
}
return object, nil
}
type encoder interface{ Encode(v interface{}) error }
type stringPassThroughEncoderWrapper struct {
Writer io.Writer
Enc encoder
}
func (enc *stringPassThroughEncoderWrapper) Encode(v interface{}) error {
if s, ok := v.(string); ok {
_, err := fmt.Fprintln(enc.Writer, s)
return err
}
return enc.Enc.Encode(v)
}
func printResult(enc encoder, iter gojq.Iter) error {
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
return err
}
err := enc.Encode(v)
if err != nil {
return err
}
}
return nil
}
func newEncoder(writer io.Writer, format string, compactOutput bool, rawOutput bool) (encoder, error) {
switch {
case format == "yaml":
return yaml.NewEncoder(writer), nil
case format == "json":
jsonenc := json.NewEncoder(writer)
jsonenc.SetEscapeHTML(false)
if !compactOutput {
jsonenc.SetIndent("", " ")
}
if rawOutput {
return &stringPassThroughEncoderWrapper{Writer: writer, Enc: jsonenc}, nil
} else {
return jsonenc, nil
}
default:
return nil, fmt.Errorf("unknown format: %s", format)
}
}
// consumeRowIterIntoResultSet construct *spannerpb.ResultSet using *spanner.RowIterator.
// rowIter must be passed without calling any method, and it will be closed by this function.
func consumeRowIterIntoResultSet(rowIter *spanner.RowIterator, redactRows bool) (*sppb.ResultSet, error) {
defer rowIter.Stop()
var rows []*structpb.ListValue
var err error
if redactRows {
err = skipRowIter(rowIter)
} else {
rows, err = consumeRowIterIntoListValues(rowIter)
}
if err != nil {
return nil, err
}
return convertToResultSet(rows, rowIter)
}
// convertToResultSet convert rows and rowIter into sppb.ResultSet.
// rowIter must be consumed with iterator.Done, and not Stop()-ed.
func convertToResultSet(rows []*structpb.ListValue, rowIter *spanner.RowIterator) (*sppb.ResultSet, error) {
// Leave null if fields are not populated
rs := &sppb.ResultSet{
Rows: rows,
Metadata: rowIter.Metadata,
}
var queryStats *structpb.Struct
if rowIter.QueryStats != nil {
qs, err := structpb.NewStruct(rowIter.QueryStats)
if err != nil {
return nil, err
}
queryStats = qs
}
// If there are no stats member, entire Stats must be nil
if rowIter.QueryPlan == nil && queryStats == nil && rowIter.RowCount == 0 {
return rs, nil
}
rs.Stats = &sppb.ResultSetStats{
QueryPlan: rowIter.QueryPlan,
QueryStats: queryStats,
}
if rowIter.RowCount != 0 {
rs.Stats.RowCount = &sppb.ResultSetStats_RowCountExact{RowCountExact: rowIter.RowCount}
}
return rs, nil
}
func rowIterSeq(rowIter *spanner.RowIterator) iter.Seq2[*spanner.Row, error] {
return func(yield func(*spanner.Row, error) bool) {
for {
r, err := rowIter.Next()
if errors.Is(err, iterator.Done) {
return
}
if err != nil {
_ = yield(nil, err)
return
}
if !yield(r, nil) {
return
}
}
}
}
func rowToListValue(r *spanner.Row) *structpb.ListValue {
return &structpb.ListValue{Values: slices.Collect(xiter.Map(xiter.Range(0, r.Size()), r.ColumnValue))}
}
func consumeRowIterIntoListValues(rowIter *spanner.RowIterator) ([]*structpb.ListValue, error) {
return xiter.TryCollect(internal.MapNonError(rowIterSeq(rowIter), rowToListValue))
}
func skipRowIter(rowIter *spanner.RowIterator) error {
return rowIter.Do(func(r *spanner.Row) error { return nil })
}