/
root.go
526 lines (479 loc) · 16.9 KB
/
root.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
package cmd
import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
"runtime/debug"
"runtime/trace"
"strings"
"sync"
"github.com/Masterminds/semver"
"github.com/pkg/profile"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
"github.com/cube2222/octosql/aggregates"
"github.com/cube2222/octosql/config"
"github.com/cube2222/octosql/datasources/csv"
"github.com/cube2222/octosql/datasources/docs"
"github.com/cube2222/octosql/datasources/json"
"github.com/cube2222/octosql/datasources/lines"
"github.com/cube2222/octosql/datasources/parquet"
"github.com/cube2222/octosql/datasources/plugins"
"github.com/cube2222/octosql/execution"
"github.com/cube2222/octosql/execution/nodes"
"github.com/cube2222/octosql/functions"
"github.com/cube2222/octosql/helpers/graph"
"github.com/cube2222/octosql/logical"
"github.com/cube2222/octosql/logs"
"github.com/cube2222/octosql/optimizer"
"github.com/cube2222/octosql/outputs/batch"
"github.com/cube2222/octosql/outputs/eager"
"github.com/cube2222/octosql/outputs/formats"
"github.com/cube2222/octosql/outputs/stream"
"github.com/cube2222/octosql/parser"
"github.com/cube2222/octosql/parser/sqlparser"
"github.com/cube2222/octosql/physical"
"github.com/cube2222/octosql/plugins/executor"
"github.com/cube2222/octosql/plugins/manager"
"github.com/cube2222/octosql/plugins/repository"
"github.com/cube2222/octosql/table_valued_functions"
"github.com/cube2222/octosql/telemetry"
)
var VERSION = "dev"
var emptyYamlNode = func() yaml.Node {
var out yaml.Node
if err := yaml.Unmarshal([]byte("{}"), &out); err != nil {
log.Fatalf("[BUG] Couldn't create empty yaml node: %s", err)
}
return out
}()
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "octosql <query>",
Args: cobra.ExactArgs(1),
Short: "",
Long: ``,
Example: `octosql "SELECT * FROM myfile.json"
octosql "SELECT * FROM mydir/myfile.csv"
octosql "SELECT * FROM plugins.plugins"`,
SilenceErrors: true,
Version: VERSION,
RunE: func(cmd *cobra.Command, args []string) error {
switch prof {
case "cpu":
defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
case "memory":
defer profile.Start(profile.MemProfile, profile.MemProfileRate(4096*8), profile.ProfilePath(".")).Stop()
case "trace":
defer profile.Start(profile.TraceProfile, profile.ProfilePath(".")).Stop()
}
ctx := cmd.Context()
cfg, err := config.Read()
if err != nil {
return fmt.Errorf("couldn't read config: %w", err)
}
ctx = config.ContextWithConfig(ctx, cfg)
debug.SetGCPercent(1000)
logs.InitializeFileLogger()
defer logs.CloseLogger()
pluginManager := &manager.PluginManager{}
pluginExecutor := executor.PluginExecutor{
Manager: pluginManager,
}
defer func() {
if err := pluginExecutor.Close(); err != nil {
log.Printf("couldn't close plugin executor: %s", err)
}
}()
installedPlugins, err := pluginManager.ListInstalledPlugins()
if err != nil {
return fmt.Errorf("couldn't list installed plugins: %w", err)
}
resolvedVersions := map[string]*semver.Version{}
// Fill in plugin versions.
dbLoop:
for i := range cfg.Databases {
if cfg.Databases[i].Version == nil {
constraint, _ := semver.NewConstraint("*")
cfg.Databases[i].Version = config.NewYamlUnmarshallableVersionConstraint(constraint)
}
for _, plugin := range installedPlugins {
if plugin.Reference != cfg.Databases[i].Type {
continue
}
for _, version := range plugin.Versions {
if cfg.Databases[i].Version.Raw().Check(version.Number) {
resolvedVersions[cfg.Databases[i].Name] = version.Number
continue dbLoop
}
}
break
}
return fmt.Errorf("database '%s' plugin '%s' used in configuration is not installed with the required version - run `octosql plugin install` to install all missing plugins", cfg.Databases[i].Name, cfg.Databases[i].Type.String())
}
databases := make(map[string]func() (physical.Database, error))
for _, dbConfig := range cfg.Databases {
once := sync.Once{}
curDbConfig := dbConfig
var db physical.Database
var err error
databases[curDbConfig.Name] = func() (physical.Database, error) {
once.Do(func() {
db, err = pluginExecutor.RunPlugin(ctx, curDbConfig.Type, curDbConfig.Name, resolvedVersions[curDbConfig.Name], curDbConfig.Config)
})
if err != nil {
return nil, fmt.Errorf("couldn't run %s plugin %s: %w", curDbConfig.Type.String(), curDbConfig.Name, err)
}
return db, nil
}
}
{
once := sync.Once{}
var repositories []repository.Repository
var err error
databases["plugins"] = func() (physical.Database, error) {
once.Do(func() {
repositories, err = repository.GetRepositories(ctx)
})
if err != nil {
return nil, fmt.Errorf("couldn't get repositories: %w", err)
}
return plugins.Creator(ctx, pluginManager, repositories)
}
}
databases["docs"] = func() (physical.Database, error) {
return docs.Creator(ctx)
}
for _, metadata := range installedPlugins {
if _, ok := databases[metadata.Reference.Name]; ok {
continue
}
curMetadata := metadata
once := sync.Once{}
var db physical.Database
var err error
databases[curMetadata.Reference.Name] = func() (physical.Database, error) {
once.Do(func() {
db, err = pluginExecutor.RunPlugin(ctx, curMetadata.Reference, curMetadata.Reference.Name, curMetadata.Versions[0].Number, emptyYamlNode)
})
if err != nil {
return nil, fmt.Errorf("couldn't run default plugin %s database: %w", curMetadata.Reference, err)
}
return db, nil
}
}
fileExtensionHandlers, err := pluginManager.GetFileExtensionHandlers()
if err != nil {
return fmt.Errorf("couldn't get file extension handlers: %w", err)
}
fileHandlers := map[string]func(ctx context.Context, name string, options map[string]string) (physical.DatasourceImplementation, physical.Schema, error){
"csv": csv.Creator(','),
"json": json.Creator,
"lines": lines.Creator,
"parquet": parquet.Creator,
"tsv": csv.Creator('\t'),
}
for ext, pluginName := range fileExtensionHandlers {
fileHandlers[ext] = func(ctx context.Context, name string, options map[string]string) (physical.DatasourceImplementation, physical.Schema, error) {
db, err := databases[pluginName]()
if err != nil {
return nil, physical.Schema{}, fmt.Errorf("couldn't get plugin %s database for plugin extensions %s: %w", pluginName, ext, err)
}
return db.GetTable(ctx, name, options)
}
}
for name := range fileHandlers {
curHandler := fileHandlers[name]
if _, ok := databases[name]; !ok {
databases[name] = func() (physical.Database, error) {
return &fileTypeDatabaseCreator{
creator: curHandler,
}, nil
}
}
}
env := physical.Environment{
Aggregates: aggregates.Aggregates,
Functions: functions.FunctionMap(),
Datasources: &physical.DatasourceRepository{
Databases: databases,
FileHandlers: fileHandlers,
},
PhysicalConfig: nil,
VariableContext: nil,
}
statement, err := sqlparser.Parse(args[0])
if err != nil {
return fmt.Errorf("couldn't parse query: %w", err)
}
selectStmt, ok := statement.(sqlparser.SelectStatement)
if !ok {
return fmt.Errorf("only SELECT statements are supported")
}
logicalPlan, outputOptions, err := parser.ParseNode(selectStmt)
if err != nil {
return fmt.Errorf("couldn't parse query: %w", err)
}
tableValuedFunctions := map[string]logical.TableValuedFunctionDescription{
"max_diff_watermark": table_valued_functions.MaxDiffWatermark,
"tumble": table_valued_functions.Tumble,
"range": table_valued_functions.Range,
"poll": table_valued_functions.Poll,
}
uniqueNameGenerator := map[string]int{}
physicalPlan, mapping, err := typecheckNode(
ctx,
logicalPlan,
env,
logical.Environment{
CommonTableExpressions: map[string]logical.CommonTableExpression{},
TableValuedFunctions: tableValuedFunctions,
UniqueNameGenerator: uniqueNameGenerator,
},
)
if err != nil {
return err
}
reverseMapping := logical.ReverseMapping(mapping)
physicalOrderByExpressions := make([]physical.Expression, len(outputOptions.OrderByExpressions))
for i := range outputOptions.OrderByExpressions {
physicalExpr, err := typecheckExpr(ctx, outputOptions.OrderByExpressions[i], env.WithRecordSchema(physicalPlan.Schema), logical.Environment{
CommonTableExpressions: map[string]logical.CommonTableExpression{},
TableValuedFunctions: tableValuedFunctions,
UniqueVariableNames: &logical.VariableMapping{
Mapping: mapping,
},
UniqueNameGenerator: uniqueNameGenerator,
})
if err != nil {
return fmt.Errorf("couldn't typecheck order by expression with index %d: %w", i, err)
}
physicalOrderByExpressions[i] = physicalExpr
}
var physicalLimitExpression *physical.Expression
if outputOptions.Limit != nil {
physicalExpr, err := typecheckExpr(ctx, *outputOptions.Limit, env.WithRecordSchema(physicalPlan.Schema), logical.Environment{
CommonTableExpressions: map[string]logical.CommonTableExpression{},
TableValuedFunctions: tableValuedFunctions,
UniqueVariableNames: &logical.VariableMapping{
Mapping: mapping,
},
UniqueNameGenerator: uniqueNameGenerator,
})
if err != nil {
return fmt.Errorf("couldn't typecheck limit expression with index: %w", err)
}
physicalLimitExpression = &physicalExpr
}
queryTelemetry := telemetry.GetQueryTelemetryData(physicalPlan, installedPlugins)
var executionPlan execution.Node
var orderByExpressions []execution.Expression
var limitExpression *execution.Expression
var outSchema physical.Schema
if describe {
telemetry.SendTelemetry(ctx, VERSION, "describe", queryTelemetry)
for i := range physicalPlan.Schema.Fields {
physicalPlan.Schema.Fields[i].Name = reverseMapping[physicalPlan.Schema.Fields[i].Name]
}
executionPlan = &DescribeNode{
Schema: physical.Schema{
Fields: formats.WithoutQualifiers(physicalPlan.Schema.Fields),
TimeField: physicalPlan.Schema.TimeField,
NoRetractions: physicalPlan.Schema.NoRetractions,
},
}
outSchema = DescribeNodeSchema
} else {
telemetry.SendTelemetry(ctx, VERSION, "query", queryTelemetry)
if optimize {
physicalPlan = optimizer.Optimize(physicalPlan)
}
if explain >= 1 {
file, err := os.CreateTemp(os.TempDir(), "octosql-explain-*.png")
if err != nil {
return fmt.Errorf("couldn't create temporary file: %w", err)
}
cmd := exec.Command("dot", "-Tpng")
cmd.Stdin = strings.NewReader(graph.Show(physical.ExplainNode(physicalPlan, explain >= 2)).String())
cmd.Stdout = file
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("couldn't render graph: %w", err)
}
if err := file.Close(); err != nil {
return fmt.Errorf("couldn't close temporary file: %w", err)
}
if err := open.Start(file.Name()); err != nil {
return fmt.Errorf("couldn't open graph: %w", err)
}
return nil
}
executionPlan, err = physicalPlan.Materialize(
ctx,
env,
)
if err != nil {
return fmt.Errorf("couldn't materialize physical plan: %w", err)
}
orderByExpressions = make([]execution.Expression, len(physicalOrderByExpressions))
for i, physicalExpr := range physicalOrderByExpressions {
execExpr, err := physicalExpr.Materialize(ctx, env.WithRecordSchema(physicalPlan.Schema))
if err != nil {
return fmt.Errorf("couldn't materialize output order by expression with index %d: %v", i, err)
}
orderByExpressions[i] = execExpr
}
if physicalLimitExpression != nil {
execExpr, err := physicalLimitExpression.Materialize(ctx, env.WithRecordSchema(physicalPlan.Schema))
if err != nil {
return fmt.Errorf("couldn't materialize output limit expression with index: %w", err)
}
limitExpression = &execExpr
}
outFields := make([]physical.SchemaField, len(physicalPlan.Schema.Fields))
copy(outFields, physicalPlan.Schema.Fields)
outSchema = physical.Schema{
Fields: outFields,
TimeField: physicalPlan.Schema.TimeField,
}
for i := range outFields {
outFields[i].Name = reverseMapping[outFields[i].Name]
}
}
var sink interface {
Run(execCtx execution.ExecutionContext) error
}
execCtx := execution.ExecutionContext{
Context: ctx,
VariableContext: nil,
}
switch output {
case "live_table", "batch_table":
var limit *int
if limitExpression != nil {
val, err := (*limitExpression).Evaluate(execCtx)
if err != nil {
return fmt.Errorf("couldn't evaluate limit expression: %w", err)
}
if val.Int < 0 {
return fmt.Errorf("limit must be positive, got %d", val.Int)
}
limit = &val.Int
if len(orderByExpressions) == 0 && physicalPlan.Schema.NoRetractions {
// We want short-circuiting.
executionPlan = nodes.NewLimit(executionPlan, *limitExpression)
}
}
sink = batch.NewOutputPrinter(
executionPlan,
orderByExpressions,
logical.DirectionsToMultipliers(outputOptions.OrderByDirections),
limit,
physicalPlan.Schema.NoRetractions,
outSchema,
func(writer io.Writer) batch.Format {
return formats.NewTableFormatter(writer)
},
output == "live_table",
)
case "csv", "json":
if len(orderByExpressions) > 0 || (limitExpression != nil && !physicalPlan.Schema.NoRetractions) {
executionPlan = nodes.NewOrderSensitiveTransform(executionPlan, orderByExpressions, logical.DirectionsToMultipliers(outputOptions.OrderByDirections), limitExpression, physicalPlan.Schema.NoRetractions)
} else if limitExpression != nil {
executionPlan = nodes.NewLimit(executionPlan, *limitExpression)
}
var formatter func(writer io.Writer) eager.Format
switch output {
case "csv":
formatter = func(writer io.Writer) eager.Format {
return formats.NewCSVFormatter(writer)
}
case "json":
formatter = func(writer io.Writer) eager.Format {
return formats.NewJSONFormatter(writer)
}
}
sink = eager.NewOutputPrinter(
executionPlan,
outSchema,
formatter,
)
case "stream_native":
if len(orderByExpressions) > 0 || (limitExpression != nil && !physicalPlan.Schema.NoRetractions) {
executionPlan = nodes.NewOrderSensitiveTransform(executionPlan, orderByExpressions, logical.DirectionsToMultipliers(outputOptions.OrderByDirections), limitExpression, physicalPlan.Schema.NoRetractions)
} else if limitExpression != nil {
executionPlan = nodes.NewLimit(executionPlan, *limitExpression)
}
sink = stream.NewOutputPrinter(
executionPlan,
stream.NewNativeFormat(outSchema),
)
default:
return fmt.Errorf("invalid output format: '%s'", output)
}
trace.Log(ctx, "octosql", "running query")
if err := sink.Run(
execution.ExecutionContext{
Context: ctx,
VariableContext: nil,
},
); err != nil {
return fmt.Errorf("couldn't run query: %w", err)
}
return nil
},
}
func Execute(ctx context.Context) {
cobra.CheckErr(rootCmd.ExecuteContext(ctx))
}
var describe bool
var explain int
var optimize bool
var output string
var prof string
func init() {
rootCmd.Flags().BoolVar(&describe, "describe", false, "Describe query output schema.")
rootCmd.Flags().IntVar(&explain, "explain", 0, "Describe query output schema.")
rootCmd.Flags().BoolVar(&optimize, "optimize", true, "Whether OctoSQL should optimize the query.")
rootCmd.Flags().StringVarP(&output, "output", "o", "live_table", "Output format to use. Available options are live_table, batch_table, csv, json and stream_native.")
rootCmd.Flags().StringVar(&prof, "profile", "", "Enable profiling of the given type: cpu, memory, trace.")
}
func typecheckNode(ctx context.Context, node logical.Node, env physical.Environment, logicalEnv logical.Environment) (_ physical.Node, _ map[string]string, outErr error) {
defer func() {
if r := recover(); r != nil {
outErr = fmt.Errorf("typecheck error: %s", r)
}
}()
physicalNode, mapping := node.Typecheck(
ctx,
env,
logicalEnv,
)
return physicalNode, mapping, nil
}
func typecheckExpr(ctx context.Context, expr logical.Expression, env physical.Environment, logicalEnv logical.Environment) (_ physical.Expression, outErr error) {
defer func() {
if r := recover(); r != nil {
outErr = fmt.Errorf("typecheck error: %s", r)
}
}()
physicalExpr := expr.Typecheck(
ctx,
env,
logicalEnv,
)
return physicalExpr, nil
}
type fileTypeDatabaseCreator struct {
creator func(ctx context.Context, name string, options map[string]string) (physical.DatasourceImplementation, physical.Schema, error)
}
func (f *fileTypeDatabaseCreator) ListTables(ctx context.Context) ([]string, error) {
return nil, nil
}
func (f *fileTypeDatabaseCreator) GetTable(ctx context.Context, name string, options map[string]string) (physical.DatasourceImplementation, physical.Schema, error) {
return f.creator(ctx, name, options)
}