-
Notifications
You must be signed in to change notification settings - Fork 500
/
nested.go
38 lines (33 loc) · 1.21 KB
/
nested.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
package values
import (
"github.com/apache/arrow/go/v16/arrow"
"github.com/apache/arrow/go/v16/arrow/array"
"github.com/apache/arrow/go/v16/arrow/memory"
)
// sanitizeNested will replace all null entries with empty ones as in CH nested types aren't nullable themselves
// https://clickhouse.com/docs/en/sql-reference/data-types/nullable
// array passed should be of arrow.NestedType
func sanitizeNested(arr arrow.Array) arrow.Array {
if arr.NullN() == 0 {
return arr
}
if _, ok := arr.DataType().(arrow.NestedType); !ok {
// This can happen only if the parent is nested & we need to construct the empty value
return array.MakeFromData(array.NewData(
arr.DataType(), arr.Len(),
append([]*memory.Buffer{nil}, arr.Data().Buffers()[1:]...), // the first elem is validity
arr.Data().Children(),
0, arr.Data().Offset(),
))
}
children := make([]arrow.ArrayData, len(arr.Data().Children()))
for i, child := range arr.Data().Children() {
children[i] = sanitizeNested(array.MakeFromData(child)).Data()
}
return array.MakeFromData(array.NewData(
arr.DataType(), arr.Len(),
append([]*memory.Buffer{nil}, arr.Data().Buffers()[1:]...), // the first elem is validity
children,
0, arr.Data().Offset(),
))
}