Skip to content

Commit 312d5c8

Browse files
cj-zhukovSergey Zhukovalamb
authored andcommitted
Consolidate flight examples (apache#18142) (apache#18442)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - part of #apache#18142. ## Rationale for this change As discussed in apache#18289 this PR is for consolidating all the `flight` examples into a single example binary. Then we can make sure we are agreed on the pattern and then we can apply it to the remaining examples <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> --------- Co-authored-by: Sergey Zhukov <[email protected]> Co-authored-by: Andrew Lamb <[email protected]>
1 parent 9ff0bb9 commit 312d5c8

File tree

6 files changed

+99
-20
lines changed

6 files changed

+99
-20
lines changed

datafusion-examples/Cargo.toml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ rust-version = { workspace = true }
3232
[lints]
3333
workspace = true
3434

35-
[[example]]
36-
name = "flight_sql_server"
37-
path = "examples/flight/flight_sql_server.rs"
38-
39-
[[example]]
40-
name = "flight_server"
41-
path = "examples/flight/flight_server.rs"
42-
43-
[[example]]
44-
name = "flight_client"
45-
path = "examples/flight/flight_client.rs"
46-
4735
[[example]]
4836
name = "dataframe_to_s3"
4937
path = "examples/external_dependency/dataframe-to-s3.rs"

datafusion-examples/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ cargo run --example dataframe
6565
- [`deserialize_to_struct.rs`](examples/deserialize_to_struct.rs): Convert query results (Arrow ArrayRefs) into Rust structs
6666
- [`expr_api.rs`](examples/expr_api.rs): Create, execute, simplify, analyze and coerce `Expr`s
6767
- [`file_stream_provider.rs`](examples/file_stream_provider.rs): Run a query on `FileStreamProvider` which implements `StreamProvider` for reading and writing to arbitrary stream sources / sinks.
68-
- [`flight_sql_server.rs`](examples/flight/flight_sql_server.rs): Run DataFusion as a standalone process and execute SQL queries from JDBC clients
68+
- [`flight/sql_server.rs`](examples/flight/sql_server.rs): Run DataFusion as a standalone process and execute SQL queries from Flight and and FlightSQL (e.g. JDBC) clients
6969
- [`function_factory.rs`](examples/function_factory.rs): Register `CREATE FUNCTION` handler to implement SQL macros
7070
- [`memory_pool_tracking.rs`](examples/memory_pool_tracking.rs): Demonstrates TrackConsumersPool for memory tracking and debugging with enhanced error messages
7171
- [`memory_pool_execution_plan.rs`](examples/memory_pool_execution_plan.rs): Shows how to implement memory-aware ExecutionPlan with memory reservation and spilling
@@ -94,4 +94,4 @@ cargo run --example dataframe
9494

9595
## Distributed
9696

97-
- [`flight_client.rs`](examples/flight/flight_client.rs) and [`flight_server.rs`](examples/flight/flight_server.rs): Run DataFusion as a standalone process and execute SQL queries from a client using the Flight protocol.
97+
- [`examples/flight/client.rs`](examples/flight/client.rs) and [`examples/flight/server.rs`](examples/flight/server.rs): Run DataFusion as a standalone process and execute SQL queries from a client using the Arrow Flight protocol.

datafusion-examples/examples/flight/flight_client.rs renamed to datafusion-examples/examples/flight/client.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ use datafusion::arrow::util::pretty;
3030
/// This example shows how to wrap DataFusion with `FlightService` to support looking up schema information for
3131
/// Parquet files and executing SQL queries against them on a remote server.
3232
/// This example is run along-side the example `flight_server`.
33-
#[tokio::main]
34-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
33+
pub async fn client() -> Result<(), Box<dyn std::error::Error>> {
3534
let testdata = datafusion::test_util::parquet_test_data();
3635

3736
// Create Flight client
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
//! # Arrow Flight Examples
19+
//!
20+
//! These examples demonstrate Arrow Flight usage.
21+
//!
22+
//! Each subcommand runs a corresponding example:
23+
//! - `client` — run DataFusion as a standalone process and execute SQL queries from a client using the Flight protocol
24+
//! - `server` — run DataFusion as a standalone process and execute SQL queries from a client using the Flight protocol
25+
//! - `sql_server` — run DataFusion as a standalone process and execute SQL queries from JDBC clients
26+
27+
mod client;
28+
mod server;
29+
mod sql_server;
30+
31+
use std::str::FromStr;
32+
33+
use datafusion::error::{DataFusionError, Result};
34+
35+
enum ExampleKind {
36+
Client,
37+
Server,
38+
SqlServer,
39+
}
40+
41+
impl AsRef<str> for ExampleKind {
42+
fn as_ref(&self) -> &str {
43+
match self {
44+
Self::Client => "client",
45+
Self::Server => "server",
46+
Self::SqlServer => "sql_server",
47+
}
48+
}
49+
}
50+
51+
impl FromStr for ExampleKind {
52+
type Err = DataFusionError;
53+
54+
fn from_str(s: &str) -> Result<Self> {
55+
match s {
56+
"client" => Ok(Self::Client),
57+
"server" => Ok(Self::Server),
58+
"sql_server" => Ok(Self::SqlServer),
59+
_ => Err(DataFusionError::Execution(format!("Unknown example: {s}"))),
60+
}
61+
}
62+
}
63+
64+
impl ExampleKind {
65+
const ALL: [Self; 3] = [Self::Client, Self::Server, Self::SqlServer];
66+
67+
const EXAMPLE_NAME: &str = "flight";
68+
69+
fn variants() -> Vec<&'static str> {
70+
Self::ALL.iter().map(|x| x.as_ref()).collect()
71+
}
72+
}
73+
74+
#[tokio::main]
75+
async fn main() -> Result<(), Box<dyn std::error::Error>> {
76+
let usage = format!(
77+
"Usage: cargo run --example {} -- [{}]",
78+
ExampleKind::EXAMPLE_NAME,
79+
ExampleKind::variants().join("|")
80+
);
81+
82+
let arg = std::env::args().nth(1).ok_or_else(|| {
83+
eprintln!("{usage}");
84+
DataFusionError::Execution("Missing argument".to_string())
85+
})?;
86+
87+
match arg.parse::<ExampleKind>()? {
88+
ExampleKind::Client => client::client().await?,
89+
ExampleKind::Server => server::server().await?,
90+
ExampleKind::SqlServer => sql_server::sql_server().await?,
91+
}
92+
93+
Ok(())
94+
}

datafusion-examples/examples/flight/flight_server.rs renamed to datafusion-examples/examples/flight/server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,7 @@ fn to_tonic_err(e: datafusion::error::DataFusionError) -> Status {
194194
/// This example shows how to wrap DataFusion with `FlightService` to support looking up schema information for
195195
/// Parquet files and executing SQL queries against them on a remote server.
196196
/// This example is run along-side the example `flight_client`.
197-
#[tokio::main]
198-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
197+
pub async fn server() -> Result<(), Box<dyn std::error::Error>> {
199198
let addr = "0.0.0.0:50051".parse()?;
200199
let service = FlightServiceImpl {};
201200

datafusion-examples/examples/flight/flight_sql_server.rs renamed to datafusion-examples/examples/flight/sql_server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ macro_rules! status {
6969
/// Based heavily on Ballista's implementation: https://github.com/apache/datafusion-ballista/blob/main/ballista/scheduler/src/flight_sql.rs
7070
/// and the example in arrow-rs: https://github.com/apache/arrow-rs/blob/master/arrow-flight/examples/flight_sql_server.rs
7171
///
72-
#[tokio::main]
73-
async fn main() -> Result<(), Box<dyn std::error::Error>> {
72+
pub async fn sql_server() -> Result<(), Box<dyn std::error::Error>> {
7473
env_logger::init();
7574
let addr = "0.0.0.0:50051".parse()?;
7675
let service = FlightSqlServiceImpl {

0 commit comments

Comments
 (0)