forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvalue.go
114 lines (98 loc) · 2.51 KB
/
value.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
package athena
import (
"database/sql/driver"
"fmt"
"strconv"
"time"
"github.com/aws/aws-sdk-go/service/athena"
)
const (
// TimestampLayout is the Go time layout string for an Athena `timestamp`.
TimestampLayout = "2006-01-02 15:04:05.999"
TimestampWithTimeZoneLayout = "2006-01-02 15:04:05.999 MST"
DateLayout = "2006-01-02"
)
const nullStringResultModeGzipDL string = "\\N"
func convertRow(columns []*athena.ColumnInfo, in []*athena.Datum, ret []driver.Value) error {
for i, val := range in {
coerced, err := convertValue(*columns[i].Type, val.VarCharValue)
if err != nil {
return err
}
ret[i] = coerced
}
return nil
}
func convertRowFromTableInfo(columns []*athena.Column, in []string, ret []driver.Value) error {
for i, val := range in {
var coerced interface{}
var err error
if val == nullStringResultModeGzipDL {
var nullVal *string
coerced, err = convertValue(*columns[i].Type, nullVal)
} else {
coerced, err = convertValue(*columns[i].Type, &val)
}
if err != nil {
return err
}
ret[i] = coerced
}
return nil
}
func convertRowFromCsv(columns []*athena.ColumnInfo, in []downloadField, ret []driver.Value) error {
for i, df := range in {
var coerced interface{}
var err error
if df.isNil {
var nullVal *string
coerced, err = convertValue(*columns[i].Type, nullVal)
} else {
coerced, err = convertValue(*columns[i].Type, &df.val)
}
if err != nil {
return err
}
ret[i] = coerced
}
return nil
}
func convertValue(athenaType string, rawValue *string) (interface{}, error) {
if rawValue == nil {
return nil, nil
}
if len(athenaType) > 7 && athenaType[:7] == "decimal" {
athenaType = "decimal"
}
val := *rawValue
switch athenaType {
case "smallint":
return strconv.ParseInt(val, 10, 16)
case "integer", "int":
return strconv.ParseInt(val, 10, 32)
case "bigint":
return strconv.ParseInt(val, 10, 64)
case "boolean":
switch val {
case "true":
return true, nil
case "false":
return false, nil
}
return nil, fmt.Errorf("cannot parse '%s' as boolean", val)
case "float":
return strconv.ParseFloat(val, 32)
case "double", "decimal":
return strconv.ParseFloat(val, 64)
case "varchar", "string":
return val, nil
case "timestamp":
return time.Parse(TimestampLayout, val)
case "timestamp with time zone":
return time.Parse(TimestampWithTimeZoneLayout, val)
case "date":
return time.Parse(DateLayout, val)
default:
panic(fmt.Errorf("unknown type `%s` with value %s", athenaType, val))
}
}