Skip to main content

hawdb_optimizer_graph/
trace.rs

1// Copyright 2026 Nowledge
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::cost::{PlanCost, PlanCostBreakdown};
16use crate::properties::PhysicalProperties;
17use crate::search::{RuleEvent, SearchMode};
18use crate::stage::StageTrace;
19use hawdb_plan_cypher::{PhysicalOperatorId, PhysicalPlanKind};
20use std::collections::BTreeMap;
21
22pub use hawdb_cascades::OptimizerConfig;
23
24#[derive(Debug, Clone, PartialEq, Eq)]
25pub struct OptimizerTrace {
26    pub groups: usize,
27    pub search_mode: SearchMode,
28    pub query_digest: Option<String>,
29    pub selected_plan: String,
30    /// The physical operator-tree shape. Bound values are intentionally absent.
31    pub selected_plan_fingerprint: String,
32    pub selected_plan_cost: PlanCost,
33    pub selected_plan_cost_breakdown: PlanCostBreakdown,
34    pub selected_plan_properties: PhysicalProperties,
35    /// Per-operator cardinalities keyed by stable, plan-local pre-order identities.
36    pub selected_plan_cardinality_estimates: Vec<OperatorCardinalityEstimate>,
37    pub selected_plan_operator_counts: BTreeMap<String, usize>,
38    pub selected_plan_class_counts: BTreeMap<String, usize>,
39    pub warnings: Vec<String>,
40    pub decisions: Vec<String>,
41    pub rule_events: Vec<RuleEvent>,
42    pub stage_events: Vec<StageTrace>,
43}
44
45/// The optimizer's output-cardinality estimate for one physical operator.
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub struct OperatorCardinalityEstimate {
48    pub operator_id: PhysicalOperatorId,
49    pub operator: PhysicalPlanKind,
50    pub estimated_rows: u64,
51}
52
53impl hawdb_core::RetainedHeapBytes for OptimizerTrace {
54    fn retained_heap_bytes(&self) -> Option<usize> {
55        let Self {
56            groups,
57            search_mode,
58            query_digest,
59            selected_plan,
60            selected_plan_fingerprint,
61            selected_plan_cost,
62            selected_plan_cost_breakdown,
63            selected_plan_properties,
64            selected_plan_cardinality_estimates,
65            selected_plan_operator_counts,
66            selected_plan_class_counts,
67            warnings,
68            decisions,
69            rule_events,
70            stage_events,
71        } = self;
72        fn leaf<T: Copy>(_: &T) {}
73        leaf(groups);
74        leaf(search_mode);
75        leaf(selected_plan_cost);
76        leaf(selected_plan_cost_breakdown);
77        fn vector<T>(values: &Vec<T>, heap: impl Fn(&T) -> Option<usize>) -> Option<usize> {
78            values.iter().try_fold(
79                values.capacity().checked_mul(std::mem::size_of::<T>())?,
80                |bytes, value| bytes.checked_add(heap(value)?),
81            )
82        }
83        hawdb_core::checked_retained_heap_sum([
84            query_digest.retained_heap_bytes(),
85            selected_plan.retained_heap_bytes(),
86            selected_plan_fingerprint.retained_heap_bytes(),
87            selected_plan_properties.retained_heap_bytes(),
88            vector(selected_plan_cardinality_estimates, |estimate| {
89                leaf(estimate);
90                Some(0)
91            }),
92            selected_plan_operator_counts.retained_heap_bytes(),
93            selected_plan_class_counts.retained_heap_bytes(),
94            warnings.retained_heap_bytes(),
95            decisions.retained_heap_bytes(),
96            vector(rule_events, RuleEvent::retained_heap_bytes),
97            vector(stage_events, StageTrace::retained_heap_bytes),
98        ])
99    }
100}