rluvaton commented on code in PR #15608:
URL: https://github.com/apache/datafusion/pull/15608#discussion_r2030274817


##########
datafusion/physical-plan/src/sorts/multi_level_sort_preserving_merge_stream.rs:
##########
@@ -0,0 +1,244 @@
+// 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.
+
+//! Merge that deals with an arbitrary size of spilled files.
+//! This is an order-preserving merge.
+
+use crate::metrics::BaselineMetrics;
+use crate::{EmptyRecordBatchStream, SpillManager};
+use arrow::array::RecordBatch;
+use arrow::datatypes::SchemaRef;
+use datafusion_common::{internal_err, Result};
+use datafusion_execution::memory_pool::MemoryReservation;
+use std::mem;
+use std::ops::Deref;
+use std::pin::Pin;
+use std::sync::Arc;
+use std::task::{Context, Poll};
+
+use crate::sorts::streaming_merge::StreamingMergeBuilder;
+use crate::spill::in_progress_spill_file::InProgressSpillFile;
+use crate::stream::RecordBatchStreamAdapter;
+use datafusion_execution::disk_manager::RefCountedTempFile;
+use datafusion_execution::{RecordBatchStream, SendableRecordBatchStream};
+use datafusion_physical_expr_common::sort_expr::LexOrdering;
+use futures::{Stream, StreamExt};
+
+enum State {
+    /// Had an error
+    Aborted,
+
+    /// Stream did not start yet or between passes
+    Uninitialized,
+
+    /// In progress of merging multiple sorted streams
+    MultiLevel {
+        stream: SendableRecordBatchStream,
+        in_progress_file: InProgressSpillFile,
+    },
+
+    /// This is the last level of the merge, just pass through the stream
+    Passthrough(SendableRecordBatchStream),
+}
+
+pub struct MultiLevelSortPreservingMergeStream {
+    schema: SchemaRef,
+    spill_manager: SpillManager,
+    sorted_spill_files: Vec<RefCountedTempFile>,
+    sorted_streams: Vec<SendableRecordBatchStream>,
+    expr: Arc<LexOrdering>,
+    metrics: BaselineMetrics,
+    batch_size: usize,
+    reservation: MemoryReservation,
+    fetch: Option<usize>,
+    enable_round_robin_tie_breaker: bool,
+
+    /// The number of blocking threads to use for merging sorted streams
+    max_blocking_threads: usize,
+
+    /// The current state of the stream
+    state: State,
+}
+
+impl MultiLevelSortPreservingMergeStream {
+    #[allow(clippy::too_many_arguments)]
+    pub fn new(
+        spill_manager: SpillManager,
+        schema: SchemaRef,
+        sorted_spill_files: Vec<RefCountedTempFile>,
+        sorted_streams: Vec<SendableRecordBatchStream>,
+        expr: LexOrdering,
+        metrics: BaselineMetrics,
+        batch_size: usize,
+        reservation: MemoryReservation,
+
+        max_blocking_threads: Option<usize>,
+
+        fetch: Option<usize>,
+        enable_round_robin_tie_breaker: bool,
+    ) -> Result<Self> {
+        // TODO - add a check to see the actual number of available blocking 
threads
+        let max_blocking_threads = max_blocking_threads.unwrap_or(128);

Review Comment:
   Currently `tokio` don't expose the value of [maximum number of blocking 
threads](https://docs.rs/tokio/latest/tokio/runtime/struct.Builder.html#method.max_blocking_threads),
 and you have no way to know if you reached the limit as 
[`spawn_blocking`](https://docs.rs/tokio/latest/tokio/task/fn.spawn_blocking.html)
 doesn't return an indication that the current pool is full.



-- 
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: github-unsubscr...@datafusion.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org
For additional commands, e-mail: github-h...@datafusion.apache.org

Reply via email to