MCPcopy Create free account
hub / github.com/apache/datafusion / plan_to_json

Function plan_to_json

datafusion/datasource-json/src/source.rs:462–515  ·  view source on GitHub ↗
(
    task_ctx: Arc<TaskContext>,
    plan: Arc<dyn ExecutionPlan>,
    path: impl AsRef<str>,
)

Source from the content-addressed store, hash-verified

460}
461
462pub async fn plan_to_json(
463 task_ctx: Arc<TaskContext>,
464 plan: Arc<dyn ExecutionPlan>,
465 path: impl AsRef<str>,
466) -> Result<()> {
467 let path = path.as_ref();
468 let parsed = ListingTableUrl::parse(path)?;
469 let object_store_url = parsed.object_store();
470 let store = task_ctx.runtime_env().object_store(&object_store_url)?;
471 let writer_buffer_size = task_ctx
472 .session_config()
473 .options()
474 .execution
475 .objectstore_writer_buffer_size;
476 let mut join_set = JoinSet::new();
477 for i in 0..plan.output_partitioning().partition_count() {
478 let storeref = Arc::clone(&store);
479 let plan: Arc<dyn ExecutionPlan> = Arc::clone(&plan);
480 let filename = format!("{}/part-{i}.json", parsed.prefix());
481 let file = object_store::path::Path::parse(filename)?;
482
483 let mut stream = plan.execute(i, Arc::clone(&task_ctx))?;
484 join_set.spawn(async move {
485 let mut buf_writer =
486 BufWriter::with_capacity(storeref, file.clone(), writer_buffer_size);
487
488 let mut buffer = Vec::with_capacity(1024);
489 while let Some(batch) = stream.next().await.transpose()? {
490 let mut writer = json::LineDelimitedWriter::new(buffer);
491 writer.write(&batch)?;
492 buffer = writer.into_inner();
493 buf_writer.write_all(&buffer).await?;
494 buffer.clear();
495 }
496
497 buf_writer.shutdown().await.map_err(DataFusionError::from)
498 });
499 }
500
501 while let Some(result) = join_set.join_next().await {
502 match result {
503 Ok(res) => res?, // propagate DataFusion error
504 Err(e) => {
505 if e.is_panic() {
506 std::panic::resume_unwind(e.into_panic());
507 } else {
508 unreachable!();
509 }
510 }
511 }
512 }
513
514 Ok(())
515}
516
517#[cfg(test)]
518mod tests {

Callers 1

write_jsonMethod · 0.50

Calls 15

newFunction · 0.85
session_configMethod · 0.80
partition_countMethod · 0.80
join_nextMethod · 0.80
as_refMethod · 0.45
object_storeMethod · 0.45
runtime_envMethod · 0.45
optionsMethod · 0.45
output_partitioningMethod · 0.45
executeMethod · 0.45
spawnMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…