-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
read_import_mysqlout.go
175 lines (150 loc) · 4.38 KB
/
read_import_mysqlout.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
// Copyright 2018 The Cockroach Authors.
//
// Licensed as a CockroachDB Enterprise file under the Cockroach Community
// License (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt
package importccl
import (
"bufio"
"context"
"io"
"unicode"
"github.com/pkg/errors"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
)
type mysqloutfileReader struct {
csvInputReader
opts roachpb.MySQLOutfileOptions
}
var _ inputConverter = &mysqloutfileReader{}
func newMysqloutfileReader(
kvCh chan kvBatch,
opts roachpb.MySQLOutfileOptions,
tableDesc *sqlbase.TableDescriptor,
expectedCols int,
) *mysqloutfileReader {
return &mysqloutfileReader{
csvInputReader: *newCSVInputReader(kvCh, roachpb.CSVOptions{}, tableDesc, expectedCols),
opts: opts,
}
}
func (d *mysqloutfileReader) readFile(
ctx context.Context,
input io.Reader,
inputIdx int32,
inputName string,
progressFn func(finished bool) error,
) error {
var count int
// The current row being read.
var row []string
// the current field being read.
var field []byte
// If we have an escaping char defined, seeing it means the next char is to be
// treated as escaped -- usually that means literal but has some specific
// mappings defined as well.
var nextLiteral bool
// If we have an enclosing char defined, seeing it begins reading a field --
// which means we do not look for separators until we see the end of the field
// as indicated by the matching enclosing char.
var readingField bool
reader := bufio.NewReaderSize(input, 1024*64)
for {
c, w, err := reader.ReadRune()
finished := err == io.EOF
// First check that if we're done and everything looks good.
if finished {
if nextLiteral {
return errors.New("unmatched literal")
}
if readingField {
return errors.New("unmatched field enclosure")
}
// flush the last row if we have one.
if len(row) > 0 {
d.csvInputReader.batch.r = append(d.csvInputReader.batch.r, row)
}
}
// Check if we need to flush due to capacity or finished.
if finished || len(d.csvInputReader.batch.r) > d.csvInputReader.batchSize {
if err := d.csvInputReader.flushBatch(ctx, finished, progressFn); err != nil {
return err
}
d.csvInputReader.batch.rowOffset = count
}
if finished {
break
}
if err != nil {
return err
}
if c == unicode.ReplacementChar && w == 1 {
if err := reader.UnreadRune(); err != nil {
return err
}
raw, err := reader.ReadByte()
if err != nil {
return err
}
field = append(field, raw)
continue
}
// Do we need to check for escaping?
if d.opts.HasEscape {
if nextLiteral {
nextLiteral = false
// See https://dev.mysql.com/doc/refman/8.0/en/load-data.html.
switch c {
case '0':
field = append(field, byte(0))
case 'b':
field = append(field, '\b')
case 'n':
field = append(field, '\n')
case 'r':
field = append(field, '\r')
case 't':
field = append(field, '\t')
case 'Z':
field = append(field, byte(26))
case 'N':
field = append(field, '\\', 'N')
default:
field = append(field, string(c)...)
}
continue
}
if c == d.opts.Escape {
nextLiteral = true
continue
}
}
// If we have a defined enclose char, check if we're starting or stopping
// an enclosed field. Technically, the enclose char can be made mandatory,
// so potentially we could have a flag to enforce that, returning an error
// if it is missing, but we don't need to care for simply decoding.
if d.opts.Enclose != roachpb.MySQLOutfileOptions_Never && c == d.opts.Encloser {
readingField = !readingField
continue
}
// Are we done with the field, or even the whole row?
if !readingField && (c == d.opts.FieldSeparator || c == d.opts.RowSeparator) {
row = append(row, string(field))
field = field[:0]
if c == d.opts.RowSeparator {
if expected := d.csvInputReader.expectedCols; expected != len(row) {
return errors.Errorf("row %d: expected %d columns, go %d: %#v", count+1, expected, len(row), row)
}
d.csvInputReader.batch.r = append(d.csvInputReader.batch.r, row)
count++
row = make([]string, 0, len(row))
}
continue
}
field = append(field, string(c)...)
}
return nil
}