Skip to content

adapt filter expressions to file schema during parquet scan #16461

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
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
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
134 changes: 129 additions & 5 deletions datafusion/datasource-parquet/src/opener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use datafusion_common::pruning::{
};
use datafusion_common::{exec_err, Result};
use datafusion_datasource::PartitionedFile;
use datafusion_physical_expr::PhysicalExprSchemaRewriter;
use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
use datafusion_physical_optimizer::pruning::PruningPredicate;
use datafusion_physical_plan::metrics::{Count, ExecutionPlanMetricsSet, MetricBuilder};
Expand Down Expand Up @@ -117,7 +118,6 @@ impl FileOpener for ParquetOpener {

let projected_schema =
SchemaRef::from(self.logical_file_schema.project(&self.projection)?);
let schema_adapter_factory = Arc::clone(&self.schema_adapter_factory);
let schema_adapter = self
.schema_adapter_factory
.create(projected_schema, Arc::clone(&self.logical_file_schema));
Expand Down Expand Up @@ -159,7 +159,7 @@ impl FileOpener for ParquetOpener {
if let Some(pruning_predicate) = pruning_predicate {
// The partition column schema is the schema of the table - the schema of the file
let mut pruning = Box::new(PartitionPruningStatistics::try_new(
vec![file.partition_values],
vec![file.partition_values.clone()],
partition_fields.clone(),
)?)
as Box<dyn PruningStatistics>;
Expand Down Expand Up @@ -248,10 +248,25 @@ impl FileOpener for ParquetOpener {
}
}

let predicate = predicate
.map(|p| {
PhysicalExprSchemaRewriter::new(
&physical_file_schema,
&logical_file_schema,
)
.with_partition_columns(
partition_fields.to_vec(),
file.partition_values,
)
.rewrite(p)
.map_err(ArrowError::from)
})
.transpose()?;

// Build predicates for this specific file
let (pruning_predicate, page_pruning_predicate) = build_pruning_predicates(
predicate.as_ref(),
&logical_file_schema,
&physical_file_schema,
&predicate_creation_errors,
);

Expand Down Expand Up @@ -288,11 +303,9 @@ impl FileOpener for ParquetOpener {
let row_filter = row_filter::build_row_filter(
&predicate,
&physical_file_schema,
&logical_file_schema,
builder.metadata(),
reorder_predicates,
&file_metrics,
&schema_adapter_factory,
);

match row_filter {
Expand Down Expand Up @@ -879,4 +892,115 @@ mod test {
assert_eq!(num_batches, 0);
assert_eq!(num_rows, 0);
}

#[tokio::test]
async fn test_prune_on_partition_value_and_data_value() {
let store = Arc::new(InMemory::new()) as Arc<dyn ObjectStore>;

// Note: number 3 is missing!
let batch = record_batch!(("a", Int32, vec![Some(1), Some(2), Some(4)])).unwrap();
let data_size =
write_parquet(Arc::clone(&store), "part=1/file.parquet", batch.clone()).await;

let file_schema = batch.schema();
let mut file = PartitionedFile::new(
"part=1/file.parquet".to_string(),
u64::try_from(data_size).unwrap(),
);
file.partition_values = vec![ScalarValue::Int32(Some(1))];

let table_schema = Arc::new(Schema::new(vec![
Field::new("part", DataType::Int32, false),
Field::new("a", DataType::Int32, false),
]));

let make_opener = |predicate| {
ParquetOpener {
partition_index: 0,
projection: Arc::new([0]),
batch_size: 1024,
limit: None,
predicate: Some(predicate),
logical_file_schema: file_schema.clone(),
metadata_size_hint: None,
metrics: ExecutionPlanMetricsSet::new(),
parquet_file_reader_factory: Arc::new(
DefaultParquetFileReaderFactory::new(Arc::clone(&store)),
),
partition_fields: vec![Arc::new(Field::new(
"part",
DataType::Int32,
false,
))],
pushdown_filters: true, // note that this is true!
reorder_filters: true,
enable_page_index: false,
enable_bloom_filter: false,
schema_adapter_factory: Arc::new(DefaultSchemaAdapterFactory),
enable_row_group_stats_pruning: false, // note that this is false!
coerce_int96: None,
}
};

let make_meta = || FileMeta {
object_meta: ObjectMeta {
location: Path::from("part=1/file.parquet"),
last_modified: Utc::now(),
size: u64::try_from(data_size).unwrap(),
e_tag: None,
version: None,
},
range: None,
extensions: None,
metadata_size_hint: None,
};

// Filter should match the partition value and data value
let expr = col("part").eq(lit(1)).or(col("a").eq(lit(1)));
let predicate = logical2physical(&expr, &table_schema);
let opener = make_opener(predicate);
let stream = opener
.open(make_meta(), file.clone())
.unwrap()
.await
.unwrap();
let (num_batches, num_rows) = count_batches_and_rows(stream).await;
assert_eq!(num_batches, 1);
assert_eq!(num_rows, 3);

// Filter should match the partition value but not the data value
let expr = col("part").eq(lit(1)).or(col("a").eq(lit(3)));
let predicate = logical2physical(&expr, &table_schema);
let opener = make_opener(predicate);
let stream = opener
.open(make_meta(), file.clone())
.unwrap()
.await
.unwrap();
let (num_batches, num_rows) = count_batches_and_rows(stream).await;
assert_eq!(num_batches, 1);
assert_eq!(num_rows, 3);

// Filter should not match the partition value but match the data value
let expr = col("part").eq(lit(2)).or(col("a").eq(lit(1)));
let predicate = logical2physical(&expr, &table_schema);
let opener = make_opener(predicate);
let stream = opener
.open(make_meta(), file.clone())
.unwrap()
.await
.unwrap();
let (num_batches, num_rows) = count_batches_and_rows(stream).await;
assert_eq!(num_batches, 1);
assert_eq!(num_rows, 1);
Copy link
Contributor Author

@adriangb adriangb Jun 19, 2025

Choose a reason for hiding this comment

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

This assertion fails on main: all 3 rows are passed because the row filter cannot handle the partition columns. This PR somewhat coincidentally happens to allow the row filter to handle predicates that depend on partition and data columns!


// Filter should not match the partition value or the data value
let expr = col("part").eq(lit(2)).or(col("a").eq(lit(3)));
let predicate = logical2physical(&expr, &table_schema);
let opener = make_opener(predicate);
let stream = opener.open(make_meta(), file).unwrap().await.unwrap();
let (num_batches, num_rows) = count_batches_and_rows(stream).await;
assert_eq!(num_batches, 0);
assert_eq!(num_rows, 0);
}
}
Loading