Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions vortex-file/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use vortex_array::expr::Expression;
use vortex_buffer::ByteBuffer;
use vortex_error::VortexResult;
use vortex_layout::LayoutReader;
use vortex_layout::layouts::list::List;
use vortex_layout::scan::layout::LayoutReaderDataSource;
use vortex_layout::scan::metrics::ScanUsage;
use vortex_layout::scan::scan_builder::ScanBuilder;
Expand Down Expand Up @@ -141,10 +142,10 @@ impl VortexFile {

/// Enable layout reader and V2 scan caching.
///
/// Repeated calls to [`layout_reader`](Self::layout_reader), [`scan`](Self::scan), and
/// [`data_source`](Self::data_source) will share the same reader tree. Repeated V2 scans of
/// [`scan_file`](Self::scan_file), and so of the plan data sources, will share its segment
/// locations and a [`ScanCache`].
/// Repeated calls to [`layout_reader`](Self::layout_reader) and [`scan`](Self::scan), and to
/// [`data_source`](Self::data_source) for a file it scans with the layout reader, will share the
/// same reader tree. Repeated V2 scans of [`scan_file`](Self::scan_file), and so of the plan
/// data sources, will share its segment locations and a [`ScanCache`].
pub fn with_caching(self) -> Self {
let scan_cache = FileScanCache {
locations: self.footer.segment_locations(),
Expand Down Expand Up @@ -273,10 +274,16 @@ impl VortexFile {

/// Create a [`DataSource`](vortex_scan::DataSource) from this file for scanning.
///
/// Scans run on the file's [layout reader](Self::layout_reader), as with
/// [`layout_reader_data_source`](Self::layout_reader_data_source). Stored footer aggregates
/// Scans run on the planner-driven executor, as with
/// [`plan_data_source`](Self::plan_data_source), unless the file has a layout that
/// executor cannot read yet. Such files are scanned on the layout reader instead, as with
/// [`layout_reader_data_source`](Self::layout_reader_data_source). Either way, a scan whose
/// filter the file-level statistics prove false reads nothing, and stored footer aggregates
/// are available through the source statistics methods without reading payloads.
pub fn data_source(&self) -> VortexResult<DataSourceRef> {
if self.planned_scan_supported()? {
return Ok(self.plan_data_source());
}
self.layout_reader_data_source()
}

Expand All @@ -294,6 +301,24 @@ impl VortexFile {
)))
}

/// Whether the planner-driven executor can scan this file, which
/// [`data_source`](Self::data_source) uses to choose its executor.
///
/// The planner-driven executor cannot read list layouts yet (tracked with making it the
/// default in #11502), so files that contain one are scanned by the layout reader instead. The
/// layout tree is only walked when the file's dtype could hold a list layout.
pub(crate) fn planned_scan_supported(&self) -> VortexResult<bool> {
if !may_contain_list_layout(self.dtype()) {
return Ok(true);
}
for layout in self.footer.layout().depth_first_traversal() {
if layout?.is::<List>() {
return Ok(false);
}
}
Ok(true)
}

/// Create a [`DataSource`](vortex_scan::DataSource) from this file whose scans run on the
/// planner-driven executor.
///
Expand Down
122 changes: 122 additions & 0 deletions vortex-file/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ use vortex_layout::layouts::buffered::BufferedStrategy;
use vortex_layout::layouts::chunked::writer::ChunkedLayoutStrategy;
use vortex_layout::layouts::file_stats::AggregateStats;
use vortex_layout::layouts::flat::writer::FlatLayoutStrategy;
use vortex_layout::layouts::list::List;
use vortex_layout::layouts::struct_::StructStrategy;
use vortex_layout::layouts::table::TableStrategy;
use vortex_layout::layouts::zoned::LegacyStats;
Expand Down Expand Up @@ -2657,6 +2658,127 @@ async fn file_sources_scan_with_either_executor(#[case] layout_reader: bool) ->
Ok(())
}

/// A file whose statistics rule out a filter is scanned with no partitions, which the layout
/// reader's source never returns, so this also shows the file is scanned on the planner-driven
/// executor.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[cfg_attr(miri, ignore)]
async fn data_source_prunes_files_with_statistics() -> VortexResult<()> {
let array = StructArray::from_fields(&[("numbers", buffer![1u32, 2, 3, 4, 5].into_array())])?
.into_array();
let mut buf = ByteBufferMut::empty();
SESSION
.write_options()
.with_file_statistics(pruning_aggregate_fns())
.write(&mut buf, array.to_array_stream())
.await?;
let file = SESSION.open_options().open_buffer(buf)?;
assert!(file.planned_scan_supported()?);
let source = file.data_source()?;
let request = |filter: Expression| ScanRequest {
projection: get_item("numbers", root()),
filter: Some(filter),
ordered: true,
..Default::default()
};

let pruned = source
.scan(request(gt(get_item("numbers", root()), lit(10u32))))
.await?;
assert_eq!(
pruned.dtype(),
&DType::Primitive(PType::U32, Nullability::NonNullable)
);
assert_eq!(pruned.partition_count(), Precision::exact(0usize));
assert_eq!(pruned.partitions().try_collect::<Vec<_>>().await?.len(), 0);

let (partitions, actual) =
scan_source(&source, request(gt(get_item("numbers", root()), lit(3u32)))).await?;
assert_eq!(partitions, 1);
assert_arrays_eq!(
actual,
buffer![4u32, 5].into_array(),
&mut SESSION.create_execution_ctx()
);

// A filter that does not bind is not pruned away: the scan reports the error.
assert!(
source
.scan(request(gt(get_item("missing", root()), lit(10u32))))
.await
.is_err()
);
Ok(())
}

/// A file with a list layout, which the planner-driven executor cannot read yet, is scanned with
/// the layout reader instead, list column included.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[cfg_attr(miri, ignore)]
async fn data_source_scans_list_layouts_with_the_layout_reader() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let lists = ListArray::try_new(
buffer![1i32, 2, 3, 4, 5, 6].into_array(),
buffer![0u32, 2, 5, 5, 6].into_array(),
Validity::NonNullable,
&mut ctx,
)?
.into_array();
let array = StructArray::from_fields(&[
("numbers", buffer![1i32, 2, 3, 4].into_array()),
("lists", lists),
])?
.into_array();
let mut buf = ByteBufferMut::empty();
SESSION
.write_options()
.write(&mut buf, array.to_array_stream())
.await?;
let file = SESSION.open_options().open_buffer(buf)?;
let mut has_list_layout = false;
for layout in file.footer().layout().depth_first_traversal() {
has_list_layout |= layout?.is::<List>();
}
assert!(has_list_layout);
assert!(!file.planned_scan_supported()?);

let planned = scan_source(&file.plan_data_source(), ScanRequest::default()).await;
let error = planned.expect_err("the planner-driven executor rejects list layouts");
assert!(error.to_string().contains("vortex.plan.list_pack"));

let (_, actual) = scan_source(&file.data_source()?, ScanRequest::default()).await?;
assert_arrays_eq!(actual, array, &mut ctx);
Ok(())
}

/// A file with list columns but no list layout is scanned on the planner-driven executor.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
#[cfg_attr(miri, ignore)]
async fn data_source_scans_flat_lists_with_the_planner() -> VortexResult<()> {
let mut ctx = SESSION.create_execution_ctx();
let lists = ListArray::try_new(
buffer![1i32, 2, 3, 4, 5, 6].into_array(),
buffer![0u32, 2, 5, 5, 6].into_array(),
Validity::NonNullable,
&mut ctx,
)?
.into_array();
let array = StructArray::from_fields(&[("lists", lists)])?.into_array();
let mut buf = ByteBufferMut::empty();
SESSION
.write_options()
.with_strategy(Arc::new(FlatLayoutStrategy::default()))
.write(&mut buf, array.to_array_stream())
.await?;
let file = SESSION.open_options().open_buffer(buf)?;
assert!(file.planned_scan_supported()?);

let (partitions, actual) = scan_source(&file.data_source()?, ScanRequest::default()).await?;
assert_eq!(partitions, 1);
assert_arrays_eq!(actual, array, &mut ctx);
Ok(())
}

#[tokio::test]
async fn test_writer_with_statistics() -> VortexResult<()> {
let array = StructArray::from_fields(&[("numbers", buffer![1u32, 2, 3, 4, 5].into_array())])?
Expand Down
Loading