Apache DataFusion SQL Query Engine
Partial Sort Plan Implementation (#9125)
ahmet enisc9e4b7b7c5c2
diff --git a/datafusion/core/src/physical_optimizer/enforce_sorting.rs b/datafusion/core/src/physical_optimizer/enforce_sorting.rsindex 5c46e64a22..9b76af2dbb 100644--- a/datafusion/core/src/physical_optimizer/enforce_sorting.rs+++ b/datafusion/core/src/physical_optimizer/enforce_sorting.rs@@ -63,6 +63,8 @@ use datafusion_common::tree_node::{Transformed, TreeNode};use datafusion_common::{plan_err, DataFusionError};use datafusion_physical_expr::{PhysicalSortExpr, PhysicalSortRequirement};use datafusion_physical_plan::repartition::RepartitionExec;+use datafusion_physical_plan::sorts::partial_sort::PartialSortExec;+use datafusion_physical_plan::unbounded_output;use itertools::izip;@@ -185,7 +187,10 @@ impl PhysicalOptimizerRule for EnforceSorting {let mut sort_pushdown = SortPushDown::new_default(updated_plan.plan);assign_initial_requirements(&mut sort_pushdown);let adjusted = sort_pushdown.transform_down(&pushdown_sorts)?;- Ok(adjusted.plan)++ adjusted+ .plan+ .transform_up(&|plan| Ok(Transformed::Yes(replace_with_partial_sort(plan)?)))}fn name(&self) -> &str {@@ -197,6 +202,42 @@ impl PhysicalOptimizerRule for EnforceSorting {}}+fn replace_with_partial_sort(+ plan: Arc<dyn ExecutionPlan>,+) -> Result<Arc<dyn ExecutionPlan>> {+ let plan_any = plan.as_any();+ if let Some(sort_plan) = plan_any.downcast_ref::<SortExec>() {+ let child = sort_plan.children()[0].clone();+ if !unbounded_output(&child) {+ return Ok(plan);+ }++ // here we're trying to find the common prefix for sorted columns that is required for the+ // sort and already satisfied by the given ordering+ let child_eq_properties = child.equivalence_properties();+ let sort_req = PhysicalSortRequirement::from_sort_exprs(sort_plan.expr());++ let mut common_prefix_length = 0;+ while child_eq_properties+ .ordering_satisfy_requirement(&sort_req[0..common_prefix_length + 1])+ {+ common_prefix_length += 1;+ }+ if common_prefix_length > 0 {+ return Ok(Arc::new(+ PartialSortExec::new(+ sort_plan.expr().to_vec(),+ sort_plan.input().clone(),+ common_prefix_length,+ )+ .with_preserve_partitioning(sort_plan.preserve_partitioning())+ .with_fetch(sort_plan.fetch()),+ ));+ }+ }+ Ok(plan)+}+/// This function turns plans of the form/// ```text/// "SortExec: expr=\[a@0 ASC\]",@@ -2205,4 +2246,101 @@ mod tests {Ok(())}++ #[tokio::test]+ async fn test_replace_with_partial_sort() -> Result<()> {+ let schema = create_test_schema3()?;+ let input_sort_exprs = vec![sort_expr("a", &schema)];+ let unbounded_input = stream_exec_ordered(&schema, input_sort_exprs);++ let physical_plan = sort_exec(+ vec![sort_expr("a", &schema), sort_expr("c", &schema)],+ unbounded_input,+ );++ let expected_input = [+ "SortExec: expr=[a@0 ASC,c@2 ASC]",+ " StreamingTableExec: partition_sizes=1, projection=[a, b, c, d, e], infinite_source=true, output_ordering=[a@0 ASC]"+ ];+ let expected_optimized = [+ "PartialSortExec: expr=[a@0 ASC,c@2 ASC], common_prefix_length=[1]",+ " StreamingTableExec: partition_sizes=1, projection=[a, b, c, d, e], infinite_source=true, output_ordering=[a@0 ASC]",+ ];+ assert_optimized!(expected_input, expected_optimized, physical_plan, true);+ Ok(())+ }++ #[tokio::test]+ async fn test_replace_with_partial_sort2() -> Result<()> {+ let schema = create_test_schema3()?;+ let input_sort_exprs = vec![sort_expr("a", &schema), sort_expr("c", &schema)];+ let unbounded_input = stream_exec_ordered(&schema, input_sort_exprs);++ let physical_plan = sort_exec(+ vec![+ sort_expr("a", &schema),+ sort_expr("c", &schema),+ sort_expr("d", &schema),+ ],+ unbounded_input,+ );++ let expected_input = [+ "SortExec: expr=[a@0 ASC,c@2 ASC,d@3 ASC]",+ " StreamingTableExec: partition_sizes=1, projection=[a, b, c, d, e], infinite_source=true, output_ordering=[a@0 ASC, c@2 ASC]"+ ];+ // let optimized+ let expected_optimized = [+ "PartialSortExec: expr=[a@0 ASC,c@2 ASC,d@3 ASC], common_prefix_length=[2]",+ " StreamingTableExec: partition_sizes=1, projection=[a, b, c, d, e], infinite_source=true, output_ordering=[a@0 ASC, c@2 ASC]",+ ];+ assert_optimized!(expected_input, expected_optimized, physical_plan, true);+ Ok(())+ }++ #[tokio::test]+ async fn test_not_replaced_with_partial_sort_for_bounded_input() -> Result<()> {+ let schema = create_test_schema3()?;+ let input_sort_exprs = vec![sort_expr("b", &schema), sort_expr("c", &schema)];+ let parquet_input = parquet_exec_sorted(&schema, input_sort_exprs);++ let physical_plan = sort_exec(+ vec![+ sort_expr("a", &schema),+ sort_expr("b", &schema),+ sort_expr("c", &schema),+ ],+ parquet_input,+ );+ let expected_input = [+ "SortExec: expr=[a@0 ASC,b@1 ASC,c@2 ASC]",+ " ParquetExec: file_groups={1 group: [[x]]}, projection=[a, b, c, d, e], output_ordering=[b@1 ASC, c@2 ASC]"+ ];+ let expected_no_change = expected_input;+ assert_optimized!(expected_input, expected_no_change, physical_plan, false);+ Ok(())+ }++ #[tokio::test]+ async fn test_not_replaced_with_partial_sort_for_unbounded_input() -> Result<()> {+ let schema = create_test_schema3()?;+ let input_sort_exprs = vec![sort_expr("b", &schema), sort_expr("c", &schema)];+ let unbounded_input = stream_exec_ordered(&schema, input_sort_exprs);++ let physical_plan = sort_exec(+ vec![+ sort_expr("a", &schema),+ sort_expr("b", &schema),+ sort_expr("c", &schema),+ ],+ unbounded_input,+ );+ let expected_input = [+ "SortExec: expr=[a@0 ASC,b@1 ASC,c@2 ASC]",+ " StreamingTableExec: partition_sizes=1, projection=[a, b, c, d, e], infinite_source=true, output_ordering=[b@1 ASC, c@2 ASC]"+ ];+ let expected_no_change = expected_input;+ assert_optimized!(expected_input, expected_no_change, physical_plan, true);+ Ok(())+ }}diff --git a/datafusion/physical-plan/src/sorts/mod.rs b/datafusion/physical-plan/src/sorts/mod.rsindex 8a1184d3c2..7c084761fd 100644--- a/datafusion/physical-plan/src/sorts/mod.rs+++ b/datafusion/physical-plan/src/sorts/mod.rs@@ -21,6 +21,7 @@ mod builder;mod cursor;mod index;mod merge;+pub mod partial_sort;pub mod sort;pub mod sort_preserving_merge;mod stream;diff --git a/datafusion/physical-plan/src/sorts/partial_sort.rs b/datafusion/physical-plan/src/sorts/partial_sort.rsnew file mode 100644index 0000000000..563a82f3ea--- /dev/null+++ b/datafusion/physical-plan/src/sorts/partial_sort.rs@@ -0,0 +1,992 @@+// 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.++//! Partial Sort deals with input data that partially+//! satisfies the required sort order. Such an input data can be+//! partitioned into segments where each segment already has the+//! required information for lexicographic sorting so sorting+//! can be done without loading the entire dataset.+//!+//! Consider a sort plan having an input with ordering `a ASC, b ASC`+//!+//! ```text+//! +---+---+---++//! | a | b | d |+//! +---+---+---++//! | 0 | 0 | 3 |+//! | 0 | 0 | 2 |+//! | 0 | 1 | 1 |+//! | 0 | 2 | 0 |+//! +---+---+---++//!```+//!+//! and required ordering for the plan is `a ASC, b ASC, d ASC`.+//! The first 3 rows(segment) can be sorted as the segment already+//! has the required information for the sort, but the last row+//! requires further information as the input can continue with a+//! batch with a starting row where a and b does not change as below+//!+//! ```text+//! +---+---+---++//! | a | b | d |+//! +---+---+---++//! | 0 | 2 | 4 |+//! +---+---+---++//!```+//!+//! The plan concats incoming data with such last rows of previous input+//! and continues partial sorting of the segments.++use std::any::Any;+use std::fmt::Debug;+use std::pin::Pin;+use std::sync::Arc;+use std::task::{Context, Poll};++use arrow::compute::concat_batches;+use arrow::datatypes::SchemaRef;+use arrow::record_batch::RecordBatch;+use futures::{ready, Stream, StreamExt};+use log::trace;++use datafusion_common::utils::evaluate_partition_ranges;+use datafusion_common::Result;+use datafusion_execution::{RecordBatchStream, TaskContext};+use datafusion_physical_expr::EquivalenceProperties;++use crate::expressions::PhysicalSortExpr;+use crate::metrics::{BaselineMetrics, ExecutionPlanMetricsSet, MetricsSet};+use crate::sorts::sort::sort_batch;+use crate::{+ DisplayAs, DisplayFormatType, Distribution, ExecutionPlan, Partitioning,+ SendableRecordBatchStream, Statistics,+};++/// Partial Sort execution plan.+#[derive(Debug, Clone)]+pub struct PartialSortExec {+ /// Input schema+ pub(crate) input: Arc<dyn ExecutionPlan>,+ /// Sort expressions+ expr: Vec<PhysicalSortExpr>,+ /// Length of continuous matching columns of input that satisfy+ /// the required ordering for the sort+ common_prefix_length: usize,+ /// Containing all metrics set created during sort+ metrics_set: ExecutionPlanMetricsSet,+ /// Preserve partitions of input plan. If false, the input partitions+ /// will be sorted and merged into a single output partition.+ preserve_partitioning: bool,+ /// Fetch highest/lowest n results+ fetch: Option<usize>,+}++impl PartialSortExec {+ /// Create a new partial sort execution plan+ pub fn new(+ expr: Vec<PhysicalSortExpr>,+ input: Arc<dyn ExecutionPlan>,+ common_prefix_length: usize,+ ) -> Self {+ assert!(common_prefix_length > 0);+ Self {+ input,+ expr,+ common_prefix_length,+ metrics_set: ExecutionPlanMetricsSet::new(),+ preserve_partitioning: false,+ fetch: None,+ }+ }++ /// Whether this `PartialSortExec` preserves partitioning of the children+ pub fn preserve_partitioning(&self) -> bool {+ self.preserve_partitioning+ }++ /// Specify the partitioning behavior of this partial sort exec+ ///+ /// If `preserve_partitioning` is true, sorts each partition+ /// individually, producing one sorted stream for each input partition.+ ///+ /// If `preserve_partitioning` is false, sorts and merges all+ /// input partitions producing a single, sorted partition.+ pub fn with_preserve_partitioning(mut self, preserve_partitioning: bool) -> Self {+ self.preserve_partitioning = preserve_partitioning;+ self+ }++ /// Modify how many rows to include in the result+ ///+ /// If None, then all rows will be returned, in sorted order.+ /// If Some, then only the top `fetch` rows will be returned.+ /// This can reduce the memory pressure required by the sort+ /// operation since rows that are not going to be included+ /// can be dropped.+ pub fn with_fetch(mut self, fetch: Option<usize>) -> Self {+ self.fetch = fetch;+ self+ }++ /// Input schema+ pub fn input(&self) -> &Arc<dyn ExecutionPlan> {+ &self.input+ }++ /// Sort expressions+ pub fn expr(&self) -> &[PhysicalSortExpr] {+ &self.expr+ }++ /// If `Some(fetch)`, limits output to only the first "fetch" items+ pub fn fetch(&self) -> Option<usize> {+ self.fetch+ }+}++impl DisplayAs for PartialSortExec {+ fn fmt_as(+ &self,+ t: DisplayFormatType,+ f: &mut std::fmt::Formatter,+ ) -> std::fmt::Result {+ match t {+ DisplayFormatType::Default | DisplayFormatType::Verbose => {+ let expr = PhysicalSortExpr::format_list(&self.expr);+ let common_prefix_length = self.common_prefix_length;+ match self.fetch {+ Some(fetch) => {+ write!(f, "PartialSortExec: TopK(fetch={fetch}), expr=[{expr}], common_prefix_length=[{common_prefix_length}]", )+ }+ None => write!(f, "PartialSortExec: expr=[{expr}], common_prefix_length=[{common_prefix_length}]"),+ }+ }+ }+ }+}++impl ExecutionPlan for PartialSortExec {+ fn as_any(&self) -> &dyn Any {+ self+ }++ fn schema(&self) -> SchemaRef {+ self.input.schema()+ }++ /// Get the output partitioning of this plan+ fn output_partitioning(&self) -> Partitioning {+ if self.preserve_partitioning {+ self.input.output_partitioning()+ } else {+ Partitioning::UnknownPartitioning(1)+ }+ }++ /// Specifies whether this plan generates an infinite stream of records.+ /// If the plan does not support pipelining, but its input(s) are+ /// infinite, returns an error to indicate this.+ fn unbounded_output(&self, children: &[bool]) -> Result<bool> {+ Ok(children[0])+ }++ fn output_ordering(&self) -> Option<&[PhysicalSortExpr]> {+ Some(&self.expr)+ }++ fn required_input_distribution(&self) -> Vec<Distribution> {+ if self.preserve_partitioning {+ vec![Distribution::UnspecifiedDistribution]+ } else {+ vec![Distribution::SinglePartition]+ }+ }++ fn benefits_from_input_partitioning(&self) -> Vec<bool> {+ vec![false]+ }++ fn equivalence_properties(&self) -> EquivalenceProperties {+ // Reset the ordering equivalence class with the new ordering:+ self.input+ .equivalence_properties()+ .with_reorder(self.expr.to_vec())+ }++ fn children(&self) -> Vec<Arc<dyn ExecutionPlan>> {+ vec![self.input.clone()]+ }++ fn with_new_children(+ self: Arc<Self>,+ children: Vec<Arc<dyn ExecutionPlan>>,+ ) -> Result<Arc<dyn ExecutionPlan>> {+ let new_partial_sort = PartialSortExec::new(+ self.expr.clone(),+ children[0].clone(),+ self.common_prefix_length,+ )+ .with_fetch(self.fetch)+ .with_preserve_partitioning(self.preserve_partitioning);++ Ok(Arc::new(new_partial_sort))+ }++ fn execute(+ &self,+ partition: usize,+ context: Arc<TaskContext>,+ ) -> Result<SendableRecordBatchStream> {+ trace!("Start PartialSortExec::execute for partition {} of context session_id {} and task_id {:?}", partition, context.session_id(), context.task_id());++ let input = self.input.execute(partition, context.clone())?;++ trace!(+ "End PartialSortExec's input.execute for partition: {}",+ partition+ );++ // Make sure common prefix length is larger than 0+ // Otherwise, we should use SortExec.+ assert!(self.common_prefix_length > 0);++ Ok(Box::pin(PartialSortStream {+ input,+ expr: self.expr.clone(),+ common_prefix_length: self.common_prefix_length,+ in_mem_batches: vec![],+ fetch: self.fetch,+ is_closed: false,+ baseline_metrics: BaselineMetrics::new(&self.metrics_set, partition),+ }))+ }++ fn metrics(&self) -> Option<MetricsSet> {+ Some(self.metrics_set.clone_inner())+ }++ fn statistics(&self) -> Result<Statistics> {+ self.input.statistics()+ }+}++struct PartialSortStream {+ /// The input plan+ input: SendableRecordBatchStream,+ /// Sort expressions+ expr: Vec<PhysicalSortExpr>,+ /// Length of prefix common to input ordering and required ordering of plan+ /// should be more than 0 otherwise PartialSort is not applicable+ common_prefix_length: usize,+ /// Used as a buffer for part of the input not ready for sort+ in_mem_batches: Vec<RecordBatch>,+ /// Fetch top N results+ fetch: Option<usize>,+ /// Whether the stream has finished returning all of its data or not+ is_closed: bool,+ /// Execution metrics+ baseline_metrics: BaselineMetrics,+}++impl Stream for PartialSortStream {+ type Item = Result<RecordBatch>;++ fn poll_next(+ mut self: Pin<&mut Self>,+ cx: &mut Context<'_>,+ ) -> Poll<Option<Self::Item>> {+ let poll = self.poll_next_inner(cx);+ self.baseline_metrics.record_poll(poll)+ }++ fn size_hint(&self) -> (usize, Option<usize>) {+ // we can't predict the size of incoming batches so re-use the size hint from the input+ self.input.size_hint()+ }+}++impl RecordBatchStream for PartialSortStream {+ fn schema(&self) -> SchemaRef {+ self.input.schema()+ }+}++impl PartialSortStream {+ fn poll_next_inner(+ self: &mut Pin<&mut Self>,+ cx: &mut Context<'_>,+ ) -> Poll<Option<Result<RecordBatch>>> {+ if self.is_closed {+ return Poll::Ready(None);+ }+ let result = match ready!(self.input.poll_next_unpin(cx)) {+ Some(Ok(batch)) => {+ if let Some(slice_point) =+ self.get_slice_point(self.common_prefix_length, &batch)?+ {+ self.in_mem_batches.push(batch.slice(0, slice_point));+ let remaining_batch =+ batch.slice(slice_point, batch.num_rows() - slice_point);+ let sorted_batch = self.sort_in_mem_batches();+ self.in_mem_batches.push(remaining_batch);+ sorted_batch+ } else {+ self.in_mem_batches.push(batch);+ Ok(RecordBatch::new_empty(self.schema()))+ }+ }+ Some(Err(e)) => Err(e),+ None => {+ self.is_closed = true;+ // once input is consumed, sort the rest of the inserted batches+ self.sort_in_mem_batches()+ }+ };++ Poll::Ready(Some(result))+ }++ /// Returns a sorted RecordBatch from in_mem_batches and clears in_mem_batches+ ///+ /// If fetch is specified for PartialSortStream `sort_in_mem_batches` will limit+ /// the last RecordBatch returned and will mark the stream as closed+ fn sort_in_mem_batches(self: &mut Pin<&mut Self>) -> Result<RecordBatch> {+ let input_batch = concat_batches(&self.schema(), &self.in_mem_batches)?;+ self.in_mem_batches.clear();+ let result = sort_batch(&input_batch, &self.expr, self.fetch)?;+ if let Some(remaining_fetch) = self.fetch {+ // remaining_fetch - result.num_rows() is always be >= 0+ // because result length of sort_batch with limit cannot be+ // more than the requested limit+ self.fetch = Some(remaining_fetch - result.num_rows());+ if remaining_fetch == result.num_rows() {+ self.is_closed = true;+ }+ }+ Ok(result)+ }++ /// Return the end index of the second last partition if the batch+ /// can be partitioned based on its already sorted columns+ ///+ /// Return None if the batch cannot be partitioned, which means the+ /// batch does not have the information for a safe sort+ fn get_slice_point(+ &self,+ common_prefix_len: usize,+ batch: &RecordBatch,+ ) -> Result<Option<usize>> {+ let common_prefix_sort_keys = (0..common_prefix_len)+ .map(|idx| self.expr[idx].evaluate_to_sort_column(batch))+ .collect::<Result<Vec<_>>>()?;+ let partition_points =+ evaluate_partition_ranges(batch.num_rows(), &common_prefix_sort_keys)?;+ // If partition points are [0..100], [100..200], [200..300]+ // we should return 200, which is the safest and furthest partition boundary+ // Please note that we shouldn't return 300 (which is number of rows in the batch),+ // because this boundary may change with new data.+ if partition_points.len() >= 2 {+ Ok(Some(partition_points[partition_points.len() - 2].end))+ } else {+ Ok(None)+ }+ }+}++#[cfg(test)]+mod tests {+ use std::collections::HashMap;++ use arrow::array::*;+ use arrow::compute::SortOptions;+ use arrow::datatypes::*;+ use futures::FutureExt;+ use itertools::Itertools;++ use datafusion_common::assert_batches_eq;++ use crate::collect;+ use crate::expressions::col;+ use crate::memory::MemoryExec;+ use crate::sorts::sort::SortExec;+ use crate::test;+ use crate::test::assert_is_pending;+ use crate::test::exec::{assert_strong_count_converges_to_zero, BlockingExec};++ use super::*;++ #[tokio::test]+ async fn test_partial_sort() -> Result<()> {+ let task_ctx = Arc::new(TaskContext::default());+ let source = test::build_table_scan_i32(+ ("a", &vec![0, 0, 0, 1, 1, 1]),+ ("b", &vec![1, 1, 2, 2, 3, 3]),+ ("c", &vec![1, 0, 5, 4, 3, 2]),+ );+ let schema = Schema::new(vec![+ Field::new("a", DataType::Int32, false),+ Field::new("b", DataType::Int32, false),+ Field::new("c", DataType::Int32, false),+ ]);+ let option_asc = SortOptions {+ descending: false,+ nulls_first: false,+ };++ let partial_sort_exec = Arc::new(PartialSortExec::new(+ vec![+ PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("b", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("c", &schema)?,+ options: option_asc,+ },+ ],+ source.clone(),+ 2,+ )) as Arc<dyn ExecutionPlan>;++ let result = collect(partial_sort_exec, task_ctx.clone()).await?;++ let expected_after_sort = [+ "+---+---+---+",+ "| a | b | c |",+ "+---+---+---+",+ "| 0 | 1 | 0 |",+ "| 0 | 1 | 1 |",+ "| 0 | 2 | 5 |",+ "| 1 | 2 | 4 |",+ "| 1 | 3 | 2 |",+ "| 1 | 3 | 3 |",+ "+---+---+---+",+ ];+ assert_eq!(2, result.len());+ assert_batches_eq!(expected_after_sort, &result);+ assert_eq!(+ task_ctx.runtime_env().memory_pool.reserved(),+ 0,+ "The sort should have returned all memory used back to the memory manager"+ );++ Ok(())+ }++ #[tokio::test]+ async fn test_partial_sort_with_fetch() -> Result<()> {+ let task_ctx = Arc::new(TaskContext::default());+ let source = test::build_table_scan_i32(+ ("a", &vec![0, 0, 1, 1, 1]),+ ("b", &vec![1, 2, 2, 3, 3]),+ ("c", &vec![4, 3, 2, 1, 0]),+ );+ let schema = Schema::new(vec![+ Field::new("a", DataType::Int32, false),+ Field::new("b", DataType::Int32, false),+ Field::new("c", DataType::Int32, false),+ ]);+ let option_asc = SortOptions {+ descending: false,+ nulls_first: false,+ };++ for common_prefix_length in [1, 2] {+ let partial_sort_exec = Arc::new(+ PartialSortExec::new(+ vec![+ PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("b", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("c", &schema)?,+ options: option_asc,+ },+ ],+ source.clone(),+ common_prefix_length,+ )+ .with_fetch(Some(4)),+ ) as Arc<dyn ExecutionPlan>;++ let result = collect(partial_sort_exec, task_ctx.clone()).await?;++ let expected_after_sort = [+ "+---+---+---+",+ "| a | b | c |",+ "+---+---+---+",+ "| 0 | 1 | 4 |",+ "| 0 | 2 | 3 |",+ "| 1 | 2 | 2 |",+ "| 1 | 3 | 0 |",+ "+---+---+---+",+ ];+ assert_eq!(2, result.len());+ assert_batches_eq!(expected_after_sort, &result);+ assert_eq!(+ task_ctx.runtime_env().memory_pool.reserved(),+ 0,+ "The sort should have returned all memory used back to the memory manager"+ );+ }++ Ok(())+ }++ #[tokio::test]+ async fn test_partial_sort2() -> Result<()> {+ let task_ctx = Arc::new(TaskContext::default());+ let source_tables = vec![+ test::build_table_scan_i32(+ ("a", &vec![0, 0, 0, 0, 1, 1, 1, 1]),+ ("b", &vec![1, 1, 3, 3, 4, 4, 2, 2]),+ ("c", &vec![7, 6, 5, 4, 3, 2, 1, 0]),+ ),+ test::build_table_scan_i32(+ ("a", &vec![0, 0, 0, 0, 1, 1, 1, 1]),+ ("b", &vec![1, 1, 3, 3, 2, 2, 4, 4]),+ ("c", &vec![7, 6, 5, 4, 1, 0, 3, 2]),+ ),+ ];+ let schema = Schema::new(vec![+ Field::new("a", DataType::Int32, false),+ Field::new("b", DataType::Int32, false),+ Field::new("c", DataType::Int32, false),+ ]);+ let option_asc = SortOptions {+ descending: false,+ nulls_first: false,+ };+ for (common_prefix_length, source) in+ [(1, &source_tables[0]), (2, &source_tables[1])]+ {+ let partial_sort_exec = Arc::new(PartialSortExec::new(+ vec![+ PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("b", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("c", &schema)?,+ options: option_asc,+ },+ ],+ source.clone(),+ common_prefix_length,+ ));++ let result = collect(partial_sort_exec, task_ctx.clone()).await?;+ assert_eq!(2, result.len());+ assert_eq!(+ task_ctx.runtime_env().memory_pool.reserved(),+ 0,+ "The sort should have returned all memory used back to the memory manager"+ );+ let expected = [+ "+---+---+---+",+ "| a | b | c |",+ "+---+---+---+",+ "| 0 | 1 | 6 |",+ "| 0 | 1 | 7 |",+ "| 0 | 3 | 4 |",+ "| 0 | 3 | 5 |",+ "| 1 | 2 | 0 |",+ "| 1 | 2 | 1 |",+ "| 1 | 4 | 2 |",+ "| 1 | 4 | 3 |",+ "+---+---+---+",+ ];+ assert_batches_eq!(expected, &result);+ }+ Ok(())+ }++ fn prepare_partitioned_input() -> Arc<dyn ExecutionPlan> {+ let batch1 = test::build_table_i32(+ ("a", &vec![1; 100]),+ ("b", &(0..100).rev().collect()),+ ("c", &(0..100).rev().collect()),+ );+ let batch2 = test::build_table_i32(+ ("a", &[&vec![1; 25][..], &vec![2; 75][..]].concat()),+ ("b", &(100..200).rev().collect()),+ ("c", &(0..100).collect()),+ );+ let batch3 = test::build_table_i32(+ ("a", &[&vec![3; 50][..], &vec![4; 50][..]].concat()),+ ("b", &(150..250).rev().collect()),+ ("c", &(0..100).rev().collect()),+ );+ let batch4 = test::build_table_i32(+ ("a", &vec![4; 100]),+ ("b", &(50..150).rev().collect()),+ ("c", &(0..100).rev().collect()),+ );+ let schema = batch1.schema();+ Arc::new(+ MemoryExec::try_new(+ &[vec![batch1, batch2, batch3, batch4]],+ schema.clone(),+ None,+ )+ .unwrap(),+ ) as Arc<dyn ExecutionPlan>+ }++ #[tokio::test]+ async fn test_partitioned_input_partial_sort() -> Result<()> {+ let task_ctx = Arc::new(TaskContext::default());+ let mem_exec = prepare_partitioned_input();+ let option_asc = SortOptions {+ descending: false,+ nulls_first: false,+ };+ let option_desc = SortOptions {+ descending: false,+ nulls_first: false,+ };+ let schema = mem_exec.schema();+ let partial_sort_executor = PartialSortExec::new(+ vec![+ PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("b", &schema)?,+ options: option_desc,+ },+ PhysicalSortExpr {+ expr: col("c", &schema)?,+ options: option_asc,+ },+ ],+ mem_exec.clone(),+ 1,+ );+ let partial_sort_exec =+ Arc::new(partial_sort_executor.clone()) as Arc<dyn ExecutionPlan>;+ let sort_exec = Arc::new(SortExec::new(+ partial_sort_executor.expr,+ partial_sort_executor.input,+ )) as Arc<dyn ExecutionPlan>;+ let result = collect(partial_sort_exec, task_ctx.clone()).await?;+ assert_eq!(+ result.iter().map(|r| r.num_rows()).collect_vec(),+ [0, 125, 125, 0, 150]+ );++ assert_eq!(+ task_ctx.runtime_env().memory_pool.reserved(),+ 0,+ "The sort should have returned all memory used back to the memory manager"+ );+ let partial_sort_result = concat_batches(&schema, &result).unwrap();+ let sort_result = collect(sort_exec, task_ctx.clone()).await?;+ assert_eq!(sort_result[0], partial_sort_result);++ Ok(())+ }++ #[tokio::test]+ async fn test_partitioned_input_partial_sort_with_fetch() -> Result<()> {+ let task_ctx = Arc::new(TaskContext::default());+ let mem_exec = prepare_partitioned_input();+ let schema = mem_exec.schema();+ let option_asc = SortOptions {+ descending: false,+ nulls_first: false,+ };+ let option_desc = SortOptions {+ descending: false,+ nulls_first: false,+ };+ for (fetch_size, expected_batch_num_rows) in [+ (Some(50), vec![0, 50]),+ (Some(120), vec![0, 120]),+ (Some(150), vec![0, 125, 25]),+ (Some(250), vec![0, 125, 125]),+ ] {+ let partial_sort_executor = PartialSortExec::new(+ vec![+ PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("b", &schema)?,+ options: option_desc,+ },+ PhysicalSortExpr {+ expr: col("c", &schema)?,+ options: option_asc,+ },+ ],+ mem_exec.clone(),+ 1,+ )+ .with_fetch(fetch_size);++ let partial_sort_exec =+ Arc::new(partial_sort_executor.clone()) as Arc<dyn ExecutionPlan>;+ let sort_exec = Arc::new(+ SortExec::new(partial_sort_executor.expr, partial_sort_executor.input)+ .with_fetch(fetch_size),+ ) as Arc<dyn ExecutionPlan>;+ let result = collect(partial_sort_exec, task_ctx.clone()).await?;+ assert_eq!(+ result.iter().map(|r| r.num_rows()).collect_vec(),+ expected_batch_num_rows+ );++ assert_eq!(+ task_ctx.runtime_env().memory_pool.reserved(),+ 0,+ "The sort should have returned all memory used back to the memory manager"+ );+ let partial_sort_result = concat_batches(&schema, &result)?;+ let sort_result = collect(sort_exec, task_ctx.clone()).await?;+ assert_eq!(sort_result[0], partial_sort_result);+ }++ Ok(())+ }++ #[tokio::test]+ async fn test_sort_metadata() -> Result<()> {+ let task_ctx = Arc::new(TaskContext::default());+ let field_metadata: HashMap<String, String> =+ vec![("foo".to_string(), "bar".to_string())]+ .into_iter()+ .collect();+ let schema_metadata: HashMap<String, String> =+ vec![("baz".to_string(), "barf".to_string())]+ .into_iter()+ .collect();++ let mut field = Field::new("field_name", DataType::UInt64, true);+ field.set_metadata(field_metadata.clone());+ let schema = Schema::new_with_metadata(vec![field], schema_metadata.clone());+ let schema = Arc::new(schema);++ let data: ArrayRef =+ Arc::new(vec![1, 1, 2].into_iter().map(Some).collect::<UInt64Array>());++ let batch = RecordBatch::try_new(schema.clone(), vec![data])?;+ let input = Arc::new(MemoryExec::try_new(&[vec![batch]], schema.clone(), None)?);++ let partial_sort_exec = Arc::new(PartialSortExec::new(+ vec![PhysicalSortExpr {+ expr: col("field_name", &schema)?,+ options: SortOptions::default(),+ }],+ input,+ 1,+ ));++ let result: Vec<RecordBatch> = collect(partial_sort_exec, task_ctx).await?;+ let expected_batch = vec![+ RecordBatch::try_new(+ schema.clone(),+ vec![Arc::new(+ vec![1, 1].into_iter().map(Some).collect::<UInt64Array>(),+ )],+ )?,+ RecordBatch::try_new(+ schema.clone(),+ vec![Arc::new(+ vec![2].into_iter().map(Some).collect::<UInt64Array>(),+ )],+ )?,+ ];++ // Data is correct+ assert_eq!(&expected_batch, &result);++ // explicitly ensure the metadata is present+ assert_eq!(result[0].schema().fields()[0].metadata(), &field_metadata);+ assert_eq!(result[0].schema().metadata(), &schema_metadata);++ Ok(())+ }++ #[tokio::test]+ async fn test_lex_sort_by_float() -> Result<()> {+ let task_ctx = Arc::new(TaskContext::default());+ let schema = Arc::new(Schema::new(vec![+ Field::new("a", DataType::Float32, true),+ Field::new("b", DataType::Float64, true),+ Field::new("c", DataType::Float64, true),+ ]));+ let option_asc = SortOptions {+ descending: false,+ nulls_first: true,+ };+ let option_desc = SortOptions {+ descending: true,+ nulls_first: true,+ };++ // define data.+ let batch = RecordBatch::try_new(+ schema.clone(),+ vec![+ Arc::new(Float32Array::from(vec![+ Some(1.0_f32),+ Some(1.0_f32),+ Some(1.0_f32),+ Some(2.0_f32),+ Some(2.0_f32),+ Some(3.0_f32),+ Some(3.0_f32),+ Some(3.0_f32),+ ])),+ Arc::new(Float64Array::from(vec![+ Some(20.0_f64),+ Some(20.0_f64),+ Some(40.0_f64),+ Some(40.0_f64),+ Some(f64::NAN),+ None,+ None,+ Some(f64::NAN),+ ])),+ Arc::new(Float64Array::from(vec![+ Some(10.0_f64),+ Some(20.0_f64),+ Some(10.0_f64),+ Some(100.0_f64),+ Some(f64::NAN),+ Some(100.0_f64),+ None,+ Some(f64::NAN),+ ])),+ ],+ )?;++ let partial_sort_exec = Arc::new(PartialSortExec::new(+ vec![+ PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("b", &schema)?,+ options: option_asc,+ },+ PhysicalSortExpr {+ expr: col("c", &schema)?,+ options: option_desc,+ },+ ],+ Arc::new(MemoryExec::try_new(&[vec![batch]], schema, None)?),+ 2,+ ));++ let expected = [+ "+-----+------+-------+",+ "| a | b | c |",+ "+-----+------+-------+",+ "| 1.0 | 20.0 | 20.0 |",+ "| 1.0 | 20.0 | 10.0 |",+ "| 1.0 | 40.0 | 10.0 |",+ "| 2.0 | 40.0 | 100.0 |",+ "| 2.0 | NaN | NaN |",+ "| 3.0 | | |",+ "| 3.0 | | 100.0 |",+ "| 3.0 | NaN | NaN |",+ "+-----+------+-------+",+ ];++ assert_eq!(+ DataType::Float32,+ *partial_sort_exec.schema().field(0).data_type()+ );+ assert_eq!(+ DataType::Float64,+ *partial_sort_exec.schema().field(1).data_type()+ );+ assert_eq!(+ DataType::Float64,+ *partial_sort_exec.schema().field(2).data_type()+ );++ let result: Vec<RecordBatch> =+ collect(partial_sort_exec.clone(), task_ctx).await?;+ assert_batches_eq!(expected, &result);+ assert_eq!(result.len(), 2);+ let metrics = partial_sort_exec.metrics().unwrap();+ assert!(metrics.elapsed_compute().unwrap() > 0);+ assert_eq!(metrics.output_rows().unwrap(), 8);++ let columns = result[0].columns();++ assert_eq!(DataType::Float32, *columns[0].data_type());+ assert_eq!(DataType::Float64, *columns[1].data_type());+ assert_eq!(DataType::Float64, *columns[2].data_type());++ Ok(())+ }++ #[tokio::test]+ async fn test_drop_cancel() -> Result<()> {+ let task_ctx = Arc::new(TaskContext::default());+ let schema = Arc::new(Schema::new(vec![+ Field::new("a", DataType::Float32, true),+ Field::new("b", DataType::Float32, true),+ ]));++ let blocking_exec = Arc::new(BlockingExec::new(Arc::clone(&schema), 1));+ let refs = blocking_exec.refs();+ let sort_exec = Arc::new(PartialSortExec::new(+ vec![PhysicalSortExpr {+ expr: col("a", &schema)?,+ options: SortOptions::default(),+ }],+ blocking_exec,+ 1,+ ));++ let fut = collect(sort_exec, task_ctx.clone());+ let mut fut = fut.boxed();++ assert_is_pending(&mut fut);+ drop(fut);+ assert_strong_count_converges_to_zero(refs).await;++ assert_eq!(+ task_ctx.runtime_env().memory_pool.reserved(),+ 0,+ "The sort should have returned all memory used back to the memory manager"+ );++ Ok(())+ }+}diff --git a/datafusion/sqllogictest/test_files/group_by.slt b/datafusion/sqllogictest/test_files/group_by.sltindex decf40cfa1..213a3f6b52 100644--- a/datafusion/sqllogictest/test_files/group_by.slt+++ b/datafusion/sqllogictest/test_files/group_by.slt@@ -2244,6 +2244,175 @@ SELECT a, b, LAST_VALUE(c) as last_c1 2 741 3 99+query TT+EXPLAIN SELECT *+FROM annotated_data_infinite2+ORDER BY a, b, d;+----+logical_plan+Sort: annotated_data_infinite2.a ASC NULLS LAST, annotated_data_infinite2.b ASC NULLS LAST, annotated_data_infinite2.d ASC NULLS LAST+--TableScan: annotated_data_infinite2 projection=[a0, a, b, c, d]+physical_plan+PartialSortExec: expr=[a@1 ASC NULLS LAST,b@2 ASC NULLS LAST,d@4 ASC NULLS LAST], common_prefix_length=[2]+--StreamingTableExec: partition_sizes=1, projection=[a0, a, b, c, d], infinite_source=true, output_ordering=[a@1 ASC NULLS LAST, b@2 ASC NULLS LAST, c@3 ASC NULLS LAST]++query TT+EXPLAIN SELECT *+FROM annotated_data_infinite2+ORDER BY a, b, d+LIMIT 50;+----+logical_plan+Limit: skip=0, fetch=50+--Sort: annotated_data_infinite2.a ASC NULLS LAST, annotated_data_infinite2.b ASC NULLS LAST, annotated_data_infinite2.d ASC NULLS LAST, fetch=50+----TableScan: annotated_data_infinite2 projection=[a0, a, b, c, d]+physical_plan+GlobalLimitExec: skip=0, fetch=50+--PartialSortExec: TopK(fetch=50), expr=[a@1 ASC NULLS LAST,b@2 ASC NULLS LAST,d@4 ASC NULLS LAST], common_prefix_length=[2]+----StreamingTableExec: partition_sizes=1, projection=[a0, a, b, c, d], infinite_source=true, output_ordering=[a@1 ASC NULLS LAST, b@2 ASC NULLS LAST, c@3 ASC NULLS LAST]++query TT+EXPLAIN SELECT *+FROM multiple_ordered_table+ORDER BY a, b, d;+----+logical_plan+Sort: multiple_ordered_table.a ASC NULLS LAST, multiple_ordered_table.b ASC NULLS LAST, multiple_ordered_table.d ASC NULLS LAST+--TableScan: multiple_ordered_table projection=[a0, a, b, c, d]+physical_plan+SortExec: expr=[a@1 ASC NULLS LAST,b@2 ASC NULLS LAST,d@4 ASC NULLS LAST]+--CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/window_2.csv]]}, projection=[a0, a, b, c, d], output_orderings=[[a@1 ASC NULLS LAST, b@2 ASC NULLS LAST], [c@3 ASC NULLS LAST]], has_header=true++query TT+EXPLAIN SELECT a, b, ARRAY_AGG(d ORDER BY d)+FROM annotated_data_infinite2+GROUP BY a, b;+----+logical_plan+Aggregate: groupBy=[[annotated_data_infinite2.a, annotated_data_infinite2.b]], aggr=[[ARRAY_AGG(annotated_data_infinite2.d) ORDER BY [annotated_data_infinite2.d ASC NULLS LAST]]]+--TableScan: annotated_data_infinite2 projection=[a, b, d]+physical_plan+AggregateExec: mode=Single, gby=[a@0 as a, b@1 as b], aggr=[ARRAY_AGG(annotated_data_infinite2.d)], ordering_mode=Sorted+--PartialSortExec: expr=[a@0 ASC NULLS LAST,b@1 ASC NULLS LAST,d@2 ASC NULLS LAST], common_prefix_length=[2]+----StreamingTableExec: partition_sizes=1, projection=[a, b, d], infinite_source=true, output_ordering=[a@0 ASC NULLS LAST, b@1 ASC NULLS LAST]++# as can be seen in the result below d is indeed ordered.+query II?+SELECT a, b, ARRAY_AGG(d ORDER BY d)+FROM annotated_data_infinite2+GROUP BY a, b;+----+0 0 [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4]+0 1 [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4]+1 2 [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4]+1 3 [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4]++query III+SELECT a, b, d+FROM annotated_data_infinite2+ORDER BY a, b, d;+----+0 0 0+0 0 0+0 0 0+0 0 0+0 0 0+0 0 0+0 0 0+0 0 1+0 0 1+0 0 1+0 0 1+0 0 1+0 0 2+0 0 2+0 0 2+0 0 2+0 0 2+0 0 2+0 0 3+0 0 3+0 0 3+0 0 4+0 0 4+0 0 4+0 0 4+0 1 0+0 1 0+0 1 0+0 1 0+0 1 0+0 1 0+0 1 1+0 1 1+0 1 1+0 1 1+0 1 1+0 1 1+0 1 1+0 1 2+0 1 2+0 1 2+0 1 2+0 1 3+0 1 3+0 1 3+0 1 3+0 1 3+0 1 4+0 1 4+0 1 4+1 2 0+1 2 0+1 2 0+1 2 1+1 2 1+1 2 1+1 2 1+1 2 1+1 2 1+1 2 1+1 2 1+1 2 1+1 2 1+1 2 2+1 2 2+1 2 2+1 2 2+1 2 3+1 2 3+1 2 4+1 2 4+1 2 4+1 2 4+1 2 4+1 2 4+1 3 0+1 3 0+1 3 0+1 3 0+1 3 0+1 3 1+1 3 1+1 3 1+1 3 1+1 3 1+1 3 2+1 3 2+1 3 2+1 3 2+1 3 2+1 3 2+1 3 2+1 3 3+1 3 3+1 3 4+1 3 4+1 3 4+1 3 4+1 3 4+1 3 4+statement okdrop table annotated_data_infinite2;