EeshanBembi commented on code in PR #17867: URL: https://github.com/apache/datafusion/pull/17867#discussion_r2483736299
########## datafusion-cli/src/progress/mod.rs: ########## @@ -0,0 +1,226 @@ +// 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. + +//! Progress reporting for DataFusion CLI +//! +//! This module provides a progress bar implementation with ETA estimation +//! for long-running queries, similar to DuckDB's progress bar. + +mod config; +mod display; +mod estimator; +mod metrics_poll; +mod plan_introspect; + +pub use config::{ProgressConfig, ProgressEstimator, ProgressMode, ProgressStyle}; + +use datafusion::error::Result; +use datafusion::physical_plan::ExecutionPlan; +use datafusion_common_runtime::SpawnedTask; +use std::sync::Arc; +use std::time::Duration; + +/// Main progress reporter that coordinates metrics collection, ETA estimation, and display +pub struct ProgressReporter { + _handle: SpawnedTask<()>, +} + +impl ProgressReporter { + /// Start a new progress reporter for the given physical plan + pub async fn start( + physical_plan: &Arc<dyn ExecutionPlan>, + config: ProgressConfig, + ) -> Result<Self> { + // Clone the plan for the background task + let plan = Arc::clone(physical_plan); + + let _handle = SpawnedTask::spawn(async move { + let reporter = ProgressReporterInner::new(plan, config); + reporter.run().await; + }); + + Ok(Self { _handle }) + } + + /// Stop the progress reporter + /// Note: The task is automatically aborted when this struct is dropped + pub async fn stop(&self) { + // Task will be aborted automatically when this struct is dropped + } +} + +/// Internal implementation of the progress reporter +struct ProgressReporterInner { + plan: Arc<dyn ExecutionPlan>, + config: ProgressConfig, +} + +impl ProgressReporterInner { + fn new(plan: Arc<dyn ExecutionPlan>, config: ProgressConfig) -> Self { + Self { plan, config } + } + + async fn run(self) { + // Early exit if progress is disabled + if !self.config.should_show_progress() { + return; + } + + let introspector = plan_introspect::PlanIntrospector::new(&self.plan); + let totals = introspector.get_totals(); + + let mut poller = metrics_poll::MetricsPoller::new(&self.plan); + let mut estimator = estimator::ProgressEstimator::new(self.config.estimator); + let mut display = display::ProgressDisplay::new(self.config.style); + + let interval = Duration::from_millis(self.config.interval_ms); + let mut ticker = tokio::time::interval(interval); + + loop { + ticker.tick().await; + let metrics = poller.poll(); + let progress = self.calculate_progress(&totals, &metrics); + + let eta = estimator.update(progress.clone()); + display.update(&progress, eta); + + // In a real implementation, we'd check for completion or cancellation + // For now, this runs indefinitely until the task is dropped + } + } + + fn calculate_progress( + &self, + totals: &plan_introspect::PlanTotals, + metrics: &metrics_poll::LiveMetrics, + ) -> ProgressInfo { + let (current, total, unit) = + if totals.total_bytes > 0 && metrics.bytes_scanned > 0 { Review Comment: Hey @pepijnve , I hadn't thought of that, i'll take that into account and push changes accordingly -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
