-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expose ExecutionPlan statistics across the FFI boundary
#22157
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
mailmindlin
wants to merge
12
commits into
apache:main
Choose a base branch
from
mailmindlin:feature/ffi-statistics
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.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7b0e037
Add metrics to `FFI_ExecutionPlan`
mailmindlin c4fa489
Fix off-by-one bug
mailmindlin 7b66b0b
Fix roundtrip tests
mailmindlin 1f6dcb0
Remove awkward docs
mailmindlin 73a495c
Explicit units
mailmindlin 1b00bbd
Fix clippy lint
mailmindlin e828d7d
fmt
mailmindlin 0c21413
move metrics.rs
mailmindlin a171b9e
FFI statistics
mailmindlin 92ca4c8
Simplify imports
mailmindlin 39f7dd9
Make statistics infallible
mailmindlin f8fe320
Faster SVec conversion
mailmindlin 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
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
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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| //! Helpers for moving [`Statistics`] across the FFI boundary as prost-encoded | ||
| //! `datafusion_proto_common::Statistics` bytes. | ||
| //! | ||
| //! [`Statistics`] contains [`Precision<ScalarValue>`] for column min/max/sum, | ||
| //! and `ScalarValue` is a large enum that's impractical to mirror in | ||
| //! `#[repr(C)]`. The proto round-trip already exists in `datafusion-proto-common` | ||
| //! and is the same pattern used to ship filter expressions across the FFI | ||
| //! boundary, so we reuse it here. | ||
| //! | ||
| //! [`Precision<ScalarValue>`]: datafusion_common::stats::Precision | ||
|
|
||
| use datafusion_common::{DataFusionError, Result, Statistics}; | ||
| use prost::Message; | ||
|
|
||
| /// Serialize [`Statistics`] to prost-encoded | ||
| /// `datafusion_proto_common::Statistics` bytes. | ||
| pub(crate) fn serialize_statistics(stats: &Statistics) -> Vec<u8> { | ||
| datafusion_proto_common::Statistics::from(stats).encode_to_vec() | ||
| } | ||
|
|
||
| /// Decode prost-encoded `datafusion_proto_common::Statistics` bytes back into | ||
| /// [`Statistics`]. | ||
| pub(crate) fn deserialize_statistics(bytes: &[u8]) -> Result<Statistics> { | ||
| let proto = datafusion_proto_common::Statistics::decode(bytes).map_err(|e| { | ||
| DataFusionError::Plan(format!("failed to decode Statistics: {e}")) | ||
| })?; | ||
| Statistics::try_from(&proto) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::datatypes::{DataType, Field, Schema}; | ||
| use datafusion_common::ScalarValue; | ||
| use datafusion_common::stats::Precision; | ||
| use datafusion_common::{ColumnStatistics, Statistics}; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn round_trip_unknown_statistics() { | ||
| let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]); | ||
| let original = Statistics::new_unknown(&Arc::new(schema)); | ||
|
|
||
| let bytes = serialize_statistics(&original); | ||
| let observed = deserialize_statistics(&bytes).expect("decode"); | ||
|
|
||
| assert_eq!(observed, original); | ||
| } | ||
|
|
||
| #[test] | ||
| fn round_trip_exact_statistics_with_scalar_values() { | ||
| let original = Statistics { | ||
| num_rows: Precision::Exact(100), | ||
| total_byte_size: Precision::Exact(4096), | ||
| column_statistics: vec![ | ||
| ColumnStatistics { | ||
| null_count: Precision::Exact(2), | ||
| max_value: Precision::Exact(ScalarValue::Int32(Some(50))), | ||
| min_value: Precision::Exact(ScalarValue::Int32(Some(-10))), | ||
| sum_value: Precision::Exact(ScalarValue::Int64(Some(1234))), | ||
| distinct_count: Precision::Exact(40), | ||
| byte_size: Precision::Exact(800), | ||
| }, | ||
| ColumnStatistics { | ||
| null_count: Precision::Exact(0), | ||
| max_value: Precision::Exact(ScalarValue::Utf8(Some( | ||
| "zebra".to_string(), | ||
| ))), | ||
| min_value: Precision::Exact(ScalarValue::Utf8(Some( | ||
| "ant".to_string(), | ||
| ))), | ||
| sum_value: Precision::Absent, | ||
| distinct_count: Precision::Inexact(95), | ||
| byte_size: Precision::Inexact(2048), | ||
| }, | ||
| ], | ||
| }; | ||
|
|
||
| let bytes = serialize_statistics(&original); | ||
| let observed = deserialize_statistics(&bytes).expect("decode"); | ||
|
|
||
| assert_eq!(observed, original); | ||
| } | ||
|
|
||
| #[test] | ||
| fn round_trip_mixed_precision() { | ||
| let original = Statistics { | ||
| num_rows: Precision::Inexact(42), | ||
| total_byte_size: Precision::Absent, | ||
| column_statistics: vec![ColumnStatistics { | ||
| null_count: Precision::Absent, | ||
| max_value: Precision::Inexact(ScalarValue::Float64(Some(1.5))), | ||
| min_value: Precision::Absent, | ||
| sum_value: Precision::Inexact(ScalarValue::Float64(Some(63.0))), | ||
| distinct_count: Precision::Absent, | ||
| byte_size: Precision::Absent, | ||
| }], | ||
| }; | ||
|
|
||
| let bytes = serialize_statistics(&original); | ||
| let observed = deserialize_statistics(&bytes).expect("decode"); | ||
|
|
||
| assert_eq!(observed, original); | ||
| } | ||
| } |
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.
why FFI_Option instead of FFI_Option?
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm not sure I understand what you mean