1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! Query execution results for graph databases
use crate::ast::ast::{CatalogPath, GraphExpression};
use crate::storage::Value;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
/// Entity identifier for tracking graph element identities in set operations
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EntityId {
/// Node with its internal ID
Node(String),
/// Edge with its internal ID
Edge(String),
}
/// Session change request returned by executor for session statements
/// Following PostgreSQL/Oracle pattern where executor validates and returns metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SessionResult {
/// Set the current graph for the session
SetGraph {
graph_expression: GraphExpression,
validated: bool, // Whether executor validated the graph exists
},
/// Set the current schema for the session
SetSchema {
schema_reference: CatalogPath,
validated: bool, // Whether executor validated the schema exists
},
/// Set session timezone
SetTimeZone { timezone: String },
/// Reset session to defaults
Reset,
/// Close session
Close,
}
/// Query execution result for graph queries
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryResult {
pub rows: Vec<Row>,
/// Variable bindings from the RETURN clause (e.g., ["p.name", "p.age"])
pub variables: Vec<String>,
pub execution_time_ms: u64,
pub rows_affected: usize,
/// Session change request if this was a session statement
pub session_result: Option<SessionResult>,
/// Warnings generated during query execution (e.g., duplicate insert detection)
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub warnings: Vec<String>,
}
impl QueryResult {
/// Create a new empty query result
pub fn new() -> Self {
Self {
rows: Vec::new(),
variables: Vec::new(),
execution_time_ms: 0,
rows_affected: 0,
session_result: None,
warnings: Vec::new(),
}
}
/// Create a query result for a session statement
pub fn for_session(session_result: SessionResult) -> Self {
Self {
rows: Vec::new(),
variables: Vec::new(),
execution_time_ms: 0,
rows_affected: 0,
session_result: Some(session_result),
warnings: Vec::new(),
}
}
/// Add a warning to the query result
pub fn add_warning(&mut self, warning: String) {
self.warnings.push(warning);
}
/// Check if there are any warnings
pub fn has_warnings(&self) -> bool {
!self.warnings.is_empty()
}
/// Get values from all rows at a specific position (for set operations)
pub fn get_values_at_position(&self, position: usize) -> Vec<&Value> {
self.rows
.iter()
.filter_map(|row| row.get_value_at_position(position))
.collect()
}
/// Create a new result with unified variable names for set operations
pub fn with_unified_variables(mut self, unified_variables: Vec<String>) -> Self {
self.variables = unified_variables;
self
}
/// Check if this result contains a session command
pub fn is_session_command(&self) -> bool {
self.session_result.is_some()
}
/// Get a formatted message for session commands (returns None if not a session command)
pub fn get_session_message(&self) -> Option<String> {
self.session_result.as_ref().map(|sr| match sr {
SessionResult::SetGraph {
graph_expression,
validated: _,
} => {
let graph_path = match graph_expression {
GraphExpression::Reference(path) => path.to_string(),
GraphExpression::Union { .. } => "UNION expression".to_string(),
GraphExpression::CurrentGraph => "CURRENT_GRAPH".to_string(),
};
format!("Session graph set to: {}", graph_path)
}
SessionResult::SetSchema {
schema_reference,
validated: _,
} => {
let schema_path = format!("/{}", schema_reference.segments.join("/"));
format!("Session schema set to: {}", schema_path)
}
SessionResult::SetTimeZone { timezone } => {
format!("Session timezone set to: {}", timezone)
}
SessionResult::Reset => "Session reset to defaults".to_string(),
SessionResult::Close => "Session closed".to_string(),
})
}
}
/// Single result row representing variable bindings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Row {
/// Named variable bindings (e.g., "p.name" -> "Alice")
pub values: HashMap<String, Value>,
/// Positional values for set operations (ordered by variable declaration)
pub positional_values: Vec<Value>,
/// Source entity IDs for identity-based set operations
/// Maps variable names to their graph entity IDs (e.g., "p" -> Node ID)
#[serde(default)]
pub source_entities: HashMap<String, EntityId>,
/// Text search relevance score (for text_match results)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub text_score: Option<f64>,
/// Highlighted snippet (for highlight() results)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub highlight_snippet: Option<String>,
}
impl Row {
/// Create a new empty row
pub fn new() -> Self {
Self {
values: HashMap::new(),
positional_values: Vec::new(),
source_entities: HashMap::new(),
text_score: None,
highlight_snippet: None,
}
}
/// Create a row from a HashMap of values (backward compatibility)
pub fn from_values(values: HashMap<String, Value>) -> Self {
Self {
values,
positional_values: Vec::new(),
source_entities: HashMap::new(),
text_score: None,
highlight_snippet: None,
}
}
/// Create a row from positional values with variable names
pub fn from_positional(values: Vec<Value>, variables: &[String]) -> Self {
let mut named_values = HashMap::new();
for (_i, (value, var_name)) in values.iter().zip(variables.iter()).enumerate() {
named_values.insert(var_name.clone(), value.clone());
}
Self {
values: named_values,
positional_values: values,
source_entities: HashMap::new(),
text_score: None,
highlight_snippet: None,
}
}
/// Set text search score
pub fn set_text_score(&mut self, score: f64) {
self.text_score = Some(score);
}
/// Get text search score
pub fn get_text_score(&self) -> Option<f64> {
self.text_score
}
/// Set highlight snippet
pub fn set_highlight_snippet(&mut self, snippet: String) {
self.highlight_snippet = Some(snippet);
}
/// Get highlight snippet
pub fn get_highlight_snippet(&self) -> Option<&str> {
self.highlight_snippet.as_deref()
}
/// Add a value to the row (both named and positional)
pub fn add_value(&mut self, name: String, value: Value) {
self.values.insert(name, value.clone());
self.positional_values.push(value);
}
/// Get a value by variable name
pub fn get_value(&self, name: &str) -> Option<&Value> {
self.values.get(name)
}
/// Get a value by position (for set operations)
pub fn get_value_at_position(&self, position: usize) -> Option<&Value> {
self.positional_values.get(position)
}
/// Set a value in the row
pub fn set_value(&mut self, name: String, value: Value) {
self.values.insert(name, value.clone());
// For simplicity, we'll rebuild positional values when setting individual values
// In a production system, you'd want to maintain position mapping
if !self.positional_values.is_empty() {
// Update the existing positional value if it exists
// This is a simplified approach - production code would need better position tracking
}
}
/// Track the source entity for a variable
/// This enables identity-based comparison in set operations
pub fn with_entity(&mut self, var_name: &str, value: &Value) {
match value {
Value::Node(node) => {
self.source_entities
.insert(var_name.to_string(), EntityId::Node(node.id.clone()));
}
Value::Edge(edge) => {
self.source_entities
.insert(var_name.to_string(), EntityId::Edge(edge.id.clone()));
}
_ => {
// Properties and computed values don't have entity IDs
// We might track their source entity in the future
}
}
}
/// Get the primary entity ID for this row (if any)
/// Used for simple identity comparisons
pub fn get_primary_entity(&self) -> Option<&EntityId> {
// Return the first entity (useful for single-entity queries)
self.source_entities.values().next()
}
/// Check if this row has any tracked entities
pub fn has_entities(&self) -> bool {
!self.source_entities.is_empty()
}
/// Get all tracked entities for this row
pub fn get_entities(&self) -> &HashMap<String, EntityId> {
&self.source_entities
}
}
impl Default for Row {
fn default() -> Self {
Self::new()
}
}
impl PartialEq for Row {
fn eq(&self, other: &Self) -> bool {
// For set operations, compare positionally if both have positional values
if !self.positional_values.is_empty() && !other.positional_values.is_empty() {
// SQL set operations: NULL != NULL
self.sql_set_equal(&other.positional_values)
} else {
// Fallback to named comparison for backward compatibility
self.sql_set_equal_named(other)
}
}
}
impl Row {
/// SQL set operation equality: NULL != NULL
fn sql_set_equal(&self, other_values: &[Value]) -> bool {
if self.positional_values.len() != other_values.len() {
return false;
}
for (self_val, other_val) in self.positional_values.iter().zip(other_values.iter()) {
// In SQL set operations, NULL != NULL
if matches!(self_val, Value::Null) || matches!(other_val, Value::Null) {
return false;
}
if self_val != other_val {
return false;
}
}
true
}
/// SQL set operation equality for named values
fn sql_set_equal_named(&self, other: &Self) -> bool {
if self.values.len() != other.values.len() {
return false;
}
for (key, self_val) in &self.values {
if let Some(other_val) = other.values.get(key) {
// In SQL set operations, NULL != NULL
if matches!(self_val, Value::Null) || matches!(other_val, Value::Null) {
return false;
}
if self_val != other_val {
return false;
}
} else {
return false;
}
}
true
}
}
impl Eq for Row {}
impl Hash for Row {
fn hash<H: Hasher>(&self, state: &mut H) {
// For set operations, hash positionally if available
if !self.positional_values.is_empty() {
self.positional_values.hash(state);
} else {
// Fallback to named hashing for backward compatibility
let mut items: Vec<_> = self.values.iter().collect();
items.sort_by_key(|(k, _)| *k);
for (key, value) in items {
key.hash(state);
value.hash(state);
}
}
}
}