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
//! Integration tests for subquery paths: CALL { subquery }, EXISTS semi/anti
//! join, OPTIONAL MATCH NULL padding, and correlated subqueries.
//!
//! Covers uncovered paths in:
//! - gql.rs: CALL { subquery } (inline), lines 406-408, 993-1050
//! - cypher.rs: CALL { subquery } with WITH import, lines 265-285
//! - apply.rs: EXISTS semi-join, anti-join, optional NULL padding
//!
//! ```bash
//! cargo test -p grafeo-engine --features full --test subquery_integration
//! ```
use grafeo_common::types::Value;
use grafeo_engine::GrafeoDB;
// ============================================================================
// Fixtures
// ============================================================================
/// Creates 3 Person + 1 Company nodes (Amsterdam/Berlin/Paris), 3 KNOWS + 2 WORKS_AT edges.
fn social_graph() -> GrafeoDB {
let db = GrafeoDB::new_in_memory();
let session = db.session();
let alix = session
.create_node_with_props(
&["Person"],
[
("name", Value::String("Alix".into())),
("age", Value::Int64(30)),
("city", Value::String("Amsterdam".into())),
],
)
.unwrap();
let gus = session
.create_node_with_props(
&["Person"],
[
("name", Value::String("Gus".into())),
("age", Value::Int64(25)),
("city", Value::String("Berlin".into())),
],
)
.unwrap();
let harm = session
.create_node_with_props(
&["Person"],
[
("name", Value::String("Harm".into())),
("age", Value::Int64(35)),
("city", Value::String("Paris".into())),
],
)
.unwrap();
let techcorp = session
.create_node_with_props(&["Company"], [("name", Value::String("TechCorp".into()))])
.unwrap();
session.create_edge(alix, gus, "KNOWS");
session.create_edge(alix, harm, "KNOWS");
session.create_edge(gus, harm, "KNOWS");
session.create_edge(alix, techcorp, "WORKS_AT");
session.create_edge(gus, techcorp, "WORKS_AT");
// Verify setup: 3 Person + 1 Company = 4 nodes, 3 KNOWS + 2 WORKS_AT = 5 edges
assert_eq!(db.node_count(), 4, "social_graph: expected 4 nodes");
assert_eq!(db.edge_count(), 5, "social_graph: expected 5 edges");
db
}
// ============================================================================
// GQL: CALL { subquery } (inline)
// ============================================================================
#[test]
fn test_gql_inline_call_subquery() {
let db = social_graph();
let session = db.session();
let result = session
.execute(
"MATCH (n:Person {name: 'Alix'}) \
CALL { WITH n MATCH (n)-[:KNOWS]->(m) RETURN m.name AS friend } \
RETURN n.name, friend \
ORDER BY friend",
)
.unwrap();
assert_eq!(result.rows().len(), 2);
let friends: Vec<String> = result
.rows()
.iter()
.map(|r| match &r[1] {
Value::String(s) => s.to_string(),
other => panic!("expected string, got {other:?}"),
})
.collect();
assert_eq!(
friends,
vec!["Gus", "Harm"],
"ORDER BY friend should sort alphabetically"
);
}
#[test]
fn test_gql_inline_call_without_outer() {
let db = social_graph();
let session = db.session();
let result = session
.execute("CALL { MATCH (n:Person) RETURN count(n) AS cnt } RETURN cnt")
.unwrap();
assert_eq!(result.rows().len(), 1);
assert_eq!(result.rows()[0][0], Value::Int64(3));
}
// ============================================================================
// Cypher: CALL { subquery } with WITH import
// ============================================================================
#[cfg(feature = "cypher")]
mod cypher_subqueries {
use super::*;
#[test]
fn test_call_subquery_with_wildcard() {
let db = social_graph();
let session = db.session();
let result = session
.execute_cypher(
"MATCH (n:Person {name: 'Alix'}) \
CALL { WITH * MATCH (n)-[:KNOWS]->(m) RETURN m.name AS friend } \
RETURN n.name, friend \
ORDER BY friend",
)
.unwrap();
// WITH * scopes n=Alix from the outer MATCH, giving 2 results (Gus, Harm).
assert_eq!(
result.rows().len(),
2,
"WITH * should scope outer variable, expected 2 rows"
);
}
#[test]
fn test_call_subquery_with_specific_var() {
let db = social_graph();
let session = db.session();
let result = session
.execute_cypher(
"MATCH (n:Person) \
CALL { WITH n MATCH (n)-[:KNOWS]->(m) RETURN count(m) AS cnt } \
RETURN n.name, cnt \
ORDER BY n.name",
)
.unwrap();
assert_eq!(result.rows().len(), 3);
}
// ============================================================================
// EXISTS as semi-join and anti-join: covers apply.rs exists_mode
// ============================================================================
#[test]
fn test_exists_semi_join() {
let db = social_graph();
let session = db.session();
let result = session
.execute_cypher(
"MATCH (n:Person) \
WHERE EXISTS { MATCH (n)-[:KNOWS]->(m)-[:WORKS_AT]->(c) } \
RETURN n.name ORDER BY n.name",
)
.unwrap();
// Alix->Gus->TechCorp path exists
let mut names: Vec<String> = result
.rows()
.iter()
.map(|r| match &r[0] {
Value::String(s) => s.to_string(),
other => panic!("expected string, got {other:?}"),
})
.collect();
names.sort();
assert!(names.contains(&"Alix".to_string()));
}
#[test]
fn test_not_exists_anti_join() {
let db = social_graph();
let session = db.session();
let result = session
.execute_cypher(
"MATCH (n:Person) \
WHERE NOT EXISTS { MATCH (n)-[:WORKS_AT]->() } \
RETURN n.name",
)
.unwrap();
assert_eq!(result.rows().len(), 1);
assert_eq!(result.rows()[0][0], Value::String("Harm".into()));
}
// ============================================================================
// OPTIONAL MATCH NULL padding: covers apply.rs optional branch
// ============================================================================
#[test]
fn test_optional_match_null_padding() {
let db = social_graph();
let session = db.session();
let result = session
.execute_cypher(
"MATCH (n:Person) \
OPTIONAL MATCH (n)-[:WORKS_AT]->(c:Company) \
RETURN n.name, c.name \
ORDER BY n.name",
)
.unwrap();
assert_eq!(result.rows().len(), 3);
let harm_row = result
.rows()
.iter()
.find(|r| r[0] == Value::String("Harm".into()))
.expect("Harm should be in results");
assert_eq!(harm_row[1], Value::Null);
}
}