forked from parquet-go/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolumn_buffer_boolean.go
More file actions
236 lines (201 loc) · 6.52 KB
/
Copy pathcolumn_buffer_boolean.go
File metadata and controls
236 lines (201 loc) · 6.52 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package parquet
import (
"io"
"github.com/parquet-go/bitpack"
"github.com/parquet-go/parquet-go/deprecated"
"github.com/parquet-go/parquet-go/internal/memory"
"github.com/parquet-go/parquet-go/sparse"
)
type booleanColumnBuffer struct{ booleanPage }
func newBooleanColumnBuffer(typ Type, columnIndex uint16, numValues int32) *booleanColumnBuffer {
// Boolean values are bit-packed, we can fit up to 8 values per byte.
bufferSize := (numValues + 7) / 8
return &booleanColumnBuffer{
booleanPage: booleanPage{
typ: typ,
bits: memory.SliceBufferFor[byte](int(bufferSize)),
columnIndex: ^columnIndex,
},
}
}
func (col *booleanColumnBuffer) Clone() ColumnBuffer {
return &booleanColumnBuffer{
booleanPage: booleanPage{
typ: col.typ,
bits: col.bits.Clone(),
offset: col.offset,
numValues: col.numValues,
columnIndex: col.columnIndex,
},
}
}
func (col *booleanColumnBuffer) ColumnIndex() (ColumnIndex, error) {
return booleanColumnIndex{&col.booleanPage}, nil
}
func (col *booleanColumnBuffer) OffsetIndex() (OffsetIndex, error) {
return booleanOffsetIndex{&col.booleanPage}, nil
}
func (col *booleanColumnBuffer) BloomFilter() BloomFilter { return nil }
func (col *booleanColumnBuffer) Dictionary() Dictionary { return nil }
func (col *booleanColumnBuffer) Pages() Pages { return onePage(col.Page()) }
func (col *booleanColumnBuffer) Page() Page { return &col.booleanPage }
func (col *booleanColumnBuffer) Reset() {
col.bits.Reset()
col.offset = 0
col.numValues = 0
}
func (col *booleanColumnBuffer) Cap() int { return 8 * col.bits.Cap() }
func (col *booleanColumnBuffer) Len() int { return int(col.numValues) }
func (col *booleanColumnBuffer) Less(i, j int) bool {
a := col.valueAt(i)
b := col.valueAt(j)
return a != b && !a
}
func (col *booleanColumnBuffer) valueAt(i int) bool {
bits := col.bits.Slice()
j := uint32(i) / 8
k := uint32(i) % 8
return ((bits[j] >> k) & 1) != 0
}
func (col *booleanColumnBuffer) setValueAt(i int, v bool) {
// `offset` is always zero in the page of a column buffer
bits := col.bits.Slice()
j := uint32(i) / 8
k := uint32(i) % 8
x := byte(0)
if v {
x = 1
}
bits[j] = (bits[j] & ^(1 << k)) | (x << k)
}
func (col *booleanColumnBuffer) Swap(i, j int) {
a := col.valueAt(i)
b := col.valueAt(j)
col.setValueAt(i, b)
col.setValueAt(j, a)
}
func (col *booleanColumnBuffer) WriteBooleans(values []bool) (int, error) {
col.writeValues(columnLevels{}, sparse.MakeBoolArray(values).UnsafeArray())
return len(values), nil
}
func (col *booleanColumnBuffer) WriteValues(values []Value) (int, error) {
col.writeValues(columnLevels{}, makeArrayValue(values, offsetOfBool))
return len(values), nil
}
func (col *booleanColumnBuffer) writeValues(_ columnLevels, rows sparse.Array) {
numBytes := bitpack.ByteCount(uint(col.numValues) + uint(rows.Len()))
if col.bits.Cap() < numBytes {
col.bits.Grow(numBytes - col.bits.Len())
}
col.bits.Resize(numBytes)
bits := col.bits.Slice()
i := 0
r := 8 - (int(col.numValues) % 8)
bytes := rows.Uint8Array()
if r <= bytes.Len() {
// First we attempt to write enough bits to align the number of values
// in the column buffer on 8 bytes. After this step the next bit should
// be written at the zero'th index of a byte of the buffer.
if r < 8 {
var b byte
for i < r {
v := bytes.Index(i)
b |= (v & 1) << uint(i)
i++
}
x := uint(col.numValues) / 8
y := uint(col.numValues) % 8
bits[x] = (b << y) | (bits[x] & ^(0xFF << y))
col.numValues += int32(i)
}
if n := ((bytes.Len() - i) / 8) * 8; n > 0 {
// At this stage, we know that that we have at least 8 bits to write
// and the bits will be aligned on the address of a byte in the
// output buffer. We can work on 8 values per loop iteration,
// packing them into a single byte and writing it to the output
// buffer. This effectively reduces by 87.5% the number of memory
// stores that the program needs to perform to generate the values.
i += sparse.GatherBits(bits[col.numValues/8:], bytes.Slice(i, i+n))
col.numValues += int32(n)
}
}
for i < bytes.Len() {
x := uint(col.numValues) / 8
y := uint(col.numValues) % 8
b := bytes.Index(i)
bits[x] = ((b & 1) << y) | (bits[x] & ^(1 << y))
col.numValues++
i++
}
col.bits.Resize(bitpack.ByteCount(uint(col.numValues)))
col.clearTrailingBits()
}
func (col *booleanColumnBuffer) writeBoolean(levels columnLevels, value bool) {
numBytes := bitpack.ByteCount(uint(col.numValues) + 1)
if col.bits.Cap() < numBytes {
col.bits.Grow(numBytes - col.bits.Len())
}
col.bits.Resize(numBytes)
bits := col.bits.Slice()
x := uint(col.numValues) / 8
y := uint(col.numValues) % 8
bit := byte(0)
if value {
bit = 1
}
bits[x] = (bit << y) | (bits[x] & ^(1 << y))
col.numValues++
col.clearTrailingBits()
}
// clearTrailingBits zeros bit positions beyond numValues in the last byte of
// the bit-packed buffer. SliceBuffer growth does not zero new bytes, so a
// partial trailing byte can otherwise leak stale pool memory and produce
// non-deterministic encoded output (https://github.com/parquet-go/parquet-go/issues/520).
func (col *booleanColumnBuffer) clearTrailingBits() {
extra := uint(col.numValues) % 8
if extra == 0 {
return
}
bits := col.bits.Slice()
bits[len(bits)-1] &= byte(1<<extra) - 1
}
func (col *booleanColumnBuffer) writeInt32(levels columnLevels, value int32) {
col.writeBoolean(levels, value != 0)
}
func (col *booleanColumnBuffer) writeInt64(levels columnLevels, value int64) {
col.writeBoolean(levels, value != 0)
}
func (col *booleanColumnBuffer) writeInt96(levels columnLevels, value deprecated.Int96) {
col.writeBoolean(levels, !value.IsZero())
}
func (col *booleanColumnBuffer) writeFloat(levels columnLevels, value float32) {
col.writeBoolean(levels, value != 0)
}
func (col *booleanColumnBuffer) writeDouble(levels columnLevels, value float64) {
col.writeBoolean(levels, value != 0)
}
func (col *booleanColumnBuffer) writeByteArray(levels columnLevels, value []byte) {
col.writeBoolean(levels, len(value) != 0)
}
func (col *booleanColumnBuffer) writeNull(levels columnLevels) {
col.writeBoolean(levels, false)
}
func (col *booleanColumnBuffer) ReadValuesAt(values []Value, offset int64) (n int, err error) {
i := int(offset)
switch {
case i < 0:
return 0, errRowIndexOutOfBounds(offset, int64(col.numValues))
case i >= int(col.numValues):
return 0, io.EOF
default:
for n < len(values) && i < int(col.numValues) {
values[n] = col.makeValue(col.valueAt(i))
n++
i++
}
if n < len(values) {
err = io.EOF
}
return n, err
}
}