-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
importer.go
475 lines (436 loc) · 12.7 KB
/
importer.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
package trdsql
import (
"bufio"
"bytes"
"compress/bzip2"
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
"github.com/klauspost/compress/zstd"
"github.com/pierrec/lz4/v4"
"github.com/ulikunitz/xz"
)
var (
// ErrInvalidColumn is returned if invalid column.
ErrInvalidColumn = errors.New("invalid column")
// ErrNoReader is returned when there is no reader.
ErrNoReader = errors.New("no reader")
// ErrUnknownFormat is returned if the format is unknown.
ErrUnknownFormat = errors.New("unknown format")
// ErrNoRows returned when there are no rows.
ErrNoRows = errors.New("no rows")
// ErrUnableConvert is returned if it cannot be converted to a table.
ErrUnableConvert = errors.New("unable to convert")
// ErrNoMatchFound is returned if no match is found.
ErrNoMatchFound = errors.New("no match found")
// ErrNonDefinition is returned when there is no definition.
ErrNonDefinition = errors.New("no definition")
// ErrInvalidJSON is returned when the JSON is invalid.
ErrInvalidJSON = errors.New("invalid JSON")
// ErrInvalidYAML is returned when the YAML is invalid.
ErrInvalidYAML = errors.New("invalid YAML")
)
// Importer is the interface import data into the database.
// Importer parses sql query to decide which file to Import.
// Therefore, the reader does not receive it directly.
type Importer interface {
Import(db *DB, query string) (string, error)
ImportContext(ctx context.Context, db *DB, query string) (string, error)
}
// ReadFormat represents a structure that satisfies the Importer.
type ReadFormat struct {
*ReadOpts
}
// NewImporter returns trdsql default Importer.
// The argument is an option of Functional Option Pattern.
//
// usage:
//
// trdsql.NewImporter(
// trdsql.InFormat(trdsql.CSV),
// trdsql.InHeader(true),
// trdsql.InDelimiter(";"),
// )
func NewImporter(options ...ReadOpt) *ReadFormat {
readOpts := NewReadOpts(options...)
return &ReadFormat{
ReadOpts: readOpts,
}
}
// DefaultDBType is default type.
const DefaultDBType = "text"
// Import is parses the SQL statement and imports one or more tables.
// Import is called from Exec.
// Return the rewritten SQL and error.
// No error is returned if there is no table to import.
func (i *ReadFormat) Import(db *DB, query string) (string, error) {
ctx := context.Background()
return i.ImportContext(ctx, db, query)
}
// ImportContext is parses the SQL statement and imports one or more tables.
// ImportContext is called from ExecContext.
// Return the rewritten SQL and error.
// No error is returned if there is no table to import.
func (i *ReadFormat) ImportContext(ctx context.Context, db *DB, query string) (string, error) {
parsedQuery := SQLFields(query)
tables, tableIdx := TableNames(parsedQuery)
if len(tables) == 0 {
// without FROM clause. ex. SELECT 1+1;
debug.Printf("table not found\n")
return query, nil
}
for fileName := range tables {
tableName, err := ImportFileContext(ctx, db, fileName, i.ReadOpts)
if err != nil {
return query, err
}
if len(tableName) > 0 {
tables[fileName] = tableName
}
}
// replace table names in query with their quoted values
for _, idx := range tableIdx {
if table, ok := tables[parsedQuery[idx]]; ok {
parsedQuery[idx] = table
}
}
// reconstruct the query with quoted table names
query = strings.Join(parsedQuery, "")
return query, nil
}
// TableNames returns a map of table names
// that may be tables by a simple SQL parser
// from the query string of the argument,
// along with the locations within the parsed
// query where those table names were found.
func TableNames(parsedQuery []string) (map[string]string, []int) {
tables := make(map[string]string)
tableIdx := []int{}
tableFlag := false
frontFlag := false
debug.Printf("[%s]", strings.Join(parsedQuery, "]["))
for i, w := range parsedQuery {
switch {
case strings.Contains(" \t\r\n;=", w): // nolint // Because each character is parsed by SQLFields.
continue
case strings.EqualFold(w, "FROM"),
strings.EqualFold(w, "*FROM"),
strings.EqualFold(w, "JOIN"),
strings.EqualFold(w, "TABLE"),
strings.EqualFold(w, "INTO"),
strings.EqualFold(w, "UPDATE"):
tableFlag = true
frontFlag = true
case isSQLKeyWords(w):
tableFlag = false
case w == ",":
frontFlag = true
default:
if tableFlag && frontFlag {
if w[len(w)-1] == ')' {
w = w[:len(w)-1]
}
if !isSQLKeyWords(w) {
tables[w] = w
tableIdx = append(tableIdx, i)
}
}
frontFlag = false
}
}
return tables, tableIdx
}
// SQLFields returns an array of string fields
// (interpreting quotes) from the argument query.
func SQLFields(query string) []string {
parsed := make([]string, 0, len(query)/2)
buf := new(bytes.Buffer)
var singleQuoted, doubleQuoted, backQuote bool
for _, r := range query {
switch r {
case ' ', '\t', '\r', '\n', ',', ';', '=', '(', ')':
if !singleQuoted && !doubleQuoted && !backQuote {
if buf.Len() != 0 {
parsed = append(parsed, buf.String())
buf.Reset()
}
parsed = append(parsed, string(r))
} else {
buf.WriteRune(r)
}
continue
case '\'':
if !doubleQuoted && !backQuote {
singleQuoted = !singleQuoted
}
case '"':
if !singleQuoted && !backQuote {
doubleQuoted = !doubleQuoted
}
case '`':
if !singleQuoted && !doubleQuoted {
backQuote = !backQuote
}
case '*':
str := buf.String()
if strings.ToUpper(str) == "SELECT" { // `SELECT*` to `SELECT *`
parsed = append(parsed, str)
parsed = append(parsed, string(r))
buf.Reset()
continue
}
}
buf.WriteRune(r)
}
if buf.Len() > 0 {
parsed = append(parsed, buf.String())
}
return parsed
}
func isSQLKeyWords(str string) bool {
switch strings.ToUpper(str) {
case "WHERE", "GROUP", "HAVING", "WINDOW", "UNION", "ORDER", "LIMIT", "OFFSET", "FETCH",
"FOR", "LEFT", "RIGHT", "CROSS", "INNER", "FULL", "LATERAL", "(SELECT":
return true
}
return false
}
// ImportFile is imports a file.
// Return the quoted table name and error.
// Do not import if file not found (no error).
// Wildcards can be passed as fileName.
func ImportFile(db *DB, fileName string, readOpts *ReadOpts) (string, error) {
return ImportFileContext(context.Background(), db, fileName, readOpts)
}
// ImportFileContext is imports a file.
// Return the quoted table name and error.
// Do not import if file not found (no error).
// Wildcards can be passed as fileName.
func ImportFileContext(ctx context.Context, db *DB, fileName string, readOpts *ReadOpts) (string, error) {
opts, fileName := GuessOpts(readOpts, fileName)
db.importCount++
file, err := importFileOpen(fileName)
if err != nil {
debug.Printf("%s\n", err)
return "", nil
}
defer func() {
if deferr := file.Close(); deferr != nil {
log.Printf("file close:%s", deferr)
}
}()
reader, err := NewReader(file, opts)
if err != nil {
return "", err
}
tableName := fileName
if opts.InJQuery != "" {
tableName = fmt.Sprintf("%s::jq%d", fileName, db.importCount)
}
tableName = db.QuotedName(tableName)
if opts.InRowNumber {
reader = newRowNumberReader(reader)
}
columnNames, err := reader.Names()
if err != nil {
if !errors.Is(err, io.EOF) {
return tableName, err
}
debug.Printf("EOF reached before argument number of rows")
}
columnTypes, err := reader.Types()
if err != nil {
if !errors.Is(err, io.EOF) {
return tableName, err
}
debug.Printf("EOF reached before argument number of rows")
}
debug.Printf("Column Names: [%v]", strings.Join(columnNames, ","))
debug.Printf("Column Types: [%v]", strings.Join(columnTypes, ","))
if err := db.CreateTableContext(ctx, tableName, columnNames, columnTypes, opts.IsTemporary); err != nil {
return tableName, err
}
return tableName, db.ImportContext(ctx, tableName, columnNames, reader)
}
// GuessOpts guesses ReadOpts from the file name and sets it.
func GuessOpts(readOpts *ReadOpts, fileName string) (*ReadOpts, string) {
if _, err := os.Stat(fileName); err != nil {
if idx := strings.Index(fileName, "::"); idx != -1 {
// jq expression.
readOpts.InJQuery = fileName[idx+2:]
fileName = fileName[:idx]
}
}
if readOpts.InFormat != GUESS {
readOpts.realFormat = readOpts.InFormat
return readOpts, fileName
}
format := guessFormat(fileName)
readOpts.realFormat = format
debug.Printf("Guess file type as %s: [%s]", readOpts.realFormat, fileName)
return readOpts, fileName
}
// guessFormat is guess format from the file name extension.
// Format extensions are searched recursively to remove
// compression extensions such as .gz.
func guessFormat(fileName string) Format {
fileName = strings.TrimRight(fileName, "\"'`")
for {
dotExt := filepath.Ext(fileName)
if dotExt == "" {
debug.Printf("Set in CSV because the extension is unknown: [%s]", fileName)
return CSV
}
ext := strings.ToUpper(strings.TrimLeft(dotExt, "."))
if format, ok := extToFormat[ext]; ok {
return format
}
fileName = fileName[:len(fileName)-len(dotExt)]
}
}
// importFileOpen opens the file specified as a table.
func importFileOpen(tableName string) (io.ReadCloser, error) {
r := regexp.MustCompile(`\*|\?|\[`)
if r.MatchString(tableName) {
return globFileOpen(tableName)
}
return singleFileOpen(tableName)
}
// uncompressedReader returns the decompressed reader
// if it is a compressed file.
func uncompressedReader(reader io.Reader) io.ReadCloser {
var err error
buf := [7]byte{}
n, err := io.ReadAtLeast(reader, buf[:], len(buf))
if err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
return io.NopCloser(bytes.NewReader(buf[:n]))
}
return io.NopCloser(bytes.NewReader(nil))
}
rd := io.MultiReader(bytes.NewReader(buf[:n]), reader)
var r io.ReadCloser
switch {
case bytes.Equal(buf[:3], []byte{0x1f, 0x8b, 0x8}):
r, err = gzip.NewReader(rd)
case bytes.Equal(buf[:3], []byte{0x42, 0x5A, 0x68}):
r = io.NopCloser(bzip2.NewReader(rd))
case bytes.Equal(buf[:4], []byte{0x28, 0xb5, 0x2f, 0xfd}):
var zr *zstd.Decoder
zr, err = zstd.NewReader(rd)
r = io.NopCloser(zr)
case bytes.Equal(buf[:4], []byte{0x04, 0x22, 0x4d, 0x18}):
r = io.NopCloser(lz4.NewReader(rd))
case bytes.Equal(buf[:7], []byte{0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x0, 0x0}):
var zr *xz.Reader
zr, err = xz.NewReader(rd)
r = io.NopCloser(zr)
}
if err != nil || r == nil {
r = io.NopCloser(rd)
}
return r
}
// singleFileOpen opens one file. Also interpret stdin.
func singleFileOpen(fileName string) (io.ReadCloser, error) {
if len(fileName) == 0 || fileName == "-" || strings.ToLower(fileName) == "stdin" {
return uncompressedReader(bufio.NewReader(os.Stdin)), nil
}
fileName = expandTilde(trimQuote(fileName))
file, err := os.Open(fileName)
if err != nil {
return nil, err
}
return uncompressedReader(file), nil
}
// globFileOpen expands the file path,
// connects multiple files and returns one io.PipeReader.
func globFileOpen(globName string) (*io.PipeReader, error) {
globName = expandTilde(trimQuote(globName))
fileNames, err := filepath.Glob(globName)
if err != nil {
return nil, err
}
if len(fileNames) == 0 {
return nil, fmt.Errorf("%w: %s", ErrNoMatchFound, fileNames)
}
pipeReader, pipeWriter := io.Pipe()
go func() {
defer func() {
if err := pipeWriter.Close(); err != nil {
log.Printf("pipe close:%s", err)
}
}()
for _, fileName := range fileNames {
if err := copyFileOpen(pipeWriter, fileName); err != nil {
log.Printf("ERROR: %s:%s", fileName, err)
continue
}
}
}()
return pipeReader, nil
}
// copyFileOpen opens the file and copies it to the writer.
func copyFileOpen(writer io.Writer, fileName string) error {
debug.Printf("Open: [%s]", fileName)
file, err := os.Open(fileName)
if err != nil {
return err
}
r := uncompressedReader(file)
if _, err := io.Copy(writer, r); err != nil {
return err
}
// For if the file does not have a line break before EOF.
if _, err := writer.Write([]byte("\n")); err != nil {
return err
}
if err := file.Close(); err != nil {
return err
}
debug.Printf("Close: [%s]", fileName)
return nil
}
func expandTilde(fileName string) string {
if strings.HasPrefix(fileName, "~") {
usr, err := user.Current()
if err != nil {
log.Printf("ERROR: %s", err)
return fileName
}
fileName = filepath.Join(usr.HomeDir, fileName[1:])
}
return fileName
}
func trimQuote(str string) string {
if str[0] == '`' && str[len(str)-1] == '`' {
str = str[1 : len(str)-1]
}
if str[0] == '"' && str[len(str)-1] == '"' {
str = str[1 : len(str)-1]
}
return str
}
func trimQuoteAll(str string) string {
if len(str) < 2 {
return str
}
if str[0] == '\'' && str[len(str)-1] == '\'' {
return str[1 : len(str)-1]
}
if str[0] == '`' && str[len(str)-1] == '`' {
return str[1 : len(str)-1]
}
if str[0] == '"' && str[len(str)-1] == '"' {
return str[1 : len(str)-1]
}
return str
}