Skip to main content

eui_core/host/
query.rs

1//! VAPP-189 (S1): THE records query, the Rust twin of
2//! `packages/exponential-ui/src/host/query.ts`, locked by
3//! `fixtures/records-query.json` (`parseCases`, `sourceCases`, `jsonCases`,
4//! `valueCases`, `expandCases`; `bucketCases` lock [`crate::dates`]). A
5//! binding's URL sugar (`records:<kind>?q`) parses INTO the structured
6//! [`RecordsQuery`]; [`records_value`] evaluates it over the rows of any
7//! store. The desktop host calls it directly; the Swift and Compose SDKs
8//! reach it through `exponential-ui-ffi` (`parse_records_query_json`,
9//! `parse_records_json`, `parse_records_source_json`, `records_value_json`).
10//!
11//! VAPP-190 (Q1): the JSON form ([`parse_records_json`], a package's
12//! `queries`, the `q=<json>` param): `group` (any field, `author`, date
13//! buckets), `aggregate`, `running`, `before`, `in` / `between` and `or`.
14
15use std::borrow::Cow;
16use std::cmp::Ordering;
17use std::collections::{BTreeMap, HashMap};
18
19use rustc_hash::FxHashMap;
20use serde::{Deserialize, Serialize};
21use serde_json::{json, Map, Value};
22
23use super::signature::canonical_json;
24use super::sources::{parse_source, source_with_params, ParsedSource};
25use super::{js_trim, utf16_cmp};
26use crate::dates::{date_bucket, days_in_month, weekday_of, DateBucket, DATE_BUCKETS};
27use crate::json::{number, number_to_string};
28
29/// The binding scheme: `records:<kind>[?query]`.
30pub const RECORDS_SCHEME: &str = "records";
31/// The URL sugar's defaults and caps (catalog/host.json `package.records.query`).
32pub const RECORDS_SORT_DEFAULT: &str = "createdAt";
33pub const RECORDS_ORDER_DEFAULT: &str = "desc";
34pub const RECORDS_LIMIT_DEFAULT: u32 = 100;
35pub const RECORDS_LIMIT_MAX: u32 = 500;
36/// Query keys with a meaning of their own; never a field filter.
37pub const RECORDS_RESERVED_PARAMS: &[&str] = &["app", "before", "by", "limit", "mine", "month", "order", "q", "query", "sort", "sum", "user"];
38/// The keys every value row carries next to its data (a data key never shadows one).
39pub const RECORDS_ROW_KEYS: &[&str] = &["id", "createdAt", "updatedAt", "createdBy", "authorId", "author"];
40/// The filter operators: `eq` is the bare key, the others a `.<op>` suffix
41/// (`in` and `between` take comma-separated values).
42pub const RECORDS_FILTER_OPS: &[&str] = &["eq", "gt", "gte", "lt", "lte", "in", "between"];
43/// The JSON form's spellings of the operators.
44pub const RECORDS_OP_ALIASES: &[(&str, &str)] = &[("=", "eq"), ("==", "eq"), (">", "gt"), (">=", "gte"), ("<", "lt"), ("<=", "lte")];
45pub const RECORDS_AGGREGATE_OPS: &[&str] = &["sum", "avg", "min", "max", "count", "countDistinct", "first", "last"];
46pub const RECORDS_RUNNING_OPS: &[&str] = &["sum", "count", "avg", "min", "max"];
47/// Names an aggregate may not take (a group's and a total's own keys).
48pub const RECORDS_AGGREGATE_RESERVED: &[&str] = &["count", "sum", "running", "rows"];
49/// The month view's names (en-US, the same ×4).
50pub const RECORDS_WEEKDAYS: [&str; 7] = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
51pub const RECORDS_MONTHS: [&str; 12] = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
54#[serde(rename_all = "lowercase")]
55pub enum RecordsFilterOp {
56    Eq,
57    Gt,
58    Gte,
59    Lt,
60    Lte,
61    In,
62    Between,
63}
64
65impl RecordsFilterOp {
66    pub fn as_str(self) -> &'static str {
67        match self {
68            Self::Eq => "eq",
69            Self::Gt => "gt",
70            Self::Gte => "gte",
71            Self::Lt => "lt",
72            Self::Lte => "lte",
73            Self::In => "in",
74            Self::Between => "between",
75        }
76    }
77
78    /// An operator by name (`eq` … `between`) or its JSON spelling (`<`, `>=`, `==`…).
79    pub fn parse(text: &str) -> Option<Self> {
80        let name = RECORDS_OP_ALIASES.iter().find(|(alias, _)| *alias == text).map_or(text, |(_, op)| op);
81        match name {
82            "eq" => Some(Self::Eq),
83            "gt" => Some(Self::Gt),
84            "gte" => Some(Self::Gte),
85            "lt" => Some(Self::Lt),
86            "lte" => Some(Self::Lte),
87            "in" => Some(Self::In),
88            "between" => Some(Self::Between),
89            _ => None,
90        }
91    }
92
93    /// The suffix operators (`<field>.<op>`); `eq` is the bare key.
94    fn suffix(text: &str) -> Option<Self> {
95        match text {
96            "gt" => Some(Self::Gt),
97            "gte" => Some(Self::Gte),
98            "lt" => Some(Self::Lt),
99            "lte" => Some(Self::Lte),
100            "in" => Some(Self::In),
101            "between" => Some(Self::Between),
102            _ => None,
103        }
104    }
105
106    fn is_list(self) -> bool {
107        matches!(self, Self::In | Self::Between)
108    }
109}
110
111/// A condition's value: one text, or a list (`in`; `between` = [low, high]).
112#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
113#[serde(untagged)]
114pub enum RecordsFilterValue {
115    One(String),
116    Many(Vec<String>),
117}
118
119/// One condition: `eq` / `in` compare as text, the ranges (`between`
120/// inclusive) as numbers when both sides parse, else as text.
121#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
122pub struct RecordsFilter {
123    pub field: String,
124    pub op: RecordsFilterOp,
125    pub value: RecordsFilterValue,
126}
127
128#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
129#[serde(rename_all = "lowercase")]
130pub enum RecordsOrder {
131    Asc,
132    Desc,
133}
134
135#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
136pub struct RecordsSortKey {
137    pub field: String,
138    pub order: RecordsOrder,
139}
140
141/// `by=<dateField>&month=YYYY-MM`: the rows of one month, every day listed.
142#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
143pub struct RecordsMonthView {
144    pub field: String,
145    /// `YYYY-MM`; `None` = the month of `now`.
146    pub key: Option<String>,
147}
148
149/// One group-by key: a field (`author` = the author, by name), or a date
150/// field cut into buckets. The group's column is named `field`.
151#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
152pub struct RecordsGroupKey {
153    pub field: String,
154    pub bucket: Option<DateBucket>,
155}
156
157#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
158#[serde(rename_all = "camelCase")]
159pub enum RecordsAggregateOp {
160    Sum,
161    Avg,
162    Min,
163    Max,
164    Count,
165    CountDistinct,
166    First,
167    Last,
168}
169
170impl RecordsAggregateOp {
171    pub fn parse(text: &str) -> Option<Self> {
172        match text {
173            "sum" => Some(Self::Sum),
174            "avg" => Some(Self::Avg),
175            "min" => Some(Self::Min),
176            "max" => Some(Self::Max),
177            "count" => Some(Self::Count),
178            "countDistinct" => Some(Self::CountDistinct),
179            "first" => Some(Self::First),
180            "last" => Some(Self::Last),
181            _ => None,
182        }
183    }
184}
185
186/// `name: [op, field?]`: a column of every group (and of `totals`).
187#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
188pub struct RecordsAggregate {
189    pub name: String,
190    pub op: RecordsAggregateOp,
191    /// `None` = `count` of the rows.
192    pub field: Option<String>,
193}
194
195#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
196#[serde(rename_all = "lowercase")]
197pub enum RecordsRunningOp {
198    Sum,
199    Count,
200    Avg,
201    Min,
202    Max,
203}
204
205impl RecordsRunningOp {
206    pub fn parse(text: &str) -> Option<Self> {
207        match text {
208            "sum" => Some(Self::Sum),
209            "count" => Some(Self::Count),
210            "avg" => Some(Self::Avg),
211            "min" => Some(Self::Min),
212            "max" => Some(Self::Max),
213            _ => None,
214        }
215    }
216}
217
218/// A cumulative value of `field` over the groups (or rows, or month days) in order.
219#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
220pub struct RecordsRunning {
221    pub field: String,
222    pub op: RecordsRunningOp,
223}
224
225/// The rows whose `field` is before `date` are left out of the rows and
226/// groups, totalled into `before` and seed every running value.
227#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
228pub struct RecordsBefore {
229    pub field: String,
230    pub date: String,
231}
232
233/// The structured query.
234#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
235pub struct RecordsQuery {
236    /// The kind; `None` = no kind (the value is null).
237    pub from: Option<String>,
238    /// Another app's slug (the HOST resolves whose rows); `None` = the hosting app.
239    pub app: Option<String>,
240    /// Only the viewer's rows (by author: `authorId`, else `createdBy`).
241    pub mine: bool,
242    /// Only this user's rows (by author).
243    pub user: Option<String>,
244    /// Every condition holds; by field, then operator (the URL sugar).
245    #[serde(rename = "where")]
246    pub where_: Vec<RecordsFilter>,
247    /// VAPP-190: one of these alternatives holds too (each: every condition).
248    #[serde(default)]
249    pub or: Vec<Vec<RecordsFilter>>,
250    /// Sort keys in order; ties always fall back to the id.
251    pub sort: Vec<RecordsSortKey>,
252    pub limit: u32,
253    /// Σ of this field over the matches (before the limit).
254    pub sum: Option<String>,
255    pub month: Option<RecordsMonthView>,
256    /// VAPP-190: the group-by keys.
257    #[serde(default)]
258    pub group: Vec<RecordsGroupKey>,
259    /// VAPP-190: the aggregates, by name.
260    #[serde(default)]
261    pub aggregate: Vec<RecordsAggregate>,
262    /// VAPP-190: the running values, by field.
263    #[serde(default)]
264    pub running: Vec<RecordsRunning>,
265    #[serde(default)]
266    pub before: Option<RecordsBefore>,
267}
268
269impl Default for RecordsQuery {
270    fn default() -> Self {
271        RecordsQuery {
272            from: None,
273            app: None,
274            mine: false,
275            user: None,
276            where_: Vec::new(),
277            or: Vec::new(),
278            sort: vec![RecordsSortKey { field: RECORDS_SORT_DEFAULT.to_string(), order: RecordsOrder::Desc }],
279            limit: RECORDS_LIMIT_DEFAULT,
280            sum: None,
281            month: None,
282            group: Vec::new(),
283            aggregate: Vec::new(),
284            running: Vec::new(),
285            before: None,
286        }
287    }
288}
289
290/// A problem with a JSON query, a JSON pointer into it.
291#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
292pub struct RecordsQueryIssue {
293    pub path: String,
294    pub message: String,
295}
296
297/// JavaScript's `Number(string)`: trimmed, empty = 0, `0x` / `0o` / `0b`
298/// integers, `[+-]Infinity`, a decimal literal; anything else NaN.
299pub(crate) fn js_string_number(s: &str) -> f64 {
300    let t = js_trim(s);
301    if t.is_empty() {
302        return 0.0;
303    }
304    for (prefix, radix) in [("0x", 16), ("0X", 16), ("0o", 8), ("0O", 8), ("0b", 2), ("0B", 2)] {
305        if let Some(digits) = t.strip_prefix(prefix) {
306            if digits.is_empty() || !digits.chars().all(|c| c.is_digit(radix)) {
307                return f64::NAN;
308            }
309            return digits.chars().fold(0.0, |n, c| n * f64::from(radix) + f64::from(c.to_digit(radix).unwrap_or(0)));
310        }
311    }
312    let (sign, body) = match t.as_bytes()[0] {
313        b'-' => (-1.0, &t[1..]),
314        b'+' => (1.0, &t[1..]),
315        _ => (1.0, t),
316    };
317    if body == "Infinity" {
318        return sign * f64::INFINITY;
319    }
320    let b = body.as_bytes();
321    let mut i = 0;
322    let digits = |i: &mut usize| {
323        let start = *i;
324        while b.get(*i).is_some_and(u8::is_ascii_digit) {
325            *i += 1;
326        }
327        *i - start
328    };
329    let int = digits(&mut i);
330    let frac = if b.get(i) == Some(&b'.') {
331        i += 1;
332        digits(&mut i)
333    } else {
334        0
335    };
336    if int == 0 && frac == 0 {
337        return f64::NAN;
338    }
339    if matches!(b.get(i), Some(b'e' | b'E')) {
340        i += 1;
341        if matches!(b.get(i), Some(b'+' | b'-')) {
342            i += 1;
343        }
344        if digits(&mut i) == 0 {
345            return f64::NAN;
346        }
347    }
348    if i != b.len() {
349        return f64::NAN;
350    }
351    body.parse::<f64>().map(|n| sign * n).unwrap_or(f64::NAN)
352}
353
354/// `YYYY-MM` with a month 01–12.
355fn is_month_key(text: &str) -> bool {
356    let b = text.as_bytes();
357    b.len() == 7 && b[..4].iter().all(u8::is_ascii_digit) && b[4] == b'-' && matches!(&text[5..7], "01" | "02" | "03" | "04" | "05" | "06" | "07" | "08" | "09" | "10" | "11" | "12")
358}
359
360fn trimmed(v: Option<&str>) -> Option<String> {
361    v.map(js_trim).filter(|s| !s.is_empty()).map(str::to_string)
362}
363
364/// Query params (as [`super::parse_source`] gives them) → the query. `kind`
365/// = the source's own name (`records:<kind>`). Unknown keys are field
366/// filters. VAPP-190: `q=<json>` = the
367/// JSON form ([`parse_records_json`]; the other params are ignored).
368pub fn parse_records_query(params: &BTreeMap<String, String>, kind: Option<&str>) -> RecordsQuery {
369    let from = trimmed(kind);
370    if let Some(q) = params.get("q").filter(|q| !js_trim(q).is_empty()) {
371        let json = serde_json::from_str::<Value>(q).ok().filter(Value::is_object).unwrap_or_else(|| json!({}));
372        return parse_records_json(&json, from.as_deref()).0;
373    }
374    let limit_raw = params.get("limit").map_or(f64::NAN, |l| js_string_number(l));
375    let mut keys: Vec<&String> = params.keys().collect();
376    keys.sort_by(|a, b| utf16_cmp(a, b));
377    let mut where_ = Vec::new();
378    for key in keys {
379        if key.is_empty() || RECORDS_RESERVED_PARAMS.contains(&key.as_str()) {
380            continue;
381        }
382        let value = params[key].clone();
383        match key.rfind('.').and_then(|dot| RecordsFilterOp::suffix(&key[dot + 1..]).map(|op| (dot, op))) {
384            Some((dot, op)) if dot > 0 => {
385                let value = if op.is_list() { RecordsFilterValue::Many(value.split(',').map(str::to_string).collect()) } else { RecordsFilterValue::One(value) };
386                where_.push(RecordsFilter { field: key[..dot].to_string(), op, value });
387            }
388            _ => where_.push(RecordsFilter { field: key.clone(), op: RecordsFilterOp::Eq, value: RecordsFilterValue::One(value) }),
389        }
390    }
391    where_.sort_by(|a, b| utf16_cmp(&a.field, &b.field).then_with(|| utf16_cmp(a.op.as_str(), b.op.as_str())));
392    let order = match params.get("order").map(String::as_str) {
393        Some("asc") => RecordsOrder::Asc,
394        Some("desc") => RecordsOrder::Desc,
395        _ if RECORDS_ORDER_DEFAULT == "asc" => RecordsOrder::Asc,
396        _ => RecordsOrder::Desc,
397    };
398    let limit = if limit_raw.is_finite() && limit_raw.fract() == 0.0 && limit_raw > 0.0 { limit_raw.min(f64::from(RECORDS_LIMIT_MAX)) as u32 } else { RECORDS_LIMIT_DEFAULT };
399    let by = trimmed(params.get("by").map(String::as_str));
400    let before = trimmed(params.get("before").map(String::as_str));
401    RecordsQuery {
402        from,
403        app: trimmed(params.get("app").map(String::as_str)),
404        mine: matches!(params.get("mine").map(String::as_str), Some("1" | "true")),
405        user: trimmed(params.get("user").map(String::as_str)),
406        where_,
407        sort: vec![RecordsSortKey { field: trimmed(params.get("sort").map(String::as_str)).unwrap_or_else(|| RECORDS_SORT_DEFAULT.to_string()), order }],
408        limit,
409        sum: trimmed(params.get("sum").map(String::as_str)),
410        before: before.map(|date| RecordsBefore { field: by.clone().unwrap_or_else(|| RECORDS_SORT_DEFAULT.to_string()), date }),
411        month: by.map(|field| RecordsMonthView { field, key: params.get("month").filter(|m| is_month_key(m)).cloned() }),
412        ..RecordsQuery::default()
413    }
414}
415
416/// `records:<kind>?…` → the query; `None` = not a records source.
417pub fn parse_records_source(source: &ParsedSource) -> Option<RecordsQuery> {
418    (source.scheme == RECORDS_SCHEME).then(|| parse_records_query(&source.params, Some(&source.name)))
419}
420
421/// A JSON scalar as the query's text (a number as JS prints it); `None` = not a scalar.
422fn scalar_text(v: &Value) -> Option<String> {
423    match v {
424        Value::Null => Some(String::new()),
425        Value::String(s) => Some(s.clone()),
426        Value::Bool(b) => Some(b.to_string()),
427        Value::Number(n) => Some(number_to_string(n.as_f64().unwrap_or(0.0))),
428        _ => None,
429    }
430}
431
432fn issue(issues: &mut Vec<RecordsQueryIssue>, path: impl Into<String>, message: impl Into<String>) {
433    issues.push(RecordsQueryIssue { path: path.into(), message: message.into() });
434}
435
436/// `[field, op, value…]` → a condition.
437fn parse_condition(entry: &Value, at: &str, issues: &mut Vec<RecordsQueryIssue>) -> Option<RecordsFilter> {
438    let Some(list) = entry.as_array().filter(|l| l.len() >= 3) else {
439        issue(issues, at, "a condition is [field, op, value]");
440        return None;
441    };
442    let Some(field) = list[0].as_str().map(js_trim).filter(|f| !f.is_empty()) else {
443        issue(issues, format!("{at}/0"), "a condition's field is a non-empty string");
444        return None;
445    };
446    let Some(op) = list[1].as_str().and_then(RecordsFilterOp::parse) else {
447        issue(issues, format!("{at}/1"), format!("unknown operator {}", crate::json::to_js_string(&list[1])));
448        return None;
449    };
450    let rest = &list[2..];
451    if op == RecordsFilterOp::In {
452        let values: Option<Vec<String>> = match rest {
453            [Value::Array(items)] => items.iter().map(scalar_text).collect(),
454            _ => None,
455        };
456        let Some(values) = values else {
457            issue(issues, format!("{at}/2"), "in takes one list of values");
458            return None;
459        };
460        return Some(RecordsFilter { field: field.to_string(), op, value: RecordsFilterValue::Many(values) });
461    }
462    let values: Option<Vec<String>> = rest.iter().map(scalar_text).collect();
463    let want = if op == RecordsFilterOp::Between { 2 } else { 1 };
464    let Some(mut values) = values.filter(|v| v.len() == want) else {
465        issue(issues, format!("{at}/2"), if op == RecordsFilterOp::Between { "between takes a low and a high value".to_string() } else { format!("{} takes one value", op.as_str()) });
466        return None;
467    };
468    let value = if op == RecordsFilterOp::Between { RecordsFilterValue::Many(values) } else { RecordsFilterValue::One(values.remove(0)) };
469    Some(RecordsFilter { field: field.to_string(), op, value })
470}
471
472const QUERY_KEYS: &[&str] = &["from", "app", "mine", "user", "where", "sort", "limit", "sum", "month", "group", "aggregate", "running", "before"];
473
474fn sorted_keys(map: &Map<String, Value>) -> Vec<&String> {
475    let mut keys: Vec<&String> = map.keys().collect();
476    keys.sort_by(|a, b| utf16_cmp(a, b));
477    keys
478}
479
480/// VAPP-190: the JSON form (a package's `queries`, the `q` param) → the
481/// query and every problem with it (a problem drops the part, the rest
482/// still runs). `kind` (a source's own) wins over `from`.
483pub fn parse_records_json(json: &Value, kind: Option<&str>) -> (RecordsQuery, Vec<RecordsQueryIssue>) {
484    let mut issues = Vec::new();
485    let mut query = RecordsQuery::default();
486    let Some(obj) = json.as_object() else {
487        issue(&mut issues, "", "a query is an object");
488        query.from = trimmed(kind);
489        return (query, issues);
490    };
491    for key in sorted_keys(obj) {
492        if !QUERY_KEYS.contains(&key.as_str()) && !key.starts_with('$') {
493            issue(&mut issues, format!("/{key}"), format!("unknown key {key}"));
494        }
495    }
496    let str_of = |key: &str, issues: &mut Vec<RecordsQueryIssue>| -> Option<String> {
497        match obj.get(key) {
498            None | Some(Value::Null) => None,
499            Some(Value::String(s)) => trimmed(Some(s)),
500            Some(_) => {
501                issue(issues, format!("/{key}"), format!("{key} is a string"));
502                None
503            }
504        }
505    };
506    query.from = match trimmed(kind) {
507        Some(k) => Some(k),
508        None => str_of("from", &mut issues),
509    };
510    query.app = str_of("app", &mut issues);
511    query.user = str_of("user", &mut issues);
512    query.sum = str_of("sum", &mut issues);
513    match obj.get("mine") {
514        None => {}
515        Some(Value::Bool(b)) => query.mine = *b,
516        Some(_) => issue(&mut issues, "/mine", "mine is a boolean"),
517    }
518    match obj.get("where") {
519        None => {}
520        Some(Value::Array(list)) => {
521            for (i, entry) in list.iter().enumerate() {
522                let at = format!("/where/{i}");
523                if let Some(alt) = entry.as_object() {
524                    let Some(alternatives) = alt.get("or").and_then(Value::as_array).filter(|_| alt.len() == 1) else {
525                        issue(&mut issues, at, "an alternative is {or: [[condition…]…]}");
526                        continue;
527                    };
528                    if !query.or.is_empty() {
529                        issue(&mut issues, at, "one or per query");
530                        continue;
531                    }
532                    for (j, alt) in alternatives.iter().enumerate() {
533                        let Some(conds) = alt.as_array() else {
534                            issue(&mut issues, format!("{at}/or/{j}"), "an alternative is a list of conditions");
535                            continue;
536                        };
537                        let parsed: Vec<RecordsFilter> = conds.iter().enumerate().filter_map(|(k, c)| parse_condition(c, &format!("{at}/or/{j}/{k}"), &mut issues)).collect();
538                        query.or.push(parsed);
539                    }
540                    continue;
541                }
542                if let Some(parsed) = parse_condition(entry, &at, &mut issues) {
543                    query.where_.push(parsed);
544                }
545            }
546        }
547        Some(_) => issue(&mut issues, "/where", "where is a list of conditions"),
548    }
549    if let Some(sort) = obj.get("sort") {
550        let single = sort.is_string();
551        let list: Option<Vec<Value>> = if single { Some(vec![sort.clone()]) } else { sort.as_array().cloned() };
552        match list {
553            None => issue(&mut issues, "/sort", "sort is a field or a list of [field, order]"),
554            Some(list) => {
555                let mut keys = Vec::new();
556                for (i, entry) in list.iter().enumerate() {
557                    let asc = Value::String("asc".into());
558                    let (name, order): (Option<&Value>, Option<&Value>) = match entry {
559                        Value::String(_) => (Some(entry), Some(&asc)),
560                        Value::Array(pair) => (pair.first(), if pair.len() > 1 { pair.get(1) } else { Some(&asc) }),
561                        _ => (None, None),
562                    };
563                    let name = name.and_then(Value::as_str).map(js_trim).filter(|n| !n.is_empty());
564                    let order = match order.and_then(Value::as_str) {
565                        Some("asc") => Some(RecordsOrder::Asc),
566                        Some("desc") => Some(RecordsOrder::Desc),
567                        _ => None,
568                    };
569                    match (name, order) {
570                        (Some(name), Some(order)) => keys.push(RecordsSortKey { field: name.to_string(), order }),
571                        _ => issue(&mut issues, if single { "/sort".to_string() } else { format!("/sort/{i}") }, "a sort key is field or [field, asc|desc]"),
572                    }
573                }
574                if !keys.is_empty() {
575                    query.sort = keys;
576                }
577            }
578        }
579    }
580    if let Some(limit) = obj.get("limit") {
581        let n = match limit {
582            Value::Number(n) => n.as_f64().unwrap_or(f64::NAN),
583            Value::String(s) if !js_trim(s).is_empty() => js_string_number(s),
584            _ => f64::NAN,
585        };
586        if n.is_finite() && n.fract() == 0.0 && n >= 1.0 {
587            query.limit = n.min(f64::from(RECORDS_LIMIT_MAX)) as u32;
588        } else {
589            issue(&mut issues, "/limit", "limit is a positive integer");
590        }
591    }
592    match obj.get("month") {
593        None | Some(Value::Null) => {}
594        Some(m) => {
595            let field = m.get("field").and_then(Value::as_str).map(js_trim).filter(|f| !f.is_empty());
596            let key = m.get("key");
597            match (m.as_object(), field) {
598                (Some(_), Some(field)) if matches!(key, None | Some(Value::Null) | Some(Value::String(_))) => {
599                    query.month = Some(RecordsMonthView { field: field.to_string(), key: key.and_then(Value::as_str).filter(|k| is_month_key(k)).map(str::to_string) });
600                }
601                _ => issue(&mut issues, "/month", "month is {field, key?}"),
602            }
603        }
604    }
605    match obj.get("group") {
606        None => {}
607        Some(Value::Array(list)) => {
608            for (i, entry) in list.iter().enumerate() {
609                let at = format!("/group/{i}");
610                let key = match entry {
611                    Value::String(s) if !js_trim(s).is_empty() => Some(RecordsGroupKey { field: js_trim(s).to_string(), bucket: None }),
612                    Value::Object(o) if o.len() == 2 => {
613                        let field = o.get("bucket").and_then(Value::as_str).map(js_trim).filter(|f| !f.is_empty());
614                        let bucket = o.get("by").and_then(Value::as_str).and_then(DateBucket::parse);
615                        field.zip(bucket).map(|(field, bucket)| RecordsGroupKey { field: field.to_string(), bucket: Some(bucket) })
616                    }
617                    _ => None,
618                };
619                let Some(key) = key else {
620                    issue(&mut issues, at, format!("a group key is a field or {{bucket: field, by: {}}}", DATE_BUCKETS.join("|")));
621                    continue;
622                };
623                if query.group.iter().any(|g| g.field == key.field || (key.field == "author" && g.field == "authorId") || (key.field == "authorId" && g.field == "author")) {
624                    issue(&mut issues, at, format!("duplicate group {}", key.field));
625                    continue;
626                }
627                query.group.push(key);
628            }
629        }
630        Some(_) => issue(&mut issues, "/group", "group is a list of keys"),
631    }
632    match obj.get("aggregate") {
633        None => {}
634        Some(Value::Object(aggs)) => {
635            for name in sorted_keys(aggs) {
636                let def = &aggs[name];
637                let at = format!("/aggregate/{name}");
638                if js_trim(name).is_empty() || RECORDS_AGGREGATE_RESERVED.contains(&name.as_str()) || query.group.iter().any(|g| g.field == *name || (g.field == "author" && name == "authorId")) {
639                    issue(&mut issues, at, format!("the name {name} is taken"));
640                    continue;
641                }
642                let list = def.as_array();
643                let op = list.and_then(|l| l.first()).and_then(Value::as_str).and_then(RecordsAggregateOp::parse);
644                let len = list.map_or(0, Vec::len);
645                let field = list.filter(|l| l.len() > 1).and_then(|l| l[1].as_str()).map(js_trim).filter(|f| !f.is_empty());
646                match op {
647                    Some(op) if len <= 2 && !(len > 1 && field.is_none()) && (op == RecordsAggregateOp::Count || field.is_some()) => {
648                        query.aggregate.push(RecordsAggregate { name: name.clone(), op, field: field.map(str::to_string) });
649                    }
650                    _ => issue(&mut issues, at, format!("an aggregate is [{}, field] (count: the field is optional)", RECORDS_AGGREGATE_OPS.join("|"))),
651                }
652            }
653        }
654        Some(_) => issue(&mut issues, "/aggregate", "aggregate is {name: [op, field?]}"),
655    }
656    match obj.get("running") {
657        None => {}
658        Some(Value::Object(running)) => {
659            for name in sorted_keys(running) {
660                match running[name].as_str().and_then(RecordsRunningOp::parse) {
661                    Some(op) if !js_trim(name).is_empty() => query.running.push(RecordsRunning { field: name.clone(), op }),
662                    _ => issue(&mut issues, format!("/running/{name}"), format!("a running value is {{field: {}}}", RECORDS_RUNNING_OPS.join("|"))),
663                }
664            }
665        }
666        Some(_) => issue(&mut issues, "/running", "running is {field: op}"),
667    }
668    match obj.get("before") {
669        None | Some(Value::Null) => {}
670        Some(Value::String(date)) => {
671            let implied = query.group.iter().find(|g| g.bucket.is_some()).map(|g| g.field.clone()).or_else(|| query.month.as_ref().map(|m| m.field.clone())).unwrap_or_else(|| RECORDS_SORT_DEFAULT.to_string());
672            query.before = trimmed(Some(date)).map(|date| RecordsBefore { field: implied, date });
673        }
674        Some(b) => {
675            let field = b.as_object().and_then(|o| o.get("field")).and_then(Value::as_str).map(js_trim).filter(|f| !f.is_empty());
676            match (field, b.get("date").and_then(Value::as_str)) {
677                (Some(field), Some(date)) => query.before = trimmed(Some(date)).map(|date| RecordsBefore { field: field.to_string(), date }),
678                _ => issue(&mut issues, "/before", "before is a date or {field, date}"),
679            }
680        }
681    }
682    (query, issues)
683}
684
685/// `${name}` in every string of a JSON query → `params[name]` ("" when unset).
686pub fn fill_query_templates(value: &Value, params: &BTreeMap<String, String>) -> Value {
687    match value {
688        Value::String(s) => {
689            let mut out = String::new();
690            let mut rest = s.as_str();
691            while let Some(start) = rest.find("${") {
692                let Some(len) = rest[start + 2..].find('}') else { break };
693                out.push_str(&rest[..start]);
694                let name = js_trim(&rest[start + 2..start + 2 + len]);
695                out.push_str(params.get(name).map_or("", String::as_str));
696                rest = &rest[start + 2 + len + 1..];
697            }
698            out.push_str(rest);
699            Value::String(out)
700        }
701        Value::Array(items) => Value::Array(items.iter().map(|v| fill_query_templates(v, params)).collect()),
702        Value::Object(map) => Value::Object(map.iter().map(|(k, v)| (k.clone(), fill_query_templates(v, params))).collect()),
703        other => other.clone(),
704    }
705}
706
707/// VAPP-190: a package binding `<scheme>:<name>?query=<id>&<params>` naming
708/// one of the package's `queries` → the same source with that query, its
709/// `${param}` templates filled from the other params, as `q=<canonical
710/// JSON>` (a `kind` param stays). Anything else is returned as written.
711pub fn expand_query_source(source: &str, queries: Option<&Value>) -> String {
712    let Some(parsed) = parse_source(source) else { return source.to_string() };
713    let Some(id) = parsed.params.get("query") else { return source.to_string() };
714    let Some(query) = queries.and_then(Value::as_object).and_then(|q| q.get(id)) else { return source.to_string() };
715    let mut params = parsed.params.clone();
716    params.remove("query");
717    let filled = fill_query_templates(query, &params);
718    let base = js_trim(source);
719    let head = base.split_once('?').map_or(base, |(h, _)| h);
720    source_with_params(head, Some(&json!({ "q": canonical_json(&filled) })))
721}
722
723/// One stored record as a host hands it to the evaluator (borrowed).
724#[derive(Debug, Clone, Copy, PartialEq)]
725pub struct RecordRowRef<'a> {
726    pub id: &'a str,
727    pub kind: &'a str,
728    pub data: &'a Value,
729    pub created_by: Option<&'a str>,
730    /// Whose record it is (#VAPP-187); `None` = `created_by`.
731    pub author_id: Option<&'a str>,
732    pub created_at: &'a str,
733    pub updated_at: &'a str,
734}
735
736impl<'a> RecordRowRef<'a> {
737    /// The row's author: `authorId`, else `createdBy`.
738    pub fn author(&self) -> Option<&'a str> {
739        self.author_id.or(self.created_by)
740    }
741}
742
743/// One stored record, owned (the FFI's rows JSON).
744#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
745#[serde(rename_all = "camelCase")]
746pub struct RecordRow {
747    pub id: String,
748    pub kind: String,
749    #[serde(default)]
750    pub data: Value,
751    #[serde(default)]
752    pub created_by: Option<String>,
753    #[serde(default)]
754    pub author_id: Option<String>,
755    pub created_at: String,
756    pub updated_at: String,
757}
758
759impl RecordRow {
760    pub fn as_ref(&self) -> RecordRowRef<'_> {
761        RecordRowRef {
762            id: &self.id,
763            kind: &self.kind,
764            data: &self.data,
765            created_by: self.created_by.as_deref(),
766            author_id: self.author_id.as_deref(),
767            created_at: &self.created_at,
768            updated_at: &self.updated_at,
769        }
770    }
771}
772
773/// What [`records_value`] needs besides the rows.
774#[derive(Debug, Clone, Copy)]
775pub struct RecordsContext<'a> {
776    /// The viewer's user id (`None` = signed out: `mine` matches nothing).
777    pub me: Option<&'a str>,
778    /// ISO 8601 (UTC): the month view's default month and `today`.
779    pub now: &'a str,
780    /// user id → display name (`author`; "" when unknown).
781    pub names: &'a HashMap<String, String>,
782    /// VAPP-214 (Q3): the queried kind's computed fields (`records.<kind>.computed`:
783    /// name → CEL), evaluated on every row of the kind before the query.
784    pub computed: Option<&'a BTreeMap<String, String>>,
785}
786
787/// VAPP-214 (Q3): at most this many computed fields per kind.
788pub const RECORDS_COMPUTED_MAX: usize = 16;
789/// The row keys a computed field reads next to its kind's fields (`author`, a name, never).
790pub const RECORDS_COMPUTED_ROW_KEYS: [&str; 5] = ["id", "createdAt", "updatedAt", "createdBy", "authorId"];
791
792/// VAPP-214 (Q3): a package's computed fields of `kind` (`records.<kind>.computed`,
793/// the string ones); `None` = none. What a host passes as `computed`.
794pub fn kind_computed(records: Option<&Value>, kind: Option<&str>) -> Option<BTreeMap<String, String>> {
795    let def = records?.as_object()?.get(kind?)?.as_object()?;
796    let out: BTreeMap<String, String> = def.get("computed")?.as_object()?.iter().filter_map(|(k, v)| v.as_str().map(|s| (k.clone(), s.to_string()))).collect();
797    (!out.is_empty()).then_some(out)
798}
799
800/// VAPP-214 (Q3): the data of every row of `kind` with its computed fields
801/// written in (`None` for other kinds' rows: they stay as they are). A field
802/// reads the row's data and the row keys by name (a missing one = null); it
803/// never reads another computed field (the checker refuses it). A failing
804/// or unparsable expression reads null. Evaluated in UTC with the query's
805/// `now`, like the query's own buckets. Never stored.
806pub fn computed_data(rows: &[RecordRowRef], kind: &str, computed: &BTreeMap<String, String>, now: &str) -> Vec<Option<Value>> {
807    let fields: Vec<(&String, Option<crate::cel::Node>, Vec<String>)> = computed
808        .iter()
809        .map(|(name, source)| {
810            let ast = crate::cel::parse_expression(source).ok();
811            let reads = ast.as_ref().map(crate::cel::expression_names).unwrap_or_default();
812            (name, ast, reads)
813        })
814        .collect();
815    let env = crate::cel::ExpressionEnv { now: Some(crate::cel::parse_timestamp(now).unwrap_or(0.0)), offset: None, max_steps: None };
816    rows.iter()
817        .map(|row| {
818            if row.kind != kind {
819                return None;
820            }
821            let mut vars: Map<String, Value> = row.data.as_object().cloned().unwrap_or_default();
822            vars.insert("id".into(), Value::String(row.id.to_string()));
823            vars.insert("createdAt".into(), Value::String(row.created_at.to_string()));
824            vars.insert("updatedAt".into(), Value::String(row.updated_at.to_string()));
825            vars.insert("createdBy".into(), row.created_by.map_or(Value::Null, |s| Value::String(s.to_string())));
826            vars.insert("authorId".into(), row.author().map_or(Value::Null, |s| Value::String(s.to_string())));
827            let mut data: Map<String, Value> = row.data.as_object().cloned().unwrap_or_default();
828            for (name, ast, reads) in &fields {
829                for r in reads {
830                    if !vars.contains_key(r) {
831                        vars.insert(r.clone(), Value::Null);
832                    }
833                }
834                let value = ast.as_ref().and_then(|ast| crate::cel::evaluate_node(ast, &vars, &env).ok()).unwrap_or(Value::Null);
835                data.insert((*name).clone(), value);
836            }
837            Some(Value::Object(data))
838        })
839        .collect()
840}
841
842/// A field as a JS value (`undefined` = `Missing`).
843#[derive(Clone, Copy)]
844enum Field<'a> {
845    Text(&'a str),
846    Json(&'a Value),
847    Missing,
848}
849
850fn num_text(s: &str) -> Option<f64> {
851    if js_trim(s).is_empty() {
852        return None;
853    }
854    Some(js_string_number(s)).filter(|f| f.is_finite())
855}
856
857fn num(v: &Field) -> Option<f64> {
858    match v {
859        Field::Json(Value::Number(n)) => n.as_f64().filter(|f| f.is_finite()),
860        Field::Json(Value::String(s)) => num_text(s),
861        Field::Text(s) => num_text(s),
862        Field::Json(_) | Field::Missing => None,
863    }
864}
865
866fn num_value(v: Option<&Value>) -> Option<f64> {
867    v.and_then(|v| num(&Field::Json(v)))
868}
869
870/// VAPP-202: the field as text, BORROWED when it is one (strings, the row
871/// keys); only numbers, booleans and objects allocate.
872fn text<'a>(v: &Field<'a>) -> Cow<'a, str> {
873    match *v {
874        Field::Missing | Field::Json(Value::Null) => Cow::Borrowed(""),
875        Field::Text(s) => Cow::Borrowed(s),
876        Field::Json(Value::String(s)) => Cow::Borrowed(s.as_str()),
877        Field::Json(Value::Bool(b)) => Cow::Borrowed(if *b { "true" } else { "false" }),
878        Field::Json(Value::Number(n)) => Cow::Owned(number_to_string(n.as_f64().unwrap_or(0.0))),
879        Field::Json(other) => Cow::Owned(canonical_json(other)),
880    }
881}
882
883/// The field as JSON (`undefined` = null).
884fn json_of(v: &Field) -> Value {
885    match v {
886        Field::Missing => Value::Null,
887        Field::Text(s) => Value::String((*s).to_string()),
888        Field::Json(v) => (*v).clone(),
889    }
890}
891
892/// VAPP-202: a field read ONCE for comparing (a sort key, a filter's bound):
893/// its number when it parses, else its text. [`cmp_keys`] = [`compare`]
894/// without parsing or stringifying per comparison.
895enum Key<'a> {
896    /// A number; the field stays for the text side of a mixed comparison.
897    Num(f64, Field<'a>),
898    Text(Cow<'a, str>),
899}
900
901impl<'a> Key<'a> {
902    fn of(v: Field<'a>) -> Self {
903        match num(&v) {
904            Some(n) => Key::Num(n, v),
905            None => Key::Text(text(&v)),
906        }
907    }
908
909    /// A text key's text ("" for a number: [`Col::of`] only asks text keys).
910    fn text_ref(&self) -> &str {
911        match self {
912            Key::Num(..) => "",
913            Key::Text(t) => t,
914        }
915    }
916
917    fn text(&self) -> Cow<'_, str> {
918        match self {
919            Key::Num(_, f) => text(f),
920            Key::Text(t) => Cow::Borrowed(t),
921        }
922    }
923}
924
925/// Numbers when both parse, else text (UTF-16 code units, no locale).
926fn cmp_keys(a: &Key, b: &Key) -> Ordering {
927    if let (Key::Num(na, _), Key::Num(nb, _)) = (a, b) {
928        return na.partial_cmp(nb).unwrap_or(Ordering::Equal);
929    }
930    utf16_cmp(&a.text(), &b.text())
931}
932
933fn compare(a: &Field, b: &Field) -> Ordering {
934    cmp_keys(&Key::of(*a), &Key::of(*b))
935}
936
937/// VAPP-202: a condition with its bounds parsed once per query, not per row.
938struct Cond<'q> {
939    filter: &'q RecordsFilter,
940    bounds: Vec<Key<'q>>,
941}
942
943impl<'q> Cond<'q> {
944    fn new(filter: &'q RecordsFilter) -> Self {
945        let bounds = match (&filter.value, filter.op) {
946            (_, RecordsFilterOp::Eq | RecordsFilterOp::In) => Vec::new(),
947            (RecordsFilterValue::One(v), _) => vec![Key::of(Field::Text(v))],
948            (RecordsFilterValue::Many(vs), _) => vs.iter().map(|v| Key::of(Field::Text(v))).collect(),
949        };
950        Cond { filter, bounds }
951    }
952
953    fn holds(&self, row: &RecordRowRef) -> bool {
954        let value = field(row, &self.filter.field);
955        match self.filter.op {
956            RecordsFilterOp::Eq => match &self.filter.value {
957                RecordsFilterValue::One(v) => text(&value) == v.as_str(),
958                RecordsFilterValue::Many(_) => false,
959            },
960            RecordsFilterOp::In => {
961                let t = text(&value);
962                match &self.filter.value {
963                    RecordsFilterValue::One(v) => t == v.as_str(),
964                    RecordsFilterValue::Many(vs) => vs.iter().any(|v| t == v.as_str()),
965                }
966            }
967            RecordsFilterOp::Between => self.bounds.len() == 2 && {
968                let k = Key::of(value);
969                cmp_keys(&k, &self.bounds[0]).is_ge() && cmp_keys(&k, &self.bounds[1]).is_le()
970            },
971            op => {
972                let [bound] = self.bounds.as_slice() else { return false };
973                let c = cmp_keys(&Key::of(value), bound);
974                match op {
975                    RecordsFilterOp::Gt => c.is_gt(),
976                    RecordsFilterOp::Gte => c.is_ge(),
977                    RecordsFilterOp::Lt => c.is_lt(),
978                    _ => c.is_le(),
979                }
980            }
981        }
982    }
983}
984
985fn data_field<'a>(row: &RecordRowRef<'a>, name: &str) -> Field<'a> {
986    match row.data.as_object().and_then(|o| o.get(name)) {
987        Some(v) => Field::Json(v),
988        None => Field::Missing,
989    }
990}
991
992static NULL: Value = Value::Null;
993
994/// A field as the query reads it: a row key (`authorId` = the author), else the data's.
995fn field<'a>(row: &RecordRowRef<'a>, name: &str) -> Field<'a> {
996    match name {
997        "createdAt" => Field::Text(row.created_at),
998        "updatedAt" => Field::Text(row.updated_at),
999        "id" => Field::Text(row.id),
1000        "createdBy" => row.created_by.map_or(Field::Json(&NULL), Field::Text),
1001        "authorId" => row.author().map_or(Field::Json(&NULL), Field::Text),
1002        _ => data_field(row, name),
1003    }
1004}
1005
1006fn parse_month(key: &str) -> (i64, i64) {
1007    let (y, m) = key.split_once('-').unwrap_or(("1970", "1"));
1008    (y.parse().unwrap_or(1970), m.parse::<i64>().unwrap_or(1).clamp(1, 12))
1009}
1010
1011/// `YYYY-MM` ± `by` months.
1012pub fn shift_month(key: &str, by: i64) -> String {
1013    let (y, m) = parse_month(key);
1014    let index = y * 12 + (m - 1) + by;
1015    format!("{}-{:02}", index.div_euclid(12), index.rem_euclid(12) + 1)
1016}
1017
1018/// `October 2026`.
1019pub fn month_label(key: &str) -> String {
1020    let (y, m) = parse_month(key);
1021    format!("{} {y}", RECORDS_MONTHS[(m - 1) as usize])
1022}
1023
1024/// One day of the month view.
1025#[derive(Debug, Clone, PartialEq)]
1026pub struct RecordsDay {
1027    pub date: String,
1028    pub day: i64,
1029    pub weekday: &'static str,
1030    pub count: u64,
1031    pub sum: f64,
1032}
1033
1034/// Every day of `key` (`YYYY-MM`), empty.
1035pub fn month_days(key: &str) -> Vec<RecordsDay> {
1036    let (y, m) = parse_month(key);
1037    (1..=days_in_month(y, m)).map(|day| RecordsDay { date: format!("{key}-{day:02}"), day, weekday: RECORDS_WEEKDAYS[weekday_of(y, m, day)], count: 0, sum: 0.0 }).collect()
1038}
1039
1040/// JS `Math.round(n * 1e6) / 1e6`: sums like 0.1 + 0.2 stay readable (and identical ×4).
1041fn round(n: f64) -> f64 {
1042    (n * 1_000_000.0 + 0.5).floor() / 1_000_000.0
1043}
1044
1045/// `s.slice(0, n)` (dates and ISO timestamps: ASCII).
1046fn prefix(s: &str, n: usize) -> String {
1047    s.chars().take(n).collect()
1048}
1049
1050/// One aggregate over rows in the query's order.
1051fn aggregate_of(agg: &RecordsAggregate, rows: &[&RecordRowRef]) -> Value {
1052    let values: Vec<Field> = match &agg.field {
1053        Some(f) => rows.iter().map(|r| field(r, f)).collect(),
1054        None => Vec::new(),
1055    };
1056    match agg.op {
1057        RecordsAggregateOp::Count => json!(if agg.field.is_none() { rows.len() } else { values.iter().filter(|v| !text(v).is_empty()).count() }),
1058        RecordsAggregateOp::Sum => number(round(values.iter().fold(0.0, |acc, v| acc + num(v).unwrap_or(0.0)))),
1059        RecordsAggregateOp::Avg => {
1060            let nums: Vec<f64> = values.iter().filter_map(num).collect();
1061            if nums.is_empty() {
1062                Value::Null
1063            } else {
1064                number(round(nums.iter().fold(0.0, |a, b| a + b) / nums.len() as f64))
1065            }
1066        }
1067        RecordsAggregateOp::Min | RecordsAggregateOp::Max => {
1068            let mut best: Option<&Field> = None;
1069            for v in &values {
1070                if text(v).is_empty() {
1071                    continue;
1072                }
1073                let better = match best {
1074                    None => true,
1075                    Some(b) if agg.op == RecordsAggregateOp::Min => compare(v, b).is_lt(),
1076                    Some(b) => compare(v, b).is_gt(),
1077                };
1078                if better {
1079                    best = Some(v);
1080                }
1081            }
1082            best.map_or(Value::Null, |b| num(b).map_or_else(|| Value::String(text(b).into_owned()), number))
1083        }
1084        RecordsAggregateOp::CountDistinct => {
1085            let mut seen: Vec<Cow<str>> = values.iter().map(text).filter(|t| !t.is_empty()).collect();
1086            seen.sort();
1087            seen.dedup();
1088            json!(seen.len())
1089        }
1090        RecordsAggregateOp::First => values.first().map_or(Value::Null, json_of),
1091        RecordsAggregateOp::Last => values.last().map_or(Value::Null, json_of),
1092    }
1093}
1094
1095fn sum_of(row: &RecordRowRef, f: &str) -> f64 {
1096    num_value(row.data.as_object().and_then(|o| o.get(f))).unwrap_or(0.0)
1097}
1098
1099/// count, sum?, then every aggregate: a group's, `totals`' and `before`'s columns.
1100fn summary(query: &RecordsQuery, rows: &[&RecordRowRef], out: &mut Map<String, Value>) {
1101    out.insert("count".into(), json!(rows.len()));
1102    if let Some(sum) = &query.sum {
1103        out.insert("sum".into(), number(round(rows.iter().fold(0.0, |acc, row| acc + sum_of(row, sum)))));
1104    }
1105    for agg in &query.aggregate {
1106        out.insert(agg.name.clone(), aggregate_of(agg, rows));
1107    }
1108}
1109
1110#[derive(Default, Clone, Copy)]
1111struct RunningAcc {
1112    sum: f64,
1113    count: u64,
1114    nums: u64,
1115    min: Option<f64>,
1116    max: Option<f64>,
1117}
1118
1119/// The running values: fed row by row, read after each step.
1120struct Running<'q> {
1121    list: &'q [RecordsRunning],
1122    acc: Vec<RunningAcc>,
1123}
1124
1125impl<'q> Running<'q> {
1126    fn new(list: &'q [RecordsRunning]) -> Self {
1127        Running { list, acc: vec![RunningAcc::default(); list.len()] }
1128    }
1129
1130    fn add(&mut self, row: &RecordRowRef) {
1131        for (r, a) in self.list.iter().zip(self.acc.iter_mut()) {
1132            a.count += 1;
1133            let Some(n) = num(&field(row, &r.field)) else { continue };
1134            a.sum += n;
1135            a.nums += 1;
1136            a.min = Some(a.min.map_or(n, |m| if n < m { n } else { m }));
1137            a.max = Some(a.max.map_or(n, |m| if n > m { n } else { m }));
1138        }
1139    }
1140
1141    fn read(&self) -> Value {
1142        let mut out = Map::new();
1143        for (r, a) in self.list.iter().zip(&self.acc) {
1144            let v = match r.op {
1145                RecordsRunningOp::Sum => number(round(a.sum)),
1146                RecordsRunningOp::Count => json!(a.count),
1147                RecordsRunningOp::Avg if a.nums > 0 => number(round(a.sum / a.nums as f64)),
1148                RecordsRunningOp::Avg => Value::Null,
1149                RecordsRunningOp::Min => a.min.map_or(Value::Null, number),
1150                RecordsRunningOp::Max => a.max.map_or(Value::Null, number),
1151            };
1152            out.insert(r.field.clone(), v);
1153        }
1154        Value::Object(out)
1155    }
1156}
1157
1158/// A group's part of the key: the author's id, a date's bucket key, any
1159/// other field's text. VAPP-202: computed once per row, borrowed when it can be.
1160fn group_part<'a>(key: &RecordsGroupKey, row: &RecordRowRef<'a>) -> Cow<'a, str> {
1161    if key.field == "author" {
1162        return Cow::Borrowed(row.author().unwrap_or(""));
1163    }
1164    let v = field(row, &key.field);
1165    match key.bucket {
1166        Some(bucket) => Cow::Owned(date_bucket(&text(&v), bucket).unwrap_or_default()),
1167        None => text(&v),
1168    }
1169}
1170
1171/// A group's column (from its first row): `author` = the author's name (+
1172/// `authorId`), a bucket = the date's bucket key, any other field its value.
1173fn group_columns(key: &RecordsGroupKey, row: &RecordRowRef, names: &HashMap<String, String>, cols: &mut Map<String, Value>) {
1174    if key.field == "author" {
1175        let author = row.author();
1176        cols.insert("author".into(), json!(author.and_then(|a| names.get(a)).cloned().unwrap_or_default()));
1177        cols.insert("authorId".into(), json!(author));
1178        return;
1179    }
1180    let v = field(row, &key.field);
1181    if let Some(bucket) = key.bucket {
1182        cols.insert(key.field.clone(), json!(date_bucket(&text(&v), bucket)));
1183        return;
1184    }
1185    cols.insert(key.field.clone(), json_of(&v));
1186}
1187
1188/// `s.slice(0, n)` borrowed (n chars).
1189fn prefix_str(s: &str, n: usize) -> &str {
1190    s.char_indices().nth(n).map_or(s, |(i, _)| &s[..i])
1191}
1192
1193/// One sort column, read once per row: every value a number, every value
1194/// text (ranked: the distinct texts sorted once, each row their index), or
1195/// both (the generic comparison).
1196enum Col<'a> {
1197    Num(Vec<f64>),
1198    Rank(Vec<u32>),
1199    Mixed(Vec<Key<'a>>),
1200}
1201
1202impl<'a> Col<'a> {
1203    fn of(rows: &[&RecordRowRef<'a>], name: &str) -> Self {
1204        let keys: Vec<Key<'a>> = rows.iter().map(|row| Key::of(field(row, name))).collect();
1205        if keys.iter().all(|k| matches!(k, Key::Num(..))) {
1206            return Col::Num(keys.iter().map(|k| if let Key::Num(n, _) = k { *n } else { 0.0 }).collect());
1207        }
1208        if !keys.iter().all(|k| matches!(k, Key::Text(_))) {
1209            return Col::Mixed(keys);
1210        }
1211        let mut ranks: FxHashMap<&str, u32> = FxHashMap::default();
1212        for k in &keys {
1213            ranks.entry(k.text_ref()).or_insert(0);
1214        }
1215        let mut distinct: Vec<&str> = ranks.keys().copied().collect();
1216        distinct.sort_unstable_by(|a, b| utf16_cmp(a, b));
1217        for (i, t) in distinct.iter().enumerate() {
1218            ranks.insert(t, i as u32);
1219        }
1220        Col::Rank(keys.iter().map(|k| ranks[k.text_ref()]).collect())
1221    }
1222
1223    fn cmp(&self, a: usize, b: usize) -> Ordering {
1224        match self {
1225            Col::Num(v) => v[a].partial_cmp(&v[b]).unwrap_or(Ordering::Equal),
1226            Col::Rank(v) => v[a].cmp(&v[b]),
1227            Col::Mixed(v) => cmp_keys(&v[a], &v[b]),
1228        }
1229    }
1230}
1231
1232/// VAPP-202: `rows` in the query's order (sort keys, ties by id), each
1233/// row's keys read ONCE into columns instead of parsing and stringifying
1234/// both sides of every comparison. Without a mixed column the order is
1235/// total (the index breaks an id tie), so the unstable sort gives the
1236/// stable one's result.
1237fn sorted<'r, 'a>(rows: Vec<&'r RecordRowRef<'a>>, sort: &[RecordsSortKey]) -> Vec<&'r RecordRowRef<'a>> {
1238    let cols: Vec<(Col<'a>, bool)> = sort.iter().map(|s| (Col::of(&rows, &s.field), s.order == RecordsOrder::Asc)).collect();
1239    let ascii: Vec<bool> = rows.iter().map(|row| row.id.is_ascii()).collect();
1240    let order = |a: usize, b: usize| {
1241        for (col, asc) in &cols {
1242            let c = col.cmp(a, b);
1243            if c.is_ne() {
1244                return if *asc { c } else { c.reverse() };
1245            }
1246        }
1247        let (ia, ib) = (rows[a].id, rows[b].id);
1248        if ascii[a] || ascii[b] {
1249            ia.as_bytes().cmp(ib.as_bytes())
1250        } else {
1251            utf16_cmp(ia, ib)
1252        }
1253    };
1254    let mut idx: Vec<usize> = (0..rows.len()).collect();
1255    if cols.iter().any(|(c, _)| matches!(c, Col::Mixed(_))) {
1256        idx.sort_by(|&a, &b| order(a, b));
1257    } else {
1258        idx.sort_unstable_by(|&a, &b| order(a, b).then(a.cmp(&b)));
1259    }
1260    idx.into_iter().map(|i| rows[i]).collect()
1261}
1262
1263/// The value a records binding emits for `query` over `rows` (every row of
1264/// the app, any kind): `None` without a kind; else {rows, count, sum?,
1265/// month?, groups?, totals?, before?}: the matches sorted (ties by id) then
1266/// cut at the limit, `count` and `sum` before the limit.
1267pub fn records_value(rows: &[RecordRowRef], query: &RecordsQuery, ctx: RecordsContext) -> Option<Value> {
1268    let kind = query.from.as_deref()?;
1269    // VAPP-214: the computed fields ride in each row's data from here on.
1270    if let Some(computed) = ctx.computed.filter(|c| !c.is_empty()) {
1271        let data = computed_data(rows, kind, computed, ctx.now);
1272        let rows: Vec<RecordRowRef> = rows.iter().zip(&data).map(|(row, d)| RecordRowRef { data: d.as_ref().unwrap_or(row.data), ..*row }).collect();
1273        return records_value(&rows, query, RecordsContext { computed: None, ..ctx });
1274    }
1275    let view = query.month.as_ref();
1276    let month_key: Option<String> = view.map(|v| v.key.clone().unwrap_or_else(|| prefix(ctx.now, 7)));
1277    let where_: Vec<Cond> = query.where_.iter().map(Cond::new).collect();
1278    let or: Vec<Vec<Cond>> = query.or.iter().map(|alt| alt.iter().map(Cond::new).collect()).collect();
1279    let before_date = query.before.as_ref().map(|b| Key::of(Field::Text(&b.date)));
1280    let mut before: Vec<&RecordRowRef> = Vec::new();
1281    let mut matched: Vec<&RecordRowRef> = Vec::new();
1282    for row in rows {
1283        if row.kind != kind {
1284            continue;
1285        }
1286        let author = row.author();
1287        if query.mine && (ctx.me.is_none() || author != ctx.me) {
1288            continue;
1289        }
1290        if let Some(user) = &query.user {
1291            if author != Some(user.as_str()) {
1292                continue;
1293            }
1294        }
1295        if !where_.iter().all(|c| c.holds(row)) {
1296            continue;
1297        }
1298        if !or.is_empty() && !or.iter().any(|alt| alt.iter().all(|c| c.holds(row))) {
1299            continue;
1300        }
1301        if let (Some(b), Some(date)) = (&query.before, &before_date) {
1302            let t = text(&field(row, &b.field));
1303            if !t.is_empty() && cmp_keys(&Key::of(Field::Text(&t)), date).is_lt() {
1304                before.push(row);
1305                continue;
1306            }
1307        }
1308        if let (Some(view), Some(key)) = (view, &month_key) {
1309            if !text(&data_field(row, &view.field)).starts_with(key.as_str()) {
1310                continue;
1311            }
1312        }
1313        matched.push(row);
1314    }
1315    let matched = sorted(matched, &query.sort);
1316    let before = sorted(before, &query.sort);
1317    let seeded = || {
1318        let mut r = Running::new(&query.running);
1319        for row in &before {
1320            r.add(row);
1321        }
1322        r
1323    };
1324    let mut row_running = (!query.running.is_empty() && query.group.is_empty()).then(seeded);
1325    let out_rows: Vec<Value> = matched
1326        .iter()
1327        .take(query.limit as usize)
1328        .map(|row| {
1329            let mut obj: Map<String, Value> = row.data.as_object().cloned().unwrap_or_default();
1330            obj.insert("id".into(), json!(row.id));
1331            obj.insert("createdAt".into(), json!(row.created_at));
1332            obj.insert("updatedAt".into(), json!(row.updated_at));
1333            obj.insert("createdBy".into(), json!(row.created_by));
1334            obj.insert("authorId".into(), json!(row.author()));
1335            let author = row.author().and_then(|a| ctx.names.get(a)).cloned().unwrap_or_default();
1336            obj.insert("author".into(), json!(author));
1337            if let Some(r) = row_running.as_mut() {
1338                r.add(row);
1339                obj.insert("running".into(), r.read());
1340            }
1341            Value::Object(obj)
1342        })
1343        .collect();
1344    let mut out = Map::new();
1345    out.insert("rows".into(), Value::Array(out_rows));
1346    out.insert("count".into(), json!(matched.len()));
1347    if let Some(sum) = &query.sum {
1348        let total = matched.iter().fold(0.0, |acc, row| acc + sum_of(row, sum));
1349        out.insert("sum".into(), number(round(total)));
1350    }
1351    if let (Some(view), Some(key)) = (view, &month_key) {
1352        let mut days = month_days(key);
1353        let mut per_day: Vec<Vec<&RecordRowRef>> = vec![Vec::new(); days.len()];
1354        for row in &matched {
1355            let t = text(&data_field(row, &view.field));
1356            let date = prefix_str(&t, 10);
1357            let Some(i) = days.iter().position(|d| d.date == date) else {
1358                continue;
1359            };
1360            let day = &mut days[i];
1361            day.count += 1;
1362            if let Some(sum) = &query.sum {
1363                day.sum = round(day.sum + sum_of(row, sum));
1364            }
1365            per_day[i].push(row);
1366        }
1367        let mut runnings: Vec<Option<Value>> = vec![None; days.len()];
1368        if !query.running.is_empty() {
1369            let mut r = seeded();
1370            for (i, rows) in per_day.iter().enumerate() {
1371                for row in rows {
1372                    r.add(row);
1373                }
1374                runnings[i] = Some(r.read());
1375            }
1376        }
1377        let labels: Vec<String> = days.iter().map(|d| d.day.to_string()).collect();
1378        let counts: Vec<u64> = days.iter().map(|d| d.count).collect();
1379        let sums: Vec<Value> = days.iter().map(|d| number(d.sum)).collect();
1380        let days_json: Vec<Value> = days
1381            .iter()
1382            .zip(runnings)
1383            .map(|(d, running)| {
1384                let mut day = json!({"date": d.date, "day": d.day, "weekday": d.weekday, "count": d.count, "sum": number(d.sum)});
1385                if let (Some(running), Some(obj)) = (running, day.as_object_mut()) {
1386                    obj.insert("running".into(), running);
1387                }
1388                day
1389            })
1390            .collect();
1391        out.insert(
1392            "month".into(),
1393            json!({
1394                "key": key,
1395                "label": month_label(key),
1396                "prev": shift_month(key, -1),
1397                "next": shift_month(key, 1),
1398                "today": prefix(ctx.now, 10),
1399                "days": days_json,
1400                "labels": labels,
1401                "counts": counts,
1402                "sums": sums,
1403            }),
1404        );
1405    }
1406    if !query.group.is_empty() {
1407        struct Group<'r, 'a> {
1408            parts: Vec<Cow<'a, str>>,
1409            rows: Vec<&'r RecordRowRef<'a>>,
1410            out: Map<String, Value>,
1411        }
1412        let mut groups: Vec<Group> = Vec::new();
1413        let mut index: FxHashMap<Vec<Cow<str>>, usize> = FxHashMap::default();
1414        let mut parts: Vec<Cow<str>> = Vec::with_capacity(query.group.len());
1415        for row in &matched {
1416            parts.clear();
1417            parts.extend(query.group.iter().map(|key| group_part(key, row)));
1418            match index.get(parts.as_slice()) {
1419                Some(&i) => groups[i].rows.push(row),
1420                None => {
1421                    let mut cols = Map::new();
1422                    for key in &query.group {
1423                        group_columns(key, row, ctx.names, &mut cols);
1424                    }
1425                    index.insert(parts.clone(), groups.len());
1426                    groups.push(Group { parts: parts.clone(), rows: vec![row], out: cols });
1427                }
1428            }
1429        }
1430        for g in &mut groups {
1431            summary(query, &g.rows, &mut g.out);
1432        }
1433        let mut columns: Vec<&str> = Vec::new();
1434        for g in &query.group {
1435            if g.field == "author" {
1436                columns.extend(["author", "authorId"]);
1437            } else {
1438                columns.push(&g.field);
1439            }
1440        }
1441        columns.push("count");
1442        if query.sum.is_some() {
1443            columns.push("sum");
1444        }
1445        columns.extend(query.aggregate.iter().map(|a| a.name.as_str()));
1446        let sort_keys: Vec<&RecordsSortKey> = query.sort.iter().filter(|k| columns.contains(&k.field.as_str())).collect();
1447        let col = |g: &Group, name: &str| -> Value { g.out.get(name).cloned().unwrap_or(Value::Null) };
1448        groups.sort_by(|a, b| {
1449            for key in &sort_keys {
1450                let (va, vb) = (col(a, &key.field), col(b, &key.field));
1451                let c = compare(&Field::Json(&va), &Field::Json(&vb));
1452                if c.is_ne() {
1453                    return if key.order == RecordsOrder::Asc { c } else { c.reverse() };
1454                }
1455            }
1456            for key in &query.group {
1457                let (va, vb) = (col(a, &key.field), col(b, &key.field));
1458                let c = compare(&Field::Json(&va), &Field::Json(&vb));
1459                if c.is_ne() {
1460                    return c;
1461                }
1462            }
1463            for (pa, pb) in a.parts.iter().zip(&b.parts) {
1464                let c = utf16_cmp(pa, pb);
1465                if c.is_ne() {
1466                    return c;
1467                }
1468            }
1469            Ordering::Equal
1470        });
1471        if !query.running.is_empty() {
1472            let mut r = seeded();
1473            for g in &mut groups {
1474                for row in &g.rows {
1475                    r.add(row);
1476                }
1477                g.out.insert("running".into(), r.read());
1478            }
1479        }
1480        out.insert("groups".into(), Value::Array(groups.into_iter().map(|g| Value::Object(g.out)).collect()));
1481    }
1482    if !query.aggregate.is_empty() {
1483        let mut totals = Map::new();
1484        summary(query, &matched, &mut totals);
1485        out.insert("totals".into(), Value::Object(totals));
1486    }
1487    if query.before.is_some() {
1488        let mut b = Map::new();
1489        summary(query, &before, &mut b);
1490        out.insert("before".into(), Value::Object(b));
1491    }
1492    Some(Value::Object(out))
1493}
1494
1495/// [`records_value`] over owned rows (the FFI's).
1496pub fn records_value_owned(rows: &[RecordRow], query: &RecordsQuery, ctx: RecordsContext) -> Option<Value> {
1497    let refs: Vec<RecordRowRef> = rows.iter().map(RecordRow::as_ref).collect();
1498    records_value(&refs, query, ctx)
1499}
1500
1501#[cfg(test)]
1502mod tests {
1503    use super::*;
1504
1505    #[test]
1506    fn number_parses_like_js() {
1507        assert_eq!(js_string_number(" 12 "), 12.0);
1508        assert_eq!(js_string_number("0x10"), 16.0);
1509        assert_eq!(js_string_number("1e2"), 100.0);
1510        assert_eq!(js_string_number(""), 0.0);
1511        assert!(js_string_number("12px").is_nan());
1512        assert!(js_string_number("inf").is_nan());
1513        assert!(js_string_number("-0x1").is_nan());
1514        assert_eq!(js_string_number("-Infinity"), f64::NEG_INFINITY);
1515    }
1516
1517    #[test]
1518    fn months_shift_across_years() {
1519        assert_eq!(shift_month("2026-01", -1), "2025-12");
1520        assert_eq!(shift_month("2026-12", 1), "2027-01");
1521        assert_eq!(shift_month("2026-03", -15), "2024-12");
1522        assert_eq!(month_label("2026-10"), "October 2026");
1523        assert_eq!(month_days("2028-02").len(), 29);
1524    }
1525}