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
//! Cypher scalar functions — collection category. Split out of the monolithic
//! `evaluate_scalar_function` dispatcher; arms are verbatim. Routed from
//! `super::evaluate_scalar_function`; returns `Ok(None)` when `name` is not
//! one of this category's functions so the dispatcher tries the next.
use super::super::helpers::*;
use super::super::*;
use crate::datatypes::values::Value;
impl<'a> CypherExecutor<'a> {
pub(super) fn eval_collection_fn(
&self,
name: &str,
args: &[Expression],
row: &ResultRow,
) -> Result<Option<Value>, String> {
let result: Result<Value, String> = match name {
"size" => {
// Phase A.1 / C2 — native Value::List fast path;
// string fallback stays for legacy collect-as-JSON
// and parameter-passed lists.
let val = self.evaluate_expression(&args[0], row)?;
match val {
Value::List(items) => Ok(Value::Int64(items.len() as i64)),
Value::Map(m) => Ok(Value::Int64(m.len() as i64)),
Value::String(s) => {
if s.starts_with('[') && s.ends_with(']') {
let items = parse_list_value(&Value::String(s));
Ok(Value::Int64(items.len() as i64))
} else {
Ok(Value::Int64(s.len() as i64))
}
}
_ => Ok(Value::Null),
}
}
"length" => {
// length(p) for paths, length(s) for strings, length(list) for lists
if let Some(Expression::Variable(var)) = args.first() {
if let Some(path) = row.path_bindings.get(var) {
return Ok(Some(Value::Int64(path.hops as i64)));
}
}
let val = self.evaluate_expression(&args[0], row)?;
match val {
// Phase A.1 / C2 — native Value::List/Path/Map paths.
Value::List(items) => Ok(Value::Int64(items.len() as i64)),
Value::Map(m) => Ok(Value::Int64(m.len() as i64)),
Value::Path(p) => Ok(Value::Int64(p.rels.len() as i64)),
Value::String(s) => {
if s.starts_with('[') && s.ends_with(']') {
let items = parse_list_value(&Value::String(s));
Ok(Value::Int64(items.len() as i64))
} else {
Ok(Value::Int64(s.len() as i64))
}
}
_ => Ok(Value::Null),
}
}
"coalesce" => {
// coalesce(expr1, expr2, ...) returns first non-null
for arg in args {
let val = self.evaluate_expression(arg, row)?;
if !matches!(val, Value::Null) {
return Ok(Some(val));
}
}
Ok(Value::Null)
}
"reverse" => {
if args.len() != 1 {
return Err("reverse() requires 1 argument".into());
}
match self.evaluate_expression(&args[0], row)? {
// Cypher reverse() on a list reverses its elements.
Value::List(mut items) => {
items.reverse();
Ok(Value::List(items))
}
Value::Null => Ok(Value::Null),
other => {
let s = match coerce_to_string(other) {
Value::String(s) => s,
_ => return Ok(Some(Value::Null)),
};
let trimmed = s.trim();
if trimmed.starts_with('[') && trimmed.ends_with(']') {
// A bracketed string is a list, consistent with
// head/last/size (parse_list_value) — reverse elements.
let mut items = parse_list_value(&Value::String(s));
items.reverse();
Ok(Value::List(items))
} else {
// Otherwise reverse characters.
Ok(Value::String(s.chars().rev().collect()))
}
}
}
}
// ── List functions ────────────────────────────────────
"head" => {
if args.len() != 1 {
return Err("head() requires 1 argument".into());
}
let val = self.evaluate_expression(&args[0], row)?;
let items = parse_list_value(&val);
Ok(items.into_iter().next().unwrap_or(Value::Null))
}
"last" => {
if args.len() != 1 {
return Err("last() requires 1 argument".into());
}
let val = self.evaluate_expression(&args[0], row)?;
let items = parse_list_value(&val);
Ok(items.into_iter().last().unwrap_or(Value::Null))
}
// ── Spatial functions ─────────────────────────────────
"range" => {
if args.len() < 2 || args.len() > 3 {
return Err(
"range() requires 2 or 3 arguments: range(start, end[, step])".into(),
);
}
let start = as_i64(&self.evaluate_expression(&args[0], row)?)?;
let end = as_i64(&self.evaluate_expression(&args[1], row)?)?;
let step = if args.len() == 3 {
let s = as_i64(&self.evaluate_expression(&args[2], row)?)?;
if s == 0 {
return Err("range() step must not be zero".into());
}
s
} else {
1
};
// Phase A.1 / C4 — native Value::List of Value::Int64.
let mut vals: Vec<Value> = Vec::new();
let mut cur = start;
if step > 0 {
while cur <= end {
vals.push(Value::Int64(cur));
cur += step;
}
} else {
while cur >= end {
vals.push(Value::Int64(cur));
cur += step;
}
}
Ok(Value::List(vals))
}
// ── Numeric math functions ──────────────────────────
_ => return Ok(None),
};
result.map(Some)
}
}