ahmetenis commented on code in PR #9125: URL: https://github.com/apache/arrow-datafusion/pull/9125#discussion_r1482038220
########## 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>) -> Self { + Self { + input, + expr, + common_prefix_length: 0, Review Comment: thanks for the suggestion -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
