-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from hasura/djh/explain-endpoint
Add `explain` endpoint
- Loading branch information
Showing
12 changed files
with
259 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//! Implement the `/explain` endpoint to return a query 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_sdk::connector; | ||
use ndc_sdk::models; | ||
use query_engine_execution::execution; | ||
use query_engine_sql::sql; | ||
use query_engine_translation::translation; | ||
|
||
use super::configuration; | ||
|
||
/// Explain a query by creating an execution plan | ||
/// | ||
/// This function implements the [explain endpoint](https://hasura.github.io/ndc-spec/specification/explain.html) | ||
/// from the NDC specification. | ||
pub async fn explain( | ||
configuration: &configuration::Configuration, | ||
state: &configuration::State, | ||
query_request: models::QueryRequest, | ||
) -> Result<models::ExplainResponse, connector::ExplainError> { | ||
async move { | ||
tracing::info!( | ||
query_request_json = serde_json::to_string(&query_request).unwrap(), | ||
query_request = ?query_request | ||
); | ||
|
||
// Compile the query. | ||
let plan = async { plan_query(configuration, state, query_request) } | ||
.instrument(info_span!("Plan query")) | ||
.await?; | ||
|
||
// Execute an explain query. | ||
let (query, plan) = execution::explain(&state.mssql_pool, plan) | ||
.instrument(info_span!("Explain query")) | ||
.await | ||
.map_err(|err| match err { | ||
execution::Error::Query(err) => { | ||
tracing::error!("{}", err); | ||
|
||
connector::ExplainError::Other(err.to_string().into()) | ||
} | ||
execution::Error::ConnectionPool(err) => { | ||
tracing::error!("{}", err); | ||
|
||
connector::ExplainError::Other(err.to_string().into()) | ||
} | ||
execution::Error::TiberiusError(err) => { | ||
tracing::error!("{}", err); | ||
|
||
connector::ExplainError::Other(err.to_string().into()) | ||
} | ||
})?; | ||
|
||
state.metrics.record_successful_explain(); | ||
|
||
let details = | ||
BTreeMap::from_iter([("SQL Query".into(), query), ("Execution Plan".into(), plan)]); | ||
|
||
let response = models::ExplainResponse { details }; | ||
|
||
Ok(response) | ||
} | ||
.instrument(info_span!("/explain")) | ||
.await | ||
} | ||
|
||
fn plan_query( | ||
configuration: &configuration::Configuration, | ||
state: &configuration::State, | ||
query_request: models::QueryRequest, | ||
) -> Result<sql::execution_plan::ExecutionPlan, connector::ExplainError> { | ||
let timer = state.metrics.time_query_plan(); | ||
let result = translation::query::translate(&configuration.config.metadata, query_request) | ||
.map_err(|err| { | ||
tracing::error!("{}", err); | ||
match err { | ||
translation::query::error::Error::CapabilityNotSupported(_) => { | ||
state.metrics.error_metrics.record_unsupported_capability(); | ||
connector::ExplainError::UnsupportedOperation(err.to_string()) | ||
} | ||
_ => { | ||
state.metrics.error_metrics.record_invalid_request(); | ||
connector::ExplainError::InvalidRequest(err.to_string()) | ||
} | ||
} | ||
}); | ||
timer.complete_with(result) | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod configuration; | ||
pub mod connector; | ||
pub mod explain; | ||
pub mod query; | ||
pub mod schema; |
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 |
---|---|---|
@@ -1,27 +1,25 @@ | ||
/* | ||
pub mod common; | ||
|
||
use crate::common::{is_contained_in_lines, run_explain}; | ||
|
||
#[tokio::test] | ||
async fn select_by_pk() { | ||
let result = run_explain("select_by_pk").await; | ||
is_contained_in_lines(vec!["Aggregate", "Scan", "35"], result.details.plan); | ||
is_contained_in_lines(vec!["Clustered", "Index", "Seek"], result.details.plan); | ||
insta::assert_snapshot!(result.details.query); | ||
} | ||
|
||
#[tokio::test] | ||
async fn select_where_variable() { | ||
let result = run_explain("select_where_variable").await; | ||
is_contained_in_lines(vec!["Aggregate", "Seq Scan", "Filter"], result.details.plan); | ||
is_contained_in_lines(vec!["Clustered", "Index", "Scan"], result.details.plan); | ||
insta::assert_snapshot!(result.details.query); | ||
} | ||
|
||
#[tokio::test] | ||
async fn select_where_name_nilike() { | ||
let result = run_explain("select_where_name_nilike").await; | ||
let keywords = vec!["Aggregate", "Subquery Scan", "Limit", "Seq Scan", "Filter"]; | ||
let result = run_explain("select_where_name_like").await; | ||
let keywords = vec!["Compute", "Scalar"]; | ||
is_contained_in_lines(keywords, result.details.plan); | ||
insta::assert_snapshot!(result.details.query); | ||
} | ||
*/ |
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.