Skip to content

Commit 4aa8940

Browse files
authored
Move array context inside FlatLayout (#3854)
This makes a bit more sense I believe, and is good preparation for changing the SegmentSource lifetime. Signed-off-by: Nicholas Gates <[email protected]>
1 parent 416b15a commit 4aa8940

File tree

22 files changed

+108
-153
lines changed

22 files changed

+108
-153
lines changed

vortex-file/src/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl VortexFile {
8181
self.footer
8282
.layout()
8383
// TODO(ngates): we may want to allow the user pass in a name here?
84-
.new_reader("".into(), segment_source, self.footer().ctx().clone())
84+
.new_reader("".into(), segment_source)
8585
}
8686

8787
/// Initiate a scan of the file, returning a builder for configuring the scan.

vortex-file/src/footer/mod.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,16 @@ use flatbuffers::root;
2222
use itertools::Itertools;
2323
pub(crate) use postscript::*;
2424
pub use segment::*;
25+
use vortex_array::ArrayRegistry;
2526
use vortex_array::stats::StatsSet;
26-
use vortex_array::{ArrayContext, ArrayRegistry};
2727
use vortex_dtype::DType;
2828
use vortex_error::{VortexResult, vortex_bail, vortex_err};
2929
use vortex_flatbuffers::{FlatBuffer, footer as fb};
30-
use vortex_layout::{LayoutContext, LayoutRef, LayoutRegistry, layout_from_flatbuffer};
30+
use vortex_layout::{LayoutRef, LayoutRegistry, layout_from_flatbuffer};
3131

3232
/// Captures the layout information of a Vortex file.
3333
#[derive(Debug, Clone)]
3434
pub struct Footer {
35-
array_ctx: ArrayContext,
36-
layout_ctx: LayoutContext,
3735
root_layout: LayoutRef,
3836
segments: Arc<[SegmentSpec]>,
3937
statistics: Option<FileStatistics>,
@@ -67,7 +65,7 @@ impl Footer {
6765
.map(|encoding| encoding.id());
6866
let array_ctx = array_registry.new_context(array_ids)?;
6967

70-
let root_layout = layout_from_flatbuffer(layout_bytes, &dtype, &layout_ctx)?;
68+
let root_layout = layout_from_flatbuffer(layout_bytes, &dtype, &layout_ctx, &array_ctx)?;
7169

7270
let segments: Arc<[SegmentSpec]> = fb_footer
7371
.segment_specs()
@@ -82,24 +80,12 @@ impl Footer {
8280
}
8381

8482
Ok(Self {
85-
array_ctx,
86-
layout_ctx,
8783
root_layout,
8884
segments,
8985
statistics,
9086
})
9187
}
9288

93-
/// Returns the array [`ArrayContext`] of the file.
94-
pub fn ctx(&self) -> &ArrayContext {
95-
&self.array_ctx
96-
}
97-
98-
/// Returns the [`LayoutContext`] of the file.
99-
pub fn layout_ctx(&self) -> &LayoutContext {
100-
&self.layout_ctx
101-
}
102-
10389
/// Returns the root [`LayoutRef`] of the file.
10490
pub fn layout(&self) -> &LayoutRef {
10591
&self.root_layout

vortex-layout/src/children.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::sync::Arc;
66

77
use flatbuffers::Follow;
88
use itertools::Itertools;
9+
use vortex_array::ArrayContext;
910
use vortex_dtype::DType;
1011
use vortex_error::{VortexResult, vortex_bail, vortex_err, vortex_panic};
1112
use vortex_flatbuffers::{FlatBuffer, layout as fbl};
@@ -94,7 +95,8 @@ impl LayoutChildren for OwnedLayoutChildren {
9495
pub(crate) struct ViewedLayoutChildren {
9596
flatbuffer: FlatBuffer,
9697
flatbuffer_loc: usize,
97-
ctx: LayoutContext,
98+
array_ctx: ArrayContext,
99+
layout_ctx: LayoutContext,
98100
}
99101

100102
impl ViewedLayoutChildren {
@@ -106,12 +108,14 @@ impl ViewedLayoutChildren {
106108
pub(super) unsafe fn new_unchecked(
107109
flatbuffer: FlatBuffer,
108110
flatbuffer_loc: usize,
109-
ctx: LayoutContext,
111+
array_ctx: ArrayContext,
112+
layout_ctx: LayoutContext,
110113
) -> Self {
111114
Self {
112115
flatbuffer,
113116
flatbuffer_loc,
114-
ctx,
117+
array_ctx,
118+
layout_ctx,
115119
}
116120
}
117121

@@ -135,10 +139,11 @@ impl LayoutChildren for ViewedLayoutChildren {
135139
let viewed_children = ViewedLayoutChildren {
136140
flatbuffer: self.flatbuffer.clone(),
137141
flatbuffer_loc: fb_child._tab.loc(),
138-
ctx: self.ctx.clone(),
142+
array_ctx: self.array_ctx.clone(),
143+
layout_ctx: self.layout_ctx.clone(),
139144
};
140145
let encoding = self
141-
.ctx
146+
.layout_ctx
142147
.lookup_encoding(fb_child.encoding())
143148
.ok_or_else(|| vortex_err!("Encoding not found: {}", fb_child.encoding()))?;
144149

@@ -156,6 +161,7 @@ impl LayoutChildren for ViewedLayoutChildren {
156161
.map(SegmentId::from)
157162
.collect_vec(),
158163
&viewed_children,
164+
self.array_ctx.clone(),
159165
)
160166
}
161167

vortex-layout/src/encoding.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::any::Any;
55
use std::fmt::{Debug, Display, Formatter};
66

77
use arcref::ArcRef;
8-
use vortex_array::DeserializeMetadata;
8+
use vortex_array::{ArrayContext, DeserializeMetadata};
99
use vortex_dtype::DType;
1010
use vortex_error::{VortexExpect, VortexResult, vortex_panic};
1111

@@ -27,6 +27,7 @@ pub trait LayoutEncoding: 'static + Send + Sync + Debug + private::Sealed {
2727
metadata: &[u8],
2828
segment_ids: Vec<SegmentId>,
2929
children: &dyn LayoutChildren,
30+
ctx: ArrayContext,
3031
) -> VortexResult<LayoutRef>;
3132
}
3233

@@ -49,9 +50,18 @@ impl<V: VTable> LayoutEncoding for LayoutEncodingAdapter<V> {
4950
metadata: &[u8],
5051
segment_ids: Vec<SegmentId>,
5152
children: &dyn LayoutChildren,
53+
ctx: ArrayContext,
5254
) -> VortexResult<LayoutRef> {
5355
let metadata = <V::Metadata as DeserializeMetadata>::deserialize(metadata)?;
54-
let layout = V::build(&self.0, dtype, row_count, &metadata, segment_ids, children)?;
56+
let layout = V::build(
57+
&self.0,
58+
dtype,
59+
row_count,
60+
&metadata,
61+
segment_ids,
62+
children,
63+
ctx,
64+
)?;
5565

5666
// Validate that the builder function returned the expected values.
5767
if layout.row_count() != row_count {

vortex-layout/src/flatbuffers.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::env;
55
use std::sync::LazyLock;
66

77
use flatbuffers::{FlatBufferBuilder, VerifierOptions, WIPOffset, root_with_opts};
8+
use vortex_array::ArrayContext;
89
use vortex_dtype::DType;
910
use vortex_error::{VortexExpect, VortexResult, vortex_err};
1011
use vortex_flatbuffers::{FlatBuffer, FlatBufferRoot, WriteFlatBuffer, layout};
@@ -34,16 +35,22 @@ static LAYOUT_VERIFIER: LazyLock<VerifierOptions> = LazyLock::new(|| {
3435
pub fn layout_from_flatbuffer(
3536
flatbuffer: FlatBuffer,
3637
dtype: &DType,
37-
ctx: &LayoutContext,
38+
layout_ctx: &LayoutContext,
39+
array_ctx: &ArrayContext,
3840
) -> VortexResult<LayoutRef> {
3941
let fb_layout = root_with_opts::<layout::Layout>(&LAYOUT_VERIFIER, &flatbuffer)?;
40-
let encoding = ctx
42+
let encoding = layout_ctx
4143
.lookup_encoding(fb_layout.encoding())
4244
.ok_or_else(|| vortex_err!("Invalid encoding ID: {}", fb_layout.encoding()))?;
4345

4446
// SAFETY: we validate the flatbuffer above in the `root` call, and extract a loc.
4547
let viewed_children = unsafe {
46-
ViewedLayoutChildren::new_unchecked(flatbuffer.clone(), fb_layout._tab.loc(), ctx.clone())
48+
ViewedLayoutChildren::new_unchecked(
49+
flatbuffer.clone(),
50+
fb_layout._tab.loc(),
51+
array_ctx.clone(),
52+
layout_ctx.clone(),
53+
)
4754
};
4855

4956
let layout = encoding.build(
@@ -60,6 +67,7 @@ pub fn layout_from_flatbuffer(
6067
.map(SegmentId::from)
6168
.collect(),
6269
&viewed_children,
70+
array_ctx.clone(),
6371
)?;
6472

6573
Ok(layout)

vortex-layout/src/layout.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::sync::Arc;
77

88
use arcref::ArcRef;
99
use itertools::Itertools;
10-
use vortex_array::{ArrayContext, SerializeMetadata};
10+
use vortex_array::SerializeMetadata;
1111
use vortex_dtype::{DType, FieldName};
1212
use vortex_error::{VortexExpect, VortexResult, vortex_err};
1313

@@ -54,7 +54,6 @@ pub trait Layout: 'static + Send + Sync + Debug + private::Sealed {
5454
&self,
5555
name: Arc<str>,
5656
segment_source: Arc<dyn SegmentSource>,
57-
ctx: ArrayContext,
5857
) -> VortexResult<LayoutReaderRef>;
5958
}
6059

@@ -248,9 +247,8 @@ impl<V: VTable> Layout for LayoutAdapter<V> {
248247
&self,
249248
name: Arc<str>,
250249
segment_source: Arc<dyn SegmentSource>,
251-
ctx: ArrayContext,
252250
) -> VortexResult<LayoutReaderRef> {
253-
V::new_reader(&self.0, name, segment_source, ctx)
251+
V::new_reader(&self.0, name, segment_source)
254252
}
255253
}
256254

vortex-layout/src/layouts/chunked/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,11 @@ impl VTable for ChunkedVTable {
6464
layout: &Self::Layout,
6565
name: Arc<str>,
6666
segment_source: Arc<dyn SegmentSource>,
67-
ctx: ArrayContext,
6867
) -> VortexResult<LayoutReaderRef> {
6968
Ok(Arc::new(ChunkedReader::new(
7069
layout.clone(),
7170
name,
7271
segment_source,
73-
ctx,
7472
)))
7573
}
7674

@@ -81,6 +79,7 @@ impl VTable for ChunkedVTable {
8179
_metadata: &<Self::Metadata as DeserializeMetadata>::Output,
8280
_segment_ids: Vec<SegmentId>,
8381
children: &dyn LayoutChildren,
82+
_ctx: ArrayContext,
8483
) -> VortexResult<Self::Layout> {
8584
Ok(ChunkedLayout::new(
8685
row_count,

vortex-layout/src/layouts/chunked/reader.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ use futures::future::ready;
1010
use futures::stream::FuturesOrdered;
1111
use futures::{FutureExt, TryStreamExt};
1212
use itertools::Itertools;
13+
use vortex_array::ArrayRef;
1314
use vortex_array::arrays::ChunkedArray;
1415
use vortex_array::stats::Precision;
15-
use vortex_array::{ArrayContext, ArrayRef};
1616
use vortex_dtype::{DType, FieldMask};
1717
use vortex_error::{VortexExpect, VortexResult};
1818
use vortex_expr::ExprRef;
@@ -39,7 +39,6 @@ impl ChunkedReader {
3939
layout: ChunkedLayout,
4040
name: Arc<str>,
4141
segment_source: Arc<dyn SegmentSource>,
42-
ctx: ArrayContext,
4342
) -> Self {
4443
let nchildren = layout.nchildren();
4544

@@ -49,7 +48,7 @@ impl ChunkedReader {
4948
}
5049
chunk_offsets.push(layout.row_count());
5150

52-
let lazy_children = LazyReaderChildren::new(layout.children.clone(), segment_source, ctx);
51+
let lazy_children = LazyReaderChildren::new(layout.children.clone(), segment_source);
5352

5453
Self {
5554
layout,
@@ -351,7 +350,7 @@ mod test {
351350

352351
#[fixture]
353352
/// Create a chunked layout with three chunks of primitive arrays.
354-
fn chunked_layout() -> (ArrayContext, Arc<dyn SegmentSource>, LayoutRef) {
353+
fn chunked_layout() -> (Arc<dyn SegmentSource>, LayoutRef) {
355354
let ctx = ArrayContext::empty();
356355
let segments = TestSegments::default();
357356
let sequence_writer = SequenceWriter::new(Box::new(segments.clone()));
@@ -374,20 +373,16 @@ mod test {
374373
)
375374
.unwrap();
376375

377-
(ctx, Arc::new(segments), layout)
376+
(Arc::new(segments), layout)
378377
}
379378

380379
#[rstest]
381380
fn test_chunked_evaluator(
382-
#[from(chunked_layout)] (ctx, segments, layout): (
383-
ArrayContext,
384-
Arc<dyn SegmentSource>,
385-
LayoutRef,
386-
),
381+
#[from(chunked_layout)] (segments, layout): (Arc<dyn SegmentSource>, LayoutRef),
387382
) {
388383
block_on(async {
389384
let result = layout
390-
.new_reader("".into(), segments, ctx)
385+
.new_reader("".into(), segments)
391386
.unwrap()
392387
.projection_evaluation(&(0..layout.row_count()), &root())
393388
.unwrap()

vortex-layout/src/layouts/dict/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,11 @@ impl VTable for DictVTable {
7474
layout: &Self::Layout,
7575
name: Arc<str>,
7676
segment_source: Arc<dyn SegmentSource>,
77-
ctx: ArrayContext,
7877
) -> VortexResult<LayoutReaderRef> {
7978
Ok(Arc::new(DictReader::try_new(
8079
layout.clone(),
8180
name,
8281
segment_source,
83-
ctx,
8482
)?))
8583
}
8684

@@ -91,6 +89,7 @@ impl VTable for DictVTable {
9189
metadata: &<Self::Metadata as DeserializeMetadata>::Output,
9290
_segment_ids: Vec<SegmentId>,
9391
children: &dyn LayoutChildren,
92+
_ctx: ArrayContext,
9493
) -> VortexResult<Self::Layout> {
9594
let values = children.child(0, dtype)?;
9695
let codes = children.child(

vortex-layout/src/layouts/dict/reader.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use std::sync::{Arc, OnceLock};
88
use async_trait::async_trait;
99
use dashmap::DashMap;
1010
use futures::{FutureExt, join};
11+
use vortex_array::ArrayRef;
1112
use vortex_array::compute::{MinMaxResult, filter, min_max};
1213
use vortex_array::stats::Precision;
13-
use vortex_array::{ArrayContext, ArrayRef};
1414
use vortex_dict::DictArray;
1515
use vortex_dtype::{DType, FieldMask};
1616
use vortex_error::{VortexExpect, VortexResult};
@@ -46,17 +46,14 @@ impl DictReader {
4646
layout: DictLayout,
4747
name: Arc<str>,
4848
segment_source: Arc<dyn SegmentSource>,
49-
ctx: ArrayContext,
5049
) -> VortexResult<Self> {
5150
let values_len = usize::try_from(layout.values.row_count())?;
52-
let values = layout.values.new_reader(
53-
format!("{name}.values").into(),
54-
segment_source.clone(),
55-
ctx.clone(),
56-
)?;
51+
let values = layout
52+
.values
53+
.new_reader(format!("{name}.values").into(), segment_source.clone())?;
5754
let codes = layout
5855
.codes
59-
.new_reader(format!("{name}.codes").into(), segment_source, ctx)?;
56+
.new_reader(format!("{name}.codes").into(), segment_source)?;
6057

6158
Ok(Self {
6259
layout,
@@ -315,7 +312,7 @@ mod tests {
315312
);
316313
assert!(layout.encoding_id() == LayoutId::new_ref("vortex.dict"));
317314
let actual = layout
318-
.new_reader("".into(), Arc::from(segments), ctx)
315+
.new_reader("".into(), Arc::from(segments))
319316
.unwrap()
320317
.projection_evaluation(&(0..layout.row_count()), &expression)
321318
.unwrap()
@@ -391,7 +388,7 @@ mod tests {
391388
let expression = not(is_null(root())); // easier to test not_is_null b/c that's the validity array
392389
assert!(layout.encoding_id() == LayoutId::new_ref("vortex.dict"));
393390
let actual = layout
394-
.new_reader("".into(), Arc::from(segments), ctx)
391+
.new_reader("".into(), Arc::from(segments))
395392
.unwrap()
396393
.projection_evaluation(&(0..layout.row_count()), &expression)
397394
.unwrap()

0 commit comments

Comments
 (0)