Skip to content

Commit b5a4215

Browse files
authored
Move bool array over to ProstMetadata (#3052)
Part of #3051
1 parent 7eade9d commit b5a4215

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vortex-array/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ num_enum = { workspace = true }
4242
parking_lot = { workspace = true }
4343
paste = { workspace = true }
4444
pin-project = { workspace = true }
45+
prost = { workspace = true }
46+
prost-types = { workspace = true }
4547
rand = { workspace = true }
4648
rkyv = { workspace = true }
4749
rstest = { workspace = true, optional = true }
@@ -54,6 +56,7 @@ vortex-dtype = { workspace = true, features = ["arrow", "rkyv", "serde"] }
5456
vortex-error = { workspace = true, features = [
5557
"flatbuffers",
5658
"flexbuffers",
59+
"prost",
5760
"rancor",
5861
] }
5962
vortex-flatbuffers = { workspace = true, features = ["array"] }

vortex-array/src/arrays/bool/array.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::stats::{ArrayStats, StatsSetRef};
1212
use crate::validity::Validity;
1313
use crate::variants::BoolArrayTrait;
1414
use crate::vtable::VTableRef;
15-
use crate::{ArrayImpl, ArrayRef, ArrayStatisticsImpl, Canonical, Encoding, RkyvMetadata};
15+
use crate::{ArrayImpl, ArrayRef, ArrayStatisticsImpl, Canonical, Encoding, ProstMetadata};
1616

1717
#[derive(Clone, Debug)]
1818
pub struct BoolArray {
@@ -26,7 +26,7 @@ pub struct BoolArray {
2626
pub struct BoolEncoding;
2727
impl Encoding for BoolEncoding {
2828
type Array = BoolArray;
29-
type Metadata = RkyvMetadata<BoolMetadata>;
29+
type Metadata = ProstMetadata<BoolMetadata>;
3030
}
3131

3232
impl BoolArray {

vortex-array/src/arrays/bool/serde.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,16 @@ use crate::validity::Validity;
99
use crate::vtable::EncodingVTable;
1010
use crate::{
1111
Array, ArrayBufferVisitor, ArrayChildVisitor, ArrayContext, ArrayRef, ArrayVisitorImpl,
12-
DeserializeMetadata, EncodingId, RkyvMetadata,
12+
DeserializeMetadata, Encoding, EncodingId, ProstMetadata,
1313
};
1414

15+
#[derive(prost::Message)]
16+
pub struct BoolMetadata {
17+
// The offset in bits must be <8
18+
#[prost(uint32, tag = "1")]
19+
pub offset: u32,
20+
}
21+
1522
impl EncodingVTable for BoolEncoding {
1623
fn id(&self) -> EncodingId {
1724
EncodingId::new_ref("vortex.bool")
@@ -24,7 +31,7 @@ impl EncodingVTable for BoolEncoding {
2431
dtype: DType,
2532
len: usize,
2633
) -> VortexResult<ArrayRef> {
27-
let metadata = RkyvMetadata::<BoolMetadata>::deserialize(parts.metadata())?;
34+
let metadata = <Self as Encoding>::Metadata::deserialize(parts.metadata())?;
2835

2936
if parts.nbuffers() != 1 {
3037
vortex_bail!("Expected 1 buffer, got {}", parts.nbuffers());
@@ -48,13 +55,7 @@ impl EncodingVTable for BoolEncoding {
4855
}
4956
}
5057

51-
#[derive(Debug, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
52-
pub struct BoolMetadata {
53-
// We know the offset in bits must be <8
54-
offset: u8,
55-
}
56-
57-
impl ArrayVisitorImpl<RkyvMetadata<BoolMetadata>> for BoolArray {
58+
impl ArrayVisitorImpl<ProstMetadata<BoolMetadata>> for BoolArray {
5859
fn _visit_buffers(&self, visitor: &mut dyn ArrayBufferVisitor) {
5960
visitor.visit_buffer(&ByteBuffer::from_arrow_buffer(
6061
self.boolean_buffer().clone().into_inner(),
@@ -66,11 +67,11 @@ impl ArrayVisitorImpl<RkyvMetadata<BoolMetadata>> for BoolArray {
6667
visitor.visit_validity(&self.validity, self.len());
6768
}
6869

69-
fn _metadata(&self) -> RkyvMetadata<BoolMetadata> {
70+
fn _metadata(&self) -> ProstMetadata<BoolMetadata> {
7071
let bit_offset = self.boolean_buffer().offset();
7172
assert!(bit_offset < 8, "Offset must be <8, got {}", bit_offset);
72-
RkyvMetadata(BoolMetadata {
73-
offset: u8::try_from(bit_offset).vortex_expect("checked"),
73+
ProstMetadata(BoolMetadata {
74+
offset: u32::try_from(bit_offset).vortex_expect("checked"),
7475
})
7576
}
7677
}

vortex-array/src/metadata.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,46 @@ where
132132
}
133133
}
134134

135+
/// A utility wrapper for Prost metadata serialization.
136+
pub struct ProstMetadata<M>(pub M);
137+
138+
impl<M: Debug> Debug for ProstMetadata<M> {
139+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
140+
self.0.fmt(f)
141+
}
142+
}
143+
144+
impl<M> SerializeMetadata for ProstMetadata<M>
145+
where
146+
M: prost::Message,
147+
{
148+
fn serialize(&self) -> Option<Vec<u8>> {
149+
Some(self.0.encode_to_vec())
150+
}
151+
}
152+
153+
impl<M> DeserializeMetadata for ProstMetadata<M>
154+
where
155+
M: Debug,
156+
M: prost::Message + Default,
157+
{
158+
type Output = M;
159+
160+
fn deserialize(metadata: Option<&[u8]>) -> VortexResult<Self::Output> {
161+
let bytes =
162+
metadata.ok_or_else(|| vortex_err!("Prost metadata requires metadata bytes"))?;
163+
Ok(M::decode(bytes)?)
164+
}
165+
166+
#[allow(clippy::use_debug)]
167+
fn format(metadata: Option<&[u8]>, f: &mut Formatter<'_>) -> std::fmt::Result {
168+
match Self::deserialize(metadata) {
169+
Ok(m) => write!(f, "{:?}", m),
170+
Err(_) => write!(f, "Failed to deserialize metadata"),
171+
}
172+
}
173+
}
174+
135175
/// A utility wrapper for automating the serialization of metadata using [serde](docs.rs/serde) into [flexbuffers](https://docs.rs/flexbuffers/latest/flexbuffers/).
136176
pub struct SerdeMetadata<M>(pub M);
137177

0 commit comments

Comments
 (0)