pub fn normalize_plan(plan: &ValidatedPlan) -> StringExpand description
Serialises a validated plan into a canonical JSON string suitable for hashing.
The normalisation rules are:
- Serialise the
QueryPlanto aserde_json::Value(field order follows the struct definition —serde_jsonusesBTreeMapinternally forValue::Object, so keys are already sorted). - Recursively sort the keys of every JSON object (defensive —
serde_jsonalready sorts, but this guards against future changes). - 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"));