Spaarsh commented on code in PR #1041: URL: https://github.com/apache/datafusion-python/pull/1041#discussion_r1978835022
########## src/dataframe.rs: ########## @@ -90,59 +90,108 @@ impl PyDataFrame { } fn __repr__(&self, py: Python) -> PyDataFusionResult<String> { - let df = self.df.as_ref().clone().limit(0, Some(10))?; + // Get 11 rows to check if there are more than 10 + let df = self.df.as_ref().clone().limit(0, Some(11))?; let batches = wait_for_future(py, df.collect())?; - let batches_as_string = pretty::pretty_format_batches(&batches); + let num_rows = batches.iter().map(|batch| batch.num_rows()).sum::<usize>(); + + // Flatten batches into a single batch for the first 10 rows + let mut all_rows = Vec::new(); + let mut total_rows = 0; + + for batch in &batches { + let num_rows_to_take = if total_rows + batch.num_rows() > 10 { + 10 - total_rows + } else { + batch.num_rows() + }; + + if num_rows_to_take > 0 { + let sliced_batch = batch.slice(0, num_rows_to_take); + all_rows.push(sliced_batch); + total_rows += num_rows_to_take; + } + + if total_rows >= 10 { + break; + } + } + + let batches_as_string = pretty::pretty_format_batches(&all_rows); + Review Comment: I did try this initiatially but calling ```collect``` twice led to a severe performance degradation. It used to take ```50ms```. With the manual slicing, it dropped to ```5ms```. You can check my initial suggestion for the same [here](https://github.com/apache/datafusion-python/issues/1026#issuecomment-2689730430) -- 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