oxirs-arq 0.4.1

Jena-style SPARQL algebra with extension points and query optimization
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
//! SPARQL Built-in Date/Time, Hash, and Aggregate Functions
//!
//! Date/time extraction (NOW, YEAR, MONTH, DAY, HOURS, MINUTES, SECONDS, TIMEZONE, TZ),
//! cryptographic hash functions (MD5, SHA1, SHA256, SHA384, SHA512),
//! and SPARQL aggregate functions (COUNT, SUM, AVG, MIN, MAX, GROUP_CONCAT, SAMPLE).

use crate::extensions::{
    AggregateState, CustomAggregate, CustomFunction, ExecutionContext, Value, ValueType,
};
use anyhow::{bail, Result};
use chrono::Datelike;

// ─── Date/Time Functions ────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub(crate) struct NowFunction;

impl CustomFunction for NowFunction {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#current-dateTime"
    }
    fn arity(&self) -> Option<usize> {
        Some(0)
    }
    fn parameter_types(&self) -> Vec<ValueType> {
        vec![]
    }
    fn return_type(&self) -> ValueType {
        ValueType::DateTime
    }
    fn documentation(&self) -> &str {
        "Returns the current date and time"
    }

    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }

    fn execute(&self, args: &[Value], context: &ExecutionContext) -> Result<Value> {
        if !args.is_empty() {
            bail!("now() takes no arguments");
        }

        Ok(Value::DateTime(context.query_time))
    }
}

#[derive(Debug, Clone)]
pub(crate) struct YearFunction;

impl CustomFunction for YearFunction {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#year-from-dateTime"
    }
    fn arity(&self) -> Option<usize> {
        Some(1)
    }
    fn parameter_types(&self) -> Vec<ValueType> {
        vec![ValueType::DateTime]
    }
    fn return_type(&self) -> ValueType {
        ValueType::Integer
    }
    fn documentation(&self) -> &str {
        "Extracts the year from a dateTime"
    }

    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }

    fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
        if args.len() != 1 {
            bail!("year() requires exactly 1 argument");
        }

        match &args[0] {
            Value::DateTime(dt) => Ok(Value::Integer(dt.year() as i64)),
            _ => bail!("year() requires a dateTime argument"),
        }
    }
}

// ─── Date/time component functions ──────────────────────────────────────────

use chrono::Timelike;

/// Generate a `CustomFunction` that extracts an integer component from a
/// `Value::DateTime` (e.g. month, hour). `$extract` is a closure taking the
/// `DateTime<Utc>` and returning the `i64` component value.
macro_rules! datetime_int_function {
    ($name:ident, $iri:expr_2021, $doc:expr_2021, $extract:expr_2021) => {
        #[derive(Debug, Clone)]
        pub(crate) struct $name;

        impl CustomFunction for $name {
            fn name(&self) -> &str {
                $iri
            }
            fn arity(&self) -> Option<usize> {
                Some(1)
            }
            fn parameter_types(&self) -> Vec<ValueType> {
                vec![ValueType::DateTime]
            }
            fn return_type(&self) -> ValueType {
                ValueType::Integer
            }
            fn documentation(&self) -> &str {
                $doc
            }
            fn clone_function(&self) -> Box<dyn CustomFunction> {
                Box::new(self.clone())
            }

            fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
                if args.len() != 1 {
                    bail!("{} requires exactly 1 argument", self.name());
                }
                match &args[0] {
                    Value::DateTime(dt) => {
                        let extract: fn(&chrono::DateTime<chrono::Utc>) -> i64 = $extract;
                        Ok(Value::Integer(extract(dt)))
                    }
                    _ => bail!("{} requires a dateTime argument", self.name()),
                }
            }
        }
    };
}

datetime_int_function!(
    MonthFunction,
    "http://www.w3.org/2005/xpath-functions#month-from-dateTime",
    "Extracts month from dateTime",
    |dt| dt.month() as i64
);
datetime_int_function!(
    DayFunction,
    "http://www.w3.org/2005/xpath-functions#day-from-dateTime",
    "Extracts day from dateTime",
    |dt| dt.day() as i64
);
datetime_int_function!(
    HoursFunction,
    "http://www.w3.org/2005/xpath-functions#hours-from-dateTime",
    "Extracts hours from dateTime",
    |dt| dt.hour() as i64
);
datetime_int_function!(
    MinutesFunction,
    "http://www.w3.org/2005/xpath-functions#minutes-from-dateTime",
    "Extracts minutes from dateTime",
    |dt| dt.minute() as i64
);
datetime_int_function!(
    SecondsFunction,
    "http://www.w3.org/2005/xpath-functions#seconds-from-dateTime",
    "Extracts seconds from dateTime",
    |dt| dt.second() as i64
);

/// `TIMEZONE(dt)` — the timezone of a dateTime as an xsd:dayTimeDuration.
/// `Value::DateTime` is normalized to UTC, so the timezone is always `PT0S`.
#[derive(Debug, Clone)]
pub(crate) struct TimezoneFunction;

impl CustomFunction for TimezoneFunction {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#timezone-from-dateTime"
    }
    fn arity(&self) -> Option<usize> {
        Some(1)
    }
    fn parameter_types(&self) -> Vec<ValueType> {
        vec![ValueType::DateTime]
    }
    fn return_type(&self) -> ValueType {
        ValueType::Literal
    }
    fn documentation(&self) -> &str {
        "Extracts timezone from dateTime"
    }
    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }
    fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
        if args.len() != 1 {
            bail!("timezone() requires exactly 1 argument");
        }
        match &args[0] {
            Value::DateTime(_) => Ok(Value::Literal {
                value: "PT0S".to_string(),
                language: None,
                datatype: Some("http://www.w3.org/2001/XMLSchema#dayTimeDuration".to_string()),
            }),
            _ => bail!("timezone() requires a dateTime argument"),
        }
    }
}

/// `TZ(dt)` — the timezone of a dateTime as a simple string. `Value::DateTime`
/// is normalized to UTC, so the result is always `"Z"`.
#[derive(Debug, Clone)]
pub(crate) struct TzFunction;

impl CustomFunction for TzFunction {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#tz"
    }
    fn arity(&self) -> Option<usize> {
        Some(1)
    }
    fn parameter_types(&self) -> Vec<ValueType> {
        vec![ValueType::DateTime]
    }
    fn return_type(&self) -> ValueType {
        ValueType::String
    }
    fn documentation(&self) -> &str {
        "Returns timezone string"
    }
    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }
    fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
        if args.len() != 1 {
            bail!("tz() requires exactly 1 argument");
        }
        match &args[0] {
            Value::DateTime(_) => Ok(Value::String("Z".to_string())),
            _ => bail!("tz() requires a dateTime argument"),
        }
    }
}

// ─── Hash Functions ──────────────────────────────────────────────────────────

/// Extract the string bytes to hash from a hash function's single argument.
fn hash_input_value(args: &[Value], fn_name: &str) -> Result<String> {
    if args.len() != 1 {
        bail!("{fn_name}() requires exactly 1 argument");
    }
    match &args[0] {
        Value::String(s) => Ok(s.clone()),
        Value::Literal { value, .. } => Ok(value.clone()),
        _ => bail!("{fn_name}() requires a string argument"),
    }
}

/// Generate a `CustomFunction` computing a lowercase-hex digest via a
/// RustCrypto `Digest` implementation.
macro_rules! digest_function {
    ($name:ident, $iri:expr_2021, $doc:expr_2021, $fn_name:expr_2021, $hasher:ty) => {
        #[derive(Debug, Clone)]
        pub(crate) struct $name;

        impl CustomFunction for $name {
            fn name(&self) -> &str {
                $iri
            }
            fn arity(&self) -> Option<usize> {
                Some(1)
            }
            fn parameter_types(&self) -> Vec<ValueType> {
                vec![ValueType::String]
            }
            fn return_type(&self) -> ValueType {
                ValueType::String
            }
            fn documentation(&self) -> &str {
                $doc
            }
            fn clone_function(&self) -> Box<dyn CustomFunction> {
                Box::new(self.clone())
            }
            fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
                use sha2::Digest;
                let input = hash_input_value(args, $fn_name)?;
                let mut hasher = <$hasher>::new();
                hasher.update(input.as_bytes());
                Ok(Value::String(hex::encode(hasher.finalize())))
            }
        }
    };
}

#[derive(Debug, Clone)]
pub(crate) struct Md5Function;

impl CustomFunction for Md5Function {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#md5"
    }
    fn arity(&self) -> Option<usize> {
        Some(1)
    }
    fn parameter_types(&self) -> Vec<ValueType> {
        vec![ValueType::String]
    }
    fn return_type(&self) -> ValueType {
        ValueType::String
    }
    fn documentation(&self) -> &str {
        "Computes MD5 hash of a string"
    }

    fn clone_function(&self) -> Box<dyn CustomFunction> {
        Box::new(self.clone())
    }

    fn execute(&self, args: &[Value], _context: &ExecutionContext) -> Result<Value> {
        if args.len() != 1 {
            bail!("md5() requires exactly 1 argument");
        }

        let s = hash_input_value(args, "md5")?;
        Ok(Value::String(format!("{:x}", md5::compute(s.as_bytes()))))
    }
}

digest_function!(
    Sha1Function,
    "http://www.w3.org/2005/xpath-functions#sha1",
    "Computes SHA1 hash",
    "sha1",
    sha1::Sha1
);
digest_function!(
    Sha256Function,
    "http://www.w3.org/2005/xpath-functions#sha256",
    "Computes SHA256 hash",
    "sha256",
    sha2::Sha256
);
digest_function!(
    Sha384Function,
    "http://www.w3.org/2005/xpath-functions#sha384",
    "Computes SHA384 hash",
    "sha384",
    sha2::Sha384
);
digest_function!(
    Sha512Function,
    "http://www.w3.org/2005/xpath-functions#sha512",
    "Computes SHA512 hash",
    "sha512",
    sha2::Sha512
);

// ─── Aggregate Functions ─────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub(crate) struct CountAggregate;

impl CustomAggregate for CountAggregate {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#count"
    }
    fn documentation(&self) -> &str {
        "Counts the number of values"
    }

    fn init(&self) -> Box<dyn AggregateState> {
        Box::new(CountState { count: 0 })
    }
}

#[derive(Debug, Clone)]
struct CountState {
    count: i64,
}

impl AggregateState for CountState {
    fn add(&mut self, _value: &Value) -> Result<()> {
        self.count += 1;
        Ok(())
    }

    fn result(&self) -> Result<Value> {
        Ok(Value::Integer(self.count))
    }

    fn reset(&mut self) {
        self.count = 0;
    }

    fn clone_state(&self) -> Box<dyn AggregateState> {
        Box::new(self.clone())
    }
}

#[derive(Debug, Clone)]
pub(crate) struct SumAggregate;

impl CustomAggregate for SumAggregate {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#sum"
    }
    fn documentation(&self) -> &str {
        "Sums numeric values"
    }

    fn init(&self) -> Box<dyn AggregateState> {
        Box::new(SumState::default())
    }
}

/// Accumulator for SPARQL `SUM`. While every operand seen so far is an
/// `xsd:integer`, the sum is tracked in an exact `i64` and the result keeps
/// `xsd:integer` typing with full precision (no `f64` rounding beyond 2^53).
/// The first non-integer operand promotes the running total to `f64`.
#[derive(Debug, Clone, Default)]
struct SumState {
    int_sum: i64,
    float_sum: f64,
    /// True once a non-integer operand has been seen (or an integer overflowed).
    promoted: bool,
    count: usize,
}

impl AggregateState for SumState {
    fn add(&mut self, value: &Value) -> Result<()> {
        match value {
            Value::Integer(i) => {
                if self.promoted {
                    self.float_sum += *i as f64;
                } else if let Some(next) = self.int_sum.checked_add(*i) {
                    self.int_sum = next;
                } else {
                    // Integer overflow: fall back to f64 accumulation.
                    self.float_sum = self.int_sum as f64 + *i as f64;
                    self.promoted = true;
                }
                self.count += 1;
            }
            Value::Float(f) => {
                if !self.promoted {
                    self.float_sum = self.int_sum as f64;
                    self.promoted = true;
                }
                self.float_sum += f;
                self.count += 1;
            }
            _ => bail!("sum() can only aggregate numeric values"),
        }
        Ok(())
    }

    fn result(&self) -> Result<Value> {
        if self.count == 0 {
            // SPARQL SUM of an empty group is 0 (xsd:integer).
            Ok(Value::Integer(0))
        } else if self.promoted {
            Ok(Value::Float(self.float_sum))
        } else {
            Ok(Value::Integer(self.int_sum))
        }
    }

    fn reset(&mut self) {
        *self = SumState::default();
    }

    fn clone_state(&self) -> Box<dyn AggregateState> {
        Box::new(self.clone())
    }
}

// ─── AVG ─────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub(crate) struct AvgAggregate;

impl CustomAggregate for AvgAggregate {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#avg"
    }
    fn documentation(&self) -> &str {
        "Computes average of numeric values"
    }
    fn init(&self) -> Box<dyn AggregateState> {
        Box::new(AvgState::default())
    }
}

#[derive(Debug, Clone, Default)]
struct AvgState {
    sum: f64,
    count: usize,
}

impl AggregateState for AvgState {
    fn add(&mut self, value: &Value) -> Result<()> {
        match value {
            Value::Integer(i) => {
                self.sum += *i as f64;
                self.count += 1;
            }
            Value::Float(f) => {
                self.sum += f;
                self.count += 1;
            }
            _ => bail!("avg() can only aggregate numeric values"),
        }
        Ok(())
    }

    fn result(&self) -> Result<Value> {
        if self.count == 0 {
            // AVG of an empty group is 0 (SPARQL 1.1).
            Ok(Value::Integer(0))
        } else {
            Ok(Value::Float(self.sum / self.count as f64))
        }
    }

    fn reset(&mut self) {
        *self = AvgState::default();
    }

    fn clone_state(&self) -> Box<dyn AggregateState> {
        Box::new(self.clone())
    }
}

// ─── MIN / MAX ───────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub(crate) struct MinAggregate;

impl CustomAggregate for MinAggregate {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#min"
    }
    fn documentation(&self) -> &str {
        "Finds minimum value"
    }
    fn init(&self) -> Box<dyn AggregateState> {
        Box::new(ExtremumState {
            keep_max: false,
            current: None,
        })
    }
}

#[derive(Debug, Clone)]
pub(crate) struct MaxAggregate;

impl CustomAggregate for MaxAggregate {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#max"
    }
    fn documentation(&self) -> &str {
        "Finds maximum value"
    }
    fn init(&self) -> Box<dyn AggregateState> {
        Box::new(ExtremumState {
            keep_max: true,
            current: None,
        })
    }
}

/// Shared MIN/MAX accumulator: keeps the extreme value seen using the total
/// ordering defined by `Value::partial_cmp`.
#[derive(Debug, Clone)]
struct ExtremumState {
    keep_max: bool,
    current: Option<Value>,
}

impl AggregateState for ExtremumState {
    fn add(&mut self, value: &Value) -> Result<()> {
        if matches!(value, Value::Null) {
            return Ok(());
        }
        match &self.current {
            None => self.current = Some(value.clone()),
            Some(existing) => {
                if let Some(ordering) = value.partial_cmp(existing) {
                    let replace = if self.keep_max {
                        ordering == std::cmp::Ordering::Greater
                    } else {
                        ordering == std::cmp::Ordering::Less
                    };
                    if replace {
                        self.current = Some(value.clone());
                    }
                }
            }
        }
        Ok(())
    }

    fn result(&self) -> Result<Value> {
        Ok(self.current.clone().unwrap_or(Value::Null))
    }

    fn reset(&mut self) {
        self.current = None;
    }

    fn clone_state(&self) -> Box<dyn AggregateState> {
        Box::new(self.clone())
    }
}

// ─── GROUP_CONCAT ────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub(crate) struct GroupConcatAggregate;

impl CustomAggregate for GroupConcatAggregate {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#group-concat"
    }
    fn documentation(&self) -> &str {
        "Concatenates group values"
    }
    fn init(&self) -> Box<dyn AggregateState> {
        Box::new(GroupConcatState::default())
    }
}

/// GROUP_CONCAT accumulator: joins the string form of each value with a single
/// space separator (the SPARQL default when no `SEPARATOR` is given).
#[derive(Debug, Clone, Default)]
struct GroupConcatState {
    parts: Vec<String>,
}

impl GroupConcatState {
    fn value_to_string(value: &Value) -> String {
        match value {
            Value::String(s) => s.clone(),
            Value::Literal { value, .. } => value.clone(),
            Value::Iri(iri) => iri.clone(),
            Value::BlankNode(id) => id.clone(),
            Value::Integer(i) => i.to_string(),
            Value::Float(f) => f.to_string(),
            Value::Boolean(b) => b.to_string(),
            Value::DateTime(dt) => dt.to_rfc3339(),
            other => format!("{other:?}"),
        }
    }
}

impl AggregateState for GroupConcatState {
    fn add(&mut self, value: &Value) -> Result<()> {
        if !matches!(value, Value::Null) {
            self.parts.push(Self::value_to_string(value));
        }
        Ok(())
    }

    fn result(&self) -> Result<Value> {
        Ok(Value::String(self.parts.join(" ")))
    }

    fn reset(&mut self) {
        self.parts.clear();
    }

    fn clone_state(&self) -> Box<dyn AggregateState> {
        Box::new(self.clone())
    }
}

// ─── SAMPLE ──────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub(crate) struct SampleAggregate;

impl CustomAggregate for SampleAggregate {
    fn name(&self) -> &str {
        "http://www.w3.org/2005/xpath-functions#sample"
    }
    fn documentation(&self) -> &str {
        "Returns sample value from group"
    }
    fn init(&self) -> Box<dyn AggregateState> {
        Box::new(SampleState { sample: None })
    }
}

/// SAMPLE accumulator: keeps the first non-null value seen (any deterministic
/// choice is valid per SPARQL 1.1).
#[derive(Debug, Clone)]
struct SampleState {
    sample: Option<Value>,
}

impl AggregateState for SampleState {
    fn add(&mut self, value: &Value) -> Result<()> {
        if self.sample.is_none() && !matches!(value, Value::Null) {
            self.sample = Some(value.clone());
        }
        Ok(())
    }

    fn result(&self) -> Result<Value> {
        Ok(self.sample.clone().unwrap_or(Value::Null))
    }

    fn reset(&mut self) {
        self.sample = None;
    }

    fn clone_state(&self) -> Box<dyn AggregateState> {
        Box::new(self.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::extensions::{CustomAggregate, CustomFunction, ExecutionContext, Value};

    /// SUM over integers must keep xsd:integer typing and full i64 precision.
    #[test]
    fn regression_sum_integer_typing() {
        let agg = SumAggregate;
        let mut state = agg.init();
        for v in [1i64, 2, 3] {
            state.add(&Value::Integer(v)).unwrap();
        }
        match state.result().unwrap() {
            Value::Integer(6) => {}
            other => panic!("SUM of integers must be Integer(6), got {other:?}"),
        }

        // Large sums beyond 2^53 keep exact precision as i64.
        let mut big = agg.init();
        big.add(&Value::Integer(9_007_199_254_740_993)).unwrap(); // 2^53 + 1
        big.add(&Value::Integer(2)).unwrap();
        match big.result().unwrap() {
            Value::Integer(v) => assert_eq!(v, 9_007_199_254_740_995),
            other => panic!("expected exact integer, got {other:?}"),
        }

        // A float operand promotes the result to Float.
        let mut mixed = agg.init();
        mixed.add(&Value::Integer(1)).unwrap();
        mixed.add(&Value::Float(0.5)).unwrap();
        match mixed.result().unwrap() {
            Value::Float(f) => assert!((f - 1.5).abs() < 1e-9),
            other => panic!("expected Float, got {other:?}"),
        }
    }

    /// AVG/MIN/MAX/SAMPLE/GROUP_CONCAT must compute real results, not Null.
    #[test]
    fn regression_aggregates_real() {
        let ctx = ExecutionContext::default();
        let _ = &ctx;

        let mut avg = AvgAggregate.init();
        for v in [2i64, 4] {
            avg.add(&Value::Integer(v)).unwrap();
        }
        match avg.result().unwrap() {
            Value::Float(f) => assert!((f - 3.0).abs() < 1e-9),
            other => panic!("AVG must be Float(3.0), got {other:?}"),
        }

        let mut min = MinAggregate.init();
        let mut max = MaxAggregate.init();
        for v in [5i64, 1, 3] {
            min.add(&Value::Integer(v)).unwrap();
            max.add(&Value::Integer(v)).unwrap();
        }
        assert_eq!(min.result().unwrap(), Value::Integer(1));
        assert_eq!(max.result().unwrap(), Value::Integer(5));

        let mut gc = GroupConcatState::default();
        gc.add(&Value::String("a".into())).unwrap();
        gc.add(&Value::String("b".into())).unwrap();
        assert_eq!(gc.result().unwrap(), Value::String("a b".into()));

        let mut sample = SampleAggregate.init();
        sample.add(&Value::Integer(7)).unwrap();
        sample.add(&Value::Integer(8)).unwrap();
        assert_eq!(sample.result().unwrap(), Value::Integer(7));
    }

    /// Registered SHA/MD5 functions must compute real digests.
    #[test]
    fn regression_registered_hash_functions_real() {
        let ctx = ExecutionContext::default();
        let sha256 = Sha256Function;
        let out = sha256
            .execute(&[Value::String("abc".into())], &ctx)
            .unwrap();
        assert_eq!(
            out,
            Value::String(
                "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad".into()
            )
        );

        let md5 = Md5Function;
        let out = md5.execute(&[Value::String("abc".into())], &ctx).unwrap();
        assert_eq!(
            out,
            Value::String("900150983cd24fb0d6963f7d28e17f72".into())
        );
    }
}