]> git.proxmox.com Git - ceph.git/blob - ceph/src/arrow/julia/Arrow/src/arraytypes/bool.jl
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / julia / Arrow / src / arraytypes / bool.jl
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements. See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership. The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License. You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 """
18 Arrow.BoolVector
19
20 A bit-packed array type, similar to [`ValidityBitmap`](@ref), but which
21 holds boolean values, `true` or `false`.
22 """
23 struct BoolVector{T} <: ArrowVector{T}
24 arrow::Vector{UInt8} # need to hold a reference to arrow memory blob
25 pos::Int
26 validity::ValidityBitmap
27 ℓ::Int64
28 metadata::Union{Nothing, Dict{String, String}}
29 end
30
31 Base.size(p::BoolVector) = (p.ℓ,)
32
33 @propagate_inbounds function Base.getindex(p::BoolVector{T}, i::Integer) where {T}
34 @boundscheck checkbounds(p, i)
35 if T >: Missing
36 @inbounds !p.validity[i] && return missing
37 end
38 a, b = fldmod1(i, 8)
39 @inbounds byte = p.arrow[p.pos + a - 1]
40 # check individual bit of byte
41 return getbit(byte, b)
42 end
43
44 @propagate_inbounds function Base.setindex!(p::BoolVector, v, i::Integer)
45 @boundscheck checkbounds(p, i)
46 x = convert(Bool, v)
47 a, b = fldmod1(i, 8)
48 @inbounds byte = p.arrow[p.pos + a - 1]
49 @inbounds p.arrow[p.pos + a - 1] = setbit(byte, x, b)
50 return v
51 end
52
53 arrowvector(::BoolType, x::BoolVector, i, nl, fi, de, ded, meta; kw...) = x
54
55 function arrowvector(::BoolType, x, i, nl, fi, de, ded, meta; kw...)
56 validity = ValidityBitmap(x)
57 len = length(x)
58 blen = cld(len, 8)
59 bytes = Vector{UInt8}(undef, blen)
60 b = 0xff
61 j = k = 1
62 for y in x
63 if y === false
64 b = setbit(b, false, j)
65 end
66 j += 1
67 if j == 9
68 @inbounds bytes[k] = b
69 b = 0xff
70 j = 1
71 k += 1
72 end
73 end
74 if j > 1
75 bytes[k] = b
76 end
77 return BoolVector{eltype(x)}(bytes, 1, validity, len, meta)
78 end
79
80 function compress(Z::Meta.CompressionType, comp, p::P) where {P <: BoolVector}
81 len = length(p)
82 nc = nullcount(p)
83 validity = compress(Z, comp, p.validity)
84 data = compress(Z, comp, view(p.arrow, p.pos:(p.pos + cld(p.ℓ, 8) - 1)))
85 return Compressed{Z, P}(p, [validity, data], len, nc, Compressed[])
86 end
87
88 function makenodesbuffers!(col::BoolVector, fieldnodes, fieldbuffers, bufferoffset, alignment)
89 len = length(col)
90 nc = nullcount(col)
91 push!(fieldnodes, FieldNode(len, nc))
92 @debug 1 "made field node: nodeidx = $(length(fieldnodes)), col = $(typeof(col)), len = $(fieldnodes[end].length), nc = $(fieldnodes[end].null_count)"
93 # validity bitmap
94 blen = nc == 0 ? 0 : bitpackedbytes(len, alignment)
95 push!(fieldbuffers, Buffer(bufferoffset, blen))
96 @debug 1 "made field buffer: bufferidx = $(length(fieldbuffers)), offset = $(fieldbuffers[end].offset), len = $(fieldbuffers[end].length), padded = $(padding(fieldbuffers[end].length, alignment))"
97 # adjust buffer offset, make primitive array buffer
98 bufferoffset += blen
99 blen = bitpackedbytes(len, alignment)
100 push!(fieldbuffers, Buffer(bufferoffset, blen))
101 @debug 1 "made field buffer: bufferidx = $(length(fieldbuffers)), offset = $(fieldbuffers[end].offset), len = $(fieldbuffers[end].length), padded = $(padding(fieldbuffers[end].length, alignment))"
102 return bufferoffset + blen
103 end
104
105 function writebuffer(io, col::BoolVector, alignment)
106 @debug 1 "writebuffer: col = $(typeof(col))"
107 @debug 2 col
108 writebitmap(io, col, alignment)
109 n = Base.write(io, view(col.arrow, col.pos:(col.pos + cld(col.ℓ, 8) - 1)))
110 return n + writezeros(io, paddinglength(n, alignment))
111 end