-
Notifications
You must be signed in to change notification settings - Fork 260
Change FieldSummary
{upper,lower}_bound
to ByteBuf
#1369
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
Fokko
wants to merge
3
commits into
apache:main
Choose a base branch
from
Fokko:fd-change-datum-into-bytes
base: main
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.
+256
−366
Open
Changes from all commits
Commits
Show all changes
3 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
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
// under the License. | ||
|
||
use fnv::FnvHashSet; | ||
use serde_bytes::ByteBuf; | ||
|
||
use crate::expr::visitors::bound_predicate_visitor::{BoundPredicateVisitor, visit}; | ||
use crate::expr::{BoundPredicate, BoundReference}; | ||
|
@@ -42,13 +43,13 @@ impl ManifestEvaluator { | |
/// see if this `ManifestFile` could possibly contain data that matches | ||
/// the scan's filter. | ||
pub(crate) fn eval(&self, manifest_file: &ManifestFile) -> Result<bool> { | ||
if manifest_file.partitions.is_empty() { | ||
return Ok(true); | ||
match &manifest_file.partitions { | ||
Some(p) if !p.is_empty() => { | ||
let mut evaluator = ManifestFilterVisitor::new(p); | ||
visit(&mut evaluator, &self.partition_filter) | ||
} | ||
_ => Ok(true), | ||
} | ||
|
||
let mut evaluator = ManifestFilterVisitor::new(&manifest_file.partitions); | ||
|
||
visit(&mut evaluator, &self.partition_filter) | ||
} | ||
} | ||
|
||
|
@@ -154,9 +155,19 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> { | |
_predicate: &BoundPredicate, | ||
) -> crate::Result<bool> { | ||
let field = self.field_summary_for_reference(reference); | ||
|
||
match &field.lower_bound { | ||
Some(bound) if datum <= bound => ROWS_CANNOT_MATCH, | ||
Some(_) => ROWS_MIGHT_MATCH, | ||
Some(bound_bytes) => { | ||
let bound = ManifestFilterVisitor::bytes_to_datum( | ||
bound_bytes, | ||
*reference.field().field_type.clone(), | ||
); | ||
if datum <= &bound { | ||
ROWS_CANNOT_MATCH | ||
} else { | ||
ROWS_MIGHT_MATCH | ||
} | ||
} | ||
None => ROWS_CANNOT_MATCH, | ||
} | ||
} | ||
|
@@ -169,8 +180,17 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> { | |
) -> crate::Result<bool> { | ||
let field = self.field_summary_for_reference(reference); | ||
match &field.lower_bound { | ||
Some(bound) if datum < bound => ROWS_CANNOT_MATCH, | ||
Some(_) => ROWS_MIGHT_MATCH, | ||
Some(bound_bytes) => { | ||
let bound = ManifestFilterVisitor::bytes_to_datum( | ||
bound_bytes, | ||
*reference.field().field_type.clone(), | ||
); | ||
if datum < &bound { | ||
ROWS_CANNOT_MATCH | ||
} else { | ||
ROWS_MIGHT_MATCH | ||
} | ||
} | ||
None => ROWS_CANNOT_MATCH, | ||
} | ||
} | ||
|
@@ -183,8 +203,17 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> { | |
) -> crate::Result<bool> { | ||
let field = self.field_summary_for_reference(reference); | ||
match &field.upper_bound { | ||
Some(bound) if datum >= bound => ROWS_CANNOT_MATCH, | ||
Some(_) => ROWS_MIGHT_MATCH, | ||
Some(bound_bytes) => { | ||
let bound = ManifestFilterVisitor::bytes_to_datum( | ||
bound_bytes, | ||
*reference.field().field_type.clone(), | ||
); | ||
if datum >= &bound { | ||
ROWS_CANNOT_MATCH | ||
} else { | ||
ROWS_MIGHT_MATCH | ||
} | ||
} | ||
None => ROWS_CANNOT_MATCH, | ||
} | ||
} | ||
|
@@ -197,8 +226,17 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> { | |
) -> crate::Result<bool> { | ||
let field = self.field_summary_for_reference(reference); | ||
match &field.upper_bound { | ||
Some(bound) if datum > bound => ROWS_CANNOT_MATCH, | ||
Some(_) => ROWS_MIGHT_MATCH, | ||
Some(bound_bytes) => { | ||
let bound = ManifestFilterVisitor::bytes_to_datum( | ||
bound_bytes, | ||
*reference.field().field_type.clone(), | ||
); | ||
if datum > &bound { | ||
ROWS_CANNOT_MATCH | ||
} else { | ||
ROWS_MIGHT_MATCH | ||
} | ||
} | ||
None => ROWS_CANNOT_MATCH, | ||
} | ||
} | ||
|
@@ -215,14 +253,22 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> { | |
return ROWS_CANNOT_MATCH; | ||
} | ||
|
||
if let Some(lower_bound) = &field.lower_bound { | ||
if lower_bound > datum { | ||
if let Some(lower_bound_bytes) = &field.lower_bound { | ||
let lower_bound = ManifestFilterVisitor::bytes_to_datum( | ||
lower_bound_bytes, | ||
*reference.field().field_type.clone(), | ||
); | ||
if &lower_bound > datum { | ||
return ROWS_CANNOT_MATCH; | ||
} | ||
} | ||
|
||
if let Some(upper_bound) = &field.upper_bound { | ||
if upper_bound < datum { | ||
if let Some(upper_bound_bytes) = &field.upper_bound { | ||
let upper_bound = ManifestFilterVisitor::bytes_to_datum( | ||
upper_bound_bytes, | ||
*reference.field().field_type.clone(), | ||
); | ||
if &upper_bound < datum { | ||
return ROWS_CANNOT_MATCH; | ||
} | ||
} | ||
|
@@ -260,23 +306,15 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> { | |
let prefix_len = prefix.len(); | ||
|
||
if let Some(lower_bound) = &field.lower_bound { | ||
let lower_bound_str = ManifestFilterVisitor::datum_as_str( | ||
lower_bound, | ||
"Cannot perform starts_with on non-string lower bound", | ||
)?; | ||
let min_len = lower_bound_str.len().min(prefix_len); | ||
if prefix.as_bytes().lt(&lower_bound_str.as_bytes()[..min_len]) { | ||
let min_len = lower_bound.len().min(prefix_len); | ||
if prefix.as_bytes().lt(&lower_bound[..min_len]) { | ||
return ROWS_CANNOT_MATCH; | ||
} | ||
} | ||
|
||
if let Some(upper_bound) = &field.upper_bound { | ||
let upper_bound_str = ManifestFilterVisitor::datum_as_str( | ||
upper_bound, | ||
"Cannot perform starts_with on non-string upper bound", | ||
)?; | ||
let min_len = upper_bound_str.len().min(prefix_len); | ||
if prefix.as_bytes().gt(&upper_bound_str.as_bytes()[..min_len]) { | ||
let min_len = upper_bound.len().min(prefix_len); | ||
if prefix.as_bytes().gt(&upper_bound[..min_len]) { | ||
return ROWS_CANNOT_MATCH; | ||
} | ||
} | ||
|
@@ -305,35 +343,19 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> { | |
// not_starts_with will match unless all values must start with the prefix. This happens when | ||
// the lower and upper bounds both start with the prefix. | ||
if let Some(lower_bound) = &field.lower_bound { | ||
let lower_bound_str = ManifestFilterVisitor::datum_as_str( | ||
lower_bound, | ||
"Cannot perform not_starts_with on non-string lower bound", | ||
)?; | ||
|
||
// if lower is shorter than the prefix then lower doesn't start with the prefix | ||
if prefix_len > lower_bound_str.len() { | ||
if prefix_len > lower_bound.len() { | ||
return ROWS_MIGHT_MATCH; | ||
} | ||
|
||
if prefix | ||
.as_bytes() | ||
.eq(&lower_bound_str.as_bytes()[..prefix_len]) | ||
{ | ||
if prefix.as_bytes().eq(&lower_bound[..prefix_len]) { | ||
if let Some(upper_bound) = &field.upper_bound { | ||
let upper_bound_str = ManifestFilterVisitor::datum_as_str( | ||
upper_bound, | ||
"Cannot perform not_starts_with on non-string upper bound", | ||
)?; | ||
|
||
// if upper is shorter than the prefix then upper can't start with the prefix | ||
if prefix_len > upper_bound_str.len() { | ||
if prefix_len > upper_bound.len() { | ||
return ROWS_MIGHT_MATCH; | ||
} | ||
|
||
if prefix | ||
.as_bytes() | ||
.eq(&upper_bound_str.as_bytes()[..prefix_len]) | ||
{ | ||
if prefix.as_bytes().eq(&upper_bound[..prefix_len]) { | ||
return ROWS_CANNOT_MATCH; | ||
} | ||
} | ||
|
@@ -359,13 +381,21 @@ impl BoundPredicateVisitor for ManifestFilterVisitor<'_> { | |
} | ||
|
||
if let Some(lower_bound) = &field.lower_bound { | ||
if literals.iter().all(|datum| lower_bound > datum) { | ||
let lower_bound = ManifestFilterVisitor::bytes_to_datum( | ||
lower_bound, | ||
*reference.field().clone().field_type, | ||
); | ||
if literals.iter().all(|datum| &lower_bound > datum) { | ||
return ROWS_CANNOT_MATCH; | ||
} | ||
} | ||
|
||
if let Some(upper_bound) = &field.upper_bound { | ||
if literals.iter().all(|datum| upper_bound < datum) { | ||
let upper_bound = ManifestFilterVisitor::bytes_to_datum( | ||
upper_bound, | ||
*reference.field().clone().field_type, | ||
); | ||
if literals.iter().all(|datum| &upper_bound < datum) { | ||
return ROWS_CANNOT_MATCH; | ||
} | ||
} | ||
|
@@ -414,6 +444,11 @@ impl ManifestFilterVisitor<'_> { | |
}; | ||
Ok(bound) | ||
} | ||
|
||
fn bytes_to_datum(bytes: &ByteBuf, t: Type) -> Datum { | ||
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. nit should this be in |
||
let p = t.as_primitive_type().unwrap(); | ||
Datum::try_from_bytes(bytes, p.clone()).unwrap() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
|
@@ -520,8 +555,8 @@ mod test { | |
FieldSummary { | ||
contains_null: false, | ||
contains_nan: None, | ||
lower_bound: Some(Datum::int(INT_MIN_VALUE)), | ||
upper_bound: Some(Datum::int(INT_MAX_VALUE)), | ||
lower_bound: Some(Datum::int(INT_MIN_VALUE).to_bytes().unwrap()), | ||
upper_bound: Some(Datum::int(INT_MAX_VALUE).to_bytes().unwrap()), | ||
}, | ||
// all_nulls_missing_nan | ||
FieldSummary { | ||
|
@@ -534,22 +569,22 @@ mod test { | |
FieldSummary { | ||
contains_null: true, | ||
contains_nan: None, | ||
lower_bound: Some(Datum::string(STRING_MIN_VALUE)), | ||
upper_bound: Some(Datum::string(STRING_MAX_VALUE)), | ||
lower_bound: Some(Datum::string(STRING_MIN_VALUE).to_bytes().unwrap()), | ||
upper_bound: Some(Datum::string(STRING_MAX_VALUE).to_bytes().unwrap()), | ||
}, | ||
// no_nulls | ||
FieldSummary { | ||
contains_null: false, | ||
contains_nan: None, | ||
lower_bound: Some(Datum::string(STRING_MIN_VALUE)), | ||
upper_bound: Some(Datum::string(STRING_MAX_VALUE)), | ||
lower_bound: Some(Datum::string(STRING_MIN_VALUE).to_bytes().unwrap()), | ||
upper_bound: Some(Datum::string(STRING_MAX_VALUE).to_bytes().unwrap()), | ||
}, | ||
// float | ||
FieldSummary { | ||
contains_null: true, | ||
contains_nan: None, | ||
lower_bound: Some(Datum::float(0.0)), | ||
upper_bound: Some(Datum::float(20.0)), | ||
lower_bound: Some(Datum::float(0.0).to_bytes().unwrap()), | ||
upper_bound: Some(Datum::float(20.0).to_bytes().unwrap()), | ||
}, | ||
// all_nulls_double | ||
FieldSummary { | ||
|
@@ -583,8 +618,8 @@ mod test { | |
FieldSummary { | ||
contains_null: false, | ||
contains_nan: Some(false), | ||
lower_bound: Some(Datum::float(0.0)), | ||
upper_bound: Some(Datum::float(20.0)), | ||
lower_bound: Some(Datum::float(0.0).to_bytes().unwrap()), | ||
upper_bound: Some(Datum::float(20.0).to_bytes().unwrap()), | ||
}, | ||
// all_nulls_missing_nan_float | ||
FieldSummary { | ||
|
@@ -597,15 +632,15 @@ mod test { | |
FieldSummary { | ||
contains_null: true, | ||
contains_nan: None, | ||
lower_bound: Some(Datum::string(STRING_MIN_VALUE)), | ||
upper_bound: Some(Datum::string(STRING_MIN_VALUE)), | ||
lower_bound: Some(Datum::string(STRING_MIN_VALUE).to_bytes().unwrap()), | ||
upper_bound: Some(Datum::string(STRING_MIN_VALUE).to_bytes().unwrap()), | ||
}, | ||
// no_nulls_same_value_a | ||
FieldSummary { | ||
contains_null: false, | ||
contains_nan: None, | ||
lower_bound: Some(Datum::string(STRING_MIN_VALUE)), | ||
upper_bound: Some(Datum::string(STRING_MIN_VALUE)), | ||
lower_bound: Some(Datum::string(STRING_MIN_VALUE).to_bytes().unwrap()), | ||
upper_bound: Some(Datum::string(STRING_MIN_VALUE).to_bytes().unwrap()), | ||
}, | ||
] | ||
} | ||
|
@@ -625,7 +660,7 @@ mod test { | |
added_rows_count: None, | ||
existing_rows_count: None, | ||
deleted_rows_count: None, | ||
partitions, | ||
partitions: Some(partitions), | ||
key_metadata: vec![], | ||
} | ||
} | ||
|
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
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.
@liurenjie1024 what's the cost of
datum_from_bytes
andbytes_to_datum
? Do we need to introduce a new type in between?