Skip to main content

hawdb_cascades/
context.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
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub struct OptimizerConfig {
17    pub max_groups: usize,
18}
19
20impl Default for OptimizerConfig {
21    fn default() -> Self {
22        Self { max_groups: 128 }
23    }
24}
25
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub enum QueryFamily {
28    GraphRead,
29    GraphWrite,
30    VectorSearch,
31    Schema,
32    System,
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum StatementClass {
37    Read,
38    Write,
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum ExplainMode {
43    None,
44    Explain,
45    Analyze,
46}
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub enum TraceSink {
50    Disabled,
51    Collect,
52}
53
54#[derive(Debug, Clone, PartialEq, Eq)]
55pub struct ResourceHints {
56    pub priority: u8,
57    pub max_memory_bytes: Option<u64>,
58    pub max_parallelism: usize,
59}
60
61impl Default for ResourceHints {
62    fn default() -> Self {
63        Self {
64            priority: 128,
65            max_memory_bytes: None,
66            max_parallelism: 1,
67        }
68    }
69}
70
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub struct OptimizerContext {
73    normalized_query: Option<String>,
74    query_digest: Option<String>,
75    query_family: QueryFamily,
76    statement_class: StatementClass,
77    resource_hints: ResourceHints,
78    explain_mode: ExplainMode,
79    trace_sink: TraceSink,
80    optimizer_config: OptimizerConfig,
81    qualification: crate::OptimizerQualificationOptions,
82}
83
84impl Default for OptimizerContext {
85    fn default() -> Self {
86        Self {
87            normalized_query: None,
88            query_digest: None,
89            query_family: QueryFamily::GraphRead,
90            statement_class: StatementClass::Read,
91            resource_hints: ResourceHints::default(),
92            explain_mode: ExplainMode::None,
93            trace_sink: TraceSink::Collect,
94            optimizer_config: OptimizerConfig::default(),
95            qualification: crate::OptimizerQualificationOptions::default(),
96        }
97    }
98}
99
100impl OptimizerContext {
101    pub fn from_config(optimizer_config: OptimizerConfig) -> Self {
102        Self {
103            optimizer_config,
104            ..Self::default()
105        }
106    }
107
108    pub fn with_normalized_query(mut self, normalized_query: impl Into<String>) -> Self {
109        self.normalized_query = Some(normalized_query.into());
110        self
111    }
112
113    pub fn with_query_identity(
114        mut self,
115        normalized_query: impl Into<String>,
116        query_digest: impl Into<String>,
117    ) -> Self {
118        self.normalized_query = Some(normalized_query.into());
119        self.query_digest = Some(query_digest.into());
120        self
121    }
122
123    pub fn with_query_family(mut self, query_family: QueryFamily) -> Self {
124        self.query_family = query_family;
125        self.statement_class = match query_family {
126            QueryFamily::GraphWrite | QueryFamily::Schema => StatementClass::Write,
127            QueryFamily::GraphRead | QueryFamily::VectorSearch | QueryFamily::System => {
128                StatementClass::Read
129            }
130        };
131        self
132    }
133
134    pub fn with_resource_hints(mut self, resource_hints: ResourceHints) -> Self {
135        self.resource_hints = resource_hints;
136        self
137    }
138
139    pub fn with_explain_mode(mut self, explain_mode: ExplainMode) -> Self {
140        self.explain_mode = explain_mode;
141        self
142    }
143
144    pub fn with_trace_sink(mut self, trace_sink: TraceSink) -> Self {
145        self.trace_sink = trace_sink;
146        self
147    }
148
149    pub fn with_qualification_options(
150        mut self,
151        options: crate::OptimizerQualificationOptions,
152    ) -> Self {
153        self.qualification = options;
154        self
155    }
156
157    pub fn qualification_options(&self) -> crate::OptimizerQualificationOptions {
158        self.qualification
159    }
160
161    pub fn normalized_query(&self) -> Option<&str> {
162        self.normalized_query.as_deref()
163    }
164
165    pub fn query_digest(&self) -> Option<&str> {
166        self.query_digest.as_deref()
167    }
168
169    pub fn query_family(&self) -> QueryFamily {
170        self.query_family
171    }
172
173    pub fn statement_class(&self) -> StatementClass {
174        self.statement_class
175    }
176
177    pub fn resource_hints(&self) -> &ResourceHints {
178        &self.resource_hints
179    }
180
181    pub fn explain_mode(&self) -> ExplainMode {
182        self.explain_mode
183    }
184
185    pub fn trace_sink(&self) -> TraceSink {
186        self.trace_sink
187    }
188
189    pub fn optimizer_config(&self) -> &OptimizerConfig {
190        &self.optimizer_config
191    }
192}
193
194#[cfg(test)]
195mod tests {
196    use super::*;
197
198    #[test]
199    fn query_family_derives_statement_class_with_query_identity() {
200        let context = OptimizerContext::default()
201            .with_query_family(QueryFamily::VectorSearch)
202            .with_query_identity("match (m) return m", "q1:test");
203
204        assert_eq!(context.statement_class(), StatementClass::Read);
205        assert_eq!(context.query_digest(), Some("q1:test"));
206        assert_eq!(context.normalized_query(), Some("match (m) return m"));
207    }
208
209    #[test]
210    fn resource_hints_are_bounded_by_explicit_parallelism() {
211        let context = OptimizerContext::default().with_resource_hints(ResourceHints {
212            priority: 200,
213            max_memory_bytes: Some(16 * 1024 * 1024),
214            max_parallelism: 2,
215        });
216
217        assert_eq!(context.resource_hints().max_parallelism, 2);
218        assert_eq!(
219            context.resource_hints().max_memory_bytes,
220            Some(16 * 1024 * 1024)
221        );
222    }
223}