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
//! Cypher scalar functions — temporal 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::*;
use super::shared::*;
use crate::datatypes::values::Value;
impl<'a> CypherExecutor<'a> {
pub(super) fn eval_temporal_fn(
&self,
name: &str,
args: &[Expression],
row: &ResultRow,
) -> Result<Option<Value>, String> {
let result: Result<Value, String> = match name {
"date" => {
if args.len() != 1 {
return Err("date() requires 1 argument: date('2020-01-15')".into());
}
let val = self.evaluate_expression(&args[0], row)?;
match val {
Value::String(s) => {
// Return Null on invalid input instead of crashing (BUG-09)
match crate::graph::features::timeseries::parse_date_query(&s) {
Ok((d, _)) => Ok(Value::DateTime(d)),
Err(_) => Ok(Value::Null),
}
}
Value::DateTime(_) => Ok(val),
Value::Null => Ok(Value::Null),
_ => Err(format!("date() argument must be a string, got {:?}", val)),
}
}
"datetime" => {
// Full date + time at second precision (Value::Timestamp).
// 0-arg form returns local "now"; a bare date parses to
// midnight. 0.12 Cluster 1 (was date-only via DateTime).
if args.is_empty() {
use chrono::Timelike;
let now = chrono::Local::now().naive_local();
return Ok(Some(Value::Timestamp(
now.with_nanosecond(0).unwrap_or(now),
)));
}
if args.len() != 1 {
return Err(
"datetime() requires 0 or 1 argument: datetime() or datetime('2024-03-15T10:30:00')".into(),
);
}
let val = self.evaluate_expression(&args[0], row)?;
match val {
Value::String(s) => {
// Full ISO datetime, else a bare date at midnight.
let parsed = chrono::NaiveDateTime::parse_from_str(&s, "%Y-%m-%dT%H:%M:%S")
.ok()
.or_else(|| {
let date_part = s.split('T').next().unwrap_or(&s);
crate::graph::features::timeseries::parse_date_query(date_part)
.ok()
.and_then(|(d, _)| d.and_hms_opt(0, 0, 0))
});
Ok(parsed.map_or(Value::Null, Value::Timestamp))
}
Value::Timestamp(_) => Ok(val),
Value::DateTime(d) => {
Ok(Value::Timestamp(d.and_hms_opt(0, 0, 0).unwrap_or_default()))
}
Value::Null => Ok(Value::Null),
_ => Err(format!(
"datetime() argument must be a string, got {:?}",
val
)),
}
}
"date_diff" | "datediff" => {
if args.len() != 2 {
return Err("date_diff() requires 2 date arguments".into());
}
let a = self.evaluate_expression(&args[0], row)?;
let b = self.evaluate_expression(&args[1], row)?;
if matches!(a, Value::Null) || matches!(b, Value::Null) {
return Ok(Some(Value::Null));
}
// Accepts date() and datetime() operands (and a mix).
match (coerce_naive_datetime(&a), coerce_naive_datetime(&b)) {
(Some(d1), Some(d2)) => Ok(Value::Int64((d1 - d2).num_days())),
_ => Err("date_diff() arguments must be dates".into()),
}
}
// 0.9.0 §3 / Cluster 2 — proper Value::Duration variant.
// Calendar units (months/years) and clock units
// (days/hours/minutes/seconds) are kept separate in the
// value, so `duration({months: 1, days: 5}).months` returns
// 1, not 35. Sub-day precision is wired in `seconds` —
// DateTime + Duration discards the seconds component for
// now because Value::DateTime is still NaiveDate (Cluster
// 1, deferred).
"duration" => {
if args.len() != 1 {
return Err("duration() requires 1 map argument: duration({days: N})".into());
}
if let Expression::MapLiteral(entries) = &args[0] {
let mut months: i64 = 0;
let mut days: i64 = 0;
let mut seconds: i64 = 0;
for (key, expr) in entries {
let v = self.evaluate_expression(expr, row)?;
let n = match v {
Value::Int64(n) => n,
Value::Float64(f) => f as i64,
Value::Null => 0,
_ => {
return Err(format!("duration({{{key}: ...}}) expects a number"));
}
};
match key.as_str() {
"years" => months += n * 12,
"months" => months += n,
"weeks" => days += n * 7,
"days" => days += n,
"hours" => seconds += n * 3600,
"minutes" => seconds += n * 60,
"seconds" => seconds += n,
other => {
return Err(format!(
"duration(): unknown key '{other}' (expected years/months/weeks/days/hours/minutes/seconds)"
));
}
}
}
Ok(Value::Duration {
months: months as i32,
days: days as i32,
seconds,
})
} else {
Err("duration() requires a map literal: duration({days: N})".into())
}
}
"duration.between" => {
if args.len() != 2 {
return Err("duration.between() requires 2 datetime arguments".into());
}
let a = self.evaluate_expression(&args[0], row)?;
let b = self.evaluate_expression(&args[1], row)?;
if matches!(a, Value::Null) || matches!(b, Value::Null) {
return Ok(Some(Value::Null));
}
// Accepts date() and datetime() operands (and a mix). Whole
// days go in `days`; any remaining sub-day delta (when a
// Timestamp is involved) is carried in `seconds`.
match (coerce_naive_datetime(&a), coerce_naive_datetime(&b)) {
(Some(start), Some(end)) => {
let total_secs = (end - start).num_seconds();
Ok(Value::Duration {
months: 0,
days: (total_secs / 86_400) as i32,
seconds: total_secs % 86_400,
})
}
_ => Err("duration.between() arguments must be datetime values".into()),
}
}
// Temporal arithmetic (2026-05-25 broad-scan lift).
// Real use case: "events scheduled in the next N days":
// MATCH (e:Event) WHERE e.date <= add_days(date(), 30)
// Date math via chrono — NaiveDate handles month/year
// arithmetic correctly (Feb 28 + 1 year = Feb 28; Jan 31
// + 1 month = Feb 28/29 depending on leap year).
"add_days" => {
if args.len() != 2 {
return Err("add_days() requires 2 args: add_days(date, n_days)".into());
}
let d = self.evaluate_expression(&args[0], row)?;
let n = self.evaluate_expression(&args[1], row)?;
match (&d, &n) {
(Value::DateTime(date), Value::Int64(n)) => {
match date.checked_add_signed(chrono::Duration::days(*n)) {
Some(out) => Ok(Value::DateTime(out)),
None => Ok(Value::Null),
}
}
(Value::Null, _) | (_, Value::Null) => Ok(Value::Null),
_ => Err("add_days() expects (date, integer)".into()),
}
}
"add_months" => {
if args.len() != 2 {
return Err("add_months() requires 2 args: add_months(date, n_months)".into());
}
let d = self.evaluate_expression(&args[0], row)?;
let n = self.evaluate_expression(&args[1], row)?;
match (&d, &n) {
(Value::DateTime(date), Value::Int64(n)) => {
let result = if *n >= 0 {
date.checked_add_months(chrono::Months::new(*n as u32))
} else {
date.checked_sub_months(chrono::Months::new((-*n) as u32))
};
match result {
Some(out) => Ok(Value::DateTime(out)),
None => Ok(Value::Null),
}
}
(Value::Null, _) | (_, Value::Null) => Ok(Value::Null),
_ => Err("add_months() expects (date, integer)".into()),
}
}
"add_years" => {
if args.len() != 2 {
return Err("add_years() requires 2 args: add_years(date, n_years)".into());
}
let d = self.evaluate_expression(&args[0], row)?;
let n = self.evaluate_expression(&args[1], row)?;
match (&d, &n) {
(Value::DateTime(date), Value::Int64(n)) => {
// 12 months per year — chrono's Months handles
// leap-year edge case (Feb 29 + 1 year → Feb 28).
let months_delta = n.saturating_mul(12);
let result = if months_delta >= 0 {
date.checked_add_months(chrono::Months::new(months_delta as u32))
} else {
date.checked_sub_months(chrono::Months::new((-months_delta) as u32))
};
match result {
Some(out) => Ok(Value::DateTime(out)),
None => Ok(Value::Null),
}
}
(Value::Null, _) | (_, Value::Null) => Ok(Value::Null),
_ => Err("add_years() expects (date, integer)".into()),
}
}
// date_truncate(date, 'unit') — round down to the start of
// a calendar period. Real use case: group analytics by
// week/month: `RETURN date_truncate(e.ts, 'month'), count(e)`.
"date_truncate" => {
use chrono::{Datelike, NaiveDate};
if args.len() != 2 {
return Err(
"date_truncate() requires 2 args: date_truncate(date, 'year'|'month'|'week'|'day')".into()
);
}
let d = self.evaluate_expression(&args[0], row)?;
let unit = self.evaluate_expression(&args[1], row)?;
let (date, unit_str) = match (&d, &unit) {
(Value::DateTime(date), Value::String(u)) => (*date, u.as_str()),
(Value::Null, _) | (_, Value::Null) => return Ok(Some(Value::Null)),
_ => return Err("date_truncate() expects (date, string)".into()),
};
let truncated = match unit_str {
"year" | "years" => NaiveDate::from_ymd_opt(date.year(), 1, 1),
"month" | "months" => NaiveDate::from_ymd_opt(date.year(), date.month(), 1),
"week" | "weeks" => {
// ISO week starts Monday. Subtract weekday-1 days.
let dow = date.weekday().num_days_from_monday() as i64;
date.checked_sub_signed(chrono::Duration::days(dow))
}
"day" | "days" => Some(date),
other => {
return Err(format!(
"date_truncate() unit must be year/month/week/day, got '{other}'"
));
}
};
Ok(truncated.map(Value::DateTime).unwrap_or(Value::Null))
}
"localdatetime" => self.eval_local_temporal(args, row, LocalTemporalKind::DateTime),
"localtime" => self.eval_local_temporal(args, row, LocalTemporalKind::Time),
"time" => self.eval_local_temporal(args, row, LocalTemporalKind::Time),
_ => return Ok(None),
};
result.map(Some)
}
}