-
Notifications
You must be signed in to change notification settings - Fork 2.3k
refactor(proto): put Partitioning / sort-expression serde on the types #24003
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
adriangb
wants to merge
3
commits into
apache:main
Choose a base branch
from
pydantic:prep/partitioning-proto-on-types
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
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,10 @@ pub use datafusion_common::SplitPoint; | |
| use datafusion_common::{Result, validate_range_split_points}; | ||
| use datafusion_physical_expr_common::physical_expr::format_physical_expr_list; | ||
| use datafusion_physical_expr_common::sort_expr::{LexOrdering, PhysicalSortExpr}; | ||
| #[cfg(feature = "proto")] | ||
| use datafusion_physical_expr_common::sort_expr::{ | ||
| sort_exprs_try_from_proto, sort_exprs_try_to_proto, | ||
| }; | ||
| use std::fmt; | ||
| use std::fmt::Display; | ||
| use std::sync::Arc; | ||
|
|
@@ -515,6 +519,156 @@ impl Partitioning { | |
| } | ||
| } | ||
|
|
||
| /// Protobuf conversions for [`Partitioning`]. | ||
| /// | ||
| /// Child expressions (hash keys, range orderings) and `ScalarValue` split | ||
| /// points are (de)serialized through the expression-level context, so this is | ||
| /// the single copy of the partitioning wire format: `RepartitionExec` and | ||
| /// `datafusion-proto`'s central serializer route through it, and the remaining | ||
| /// per-plan migrations (`FileScanConfig` and friends) are meant to do the same | ||
| /// rather than grow another copy. | ||
| /// | ||
| /// [`protobuf::Partitioning`]: datafusion_proto_models::protobuf::Partitioning | ||
| #[cfg(feature = "proto")] | ||
| impl Partitioning { | ||
| /// Serialize this partitioning into its protobuf representation. | ||
| pub fn try_to_proto( | ||
| &self, | ||
| ctx: &datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx<'_>, | ||
| ) -> Result<datafusion_proto_models::protobuf::Partitioning> { | ||
| use datafusion_proto_models::protobuf; | ||
|
|
||
| let partition_method = match self { | ||
| Partitioning::RoundRobinBatch(n) => { | ||
| protobuf::partitioning::PartitionMethod::RoundRobin(wire_partition_count( | ||
| *n, | ||
| )?) | ||
| } | ||
| Partitioning::Hash(exprs, n) => { | ||
| protobuf::partitioning::PartitionMethod::Hash( | ||
| protobuf::PhysicalHashRepartition { | ||
| hash_expr: ctx.encode_children_expressions(exprs)?, | ||
| partition_count: wire_partition_count(*n)?, | ||
| }, | ||
| ) | ||
| } | ||
| Partitioning::Range(range) => { | ||
| let sort_expr = sort_exprs_try_to_proto(range.ordering().iter(), ctx)?; | ||
| let split_point = range | ||
| .split_points() | ||
| .iter() | ||
| .map(|split_point| { | ||
| let value = split_point | ||
| .values() | ||
| .iter() | ||
| .map(|value| value.try_into().map_err(Into::into)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| Ok(protobuf::PhysicalRangeSplitPoint { value }) | ||
| }) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| protobuf::partitioning::PartitionMethod::Range( | ||
| protobuf::PhysicalRangePartitioning { | ||
| sort_expr, | ||
| split_point, | ||
| }, | ||
| ) | ||
| } | ||
| Partitioning::UnknownPartitioning(n) => { | ||
| protobuf::partitioning::PartitionMethod::Unknown(wire_partition_count( | ||
| *n, | ||
| )?) | ||
| } | ||
| }; | ||
|
Comment on lines
+576
to
+581
Contributor
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 as the sibling comment above — both encode sites now use the checked |
||
| Ok(protobuf::Partitioning { | ||
| partition_method: Some(partition_method), | ||
| }) | ||
| } | ||
|
|
||
| /// Reconstruct a [`Partitioning`] from its protobuf representation. | ||
| /// | ||
| /// Returns `Ok(None)` when the message carries no `partition_method`, which | ||
| /// the wire format uses to mean "no output partitioning declared"; callers | ||
| /// for which it is required should turn that into their own error. | ||
| pub fn try_from_proto( | ||
| node: &datafusion_proto_models::protobuf::Partitioning, | ||
| ctx: &datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx<'_>, | ||
| ) -> Result<Option<Self>> { | ||
| use datafusion_common::{ScalarValue, internal_datafusion_err, internal_err}; | ||
| use datafusion_proto_models::protobuf; | ||
|
|
||
| let Some(partition_method) = node.partition_method.as_ref() else { | ||
| return Ok(None); | ||
| }; | ||
| let partitioning = match partition_method { | ||
| protobuf::partitioning::PartitionMethod::RoundRobin(n) => { | ||
| Partitioning::RoundRobinBatch(partition_count(*n)?) | ||
| } | ||
| protobuf::partitioning::PartitionMethod::Hash(hash) => { | ||
| let exprs = hash | ||
| .hash_expr | ||
| .iter() | ||
| .map(|expr| ctx.decode(expr)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| Partitioning::Hash(exprs, partition_count(hash.partition_count)?) | ||
| } | ||
| protobuf::partitioning::PartitionMethod::Unknown(n) => { | ||
| Partitioning::UnknownPartitioning(partition_count(*n)?) | ||
| } | ||
| protobuf::partitioning::PartitionMethod::Range(range) => { | ||
| let sort_exprs = sort_exprs_try_from_proto(&range.sort_expr, ctx)?; | ||
| let sort_expr_count = sort_exprs.len(); | ||
| let ordering = LexOrdering::new(sort_exprs).ok_or_else(|| { | ||
| internal_datafusion_err!( | ||
| "Range partitioning requires non-empty ordering" | ||
| ) | ||
| })?; | ||
| if ordering.len() != sort_expr_count { | ||
| return internal_err!( | ||
| "Range partitioning ordering must not contain duplicate expressions" | ||
| ); | ||
| } | ||
| let split_points = range | ||
| .split_point | ||
| .iter() | ||
| .map(|split_point| { | ||
| let values = split_point | ||
| .value | ||
| .iter() | ||
| .map(|value| ScalarValue::try_from(value).map_err(Into::into)) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| Ok(SplitPoint::new(values)) | ||
| }) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| Partitioning::Range(RangePartitioning::try_new(ordering, split_points)?) | ||
| } | ||
| }; | ||
| Ok(Some(partitioning)) | ||
| } | ||
| } | ||
|
|
||
| /// Narrow a wire partition count to `usize`. | ||
| #[cfg(feature = "proto")] | ||
| fn partition_count(count: u64) -> Result<usize> { | ||
| usize::try_from(count).map_err(|_| { | ||
| datafusion_common::internal_datafusion_err!( | ||
| "Partition count {count} exceeds usize::MAX" | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| /// Widen a partition count to its `u64` wire representation. | ||
| /// | ||
| /// The mirror of [`partition_count`]: an out-of-range count is an error on both | ||
| /// sides rather than a silent truncation on the way out. | ||
| #[cfg(feature = "proto")] | ||
| fn wire_partition_count(count: usize) -> Result<u64> { | ||
| u64::try_from(count).map_err(|_| { | ||
| datafusion_common::internal_datafusion_err!( | ||
| "Partition count {count} exceeds u64::MAX" | ||
| ) | ||
| }) | ||
| } | ||
|
|
||
| impl PartialEq for Partitioning { | ||
| fn eq(&self, other: &Partitioning) -> bool { | ||
| match (self, other) { | ||
|
|
@@ -1138,3 +1292,113 @@ mod tests { | |
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(test, feature = "proto"))] | ||
| mod ordering_proto_tests { | ||
| use std::sync::Arc; | ||
|
|
||
| use arrow::compute::SortOptions; | ||
| use arrow::datatypes::{DataType, Field, Schema}; | ||
| use datafusion_physical_expr_common::physical_expr::proto_decode::PhysicalExprDecodeCtx; | ||
| use datafusion_physical_expr_common::physical_expr::proto_encode::PhysicalExprEncodeCtx; | ||
| use datafusion_physical_expr_common::sort_expr::{ | ||
| LexRequirement, PhysicalSortExpr, PhysicalSortRequirement, | ||
| sort_exprs_try_from_proto, sort_exprs_try_to_proto, | ||
| }; | ||
|
|
||
| use crate::expressions::Column; | ||
| use crate::proto_test_util::{StubDecoder, StubEncoder}; | ||
|
|
||
| fn schema() -> Schema { | ||
| Schema::new(vec![Field::new("a", DataType::Int32, false)]) | ||
| } | ||
|
|
||
| fn sort_expr(descending: bool, nulls_first: bool) -> PhysicalSortExpr { | ||
| PhysicalSortExpr::new( | ||
| Arc::new(Column::new("a", 0)), | ||
| SortOptions { | ||
| descending, | ||
| nulls_first, | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn sort_exprs_round_trip_preserves_options_and_order() { | ||
| let encoder = StubEncoder::ok(); | ||
| let encode_ctx = PhysicalExprEncodeCtx::new(&encoder); | ||
| let exprs = vec![sort_expr(true, false), sort_expr(false, true)]; | ||
|
|
||
| let nodes = sort_exprs_try_to_proto(&exprs, &encode_ctx).unwrap(); | ||
| // `asc` is the inverse of `descending` on the wire. | ||
| assert_eq!( | ||
| nodes | ||
| .iter() | ||
| .map(|node| (node.asc, node.nulls_first)) | ||
| .collect::<Vec<_>>(), | ||
| vec![(false, false), (true, true)] | ||
| ); | ||
|
|
||
| let schema = schema(); | ||
| let decoder = StubDecoder::ok(); | ||
| let decode_ctx = PhysicalExprDecodeCtx::new(&schema, &decoder); | ||
| let decoded = sort_exprs_try_from_proto(&nodes, &decode_ctx).unwrap(); | ||
| assert_eq!( | ||
| decoded.iter().map(|expr| expr.options).collect::<Vec<_>>(), | ||
| exprs.iter().map(|expr| expr.options).collect::<Vec<_>>() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn sort_exprs_accepts_owned_requirements() { | ||
| let encoder = StubEncoder::ok(); | ||
| let encode_ctx = PhysicalExprEncodeCtx::new(&encoder); | ||
| let requirement = LexRequirement::from([PhysicalSortRequirement::new( | ||
| Arc::new(Column::new("a", 0)), | ||
| Some(SortOptions { | ||
| descending: true, | ||
| nulls_first: true, | ||
| }), | ||
| )]); | ||
|
|
||
| let nodes = sort_exprs_try_to_proto( | ||
| requirement | ||
| .iter() | ||
| .map(|req| PhysicalSortExpr::from(req.clone())), | ||
| &encode_ctx, | ||
| ) | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(nodes.len(), 1); | ||
| assert!(!nodes[0].asc); | ||
| assert!(nodes[0].nulls_first); | ||
| } | ||
|
|
||
| #[test] | ||
| fn sort_exprs_propagate_encode_errors() { | ||
| let encoder = StubEncoder::failing_on(2); | ||
| let encode_ctx = PhysicalExprEncodeCtx::new(&encoder); | ||
| let exprs = vec![sort_expr(false, false), sort_expr(true, true)]; | ||
|
|
||
| let err = sort_exprs_try_to_proto(&exprs, &encode_ctx).unwrap_err(); | ||
| assert!(err.to_string().contains("stub encode failure on call 2")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn sort_exprs_reject_missing_inner_expr() { | ||
| let encoder = StubEncoder::ok(); | ||
| let encode_ctx = PhysicalExprEncodeCtx::new(&encoder); | ||
| let mut nodes = | ||
| sort_exprs_try_to_proto(&[sort_expr(false, false)], &encode_ctx).unwrap(); | ||
| nodes[0].expr = None; | ||
|
|
||
| let schema = schema(); | ||
| let decoder = StubDecoder::ok(); | ||
| let decode_ctx = PhysicalExprDecodeCtx::new(&schema, &decoder); | ||
| let err = sort_exprs_try_from_proto(&nodes, &decode_ctx).unwrap_err(); | ||
| assert!( | ||
| err.to_string() | ||
| .contains("PhysicalSortExpr is missing required field 'expr'") | ||
| ); | ||
| } | ||
| } | ||
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.
Fixed. Encode now goes through a
wire_partition_counthelper (u64::try_from, internal error on overflow), mirroring thepartition_countnarrowing on the decode side, so an out-of-range count is an error in both directions rather than a silent truncation. Applied to all three sites (round-robin, hash, unknown).