use arrow::util::pretty::pretty_format_batches;
use async_trait::async_trait;
use datafusion::common::DataFusionError;
use datafusion::execution::SessionStateBuilder;
use datafusion::prelude::{ParquetReadOptions, SessionContext};
use datafusion_distributed::{
BoxCloneSyncChannel, ChannelResolver, DistributedExt, SessionStateBuilderExt, Worker,
WorkerQueryContext, WorkerResolver, WorkerServiceClient, create_worker_client,
display_plan_ascii,
};
use futures::TryStreamExt;
use hyper_util::rt::TokioIo;
use std::error::Error;
use structopt::StructOpt;
use tonic::transport::{Endpoint, Server};
#[derive(StructOpt)]
#[structopt(
name = "run",
about = "Run a query in an in-memory Distributed DataFusion cluster"
)]
struct Args {
#[structopt()]
query: String,
#[structopt(long)]
show_distributed_plan: bool,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let args = Args::from_args();
let state = SessionStateBuilder::new()
.with_default_features()
.with_distributed_worker_resolver(InMemoryWorkerResolver)
.with_distributed_channel_resolver(InMemoryChannelResolver::new())
.with_distributed_planner()
.with_distributed_file_scan_config_bytes_per_partition(1)?
.build();
let ctx = SessionContext::from(state);
ctx.register_parquet("weather", "testdata/weather", ParquetReadOptions::default())
.await?;
let df = ctx.sql(&args.query).await?;
if args.show_distributed_plan {
let plan = df.create_physical_plan().await?;
println!("{}", display_plan_ascii(plan.as_ref(), false));
} else {
let stream = df.execute_stream().await?;
let batches = stream.try_collect::<Vec<_>>().await?;
let formatted = pretty_format_batches(&batches)?;
println!("{formatted}");
}
Ok(())
}
const DUMMY_URL: &str = "http://localhost:50051";
#[derive(Clone)]
struct InMemoryChannelResolver {
channel: WorkerServiceClient<BoxCloneSyncChannel>,
}
impl InMemoryChannelResolver {
fn new() -> Self {
let (client, server) = tokio::io::duplex(1024 * 1024);
let mut client = Some(client);
let channel = Endpoint::try_from(DUMMY_URL)
.expect("Invalid dummy URL for building an endpoint. This should never happen")
.connect_with_connector_lazy(tower::service_fn(move |_| {
let client = client
.take()
.expect("Client taken twice. This should never happen");
async move { Ok::<_, std::io::Error>(TokioIo::new(client)) }
}));
let this = Self {
channel: create_worker_client(BoxCloneSyncChannel::new(channel)),
};
let this_clone = this.clone();
let endpoint = Worker::from_session_builder(move |ctx: WorkerQueryContext| {
let this = this.clone();
async move { Ok(ctx.builder.with_distributed_channel_resolver(this).build()) }
});
#[allow(clippy::disallowed_methods)]
tokio::spawn(async move {
Server::builder()
.add_service(endpoint.into_worker_server())
.serve_with_incoming(tokio_stream::once(Ok::<_, std::io::Error>(server)))
.await
});
this_clone
}
}
#[async_trait]
impl ChannelResolver for InMemoryChannelResolver {
async fn get_worker_client_for_url(
&self,
_: &url::Url,
) -> Result<WorkerServiceClient<BoxCloneSyncChannel>, DataFusionError> {
Ok(self.channel.clone())
}
}
struct InMemoryWorkerResolver;
impl WorkerResolver for InMemoryWorkerResolver {
fn get_urls(&self) -> Result<Vec<url::Url>, DataFusionError> {
Ok(vec![url::Url::parse(DUMMY_URL).unwrap(); 16]) }
}