-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_parquet.go
49 lines (42 loc) · 1.15 KB
/
read_parquet.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
package pandat
import (
"github.com/xitongsys/parquet-go-source/local"
"github.com/xitongsys/parquet-go/reader"
"github.com/xitongsys/parquet-go/schema"
"github.com/xitongsys/parquet-go/source"
"runtime"
)
func ReadParquetPath(filepath string) (*DataFrame[any], error) {
f, err := local.NewLocalFileReader(filepath)
if err != nil {
return nil, err
}
return ReadParquet(f)
}
func ReadParquet(f source.ParquetFile) (*DataFrame[any], error) {
pr, err := reader.NewParquetReader(f, nil, int64(runtime.NumCPU()))
if err != nil {
return nil, err
}
defer pr.ReadStop()
names := parquetColumnNames(pr.SchemaHandler)
seriess := make([]*Series[any], 0, len(names))
for i := 0; i < len(names); i++ {
values, _, _, err := pr.ReadColumnByIndex(int64(i), pr.GetNumRows())
if err != nil {
return nil, err
}
seriess = append(seriess, NewSeries(names[i], values...))
}
return NewDataFrame(seriess...), nil
}
func parquetColumnNames(schema *schema.SchemaHandler) []string {
names := make([]string, 0, len(schema.SchemaElements)-1)
for i, tag := range schema.Infos {
if i == 0 {
continue
}
names = append(names, tag.ExName)
}
return names
}