-
Notifications
You must be signed in to change notification settings - Fork 279
[WIP] Add ManifestEvaluator
to allow filtering of files in a table scan (Issue #152)
#241
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
9839ef1
feat: add PartitionEvaluator
sdd 9a0f80f
feat(wip): add ManifestEvalVisitor and InclusiveProjection
sdd 91c2852
feat: evaluate ManifestFile partition values before loading its Manifest
sdd 53c6073
feat: avoid unneeded extra iter and await
sdd f270443
chore: fix some clippy lints
sdd b8b6bf2
feat: implement field_id accessor for BoundReference Expressions
sdd 380f4a0
feat(wip): add accessors
sdd 47270d2
fix: dont make predicate op pub(crate)"
sdd 9156a56
refactor: change TableScanBuilder case sensitivity setter
sdd 22ac049
chore: remove outdated comment and fix typo
sdd 7b1eff8
feat: redo accessors. Add field_id to accessor map to Schema, popualt…
sdd 56d935c
feat: ensure visit/project are fallible, accessor returns option. Add…
sdd 7bf331d
feat: add BoundPredicateEvaluator
sdd fbe09e9
feat: update accessors to use Arc and store their map as Arcs.
sdd 35428c9
fix: InclusiveProjection
sdd c663a7d
refactor: factor out PartitionEvaluator, use only ManifestEvaluator
sdd ae8de9b
refactor: add BoundPredicateEvaluator, use for ManifestEvaluator, mov…
sdd 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
feat(wip): add ManifestEvalVisitor and InclusiveProjection
- Loading branch information
commit 9a0f80fe91d2041770f0f40ac267f81df9b5b4cf
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,22 +19,20 @@ | |
|
||
use crate::arrow::ArrowReaderBuilder; | ||
use crate::expr::BoundPredicate::AlwaysTrue; | ||
use crate::expr::{Bind, BoundPredicate, BoundReference, LogicalExpression, Predicate, PredicateOperator}; | ||
use crate::expr::{Bind, BoundPredicate, LogicalExpression, Predicate, PredicateOperator}; | ||
use crate::io::FileIO; | ||
use crate::spec::{ | ||
DataContentType, FieldSummary, Manifest, ManifestEntry, ManifestEntryRef, ManifestFile, | ||
PartitionField, PartitionSpec, PartitionSpecRef, Schema, SchemaRef, SnapshotRef, | ||
TableMetadataRef, Transform, | ||
DataContentType, FieldSummary, ManifestEntryRef, ManifestFile, PartitionField, | ||
PartitionSpecRef, Schema, SchemaRef, SnapshotRef, TableMetadataRef, | ||
}; | ||
use crate::table::Table; | ||
use crate::{Error, ErrorKind}; | ||
use arrow_array::RecordBatch; | ||
use async_stream::try_stream; | ||
use futures::stream::{iter, BoxStream}; | ||
use futures::StreamExt; | ||
use std::collections::HashMap; | ||
use std::ops::Deref; | ||
use std::sync::Arc; | ||
use async_stream::try_stream; | ||
|
||
/// Builder to create table scan. | ||
pub struct TableScanBuilder<'a> { | ||
|
@@ -173,8 +171,7 @@ pub type FileScanTaskStream = BoxStream<'static, crate::Result<FileScanTask>>; | |
impl TableScan { | ||
/// Returns a stream of file scan tasks. | ||
|
||
|
||
pub async fn plan_files(&self) -> crate::Result<FileScanTaskStream> { | ||
pub async fn plan_files(&'static self) -> crate::Result<FileScanTaskStream> { | ||
// Cache `PartitionEvaluator`s created as part of this scan | ||
let mut partition_evaluator_cache: HashMap<i32, PartitionEvaluator> = HashMap::new(); | ||
|
||
|
@@ -197,20 +194,12 @@ impl TableScan { | |
// PartitionEvaluator that matches this manifest's partition spec ID. | ||
// Use one from the cache if there is one. If not, create one, put it in | ||
// the cache, and take a reference to it. | ||
let partition_evaluator = if let Some(filter) = self.filter.as_ref() { | ||
Some( | ||
partition_evaluator_cache | ||
if let Some(filter) = self.filter.as_ref() { | ||
let partition_evaluator = partition_evaluator_cache | ||
.entry(manifest.partition_spec_id()) | ||
.or_insert_with_key(self.create_partition_evaluator(filter)) | ||
.deref(), | ||
) | ||
} else { | ||
None | ||
}; | ||
.or_insert_with_key(|key| self.create_partition_evaluator(key, filter)); | ||
|
||
// If this scan has a filter, reject any manifest files whose partition values | ||
// don't match the filter. | ||
if let Some(partition_evaluator) = partition_evaluator { | ||
// reject any manifest files whose partition values don't match the filter. | ||
if !partition_evaluator.filter_manifest_file(&entry) { | ||
sdd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue; | ||
} | ||
|
@@ -236,20 +225,24 @@ impl TableScan { | |
} | ||
} | ||
} | ||
}.boxed()) | ||
} | ||
.boxed()) | ||
} | ||
|
||
fn create_partition_evaluator(&self, filter: &Predicate) -> fn(&i32) -> crate::Result<PartitionEvaluator> { | ||
|&id| { | ||
// TODO: predicate binding not yet merged to main | ||
let bound_predicate = filter.bind(self.schema.clone(), self.case_sensitive)?; | ||
fn create_partition_evaluator(&self, id: &i32, filter: &Predicate) -> PartitionEvaluator { | ||
|
||
let partition_spec = self.table_metadata.partition_spec_by_id(id).unwrap(); | ||
PartitionEvaluator::new(partition_spec.clone(), bound_predicate, self.schema.clone()) | ||
} | ||
// TODO: this does not work yet. `bind` consumes self, but `Predicate` | ||
// does not implement `Clone` or `Copy`. | ||
let bound_predicate = filter.clone() | ||
.bind(self.schema.clone(), self.case_sensitive) | ||
.unwrap(); | ||
|
||
let partition_spec = self.table_metadata.partition_spec_by_id(*id).unwrap(); | ||
PartitionEvaluator::new(partition_spec.clone(), bound_predicate, self.schema.clone()) | ||
.unwrap() | ||
} | ||
|
||
pub async fn to_arrow(&self) -> crate::Result<ArrowRecordBatchStream> { | ||
pub async fn to_arrow(&'static self) -> crate::Result<ArrowRecordBatchStream> { | ||
let mut arrow_reader_builder = | ||
ArrowReaderBuilder::new(self.file_io.clone(), self.schema.clone()); | ||
|
||
|
@@ -315,7 +308,11 @@ struct ManifestEvalVisitor { | |
} | ||
|
||
impl ManifestEvalVisitor { | ||
fn new(partition_schema: SchemaRef, partition_filter: Predicate, case_sensitive: bool) -> crate::Result<Self> { | ||
fn new( | ||
partition_schema: SchemaRef, | ||
partition_filter: Predicate, | ||
case_sensitive: bool, | ||
) -> crate::Result<Self> { | ||
let partition_filter = partition_filter.bind(partition_schema.clone(), case_sensitive)?; | ||
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. I think we have |
||
|
||
Ok(Self { | ||
|
@@ -332,8 +329,13 @@ impl ManifestEvalVisitor { | |
case_sensitive: bool, | ||
) -> crate::Result<Self> { | ||
let partition_type = partition_spec.partition_type(&table_schema)?; | ||
|
||
// this is needed as SchemaBuilder.with_fields expects an iterator over | ||
// Arc<NestedField> rather than &Arc<NestedField> | ||
let cloned_partition_fields: Vec<_> = partition_type.fields().iter().map(Arc::clone).collect(); | ||
|
||
let partition_schema = Schema::builder() | ||
.with_fields(partition_type.fields()) | ||
.with_fields(cloned_partition_fields) | ||
.build()?; | ||
|
||
let partition_schema_ref = Arc::new(partition_schema); | ||
|
@@ -478,14 +480,20 @@ impl InclusiveProjection { | |
|
||
// TODO: cache this? | ||
let mut parts: Vec<&PartitionField> = vec![]; | ||
for partition_spec_field in self.partition_spec.fields { | ||
for partition_spec_field in &self.partition_spec.fields { | ||
if partition_spec_field.source_id == field_id { | ||
parts.push(&partition_spec_field) | ||
} | ||
} | ||
|
||
parts.iter().fold(Predicate::AlwaysTrue, |res, &part| { | ||
res.and(part.transform.project(&part.name, &predicate)) | ||
// should this use ? instead of destructuring Ok() so that the whole call fails | ||
// if the transform project() call errors? This would require changing the signature of `visit`. | ||
if let Ok(Some(pred_for_part)) = part.transform.project(&part.name, &predicate) { | ||
res.and(pred_for_part) | ||
} else { | ||
res | ||
} | ||
}) | ||
} | ||
} | ||
|
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
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.
I don't think these are needed anymore.