aylis-data 0.14.0

Camada de dados do Aylis: ClientDataSet, Field, DataSource e delta para o backend
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! Expressões sobre os campos de um registro: o `filter` do dataset e os campos
//! calculados (`expression` do [`Field`](crate::Field)).
//!
//! ```text
//! cidade = 'São Paulo' and limite >= 1000
//! nome like 'mar%' or not ativo
//! uf in ('SP', 'RJ') and data_cadastro >= '2024-01-01'
//! quantidade * preco_unitario            (campo calculado)
//! upper(nome) + ' (' + uf + ')'
//! iif(limite > 5000, 'VIP', 'Normal')
//! ```
//!
//! - comparações: `=`, `<>` (ou `!=`), `<`, `<=`, `>`, `>=`, `like` (com `%` e `_`,
//!   sem diferenciar maiúsculas), `in (...)`, `is null`, `is not null`;
//! - texto compara sem diferenciar maiúsculas e minúsculas;
//! - lógicos: `and`, `or`, `not`; contas: `+ - * / %` (`+` junta textos);
//! - funções: `upper`, `lower`, `fold` (sem acento e sem maiúscula), `trim`, `len`, `substr(t, início, tamanho)`, `round(x, casas, modo)`,
//!   `abs`, `coalesce(a, b)`, `iif(condição, a, b)`, `year`, `month`, `day`, `today()` e
//!   `now()` (no horário local da tela);
//! - nomes de campo com espaço ou acento: `[Data de cadastro]`.

use std::cmp::Ordering;

use crate::value::{Date, Decimal, Rounding, Value};

/// Uma expressão já lida (lida uma vez, avaliada em cada registro).
#[derive(Clone, Debug, PartialEq)]
pub struct Expr(Node);

#[derive(Clone, Debug, PartialEq)]
enum Node {
    Const(Value),
    Field(String),
    Unary(char, Box<Node>),
    Binary(Op, Box<Node>, Box<Node>),
    Not(Box<Node>),
    And(Box<Node>, Box<Node>),
    Or(Box<Node>, Box<Node>),
    Like(Box<Node>, Box<Node>, bool),
    In(Box<Node>, Vec<Node>, bool),
    IsNull(Box<Node>, bool),
    Call(String, Vec<Node>),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Op {
    Eq,
    Ne,
    Lt,
    Le,
    Gt,
    Ge,
    Add,
    Sub,
    Mul,
    Div,
    Mod,
}

impl Expr {
    /// Lê a expressão. O erro diz onde está o problema.
    pub fn parse(text: &str) -> Result<Expr, String> {
        if text.len() > MAX_TEXT {
            return Err(format!("expressão longa demais ({} KB; o máximo é {} KB)", text.len() / 1024, MAX_TEXT / 1024));
        }
        let tokens = tokenize(text)?;
        let mut p = Parser { tokens, pos: 0, depth: 0, nesting: 0 };
        let node = p.or()?;
        if p.pos < p.tokens.len() {
            return Err(format!("sobrou '{}' no fim da expressão", p.tokens[p.pos].text()));
        }
        Ok(Expr(node))
    }

    /// Nomes de campo usados (para conferir que existem); os parâmetros ficam de fora.
    pub fn fields(&self) -> Vec<String> {
        self.names().into_iter().filter(|n| !n.starts_with(':')).collect()
    }

    /// Parâmetros usados (`:minimo` → `minimo`).
    pub fn params(&self) -> Vec<String> {
        self.names().into_iter().filter_map(|n| n.strip_prefix(':').map(str::to_string)).collect()
    }

    fn names(&self) -> Vec<String> {
        fn walk(n: &Node, out: &mut Vec<String>) {
            match n {
                Node::Field(f) => {
                    if !out.iter().any(|o: &String| o.eq_ignore_ascii_case(f)) {
                        out.push(f.clone());
                    }
                }
                Node::Unary(_, a) | Node::Not(a) | Node::IsNull(a, _) => walk(a, out),
                Node::Binary(_, a, b) | Node::And(a, b) | Node::Or(a, b) | Node::Like(a, b, _) => {
                    walk(a, out);
                    walk(b, out);
                }
                Node::In(a, list, _) => {
                    walk(a, out);
                    list.iter().for_each(|x| walk(x, out));
                }
                Node::Call(_, args) => args.iter().for_each(|x| walk(x, out)),
                Node::Const(_) => {}
            }
        }
        let mut out = Vec::new();
        walk(&self.0, &mut out);
        out
    }

    /// Avalia com os valores do registro (`get` devolve o valor de um campo pelo nome).
    pub fn eval(&self, get: &dyn Fn(&str) -> Option<Value>) -> Result<Value, String> {
        eval(&self.0, get)
    }

    /// Avalia como condição (filtro): `Null` e erros contam como falso.
    pub fn matches(&self, get: &dyn Fn(&str) -> Option<Value>) -> bool {
        matches!(self.eval(get), Ok(Value::Bool(true)))
    }
}

// ---------------------------------------------------------------------------
// Tokens

#[derive(Clone, Debug, PartialEq)]
enum Tok {
    Num(String),
    Str(String),
    Ident(String),
    Sym(&'static str),
}

impl Tok {
    fn text(&self) -> String {
        match self {
            Tok::Num(s) | Tok::Ident(s) => s.clone(),
            Tok::Str(s) => format!("'{s}'"),
            Tok::Sym(s) => s.to_string(),
        }
    }

    fn is_word(&self, word: &str) -> bool {
        matches!(self, Tok::Ident(s) if s.eq_ignore_ascii_case(word))
    }
}

fn tokenize(text: &str) -> Result<Vec<Tok>, String> {
    let chars: Vec<char> = text.chars().collect();
    let mut out = Vec::new();
    let mut i = 0;
    while i < chars.len() {
        let c = chars[i];
        if c.is_whitespace() {
            i += 1;
        } else if c.is_ascii_digit() || (c == '.' && chars.get(i + 1).is_some_and(char::is_ascii_digit)) {
            let start = i;
            while i < chars.len() && (chars[i].is_ascii_digit() || chars[i] == '.') {
                i += 1;
            }
            out.push(Tok::Num(chars[start..i].iter().collect()));
        } else if c == '\'' || c == '"' {
            let quote = c;
            let mut s = String::new();
            i += 1;
            loop {
                match chars.get(i) {
                    None => return Err("texto sem a aspa de fechamento".into()),
                    Some(&q) if q == quote && chars.get(i + 1) == Some(&quote) => {
                        s.push(quote);
                        i += 2;
                    }
                    Some(&q) if q == quote => {
                        i += 1;
                        break;
                    }
                    Some(&ch) => {
                        s.push(ch);
                        i += 1;
                    }
                }
            }
            out.push(Tok::Str(s));
        } else if c == '[' {
            let end = chars[i..].iter().position(|&x| x == ']').ok_or("nome de campo sem ']'")?;
            out.push(Tok::Ident(chars[i + 1..i + end].iter().collect()));
            i += end + 1;
        } else if c == ':' && chars.get(i + 1).is_some_and(|n| n.is_alphabetic() || *n == '_') {
            // Parâmetro (`:minimo`): lido como um campo de nome ":minimo"; quem avalia
            // entrega o valor.
            let start = i;
            i += 1;
            while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_') {
                i += 1;
            }
            out.push(Tok::Ident(chars[start..i].iter().collect()));
        } else if c.is_alphabetic() || c == '_' {
            let start = i;
            while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_' || chars[i] == '.') {
                i += 1;
            }
            out.push(Tok::Ident(chars[start..i].iter().collect()));
        } else {
            let two: String = chars[i..(i + 2).min(chars.len())].iter().collect();
            let sym = match two.as_str() {
                "<>" | "!=" => Some("<>"),
                "<=" => Some("<="),
                ">=" => Some(">="),
                "==" => Some("="),
                "&&" => Some("and"),
                "||" => Some("or"),
                _ => None,
            };
            if let Some(s) = sym {
                out.push(Tok::Sym(s));
                i += 2;
                continue;
            }
            let one = match c {
                '=' => "=",
                '<' => "<",
                '>' => ">",
                '+' => "+",
                '-' => "-",
                '*' => "*",
                '/' => "/",
                '%' => "%",
                '(' => "(",
                ')' => ")",
                ',' => ",",
                '!' => "not",
                _ => return Err(format!("caractere inesperado '{c}'")),
            };
            out.push(Tok::Sym(one));
            i += 1;
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Leitura (descida recursiva)

/// Profundidade máxima da árvore: a avaliação e o descarte são recursivos, e um texto como
/// `((((…` ou `a + a + a…` com milhares de termos estouraria a pilha, o que encerra o processo
/// sem chance de tratar. Conferida enquanto a árvore é montada (montada inteira, ela já
/// estouraria ao ser descartada).
const MAX_DEPTH: usize = 256;
/// Tamanho máximo do texto de uma expressão.
const MAX_TEXT: usize = 64 * 1024;

fn too_deep() -> String {
    format!("expressão aninhada demais (o máximo é {MAX_DEPTH} níveis)")
}

struct Parser {
    tokens: Vec<Tok>,
    pos: usize,
    /// Profundidade do último nó devolvido.
    depth: usize,
    /// Leituras aninhadas abertas (parênteses, sinais, `not`): limitam a recursão da própria
    /// leitura, que em `((((a))))` não aumenta a profundidade da árvore.
    nesting: usize,
}

impl Parser {
    /// Um nó acima de filhos com profundidade `below`.
    fn node(&mut self, node: Node, below: usize) -> Result<Node, String> {
        self.depth = below + 1;
        if self.depth > MAX_DEPTH {
            return Err(too_deep());
        }
        Ok(node)
    }

    fn enter(&mut self) -> Result<(), String> {
        self.nesting += 1;
        if self.nesting > MAX_DEPTH { Err(too_deep()) } else { Ok(()) }
    }

    fn leave(&mut self) {
        self.nesting -= 1;
    }

    fn peek(&self) -> Option<&Tok> {
        self.tokens.get(self.pos)
    }

    fn eat_sym(&mut self, s: &str) -> bool {
        if matches!(self.peek(), Some(Tok::Sym(x)) if *x == s) {
            self.pos += 1;
            true
        } else {
            false
        }
    }

    fn eat_word(&mut self, w: &str) -> bool {
        if self.peek().is_some_and(|t| t.is_word(w)) || (matches!(self.peek(), Some(Tok::Sym(x)) if *x == w)) {
            self.pos += 1;
            true
        } else {
            false
        }
    }

    fn expect(&mut self, s: &str) -> Result<(), String> {
        if self.eat_sym(s) {
            Ok(())
        } else {
            Err(format!("esperava '{s}'{}", self.peek().map(|t| format!(" e veio '{}'", t.text())).unwrap_or_default()))
        }
    }

    fn or(&mut self) -> Result<Node, String> {
        let mut left = self.and()?;
        let mut depth = self.depth;
        while self.eat_word("or") {
            let right = self.and()?;
            left = self.node(Node::Or(Box::new(left), Box::new(right)), depth.max(self.depth))?;
            depth = self.depth;
        }
        self.depth = depth;
        Ok(left)
    }

    fn and(&mut self) -> Result<Node, String> {
        let mut left = self.not()?;
        let mut depth = self.depth;
        while self.eat_word("and") {
            let right = self.not()?;
            left = self.node(Node::And(Box::new(left), Box::new(right)), depth.max(self.depth))?;
            depth = self.depth;
        }
        self.depth = depth;
        Ok(left)
    }

    fn not(&mut self) -> Result<Node, String> {
        if self.eat_word("not") {
            self.enter()?;
            let inner = self.not()?;
            self.leave();
            let below = self.depth;
            return self.node(Node::Not(Box::new(inner)), below);
        }
        self.compare()
    }

    fn compare(&mut self) -> Result<Node, String> {
        let left = self.add()?;
        let below = self.depth;
        let op = match self.peek() {
            Some(Tok::Sym("=")) => Some(Op::Eq),
            Some(Tok::Sym("<>")) => Some(Op::Ne),
            Some(Tok::Sym("<")) => Some(Op::Lt),
            Some(Tok::Sym("<=")) => Some(Op::Le),
            Some(Tok::Sym(">")) => Some(Op::Gt),
            Some(Tok::Sym(">=")) => Some(Op::Ge),
            _ => None,
        };
        if let Some(op) = op {
            self.pos += 1;
            let right = self.add()?;
            let below = below.max(self.depth);
            return self.node(Node::Binary(op, Box::new(left), Box::new(right)), below);
        }
        let negated = self.peek().is_some_and(|t| t.is_word("not"))
            && self.tokens.get(self.pos + 1).is_some_and(|t| t.is_word("like") || t.is_word("in"));
        if negated {
            self.pos += 1;
        }
        if self.eat_word("like") {
            let pattern = self.add()?;
            let below = below.max(self.depth);
            return self.node(Node::Like(Box::new(left), Box::new(pattern), negated), below);
        }
        if self.eat_word("in") {
            self.expect("(")?;
            let mut list = vec![self.add()?];
            let mut below = below.max(self.depth);
            while self.eat_sym(",") {
                list.push(self.add()?);
                below = below.max(self.depth);
            }
            self.expect(")")?;
            return self.node(Node::In(Box::new(left), list, negated), below);
        }
        if self.eat_word("is") {
            let not = self.eat_word("not");
            if !self.eat_word("null") {
                return Err("esperava 'null' depois de 'is'".into());
            }
            return self.node(Node::IsNull(Box::new(left), not), below);
        }
        self.depth = below;
        Ok(left)
    }

    fn add(&mut self) -> Result<Node, String> {
        let mut left = self.mul()?;
        let mut depth = self.depth;
        loop {
            let op = if self.eat_sym("+") {
                Op::Add
            } else if self.eat_sym("-") {
                Op::Sub
            } else {
                self.depth = depth;
                return Ok(left);
            };
            let right = self.mul()?;
            left = self.node(Node::Binary(op, Box::new(left), Box::new(right)), depth.max(self.depth))?;
            depth = self.depth;
        }
    }

    fn mul(&mut self) -> Result<Node, String> {
        let mut left = self.unary()?;
        let mut depth = self.depth;
        loop {
            let op = if self.eat_sym("*") {
                Op::Mul
            } else if self.eat_sym("/") {
                Op::Div
            } else if self.eat_sym("%") {
                Op::Mod
            } else {
                self.depth = depth;
                return Ok(left);
            };
            let right = self.unary()?;
            left = self.node(Node::Binary(op, Box::new(left), Box::new(right)), depth.max(self.depth))?;
            depth = self.depth;
        }
    }

    fn unary(&mut self) -> Result<Node, String> {
        if self.eat_sym("-") {
            self.enter()?;
            let inner = self.unary()?;
            self.leave();
            let below = self.depth;
            return self.node(Node::Unary('-', Box::new(inner)), below);
        }
        if self.eat_sym("+") {
            self.enter()?;
            let inner = self.unary()?;
            self.leave();
            return Ok(inner);
        }
        self.primary()
    }

    fn primary(&mut self) -> Result<Node, String> {
        let tok = self.peek().cloned().ok_or("expressão incompleta")?;
        self.pos += 1;
        // Folhas; os parênteses devolvem a profundidade do que está dentro.
        self.depth = 1;
        match tok {
            Tok::Num(n) => {
                if n.contains('.') {
                    Decimal::parse(&n).map(|d| Node::Const(Value::Decimal(d))).ok_or_else(|| format!("número inválido '{n}'"))
                } else {
                    n.parse::<i64>().map(|v| Node::Const(Value::Int(v))).map_err(|_| format!("número inválido '{n}'"))
                }
            }
            Tok::Str(s) => Ok(Node::Const(Value::String(s))),
            Tok::Sym("(") => {
                self.enter()?;
                let inner = self.or()?;
                self.expect(")")?;
                self.leave();
                Ok(inner)
            }
            Tok::Ident(name) => {
                match name.to_lowercase().as_str() {
                    "true" | "verdadeiro" => return Ok(Node::Const(Value::Bool(true))),
                    "false" | "falso" => return Ok(Node::Const(Value::Bool(false))),
                    "null" => return Ok(Node::Const(Value::Null)),
                    _ => {}
                }
                if self.eat_sym("(") {
                    self.enter()?;
                    let mut args = Vec::new();
                    let mut below = 0;
                    if !self.eat_sym(")") {
                        args.push(self.or()?);
                        below = self.depth;
                        while self.eat_sym(",") {
                            args.push(self.or()?);
                            below = below.max(self.depth);
                        }
                        self.expect(")")?;
                    }
                    self.leave();
                    return self.node(Node::Call(name.to_lowercase(), args), below);
                }
                Ok(Node::Field(name))
            }
            Tok::Sym(s) => Err(format!("'{s}' fora do lugar")),
        }
    }
}

// ---------------------------------------------------------------------------
// Avaliação

fn eval(node: &Node, get: &dyn Fn(&str) -> Option<Value>) -> Result<Value, String> {
    Ok(match node {
        Node::Const(v) => v.clone(),
        Node::Field(name) => get(name).ok_or_else(|| format!("campo '{name}' não existe"))?,
        Node::Unary(_, a) => match eval(a, get)? {
            Value::Null => Value::Null,
            Value::Int(i) => Value::Int(-i),
            Value::Float(f) => Value::Float(-f),
            Value::Decimal(d) => Value::Decimal(Decimal::new(-d.mantissa(), d.scale())),
            v => return Err(format!("não dá para negar {}", v.kind_name())),
        },
        Node::Not(a) => match truth(&eval(a, get)?) {
            Some(b) => Value::Bool(!b),
            None => Value::Null,
        },
        Node::And(a, b) => {
            let left = truth(&eval(a, get)?);
            if left == Some(false) {
                return Ok(Value::Bool(false));
            }
            match (left, truth(&eval(b, get)?)) {
                (_, Some(false)) => Value::Bool(false),
                (Some(true), Some(true)) => Value::Bool(true),
                _ => Value::Null,
            }
        }
        Node::Or(a, b) => {
            let left = truth(&eval(a, get)?);
            if left == Some(true) {
                return Ok(Value::Bool(true));
            }
            match (left, truth(&eval(b, get)?)) {
                (_, Some(true)) => Value::Bool(true),
                (Some(false), Some(false)) => Value::Bool(false),
                _ => Value::Null,
            }
        }
        Node::IsNull(a, not) => Value::Bool(eval(a, get)?.is_null() != *not),
        Node::Like(a, pattern, negated) => {
            let (v, p) = (eval(a, get)?, eval(pattern, get)?);
            if v.is_null() || p.is_null() {
                return Ok(Value::Null);
            }
            Value::Bool(like(&v.to_string().to_lowercase(), &p.to_string().to_lowercase()) != *negated)
        }
        Node::In(a, list, negated) => {
            let v = eval(a, get)?;
            if v.is_null() {
                return Ok(Value::Null);
            }
            let mut found = false;
            for item in list {
                if compare(&v, &eval(item, get)?) == Some(Ordering::Equal) {
                    found = true;
                    break;
                }
            }
            Value::Bool(found != *negated)
        }
        Node::Binary(op, a, b) => binary(*op, eval(a, get)?, eval(b, get)?)?,
        Node::Call(name, args) => call(name, args, get)?,
    })
}

fn truth(v: &Value) -> Option<bool> {
    match v {
        Value::Null => None,
        Value::Bool(b) => Some(*b),
        Value::Int(i) => Some(*i != 0),
        other => Some(!other.to_string().is_empty()),
    }
}

/// Compara dois valores (texto sem diferenciar maiúsculas; números de tipos diferentes
/// entre si; datas com texto ISO). `None` se não forem comparáveis ou um for `Null`.
pub fn compare(a: &Value, b: &Value) -> Option<Ordering> {
    use Value::*;
    match (a, b) {
        (Null, _) | (_, Null) => None,
        (String(x), String(y)) => Some(x.to_lowercase().cmp(&y.to_lowercase())),
        (Bool(x), Bool(y)) => Some(x.cmp(y)),
        (Date(x), Date(y)) => Some(x.cmp(y)),
        (Time(x), Time(y)) => Some(x.cmp(y)),
        (DateTime(x), DateTime(y)) => Some(x.cmp(y)),
        (Date(x), DateTime(y)) => Some(x.cmp(&y.date)),
        (DateTime(x), Date(y)) => Some(x.date.cmp(y)),
        (Timestamp(x), Timestamp(y)) => Some(x.cmp(y)),
        // Com uma data e hora sem fuso (ou uma data), compara no horário local.
        (Timestamp(x), DateTime(y)) => Some(x.to_local().cmp(y)),
        (DateTime(x), Timestamp(y)) => Some(x.cmp(&y.to_local())),
        (Timestamp(x), Date(y)) => Some(x.to_local().date.cmp(y)),
        (Date(x), Timestamp(y)) => Some(x.cmp(&y.to_local().date)),
        (Date(x), String(s)) => crate::value::Date::parse_iso(s).map(|y| x.cmp(&y)),
        (String(s), Date(y)) => crate::value::Date::parse_iso(s).map(|x| x.cmp(y)),
        (Decimal(x), Decimal(y)) => Some(x.cmp(y)),
        (Int(x), Int(y)) => Some(x.cmp(y)),
        _ => match (a.as_f64(), b.as_f64()) {
            (Some(x), Some(y)) => x.partial_cmp(&y),
            _ => Some(a.to_string().to_lowercase().cmp(&b.to_string().to_lowercase())),
        },
    }
}

/// Ordem para o índice: `Null` antes de tudo.
pub fn sort_order(a: &Value, b: &Value) -> Ordering {
    match (a.is_null(), b.is_null()) {
        (true, true) => Ordering::Equal,
        (true, false) => Ordering::Less,
        (false, true) => Ordering::Greater,
        _ => compare(a, b).unwrap_or(Ordering::Equal),
    }
}

fn binary(op: Op, a: Value, b: Value) -> Result<Value, String> {
    let cmp = |want: fn(Ordering) -> bool| Ok(compare(&a, &b).map_or(Value::Null, |o| Value::Bool(want(o))));
    match op {
        Op::Eq => cmp(|o| o == Ordering::Equal),
        Op::Ne => cmp(|o| o != Ordering::Equal),
        Op::Lt => cmp(|o| o == Ordering::Less),
        Op::Le => cmp(|o| o != Ordering::Greater),
        Op::Gt => cmp(|o| o == Ordering::Greater),
        Op::Ge => cmp(|o| o != Ordering::Less),
        _ if a.is_null() || b.is_null() => Ok(Value::Null),
        Op::Add if matches!(a, Value::String(_)) || matches!(b, Value::String(_)) => Ok(Value::String(format!("{a}{b}"))),
        Op::Add | Op::Sub if matches!(a, Value::Date(_)) => {
            let days = b.as_i64().ok_or("só dá para somar dias (inteiros) a uma data")?;
            let Value::Date(d) = a else { unreachable!() };
            Ok(Value::Date(add_days(d, if op == Op::Add { days } else { -days })))
        }
        _ => arithmetic(op, &a, &b),
    }
}

fn add_days(d: Date, days: i64) -> Date {
    Date::from_days(d.to_days() + days).unwrap_or(d)
}

fn op_symbol(op: Op) -> &'static str {
    match op {
        Op::Add => "+",
        Op::Sub => "-",
        Op::Mul => "*",
        Op::Div => "/",
        Op::Mod => "%",
        Op::Eq => "=",
        Op::Ne => "<>",
        Op::Lt => "<",
        Op::Le => "<=",
        Op::Gt => ">",
        Op::Ge => ">=",
    }
}

/// Contas: inteiros ficam inteiros (a divisão vira decimal), decimais ficam exatos (a divisão
/// também), e estouro é erro, nunca um valor errado.
fn arithmetic(op: Op, a: &Value, b: &Value) -> Result<Value, String> {
    let not_number = |v: &Value| format!("'{v}' não é número");
    let overflow = || format!("estouro: {a} {} {b} não cabe", op_symbol(op));
    if let (Value::Int(x), Value::Int(y)) = (a, b) {
        return match op {
            Op::Add => x.checked_add(*y).map(Value::Int).ok_or_else(overflow),
            Op::Sub => x.checked_sub(*y).map(Value::Int).ok_or_else(overflow),
            Op::Mul => x.checked_mul(*y).map(Value::Int).ok_or_else(overflow),
            Op::Mod if *y != 0 => Ok(Value::Int(x % y)),
            // Inteiros são exatos: a divisão também (7 / 2 = 3,5000), sem passar por f64.
            Op::Div if *y != 0 => {
                let (x, y) = (Decimal::from(*x), Decimal::from(*y));
                x.checked_div(&y, x.default_div_scale(&y), Rounding::HalfUp).map(Value::Decimal).ok_or_else(overflow)
            }
            _ => Ok(Value::Null), // divisão por zero
        };
    }
    let decimal = matches!(a, Value::Decimal(_)) || matches!(b, Value::Decimal(_));
    let floaty = matches!(a, Value::Float(_)) || matches!(b, Value::Float(_));
    if decimal && !floaty && op != Op::Mod {
        let (x, y) = (a.as_decimal().ok_or_else(|| not_number(a))?, b.as_decimal().ok_or_else(|| not_number(b))?);
        let result = match op {
            Op::Add => x.checked_add(&y),
            Op::Sub => x.checked_sub(&y),
            // O produto com as casas do maior mais 2 (2,5 × 1,333 = 3,33250), como antes.
            Op::Mul => x.checked_mul(&y).and_then(|p| p.round(p.scale().min(x.scale().max(y.scale()).max(2) + 2), Rounding::HalfUp)),
            // Exata, sem f64 (0,30 / 0,10 = 3): o campo da expressão depois arredonda para as
            // casas e o modo dele.
            Op::Div if y.mantissa() == 0 => return Ok(Value::Null),
            _ => x.checked_div(&y, x.default_div_scale(&y), Rounding::HalfUp),
        };
        return result.map(Value::Decimal).ok_or_else(overflow);
    }
    let (x, y) = (a.as_f64().ok_or_else(|| not_number(a))?, b.as_f64().ok_or_else(|| not_number(b))?);
    let r = match op {
        Op::Add => x + y,
        Op::Sub => x - y,
        Op::Mul => x * y,
        Op::Div if y != 0.0 => x / y,
        Op::Mod if y != 0.0 => x % y,
        _ => return Ok(Value::Null),
    };
    Ok(Value::Float(r))
}

/// `%` = qualquer sequência, `_` = um caractere.
fn like(text: &str, pattern: &str) -> bool {
    let t: Vec<char> = text.chars().collect();
    let p: Vec<char> = pattern.chars().collect();
    let (mut ti, mut pi) = (0, 0);
    let (mut star, mut mark) = (None, 0);
    while ti < t.len() {
        if pi < p.len() && (p[pi] == '_' || p[pi] == t[ti]) {
            ti += 1;
            pi += 1;
        } else if pi < p.len() && p[pi] == '%' {
            star = Some(pi);
            mark = ti;
            pi += 1;
        } else if let Some(s) = star {
            pi = s + 1;
            mark += 1;
            ti = mark;
        } else {
            return false;
        }
    }
    while pi < p.len() && p[pi] == '%' {
        pi += 1;
    }
    pi == p.len()
}

fn call(name: &str, args: &[Node], get: &dyn Fn(&str) -> Option<Value>) -> Result<Value, String> {
    let values: Vec<Value> = if name == "iif" { Vec::new() } else { args.iter().map(|a| eval(a, get)).collect::<Result<_, _>>()? };
    let arg = |i: usize| values.get(i).cloned().unwrap_or(Value::Null);
    let need = |n: usize| if args.len() < n { Err(format!("{name} precisa de {n} argumento(s)")) } else { Ok(()) };
    let text = |v: &Value| v.to_string();
    Ok(match name {
        "upper" => {
            need(1)?;
            if arg(0).is_null() { Value::Null } else { Value::String(text(&arg(0)).to_uppercase()) }
        }
        "lower" => {
            need(1)?;
            if arg(0).is_null() { Value::Null } else { Value::String(text(&arg(0)).to_lowercase()) }
        }
        // Sem acento e sem maiúscula (o `aylis_core::text::fold`): `fold(nome) like '%sao%'`
        // acha "São Paulo".
        "fold" => {
            need(1)?;
            if arg(0).is_null() { Value::Null } else { Value::String(aylis_core::text::fold(&text(&arg(0)))) }
        }
        "trim" => {
            need(1)?;
            if arg(0).is_null() { Value::Null } else { Value::String(text(&arg(0)).trim().to_string()) }
        }
        "len" | "length" => {
            need(1)?;
            Value::Int(if arg(0).is_null() { 0 } else { text(&arg(0)).chars().count() as i64 })
        }
        "substr" | "substring" => {
            need(2)?;
            let s = text(&arg(0));
            let start = arg(1).as_i64().unwrap_or(1).max(1) as usize - 1;
            let len = values.get(2).and_then(Value::as_i64).map_or(usize::MAX, |l| l.max(0) as usize);
            Value::String(s.chars().skip(start).take(len).collect())
        }
        "abs" => {
            need(1)?;
            match arg(0) {
                Value::Int(i) => Value::Int(i.abs()),
                Value::Decimal(d) => Value::Decimal(Decimal::new(d.mantissa().abs(), d.scale())),
                v => v.as_f64().map_or(Value::Null, |f| Value::Float(f.abs())),
            }
        }
        "round" => {
            need(1)?;
            let places = values.get(1).and_then(Value::as_i64).unwrap_or(0).clamp(0, 12) as u8;
            // round(x, casas, 'half_even'): o modo (half_up, o padrão; half_even, o bancário;
            // truncate; up).
            let mode = match values.get(2) {
                None | Some(Value::Null) => Rounding::HalfUp,
                Some(m) => {
                    let name = m.to_string();
                    Rounding::from_name(&name).ok_or_else(|| format!("arredondamento '{name}' desconhecido (half_up, half_even, truncate, up)"))?
                }
            };
            match arg(0) {
                Value::Null => Value::Null,
                Value::Decimal(d) => Value::Decimal(d.round(places, mode).ok_or_else(|| format!("estouro: round({d}, {places})"))?),
                Value::Int(i) => Value::Int(i),
                v => {
                    let f = v.as_f64().ok_or("round precisa de número")?;
                    let factor = 10f64.powi(places as i32);
                    let r = (f * factor).round() / factor;
                    if places == 0 { Value::Int(r as i64) } else { Value::Float(r) }
                }
            }
        }
        "coalesce" | "ifnull" => values.into_iter().find(|v| !v.is_null()).unwrap_or(Value::Null),
        "iif" | "if" => {
            need(3)?;
            if truth(&eval(&args[0], get)?) == Some(true) { eval(&args[1], get)? } else { eval(&args[2], get)? }
        }
        "year" | "month" | "day" => {
            need(1)?;
            match arg(0).as_date() {
                Some(d) => Value::Int(match name {
                    "year" => d.year() as i64,
                    "month" => d.month() as i64,
                    _ => d.day() as i64,
                }),
                None => Value::Null,
            }
        }
        // No horário local da tela (antes era o dia em UTC: em Brasília, depois das 21h, já
        // era amanhã).
        "today" => Value::Date(Date::today()),
        "now" => Value::DateTime(crate::value::DateTime::now()),
        other => return Err(format!("função desconhecida '{other}'")),
    })
}