-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
192 lines (170 loc) · 4.62 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
package main
import (
"errors"
"flag"
"fmt"
"io"
"maps"
"os"
"slices"
"strings"
"github.com/martindrlik/rex/box"
"github.com/martindrlik/rex/persist"
"github.com/martindrlik/rex/table"
)
func main() {
must := func(t *table.Table, err error) *table.Table {
if err != nil {
panic(err)
}
return t
}
bind("union", "", func(a, b *table.Table) *table.Table { return must(a.Union(b)) })
bind("difference", "", func(a, b *table.Table) *table.Table { return must(a.Difference(b)) })
bind("natural-join", "", func(a, b *table.Table) *table.Table { return a.NaturalJoin(b) })
exec(parse(os.Args[1:]))
}
func exec(op string, tables []*table.Table, outputFormat string, schema []string) {
func(fn func([]*table.Table) []*table.Table) {
for _, t := range fn(tables) {
projection(t, outputFormat, schema...)
}
}(binaryOp(op))
}
func binaryOp(op string) func([]*table.Table) []*table.Table {
return aggr(func(a, b *table.Table) *table.Table {
if desc, ok := ops[op]; ok {
return desc.fn(a, b)
}
panic("unreachable")
})
}
func aggr(fn func(a, b *table.Table) *table.Table) func([]*table.Table) []*table.Table {
return func(tables []*table.Table) []*table.Table {
result := tables[0]
for _, t := range tables[1:] {
result = fn(result, t)
}
return []*table.Table{result}
}
}
func projection(t *table.Table, outputFormat string, schema ...string) {
if len(schema) == 0 {
schema = slices.Collect(maps.Keys(t.Schema))
}
w, err := t.Projection(schema...)
if err != nil {
fmt.Fprintf(os.Stderr, "unable to project: %v\n", err)
return
}
switch outputFormat {
case "json":
if err := persist.WriteJson(os.Stdout, w); err != nil {
fmt.Fprintf(os.Stderr, "unable to write json output: %v", err)
}
case "table":
fmt.Println(box.Relation(schema, w.List()))
}
}
func parse(args []string) (string, []*table.Table, string, []string) {
if len(args) < 2 {
usage(errors.New("missing arguments"))
}
fs := flag.NewFlagSet("", flag.ExitOnError)
var (
schemalessFilenames = stringsFlag{}
schemalessInlines = stringsFlag{}
outputFormat = fs.String("of", "table", "table or json")
)
fs.Var(&schemalessFilenames, "fa", "name of file that contains array of tuples")
fs.Var(&schemalessInlines, "ia", "inline array of tuples")
op := args[0]
_, ok := ops[op]
if !ok {
usage(fmt.Errorf("unknown op: %s", op))
}
fs.Parse(args[1:])
if len(schemalessFilenames) == 0 && len(schemalessInlines) == 0 {
usage(errors.New("missing table"))
}
tables := []*table.Table{}
load := func(r io.Reader, fn func(io.Reader) (*table.Table, error)) error {
t, err := fn(r)
if err != nil {
return err
}
tables = append(tables, t)
return nil
}
loadFile := func(name string, fn func(io.Reader) (*table.Table, error)) error {
f, err := os.Open(name)
if err != nil {
return err
}
defer f.Close()
return load(f, fn)
}
loadFiles := func(filenames []string, fn func(io.Reader) (*table.Table, error)) {
for _, name := range filenames {
if err := loadFile(name, fn); err != nil {
usage(fmt.Errorf("loading file: %w", err))
}
}
}
loadInline := func(inlines []string, fn func(io.Reader) (*table.Table, error)) {
for _, inline := range inlines {
t, err := fn(strings.NewReader(inline))
if err != nil {
usage(fmt.Errorf("loading inline %v: %w", inline, err))
}
tables = append(tables, t)
}
}
loadFiles(schemalessFilenames, persist.Load)
loadInline(schemalessInlines, persist.Load)
return op, tables, *outputFormat, fs.Args()
}
type stringsFlag []string
func (s *stringsFlag) String() string {
return fmt.Sprint(*s)
}
func (s *stringsFlag) Set(value string) error {
*s = append(*s, value)
return nil
}
func usage(err error) {
if err != nil {
fmt.Println("Error:")
fmt.Printf(" %v\n", err)
}
fmt.Println("Usage:")
fmt.Println(" rex <command> <input> <options> [attribute ...]")
fmt.Println("Commands:")
names := slices.Collect(maps.Keys(ops))
slices.Sort(names)
for _, name := range names {
fmt.Printf(" %s", name)
desc := ops[name].desc
if desc == "" {
fmt.Println()
} else {
fmt.Printf("%s\n", desc)
}
}
fmt.Println("Input:")
fmt.Println(" -fa <file> [-ta <file> ...]: name of file that contains array of tuples")
fmt.Println(" -ia <inline> [-ia <inline> ...]: inline array of tuples")
fmt.Println("Options:")
fmt.Println(" -of <format>: output format: table or json")
fmt.Println("Note:")
fmt.Println(" JSON is used as an input format")
os.Exit(1)
}
type opDesc struct {
desc string
fn func(a, b *table.Table) *table.Table
}
var ops = map[string]opDesc{}
func bind(name, desc string, fn func(a, b *table.Table) *table.Table) {
ops[name] = opDesc{desc, fn}
}