-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### What We want to support the [spec](https://hasura.github.io/ndc-spec/specification/mutations/index.html) definition for mutations over procedures by allowing users to define native queries that can do mutations. Native Query Mutations should return the columns of the table using a `RETURNING` clause, so we can select those when building the response query. ![Screenshot from 2023-11-28 17-24-58](https://github.com/hasura/ndc-postgres/assets/8547573/9451327a-9ab2-488e-82ae-65a23f3f63b2) ### How tl;dr, we: 1. revise the `ExecutionPlan` to support a list of mutations as well as a query. 2. translate each mutation to something like `with "nq" as (<native query>) select <fields>, <affected row> from <selects>`. 3. implement the execution of the mutations list by running them on after the other in a transaction and concatenate the results. 4. implement the `/mutation` endpoint to call the relevant parts of the translation and execution.
- Loading branch information
Gil Mizrahi
authored
Dec 1, 2023
1 parent
addc37f
commit 9e5377b
Showing
36 changed files
with
3,460 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
//! Implement the `/mutation` endpoint to run a mutation statement against postgres. | ||
//! See the Hasura | ||
//! [Native Data Connector Specification](https://hasura.github.io/ndc-spec/specification/mutations/index.html) | ||
//! for further details. | ||
|
||
use tracing::{info_span, Instrument}; | ||
|
||
use ndc_sdk::connector; | ||
use ndc_sdk::json_response::JsonResponse; | ||
use ndc_sdk::models; | ||
use query_engine_execution; | ||
use query_engine_sql::sql; | ||
use query_engine_translation::translation; | ||
|
||
use super::configuration; | ||
use super::state; | ||
|
||
/// Execute a mutation | ||
/// | ||
/// This function implements the [mutation endpoint](https://hasura.github.io/ndc-spec/specification/mutations/index.html) | ||
/// from the NDC specification. | ||
pub async fn mutation<'a>( | ||
configuration: &configuration::RuntimeConfiguration<'a>, | ||
state: &state::State, | ||
request: models::MutationRequest, | ||
) -> Result<JsonResponse<models::MutationResponse>, connector::MutationError> { | ||
let timer = state.metrics.time_query_total(); | ||
|
||
// See https://docs.rs/tracing/0.1.29/tracing/span/struct.Span.html#in-asynchronous-code | ||
let result = async move { | ||
tracing::info!( | ||
request_json = serde_json::to_string(&request).unwrap(), | ||
request = ?request | ||
); | ||
|
||
let plan = async { plan_mutation(configuration, state, request) } | ||
.instrument(info_span!("Plan mutation")) | ||
.await?; | ||
|
||
let result = execute_mutation(state, plan) | ||
.instrument(info_span!("Execute mutation")) | ||
.await?; | ||
|
||
state.metrics.record_successful_mutation(); | ||
Ok(result) | ||
} | ||
.instrument(info_span!("/mutation")) | ||
.await; | ||
|
||
timer.complete_with(result) | ||
} | ||
|
||
fn plan_mutation( | ||
configuration: &configuration::RuntimeConfiguration, | ||
state: &state::State, | ||
request: models::MutationRequest, | ||
) -> Result< | ||
sql::execution_plan::ExecutionPlan<sql::execution_plan::Mutations>, | ||
connector::MutationError, | ||
> { | ||
let timer = state.metrics.time_query_plan(); | ||
let mutations = request | ||
.operations | ||
.into_iter() | ||
.map(|operation| { | ||
translation::mutation::translate( | ||
configuration.metadata, | ||
operation, | ||
request.collection_relationships.clone(), | ||
) | ||
.map_err(|err| { | ||
tracing::error!("{}", err); | ||
// log metrics | ||
match err { | ||
translation::error::Error::CapabilityNotSupported(_) => { | ||
state.metrics.error_metrics.record_unsupported_capability(); | ||
connector::MutationError::UnsupportedOperation(err.to_string()) | ||
} | ||
translation::error::Error::NotImplementedYet(_) => { | ||
state.metrics.error_metrics.record_unsupported_feature(); | ||
connector::MutationError::UnsupportedOperation(err.to_string()) | ||
} | ||
_ => { | ||
state.metrics.error_metrics.record_invalid_request(); | ||
connector::MutationError::InvalidRequest(err.to_string()) | ||
} | ||
} | ||
}) | ||
}) | ||
.collect::<Result<Vec<_>, connector::MutationError>>()?; | ||
timer.complete_with(Ok(sql::execution_plan::simple_mutations_execution_plan( | ||
mutations, | ||
))) | ||
} | ||
|
||
async fn execute_mutation( | ||
state: &state::State, | ||
plan: sql::execution_plan::ExecutionPlan<sql::execution_plan::Mutations>, | ||
) -> Result<JsonResponse<models::MutationResponse>, connector::MutationError> { | ||
query_engine_execution::mutation::execute( | ||
&state.pool, | ||
&state.database_info, | ||
&state.metrics, | ||
plan, | ||
) | ||
.await | ||
.map(JsonResponse::Serialized) | ||
.map_err(|err| { | ||
tracing::error!("{}", err); | ||
log_err_metrics(state, &err); | ||
connector::MutationError::Other(err.to_string().into()) | ||
}) | ||
} | ||
|
||
fn log_err_metrics(state: &state::State, err: &query_engine_execution::mutation::Error) { | ||
match err { | ||
query_engine_execution::mutation::Error::Query(err) => match &err { | ||
query_engine_execution::mutation::QueryError::NotSupported(_) => { | ||
state.metrics.error_metrics.record_unsupported_feature() | ||
} | ||
}, | ||
query_engine_execution::mutation::Error::DB(_) => { | ||
state.metrics.error_metrics.record_database_error(); | ||
} | ||
query_engine_execution::mutation::Error::Multiple(err1, err2) => { | ||
log_err_metrics(state, err1); | ||
log_err_metrics(state, err2); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.