Skip to content
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

fix guarantees in allways_true of PruningPredicate #8732

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 48 additions & 30 deletions datafusion/core/src/datasource/physical_plan/parquet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1768,8 +1768,9 @@ mod tests {
);
}

#[tokio::test]
async fn parquet_exec_metrics() {
/// Returns a string array with contents:
/// "[Foo, null, bar, bar, bar, bar, zzz]"
fn string_batch() -> RecordBatch {
let c1: ArrayRef = Arc::new(StringArray::from(vec![
Some("Foo"),
None,
Expand All @@ -1781,9 +1782,15 @@ mod tests {
]));

// batch1: c1(string)
let batch1 = create_batch(vec![("c1", c1.clone())]);
create_batch(vec![("c1", c1.clone())])
}

#[tokio::test]
async fn parquet_exec_metrics() {
// batch1: c1(string)
let batch1 = string_batch();

// on
// c1 != 'bar'
let filter = col("c1").not_eq(lit("bar"));

// read/write them files:
Expand Down Expand Up @@ -1812,20 +1819,10 @@ mod tests {

#[tokio::test]
async fn parquet_exec_display() {
let c1: ArrayRef = Arc::new(StringArray::from(vec![
Some("Foo"),
None,
Some("bar"),
Some("bar"),
Some("bar"),
Some("bar"),
Some("zzz"),
]));

// batch1: c1(string)
let batch1 = create_batch(vec![("c1", c1.clone())]);
let batch1 = string_batch();

// on
// c1 != 'bar'
let filter = col("c1").not_eq(lit("bar"));

let rt = RoundTrip::new()
Expand Down Expand Up @@ -1854,21 +1851,15 @@ mod tests {
}

#[tokio::test]
async fn parquet_exec_skip_empty_pruning() {
let c1: ArrayRef = Arc::new(StringArray::from(vec![
Some("Foo"),
None,
Some("bar"),
Some("bar"),
Some("bar"),
Some("bar"),
Some("zzz"),
]));

async fn parquet_exec_has_no_pruning_predicate_if_can_not_prune() {
// batch1: c1(string)
let batch1 = create_batch(vec![("c1", c1.clone())]);
let batch1 = string_batch();

// filter is too complicated for pruning
// filter is too complicated for pruning (PruningPredicate code does not
// handle case expressions), so the pruning predicate will always be
// "true"

// WHEN c1 != bar THEN true ELSE false END
let filter = when(col("c1").not_eq(lit("bar")), lit(true))
.otherwise(lit(false))
.unwrap();
Expand All @@ -1879,7 +1870,7 @@ mod tests {
.round_trip(vec![batch1])
.await;

// Should not contain a pruning predicate
// Should not contain a pruning predicate (since nothing can be pruned)
let pruning_predicate = &rt.parquet_exec.pruning_predicate;
assert!(
pruning_predicate.is_none(),
Expand All @@ -1892,6 +1883,33 @@ mod tests {
assert_eq!(predicate.unwrap().to_string(), filter_phys.to_string());
}

#[tokio::test]
async fn parquet_exec_has_pruning_predicate_for_guarantees() {
// batch1: c1(string)
let batch1 = string_batch();

// part of the filter is too complicated for pruning (PruningPredicate code does not
// handle case expressions), but part (c1 = 'foo') can be used for bloom filtering, so
// should still have the pruning predicate.

// c1 = 'foo' AND (WHEN c1 != bar THEN true ELSE false END)
let filter = col("c1").eq(lit("foo")).and(
when(col("c1").not_eq(lit("bar")), lit(true))
.otherwise(lit(false))
.unwrap(),
);

let rt = RoundTrip::new()
.with_predicate(filter.clone())
.with_pushdown_predicate()
.round_trip(vec![batch1])
.await;

// Should have a pruning predicate
let pruning_predicate = &rt.parquet_exec.pruning_predicate;
assert!(pruning_predicate.is_some());
}

/// returns the sum of all the metrics with the specified name
/// the returned set.
///
Expand Down
7 changes: 5 additions & 2 deletions datafusion/core/src/physical_optimizer/pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,12 @@ impl PruningPredicate {
&self.predicate_expr
}

/// Returns true if this pruning predicate is "always true" (aka will not prune anything)
/// Returns true if this pruning predicate can not prune anything.
///
/// This happens if the predicate is a literal `true` and
/// literal_guarantees is empty.
pub fn allways_true(&self) -> bool {
is_always_true(&self.predicate_expr)
is_always_true(&self.predicate_expr) && self.literal_guarantees.is_empty()
}

pub(crate) fn required_columns(&self) -> &RequiredColumns {
Expand Down