-
Notifications
You must be signed in to change notification settings - Fork 149
Late materialization support for duckdb #7631
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
Merged
Merged
Changes from all commits
Commits
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
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
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 |
|---|---|---|
| @@ -1,14 +1,17 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // SPDX-FileCopyrightText: Copyright the Vortex contributors | ||
|
|
||
| use std::ops::Range; | ||
| use std::sync::Arc; | ||
|
|
||
| use itertools::Itertools; | ||
| use vortex::buffer::Buffer; | ||
| use vortex::dtype::DType; | ||
| use vortex::dtype::Nullability; | ||
| use vortex::error::VortexExpect; | ||
| use vortex::error::VortexResult; | ||
| use vortex::error::vortex_bail; | ||
| use vortex::error::vortex_err; | ||
| use vortex::expr::Expression; | ||
| use vortex::expr::and_collect; | ||
| use vortex::expr::get_item; | ||
|
|
@@ -21,10 +24,13 @@ use vortex::scalar::Scalar; | |
| use vortex::scalar_fn::ScalarFnVTableExt; | ||
| use vortex::scalar_fn::fns::binary::Binary; | ||
| use vortex::scalar_fn::fns::operators::CompareOperator; | ||
| use vortex::scan::selection::Selection; | ||
|
|
||
| use crate::cpp::DUCKDB_VX_EXPR_TYPE; | ||
| use crate::duckdb::ExtractedValue; | ||
| use crate::duckdb::TableFilterClass; | ||
| use crate::duckdb::TableFilterRef; | ||
| use crate::duckdb::ValueRef; | ||
|
|
||
| pub fn try_from_table_filter( | ||
| value: &TableFilterRef, | ||
|
|
@@ -125,3 +131,96 @@ pub fn try_from_table_filter( | |
| } | ||
| })) | ||
| } | ||
|
|
||
| fn nonnegative_number_from_value(value: &ValueRef) -> VortexResult<u64> { | ||
| match value.extract() { | ||
| ExtractedValue::BigInt(i) => { | ||
| u64::try_from(i).map_err(|_| vortex_err!("negative value: {i}")) | ||
| } | ||
| ExtractedValue::Integer(i) => { | ||
| u64::try_from(i).map_err(|_| vortex_err!("negative value: {i}")) | ||
| } | ||
| ExtractedValue::UBigInt(u) => Ok(u), | ||
| ExtractedValue::UInteger(u) => Ok(u64::from(u)), | ||
| _ => vortex_bail!("unexpected value type"), | ||
| } | ||
| } | ||
|
|
||
| fn intersect_sorted(left: &[u64], right: &[u64]) -> Vec<u64> { | ||
| let mut result = Vec::new(); | ||
| let (mut i, mut j) = (0, 0); | ||
| while i < left.len() && j < right.len() { | ||
| match left[i].cmp(&right[j]) { | ||
| std::cmp::Ordering::Equal => { | ||
| result.push(left[i]); | ||
| i += 1; | ||
| j += 1; | ||
| } | ||
| std::cmp::Ordering::Less => i += 1, | ||
| std::cmp::Ordering::Greater => j += 1, | ||
| } | ||
| } | ||
| result | ||
| } | ||
|
|
||
| /// For constant comparison on IN filters over file_index or file_row_number | ||
| /// virtual column, create a selection and a range covering the same range as | ||
| /// expressions do. | ||
| pub fn try_from_virtual_column_filter( | ||
|
myrrc marked this conversation as resolved.
|
||
| filter: &TableFilterRef, | ||
| ) -> VortexResult<(Selection, Option<Range<u64>>)> { | ||
| match filter.as_class() { | ||
| TableFilterClass::InFilter(values) => { | ||
| let indices = values | ||
| .iter() | ||
| .map(nonnegative_number_from_value) | ||
| .collect::<VortexResult<Vec<u64>>>()?; | ||
| Ok((Selection::IncludeByIndex(Buffer::from_iter(indices)), None)) | ||
| } | ||
| TableFilterClass::ConstantComparison(const_) => { | ||
| let n = nonnegative_number_from_value(const_.value)?; | ||
| let range = match const_.operator { | ||
| DUCKDB_VX_EXPR_TYPE::DUCKDB_VX_EXPR_TYPE_COMPARE_EQUAL => Some(n..n + 1), | ||
| DUCKDB_VX_EXPR_TYPE::DUCKDB_VX_EXPR_TYPE_COMPARE_GREATERTHANOREQUALTO => { | ||
| Some(n..u64::MAX) | ||
| } | ||
| DUCKDB_VX_EXPR_TYPE::DUCKDB_VX_EXPR_TYPE_COMPARE_GREATERTHAN => { | ||
| Some(n.saturating_add(1)..u64::MAX) | ||
| } | ||
| DUCKDB_VX_EXPR_TYPE::DUCKDB_VX_EXPR_TYPE_COMPARE_LESSTHANOREQUALTO => { | ||
| Some(0..n.saturating_add(1)) | ||
| } | ||
| DUCKDB_VX_EXPR_TYPE::DUCKDB_VX_EXPR_TYPE_COMPARE_LESSTHAN => Some(0..n), | ||
| _ => None, | ||
| }; | ||
| Ok((Selection::All, range)) | ||
| } | ||
| TableFilterClass::ConjunctionAnd(conj) => { | ||
| let mut start = 0u64; | ||
| let mut end = u64::MAX; | ||
| let mut indices: Option<Vec<u64>> = None; | ||
| for child in conj.children() { | ||
| let (sel, range) = try_from_virtual_column_filter(child)?; | ||
| if let Selection::IncludeByIndex(buf) = sel { | ||
| indices = Some(match indices { | ||
| None => buf.iter().copied().collect(), | ||
| Some(existing) => intersect_sorted(&existing, buf.as_ref()), | ||
| }); | ||
| } | ||
| if let Some(r) = range { | ||
| start = start.max(r.start); | ||
| end = end.min(r.end); | ||
| } | ||
| } | ||
| let range = (start < end).then_some(start..end); | ||
| let sel = indices | ||
| .map(|v| Selection::IncludeByIndex(Buffer::from_iter(v))) | ||
| .unwrap_or(Selection::All); | ||
| Ok((sel, range)) | ||
|
Comment on lines
+202
to
+219
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is just selection merge? |
||
| } | ||
| TableFilterClass::Optional(child) => { | ||
| try_from_virtual_column_filter(child).or_else(|_| Ok((Selection::All, None))) | ||
| } | ||
| _ => Ok((Selection::All, None)), | ||
| } | ||
| } | ||
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 should be in a method Selection::merge
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.
Selection::merge is a more generic (and hard) method to implement. Here we're sure we're handling either Selection::All or Selection::IncludeByIndex.