kosiew commented on code in PR #1041:
URL: 
https://github.com/apache/datafusion-python/pull/1041#discussion_r1980965152


##########
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:
   > calling collect twice led to a severe performance degradation
   
   I ran this test to compare the performance:
   ```python
   import pyarrow as pa
   from datafusion import (
       SessionContext,
   )
   import time
   
   
   def run_dataframe_repr_long() -> None:
       ctx = SessionContext()
       # Create a DataFrame with more than 10 rows
       batch = pa.RecordBatch.from_arrays(
           [
               pa.array(list(range(15))),
               pa.array([x * 2 for x in range(15)]),
               pa.array([x * 3 for x in range(15)]),
           ],
           names=["a", "b", "c"],
       )
       df = ctx.create_dataframe([[batch]])
   
       output = repr(df)
   
   
   def average_runtime(func, runs=100):
       total_time = 0
       for _ in range(runs):
           start_time = time.time()
           func()
           end_time = time.time()
           total_time += end_time - start_time
       return total_time / runs
   
   
   average_time = average_runtime(run_dataframe_repr_long)
   print(f"Average runtime over {100} runs: {average_time:.6f} seconds")
   ```
   
   and found no significant difference:
   
   <img width="515" alt="image" 
src="https://github.com/user-attachments/assets/e58bb136-2097-4b3e-8e4d-b1648cf29078";
 />
   
   pr_1041 - is the branch with one `collect`
   amended_pr_1041 - is the branch with two `collect`



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