Apache DataFusion SQL Query Engine
Serialize preserve_partitioning in SortExec (#5306)
Dan Harrisac2222a02120
diff --git a/datafusion/proto/proto/datafusion.proto b/datafusion/proto/proto/datafusion.protoindex 8a9a8c0f6d..3bcd7ad76a 100644--- a/datafusion/proto/proto/datafusion.proto+++ b/datafusion/proto/proto/datafusion.proto@@ -1266,6 +1266,7 @@ message SortExecNode {repeated PhysicalExprNode expr = 2;// Maximum number of highest/lowest rows to fetch; negative means no limitint64 fetch = 3;+ bool preserve_partitioning = 4;}message SortPreservingMergeExecNode {diff --git a/datafusion/proto/src/generated/pbjson.rs b/datafusion/proto/src/generated/pbjson.rsindex 65d7af706b..7437fc2384 100644--- a/datafusion/proto/src/generated/pbjson.rs+++ b/datafusion/proto/src/generated/pbjson.rs@@ -18755,6 +18755,9 @@ impl serde::Serialize for SortExecNode {if self.fetch != 0 {len += 1;}+ if self.preserve_partitioning {+ len += 1;+ }let mut struct_ser = serializer.serialize_struct("datafusion.SortExecNode", len)?;if let Some(v) = self.input.as_ref() {struct_ser.serialize_field("input", v)?;@@ -18765,6 +18768,9 @@ impl serde::Serialize for SortExecNode {if self.fetch != 0 {struct_ser.serialize_field("fetch", ToString::to_string(&self.fetch).as_str())?;}+ if self.preserve_partitioning {+ struct_ser.serialize_field("preservePartitioning", &self.preserve_partitioning)?;+ }struct_ser.end()}}@@ -18778,6 +18784,8 @@ impl<'de> serde::Deserialize<'de> for SortExecNode {"input","expr","fetch",+ "preserve_partitioning",+ "preservePartitioning",];#[allow(clippy::enum_variant_names)]@@ -18785,6 +18793,7 @@ impl<'de> serde::Deserialize<'de> for SortExecNode {Input,Expr,Fetch,+ PreservePartitioning,}impl<'de> serde::Deserialize<'de> for GeneratedField {fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>@@ -18809,6 +18818,7 @@ impl<'de> serde::Deserialize<'de> for SortExecNode {"input" => Ok(GeneratedField::Input),"expr" => Ok(GeneratedField::Expr),"fetch" => Ok(GeneratedField::Fetch),+ "preservePartitioning" | "preserve_partitioning" => Ok(GeneratedField::PreservePartitioning),_ => Err(serde::de::Error::unknown_field(value, FIELDS)),}}@@ -18831,6 +18841,7 @@ impl<'de> serde::Deserialize<'de> for SortExecNode {let mut input__ = None;let mut expr__ = None;let mut fetch__ = None;+ let mut preserve_partitioning__ = None;while let Some(k) = map.next_key()? {match k {GeneratedField::Input => {@@ -18853,12 +18864,19 @@ impl<'de> serde::Deserialize<'de> for SortExecNode {Some(map.next_value::<::pbjson::private::NumberDeserialize<_>>()?.0);}+ GeneratedField::PreservePartitioning => {+ if preserve_partitioning__.is_some() {+ return Err(serde::de::Error::duplicate_field("preservePartitioning"));+ }+ preserve_partitioning__ = Some(map.next_value()?);+ }}}Ok(SortExecNode {input: input__,expr: expr__.unwrap_or_default(),fetch: fetch__.unwrap_or_default(),+ preserve_partitioning: preserve_partitioning__.unwrap_or_default(),})}}diff --git a/datafusion/proto/src/generated/prost.rs b/datafusion/proto/src/generated/prost.rsindex 21816f5a90..67bddc23a1 100644--- a/datafusion/proto/src/generated/prost.rs+++ b/datafusion/proto/src/generated/prost.rs@@ -1826,6 +1826,8 @@ pub struct SortExecNode {/// Maximum number of highest/lowest rows to fetch; negative means no limit#[prost(int64, tag = "3")]pub fetch: i64,+ #[prost(bool, tag = "4")]+ pub preserve_partitioning: bool,}#[allow(clippy::derive_partial_eq_without_eq)]#[derive(Clone, PartialEq, ::prost::Message)]diff --git a/datafusion/proto/src/physical_plan/mod.rs b/datafusion/proto/src/physical_plan/mod.rsindex 4c0f701497..9647dbdd21 100644--- a/datafusion/proto/src/physical_plan/mod.rs+++ b/datafusion/proto/src/physical_plan/mod.rs@@ -608,7 +608,12 @@ impl AsExecutionPlan for PhysicalPlanNode {} else {Some(sort.fetch as usize)};- Ok(Arc::new(SortExec::try_new(exprs, input, fetch)?))+ Ok(Arc::new(SortExec::new_with_partitioning(+ exprs,+ input,+ sort.preserve_partitioning,+ fetch,+ )))}PhysicalPlanType::SortPreservingMerge(sort) => {let input: Arc<dyn ExecutionPlan> =@@ -1043,6 +1048,7 @@ impl AsExecutionPlan for PhysicalPlanNode {Some(n) => n as i64,_ => -1,},+ preserve_partitioning: exec.preserve_partitioning(),},))),})@@ -1441,6 +1447,43 @@ mod roundtrip_tests {)?))}+ #[test]+ fn roundtrip_sort_preserve_partitioning() -> Result<()> {+ let field_a = Field::new("a", DataType::Boolean, false);+ let field_b = Field::new("b", DataType::Int64, false);+ let schema = Arc::new(Schema::new(vec![field_a, field_b]));+ let sort_exprs = vec![+ PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: SortOptions {+ descending: true,+ nulls_first: false,+ },+ },+ PhysicalSortExpr {+ expr: col("b", &schema)?,+ options: SortOptions {+ descending: false,+ nulls_first: true,+ },+ },+ ];++ roundtrip_test(Arc::new(SortExec::new_with_partitioning(+ sort_exprs.clone(),+ Arc::new(EmptyExec::new(false, schema.clone())),+ false,+ None,+ )))?;++ roundtrip_test(Arc::new(SortExec::new_with_partitioning(+ sort_exprs,+ Arc::new(EmptyExec::new(false, schema)),+ true,+ None,+ )))+ }+#[test]fn roundtrip_parquet_exec_with_pruning_predicate() -> Result<()> {let scan_config = FileScanConfig {