|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::mem; |
| 5 | +use std::mem::MaybeUninit; |
| 6 | + |
| 7 | +use vortex_array::Canonical; |
| 8 | +use vortex_array::ExecutionCtx; |
| 9 | +use vortex_array::IntoArray; |
| 10 | +use vortex_array::arrays::BoolArray; |
| 11 | +use vortex_array::validity::Validity; |
| 12 | +use vortex_buffer::BitBuffer; |
| 13 | +use vortex_buffer::ByteBuffer; |
| 14 | +use vortex_buffer::ByteBufferMut; |
| 15 | +use vortex_error::VortexExpect; |
| 16 | +use vortex_error::VortexResult; |
| 17 | + |
| 18 | +use crate::bit_transpose::transpose_bits; |
| 19 | +use crate::bit_transpose::untranspose_bits; |
| 20 | + |
| 21 | +pub fn transpose_validity(validity: &Validity, ctx: &mut ExecutionCtx) -> VortexResult<Validity> { |
| 22 | + match validity { |
| 23 | + Validity::Array(mask) => { |
| 24 | + let bools = mask |
| 25 | + .clone() |
| 26 | + .execute::<Canonical>(ctx)? |
| 27 | + .into_bool() |
| 28 | + .into_bit_buffer(); |
| 29 | + |
| 30 | + Ok(Validity::Array( |
| 31 | + BoolArray::new(transpose_bitbuffer(bools), Validity::NonNullable).into_array(), |
| 32 | + )) |
| 33 | + } |
| 34 | + v @ Validity::AllValid | v @ Validity::AllInvalid | v @ Validity::NonNullable => { |
| 35 | + Ok(v.clone()) |
| 36 | + } |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +#[inline] |
| 41 | +pub fn transpose_bitbuffer(bits: BitBuffer) -> BitBuffer { |
| 42 | + let (offset, len, bytes) = bits.into_inner(); |
| 43 | + |
| 44 | + if bytes.len().is_multiple_of(128) { |
| 45 | + match bytes.try_into_mut() { |
| 46 | + Ok(mut bytes_mut) => { |
| 47 | + // We can ignore the spare trailer capacity that can be an artifact of allocator as we requested 128 multiple chunks |
| 48 | + let (chunks, _) = bytes_mut.as_chunks_mut::<128>(); |
| 49 | + let mut tmp = [0u8; 128]; |
| 50 | + for chunk in chunks { |
| 51 | + transpose_bits(chunk, &mut tmp); |
| 52 | + chunk.copy_from_slice(&tmp); |
| 53 | + } |
| 54 | + BitBuffer::new_with_offset(bytes_mut.freeze().into_byte_buffer(), len, offset) |
| 55 | + } |
| 56 | + Err(bytes) => bits_op_with_copy(bytes, len, offset, transpose_bits), |
| 57 | + } |
| 58 | + } else { |
| 59 | + bits_op_with_copy(bytes, len, offset, transpose_bits) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +pub fn untranspose_validity(validity: &Validity, ctx: &mut ExecutionCtx) -> VortexResult<Validity> { |
| 64 | + match validity { |
| 65 | + Validity::Array(mask) => { |
| 66 | + let bools = mask |
| 67 | + .clone() |
| 68 | + .execute::<Canonical>(ctx)? |
| 69 | + .into_bool() |
| 70 | + .into_bit_buffer(); |
| 71 | + |
| 72 | + Ok(Validity::Array( |
| 73 | + BoolArray::new(untranspose_bitbuffer(bools), Validity::NonNullable).into_array(), |
| 74 | + )) |
| 75 | + } |
| 76 | + v @ Validity::AllValid | v @ Validity::AllInvalid | v @ Validity::NonNullable => { |
| 77 | + Ok(v.clone()) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +#[inline] |
| 83 | +pub fn untranspose_bitbuffer(bits: BitBuffer) -> BitBuffer { |
| 84 | + assert!( |
| 85 | + bits.inner().len().is_multiple_of(128), |
| 86 | + "Transpose BitBuffer must be 128-byte aligned" |
| 87 | + ); |
| 88 | + let (offset, len, bytes) = bits.into_inner(); |
| 89 | + match bytes.try_into_mut() { |
| 90 | + Ok(mut bytes_mut) => { |
| 91 | + let (chunks, _) = bytes_mut.as_chunks_mut::<128>(); |
| 92 | + let mut tmp = [0u8; 128]; |
| 93 | + for chunk in chunks { |
| 94 | + untranspose_bits(chunk, &mut tmp); |
| 95 | + chunk.copy_from_slice(&tmp); |
| 96 | + } |
| 97 | + BitBuffer::new_with_offset(bytes_mut.freeze().into_byte_buffer(), len, offset) |
| 98 | + } |
| 99 | + Err(bytes) => bits_op_with_copy(bytes, len, offset, untranspose_bits), |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fn bits_op_with_copy<F: Fn(&[u8; 128], &mut [u8; 128])>( |
| 104 | + bytes: ByteBuffer, |
| 105 | + len: usize, |
| 106 | + offset: usize, |
| 107 | + op: F, |
| 108 | +) -> BitBuffer { |
| 109 | + let output_len = bytes.len().next_multiple_of(128); |
| 110 | + let mut output = ByteBufferMut::with_capacity(output_len); |
| 111 | + let (input_chunks, input_trailer) = bytes.as_chunks::<128>(); |
| 112 | + // We can ignore the spare trailer capacity that can be an artifact of allocator as we requested 128 multiple chunks |
| 113 | + let (output_chunks, _) = output.spare_capacity_mut().as_chunks_mut::<128>(); |
| 114 | + |
| 115 | + for (input, output) in input_chunks.iter().zip(output_chunks.iter_mut()) { |
| 116 | + op(input, unsafe { |
| 117 | + mem::transmute::<&mut [MaybeUninit<u8>; 128], &mut [u8; 128]>(output) |
| 118 | + }); |
| 119 | + } |
| 120 | + |
| 121 | + if !input_trailer.is_empty() { |
| 122 | + let mut padded_input = [0u8; 128]; |
| 123 | + padded_input[0..input_trailer.len()].clone_from_slice(input_trailer); |
| 124 | + op(&padded_input, unsafe { |
| 125 | + mem::transmute::<&mut [MaybeUninit<u8>; 128], &mut [u8; 128]>( |
| 126 | + output_chunks |
| 127 | + .last_mut() |
| 128 | + .vortex_expect("Output wasn't a multiple of 128 bytes"), |
| 129 | + ) |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + unsafe { output.set_len(output_len) }; |
| 134 | + BitBuffer::new_with_offset( |
| 135 | + output.freeze().into_byte_buffer(), |
| 136 | + len.next_multiple_of(1024), |
| 137 | + offset, |
| 138 | + ) |
| 139 | +} |
0 commit comments