-
Notifications
You must be signed in to change notification settings - Fork 342
/
parquet.go
50 lines (45 loc) · 1.17 KB
/
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
50
package parquet
import (
"github.com/Shopify/go-lua"
"github.com/treeverse/lakefs/pkg/actions/lua/util"
"github.com/xitongsys/parquet-go-source/buffer"
"github.com/xitongsys/parquet-go/reader"
)
func Open(l *lua.State) {
parquetOpen := func(l *lua.State) int {
lua.NewLibrary(l, parquetLibrary)
return 1
}
lua.Require(l, "encoding/parquet", parquetOpen, false)
l.Pop(1)
}
var parquetLibrary = []lua.RegistryFunction{
{Name: "get_schema", Function: getSchema},
}
func check(l *lua.State, err error) {
if err != nil {
lua.Errorf(l, err.Error())
panic("unreachable")
}
}
func getSchema(l *lua.State) int {
payload := lua.CheckString(l, 1)
bin := []byte(payload)
t := struct{}{}
fp, err := buffer.NewBufferFile(bin)
check(l, err)
r, err := reader.NewParquetReader(fp, &t, 1)
check(l, err)
output := make([]map[string]string, 0)
for i, elem := range r.Footer.GetSchema() {
if i == 0 {
continue // root element
}
if elem.Type == nil {
output = append(output, map[string]string{"name": elem.Name, "type": "N/A"})
} else {
output = append(output, map[string]string{"name": elem.Name, "type": elem.Type.String()})
}
}
return util.DeepPush(l, output)
}