Closed vandenoever closed 8 years ago
This looks out of esqueleto's scope. While we strive to make errors pop at compile time, these nuances are too difficult to bother.
If you think that the generated SQL is wrong, please reopen this issue and write which (valid) SQL query should have been output.
The query that I am aiming for is this one:
SELECT sub.software, AVG(sub.score) FROM (
SELECT test_result.test, test_result.software, ((COUNT(CASE WHEN (test_result.pass = 1) THEN 1 ELSE null END) * 1.0) / COUNT(*)) as score
FROM test_result
GROUP BY test_result.test, test_result.software
) as sub
GROUP BY sub.test
Perhaps I should move the where_
statements from scoreQuery
to getSoftwareOSScores
to achieve that.
Hmmm, I see. Esqueleto currently doesn't support FROM queries like that, it's very difficult to support them on our current design.
I can imagine. You need to generate names for the subqueries to do that. Good to know the current limitation.
It's actually more due to the type system. Take a look at how from
is implemented. I guess it is possible to support these queries, but there has never been enough pressure to actually do the very hard work of bolting them in.
Ah, is it because you now use types defined by Persistent and subqueries would require creating types for the new 'tables' on the fly? So perhaps defining table views would be easier.
So, it they are not possible in Esqueleto nowadays, what would be a good workaround?
Esqueleto can give invalid sql at runtime by combining a subquery with an aggregate function. The resulting SQL contains for example SELECT AVG((COUNT(...)) which gives the error "misuse of aggregate function COUNT())"
uncaught exception: SqliteException (SQLite3 returned ErrorError while attempting to perform prepare "SELECT \"software_o_s\".\"id\", AVG((COUNT(CASE WHEN (\"test_result\".\"pass\" = ?) THEN ? ELSE ? END) / ?) * COUNT(*))\nFROM \"test_set_member\", \"software_o_s\", \"test_result\"\nWHERE ((\"test_result\".\"test\" = \"test_set_member\".\"test\") AND (\"test_result\".\"software\" = \"software_o_s\".\"id\")) AND (\"test_set_member\".\"set\" = ?)\nGROUP BY \"test_result\".\"test\", \"test_result\".\"software\", \"software_o_s\".\"id\"\n": misuse of aggregate function COUNT())