-
Notifications
You must be signed in to change notification settings - Fork 197
feat: add dense union encoding #9367
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
Open
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| [package] | ||
| name = "vortex-dense-union" | ||
| authors = { workspace = true } | ||
| categories = { workspace = true } | ||
| description = "Dense union encoding for Vortex arrays" | ||
| edition = { workspace = true } | ||
| homepage = { workspace = true } | ||
| include = { workspace = true } | ||
| keywords = { workspace = true } | ||
| license = { workspace = true } | ||
| readme = "README.md" | ||
| repository = { workspace = true } | ||
| rust-version = { workspace = true } | ||
| version = { workspace = true } | ||
|
|
||
| [dependencies] | ||
| prost = { workspace = true } | ||
| vortex-array = { workspace = true } | ||
| vortex-error = { workspace = true } | ||
| vortex-mask = { workspace = true } | ||
| vortex-session = { workspace = true } | ||
|
|
||
| [dev-dependencies] | ||
| divan = { workspace = true } | ||
| vortex-array = { workspace = true, features = ["_test-harness"] } | ||
| vortex-buffer = { workspace = true } | ||
|
|
||
| [lints] | ||
| workspace = true | ||
|
|
||
| [[bench]] | ||
| name = "take" | ||
| harness = false |
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,3 @@ | ||
| # Vortex Dense Union | ||
|
|
||
| An external dense physical encoding for Vortex's logical `DType::Union`. |
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,118 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| #![expect(clippy::unwrap_used)] | ||
| #![expect(clippy::cast_possible_truncation)] | ||
|
|
||
| use std::sync::LazyLock; | ||
|
|
||
| use divan::Bencher; | ||
| use vortex_array::ArrayRef; | ||
| use vortex_array::IntoArray; | ||
| use vortex_array::RecursiveCanonical; | ||
| use vortex_array::VortexSessionExecute; | ||
| use vortex_array::array_session; | ||
| use vortex_array::arrays::PrimitiveArray; | ||
| use vortex_array::arrays::UnionArray; | ||
| use vortex_array::dtype::DType; | ||
| use vortex_array::dtype::FieldNames; | ||
| use vortex_array::dtype::Nullability; | ||
| use vortex_array::dtype::PType; | ||
| use vortex_array::dtype::UnionVariants; | ||
| use vortex_dense_union::DenseUnion; | ||
| use vortex_dense_union::initialize; | ||
| use vortex_session::VortexSession; | ||
|
|
||
| const LEN: usize = 65_536; | ||
| const N_VARIANTS: usize = 28; | ||
| const TAKE_LEN: usize = 4_096; | ||
|
|
||
| fn main() { | ||
| LazyLock::force(&SESSION); | ||
| divan::main(); | ||
| } | ||
|
|
||
| static SESSION: LazyLock<VortexSession> = LazyLock::new(|| { | ||
| let session = array_session(); | ||
| initialize(&session); | ||
| session | ||
| }); | ||
|
|
||
| fn variants() -> UnionVariants { | ||
| let names = FieldNames::from_iter((0..N_VARIANTS).map(|index| format!("variant_{index}"))); | ||
| let dtypes = vec![DType::Primitive(PType::I32, Nullability::NonNullable); N_VARIANTS]; | ||
| let type_ids = (1..=N_VARIANTS).map(|type_id| type_id as u8).collect(); | ||
| UnionVariants::try_new(names, dtypes, type_ids).unwrap() | ||
| } | ||
|
|
||
| fn selectors() -> (ArrayRef, ArrayRef, Vec<usize>) { | ||
| let mut child_lengths = vec![0usize; N_VARIANTS]; | ||
| let mut type_ids = Vec::with_capacity(LEN); | ||
| let mut offsets = Vec::with_capacity(LEN); | ||
| for row in 0..LEN { | ||
| let child_index = row % N_VARIANTS; | ||
| type_ids.push((child_index + 1) as u8); | ||
| offsets.push(child_lengths[child_index] as i32); | ||
| child_lengths[child_index] += 1; | ||
| } | ||
| ( | ||
| PrimitiveArray::from_iter(type_ids).into_array(), | ||
| PrimitiveArray::from_iter(offsets).into_array(), | ||
| child_lengths, | ||
| ) | ||
| } | ||
|
|
||
| fn dense_union() -> ArrayRef { | ||
| let (type_ids, offsets, child_lengths) = selectors(); | ||
| let children = child_lengths | ||
| .into_iter() | ||
| .map(|len| PrimitiveArray::from_iter(0..len as i32).into_array()) | ||
| .collect::<Vec<_>>(); | ||
| DenseUnion::try_new(type_ids, offsets, variants(), children) | ||
| .unwrap() | ||
| .into_array() | ||
| } | ||
|
|
||
| fn sparse_union() -> ArrayRef { | ||
| let (type_ids, ..) = selectors(); | ||
| let children = (0..N_VARIANTS) | ||
| .map(|child_index| { | ||
| PrimitiveArray::from_iter((0..LEN).map(move |row| { | ||
| if row % N_VARIANTS == child_index { | ||
| (row / N_VARIANTS) as i32 | ||
| } else { | ||
| 0 | ||
| } | ||
| })) | ||
| .into_array() | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
| UnionArray::try_new(type_ids, variants(), children) | ||
| .unwrap() | ||
| .into_array() | ||
| } | ||
|
|
||
| fn indices() -> ArrayRef { | ||
| PrimitiveArray::from_iter((0..TAKE_LEN).rev().map(|index| index as u32)).into_array() | ||
| } | ||
|
|
||
| fn bench_take(bencher: Bencher, array: ArrayRef, indices: ArrayRef) { | ||
| bencher | ||
| .with_inputs(|| (&array, &indices, SESSION.create_execution_ctx())) | ||
| .bench_refs(|(array, indices, ctx)| { | ||
| array | ||
| .take((*indices).clone()) | ||
| .unwrap() | ||
| .execute::<RecursiveCanonical>(ctx) | ||
| }); | ||
| } | ||
|
|
||
| #[divan::bench] | ||
| fn dense_take(bencher: Bencher) { | ||
| bench_take(bencher, dense_union(), indices()); | ||
| } | ||
|
|
||
| #[divan::bench] | ||
| fn sparse_take(bencher: Bencher) { | ||
| bench_take(bencher, sparse_union(), indices()); | ||
| } | ||
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.
You need to execute the array here otherwise there is no work that is done
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.
Ah, that's correct, for now it is lazy-take. Fixed.
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.
im interested in what the new benchmark results are?
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.
Already showed in the PR content. DenseUnion is 2.10-2.12x slower.