/
datasource.go
200 lines (167 loc) · 5.09 KB
/
datasource.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
package csv
// only comma-separated now. Changing that means setting RecordStream.r.Coma
// .csv reader trims leading white space(s)
import (
"context"
"encoding/csv"
"fmt"
"io"
"os"
"unicode/utf8"
"github.com/pkg/errors"
"github.com/cube2222/octosql"
"github.com/cube2222/octosql/config"
"github.com/cube2222/octosql/execution"
"github.com/cube2222/octosql/physical"
"github.com/cube2222/octosql/physical/metadata"
)
var availableFilters = map[physical.FieldType]map[physical.Relation]struct{}{
physical.Primary: make(map[physical.Relation]struct{}),
physical.Secondary: make(map[physical.Relation]struct{}),
}
type DataSource struct {
path string
alias string
hasColumnNames bool
separator rune
}
func NewDataSourceBuilderFactory() physical.DataSourceBuilderFactory {
return physical.NewDataSourceBuilderFactory(
func(ctx context.Context, matCtx *physical.MaterializationContext, dbConfig map[string]interface{}, filter physical.Formula, alias string) (execution.Node, error) {
path, err := config.GetString(dbConfig, "path")
if err != nil {
return nil, errors.Wrap(err, "couldn't get path")
}
hasColumns, err := config.GetBool(dbConfig, "headerRow", config.WithDefault(true))
if err != nil {
return nil, errors.Wrap(err, "couldn't get headerRow")
}
separator, err := config.GetString(dbConfig, "separator", config.WithDefault(","))
if err != nil {
return nil, errors.Wrap(err, "couldn't get separator")
}
r, _ := utf8.DecodeRune([]byte(separator))
if r == utf8.RuneError {
return nil, errors.Errorf("couldn't decode separator %s to rune", separator)
}
return &DataSource{
path: path,
alias: alias,
hasColumnNames: hasColumns,
separator: r,
}, nil
},
nil,
availableFilters,
metadata.BoundedFitsInLocalStorage,
)
}
// NewDataSourceBuilderFactoryFromConfig creates a data source builder factory using the configuration.
func NewDataSourceBuilderFactoryFromConfig(dbConfig map[string]interface{}) (physical.DataSourceBuilderFactory, error) {
return NewDataSourceBuilderFactory(), nil
}
func (ds *DataSource) Get(variables octosql.Variables) (execution.RecordStream, error) {
file, err := os.Open(ds.path)
if err != nil {
return nil, errors.Wrap(err, "couldn't open file")
}
r := csv.NewReader(file)
r.Comma = ds.separator
r.TrimLeadingSpace = true
return &RecordStream{
file: file,
r: r,
isDone: false,
alias: ds.alias,
first: true,
hasColumnHeader: ds.hasColumnNames,
}, nil
}
type RecordStream struct {
file *os.File
r *csv.Reader
isDone bool
alias string
aliasedFields []octosql.VariableName
first bool
hasColumnHeader bool
}
func (rs *RecordStream) Close() error {
err := rs.file.Close()
if err != nil {
return errors.Wrap(err, "couldn't close underlying file")
}
return nil
}
func parseDataTypes(row []string) []octosql.Value {
resultRow := make([]octosql.Value, len(row))
for i, v := range row {
resultRow[i] = execution.ParseType(v)
}
return resultRow
}
func (rs *RecordStream) initializeColumnsWithHeaderRow() error {
columns, err := rs.r.Read()
if err != nil {
return errors.Wrap(err, "couldn't read row")
}
rs.aliasedFields = make([]octosql.VariableName, len(columns))
for i, c := range columns {
rs.aliasedFields[i] = octosql.NewVariableName(fmt.Sprintf("%s.%s", rs.alias, c))
}
rs.r.FieldsPerRecord = len(rs.aliasedFields)
set := make(map[octosql.VariableName]struct{})
for _, f := range rs.aliasedFields {
if _, present := set[f]; present {
return errors.New("column names not unique")
}
set[f] = struct{}{}
}
return nil
}
func (rs *RecordStream) initializeColumnsWithoutHeaderRow() (*execution.Record, error) {
row, err := rs.r.Read()
if err != nil {
return nil, errors.Wrap(err, "couldn't read row")
}
rs.aliasedFields = make([]octosql.VariableName, len(row))
for i := range row {
rs.aliasedFields[i] = octosql.NewVariableName(fmt.Sprintf("%s.col%d", rs.alias, i+1))
}
return execution.NewRecordFromSlice(rs.aliasedFields, parseDataTypes(row)), nil
}
func (rs *RecordStream) Next() (*execution.Record, error) {
if rs.isDone {
return nil, execution.ErrEndOfStream
}
if rs.first {
rs.first = false
if rs.hasColumnHeader {
err := rs.initializeColumnsWithHeaderRow()
if err != nil {
return nil, errors.Wrap(err, "couldn't initialize columns for record stream")
}
return rs.Next()
} else {
record, err := rs.initializeColumnsWithoutHeaderRow()
if err != nil {
return nil, errors.Wrap(err, "couldn't initialize columns for record stream")
}
return record, nil
}
}
line, err := rs.r.Read()
if err == io.EOF {
rs.isDone = true
rs.file.Close()
return nil, execution.ErrEndOfStream
}
if err != nil {
return nil, errors.Wrap(err, "couldn't read record")
}
aliasedRecord := make(map[octosql.VariableName]octosql.Value)
for i, v := range line {
aliasedRecord[rs.aliasedFields[i]] = execution.ParseType(v)
}
return execution.NewRecord(rs.aliasedFields, aliasedRecord), nil
}