mirror of
https://github.com/aljazceru/turso.git
synced 2025-12-25 12:04:21 +01:00
Merge 'Unify resolution of aggregate functions' from Piotr Rżysko
This PR unifies the logic for resolving aggregate functions. Previously,
bare aggregates (e.g. `SELECT max(a) FROM t1`) and aggregates wrapped in
expressions (e.g. `SELECT max(a) + 1 FROM t1`) were handled differently,
which led to duplicated code. Now both cases are resolved consistently.
The added benchmark shows a small improvement:
```
Prepare `SELECT first_name, last_name, state, city, age + 10, LENGTH(email), UPPER(first_name), LOWE...
time: [59.791 µs 59.898 µs 60.006 µs]
change: [-7.7090% -7.2760% -6.8242%] (p = 0.00 < 0.05)
Performance has improved.
Found 10 outliers among 100 measurements (10.00%)
8 (8.00%) high mild
2 (2.00%) high severe
```
For an existing benchmark, no change:
```
Prepare `SELECT first_name, count(1) FROM users GROUP BY first_name HAVING count(1) > 1 ORDER BY cou...
time: [11.895 µs 11.913 µs 11.931 µs]
change: [-0.2545% +0.2426% +0.6960%] (p = 0.34 > 0.05)
No change in performance detected.
Found 8 outliers among 100 measurements (8.00%)
1 (1.00%) low severe
2 (2.00%) high mild
5 (5.00%) high severe
```
Reviewed-by: Jussi Saurio <jussi.saurio@gmail.com>
Reviewed-by: Preston Thorpe <preston@turso.tech>
Closes #2884
This commit is contained in:
@@ -175,3 +175,27 @@ do_execsql_test select-json-group-object-no-sorting-required {
|
||||
3|{"3437":"Amanda"}
|
||||
5|{"2378":"Amy","3227":"Amy","5605":"Amanda"}
|
||||
7|{"2454":"Amber"}}
|
||||
|
||||
do_execsql_test_error_content select-max-star {
|
||||
SELECT max(*) FROM users;
|
||||
} {"wrong number of arguments to function"}
|
||||
|
||||
do_execsql_test_error_content select-max-star-in-expression {
|
||||
SELECT CASE WHEN max(*) > 0 THEN 1 ELSE 0 END FROM users;
|
||||
} {"wrong number of arguments to function"}
|
||||
|
||||
do_execsql_test_error select-scalar-func-star {
|
||||
SELECT abs(*) FROM users;
|
||||
} {.*(Invalid aggregate function|wrong number of arguments to function).*}
|
||||
|
||||
do_execsql_test_error select-scalar-func-star-in-expression {
|
||||
SELECT CASE WHEN abs(*) > 0 THEN 1 ELSE 0 END FROM users;
|
||||
} {.*(Invalid aggregate function|wrong number of arguments to function).*}
|
||||
|
||||
do_execsql_test_error_content select-nested-agg-func {
|
||||
SELECT max(abs(sum(age))), sum(age) FROM users;
|
||||
} {"misuse of aggregate function"}
|
||||
|
||||
do_execsql_test_error_content select-nested-agg-func-in-expression {
|
||||
SELECT CASE WHEN max(abs(sum(age))) > 0 THEN 1 ELSE 0 END, sum(age) FROM users;
|
||||
} {"misuse of aggregate function"}
|
||||
|
||||
@@ -153,6 +153,11 @@ def test_aggregates():
|
||||
validate_median,
|
||||
"median agg function works",
|
||||
)
|
||||
limbo.run_test_fn(
|
||||
"select CASE WHEN median(value) > 0 THEN median(value) ELSE 0 END from numbers;",
|
||||
validate_median,
|
||||
"median agg function wrapped in expression works",
|
||||
)
|
||||
limbo.execute_dot("INSERT INTO numbers (value) VALUES (8.0);\n")
|
||||
limbo.run_test_fn(
|
||||
"select median(value) from numbers;",
|
||||
@@ -184,6 +189,11 @@ def test_grouped_aggregates():
|
||||
lambda res: "2.0\n5.5" == res,
|
||||
"median aggregate function works",
|
||||
)
|
||||
limbo.run_test_fn(
|
||||
"select CASE WHEN median(value) > 0 THEN median(value) ELSE 0 END from numbers GROUP BY category;",
|
||||
lambda res: "2.0\n5.5" == res,
|
||||
"median aggregate function wrapped in expression works",
|
||||
)
|
||||
limbo.run_test_fn(
|
||||
"SELECT percentile(value, percent) FROM test GROUP BY category;",
|
||||
lambda res: "12.5\n30.0\n45.0\n70.0" == res,
|
||||
|
||||
Reference in New Issue
Block a user