-
Notifications
You must be signed in to change notification settings - Fork 180
feat: add ListLayout
#8071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mhk197
wants to merge
37
commits into
develop
Choose a base branch
from
mk/add-list-layout
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
feat: add ListLayout
#8071
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
2ccea9e
first pass
80ef397
fix
d56512c
second claude pass
ae2bd84
small fixes
b323632
fix
cfb3c40
fix
4f03707
clean mod
mhk197 262c0de
fix writer
mhk197 ac981aa
fix writer
mhk197 3926e49
fix
mhk197 d1b0803
improve projection eval
mhk197 4fc585a
projection evaluation
mhk197 18d5b06
tests
mhk197 2d876a8
tests
mhk197 e020493
fix test
mhk197 874a261
skip pruning eval
mhk197 76a540d
few more tests
mhk197 f250c66
lint fix
mhk197 f74cab2
fix test
mhk197 8604883
quick fix
mhk197 1342c1d
add anylist matcher
mhk197 27eb870
read validity with all-true mask, not caller's mask
mhk197 729334c
narrow elements io for sparse mask instead of defering filtering
mhk197 aa356f8
shortcut on whole-chunk unmasked reads
mhk197 262a5be
add required fallback to ListLayoutStrategy for non-list input
mhk197 850510f
fmt
mhk197 8b34aa9
cleanup
mhk197 3f71c73
fix rebase conflicts
mhk197 cd031a7
use ListLayoutStrategy as default leaf under unstable_encodings
mhk197 29f16cf
recurse into nested lists
mhk197 2133967
fix: update ListLayout::build to use LayoutBuildContext after trait c…
mhk197 020bcb4
comments
mhk197 ab45a9e
fix
mhk197 24fa450
clean up writer
mhk197 3fc6eb6
Improve list reader
mhk197 39059cc
Fix list docs
mhk197 af54f25
Add list filter evaluation
mhk197 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| mod reader; | ||
| pub mod writer; | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use reader::ListReader; | ||
| use vortex_array::DeserializeMetadata; | ||
| use vortex_array::ProstMetadata; | ||
| use vortex_array::dtype::DType; | ||
| use vortex_array::dtype::Nullability; | ||
| use vortex_array::dtype::PType; | ||
| use vortex_error::VortexExpect; | ||
| use vortex_error::VortexResult; | ||
| use vortex_error::vortex_bail; | ||
| use vortex_error::vortex_ensure_eq; | ||
| use vortex_error::vortex_err; | ||
| use vortex_error::vortex_panic; | ||
| use vortex_session::VortexSession; | ||
|
|
||
| use crate::LayoutBuildContext; | ||
| use crate::LayoutChildType; | ||
| use crate::LayoutEncodingRef; | ||
| use crate::LayoutId; | ||
| use crate::LayoutReaderContext; | ||
| use crate::LayoutReaderRef; | ||
| use crate::LayoutRef; | ||
| use crate::VTable; | ||
| use crate::children::LayoutChildren; | ||
| use crate::segments::SegmentId; | ||
| use crate::segments::SegmentSource; | ||
| use crate::vtable; | ||
|
|
||
| /// Child index of the `elements` layout. | ||
| pub const ELEMENTS_CHILD_INDEX: usize = 0; | ||
| /// Child index of the `offsets` layout. | ||
| pub const OFFSETS_CHILD_INDEX: usize = 1; | ||
| /// Child index of the `validity` layout (only present when the list dtype is nullable). | ||
| pub const VALIDITY_CHILD_INDEX: usize = 2; | ||
|
|
||
| /// Number of children when the list dtype is non-nullable. | ||
| pub const NUM_CHILDREN_NON_NULLABLE: usize = 2; | ||
|
|
||
| vtable!(List); | ||
|
|
||
| impl VTable for List { | ||
| type Layout = ListLayout; | ||
| type Encoding = ListLayoutEncoding; | ||
| type Metadata = ProstMetadata<ListLayoutMetadata>; | ||
|
|
||
| fn id(_encoding: &Self::Encoding) -> LayoutId { | ||
| LayoutId::new("vortex.list") | ||
| } | ||
|
|
||
| fn encoding(_layout: &Self::Layout) -> LayoutEncodingRef { | ||
| LayoutEncodingRef::new_ref(ListLayoutEncoding.as_ref()) | ||
| } | ||
|
|
||
| fn row_count(layout: &Self::Layout) -> u64 { | ||
| layout.row_count() | ||
| } | ||
|
|
||
| fn dtype(layout: &Self::Layout) -> &DType { | ||
| &layout.dtype | ||
| } | ||
|
|
||
| fn metadata(layout: &Self::Layout) -> Self::Metadata { | ||
| ProstMetadata(ListLayoutMetadata::new(layout.offsets_ptype())) | ||
| } | ||
|
|
||
| fn segment_ids(_layout: &Self::Layout) -> Vec<SegmentId> { | ||
| vec![] | ||
| } | ||
|
|
||
| fn nchildren(layout: &Self::Layout) -> usize { | ||
| if layout.dtype.is_nullable() { | ||
| NUM_CHILDREN_NON_NULLABLE + 1 | ||
| } else { | ||
| NUM_CHILDREN_NON_NULLABLE | ||
| } | ||
| } | ||
|
|
||
| fn child(layout: &Self::Layout, idx: usize) -> VortexResult<LayoutRef> { | ||
| match (idx, layout.validity.as_ref()) { | ||
| (ELEMENTS_CHILD_INDEX, _) => Ok(Arc::clone(&layout.elements)), | ||
| (OFFSETS_CHILD_INDEX, _) => Ok(Arc::clone(&layout.offsets)), | ||
| (VALIDITY_CHILD_INDEX, Some(validity)) => Ok(Arc::clone(validity)), | ||
| _ => vortex_bail!("Invalid child index {idx} for ListLayout"), | ||
| } | ||
| } | ||
|
|
||
| fn child_type(layout: &Self::Layout, idx: usize) -> LayoutChildType { | ||
| match (idx, layout.validity.is_some()) { | ||
| (ELEMENTS_CHILD_INDEX, _) => LayoutChildType::Auxiliary("elements".into()), | ||
| (OFFSETS_CHILD_INDEX, _) => LayoutChildType::Auxiliary("offsets".into()), | ||
| (VALIDITY_CHILD_INDEX, true) => LayoutChildType::Auxiliary("validity".into()), | ||
| _ => vortex_panic!("Invalid child index {idx} for ListLayout"), | ||
| } | ||
| } | ||
|
|
||
| fn new_reader( | ||
| layout: &Self::Layout, | ||
| name: Arc<str>, | ||
| segment_source: Arc<dyn SegmentSource>, | ||
| session: &VortexSession, | ||
| ctx: &LayoutReaderContext, | ||
| ) -> VortexResult<LayoutReaderRef> { | ||
| Ok(Arc::new(ListReader::try_new( | ||
| layout.clone(), | ||
| name, | ||
| segment_source, | ||
| session.clone(), | ||
| ctx, | ||
| )?)) | ||
| } | ||
|
|
||
| fn build( | ||
| _encoding: &Self::Encoding, | ||
| dtype: &DType, | ||
| _row_count: u64, | ||
| metadata: &<Self::Metadata as DeserializeMetadata>::Output, | ||
| _segment_ids: Vec<SegmentId>, | ||
| children: &dyn LayoutChildren, | ||
| _ctx: &LayoutBuildContext<'_>, | ||
| ) -> VortexResult<Self::Layout> { | ||
| validate_children(dtype, children.nchildren())?; | ||
|
|
||
| let elements_dtype = dtype | ||
| .as_list_element_opt() | ||
| .ok_or_else(|| vortex_err!("ListLayout requires a List dtype, got {dtype}"))?; | ||
| let elements = children.child(ELEMENTS_CHILD_INDEX, elements_dtype.as_ref())?; | ||
|
|
||
| let offsets_dtype = DType::Primitive(metadata.offsets_ptype(), Nullability::NonNullable); | ||
| let offsets = children.child(OFFSETS_CHILD_INDEX, &offsets_dtype)?; | ||
|
|
||
| let validity = dtype | ||
| .is_nullable() | ||
| .then(|| children.child(VALIDITY_CHILD_INDEX, &DType::Bool(Nullability::NonNullable))) | ||
| .transpose()?; | ||
|
|
||
| Ok(ListLayout { | ||
| dtype: dtype.clone(), | ||
| elements, | ||
| offsets, | ||
| validity, | ||
| }) | ||
| } | ||
|
|
||
| fn with_children(layout: &mut Self::Layout, children: Vec<LayoutRef>) -> VortexResult<()> { | ||
| validate_children(layout.dtype(), children.len())?; | ||
|
|
||
| let mut iter = children.into_iter(); | ||
| layout.elements = iter | ||
| .next() | ||
| .ok_or_else(|| vortex_err!("missing elements child"))?; | ||
| layout.offsets = iter | ||
| .next() | ||
| .ok_or_else(|| vortex_err!("missing offsets child"))?; | ||
| layout.validity = layout | ||
| .dtype | ||
| .is_nullable() | ||
| .then(|| { | ||
| iter.next() | ||
| .ok_or_else(|| vortex_err!("missing validity child")) | ||
| }) | ||
| .transpose()?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Validates expected number of children based on `dtype` | ||
| fn validate_children(dtype: &DType, n_children: usize) -> VortexResult<()> { | ||
| let expected = if dtype.is_nullable() { | ||
| NUM_CHILDREN_NON_NULLABLE + 1 | ||
| } else { | ||
| NUM_CHILDREN_NON_NULLABLE | ||
| }; | ||
|
|
||
| vortex_ensure_eq!(n_children, expected); | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct ListLayoutEncoding; | ||
|
|
||
| /// Stores a list-typed array by shredding `elements`, `offsets`, and optional `validity` children. | ||
| #[derive(Clone, Debug)] | ||
| pub struct ListLayout { | ||
| dtype: DType, | ||
| elements: LayoutRef, | ||
| offsets: LayoutRef, | ||
| validity: Option<LayoutRef>, | ||
| } | ||
|
|
||
| impl ListLayout { | ||
| /// Construct a new `ListLayout` from its components. | ||
| /// | ||
| /// # Invariants | ||
| /// | ||
| /// - `dtype` must be a [`DType::List`]. | ||
| /// - `validity` must be `Some` iff `dtype.is_nullable()`. | ||
| /// - `offsets.dtype()` must be a non-nullable integer. | ||
| /// - `offsets.row_count()` is the Arrow-canonical `n+1` for `n` lists (or `0` for empty). | ||
| /// - When present, `validity.row_count() == offsets.row_count().saturating_sub(1)`. | ||
| pub fn new( | ||
| dtype: DType, | ||
| elements: LayoutRef, | ||
| offsets: LayoutRef, | ||
| validity: Option<LayoutRef>, | ||
| ) -> Self { | ||
| Self { | ||
| dtype, | ||
| elements, | ||
| offsets, | ||
| validity, | ||
| } | ||
| } | ||
|
|
||
| /// Number of lists in this layout. | ||
| #[inline] | ||
| pub fn row_count(&self) -> u64 { | ||
| self.offsets.row_count().saturating_sub(1) | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn elements(&self) -> &LayoutRef { | ||
| &self.elements | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn offsets(&self) -> &LayoutRef { | ||
| &self.offsets | ||
| } | ||
|
|
||
| #[inline] | ||
| pub fn validity(&self) -> Option<&LayoutRef> { | ||
| self.validity.as_ref() | ||
| } | ||
|
|
||
| /// The integer type used for the `offsets` child layout. | ||
| #[inline] | ||
| pub fn offsets_ptype(&self) -> PType { | ||
| self.offsets.dtype().as_ptype() | ||
| } | ||
|
|
||
| /// The dtype of the inner elements column. | ||
| pub fn elements_dtype(&self) -> &DType { | ||
| self.dtype | ||
| .as_list_element_opt() | ||
| .vortex_expect("ListLayout dtype must be a List") | ||
| } | ||
| } | ||
|
|
||
| #[derive(prost::Message)] | ||
| pub struct ListLayoutMetadata { | ||
| #[prost(enumeration = "PType", tag = "1")] | ||
| offsets_ptype: i32, | ||
| } | ||
|
|
||
| impl ListLayoutMetadata { | ||
| pub fn new(offsets_ptype: PType) -> Self { | ||
| let mut metadata = Self::default(); | ||
| metadata.set_offsets_ptype(offsets_ptype); | ||
| metadata | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a strange constructor? Why not just do
Self { offsets_ptype }?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
conversion from enum to i32 in prost needs this apparently, see
DictLayout