-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathfieldcolumn_reading.jl
188 lines (162 loc) · 6.87 KB
/
fieldcolumn_reading.jl
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
_field_output_type(::Type{F}) where F
This is function is used in two ways:
- provide a output type prediction for each "field" in RNTuple so we can
achieve type stability
- it's also used to enforce the type stability in [`read_field`](@ref):
```
# this is basically a type assertion for `res`
return res::_field_output_type(field)
```
"""
function _field_output_type() end
"""
read_field(io, field::F, page_list) where F
Read a field from the `io` stream. The `page_list` is a list of PageLinks for the
current cluster group. The type stability is achieved by type asserting
based on type `F` via [`_field_output_type`](@ref) function.
"""
function read_field() end
_field_output_type(x::T) where T = _field_output_type(T)
function _field_output_type(::Type{StdArrayField{N, T}}) where {N, T<:LeafField}
content_type = _field_output_type(T)
elT = eltype(content_type)
return Base.ReinterpretArray{SVector{N, elT}, 1, UInt8, Vector{UInt8}, false}
end
function _field_output_type(::Type{StdArrayField{N, T}}) where {N, T}
content_type = _field_output_type(T)
elT = eltype(content_type)
return Base.ReinterpretArray{SVector{N, elT}, 1, elT, content_type, false}
end
function read_field(io, field::StdArrayField{N, T}, page_list) where {N, T}
content = read_field(io, field.content_col, page_list)
res = reinterpret(SVector{N, eltype(content)}, content)
return res::_field_output_type(field)
end
_field_output_type(::Type{StringField{O, T}}) where {O, T} = Vector{String}
function read_field(io, field::StringField{O, T}, page_list) where {O, T}
nbits = field.content_col.nbits
pages = page_list[field.content_col.content_col_idx]
offset = read_field(io, field.offset_col, page_list)
content = read_pagedesc(io, pages, nbits)
o = one(eltype(offset))
jloffset = pushfirst!(offset .+ o, o) #change to 1-indexed, and add a 1 at the beginning
res = String.(VectorOfVectors(content, jloffset, ArraysOfArrays.no_consistency_checks))
return res::_field_output_type(field)
end
const T_Reinter{T} = Base.ReinterpretArray{T, 1, UInt8, Vector{UInt8}, false}
struct CardinalityVector{T} <: AbstractVector{T}
contents::T_Reinter{T}
end
Base.length(ary::CardinalityVector) = length(ary.contents)
Base.size(ary::CardinalityVector) = (length(ary.contents), )
Base.IndexStyle(::CardinalityVector) = IndexLinear()
function Base.getindex(ary::CardinalityVector{T}, i::Int) where {T}
ary.contents[i] - get(ary.contents, i-1, zero(T))
end
_field_output_type(::Type{RNTupleCardinality{T}}) where {T} = CardinalityVector{T}
function read_field(io, field::RNTupleCardinality{T}, page_list) where T
nbits = field.leaf_field.nbits
pages = page_list[field.leaf_field.content_col_idx]
typenum = field.leaf_field.type
split = 14 <= typenum <= 21 || 26 <= typenum <= 28
delta = 14 <= typenum <= 15
bytes = read_pagedesc(io, pages, nbits; split)
contents = reinterpret(T, bytes)
if delta
cumsum!(contents, contents)
end
res = CardinalityVector(contents)
return res::_field_output_type(field)
end
_from_zigzag(n) = (n >> 1) ⊻ -(n & 1)
_to_zigzag(n) = (n << 1) ⊻ (n >> 63)
_field_output_type(::Type{LeafField{T}}) where {T} = T_Reinter{T}
function read_field(io, field::LeafField{T}, page_list) where T
nbits = field.nbits
pages = page_list[field.content_col_idx]
# handle split encoding within page
typenum = field.type
split = 14 <= typenum <= 21 || 26 <= typenum <= 28
zigzag = 26 <= typenum <= 28
delta = 14 <= typenum <= 15
bytes = read_pagedesc(io, pages, nbits; split = split)
res = reinterpret(T, bytes)
if zigzag
@simd for i in eachindex(res)
res[i] = _from_zigzag(res[i])
end
elseif delta
cumsum!(res, res)
end
return res::_field_output_type(field)
end
_field_output_type(::Type{LeafField{Bool}}) = BitVector
function read_field(io, field::LeafField{Bool}, page_list)
nbits = field.nbits
pages = page_list[field.content_col_idx]
total_num_elements = sum(p.num_elements for p in pages)
# pad to nearest 8*k bytes because each chunk needs to be UInt64
bytes = read_pagedesc(io, pages, nbits)
append!(bytes, zeros(eltype(bytes), 8 - rem(total_num_elements, 8)))
chunks = reinterpret(UInt64, bytes)
res = BitVector(undef, total_num_elements)
copyto!(res.chunks, chunks) # don't want jam ReinterpretArray into BitVector
return res::_field_output_type(field)
end
_field_output_type(::Type{VectorField{O, T}}) where {O, T} = VectorOfVectors{eltype(_field_output_type(T)), _field_output_type(T), Vector{eltype(O)}, Vector{Tuple{}}}
function read_field(io, field::VectorField{O, T}, page_list) where {O, T}
offset = read_field(io, field.offset_col, page_list)
content = read_field(io, field.content_col, page_list)
o = one(eltype(offset))
jloffset = pushfirst!(offset .+ o, o) #change to 1-indexed, and add a 1 at the beginning
res = VectorOfVectors(content, jloffset)
return res::_field_output_type(field)
end
function _field_output_type(::Type{StructField{N, T}}) where {N, T}
types = Tuple{eltype.(_field_output_type.(T.types))...}
types2 = Tuple{_field_output_type.(T.types)...}
StructArray{NamedTuple{N, types}, 1, NamedTuple{N, types2}, Int64}
end
"""
read_field(io, field::StructField{N, T}, page_list) where {N, T}
Since each field of the struct is stored in a separate field of the RNTuple,
this function returns a `StructArray` to maximize efficiency.
"""
function read_field(io, field::StructField{N, T}, page_list) where {N, T}
contents = (read_field(io, col, page_list) for col in field.content_cols)
res = StructArray(NamedTuple{N}(contents))
return res::_field_output_type(field)
end
struct UnionVector{T, N} <: AbstractVector{T}
kindex::Vector{UInt64}
tag::Vector{Int8}
contents::N
function UnionVector(kindex, tag, contents::N) where N
T = Union{eltype.(contents)...}
return new{T, N}(kindex, tag, contents)
end
end
Base.length(ary::UnionVector) = length(ary.tag)
Base.size(ary::UnionVector) = (length(ary.tag), )
Base.IndexStyle(::UnionVector) = IndexLinear()
function Base.getindex(ary::UnionVector, i::Int)
ith_ele = ary.kindex[i]
ith_type = ary.tag[i]
return ary.contents[ith_type][ith_ele]
end
function _split_switch_bits(content)
kindex = content .& 0x00000000000FFFFF .+ 1
tags = Int8.(content .>> 44)
return kindex, tags
end
function _field_output_type(::Type{UnionField{S, T}}) where {S, T}
types = _field_output_type.(T.types)
return UnionVector{Union{eltype.(types)...}, Tuple{types...}}
end
function read_field(io, field::UnionField{S, T}, page_list) where {S, T}
switch = read_field(io, field.switch_col, page_list)
content = Tuple(read_field(io, col, page_list) for col in field.content_cols)
res = UnionVector(_split_switch_bits(switch)..., content)
return res::_field_output_type(field)
end