Skip to content

Commit f5b732f

Browse files
committed
Take the permitted serialized IDs from the array context
The array context already holds the allowed set as its interning guard. Interner::allowed_ids reads it back, so the writer builds the set once and hands the compressor the same one; a write with editions disabled leaves the compressor unrestricted instead of passing it the whole registry. Signed-off-by: Matt Katz <mhkatz97@gmail.com>
1 parent c27a6d6 commit f5b732f

2 files changed

Lines changed: 31 additions & 30 deletions

File tree

‎vortex-file/src/writer.rs‎

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use futures::pin_mut;
1717
use futures::select;
1818
use itertools::Itertools;
1919
use vortex_array::ArrayContext;
20-
use vortex_array::ArrayId;
2120
use vortex_array::ArrayRef;
2221
use vortex_array::dtype::DType;
2322
use vortex_array::dtype::FieldPath;
@@ -239,24 +238,30 @@ impl VortexWriteOptions {
239238
let enforce_editions = !self.disable_editions;
240239
// The array context is built here, rather than when the options were constructed, so that
241240
// encodings registered on the session in between are still eligible for the file.
242-
let (array_ctx, allowed_serialized_ids) =
243-
new_array_context(&self.session, enforce_editions);
241+
let array_ctx = new_array_context(&self.session, enforce_editions);
242+
let strategy = match self.strategy {
243+
Some(strategy) => strategy,
244+
None => {
245+
// The compressor sees the serialized IDs the context permits: it keeps the schemes
246+
// that can write one of them, and an encoding with several wire formats produces
247+
// the newest one permitted.
248+
let builder = BtrBlocksCompressorBuilder::default();
249+
let builder = match array_ctx.allowed_ids() {
250+
Some(allowed) => builder.allow_serialized_ids(allowed),
251+
None => builder,
252+
};
253+
WriteStrategyBuilder::default()
254+
.with_btrblocks_builder(builder)
255+
.build()
256+
}
257+
};
244258
let ctx = LayoutWriterContext::new(array_ctx)
245259
.with_buffered_bytes_tracker(self.buffered_bytes.clone());
246260
let ctx = if enforce_editions {
247261
ctx.with_allowed_aggregates(edition_filter(&self.session, ComponentKind::Aggregate))
248262
} else {
249263
ctx
250264
};
251-
let strategy = match self.strategy {
252-
Some(strategy) => strategy,
253-
None => WriteStrategyBuilder::default()
254-
.with_btrblocks_builder(
255-
BtrBlocksCompressorBuilder::default()
256-
.allow_serialized_ids(&allowed_serialized_ids),
257-
)
258-
.build(),
259-
};
260265
let dtype = stream.dtype().clone();
261266
if enforce_editions {
262267
validate_dtype_editions(&self.session, &dtype)?;
@@ -384,11 +389,9 @@ impl VortexWriteOptions {
384389
}
385390
}
386391

387-
/// Returns the array context and the serialized IDs the compressor may write its output under.
388-
fn new_array_context(
389-
session: &VortexSession,
390-
enforce_editions: bool,
391-
) -> (ArrayContext, HashSet<ArrayId>) {
392+
/// Returns the array context for a write, restricted to the serialized IDs of the enabled
393+
/// editions when they are enforced.
394+
fn new_array_context(session: &VortexSession, enforce_editions: bool) -> ArrayContext {
392395
// NOTE(os): Set up an array context with all eligible serialized IDs pre-populated.
393396
// This is preferred for now over having an empty context here, because only the
394397
// serialised array order is deterministic. The serialisation of arrays are done
@@ -402,17 +405,13 @@ fn new_array_context(
402405
.registry()
403406
.read(|registry| registry.keys().copied().collect())
404407
};
405-
// The compressor sees the same set: it keeps the schemes that can write one of these IDs, and
406-
// an encoding with several wire formats produces the newest one permitted.
407-
let allowed_serialized_ids: HashSet<ArrayId> = serialized_ids.iter().copied().collect();
408408
let array_ctx = ArrayContext::new(serialized_ids.iter().copied().sorted().collect());
409-
let array_ctx = if enforce_editions {
409+
if enforce_editions {
410410
// Only permit serialized IDs in the enabled editions.
411411
array_ctx.with_allowed_ids(serialized_ids.into_iter().collect())
412412
} else {
413413
array_ctx
414-
};
415-
(array_ctx, allowed_serialized_ids)
414+
}
416415
}
417416

418417
/// The ids of `kind` the enabled editions permit.
@@ -786,10 +785,10 @@ mod tests {
786785
session.register_edition(&DECLARATION)?;
787786
session.enable_edition(EDITION)?;
788787

789-
let (ctx, allowed_serialized_ids) = new_array_context(&session, true);
788+
let ctx = new_array_context(&session, true);
790789
assert_eq!(ctx.to_ids(), [Primitive.id()]);
791790
assert!(ctx.intern(&Bool.id()).is_none());
792-
assert_eq!(allowed_serialized_ids, HashSet::from([Primitive.id()]));
791+
assert_eq!(ctx.allowed_ids(), Some(&HashSet::from([Primitive.id()])));
793792
Ok(())
794793
}
795794

@@ -801,12 +800,9 @@ mod tests {
801800
.registry()
802801
.read(|registry| registry.keys().copied().sorted().collect::<Vec<_>>());
803802

804-
let (ctx, allowed_serialized_ids) = new_array_context(&session, false);
803+
let ctx = new_array_context(&session, false);
805804
assert_eq!(ctx.to_ids(), registered_ids);
806-
assert_eq!(
807-
allowed_serialized_ids,
808-
registered_ids.iter().copied().collect::<HashSet<_>>()
809-
);
805+
assert!(ctx.allowed_ids().is_none());
810806
assert!(ctx.intern(&Bool.id()).is_some());
811807
}
812808

‎vortex-session/src/registry.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ impl Interner {
207207
self
208208
}
209209

210+
/// The permissible set of interned IDs, or `None` when any ID may be interned.
211+
pub fn allowed_ids(&self) -> Option<&HashSet<Id>> {
212+
self.allowed.as_deref()
213+
}
214+
210215
/// Intern an ID, returning its index.
211216
pub fn intern(&self, id: &Id) -> Option<u16> {
212217
if self

0 commit comments

Comments
 (0)