Skip to main content

normalize_plan

Function normalize_plan 

Source
pub fn normalize_plan(plan: &ValidatedPlan) -> String
Expand description

Serialises a validated plan into a canonical JSON string suitable for hashing.

The normalisation rules are:

  1. Serialise the QueryPlan to a serde_json::Value (field order follows the struct definition — serde_json uses BTreeMap internally for Value::Object, so keys are already sorted).
  2. Recursively sort the keys of every JSON object (defensive — serde_json already sorts, but this guards against future changes).
  3. Serialise the sorted value to a compact string (no whitespace).

This ensures two plans that are semantically identical but have fields in different orders produce the same normalised string.

§Examples

use vlorql_core::cache::normalize_plan;
use vlorql_core::schema::{QueryPlan, Projection, FromClause};
use vlorql_core::validate::ValidatedPlan;
use std::sync::Arc;

let plan = QueryPlan {
    select: vec![Projection::Column {
        table: None, column: "id".to_owned(), alias: None,
    }],
    from: FromClause { table: "users".to_owned(), alias: None },
    r#where: None, group_by: None, having: None,
    order_by: None, limit: None, offset: None,
    joins: None, ctes: None, distinct: false, distinct_on: None, set_operation: None,
};
let validated = ValidatedPlan(Arc::new(plan));
let json = normalize_plan(&validated);
assert!(json.contains("users"));
assert!(json.contains("id"));