limbo/testing
Jussi Saurio af039ffa6e Merge 'Initial support for aggregate functions in extensions' from Preston Thorpe
#708
This PR adds basic support for the following API for defining
Aggregates, and changes the existing API for scalars.
```rust
register_extension! {
    scalars: { Double },
    aggregates: { MedianState },
}

#[derive(ScalarDerive)]
struct Double;

impl Scalar for Double {
    fn name(&self) -> &'static str {
        "double"
    }
    fn call(&self, args: &[Value]) -> Value {
        if let Some(arg) = args.first() {
            match arg.value_type() {
                ValueType::Float => {
                    let val = arg.to_float().unwrap();
                    Value::from_float(val * 2.0)
                }
                ValueType::Integer => {
                    let val = arg.to_integer().unwrap();
                    Value::from_integer(val * 2)
                }
                _ => {
                    println!("arg: {:?}", arg);
                    Value::null()
                }
            }
        } else {
            Value::null()
        }
    }
}

#[derive(AggregateDerive)]
struct MedianState;

impl AggFunc for MedianState {
    type State = Vec<f64>;

    fn name(&self) -> &'static str {
        "median"
    }
    fn args(&self) -> i32 { 1 }

    fn step(state: &mut Self::State, args: &[Value]) {
        if let Some(val) = args.first().and_then(Value::to_float) {
            state.push(val);
        }
    }

    fn finalize(state: Self::State) -> Value {
        if state.is_empty() {
            return Value::null();
        }
        let mut sorted = state;
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
        let len = sorted.len();
        if len % 2 == 1 {
            Value::from_float(sorted[len / 2])
        } else {
            let mid1 = sorted[len / 2 - 1];
            let mid2 = sorted[len / 2];
            Value::from_float((mid1 + mid2) / 2.0)
        }
    }
}

```
I know it's a bit more verbose than the previous version, but I think in
the long run this will be more robust, and it matches the aggregate API
of implementing a trait on a struct that you derive the relevant trait
on.
Also this allows for better registration of functions, I think passing
in the struct identifiers just feels much better than the `"func_name"
=> function_ptr`

Closes #721
2025-01-18 11:07:06 +02:00
..
test_files Fix import csv failing when single quote is in string 2025-01-11 13:48:21 +05:30
wal testing: Add test database + WAL file 2024-08-03 12:16:34 +03:00
agg-functions.test support TRUE and FALSE in predicates 2024-12-13 22:58:29 +02:00
all.test feat: subqueries in FROM clause 2024-12-31 14:18:29 +02:00
cmdlineshell.test refactor(testing): move .table tests to shelltests.py 2024-12-18 09:10:37 +02:00
coalesce.test testing: coalesce.test 2024-07-16 07:20:35 +03:00
extensions.py Complete percentile extension, enable col+delimeter args 2025-01-17 21:15:09 -05:00
gen-database.py testing: update testing with products table and cross join test 2024-07-09 18:08:16 +02:00
glob.test Fix glob 2024-12-30 17:02:31 +05:30
groupby.test Dont assume GROUP BY has aggregates 2024-12-16 00:30:38 +02:00
insert.test Align MustBeInt logic with sqlite 2025-01-16 00:09:45 +05:30
join.test chore: rollback using rowid(sqlite3 unsupported) 2025-01-14 22:56:49 +08:00
json.test Merge branch 'main' into json-error-position 2025-01-13 18:21:37 +02:00
like.test Support like function with escape 2024-12-28 13:55:12 +05:30
math.test Implement ShiftRight 2025-01-15 21:21:51 +05:30
orderby.test test ordering by aggregate not mentioned in select 2024-11-26 17:31:51 +02:00
pragma.test pragma: inital approach to handle pragma statements 2024-12-18 17:26:16 +01:00
scalar-functions-datetime.test Julianday function and additional tests/comments 2025-01-04 10:42:34 -05:00
scalar-functions.test Fix sqlite_version() out of bound 2024-12-27 11:39:33 -03:00
select.test chore: rollback using rowid(sqlite3 unsupported) 2025-01-14 22:56:49 +08:00
shelltests.py Correct CLI comment handling to mimic sqlite behavior 2025-01-17 13:59:34 -05:00
subquery.test feat: subqueries in FROM clause 2024-12-31 14:18:29 +02:00
tester.tcl Add test function with regex 2024-12-27 11:39:02 -03:00
testing.db index scan wip foo doesnt work yet 2024-10-05 18:25:04 +03:00
testing_norowidalias.db Run all tcl tests on 2 separate dbs (1. with rowid aliases 2. without rowid aliases) 2024-12-14 17:13:45 +02:00
where.test Expr: fix recursive binary operation logic 2025-01-15 14:12:08 +02:00