Merge 'Handle count(*)' from Vegard Stikbakke

This PR adds support for `count(*)`. I did not find any other functions than `count` which expect a `*` argument yet, but I'm sure there are some?

Surprisingly (to me) I did not need to make any changes to `translate_expr`, only to `analyze_expr`.

```
limbo> SELECT count(id) FROM users;
2
limbo> SELECT count(*) FROM users;
2
limbo> SELECT count(*) FROM users where id = 1;
1
limbo> SELECT count(id) FROM users where id = 1;
1
```

Other aggregation functions such as sum fail, since they expect a specific column:
```
limbo> select sum(*) from users;
Parse error: sum bad number of arguments
```

Closes #285
This commit is contained in:
Pekka Enberg
2024-08-11 16:57:18 +03:00
2 changed files with 21 additions and 1 deletions

View File

@@ -1,3 +1,5 @@
use core::panic;
use crate::{function::JsonFunc, Result};
use sqlite3_parser::ast::{self, Expr, UnaryOperator};
@@ -582,7 +584,21 @@ pub fn analyze_expr<'a>(expr: &'a Expr, column_info_out: &mut ColumnInfo<'a>) {
column_info_out.args = args;
}
}
ast::Expr::FunctionCallStar { .. } => todo!(),
ast::Expr::FunctionCallStar {
name,
filter_over: _,
} => {
let func_type =
match Func::resolve_function(normalize_ident(name.0.as_str()).as_str(), 1) {
Ok(func) => Some(func),
Err(_) => None,
};
if func_type.is_none() {
panic!("Function not found");
} else {
column_info_out.func = func_type;
}
}
_ => {}
}
}

View File

@@ -35,6 +35,10 @@ do_execsql_test select-count {
SELECT count(id) FROM users;
} {10000}
do_execsql_test select-count {
SELECT count(*) FROM users;
} {10000}
do_execsql_test select-max {
SELECT max(age) FROM users;
} {100}