forked from segmentio/go-athena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
68 lines (60 loc) · 1.54 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
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"
)
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 convertValue(athenaType string, rawValue *string) (interface{}, error) {
if rawValue == nil {
return nil, nil
}
val := *rawValue
switch athenaType {
case "smallint":
return strconv.ParseInt(val, 10, 16)
case "integer":
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))
}
}