Skip to content

Commit c1973b1

Browse files
Add --flightsql-host param to cli args (#246)
1 parent 4dccb5b commit c1973b1

File tree

10 files changed

+64
-15
lines changed

10 files changed

+64
-15
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ $ dft -c "SELECT 1+2"
9292

9393
#### FlightSQL
9494

95-
Both of the commands above support the `--flightsql` parameter to run the SQL with your configured FlightSQL client.
95+
Both of the commands above support the `--flightsql` parameter to run the SQL with your configured FlightSQL client. You can also configure the host used for creating FlightSQL client per command with `--flightsql-host` - for example `--flightsql-host "http://127.0.0.1:50052"`.
9696

9797
#### DDL
9898

src/args.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ pub struct DftArgs {
9090

9191
#[clap(short = 'n', help = "Set the number of benchmark iterations to run")]
9292
pub benchmark_iterations: Option<usize>,
93+
94+
#[cfg(feature = "flightsql")]
95+
#[clap(long, help = "Set the host and port to be used for FlightSQL")]
96+
pub flightsql_host: Option<String>,
9397
}
9498

9599
impl DftArgs {

src/execution/flightsql.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ impl FlightSQLContext {
4949
}
5050

5151
/// Create FlightSQL client from users FlightSQL config
52-
pub async fn create_client(&self) -> Result<()> {
53-
let url = Box::leak(self.config.connection_url.clone().into_boxed_str());
52+
pub async fn create_client(&self, cli_host: Option<String>) -> Result<()> {
53+
let final_url = cli_host.unwrap_or(self.config.connection_url.clone());
54+
let url = Box::leak(final_url.into_boxed_str());
5455
info!("Connecting to FlightSQL host: {}", url);
5556
let channel = Channel::from_static(url).connect().await;
5657
match channel {

src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ async fn main() -> Result<()> {
4646
{
4747
if cli.flightsql {
4848
let flightsql_ctx = FlightSQLContext::new(state.config.flightsql.clone());
49-
flightsql_ctx.create_client().await?;
49+
flightsql_ctx
50+
.create_client(cli.flightsql_host.clone())
51+
.await?;
5052
app_execution.with_flightsql_ctx(flightsql_ctx);
5153
}
5254
}
53-
let app = CliApp::new(app_execution, cli);
55+
let app = CliApp::new(app_execution, cli.clone());
5456
app.execute_files_or_commands().await?;
5557
} else if cli.serve {
5658
#[cfg(feature = "experimental-flightsql-server")]
@@ -66,7 +68,12 @@ async fn main() -> Result<()> {
6668
}
6769
let app_execution = AppExecution::new(execution_ctx);
6870
let server = FlightSqlServiceImpl::new(app_execution);
69-
let app = FlightSqlApp::new(server.service(), DEFAULT_SERVER_ADDRESS).await;
71+
let app = FlightSqlApp::new(
72+
server.service(),
73+
&cli.flightsql_host
74+
.unwrap_or(DEFAULT_SERVER_ADDRESS.to_string()),
75+
)
76+
.await;
7077
app.run_app().await;
7178
}
7279

@@ -82,7 +89,7 @@ async fn main() -> Result<()> {
8289
let state = state::initialize(cli.config_path());
8390
let execution_ctx = ExecutionContext::try_new(&state.config.execution, AppType::Tui)?;
8491
let app_execution = AppExecution::new(execution_ctx);
85-
let app = App::new(state, app_execution);
92+
let app = App::new(state, cli, app_execution);
8693
app.run_app().await?;
8794
}
8895

src/tui/execution.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,9 @@ impl TuiExecution {
403403

404404
// TODO: Maybe just expose `inner` and use that rather than re-implementing the same
405405
// functions here.
406-
407406
#[cfg(feature = "flightsql")]
408-
pub async fn create_flightsql_client(&self) -> Result<()> {
409-
self.inner.flightsql_ctx().create_client().await
407+
pub async fn create_flightsql_client(&self, cli_host: Option<String>) -> Result<()> {
408+
self.inner.flightsql_ctx().create_client(cli_host).await
410409
}
411410

412411
#[cfg(feature = "flightsql")]

src/tui/handlers/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,9 @@ pub fn app_event_handler(app: &mut App, event: AppEvent) -> Result<()> {
245245
AppEvent::FlightSQLEstablishConnection => {
246246
let execution = Arc::clone(&app.execution);
247247
let _event_tx = app.event_tx.clone();
248+
let cli_url = app.args.flightsql_host.clone();
248249
tokio::spawn(async move {
249-
if let Err(e) = execution.create_flightsql_client().await {
250+
if let Err(e) = execution.create_flightsql_client(cli_url).await {
250251
error!("Error creating FlightSQL client: {:?}", e);
251252
if let Err(e) = _event_tx.send(AppEvent::FlightSQLFailedToConnect) {
252253
error!("Error sending FlightSQLFailedToConnect message: {e}");

src/tui/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ use tokio_util::sync::CancellationToken;
4040

4141
use self::execution::{ExecutionError, ExecutionResultsBatch, TuiExecution};
4242
use self::handlers::{app_event_handler, crossterm_event_handler};
43+
use crate::args::DftArgs;
4344
use crate::execution::sql_utils::clean_sql;
4445
use crate::execution::AppExecution;
4546

@@ -92,18 +93,19 @@ pub struct App<'app> {
9293
cancellation_token: CancellationToken,
9394
task: JoinHandle<()>,
9495
ddl_task: Option<JoinHandle<()>>,
96+
args: DftArgs,
9597
}
9698

9799
impl<'app> App<'app> {
98-
pub fn new(state: state::AppState<'app>, execution: AppExecution) -> Self {
100+
pub fn new(state: state::AppState<'app>, args: DftArgs, execution: AppExecution) -> Self {
99101
let (event_tx, event_rx) = mpsc::unbounded_channel();
100102
let cancellation_token = CancellationToken::new();
101103
let task = tokio::spawn(async {});
102-
// let ddl_task = tokio::spawn(async {});
103104
let app_execution = Arc::new(TuiExecution::new(Arc::new(execution)));
104105

105106
Self {
106107
state,
108+
args,
107109
task,
108110
event_rx,
109111
event_tx,

tests/extension_cases/flightsql.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,34 @@ SELECT 1
495495
assert.stdout(contains_str(expected));
496496
fixture.shutdown_and_wait().await;
497497
}
498+
499+
#[tokio::test]
500+
pub async fn test_execute_custom_port() {
501+
let test_server = TestFlightSqlServiceImpl::new();
502+
let fixture = TestFixture::new(test_server.service(), "127.0.0.1:50052").await;
503+
504+
let assert = tokio::task::spawn_blocking(|| {
505+
Command::cargo_bin("dft")
506+
.unwrap()
507+
.arg("-c")
508+
.arg("SELECT 1 + 2;")
509+
.arg("--flightsql")
510+
.arg("--flightsql-host")
511+
.arg("http://localhost:50052")
512+
.timeout(Duration::from_secs(5))
513+
.assert()
514+
.success()
515+
})
516+
.await
517+
.unwrap();
518+
519+
let expected = r##"
520+
+---------------------+
521+
| Int64(1) + Int64(2) |
522+
+---------------------+
523+
| 3 |
524+
+---------------------+
525+
"##;
526+
assert.stdout(contains_str(expected));
527+
fixture.shutdown_and_wait().await;
528+
}

tests/tui_cases/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod quit;
2424
use datafusion::arrow::array::RecordBatch;
2525
use datafusion::common::Result;
2626
use dft::{
27+
args::DftArgs,
2728
execution::{local::ExecutionContext, AppExecution, AppType},
2829
tui::{state::initialize, App, AppEvent},
2930
};
@@ -51,7 +52,8 @@ impl<'app> TestApp<'app> {
5152
let execution_ctx =
5253
ExecutionContext::try_new(&state.config.execution, AppType::Tui).unwrap();
5354
let app_execution = AppExecution::new(execution_ctx);
54-
let mut app = App::new(state, app_execution);
55+
let args = DftArgs::default();
56+
let mut app = App::new(state, args, app_execution);
5557
app.enter(false).unwrap();
5658
Self { config_path, app }
5759
}

tests/tui_cases/quit.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
//! Tests for the TUI (e.g. user application with keyboard commands)
1919
20+
use dft::args::DftArgs;
2021
use dft::execution::{local::ExecutionContext, AppExecution, AppType};
2122
use dft::tui::state::initialize;
2223
use dft::tui::{App, AppEvent};
@@ -105,8 +106,9 @@ impl<'app> TestApp<'app> {
105106
let config_path = tempdir().unwrap();
106107
let state = initialize(config_path.path().to_path_buf());
107108
let execution = ExecutionContext::try_new(&state.config.execution, AppType::Tui).unwrap();
109+
let args = DftArgs::default();
108110
let app_execution = AppExecution::new(execution);
109-
let app = App::new(state, app_execution);
111+
let app = App::new(state, args, app_execution);
110112
Self { config_path, app }
111113
}
112114

0 commit comments

Comments
 (0)