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
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
use std::collections::HashMap;
use crate::ast::ast::InsertStatement;
use crate::exec::write_stmt::data_stmt::DataStatementExecutor;
use crate::exec::write_stmt::{ExecutionContext, StatementExecutor};
use crate::exec::ExecutionError;
use crate::plan::insert_planner::InsertPlanner;
use crate::plan::physical::PhysicalPlan;
use crate::storage::GraphCache;
use crate::txn::UndoOperation;
/// Executor for INSERT statements using planned execution
pub struct PlannedInsertExecutor {
statement: InsertStatement,
}
impl PlannedInsertExecutor {
/// Create a new PlannedInsertExecutor
pub fn new(statement: InsertStatement) -> Self {
Self { statement }
}
/// Update text indexes for a newly inserted node (automatic indexing)
///
/// This function finds all text indexes that match the node's labels and updates them
/// with the node's text property values.
fn update_text_indexes_for_node(
_node_id: &str,
labels: &[String],
_properties: &HashMap<String, crate::storage::Value>,
context: &mut ExecutionContext,
) {
// Get index manager from storage manager
let index_manager = match context.storage_manager.as_ref() {
Some(sm) => match sm.get_index_manager() {
Some(mgr) => mgr,
None => {
log::debug!("No index manager available for automatic indexing");
return;
}
},
None => {
log::debug!("No storage manager available for automatic indexing");
return;
}
};
// For each label on this node, find matching text indexes
for label in labels {
log::debug!("Checking for text indexes on label '{}'", label);
// Find all indexes for this label
let matching_indexes = index_manager.find_indexes_for_label(label);
// Text indexes not supported in GraphLite - this is a stub
#[allow(unused_variables)]
for _index_name in matching_indexes {
// Text indexing not supported in GraphLite - no-op
log::debug!("Text index automatic indexing skipped (not supported in GraphLite)");
}
}
}
}
impl StatementExecutor for PlannedInsertExecutor {
fn operation_type(&self) -> crate::txn::state::OperationType {
crate::txn::state::OperationType::Insert
}
fn operation_description(&self, _context: &ExecutionContext) -> String {
// Count nodes and edges in the patterns
let mut node_count = 0;
let mut edge_count = 0;
for pattern in &self.statement.graph_patterns {
for element in &pattern.elements {
match element {
crate::ast::ast::PatternElement::Node(_) => node_count += 1,
crate::ast::ast::PatternElement::Edge(_) => edge_count += 1,
}
}
}
// Generate meaningful message based on what's being created
match (node_count, edge_count) {
(0, 0) => "INSERT 0".to_string(),
(n, 0) if n == 1 => "Created 1 node".to_string(),
(n, 0) => format!("Created {} nodes", n),
(0, e) if e == 1 => "Created 1 relationship".to_string(),
(0, e) => format!("Created {} relationships", e),
(1, 1) => "Created 1 node, 1 relationship".to_string(),
(1, e) => format!("Created 1 node, {} relationships", e),
(n, 1) => format!("Created {} nodes, 1 relationship", n),
(n, e) => format!("Created {} nodes, {} relationships", n, e),
}
}
}
impl DataStatementExecutor for PlannedInsertExecutor {
fn execute_modification(
&self,
graph: &mut GraphCache,
context: &mut ExecutionContext,
) -> Result<(UndoOperation, usize), ExecutionError> {
// Step 1: Logical planning
let mut logical_planner = InsertPlanner::new();
let logical_plan = logical_planner
.plan_insert(&self.statement)
.map_err(|e| ExecutionError::RuntimeError(format!("Logical planning error: {}", e)))?;
log::debug!(
"Logical plan created with {} variables",
logical_plan.variables.len()
);
// Step 2: Physical planning
let physical_plan = PhysicalPlan::from_logical(&logical_plan);
log::debug!("Physical plan created");
// Step 3: Execute physical plan
let mut rows_affected = 0usize;
let mut undo_operations = Vec::new();
// Get the graph path for undo operations
let graph_path = context.get_graph_name().unwrap_or_else(|_| String::new()); // Fall back to empty string if no graph context
match &physical_plan.root {
crate::plan::physical::PhysicalNode::Insert {
node_creations,
edge_creations,
..
} => {
// Execute node creations
for node_creation in node_creations {
// Convert expression properties to storage values
let mut properties = HashMap::new();
for (key, expr) in &node_creation.properties {
// Use ExecutionContext's evaluate_simple_expression to handle literals and function calls
match context.evaluate_simple_expression(expr) {
Ok(value) => {
properties.insert(key.clone(), value);
}
Err(e) => {
log::warn!("Failed to evaluate property expression for '{}': {}. Skipping property.", key, e);
}
}
}
let node = crate::storage::Node {
id: node_creation.storage_id.clone(),
labels: node_creation.labels.clone(),
properties,
};
// Add node to graph
let node_id = node_creation.storage_id.clone();
let node_labels = node_creation.labels.clone();
let node_props = node.properties.clone();
match graph.add_node(node) {
Ok(_) => {
log::debug!("Successfully added node '{}' to graph", node_id);
rows_affected += 1;
// AUTOMATIC INDEXING: Update text indexes for this node
Self::update_text_indexes_for_node(
&node_id,
&node_labels,
&node_props,
context,
);
// Add undo operation for transaction management
undo_operations.push(UndoOperation::InsertNode {
graph_path: graph_path.clone(),
node_id,
});
}
Err(crate::storage::types::GraphError::NodeAlreadyExists(_)) => {
log::info!(
"Node '{}' already exists, skipping duplicate",
node_creation.storage_id
);
// Add warning about duplicate insertion
let warning_msg = format!("Duplicate node detected: Node with identical properties already exists (node_id: {})", node_creation.storage_id);
context.add_warning(warning_msg);
}
Err(e) => {
return Err(ExecutionError::RuntimeError(format!(
"Failed to add node '{}': {}",
node_creation.storage_id, e
)));
}
}
}
// Execute edge creations
for edge_creation in edge_creations {
// Convert expression properties to storage values
let mut properties = HashMap::new();
for (key, expr) in &edge_creation.properties {
// Use ExecutionContext's evaluate_simple_expression to handle literals and function calls
match context.evaluate_simple_expression(expr) {
Ok(value) => {
properties.insert(key.clone(), value);
}
Err(e) => {
log::warn!("Failed to evaluate property expression for '{}': {}. Skipping property.", key, e);
}
}
}
let edge = crate::storage::Edge {
id: edge_creation.storage_id.clone(),
from_node: edge_creation.from_node_id.clone(),
to_node: edge_creation.to_node_id.clone(),
label: edge_creation.label.clone(),
properties,
};
// Add edge to graph
match graph.add_edge(edge) {
Ok(_) => {
log::debug!(
"Successfully added edge '{}' to graph",
edge_creation.storage_id
);
rows_affected += 1;
// Add undo operation for transaction management
undo_operations.push(UndoOperation::InsertEdge {
graph_path: graph_path.clone(),
edge_id: edge_creation.storage_id.clone(),
});
}
Err(crate::storage::types::GraphError::EdgeAlreadyExists(_)) => {
log::info!(
"Edge '{}' already exists, skipping duplicate",
edge_creation.storage_id
);
}
Err(e) => {
return Err(ExecutionError::RuntimeError(format!(
"Failed to add edge '{}': {}",
edge_creation.storage_id, e
)));
}
}
}
log::debug!(
"Planned INSERT completed: {} nodes, {} edges",
node_creations.len(),
edge_creations.len()
);
}
_ => {
return Err(ExecutionError::RuntimeError(
"Invalid physical plan for INSERT".to_string(),
));
}
}
// Return a composite undo operation (for now, just return the first one or create a composite)
let composite_undo = if undo_operations.is_empty() {
// No operations were performed
UndoOperation::InsertNode {
graph_path: graph_path.clone(),
node_id: "dummy".to_string(),
}
} else {
// Return the first operation (the framework handles multiple operations via transaction logs)
undo_operations.into_iter().next().unwrap()
};
Ok((composite_undo, rows_affected))
}
}