Kontinuation commented on PR #14644:
URL: https://github.com/apache/datafusion/pull/14644#issuecomment-2661348012

   > * **This PR can't solve**
   >   `select * from t` consumes 2GB memory to fully materialize the output, 
in the best case `select * from t order by c1` consumes around 2GB memory, 
given sorting is an O(1) space operation. The current situation is it consumes 
4GB+ (more than 2 times).
   
   I think this is by design. The physical operators should not reserve memory 
for the batches it produces. It is the parent operator's duty to reserve memory 
for the batches fetched from children operators if it needs to hold the batches 
in memory for a long time.
   
   > 
   > ### Problem
   > I tried one query and this PR is not working as expected, I specified one 
query to run under 5GB memory (`select *` without order requires 7GB) but it's 
still consuming around 10GB, could you double check? I suspect we missed some 
details.
   > 
   
   It is a problem of datafusion-cli. If datafusion-cli decides to hold all the 
result batches in memory, it should create a memory consumer for itself and 
reserve memory for the result batches.
   
   `datafusion-cli` [calls 
`collect`](https://github.com/apache/datafusion/blob/45.0.0/datafusion-cli/src/exec.rs#L249-L254)
 to hold the entire result set in memory before displaying it, this is 
unnecessary when `maxrows` is not unlimited. I've tried the following code to 
replace the `collect` call, and the `maximum resident set size` has reduced to 
4.8 GB:
   
   ```rust
   // Bounded stream; collected results are printed after all input consumed.
   let schema = physical_plan.schema();
   let mut stream = execute_stream(physical_plan, task_ctx.clone())?;
   let mut results = vec![];
   while let Some(batch) = stream.next().await {
       let batch = batch?;
       results.push(batch);
       if let MaxRows::Limited(max_rows) = print_options.maxrows {
           if results.len() >= max_rows {
               break;
           }
       }
   }
   adjusted.into_inner().print_batches(schema, &results, now)?;
   ```
   


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