-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
80 lines (64 loc) · 1.94 KB
/
table.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
package bridge
import (
"errors"
"github.com/DataDog/go-python3"
"github.com/apache/arrow/go/arrow"
"github.com/apache/arrow/go/arrow/array"
)
func PyTableToTable(pyTable *python3.PyObject) (array.Table, error) {
schema, cols, err := PyTableToColumns(pyTable)
if err != nil {
return nil, err
}
// Build the table
table := array.NewTable(schema, cols, -1) // -1 tells it to determine the numRows from the first column
return table, nil
}
// PyTableToColumns returns the records in the pyarrow table.
func PyTableToColumns(pyTable *python3.PyObject) (*arrow.Schema, []array.Column, error) {
// Get the PySchema from the PyTable
pySchema, err := PySchemaFromPyTable(pyTable)
if err != nil {
return nil, nil, err
}
defer pySchema.DecRef()
// Get the GoSchema
schema, err := PySchemaToSchema(pySchema)
if err != nil {
return nil, nil, err
}
columns, err := PyTableToColumnsWithSchema(pyTable, schema)
if err != nil {
return nil, nil, err
}
return schema, columns, nil
}
// PyTableToColumns returns the columns in the pyarrow table.
func PyTableToColumnsWithSchema(pyTable *python3.PyObject, schema *arrow.Schema) ([]array.Column, error) {
fields := schema.Fields()
columns := make([]array.Column, 0, len(fields))
for i := range fields {
pyColumn, err := PyTableGetPyColumn(pyTable, fields[i].Name)
if err != nil {
return nil, err
}
defer pyColumn.DecRef()
col, err := PyColumnToColumnWithField(pyColumn, fields[i])
if err != nil {
return nil, err
}
// columns[i] = *col
columns = append(columns, *col)
}
return columns, nil
}
// PyTableGetPyColumn returns the PyColumn given the name from the PyTable
func PyTableGetPyColumn(pyTable *python3.PyObject, name string) (*python3.PyObject, error) {
pyName := python3.PyUnicode_FromString(name)
defer pyName.DecRef()
pyColumn := CallPyFunc(pyTable, "column", pyName)
if pyColumn == nil {
return nil, errors.New("could not get pyColumn")
}
return pyColumn, nil
}