Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions arrow-flight/examples/flight_sql_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ impl FlightSqlService for FlightSqlServiceImpl {
prepared_statement_handle: FAKE_HANDLE.into(),
dataset_schema: schema_bytes,
parameter_schema: Default::default(), // TODO: parameters
is_update: Some(false),
};
Ok(res)
}
Expand Down Expand Up @@ -952,6 +953,7 @@ mod tests {
auth_client(&mut client).await;

let mut stmt = client.prepare("select 1;".to_string(), None).await.unwrap();
assert_eq!(stmt.is_update(), Some(false));

let flight_info = stmt.execute().await.unwrap();

Expand Down
5 changes: 5 additions & 0 deletions arrow-flight/src/sql/arrow.flight.protocol.sql.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions arrow-flight/src/sql/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ where
prepared_result.prepared_statement_handle,
dataset_schema,
parameter_schema,
prepared_result.is_update,
))
}

Expand Down Expand Up @@ -491,6 +492,7 @@ pub struct PreparedStatement<T> {
handle: Bytes,
dataset_schema: Schema,
parameter_schema: Schema,
is_update: Option<bool>,
}

impl<T> PreparedStatement<T>
Expand All @@ -505,13 +507,15 @@ where
handle: impl Into<Bytes>,
dataset_schema: Schema,
parameter_schema: Schema,
is_update: Option<bool>,
) -> Self {
PreparedStatement {
flight_sql_client: flight_client,
parameter_binding: None,
handle: handle.into(),
dataset_schema,
parameter_schema,
is_update,
}
}

Expand Down Expand Up @@ -562,6 +566,43 @@ where
Ok(&self.dataset_schema)
}

/// Returns the server's indication of how this statement should be executed.
///
/// `Some(true)` indicates [`Self::execute_update`], `Some(false)` indicates
/// [`Self::execute`], and `None` means the server did not provide this information.
///
/// When the field is absent, the caller chooses an execution method based on
/// its knowledge of the statement. This example falls back to query execution
/// because it prepares a `SELECT` statement.
///
/// ```no_run
/// # use arrow_flight::error::FlightError;
/// # use arrow_flight::sql::client::FlightSqlServiceClient;
/// # use tonic::transport::Channel;
/// # async fn example(mut client: FlightSqlServiceClient<Channel>) -> Result<(), FlightError> {
/// let mut statement = client.prepare("SELECT 1".to_string(), None).await?;
/// match statement.is_update() {
/// Some(true) => {
/// let affected_rows = statement.execute_update().await?;
/// println!("{affected_rows} rows affected");
/// }
/// Some(false) => {
/// let flight_info = statement.execute().await?;
/// println!("{flight_info:?}");
/// }
/// None => {
/// // The application knows this statement returns a result set.
/// let flight_info = statement.execute().await?;
/// println!("{flight_info:?}");
/// }
/// }
/// # Ok(())
/// # }
/// ```
pub fn is_update(&self) -> Option<bool> {
self.is_update
}

/// Set a RecordBatch that contains the parameters that will be bind.
pub fn set_parameters(&mut self, parameter_binding: RecordBatch) -> Result<()> {
self.parameter_binding = Some(parameter_binding);
Expand Down
35 changes: 32 additions & 3 deletions arrow-flight/tests/flight_sql_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
mod common;

use crate::common::fixture::TestFixture;
use crate::common::server::TestFlightServer;
use crate::common::utils::make_primitive_batch;

use arrow_array::RecordBatch;
Expand All @@ -15,9 +16,10 @@ use arrow_flight::flight_service_server::FlightServiceServer;
use arrow_flight::sql::client::FlightSqlServiceClient;
use arrow_flight::sql::server::{FlightSqlService, PeekableFlightDataStream};
use arrow_flight::sql::{
ActionBeginTransactionRequest, ActionBeginTransactionResult, ActionEndTransactionRequest,
CommandStatementIngest, EndTransaction, FallibleRequestStream, ProstMessageExt,
TableDefinitionOptions, TableExistsOption, TableNotExistOption,
ActionBeginTransactionRequest, ActionBeginTransactionResult,
ActionCreatePreparedStatementResult, ActionEndTransactionRequest, CommandStatementIngest,
EndTransaction, FallibleRequestStream, ProstMessageExt, TableDefinitionOptions,
TableExistsOption, TableNotExistOption,
};
use arrow_flight::{Action, FlightData, FlightDescriptor};
use futures::{StreamExt, TryStreamExt};
Expand All @@ -28,6 +30,33 @@ use tokio::sync::Mutex;
use tonic::{IntoStreamingRequest, Request, Status};
use uuid::Uuid;

#[tokio::test]
pub async fn test_prepare_is_update() {
let test_server = TestFlightServer::new();
let fixture = TestFixture::new(test_server.service()).await;
let channel = fixture.channel().await;
let mut flight_sql_client = FlightSqlServiceClient::new(channel);

for is_update in [Some(true), Some(false), None] {
let response = ActionCreatePreparedStatementResult {
prepared_statement_handle: "handle".into(),
is_update,
..Default::default()
};
test_server.set_do_action_response(vec![Ok(arrow_flight::Result::new(
response.as_any().encode_to_vec(),
))]);

let statement = flight_sql_client
.prepare("SELECT 1".to_string(), None)
.await
.unwrap();
assert_eq!(statement.is_update(), is_update);
}

fixture.shutdown_and_wait().await;
}

#[tokio::test]
pub async fn test_begin_end_transaction() {
let test_server = FlightSqlServiceImpl::new();
Expand Down
1 change: 1 addition & 0 deletions arrow-flight/tests/flight_sql_client_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ impl FlightSqlServiceImpl {
prepared_statement_handle: handle.into(),
dataset_schema: serialize_schema(&schema)?,
parameter_schema: serialize_schema(&parameter_schema)?,
is_update: None,
})
}

Expand Down
5 changes: 5 additions & 0 deletions format/FlightSql.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,11 @@ message ActionCreatePreparedStatementResult {
// If the query provided contained parameters, parameter_schema contains the
// schema of the expected parameters. It should be an IPC-encapsulated Schema, as described in Schema.fbs.
bytes parameter_schema = 3;

// When set to true, the query should be executed with CommandPreparedStatementUpdate,
// when set to false, the query should be executed with CommandPreparedStatementQuery.
// If not set, the client can choose how to execute the query.
optional bool is_update = 4;
}

/*
Expand Down
Loading