zhuqi-lucas commented on code in PR #16196:
URL: https://github.com/apache/datafusion/pull/16196#discussion_r2122365680


##########
datafusion/physical-plan/src/yield_stream.rs:
##########
@@ -0,0 +1,235 @@
+// 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.
+
+use std::any::Any;
+use std::pin::Pin;
+use std::sync::Arc;
+use std::task::{Context, Poll};
+
+use crate::execution_plan::CardinalityEffect;
+use crate::execution_plan::CardinalityEffect::Equal;
+use crate::{
+    DisplayAs, DisplayFormatType, ExecutionPlan, PlanProperties, 
RecordBatchStream,
+    SendableRecordBatchStream,
+};
+use arrow::record_batch::RecordBatch;
+use arrow_schema::Schema;
+use datafusion_common::{Result, Statistics};
+use datafusion_execution::TaskContext;
+use futures::Stream;
+
+/// Number of batches to yield before voluntarily returning Pending.
+/// This allows long-running operators to periodically yield control
+/// back to the executor (e.g., to handle cancellation).
+const YIELD_BATCHES: usize = 64;
+
+/// A stream that yields batches of data, yielding control back to the 
executor every `YIELD_BATCHES` batches
+///
+/// This can be useful to allow operators that might not yield to check for 
cancellation
+pub struct YieldStream {
+    inner: SendableRecordBatchStream,
+    batches_processed: usize,
+    buffer: Option<Result<RecordBatch>>,
+}
+
+impl YieldStream {
+    pub fn new(inner: SendableRecordBatchStream) -> Self {
+        Self {
+            inner,
+            batches_processed: 0,
+            buffer: None,
+        }
+    }
+}
+
+// Stream<Item = Result<RecordBatch>> to poll_next_unpin
+impl Stream for YieldStream {
+    type Item = Result<RecordBatch>;
+
+    fn poll_next(
+        mut self: Pin<&mut Self>,
+        cx: &mut Context<'_>,
+    ) -> Poll<Option<Self::Item>> {
+        let this = &mut *self;
+
+        if let Some(batch) = this.buffer.take() {

Review Comment:
   I was not adding the buffer, but If we remove the buffer, it will generate 
wrong result, here is the reproducer case:
   
   ```rust
   SELECT SUM(value)
   FROM range(1,500000000) AS t;
   +--------------------+
   | sum(t.value)       |
   +--------------------+
   | 123047621195087232 |
   +--------------------+
   1 row(s) fetched.
   Elapsed 0.248 seconds.
   ```
   
   Right result from main branch:
   
   ```rust
   SELECT SUM(value)
   FROM range(1,500000000) AS t;
   +--------------------+
   | sum(t.value)       |
   +--------------------+
   | 124999999750000000 |
   +--------------------+
   1 row(s) fetched.
   Elapsed 0.319 seconds.
   ```
   
   
   So i added the buffer logic. And testing cases will generate right answer.



-- 
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