Kontinuation commented on code in PR #563: URL: https://github.com/apache/sedona-db/pull/563#discussion_r2748721437
########## rust/sedona-spatial-join/src/probe/first_pass_stream.rs: ########## @@ -0,0 +1,236 @@ +// 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::{ + collections::VecDeque, + pin::Pin, + sync::Arc, + task::{Context, Poll}, +}; + +use datafusion_common::{DataFusionError, Result}; +use futures::{Stream, StreamExt}; +use sedona_common::sedona_internal_err; + +use crate::probe::ProbeStreamMetrics; +use crate::{ + evaluated_batch::{ + evaluated_batch_stream::{EvaluatedBatchStream, SendableEvaluatedBatchStream}, + EvaluatedBatch, + }, + partitioning::{ + stream_repartitioner::{ + assign_rows, interleave_evaluated_batch, SpilledPartitions, StreamRepartitioner, + }, + PartitionedSide, SpatialPartition, SpatialPartitioner, + }, +}; + +pub(crate) trait FirstPassStreamCallback { + fn call(self, result: Result<SpilledPartitions>) -> Result<()>; +} + +impl<F: FnOnce(Result<SpilledPartitions>) -> Result<()>> FirstPassStreamCallback for F { + fn call(self, result: Result<SpilledPartitions>) -> Result<()> { + self(result) + } +} + +pub(crate) struct FirstPassStream<C: FirstPassStreamCallback> { + source: SendableEvaluatedBatchStream, + repartitioner: Option<StreamRepartitioner>, + partitioner: Arc<dyn SpatialPartitioner>, + pending_output: VecDeque<Result<EvaluatedBatch>>, + metrics: ProbeStreamMetrics, + callback: Option<C>, +} + +impl<C: FirstPassStreamCallback> FirstPassStream<C> { + pub fn new( + source: SendableEvaluatedBatchStream, + repartitioner: StreamRepartitioner, + partitioner: Arc<dyn SpatialPartitioner>, + metrics: ProbeStreamMetrics, + callback: C, + ) -> Self { + Self { + source, + repartitioner: Some(repartitioner), + partitioner, + pending_output: VecDeque::new(), + metrics, + callback: Some(callback), + } + } + + fn finish_first_pass(&mut self) -> Result<()> { + let repartitioner = self.repartitioner.take().ok_or_else(|| { + DataFusionError::Internal("First pass repartitioner already finished".into()) + })?; + let parts = repartitioner.finish()?; + let callback_opt = self.callback.take(); + match callback_opt { + Some(callback) => callback.call(Ok(parts)), + None => sedona_internal_err!("Callback has already been called"), + } + } + + fn transition_to_failed(&mut self, err: DataFusionError) -> DataFusionError { + let err_arc = Arc::new(err); + let callback_opt = self.callback.take(); + if let Some(callback) = callback_opt { + callback + .call(Err(DataFusionError::Shared(err_arc.clone()))) + .ok(); Review Comment: Good idea. I'll use log::warn! for this. -- 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]
