|
| 1 | +use arrow::util::pretty::pretty_format_batches; |
| 2 | +use arrow_flight::flight_service_server::FlightServiceServer; |
| 3 | +use async_trait::async_trait; |
| 4 | +use datafusion::common::DataFusionError; |
| 5 | +use datafusion::execution::SessionStateBuilder; |
| 6 | +use datafusion::physical_plan::displayable; |
| 7 | +use datafusion::prelude::{ParquetReadOptions, SessionContext}; |
| 8 | +use datafusion_distributed::{ |
| 9 | + ArrowFlightEndpoint, BoxCloneSyncChannel, ChannelResolver, DistributedExt, |
| 10 | + DistributedPhysicalOptimizerRule, DistributedSessionBuilderContext, |
| 11 | +}; |
| 12 | +use futures::TryStreamExt; |
| 13 | +use hyper_util::rt::TokioIo; |
| 14 | +use std::error::Error; |
| 15 | +use std::sync::Arc; |
| 16 | +use structopt::StructOpt; |
| 17 | +use tonic::transport::{Endpoint, Server}; |
| 18 | + |
| 19 | +#[derive(StructOpt)] |
| 20 | +#[structopt( |
| 21 | + name = "run", |
| 22 | + about = "An in-memory cluster Distributed DataFusion runner" |
| 23 | +)] |
| 24 | +struct Args { |
| 25 | + #[structopt()] |
| 26 | + query: String, |
| 27 | + |
| 28 | + #[structopt(long)] |
| 29 | + explain: bool, |
| 30 | +} |
| 31 | + |
| 32 | +#[tokio::main] |
| 33 | +async fn main() -> Result<(), Box<dyn Error>> { |
| 34 | + let args = Args::from_args(); |
| 35 | + |
| 36 | + let state = SessionStateBuilder::new() |
| 37 | + .with_default_features() |
| 38 | + .with_distributed_channel_resolver(InMemoryChannelResolver::new()) |
| 39 | + .with_physical_optimizer_rule(Arc::new(DistributedPhysicalOptimizerRule::new())) |
| 40 | + .build(); |
| 41 | + |
| 42 | + let ctx = SessionContext::from(state); |
| 43 | + |
| 44 | + ctx.register_parquet( |
| 45 | + "flights_1m", |
| 46 | + "testdata/flights-1m.parquet", |
| 47 | + ParquetReadOptions::default(), |
| 48 | + ) |
| 49 | + .await?; |
| 50 | + |
| 51 | + ctx.register_parquet( |
| 52 | + "weather", |
| 53 | + "testdata/weather.parquet", |
| 54 | + ParquetReadOptions::default(), |
| 55 | + ) |
| 56 | + .await?; |
| 57 | + |
| 58 | + let df = ctx.sql(&args.query).await?; |
| 59 | + if args.explain { |
| 60 | + let plan = df.create_physical_plan().await?; |
| 61 | + let display = displayable(plan.as_ref()).indent(true).to_string(); |
| 62 | + println!("{display}"); |
| 63 | + } else { |
| 64 | + let stream = df.execute_stream().await?; |
| 65 | + let batches = stream.try_collect::<Vec<_>>().await?; |
| 66 | + let formatted = pretty_format_batches(&batches)?; |
| 67 | + println!("{formatted}"); |
| 68 | + } |
| 69 | + Ok(()) |
| 70 | +} |
| 71 | + |
| 72 | +const DUMMY_URL: &str = "http://localhost:50051"; |
| 73 | + |
| 74 | +/// [ChannelResolver] implementation that returns gRPC clients baked by an in-memory |
| 75 | +/// tokio duplex rather than a TCP connection. |
| 76 | +#[derive(Clone)] |
| 77 | +struct InMemoryChannelResolver { |
| 78 | + channel: BoxCloneSyncChannel, |
| 79 | +} |
| 80 | + |
| 81 | +impl InMemoryChannelResolver { |
| 82 | + fn new() -> Self { |
| 83 | + let (client, server) = tokio::io::duplex(1024 * 1024); |
| 84 | + |
| 85 | + let mut client = Some(client); |
| 86 | + let channel = Endpoint::try_from(DUMMY_URL) |
| 87 | + .expect("Invalid dummy URL for building an endpoint. This should never happen") |
| 88 | + .connect_with_connector_lazy(tower::service_fn(move |_| { |
| 89 | + let client = client |
| 90 | + .take() |
| 91 | + .expect("Client taken twice. This should never happen"); |
| 92 | + async move { Ok::<_, std::io::Error>(TokioIo::new(client)) } |
| 93 | + })); |
| 94 | + |
| 95 | + let this = Self { |
| 96 | + channel: BoxCloneSyncChannel::new(channel), |
| 97 | + }; |
| 98 | + let this_clone = this.clone(); |
| 99 | + |
| 100 | + let endpoint = |
| 101 | + ArrowFlightEndpoint::try_new(move |ctx: DistributedSessionBuilderContext| { |
| 102 | + let this = this.clone(); |
| 103 | + async move { |
| 104 | + let builder = SessionStateBuilder::new() |
| 105 | + .with_default_features() |
| 106 | + .with_distributed_channel_resolver(this) |
| 107 | + .with_runtime_env(ctx.runtime_env.clone()); |
| 108 | + Ok(builder.build()) |
| 109 | + } |
| 110 | + }) |
| 111 | + .unwrap(); |
| 112 | + |
| 113 | + tokio::spawn(async move { |
| 114 | + Server::builder() |
| 115 | + .add_service(FlightServiceServer::new(endpoint)) |
| 116 | + .serve_with_incoming(tokio_stream::once(Ok::<_, std::io::Error>(server))) |
| 117 | + .await |
| 118 | + }); |
| 119 | + |
| 120 | + this_clone |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[async_trait] |
| 125 | +impl ChannelResolver for InMemoryChannelResolver { |
| 126 | + fn get_urls(&self) -> Result<Vec<url::Url>, DataFusionError> { |
| 127 | + Ok(vec![url::Url::parse(DUMMY_URL).unwrap()]) |
| 128 | + } |
| 129 | + |
| 130 | + async fn get_channel_for_url( |
| 131 | + &self, |
| 132 | + _: &url::Url, |
| 133 | + ) -> Result<BoxCloneSyncChannel, DataFusionError> { |
| 134 | + Ok(self.channel.clone()) |
| 135 | + } |
| 136 | +} |
0 commit comments