I am encountering the following issue when trying to use JPA to query Druid:
I tried to order the results by a custom standard:
cb.desc(
cb.selectCase()
.when(cb.equal(root.get("eventTimeGroup"), "Monday"), cb.literal(1))
.when(cb.equal(root.get("eventTimeGroup"), "Tuesday"), cb.literal(2))
.when(cb.equal(root.get("eventTimeGroup"), "Wednesday"), cb.literal(3))
.when(cb.equal(root.get("eventTimeGroup"), "Thursday"), cb.literal(4))
.when(cb.equal(root.get("eventTimeGroup"), "Friday"), cb.literal(5))
.when(cb.equal(root.get("eventTimeGroup"), "Saturday"), cb.literal(6))
.when(cb.equal(root.get("eventTimeGroup"), "Sunday"), cb.literal(7))
.otherwise(cb.literal(8)));
Translating to SQL, it should be:
order by
case
when TIME_FORMAT(TIME_PARSE(event_business_date, NULL),'EEEE')='Monday' then 1
when TIME_FORMAT(TIME_PARSE(event_business_date, NULL),'EEEE')='Tuesday' then 2
when TIME_FORMAT(TIME_PARSE(event_business_date, NULL),'EEEE')='Wednesday' then 3
when TIME_FORMAT(TIME_PARSE(event_business_date, NULL),'EEEE')='Thursday' then 4
when TIME_FORMAT(TIME_PARSE(event_business_date, NULL),'EEEE')='Friday' then 5
when TIME_FORMAT(TIME_PARSE(event_business_date, NULL),'EEEE')='Saturday' then 6
when TIME_FORMAT(TIME_PARSE(event_business_date, NULL),'EEEE')='Sunday' then 7
else 8 end asc
However, according to Druid broker's query request log, there is an error:
"exception":"java.lang.RuntimeException: while converting
CASE
WHEN TIME_FORMAT(TIME_PARSE(`orderentit0_`.`event_business_date`, NULL), 'EEEE') = CAST(? AS VARCHAR CHARACTER SET `UTF-16LE`) THEN 1
WHEN TIME_FORMAT(TIME_PARSE(`orderentit0_`.`event_business_date`, NULL), 'EEEE') = CAST(? AS VARCHAR CHARACTER SET `UTF-16LE`) THEN 2
WHEN TIME_FORMAT(TIME_PARSE(`orderentit0_`.`event_business_date`, NULL), 'EEEE') = CAST(? AS VARCHAR CHARACTER SET `UTF-16LE`) THEN 3
WHEN TIME_FORMAT(TIME_PARSE(`orderentit0_`.`event_business_date`, NULL), 'EEEE') = CAST(? AS VARCHAR CHARACTER SET `UTF-16LE`) THEN 4
WHEN TIME_FORMAT(TIME_PARSE(`orderentit0_`.`event_business_date`, NULL), 'EEEE') = CAST(? AS VARCHAR CHARACTER SET `UTF-16LE`) THEN 5
WHEN TIME_FORMAT(TIME_PARSE(`orderentit0_`.`event_business_date`, NULL), 'EEEE') = CAST(? AS VARCHAR CHARACTER SET `UTF-16LE`) THEN 6
WHEN TIME_FORMAT(TIME_PARSE(`orderentit0_`.`event_business_date`, NULL), 'EEEE') = CAST(? AS VARCHAR CHARACTER SET `UTF-16LE`) THEN 7
ELSE 8 END"
I suspect that the problem is at the question mark. The reason for my suspicion is that when I enter the same SQL query directly into Druid console, with the question marks, Druid gives the same error message.
Now this puzzles me, because in other use cases, for instance in the WHERE clause of the query, I am also doing similar things:
cb.equal(root.get("eventType"), "SALES")
which translating to SQL is:
eventType = 'SALES'
and there is no error at this place.
I am wondering if somehow Spring's parameterization does not work inside the CASE WHEN THEN or ORDER BY block (but then I did not get any Spring error saying parameter binding failed)? Or if somehow the CAST() in Druid's error message is what caused the parameterization not working?
Thank you very much for your time and help!