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: Parquet predicate pushdown for lit(_) != #19246

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 0 additions & 8 deletions crates/polars-expr/src/expressions/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,14 +525,6 @@ fn apply_multiple_elementwise<'a>(
impl StatsEvaluator for ApplyExpr {
fn should_read(&self, stats: &BatchStats) -> PolarsResult<bool> {
let read = self.should_read_impl(stats)?;
if ExecutionState::new().verbose() {
if read {
eprintln!("parquet file must be read, statistics not sufficient for predicate.")
} else {
eprintln!("parquet file can be skipped, the statistics were sufficient to apply the predicate.")
}
}

Ok(read)
}
}
Expand Down
6 changes: 1 addition & 5 deletions crates/polars-expr/src/expressions/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ mod stats {
use ChunkCompareIneq as C;
match op {
Operator::Eq => apply_operator_stats_eq(min_max, literal),
Operator::NotEq => apply_operator_stats_eq(min_max, literal),
Operator::NotEq => apply_operator_stats_neq(min_max, literal),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the actual fix. The rest are all drive-by's.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auch.

Operator::Gt => {
// Literal is bigger than max value, selection needs all rows.
C::gt(literal, min_max).map(|ca| ca.any()).unwrap_or(false)
Expand Down Expand Up @@ -457,10 +457,6 @@ mod stats {

impl StatsEvaluator for BinaryExpr {
fn should_read(&self, stats: &BatchStats) -> PolarsResult<bool> {
if std::env::var("POLARS_NO_PARQUET_STATISTICS").is_ok() {
return Ok(true);
}

use Operator::*;
match (
self.left.as_stats_evaluator(),
Expand Down
31 changes: 25 additions & 6 deletions crates/polars-io/src/parquet/read/predicates.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use polars_core::config;
use polars_core::prelude::*;
use polars_parquet::read::statistics::{deserialize, Statistics};
use polars_parquet::read::RowGroupMetadata;
Expand Down Expand Up @@ -50,18 +51,36 @@ pub fn read_this_row_group(
md: &RowGroupMetadata,
schema: &ArrowSchema,
) -> PolarsResult<bool> {
if std::env::var("POLARS_NO_PARQUET_STATISTICS").is_ok() {
return Ok(true);
}

let mut should_read = true;

if let Some(pred) = predicate {
if let Some(pred) = pred.as_stats_evaluator() {
if let Some(stats) = collect_statistics(md, schema)? {
let should_read = pred.should_read(&stats);
let pred_result = pred.should_read(&stats);

// a parquet file may not have statistics of all columns
if matches!(should_read, Ok(false)) {
return Ok(false);
} else if !matches!(should_read, Err(PolarsError::ColumnNotFound(_))) {
let _ = should_read?;
match pred_result {
Err(PolarsError::ColumnNotFound(errstr)) => {
return Err(PolarsError::ColumnNotFound(errstr))
},
Ok(false) => should_read = false,
_ => {},
}
}
}
}
Ok(true)

if config::verbose() {
if should_read {
eprintln!("parquet row group must be read, statistics not sufficient for predicate.");
} else {
eprintln!("parquet row group can be skipped, the statistics were sufficient to apply the predicate.");
}
}

Ok(should_read)
}
Loading