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}
783
784/// A field as a JS value (`undefined` = `Missing`).
785#[derive(Clone, Copy)]
786enum Field<'a> {
787    Text(&'a str),
788    Json(&'a Value),
789    Missing,
790}
791
792fn num_text(s: &str) -> Option<f64> {
793    if js_trim(s).is_empty() {
794        return None;
795    }
796    Some(js_string_number(s)).filter(|f| f.is_finite())
797}
798
799fn num(v: &Field) -> Option<f64> {
800    match v {
801        Field::Json(Value::Number(n)) => n.as_f64().filter(|f| f.is_finite()),
802        Field::Json(Value::String(s)) => num_text(s),
803        Field::Text(s) => num_text(s),
804        Field::Json(_) | Field::Missing => None,
805    }
806}
807
808fn num_value(v: Option<&Value>) -> Option<f64> {
809    v.and_then(|v| num(&Field::Json(v)))
810}
811
812/// VAPP-202: the field as text, BORROWED when it is one (strings, the row
813/// keys); only numbers, booleans and objects allocate.
814fn text<'a>(v: &Field<'a>) -> Cow<'a, str> {
815    match *v {
816        Field::Missing | Field::Json(Value::Null) => Cow::Borrowed(""),
817        Field::Text(s) => Cow::Borrowed(s),
818        Field::Json(Value::String(s)) => Cow::Borrowed(s.as_str()),
819        Field::Json(Value::Bool(b)) => Cow::Borrowed(if *b { "true" } else { "false" }),
820        Field::Json(Value::Number(n)) => Cow::Owned(number_to_string(n.as_f64().unwrap_or(0.0))),
821        Field::Json(other) => Cow::Owned(canonical_json(other)),
822    }
823}
824
825/// The field as JSON (`undefined` = null).
826fn json_of(v: &Field) -> Value {
827    match v {
828        Field::Missing => Value::Null,
829        Field::Text(s) => Value::String((*s).to_string()),
830        Field::Json(v) => (*v).clone(),
831    }
832}
833
834/// VAPP-202: a field read ONCE for comparing (a sort key, a filter's bound):
835/// its number when it parses, else its text. [`cmp_keys`] = [`compare`]
836/// without parsing or stringifying per comparison.
837enum Key<'a> {
838    /// A number; the field stays for the text side of a mixed comparison.
839    Num(f64, Field<'a>),
840    Text(Cow<'a, str>),
841}
842
843impl<'a> Key<'a> {
844    fn of(v: Field<'a>) -> Self {
845        match num(&v) {
846            Some(n) => Key::Num(n, v),
847            None => Key::Text(text(&v)),
848        }
849    }
850
851    /// A text key's text ("" for a number: [`Col::of`] only asks text keys).
852    fn text_ref(&self) -> &str {
853        match self {
854            Key::Num(..) => "",
855            Key::Text(t) => t,
856        }
857    }
858
859    fn text(&self) -> Cow<'_, str> {
860        match self {
861            Key::Num(_, f) => text(f),
862            Key::Text(t) => Cow::Borrowed(t),
863        }
864    }
865}
866
867/// Numbers when both parse, else text (UTF-16 code units, no locale).
868fn cmp_keys(a: &Key, b: &Key) -> Ordering {
869    if let (Key::Num(na, _), Key::Num(nb, _)) = (a, b) {
870        return na.partial_cmp(nb).unwrap_or(Ordering::Equal);
871    }
872    utf16_cmp(&a.text(), &b.text())
873}
874
875fn compare(a: &Field, b: &Field) -> Ordering {
876    cmp_keys(&Key::of(*a), &Key::of(*b))
877}
878
879/// VAPP-202: a condition with its bounds parsed once per query, not per row.
880struct Cond<'q> {
881    filter: &'q RecordsFilter,
882    bounds: Vec<Key<'q>>,
883}
884
885impl<'q> Cond<'q> {
886    fn new(filter: &'q RecordsFilter) -> Self {
887        let bounds = match (&filter.value, filter.op) {
888            (_, RecordsFilterOp::Eq | RecordsFilterOp::In) => Vec::new(),
889            (RecordsFilterValue::One(v), _) => vec![Key::of(Field::Text(v))],
890            (RecordsFilterValue::Many(vs), _) => vs.iter().map(|v| Key::of(Field::Text(v))).collect(),
891        };
892        Cond { filter, bounds }
893    }
894
895    fn holds(&self, row: &RecordRowRef) -> bool {
896        let value = field(row, &self.filter.field);
897        match self.filter.op {
898            RecordsFilterOp::Eq => match &self.filter.value {
899                RecordsFilterValue::One(v) => text(&value) == v.as_str(),
900                RecordsFilterValue::Many(_) => false,
901            },
902            RecordsFilterOp::In => {
903                let t = text(&value);
904                match &self.filter.value {
905                    RecordsFilterValue::One(v) => t == v.as_str(),
906                    RecordsFilterValue::Many(vs) => vs.iter().any(|v| t == v.as_str()),
907                }
908            }
909            RecordsFilterOp::Between => self.bounds.len() == 2 && {
910                let k = Key::of(value);
911                cmp_keys(&k, &self.bounds[0]).is_ge() && cmp_keys(&k, &self.bounds[1]).is_le()
912            },
913            op => {
914                let [bound] = self.bounds.as_slice() else { return false };
915                let c = cmp_keys(&Key::of(value), bound);
916                match op {
917                    RecordsFilterOp::Gt => c.is_gt(),
918                    RecordsFilterOp::Gte => c.is_ge(),
919                    RecordsFilterOp::Lt => c.is_lt(),
920                    _ => c.is_le(),
921                }
922            }
923        }
924    }
925}
926
927fn data_field<'a>(row: &RecordRowRef<'a>, name: &str) -> Field<'a> {
928    match row.data.as_object().and_then(|o| o.get(name)) {
929        Some(v) => Field::Json(v),
930        None => Field::Missing,
931    }
932}
933
934static NULL: Value = Value::Null;
935
936/// A field as the query reads it: a row key (`authorId` = the author), else the data's.
937fn field<'a>(row: &RecordRowRef<'a>, name: &str) -> Field<'a> {
938    match name {
939        "createdAt" => Field::Text(row.created_at),
940        "updatedAt" => Field::Text(row.updated_at),
941        "id" => Field::Text(row.id),
942        "createdBy" => row.created_by.map_or(Field::Json(&NULL), Field::Text),
943        "authorId" => row.author().map_or(Field::Json(&NULL), Field::Text),
944        _ => data_field(row, name),
945    }
946}
947
948fn parse_month(key: &str) -> (i64, i64) {
949    let (y, m) = key.split_once('-').unwrap_or(("1970", "1"));
950    (y.parse().unwrap_or(1970), m.parse::<i64>().unwrap_or(1).clamp(1, 12))
951}
952
953/// `YYYY-MM` ± `by` months.
954pub fn shift_month(key: &str, by: i64) -> String {
955    let (y, m) = parse_month(key);
956    let index = y * 12 + (m - 1) + by;
957    format!("{}-{:02}", index.div_euclid(12), index.rem_euclid(12) + 1)
958}
959
960/// `October 2026`.
961pub fn month_label(key: &str) -> String {
962    let (y, m) = parse_month(key);
963    format!("{} {y}", RECORDS_MONTHS[(m - 1) as usize])
964}
965
966/// One day of the month view.
967#[derive(Debug, Clone, PartialEq)]
968pub struct RecordsDay {
969    pub date: String,
970    pub day: i64,
971    pub weekday: &'static str,
972    pub count: u64,
973    pub sum: f64,
974}
975
976/// Every day of `key` (`YYYY-MM`), empty.
977pub fn month_days(key: &str) -> Vec<RecordsDay> {
978    let (y, m) = parse_month(key);
979    (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()
980}
981
982/// JS `Math.round(n * 1e6) / 1e6`: sums like 0.1 + 0.2 stay readable (and identical ×4).
983fn round(n: f64) -> f64 {
984    (n * 1_000_000.0 + 0.5).floor() / 1_000_000.0
985}
986
987/// `s.slice(0, n)` (dates and ISO timestamps: ASCII).
988fn prefix(s: &str, n: usize) -> String {
989    s.chars().take(n).collect()
990}
991
992/// One aggregate over rows in the query's order.
993fn aggregate_of(agg: &RecordsAggregate, rows: &[&RecordRowRef]) -> Value {
994    let values: Vec<Field> = match &agg.field {
995        Some(f) => rows.iter().map(|r| field(r, f)).collect(),
996        None => Vec::new(),
997    };
998    match agg.op {
999        RecordsAggregateOp::Count => json!(if agg.field.is_none() { rows.len() } else { values.iter().filter(|v| !text(v).is_empty()).count() }),
1000        RecordsAggregateOp::Sum => number(round(values.iter().fold(0.0, |acc, v| acc + num(v).unwrap_or(0.0)))),
1001        RecordsAggregateOp::Avg => {
1002            let nums: Vec<f64> = values.iter().filter_map(num).collect();
1003            if nums.is_empty() {
1004                Value::Null
1005            } else {
1006                number(round(nums.iter().fold(0.0, |a, b| a + b) / nums.len() as f64))
1007            }
1008        }
1009        RecordsAggregateOp::Min | RecordsAggregateOp::Max => {
1010            let mut best: Option<&Field> = None;
1011            for v in &values {
1012                if text(v).is_empty() {
1013                    continue;
1014                }
1015                let better = match best {
1016                    None => true,
1017                    Some(b) if agg.op == RecordsAggregateOp::Min => compare(v, b).is_lt(),
1018                    Some(b) => compare(v, b).is_gt(),
1019                };
1020                if better {
1021                    best = Some(v);
1022                }
1023            }
1024            best.map_or(Value::Null, |b| num(b).map_or_else(|| Value::String(text(b).into_owned()), number))
1025        }
1026        RecordsAggregateOp::CountDistinct => {
1027            let mut seen: Vec<Cow<str>> = values.iter().map(text).filter(|t| !t.is_empty()).collect();
1028            seen.sort();
1029            seen.dedup();
1030            json!(seen.len())
1031        }
1032        RecordsAggregateOp::First => values.first().map_or(Value::Null, json_of),
1033        RecordsAggregateOp::Last => values.last().map_or(Value::Null, json_of),
1034    }
1035}
1036
1037fn sum_of(row: &RecordRowRef, f: &str) -> f64 {
1038    num_value(row.data.as_object().and_then(|o| o.get(f))).unwrap_or(0.0)
1039}
1040
1041/// count, sum?, then every aggregate: a group's, `totals`' and `before`'s columns.
1042fn summary(query: &RecordsQuery, rows: &[&RecordRowRef], out: &mut Map<String, Value>) {
1043    out.insert("count".into(), json!(rows.len()));
1044    if let Some(sum) = &query.sum {
1045        out.insert("sum".into(), number(round(rows.iter().fold(0.0, |acc, row| acc + sum_of(row, sum)))));
1046    }
1047    for agg in &query.aggregate {
1048        out.insert(agg.name.clone(), aggregate_of(agg, rows));
1049    }
1050}
1051
1052#[derive(Default, Clone, Copy)]
1053struct RunningAcc {
1054    sum: f64,
1055    count: u64,
1056    nums: u64,
1057    min: Option<f64>,
1058    max: Option<f64>,
1059}
1060
1061/// The running values: fed row by row, read after each step.
1062struct Running<'q> {
1063    list: &'q [RecordsRunning],
1064    acc: Vec<RunningAcc>,
1065}
1066
1067impl<'q> Running<'q> {
1068    fn new(list: &'q [RecordsRunning]) -> Self {
1069        Running { list, acc: vec![RunningAcc::default(); list.len()] }
1070    }
1071
1072    fn add(&mut self, row: &RecordRowRef) {
1073        for (r, a) in self.list.iter().zip(self.acc.iter_mut()) {
1074            a.count += 1;
1075            let Some(n) = num(&field(row, &r.field)) else { continue };
1076            a.sum += n;
1077            a.nums += 1;
1078            a.min = Some(a.min.map_or(n, |m| if n < m { n } else { m }));
1079            a.max = Some(a.max.map_or(n, |m| if n > m { n } else { m }));
1080        }
1081    }
1082
1083    fn read(&self) -> Value {
1084        let mut out = Map::new();
1085        for (r, a) in self.list.iter().zip(&self.acc) {
1086            let v = match r.op {
1087                RecordsRunningOp::Sum => number(round(a.sum)),
1088                RecordsRunningOp::Count => json!(a.count),
1089                RecordsRunningOp::Avg if a.nums > 0 => number(round(a.sum / a.nums as f64)),
1090                RecordsRunningOp::Avg => Value::Null,
1091                RecordsRunningOp::Min => a.min.map_or(Value::Null, number),
1092                RecordsRunningOp::Max => a.max.map_or(Value::Null, number),
1093            };
1094            out.insert(r.field.clone(), v);
1095        }
1096        Value::Object(out)
1097    }
1098}
1099
1100/// A group's part of the key: the author's id, a date's bucket key, any
1101/// other field's text. VAPP-202: computed once per row, borrowed when it can be.
1102fn group_part<'a>(key: &RecordsGroupKey, row: &RecordRowRef<'a>) -> Cow<'a, str> {
1103    if key.field == "author" {
1104        return Cow::Borrowed(row.author().unwrap_or(""));
1105    }
1106    let v = field(row, &key.field);
1107    match key.bucket {
1108        Some(bucket) => Cow::Owned(date_bucket(&text(&v), bucket).unwrap_or_default()),
1109        None => text(&v),
1110    }
1111}
1112
1113/// A group's column (from its first row): `author` = the author's name (+
1114/// `authorId`), a bucket = the date's bucket key, any other field its value.
1115fn group_columns(key: &RecordsGroupKey, row: &RecordRowRef, names: &HashMap<String, String>, cols: &mut Map<String, Value>) {
1116    if key.field == "author" {
1117        let author = row.author();
1118        cols.insert("author".into(), json!(author.and_then(|a| names.get(a)).cloned().unwrap_or_default()));
1119        cols.insert("authorId".into(), json!(author));
1120        return;
1121    }
1122    let v = field(row, &key.field);
1123    if let Some(bucket) = key.bucket {
1124        cols.insert(key.field.clone(), json!(date_bucket(&text(&v), bucket)));
1125        return;
1126    }
1127    cols.insert(key.field.clone(), json_of(&v));
1128}
1129
1130/// `s.slice(0, n)` borrowed (n chars).
1131fn prefix_str(s: &str, n: usize) -> &str {
1132    s.char_indices().nth(n).map_or(s, |(i, _)| &s[..i])
1133}
1134
1135/// One sort column, read once per row: every value a number, every value
1136/// text (ranked: the distinct texts sorted once, each row their index), or
1137/// both (the generic comparison).
1138enum Col<'a> {
1139    Num(Vec<f64>),
1140    Rank(Vec<u32>),
1141    Mixed(Vec<Key<'a>>),
1142}
1143
1144impl<'a> Col<'a> {
1145    fn of(rows: &[&RecordRowRef<'a>], name: &str) -> Self {
1146        let keys: Vec<Key<'a>> = rows.iter().map(|row| Key::of(field(row, name))).collect();
1147        if keys.iter().all(|k| matches!(k, Key::Num(..))) {
1148            return Col::Num(keys.iter().map(|k| if let Key::Num(n, _) = k { *n } else { 0.0 }).collect());
1149        }
1150        if !keys.iter().all(|k| matches!(k, Key::Text(_))) {
1151            return Col::Mixed(keys);
1152        }
1153        let mut ranks: FxHashMap<&str, u32> = FxHashMap::default();
1154        for k in &keys {
1155            ranks.entry(k.text_ref()).or_insert(0);
1156        }
1157        let mut distinct: Vec<&str> = ranks.keys().copied().collect();
1158        distinct.sort_unstable_by(|a, b| utf16_cmp(a, b));
1159        for (i, t) in distinct.iter().enumerate() {
1160            ranks.insert(t, i as u32);
1161        }
1162        Col::Rank(keys.iter().map(|k| ranks[k.text_ref()]).collect())
1163    }
1164
1165    fn cmp(&self, a: usize, b: usize) -> Ordering {
1166        match self {
1167            Col::Num(v) => v[a].partial_cmp(&v[b]).unwrap_or(Ordering::Equal),
1168            Col::Rank(v) => v[a].cmp(&v[b]),
1169            Col::Mixed(v) => cmp_keys(&v[a], &v[b]),
1170        }
1171    }
1172}
1173
1174/// VAPP-202: `rows` in the query's order (sort keys, ties by id), each
1175/// row's keys read ONCE into columns instead of parsing and stringifying
1176/// both sides of every comparison. Without a mixed column the order is
1177/// total (the index breaks an id tie), so the unstable sort gives the
1178/// stable one's result.
1179fn sorted<'r, 'a>(rows: Vec<&'r RecordRowRef<'a>>, sort: &[RecordsSortKey]) -> Vec<&'r RecordRowRef<'a>> {
1180    let cols: Vec<(Col<'a>, bool)> = sort.iter().map(|s| (Col::of(&rows, &s.field), s.order == RecordsOrder::Asc)).collect();
1181    let ascii: Vec<bool> = rows.iter().map(|row| row.id.is_ascii()).collect();
1182    let order = |a: usize, b: usize| {
1183        for (col, asc) in &cols {
1184            let c = col.cmp(a, b);
1185            if c.is_ne() {
1186                return if *asc { c } else { c.reverse() };
1187            }
1188        }
1189        let (ia, ib) = (rows[a].id, rows[b].id);
1190        if ascii[a] || ascii[b] {
1191            ia.as_bytes().cmp(ib.as_bytes())
1192        } else {
1193            utf16_cmp(ia, ib)
1194        }
1195    };
1196    let mut idx: Vec<usize> = (0..rows.len()).collect();
1197    if cols.iter().any(|(c, _)| matches!(c, Col::Mixed(_))) {
1198        idx.sort_by(|&a, &b| order(a, b));
1199    } else {
1200        idx.sort_unstable_by(|&a, &b| order(a, b).then(a.cmp(&b)));
1201    }
1202    idx.into_iter().map(|i| rows[i]).collect()
1203}
1204
1205/// The value a records binding emits for `query` over `rows` (every row of
1206/// the app, any kind): `None` without a kind; else {rows, count, sum?,
1207/// month?, groups?, totals?, before?}: the matches sorted (ties by id) then
1208/// cut at the limit, `count` and `sum` before the limit.
1209pub fn records_value(rows: &[RecordRowRef], query: &RecordsQuery, ctx: RecordsContext) -> Option<Value> {
1210    let kind = query.from.as_deref()?;
1211    let view = query.month.as_ref();
1212    let month_key: Option<String> = view.map(|v| v.key.clone().unwrap_or_else(|| prefix(ctx.now, 7)));
1213    let where_: Vec<Cond> = query.where_.iter().map(Cond::new).collect();
1214    let or: Vec<Vec<Cond>> = query.or.iter().map(|alt| alt.iter().map(Cond::new).collect()).collect();
1215    let before_date = query.before.as_ref().map(|b| Key::of(Field::Text(&b.date)));
1216    let mut before: Vec<&RecordRowRef> = Vec::new();
1217    let mut matched: Vec<&RecordRowRef> = Vec::new();
1218    for row in rows {
1219        if row.kind != kind {
1220            continue;
1221        }
1222        let author = row.author();
1223        if query.mine && (ctx.me.is_none() || author != ctx.me) {
1224            continue;
1225        }
1226        if let Some(user) = &query.user {
1227            if author != Some(user.as_str()) {
1228                continue;
1229            }
1230        }
1231        if !where_.iter().all(|c| c.holds(row)) {
1232            continue;
1233        }
1234        if !or.is_empty() && !or.iter().any(|alt| alt.iter().all(|c| c.holds(row))) {
1235            continue;
1236        }
1237        if let (Some(b), Some(date)) = (&query.before, &before_date) {
1238            let t = text(&field(row, &b.field));
1239            if !t.is_empty() && cmp_keys(&Key::of(Field::Text(&t)), date).is_lt() {
1240                before.push(row);
1241                continue;
1242            }
1243        }
1244        if let (Some(view), Some(key)) = (view, &month_key) {
1245            if !text(&data_field(row, &view.field)).starts_with(key.as_str()) {
1246                continue;
1247            }
1248        }
1249        matched.push(row);
1250    }
1251    let matched = sorted(matched, &query.sort);
1252    let before = sorted(before, &query.sort);
1253    let seeded = || {
1254        let mut r = Running::new(&query.running);
1255        for row in &before {
1256            r.add(row);
1257        }
1258        r
1259    };
1260    let mut row_running = (!query.running.is_empty() && query.group.is_empty()).then(seeded);
1261    let out_rows: Vec<Value> = matched
1262        .iter()
1263        .take(query.limit as usize)
1264        .map(|row| {
1265            let mut obj: Map<String, Value> = row.data.as_object().cloned().unwrap_or_default();
1266            obj.insert("id".into(), json!(row.id));
1267            obj.insert("createdAt".into(), json!(row.created_at));
1268            obj.insert("updatedAt".into(), json!(row.updated_at));
1269            obj.insert("createdBy".into(), json!(row.created_by));
1270            obj.insert("authorId".into(), json!(row.author()));
1271            let author = row.author().and_then(|a| ctx.names.get(a)).cloned().unwrap_or_default();
1272            obj.insert("author".into(), json!(author));
1273            if let Some(r) = row_running.as_mut() {
1274                r.add(row);
1275                obj.insert("running".into(), r.read());
1276            }
1277            Value::Object(obj)
1278        })
1279        .collect();
1280    let mut out = Map::new();
1281    out.insert("rows".into(), Value::Array(out_rows));
1282    out.insert("count".into(), json!(matched.len()));
1283    if let Some(sum) = &query.sum {
1284        let total = matched.iter().fold(0.0, |acc, row| acc + sum_of(row, sum));
1285        out.insert("sum".into(), number(round(total)));
1286    }
1287    if let (Some(view), Some(key)) = (view, &month_key) {
1288        let mut days = month_days(key);
1289        let mut per_day: Vec<Vec<&RecordRowRef>> = vec![Vec::new(); days.len()];
1290        for row in &matched {
1291            let t = text(&data_field(row, &view.field));
1292            let date = prefix_str(&t, 10);
1293            let Some(i) = days.iter().position(|d| d.date == date) else {
1294                continue;
1295            };
1296            let day = &mut days[i];
1297            day.count += 1;
1298            if let Some(sum) = &query.sum {
1299                day.sum = round(day.sum + sum_of(row, sum));
1300            }
1301            per_day[i].push(row);
1302        }
1303        let mut runnings: Vec<Option<Value>> = vec![None; days.len()];
1304        if !query.running.is_empty() {
1305            let mut r = seeded();
1306            for (i, rows) in per_day.iter().enumerate() {
1307                for row in rows {
1308                    r.add(row);
1309                }
1310                runnings[i] = Some(r.read());
1311            }
1312        }
1313        let labels: Vec<String> = days.iter().map(|d| d.day.to_string()).collect();
1314        let counts: Vec<u64> = days.iter().map(|d| d.count).collect();
1315        let sums: Vec<Value> = days.iter().map(|d| number(d.sum)).collect();
1316        let days_json: Vec<Value> = days
1317            .iter()
1318            .zip(runnings)
1319            .map(|(d, running)| {
1320                let mut day = json!({"date": d.date, "day": d.day, "weekday": d.weekday, "count": d.count, "sum": number(d.sum)});
1321                if let (Some(running), Some(obj)) = (running, day.as_object_mut()) {
1322                    obj.insert("running".into(), running);
1323                }
1324                day
1325            })
1326            .collect();
1327        out.insert(
1328            "month".into(),
1329            json!({
1330                "key": key,
1331                "label": month_label(key),
1332                "prev": shift_month(key, -1),
1333                "next": shift_month(key, 1),
1334                "today": prefix(ctx.now, 10),
1335                "days": days_json,
1336                "labels": labels,
1337                "counts": counts,
1338                "sums": sums,
1339            }),
1340        );
1341    }
1342    if !query.group.is_empty() {
1343        struct Group<'r, 'a> {
1344            parts: Vec<Cow<'a, str>>,
1345            rows: Vec<&'r RecordRowRef<'a>>,
1346            out: Map<String, Value>,
1347        }
1348        let mut groups: Vec<Group> = Vec::new();
1349        let mut index: FxHashMap<Vec<Cow<str>>, usize> = FxHashMap::default();
1350        let mut parts: Vec<Cow<str>> = Vec::with_capacity(query.group.len());
1351        for row in &matched {
1352            parts.clear();
1353            parts.extend(query.group.iter().map(|key| group_part(key, row)));
1354            match index.get(parts.as_slice()) {
1355                Some(&i) => groups[i].rows.push(row),
1356                None => {
1357                    let mut cols = Map::new();
1358                    for key in &query.group {
1359                        group_columns(key, row, ctx.names, &mut cols);
1360                    }
1361                    index.insert(parts.clone(), groups.len());
1362                    groups.push(Group { parts: parts.clone(), rows: vec![row], out: cols });
1363                }
1364            }
1365        }
1366        for g in &mut groups {
1367            summary(query, &g.rows, &mut g.out);
1368        }
1369        let mut columns: Vec<&str> = Vec::new();
1370        for g in &query.group {
1371            if g.field == "author" {
1372                columns.extend(["author", "authorId"]);
1373            } else {
1374                columns.push(&g.field);
1375            }
1376        }
1377        columns.push("count");
1378        if query.sum.is_some() {
1379            columns.push("sum");
1380        }
1381        columns.extend(query.aggregate.iter().map(|a| a.name.as_str()));
1382        let sort_keys: Vec<&RecordsSortKey> = query.sort.iter().filter(|k| columns.contains(&k.field.as_str())).collect();
1383        let col = |g: &Group, name: &str| -> Value { g.out.get(name).cloned().unwrap_or(Value::Null) };
1384        groups.sort_by(|a, b| {
1385            for key in &sort_keys {
1386                let (va, vb) = (col(a, &key.field), col(b, &key.field));
1387                let c = compare(&Field::Json(&va), &Field::Json(&vb));
1388                if c.is_ne() {
1389                    return if key.order == RecordsOrder::Asc { c } else { c.reverse() };
1390                }
1391            }
1392            for key in &query.group {
1393                let (va, vb) = (col(a, &key.field), col(b, &key.field));
1394                let c = compare(&Field::Json(&va), &Field::Json(&vb));
1395                if c.is_ne() {
1396                    return c;
1397                }
1398            }
1399            for (pa, pb) in a.parts.iter().zip(&b.parts) {
1400                let c = utf16_cmp(pa, pb);
1401                if c.is_ne() {
1402                    return c;
1403                }
1404            }
1405            Ordering::Equal
1406        });
1407        if !query.running.is_empty() {
1408            let mut r = seeded();
1409            for g in &mut groups {
1410                for row in &g.rows {
1411                    r.add(row);
1412                }
1413                g.out.insert("running".into(), r.read());
1414            }
1415        }
1416        out.insert("groups".into(), Value::Array(groups.into_iter().map(|g| Value::Object(g.out)).collect()));
1417    }
1418    if !query.aggregate.is_empty() {
1419        let mut totals = Map::new();
1420        summary(query, &matched, &mut totals);
1421        out.insert("totals".into(), Value::Object(totals));
1422    }
1423    if query.before.is_some() {
1424        let mut b = Map::new();
1425        summary(query, &before, &mut b);
1426        out.insert("before".into(), Value::Object(b));
1427    }
1428    Some(Value::Object(out))
1429}
1430
1431/// [`records_value`] over owned rows (the FFI's).
1432pub fn records_value_owned(rows: &[RecordRow], query: &RecordsQuery, ctx: RecordsContext) -> Option<Value> {
1433    let refs: Vec<RecordRowRef> = rows.iter().map(RecordRow::as_ref).collect();
1434    records_value(&refs, query, ctx)
1435}
1436
1437#[cfg(test)]
1438mod tests {
1439    use super::*;
1440
1441    #[test]
1442    fn number_parses_like_js() {
1443        assert_eq!(js_string_number(" 12 "), 12.0);
1444        assert_eq!(js_string_number("0x10"), 16.0);
1445        assert_eq!(js_string_number("1e2"), 100.0);
1446        assert_eq!(js_string_number(""), 0.0);
1447        assert!(js_string_number("12px").is_nan());
1448        assert!(js_string_number("inf").is_nan());
1449        assert!(js_string_number("-0x1").is_nan());
1450        assert_eq!(js_string_number("-Infinity"), f64::NEG_INFINITY);
1451    }
1452
1453    #[test]
1454    fn months_shift_across_years() {
1455        assert_eq!(shift_month("2026-01", -1), "2025-12");
1456        assert_eq!(shift_month("2026-12", 1), "2027-01");
1457        assert_eq!(shift_month("2026-03", -15), "2024-12");
1458        assert_eq!(month_label("2026-10"), "October 2026");
1459        assert_eq!(month_days("2028-02").len(), 29);
1460    }
1461}