/
helpers.go
92 lines (78 loc) · 2.02 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
package godatabend
import (
"bytes"
"fmt"
"net/http"
"strings"
"time"
"github.com/pkg/errors"
)
var (
escaper = strings.NewReplacer(`\`, `\\`, `'`, `\'`)
dateFormat = "2006-01-02"
timeFormat = "2006-01-02 15:04:05"
dateTime64Format = "2006-01-02 15:04:05.999999999"
)
func escape(s string) string {
return escaper.Replace(s)
}
func quote(s string) string {
return "'" + s + "'"
}
func formatTime(value time.Time) string {
return quote(value.Format(timeFormat))
}
func formatDate(value time.Time) string {
return quote(value.Format(dateFormat))
}
func readResponse(response *http.Response) (result []byte, err error) {
if response.ContentLength > 0 {
result = make([]byte, 0, response.ContentLength)
}
buf := bytes.NewBuffer(result)
defer response.Body.Close()
_, err = buf.ReadFrom(response.Body)
result = buf.Bytes()
return
}
func getTableFromInsertQuery(query string) (string, error) {
if !strings.Contains(query, "insert") && !strings.Contains(query, "INSERT") {
return "", errors.New("wrong insert statement")
}
splitQuery := strings.Split(query, " ")
if len(splitQuery) > 2 {
return strings.TrimSpace(splitQuery[2]), nil
}
return "", errors.New("wrong insert")
}
func generateDescTable(query string) (string, error) {
table, err := getTableFromInsertQuery(query)
if err != nil {
return "", err
}
return fmt.Sprintf("DESC %s", table), nil
}
func databendParquetReflect(databendType string) string {
var parquetType string
switch databendType {
case "VARCHAR":
parquetType = "type=BYTE_ARRAY, convertedtype=UTF8, encoding=PLAIN_DICTIONARY"
case "BOOLEAN":
parquetType = "type=BOOLEAN"
case "TINYINT", "SMALLINT", "INT":
parquetType = "type=INT32"
case "BIGINT":
parquetType = "type=INT64"
case "FLOAT":
parquetType = "type=FLOAT"
case "DOUBLE":
parquetType = "type=DOUBLE"
case "DATE":
parquetType = "type=INT32, convertedtype=DATE"
case "TIMESTAMP":
parquetType = "type=INT64"
case "ARRAY":
parquetType = "type=LIST, convertedtype=LIST"
}
return parquetType
}