Repository navigation
fix(ffi): route partition statistics through statistics_with_args #2
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
alamb
wants to merge
1
commit into
asolimando:asolimando/partition-statistics-context
from
alamb:fix/ffi-statistics-with-args
Closed
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ use datafusion_common::{DataFusionError, Result, Statistics}; | |
| use datafusion_execution::{SendableRecordBatchStream, TaskContext}; | ||
| use datafusion_physical_expr_common::metrics::MetricsSet; | ||
| use datafusion_physical_plan::{ | ||
| DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties, | ||
| DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties, StatisticsArgs, | ||
| }; | ||
| use stabby::string::String as SString; | ||
| use stabby::vec::Vec as SVec; | ||
|
|
@@ -209,9 +209,8 @@ unsafe extern "C" fn partition_statistics_fn_wrapper( | |
| partition: FFI_Option<usize>, | ||
| ) -> FFI_Result<SVec<u8>> { | ||
| let partition: Option<usize> = partition.into(); | ||
| #[expect(deprecated)] | ||
| plan.inner() | ||
| .partition_statistics(partition) | ||
| .statistics_with_args(&StatisticsArgs::new(partition)) | ||
| .map(|stats| SVec::from(serialize_statistics(stats.as_ref()).as_slice())) | ||
| .into() | ||
| } | ||
|
|
@@ -557,9 +556,9 @@ pub mod tests { | |
| self.metrics.clone() | ||
| } | ||
|
|
||
| fn partition_statistics( | ||
| fn statistics_with_args( | ||
| &self, | ||
| _partition: Option<usize>, | ||
| _args: &StatisticsArgs, | ||
| ) -> Result<Arc<Statistics>> { | ||
| Ok(Arc::new(self.statistics.clone().unwrap_or_else(|| { | ||
| Statistics::new_unknown(self.props.eq_properties.schema()) | ||
|
|
@@ -673,28 +672,34 @@ pub mod tests { | |
| Ok(()) | ||
| } | ||
|
|
||
| #[test] | ||
| #[expect(deprecated)] | ||
| fn test_ffi_execution_plan_partition_statistics_round_trip() -> Result<()> { | ||
| /// Build an `EmptyExec` carrying `statistics`, then export it across the | ||
| /// (mock) FFI boundary and return the resulting foreign plan. | ||
| #[cfg(test)] | ||
| fn export_empty_exec_over_ffi( | ||
| schema: &arrow::datatypes::SchemaRef, | ||
| statistics: Option<Statistics>, | ||
| ) -> Result<Arc<dyn ExecutionPlan>> { | ||
| let mut plan = EmptyExec::new(Arc::clone(schema)); | ||
| if let Some(statistics) = statistics { | ||
| plan = plan.with_statistics(statistics); | ||
| } | ||
| let mut local = FFI_ExecutionPlan::new(Arc::new(plan), None); | ||
| local.library_marker_id = crate::mock_foreign_marker_id; | ||
| let foreign: Arc<dyn ExecutionPlan> = (&local).try_into()?; | ||
| Ok(foreign) | ||
| } | ||
|
|
||
| /// Schema and a fully-populated `Statistics` (including `ScalarValue`-typed | ||
| /// min/max) shared by the FFI statistics round-trip tests. | ||
| #[cfg(test)] | ||
| fn stats_round_trip_fixture() -> (arrow::datatypes::SchemaRef, Statistics) { | ||
| use datafusion_common::stats::Precision; | ||
| use datafusion_common::{ColumnStatistics, ScalarValue}; | ||
|
|
||
| let schema = Arc::new(arrow::datatypes::Schema::new(vec![ | ||
| arrow::datatypes::Field::new("a", arrow::datatypes::DataType::Int32, true), | ||
| ])); | ||
|
|
||
| // Plans without explicit statistics return Statistics::new_unknown across | ||
| // the boundary. | ||
| let bare_plan = Arc::new(EmptyExec::new(Arc::clone(&schema))); | ||
| let mut bare_local = FFI_ExecutionPlan::new(bare_plan, None); | ||
| bare_local.library_marker_id = crate::mock_foreign_marker_id; | ||
| let bare_foreign: Arc<dyn ExecutionPlan> = (&bare_local).try_into()?; | ||
| let bare_stats = bare_foreign.partition_statistics(None)?; | ||
| assert_eq!(bare_stats.as_ref(), &Statistics::new_unknown(&schema)); | ||
|
|
||
| // Plans with statistics round-trip them faithfully, including | ||
| // ScalarValue-typed min/max. | ||
| let original_stats = Statistics { | ||
| let statistics = Statistics { | ||
| num_rows: Precision::Exact(7), | ||
| total_byte_size: Precision::Inexact(128), | ||
| column_statistics: vec![ColumnStatistics { | ||
|
|
@@ -706,18 +711,68 @@ pub mod tests { | |
| byte_size: Precision::Exact(28), | ||
| }], | ||
| }; | ||
| let stats_plan = Arc::new( | ||
| EmptyExec::new(Arc::clone(&schema)).with_statistics(original_stats.clone()), | ||
| (schema, statistics) | ||
| } | ||
|
|
||
| /// Statistics survive an FFI round trip when queried through the | ||
| /// **deprecated** `partition_statistics` entry point on the foreign plan. | ||
| #[test] | ||
| #[expect(deprecated)] | ||
| fn test_ffi_execution_plan_partition_statistics_round_trip() -> Result<()> { | ||
| let (schema, original_stats) = stats_round_trip_fixture(); | ||
|
|
||
| // A plan without explicit statistics reports new_unknown. | ||
| let bare = export_empty_exec_over_ffi(&schema, None)?; | ||
| assert_eq!( | ||
| bare.partition_statistics(None)?.as_ref(), | ||
| &Statistics::new_unknown(&schema) | ||
| ); | ||
|
|
||
| // A plan with statistics round-trips them for overall and per-partition queries. | ||
| let with_stats = | ||
| export_empty_exec_over_ffi(&schema, Some(original_stats.clone()))?; | ||
| assert_eq!( | ||
| with_stats.partition_statistics(None)?.as_ref(), | ||
|
Author
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. old API |
||
| &original_stats | ||
| ); | ||
| assert_eq!( | ||
| with_stats.partition_statistics(Some(1))?.as_ref(), | ||
| &original_stats | ||
| ); | ||
| let mut stats_local = FFI_ExecutionPlan::new(stats_plan, None); | ||
| stats_local.library_marker_id = crate::mock_foreign_marker_id; | ||
| let stats_foreign: Arc<dyn ExecutionPlan> = (&stats_local).try_into()?; | ||
|
|
||
| let observed = stats_foreign.partition_statistics(None)?; | ||
| assert_eq!(observed.as_ref(), &original_stats); | ||
| Ok(()) | ||
| } | ||
|
|
||
| let observed_partition = stats_foreign.partition_statistics(Some(1))?; | ||
| assert_eq!(observed_partition.as_ref(), &original_stats); | ||
| /// Same round trip as | ||
| /// [`test_ffi_execution_plan_partition_statistics_round_trip`], but queried | ||
| /// through the **new** `statistics_with_args` entry point. | ||
| #[test] | ||
| fn test_ffi_execution_plan_statistics_with_args_round_trip() -> Result<()> { | ||
| let (schema, original_stats) = stats_round_trip_fixture(); | ||
|
|
||
| // A plan without explicit statistics reports new_unknown. | ||
| let bare = export_empty_exec_over_ffi(&schema, None)?; | ||
| assert_eq!( | ||
| bare.statistics_with_args(&StatisticsArgs::new(None))? | ||
| .as_ref(), | ||
| &Statistics::new_unknown(&schema) | ||
| ); | ||
|
|
||
| // A plan with statistics round-trips them for overall and per-partition queries. | ||
| let with_stats = | ||
| export_empty_exec_over_ffi(&schema, Some(original_stats.clone()))?; | ||
| assert_eq!( | ||
| with_stats | ||
| .statistics_with_args(&StatisticsArgs::new(None))? | ||
|
Author
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. Same test, with new API |
||
| .as_ref(), | ||
| &original_stats | ||
| ); | ||
| assert_eq!( | ||
| with_stats | ||
| .statistics_with_args(&StatisticsArgs::new(Some(1)))? | ||
| .as_ref(), | ||
| &original_stats | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
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.
Refactored out into a common fixture so I could avoid a copy/pasta version of the test that was hard to understand what was different