Skip to content

Commit

Permalink
Merge pull request #14 from hasura/allow-explain-invalid-queries
Browse files Browse the repository at this point in the history
remove error handling for invalid foreach requests
  • Loading branch information
BenoitRanque authored Apr 12, 2024
2 parents 54e7819 + 509c369 commit cd1f81b
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 15 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.4]

- Allow explain of invalid foreach queries. Will generate invalid SQL, for proper execution these should be parameterized

## [0.2.3]

- Fix ordering of result sets for foreach queries (remote joins)
Expand Down
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ members = [
]
resolver = "2"

package.version = "0.2.3"
package.version = "0.2.4"
package.edition = "2021"
29 changes: 28 additions & 1 deletion crates/ndc-clickhouse/src/connector/handler/explain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,18 @@ pub async fn explain(
let details = BTreeMap::from_iter(vec![
(
"SQL Query".to_string(),
pretty_print_sql(&unsafe_statement.to_unsafe_sql_string()),
add_variables_note(
&request,
&pretty_print_sql(&unsafe_statement.to_unsafe_sql_string()),
),
),
(
"Parameterized SQL Query".to_string(),
add_variables_note(&request, &pretty_print_sql(&statement_string)),
),
(
"Parameters".to_string(),
serde_json::to_string(&parameters).map_err(|err| ExplainError::Other(Box::new(err)))?,
),
("Execution Plan".to_string(), explain),
]);
Expand All @@ -68,3 +79,19 @@ fn pretty_print_sql(query: &str) -> String {

format(query, &params, options)
}

const EXPLAIN_NOTE: &str = r#"-- note: the source object for _vars should be a JSON string of the form
-- `{"_varset_id": [1,2,3], "_var_ID": [1,2,3], "_var_NAME": ["Name1","Name2","Name3"]}`
-- The example assumes the variables ID and NAME, change as appropriate. "_varset_id" is an index starting from 1
-- Each array member corresponds to a row, all arrays should have the same number of members. See clickhouse docs for more:
-- https://clickhouse.com/docs/en/interfaces/formats#jsoncolumns
-- https://clickhouse.com/docs/en/sql-reference/table-functions/format
"#;

fn add_variables_note(request: &models::QueryRequest, statement: &str) -> String {
if request.variables.is_some() {
format!("{EXPLAIN_NOTE}\n{statement}")
} else {
statement.to_owned()
}
}
4 changes: 0 additions & 4 deletions crates/ndc-clickhouse/src/sql/query_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ impl<'r, 'c> QueryBuilder<'r, 'c> {
};

let with = if let Some(variables) = &self.request.variables {
if variables.is_empty() {
return Err(QueryBuilderError::EmptyQueryVariablesList);
}
let mut variable_values: IndexMap<String, Vec<serde_json::Value>> = IndexMap::new();

variable_values.insert(
Expand Down Expand Up @@ -1658,7 +1655,6 @@ impl<'r, 'c> QueryBuilder<'r, 'c> {
}
}
}

fn collection_relationship(
&self,
relationship: &str,
Expand Down
6 changes: 0 additions & 6 deletions crates/ndc-clickhouse/src/sql/query_builder/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ pub enum QueryBuilderError {
Unexpected(String),
/// There was an issue creating typecasting strings
Typecasting(String),
/// An empty list of variables was passed. If variables are passed, we expect at least one set.
EmptyQueryVariablesList,
}

impl fmt::Display for QueryBuilderError {
Expand Down Expand Up @@ -66,10 +64,6 @@ impl fmt::Display for QueryBuilderError {
QueryBuilderError::NotSupported(e) => write!(f, "Not supported: {e}"),
QueryBuilderError::Unexpected(e) => write!(f, "Unexpected: {e}"),
QueryBuilderError::Typecasting(e) => write!(f, "Typecasting: {e}"),
QueryBuilderError::EmptyQueryVariablesList => write!(
f,
"Empty query variables list: we expect at least one set, or no list."
),
}
}
}
Expand Down

0 comments on commit cd1f81b

Please sign in to comment.