-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datatype.go
82 lines (73 loc) · 2.67 KB
/
datatype.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
package bridge
import (
"errors"
"fmt"
"github.com/DataDog/go-python3"
"github.com/apache/arrow/go/arrow"
)
// PyDataTypeToDataType returns the Go arrow DataType given the Python type.
func PyDataTypeToDataType(pyDtype *python3.PyObject) (arrow.DataType, error) {
// get the id
id, err := PyDataTypeGetID(pyDtype)
if err != nil {
return nil, err
}
t := arrow.Type(id)
return GetFromType(t)
}
func PyDataTypeGetID(pyDtype *python3.PyObject) (int, error) {
v, ok := GetIntAttr(pyDtype, "id")
if !ok {
return 0, errors.New("could not get pyDtype.id")
}
return v, nil
}
var (
dataTypeForType [32]arrow.DataType
)
// GetFromType returns a arrow.DataType for a given arrow.Type
func GetFromType(t arrow.Type) (arrow.DataType, error) {
dtype := dataTypeForType[byte(t&0x1f)]
if dtype == nil {
return nil, fmt.Errorf("DataType for id=%v is not yet implemented", t)
}
return dtype, nil
}
// DataTypeFromType returns an arrow.DataType given the Type
func init() {
dataTypeForType = [...]arrow.DataType{
arrow.NULL: arrow.Null,
arrow.BOOL: arrow.FixedWidthTypes.Boolean,
arrow.UINT8: arrow.PrimitiveTypes.Uint8,
arrow.INT8: arrow.PrimitiveTypes.Int8,
arrow.UINT16: arrow.PrimitiveTypes.Uint16,
arrow.INT16: arrow.PrimitiveTypes.Int16,
arrow.UINT32: arrow.PrimitiveTypes.Uint32,
arrow.INT32: arrow.PrimitiveTypes.Int32,
arrow.UINT64: arrow.PrimitiveTypes.Uint64,
arrow.INT64: arrow.PrimitiveTypes.Int64,
arrow.FLOAT16: arrow.FixedWidthTypes.Float16,
arrow.FLOAT32: arrow.PrimitiveTypes.Float32,
arrow.FLOAT64: arrow.PrimitiveTypes.Float64,
arrow.STRING: arrow.BinaryTypes.String,
arrow.BINARY: arrow.BinaryTypes.Binary,
arrow.FIXED_SIZE_BINARY: nil, // arrow.FixedSizeBinaryType,
arrow.DATE32: arrow.PrimitiveTypes.Date32,
arrow.DATE64: arrow.PrimitiveTypes.Date64,
arrow.TIMESTAMP: nil, // arrow.FixedWidthTypes.Timestamp_s, // TODO
arrow.TIME32: nil, // arrow.FixedWidthTypes.Time32s, // TODO
arrow.TIME64: nil, // arrow.FixedWidthTypes.Time64us, // TODO
arrow.INTERVAL: nil, // arrow.FixedWidthTypes.MonthInterval, // TODO
arrow.DECIMAL: nil,
arrow.LIST: nil,
arrow.STRUCT: nil,
arrow.UNION: nil,
arrow.DICTIONARY: nil,
arrow.MAP: nil,
arrow.EXTENSION: nil,
arrow.FIXED_SIZE_LIST: nil,
arrow.DURATION: nil, // arrow.FixedWidthTypes.Duration_s, // TODO
// invalid data types to fill out array size 2⁵-1
31: nil,
}
}