-
Notifications
You must be signed in to change notification settings - Fork 1
/
ch_format.go
264 lines (227 loc) · 7.52 KB
/
ch_format.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
package main
import (
"database/sql/driver"
"encoding/csv"
"errors"
"github.com/goccy/go-json"
"io"
)
type ClickhouseFormatWriter interface {
Write(value []any) error
io.Closer
}
type ClickhouseFormatReader interface {
Read([]driver.Value) error
io.Closer
}
type ClickhouseFormatReaderFactory func(columnNames, columnTypes []string, reader io.Reader) (ClickhouseFormatReader, error)
type ClickhouseFormatWriterFactory func(columnNames, columnTypes []string, writer io.Writer) (ClickhouseFormatWriter, error)
func newJsonLinesFormatReader(columnNames, columnTypes []string, reader io.Reader) (ClickhouseFormatReader, error) {
decoder := json.NewDecoder(reader)
return &JsonLinesFormatReader{
columns: columnNames,
decoder: decoder,
receiver: make(map[string]any, len(columnNames)),
}, nil
}
type JsonLinesFormatReader struct {
columns []string
decoder *json.Decoder
receiver map[string]any
closer io.Closer
}
func (j *JsonLinesFormatReader) Read(value []driver.Value) error {
err := j.decoder.Decode(&j.receiver)
if err != nil {
return err
}
if len(j.columns) != len(value) {
return errors.New("column length mismatch")
}
for i, column := range j.columns {
value[i] = j.receiver[column]
}
return nil
}
func (j *JsonLinesFormatReader) Close() error {
return j.closer.Close()
}
func newJsonLinesFormatWriter(columnNames, columnTypes []string, writer io.Writer) (ClickhouseFormatWriter, error) {
encoder := json.NewEncoder(writer)
return &JsonLinesFormatWriter{
columns: columnNames,
encoder: encoder,
m: make(map[string]any, len(columnNames)),
}, nil
}
type JsonLinesFormatWriter struct {
columns []string
encoder *json.Encoder
m map[string]any
}
func (j *JsonLinesFormatWriter) Write(value []any) error {
for i, column := range j.columns {
j.m[column] = value[i]
}
return j.encoder.Encode(j.m)
}
func (j *JsonLinesFormatWriter) Close() error {
return nil
}
func newCSVFormatReaderGeneric(columnNames, columnTypes []string, reader io.Reader, sep rune, header bool) (ClickhouseFormatReader, error) {
r := csv.NewReader(reader)
r.ReuseRecord = true
r.Comma = sep
if header {
_, err := r.Read()
if err != nil {
return nil, err
}
}
columnParsers := make([]func(string) (driver.Value, error), len(columnTypes))
for i, columnType := range columnTypes {
columnParsers[i] = getDuckDBConverter(columnType)
}
return &CSVFormatReader{
columns: columnNames,
columnParsers: columnParsers,
reader: r,
}, nil
}
func newCSVFormatReader(columnNames, columnTypes []string, reader io.Reader) (ClickhouseFormatReader, error) {
return newCSVFormatReaderGeneric(columnNames, columnTypes, reader, ',', false)
}
func newCSVHeaderFormatReader(columnNames, columnTypes []string, reader io.Reader) (ClickhouseFormatReader, error) {
return newCSVFormatReaderGeneric(columnNames, columnTypes, reader, ',', true)
}
func newTSVFormatReader(columnNames, columnTypes []string, reader io.Reader) (ClickhouseFormatReader, error) {
return newCSVFormatReaderGeneric(columnNames, columnTypes, reader, '\t', false)
}
func newTSVHeaderFormatReader(columnNames, columnTypes []string, reader io.Reader) (ClickhouseFormatReader, error) {
return newCSVFormatReaderGeneric(columnNames, columnTypes, reader, '\t', true)
}
type CSVFormatReader struct {
columns []string
columnParsers []func(string) (driver.Value, error)
reader *csv.Reader
closer io.Closer
}
func (c *CSVFormatReader) Read(values []driver.Value) error {
if len(c.columns) != len(values) {
return errors.New("column length mismatch")
}
record, err := c.reader.Read()
if err != nil {
return err
}
for i := range c.columns {
values[i], err = c.columnParsers[i](record[i])
if err != nil {
return err
}
}
return nil
}
func (c *CSVFormatReader) Close() error {
return c.closer.Close()
}
type CSVFormatWriter struct {
columns []string
writer *csv.Writer
closer io.Closer
}
func (c *CSVFormatWriter) Write(values []any) error {
strValues := make([]string, len(values))
for i, value := range values {
strValues[i] = duckValueToString(value)
}
return c.writer.Write(strValues)
}
func (c *CSVFormatWriter) Close() error {
c.writer.Flush()
return nil
}
var typesMapping = map[string]string{
"INTEGER": "Int32",
"VARCHAR": "String",
"BIGINT": "Int64",
"BOOLEAN": "UInt8",
"DOUBLE": "Float64",
}
func typesToClickhouseTypes(types []string) []string {
clickhouseTypes := make([]string, len(types))
for i, t := range types {
clickhouseTypes[i] = typesMapping[t]
if clickhouseTypes[i] == "" {
clickhouseTypes[i] = "String"
}
}
return clickhouseTypes
}
func newCSVFormatWriterGeneric(columnNames, columnTypes []string, writer io.Writer, sep rune, header bool, types bool) (ClickhouseFormatWriter, error) {
w := csv.NewWriter(writer)
w.Comma = sep
if header {
err := w.Write(columnNames)
if err != nil {
return nil, err
}
}
if types {
err := w.Write(typesToClickhouseTypes(columnTypes))
if err != nil {
return nil, err
}
}
return &CSVFormatWriter{
columns: columnNames,
writer: w,
}, nil
}
func newCSVFormatWriter(columnNames, columnTypes []string, writer io.Writer) (ClickhouseFormatWriter, error) {
return newCSVFormatWriterGeneric(columnNames, columnTypes, writer, ',', false, false)
}
func newCSVHeaderFormatWriter(columnNames, columnTypes []string, writer io.Writer) (ClickhouseFormatWriter, error) {
return newCSVFormatWriterGeneric(columnNames, columnTypes, writer, ',', true, false)
}
func newTSVFormatWriter(columnNames, columnTypes []string, writer io.Writer) (ClickhouseFormatWriter, error) {
return newCSVFormatWriterGeneric(columnNames, columnTypes, writer, '\t', false, false)
}
func newTSVHeaderFormatWriter(columnNames, columnTypes []string, writer io.Writer) (ClickhouseFormatWriter, error) {
return newCSVFormatWriterGeneric(columnNames, columnTypes, writer, '\t', true, false)
}
func newTSVHeaderWithTypesFormatWriter(columnNames, columnTypes []string, writer io.Writer) (ClickhouseFormatWriter, error) {
return newCSVFormatWriterGeneric(columnNames, columnTypes, writer, '\t', true, true)
}
var chInputFormats = map[string]ClickhouseFormatReaderFactory{
"JSONEachRow": newJsonLinesFormatReader,
"CSV": newCSVFormatReader,
"CSVWithNames": newCSVHeaderFormatReader,
"TabSeparated": newTSVFormatReader,
"TabSeparatedWithNames": newTSVHeaderFormatReader,
}
var chOutputFormats = map[string]ClickhouseFormatWriterFactory{
"JSONEachRow": newJsonLinesFormatWriter,
"CSV": newCSVFormatWriter,
"CSVWithNames": newCSVHeaderFormatWriter,
"TabSeparated": newTSVFormatWriter,
"TabSeparatedWithNames": newTSVHeaderFormatWriter,
"TabSeparatedWithNamesAndTypes": newTSVHeaderWithTypesFormatWriter,
}
var chFormatContentTypes = map[string]string{
"TabSeparated": "text/tab-separated-values; charset=UTF-8",
"TabSeparatedWithNames": "text/tab-separated-values; charset=UTF-8",
"TabSeparatedWithNamesAndTypes": "text/tab-separated-values; charset=UTF-8",
"CSV": "text/csv; charset=UTF-8",
"CSVWithNames": "text/csv; charset=UTF-8",
"JSONEachRow": "application/json; charset=UTF-8",
}
func GetClickhouseFormatContentType(name string) string {
return chFormatContentTypes[name]
}
func GetClickhouseInputFormat(name string) ClickhouseFormatReaderFactory {
return chInputFormats[name]
}
func GetClickhouseOutputFormat(name string) ClickhouseFormatWriterFactory {
return chOutputFormats[name]
}