-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
144 lines (137 loc) · 3.54 KB
/
helpers.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
package main
import (
"bufio"
"io"
"strconv"
"strings"
)
func readTabSeparated(rd io.Reader) ([][]string, error) {
res := [][]string{}
bufferedReader := bufio.NewReader(rd)
for {
line, err := bufferedReader.ReadString('\n')
line = strings.TrimRight(line, "\n")
switch err {
case nil:
fields := strings.Split(line, "\t")
for idx, v := range fields {
// \b, \f, \r, \n, \t, \0, \', and \\
v = strings.Replace(v, "\\b", "\b", -1)
v = strings.Replace(v, "\\f", "\f", -1)
v = strings.Replace(v, "\\r", "\r", -1)
v = strings.Replace(v, "\\n", "\n", -1)
v = strings.Replace(v, "\\t", "\t", -1)
v = strings.Replace(v, "\\0", "\x00", -1)
v = strings.Replace(v, "\\'", "'", -1)
v = strings.Replace(v, "\\\\", "\\", -1)
fields[idx] = v
}
res = append(res, fields)
case io.EOF:
return res, nil
default:
return res, err
}
}
}
type counerFunc func(string) uint64
func getRowsCounter(format string) counerFunc {
var counterState int
var count uint64
switch format {
case formatTabSeparated, "TSV", "CSV", "TSKV", "JSONEachRow", "TabSeparatedRaw", "TSVRaw":
return func(line string) uint64 {
count++
return count
}
case "TabSeparatedWithNames", "TSVWithNames", "CSVWithNames":
return func(line string) uint64 {
if counterState > 0 {
count++
} else {
counterState++
}
return count
}
case "TabSeparatedWithNamesAndTypes", "TSVWithNamesAndTypes", "PrettySpace", "PrettySpaceNoEscapes":
return func(line string) uint64 {
if counterState > 1 {
count++
} else {
counterState++
}
return count
}
case "BlockTabSeparated":
return func(line string) uint64 {
if counterState == 0 {
count = uint64(strings.Count(line, "\t")) + 1
counterState = 1
}
return count
}
case "Pretty", "PrettyCompact", "PrettyCompactMonoBlock", "PrettyNoEscapes", "PrettyCompactNoEscapes":
return func(line string) uint64 {
if strings.HasPrefix(line, "│") {
count++
}
return count
}
case formatVertical, "VerticalRaw":
return func(line string) uint64 {
if strings.HasPrefix(line, "───") {
count++
}
return count
}
case "JSON", "JSONCompact":
return func(line string) uint64 {
lineTrimmed := strings.TrimSpace(line)
switch counterState {
case 0: // waiting for data start
if lineTrimmed == "\"data\":" {
counterState = 1
}
case 1: // waiting for </data> start
if lineTrimmed == "]," {
counterState = 2
}
case 2: // waiting for <rows>
if strings.HasPrefix(lineTrimmed, "\"rows\":") {
lineTrimmed = strings.TrimPrefix(lineTrimmed, "\"rows\": ")
lineTrimmed = strings.TrimSuffix(lineTrimmed, ",")
count, _ = strconv.ParseUint(lineTrimmed, 10, 64)
counterState = 3
}
}
return count
}
case "XML":
return func(line string) uint64 {
lineTrimmed := strings.TrimSpace(line)
switch counterState {
case 0: // waiting for <data> start
if strings.HasPrefix(lineTrimmed, "<data>") {
counterState = 1
}
case 1: // waiting for </data> start
if strings.HasPrefix(lineTrimmed, "</data>") {
counterState = 2
}
case 2: // waiting for <rows>
if strings.HasPrefix(lineTrimmed, "<rows>") {
lineTrimmed = strings.TrimPrefix(lineTrimmed, "<rows>")
lineTrimmed = strings.TrimSuffix(lineTrimmed, "</rows>")
count, _ = strconv.ParseUint(lineTrimmed, 10, 64)
counterState = 3
}
}
return count
}
default:
// case "Null","Native","RowBinary","Values","CapnProto","ODBCDriver":
return func(line string) uint64 {
return 0
}
}
}