Hello,
I encountered a few strange behaviors using JPQL and SQL I would like to show, asking if there are bugs or if I am doing something wrong. I transferred all examples to the sec_userRole entity so you can try it in any project. For the ease of use I was executing these examples in the SQL-/JPQL-console from the Admin Tools Plugin.
Basically I want to see how many different versions of roles are there and how many users have those roles. I only want to count users that don’t have a deleted role. Usually I would write it like this (sorry about the blockquote, the preformatting doesn’t seem to work):
SELECT
s.version,
COUNT(DISTINCT CASE WHEN s.deletedBy IS NULL THEN s.user ELSE null END)
FROM sec$UserRole s
GROUP BY s.version
This gives me a syntax error which may be caused by the fact, that the generated SQL ommits s.user from the CASE-statement:
SELECT t0.VERSION,
COUNT(DISTINCT(CASE WHEN (t0.DELETED_BY IS NULL) THEN ELSE ? END))
FROM SEC_USER_ROLE t0, SEC_USER t1
WHERE ((t0.DELETE_TS IS NULL) AND ((t1.ID = t0.USER_ID) AND (t1.DELETE_TS IS NULL))) GROUP BY t0.VERSION
When I replace s.user with a literal e.g. 1 and the null with 2 then I get a (useless) result so the error may have to do with counting entities and null together(?).
I then tried to write the statement in a different way and had success with is:
SELECT
s.version,
(SELECT COUNT(DISTINCT s2.user) FROM sec$UserRole s2 WHERE s2.version=s.version AND s2.deletedBy IS NULL)
FROM sec$UserRole s
GROUP BY s.version
Here I got the result, that there are 2 users with non-deleted roles of version 0.
But then I also wanted to count the users no matter what their deletedBy was so I added just another field:
SELECT
s.version,
COUNT(s.user),
(SELECT COUNT(DISTINCT s2.user) FROM sec$UserRole s2 WHERE s2.version=s.version AND s2.deletedBy IS NULL)
FROM sec$UserRole s
GROUP BY s.version
With the result, that my subselect went to 0. So I got version=0 and total users=2 but the users of non-deleted roles are now 0!
I thought maybe there is some weird JPQL transformation going on and the resulting SQL is broken but it doesn’t seem to be and to be sure, I wrote the query in very simple form in SQL:
SELECT
t0.VERSION,
COUNT(t0.USER_ID),
(SELECT COUNT(DISTINCT(t3.USER_ID))
FROM SEC_USER_ROLE t3
WHERE t3.VERSION = t0.VERSION
AND t3.DELETED_BY IS NULL
)
FROM SEC_USER_ROLE t0
GROUP BY t0.VERSION
The result is the same, my subselect is 0 and only delivers the correct number when I ommit the other COUNT.
I tried this method in another database (Oracle) outside of CUBA and this is not a problem. So is it a bug in the HSQL database or does the SQL get transformed by CUBA or what could it be?
My worries are that I have to make quite a number of statistical work and have been spending a lot of time to find out what is working in JPQL and what not and I don’t want to spend more time avoiding obstacles than writing the queries.
Thank you very much in advance, if you need any more clarification I will provide everything that is needed.