Skip to content

Commit

Permalink
refactor(postgres): simplify query plan handling
Browse files Browse the repository at this point in the history
  • Loading branch information
abonander committed Jul 31, 2023
1 parent 9102d5d commit b638e63
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions sqlx-postgres/src/connection/describe.rs
Expand Up @@ -451,11 +451,15 @@ WHERE rngtypid = $1

let mut nullables = Vec::new();

if let Explain::QueryPlan(query_plan @ QueryPlan { plan, .. }) = &explain {
if let Some(outputs) = &query_plan.plan.output {
nullables.resize(outputs.len(), None);
visit_plan(&plan, outputs, &mut nullables);
}
if let Some(
plan @ Plan {
output: Some(ref outputs),
..
},
) = &explain.plan
{
nullables.resize(outputs.len(), None);
visit_plan(plan, outputs, &mut nullables);
}

Ok(nullables)
Expand Down Expand Up @@ -488,19 +492,16 @@ fn visit_plan(plan: &Plan, outputs: &[String], nullables: &mut Vec<Option<bool>>
}

#[derive(serde::Deserialize)]
#[serde(untagged)]
enum Explain {
/// {"Plan": ...} -- returned for most statements
QueryPlan(QueryPlan),
/// The string "Utility Statement" -- returned for
/// a CALL statement
UtilityStatement(String),
}

#[derive(serde::Deserialize)]
struct QueryPlan {
#[serde(rename = "Plan")]
plan: Plan,
struct Explain {
// NOTE: the returned JSON may not contain a `plan` field, for example, with `CALL` statements:
// https://github.com/launchbadge/sqlx/issues/1449
//
// In this case, we should just fall back to assuming all is nullable.
#[serde(default)]
plan: Option<Plan>,
// It may also contain additional fields we don't care about, which should not break parsing:
// https://github.com/launchbadge/sqlx/issues/2587
// https://github.com/launchbadge/sqlx/issues/2622
}

#[derive(serde::Deserialize)]
Expand Down

0 comments on commit b638e63

Please sign in to comment.