-
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.
update ndc-spec to 0.1.0-rc14 (#283)
- depends on: hasura/ndc-hub#93 - todo: add changelog entry ### What We are updating connector to support the latest ndc-connector spec (0.1.0-rc14). While fixing some breaking changes, such as renaming `where` to `predicate`, we also support new capabilities: 1. Explaining mutations via the `/mutation/explain` endpoint 2. Supporting filtering using `in` by columns and variables ### How 1. Since a mutation request may contain multiple mutation operations, the explain response will contain a field named `<operation_name> SQL Mutation` and a field named `<operation_name> Execution Plan` for each operation. 2. To filter in column or variable, we need to compare against a subquery of the shape `(select value from unnest(<column-or-variable>) as table(value))`.
- Loading branch information
Gil Mizrahi
authored
Feb 14, 2024
1 parent
121ec33
commit 553ad47
Showing
115 changed files
with
1,878 additions
and
528 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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/explain` endpoint to return a mutation execution plan. | ||
//! See the Hasura | ||
//! [Native Data Connector Specification](https://hasura.github.io/ndc-spec/specification/explain.html) | ||
//! for further details. | ||
use std::collections::BTreeMap; | ||
|
||
use tracing::{info_span, Instrument}; | ||
|
||
use ndc_postgres_configuration as configuration; | ||
|
||
use crate::mutation; | ||
use ndc_sdk::connector; | ||
use ndc_sdk::models; | ||
|
||
use crate::state; | ||
|
||
/// Explain a mutation by creating an execution plan. | ||
/// | ||
/// This function implements the [mutation/explain endpoint](https://hasura.github.io/ndc-spec/specification/explain.html) | ||
/// from the NDC specification. | ||
pub async fn explain<'a>( | ||
configuration: configuration::RuntimeConfiguration<'_>, | ||
state: &state::State, | ||
mutation_request: models::MutationRequest, | ||
) -> Result<models::ExplainResponse, connector::ExplainError> { | ||
async move { | ||
tracing::info!( | ||
mutation_request_json = serde_json::to_string(&mutation_request).unwrap(), | ||
mutation_request = ?mutation_request | ||
); | ||
|
||
// Compile the mutation. | ||
let plan = async { mutation::plan_mutation(configuration, state, mutation_request) } | ||
.instrument(info_span!("Plan mutation")) | ||
.await | ||
.map_err(|err| convert_mutation_error(&err))?; | ||
|
||
// Execute an explain query. | ||
let results = query_engine_execution::mutation::explain( | ||
&state.pool, | ||
&state.database_info, | ||
&state.metrics, | ||
plan, | ||
) | ||
.instrument(info_span!("Explain mutation")) | ||
.await | ||
.map_err(|err| { | ||
tracing::error!("{}", err); | ||
log_err_metrics_and_convert_error(state, &err) | ||
})?; | ||
|
||
state.metrics.record_successful_explain(); | ||
|
||
let details: BTreeMap<String, String> = results | ||
.into_iter() | ||
.flat_map(|(name, sql, plan)| { | ||
vec![ | ||
(format!("{name} SQL Mutation"), sql), | ||
(format!("{name} Execution Plan"), plan), | ||
] | ||
}) | ||
.collect(); | ||
|
||
let response = models::ExplainResponse { details }; | ||
|
||
Ok(response) | ||
} | ||
.instrument(info_span!("/explain")) | ||
.await | ||
} | ||
|
||
fn log_err_metrics_and_convert_error( | ||
state: &state::State, | ||
err: &query_engine_execution::mutation::Error, | ||
) -> connector::ExplainError { | ||
match err { | ||
query_engine_execution::mutation::Error::Query(err) => { | ||
tracing::error!("{}", err); | ||
// log error metric | ||
match &err { | ||
query_engine_execution::mutation::QueryError::NotSupported(_) => { | ||
state.metrics.error_metrics.record_unsupported_feature(); | ||
connector::ExplainError::UnsupportedOperation(err.to_string()) | ||
} | ||
query_engine_execution::mutation::QueryError::DBError(_) => { | ||
state.metrics.error_metrics.record_invalid_request(); | ||
connector::ExplainError::UnprocessableContent(err.to_string()) | ||
} | ||
query_engine_execution::mutation::QueryError::DBConstraintError(_) => { | ||
state.metrics.error_metrics.record_invalid_request(); | ||
connector::ExplainError::UnprocessableContent(err.to_string()) | ||
} | ||
} | ||
} | ||
query_engine_execution::mutation::Error::DB(err) => { | ||
tracing::error!("{}", err); | ||
state.metrics.error_metrics.record_database_error(); | ||
connector::ExplainError::Other(err.to_string().into()) | ||
} | ||
query_engine_execution::mutation::Error::Multiple(err1, err2) => { | ||
log_err_metrics_and_convert_error(state, err1); | ||
log_err_metrics_and_convert_error(state, err2); | ||
connector::ExplainError::Other(err.to_string().into()) | ||
} | ||
} | ||
} | ||
|
||
fn convert_mutation_error(err: &connector::MutationError) -> connector::ExplainError { | ||
match err { | ||
connector::MutationError::InvalidRequest(_) => { | ||
connector::ExplainError::InvalidRequest(err.to_string()) | ||
} | ||
connector::MutationError::UnprocessableContent(_) => { | ||
connector::ExplainError::UnprocessableContent(err.to_string()) | ||
} | ||
connector::MutationError::UnsupportedOperation(_) => { | ||
connector::ExplainError::UnsupportedOperation(err.to_string()) | ||
} | ||
connector::MutationError::Conflict(_) => { | ||
connector::ExplainError::Other(err.to_string().into()) | ||
} | ||
connector::MutationError::ConstraintNotMet(_) => { | ||
connector::ExplainError::Other(err.to_string().into()) | ||
} | ||
connector::MutationError::Other(_) => { | ||
connector::ExplainError::Other(err.to_string().into()) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.