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
//! Auto-generated submodule — see engine/mod.rs for context.
use super::*;
impl Engine {
// ── UNION ─────────────────────────────────────────────────────────────────
/// Execute `stmt1 UNION [ALL] stmt2`.
///
/// Concatenates the row sets from both sides. When `!all`, duplicate rows
/// are eliminated using the same `deduplicate_rows` logic used by DISTINCT.
/// Both sides must produce the same number of columns; column names are taken
/// from the left side.
pub(crate) fn execute_union(&mut self, u: UnionStatement) -> Result<QueryResult> {
let left_result = self.execute_bound(*u.left)?;
let right_result = self.execute_bound(*u.right)?;
// Validate column counts match.
if !left_result.columns.is_empty()
&& !right_result.columns.is_empty()
&& left_result.columns.len() != right_result.columns.len()
{
return Err(sparrowdb_common::Error::InvalidArgument(format!(
"UNION: left side has {} columns, right side has {}",
left_result.columns.len(),
right_result.columns.len()
)));
}
let columns = if !left_result.columns.is_empty() {
left_result.columns.clone()
} else {
right_result.columns.clone()
};
let mut rows = left_result.rows;
rows.extend(right_result.rows);
if !u.all {
deduplicate_rows(&mut rows);
}
Ok(QueryResult { columns, rows })
}
// ── WITH clause pipeline ──────────────────────────────────────────────────
/// Execute `MATCH … WITH expr AS alias [WHERE pred] … RETURN …`.
///
/// 1. Scan MATCH patterns → collect intermediate rows as `Vec<HashMap<String, Value>>`.
/// 2. Project each row through the WITH items (evaluate expr, bind to alias).
/// 3. Apply WITH WHERE predicate on the projected map.
/// 4. Evaluate RETURN expressions against the projected map.
pub(crate) fn execute_match_with(&self, m: &MatchWithStatement) -> Result<QueryResult> {
// Step 1: collect intermediate rows from MATCH scan.
let intermediate = self.collect_match_rows_for_with(
&m.match_patterns,
m.match_where.as_ref(),
&m.with_clause,
)?;
// Step 2: check if WITH clause has aggregate expressions.
// If so, we aggregate the intermediate rows first, producing one output row
// per unique grouping key.
let has_agg = m
.with_clause
.items
.iter()
.any(|item| is_aggregate_expr(&item.expr));
let projected: Vec<HashMap<String, Value>> = if has_agg {
// Aggregate the intermediate rows into a set of projected rows.
let agg_rows = self.aggregate_with_items(&intermediate, &m.with_clause.items);
// Apply WHERE filter on the aggregated rows.
agg_rows
.into_iter()
.filter(|with_vals| {
if let Some(ref where_expr) = m.with_clause.where_clause {
let mut with_vals_p = with_vals.clone();
with_vals_p.extend(self.dollar_params());
self.eval_where_graph(where_expr, &with_vals_p)
} else {
true
}
})
.map(|mut with_vals| {
with_vals.extend(self.dollar_params());
with_vals
})
.collect()
} else {
// Non-aggregate path: project each row through the WITH items.
let mut projected: Vec<HashMap<String, Value>> = Vec::new();
for row_vals in &intermediate {
let mut with_vals: HashMap<String, Value> = HashMap::new();
for item in &m.with_clause.items {
let val = self.eval_expr_graph(&item.expr, row_vals);
with_vals.insert(item.alias.clone(), val);
// SPA-134: if the WITH item is a bare Var (e.g. `n AS person`),
// also inject the NodeRef under the alias so that EXISTS subqueries
// in a subsequent WHERE clause can resolve the source node.
if let sparrowdb_cypher::ast::Expr::Var(ref src_var) = item.expr {
if let Some(node_ref) = row_vals.get(src_var) {
if matches!(node_ref, Value::NodeRef(_)) {
with_vals.insert(item.alias.clone(), node_ref.clone());
with_vals.insert(
format!("{}.__node_id__", item.alias),
node_ref.clone(),
);
}
}
// Also check __node_id__ key.
let nid_key = format!("{src_var}.__node_id__");
if let Some(node_ref) = row_vals.get(&nid_key) {
with_vals
.insert(format!("{}.__node_id__", item.alias), node_ref.clone());
}
}
}
if let Some(ref where_expr) = m.with_clause.where_clause {
let mut with_vals_p = with_vals.clone();
with_vals_p.extend(self.dollar_params());
if !self.eval_where_graph(where_expr, &with_vals_p) {
continue;
}
}
// Merge dollar_params into the projected row so that downstream
// RETURN/ORDER-BY/SKIP/LIMIT expressions can resolve $param references.
with_vals.extend(self.dollar_params());
projected.push(with_vals);
}
projected
};
// Step 3: project RETURN from the WITH-projected rows.
let column_names = extract_return_column_names(&m.return_clause.items);
// Apply ORDER BY on the projected rows (which still have all WITH aliases)
// before projecting down to RETURN columns — this allows ORDER BY on columns
// that are not in the RETURN clause (e.g. ORDER BY age when only name is returned).
let mut ordered_projected = projected;
if !m.order_by.is_empty() {
ordered_projected.sort_by(|a, b| {
for (expr, dir) in &m.order_by {
let val_a = eval_expr(expr, a);
let val_b = eval_expr(expr, b);
let cmp = compare_values(&val_a, &val_b);
let cmp = if *dir == SortDir::Desc {
cmp.reverse()
} else {
cmp
};
if cmp != std::cmp::Ordering::Equal {
return cmp;
}
}
std::cmp::Ordering::Equal
});
}
// Apply SKIP / LIMIT before final projection.
if let Some(skip) = m.skip {
let skip = (skip as usize).min(ordered_projected.len());
ordered_projected.drain(0..skip);
}
if let Some(lim) = m.limit {
ordered_projected.truncate(lim as usize);
}
let mut rows: Vec<Vec<Value>> = ordered_projected
.iter()
.map(|with_vals| {
m.return_clause
.items
.iter()
.map(|item| self.eval_expr_graph(&item.expr, with_vals))
.collect()
})
.collect();
if m.distinct {
deduplicate_rows(&mut rows);
}
Ok(QueryResult {
columns: column_names,
rows,
})
}
/// Aggregate a set of raw scan rows through a list of WITH items that
/// include aggregate expressions (COUNT(*), collect(), etc.).
///
/// Returns one `HashMap<String, Value>` per unique grouping key.
pub(crate) fn aggregate_with_items(
&self,
rows: &[HashMap<String, Value>],
items: &[sparrowdb_cypher::ast::WithItem],
) -> Vec<HashMap<String, Value>> {
// Classify each WITH item as key or aggregate.
let key_indices: Vec<usize> = items
.iter()
.enumerate()
.filter(|(_, item)| !is_aggregate_expr(&item.expr))
.map(|(i, _)| i)
.collect();
let agg_indices: Vec<usize> = items
.iter()
.enumerate()
.filter(|(_, item)| is_aggregate_expr(&item.expr))
.map(|(i, _)| i)
.collect();
// Build groups.
let mut group_keys: Vec<Vec<Value>> = Vec::new();
let mut group_accum: Vec<Vec<Vec<Value>>> = Vec::new(); // [group][agg_pos] → values
for row_vals in rows {
let key: Vec<Value> = key_indices
.iter()
.map(|&i| eval_expr(&items[i].expr, row_vals))
.collect();
let group_idx = if let Some(pos) = group_keys.iter().position(|k| k == &key) {
pos
} else {
group_keys.push(key);
group_accum.push(vec![vec![]; agg_indices.len()]);
group_keys.len() - 1
};
for (ai, &ri) in agg_indices.iter().enumerate() {
match &items[ri].expr {
sparrowdb_cypher::ast::Expr::CountStar => {
group_accum[group_idx][ai].push(Value::Int64(1));
}
sparrowdb_cypher::ast::Expr::FnCall { name, args }
if name.to_lowercase() == "collect" =>
{
let val = if !args.is_empty() {
eval_expr(&args[0], row_vals)
} else {
Value::Null
};
if !matches!(val, Value::Null) {
group_accum[group_idx][ai].push(val);
}
}
sparrowdb_cypher::ast::Expr::FnCall { name, args }
if matches!(
name.to_lowercase().as_str(),
"count" | "sum" | "avg" | "min" | "max"
) =>
{
let val = if !args.is_empty() {
eval_expr(&args[0], row_vals)
} else {
Value::Null
};
if !matches!(val, Value::Null) {
group_accum[group_idx][ai].push(val);
}
}
_ => {}
}
}
}
// If no rows were seen, still produce one output row for global aggregates
// (e.g. COUNT(*) over an empty scan returns 0).
if rows.is_empty() && key_indices.is_empty() {
let mut out_row: HashMap<String, Value> = HashMap::new();
for &ri in &agg_indices {
let val = match &items[ri].expr {
sparrowdb_cypher::ast::Expr::CountStar => Value::Int64(0),
sparrowdb_cypher::ast::Expr::FnCall { name, .. }
if name.to_lowercase() == "collect" =>
{
Value::List(vec![])
}
_ => Value::Int64(0),
};
out_row.insert(items[ri].alias.clone(), val);
}
return vec![out_row];
}
// Finalize each group.
let mut result: Vec<HashMap<String, Value>> = Vec::new();
for (gi, key_vals) in group_keys.iter().enumerate() {
let mut out_row: HashMap<String, Value> = HashMap::new();
// Insert key values.
for (ki, &ri) in key_indices.iter().enumerate() {
out_row.insert(items[ri].alias.clone(), key_vals[ki].clone());
}
// Finalize aggregates.
for (ai, &ri) in agg_indices.iter().enumerate() {
let accum = &group_accum[gi][ai];
let val = match &items[ri].expr {
sparrowdb_cypher::ast::Expr::CountStar => Value::Int64(accum.len() as i64),
sparrowdb_cypher::ast::Expr::FnCall { name, .. }
if name.to_lowercase() == "collect" =>
{
Value::List(accum.clone())
}
sparrowdb_cypher::ast::Expr::FnCall { name, .. }
if name.to_lowercase() == "count" =>
{
Value::Int64(accum.len() as i64)
}
sparrowdb_cypher::ast::Expr::FnCall { name, .. }
if name.to_lowercase() == "sum" =>
{
let sum: i64 = accum
.iter()
.filter_map(|v| {
if let Value::Int64(n) = v {
Some(*n)
} else {
None
}
})
.sum();
Value::Int64(sum)
}
sparrowdb_cypher::ast::Expr::FnCall { name, .. }
if name.to_lowercase() == "min" =>
{
accum
.iter()
.min_by(|a, b| compare_values(a, b))
.cloned()
.unwrap_or(Value::Null)
}
sparrowdb_cypher::ast::Expr::FnCall { name, .. }
if name.to_lowercase() == "max" =>
{
accum
.iter()
.max_by(|a, b| compare_values(a, b))
.cloned()
.unwrap_or(Value::Null)
}
_ => Value::Null,
};
out_row.insert(items[ri].alias.clone(), val);
}
result.push(out_row);
}
result
}
}