Value Datasource JPQL query demo$Order to aggregate/sum daily amount (without time portion)

Hi there,
With the help of the cuba team, I managed to get the Value Datasource working (Thank you!).
My valueCollectionDatasource JPQL is:

        select o.create_ts, sum(o.amount) from demo$Order o group by o.create_ts

Here are 3 order records in demo$Order table:

create_ts	                                amount
2017-03-02 20:01:06.996 	186.00
2017-03-03 19:25:46.677        150.00
2017-03-03 19:59:31.578         100.00

I wanted to get the total order amount grouped by day without timestamp like this:

2017-03-02   	186.00
2017-03-03          250.00

The above JPQL gives me 3 records exactly the same as in the order table (no aggregation grouped by date at all).
My valueCollectionDatasource is:

       <valueCollectionDatasource id="salesAggregatorDs">
                <![CDATA[select o.create_ts, sum(o.amount) from demo$Order o group by o.create_ts]]>
                <property datatype="date"
                <property datatype="decimal"

Also how to specify the format “yyyy-mm-dd” to property “orderDate” ?
Thanks advance for your help,

Hi Mike,

The createTs system attribute contains time part, so you have grouping by date and time, not only by date. I would suggest adding a special attribute of type Date and use it for grouping.

Hi Konstantin,

I followed your suggestion by adding a special attribute of type Date: orderDate for grouping, it works great with this query:

select o.orderDate, sum(o.amount) from demo$Order o group by o.orderDate ORDER BY o.orderDate

Now I need to add date range to the above query. i.e., there must be a way to specify the start and end Date parameters. I did the following and got a nullPointerException: (salesAggregatorDs is the Value Datasource, todayDate is correctly assigned. This example tried to get the daily sum(o.amount) of the last 7 days )

        LoadContext lc = salesAggregatorDs.getCompiledLoadContext();
        lc.setQueryString("select o.orderDate, sum(o.amount) from demo$Order o where (o.orderDate between :start and :end) group by o.orderDate ORDER BY o.orderDate")
                .setParameter("start", DateUtils.addDays(todayDate, -7))
                .setParameter("end", DateUtils.addDays(todayDate, 1));


The statement of “salesAggregatorDs.refresh();” asserts nullPointerException:

java.lang.NullPointerException: null
        at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(
        at sun.reflect.GeneratedMethodAccessor265.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(
        at java.lang.reflect.Method.invoke(
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(
        at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(
        at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(
        at com.sun.proxy.$Proxy210.loadValues(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(
        at java.lang.reflect.Method.invoke(
        at com.haulmont.cuba.core.sys.remoting.LocalServiceInvokerImpl.invoke(
        at com.haulmont.cuba.web.sys.remoting.LocalServiceProxy$LocalServiceInvocationHandler.invoke(
        at com.sun.proxy.$Proxy26.loadValues(Unknown Source)
        at com.haulmont.cuba.client.sys.DataManagerClientImpl.loadValues(
        at cloud.pospro.posportal.web.screens.Sales.onLinkBtnLast7DaysClick(
        ... 61 more

What’s the correct way to query a Date range in this case?

Thank you,

Hi Mike,

The exception is probably caused not by JPQL but by the way you are trying to modify the datasource query and parameters. The getCompiledLoadContext() method is not designed for this purpose, as stated in its JavaDoc.

See an example of modifying the datasource query here. In JPQL, you can use parameters with param$, component$ or cuatom$ prefix, see the explanation in the docs. In the latter case, you need to invoke refresh() in the controller’s code.

Let me know if you have difficulties using this approach for value datasources.

Hi Konstantin,

Thank you very much for your solution. It helps a lot.

Here is the code that specifies the date range of last 30 days with the Value Datasource:

salesAggregatorDs.refresh(ParamsMap.of("startDate", DateUtils.addDays(todayDate, -30), "endDate", DateUtils.addDays(todayDate, 1)));

Again thanks for the great help,

Hi Konstantin,

I am quite happy that Value Datasource works and date range parameters can be specified by Ds.refresh(…).

For curiosity, I would like to understand more about the aggregation GROUP BY flexibility on Cuba platform. Is GROUP BY the “YEAR, MONTH, DAY, HOUR, MINUTE, SECOND” of a dateTime type possible on Cuba?

The suggestion of creating a separate column of type Date seems to be a workaround than a solution(it works, thank you!). From related discussions on StackOverflow, JPQL does have this capability. For example, eclipselink has EXTRACT function: "Use EXTRACT to retrieve the date portion of a date/time value."

However, when I use it to group the orders by HOUR like this:

select o.createTs, sum(o.amount) from demo$Order o where (o.orderDate between :custom$startDate and :custom$endDate) group by EXTRACT(HOUR, o.createTs)

The error occurs:

Caused by: com.haulmont.cuba.core.sys.jpql.JpqlSyntaxException: Errors found for input jpql:[select o.createTs, sum(o.amount) from posportal$Order o where (o.orderDate between :custom_startDate and :custom_endDate) group by EXTRACT(HOUR, o.createTs) ORDER BY o.createTs]
CommonErrorNode [<unexpected: [@57,148:155='EXTRACT(',<100>,1:148], resync=EXTRACT(HOUR>]
        at com.haulmont.cuba.core.sys.jpql.Parser.checkTreeForExceptions(
        at com.haulmont.cuba.core.sys.jpql.Parser.parse(
        at com.haulmont.cuba.core.sys.jpql.QueryTreeAnalyzer.prepare(
        at com.haulmont.cuba.core.sys.jpql.QueryTreeAnalyzer.prepare(
        at cloud.pospro.posportal.web.screens.Sales.init(
        at com.haulmont.cuba.gui.WindowManager.init(
        at com.haulmont.cuba.gui.WindowManager.initWrapperFrame(
        at com.haulmont.cuba.gui.WindowManager.createWindow(
        at com.haulmont.cuba.gui.WindowManager.openWindow(
        at com.haulmont.cuba.web.WebWindowManager.openWindow(
        at com.haulmont.cuba.gui.WindowManager.openWindow(
        at com.haulmont.cuba.gui.components.WindowDelegate.openWindow(
        at com.haulmont.cuba.web.gui.WebWindow.openWindow(
        at com.haulmont.cuba.gui.components.AbstractFrame.openWindow(
        at cloud.pospro.posportal.web.mainwindow.ExtAppMainWindow.onSalesClick(
        ... 59 more

Does cuba platform support GROUP BY “YEAR, MONTH, DAY, HOUR, SECOND”, or it is something else that cause this problem? If Cuba supports, what is the correct method to specify GROUP BY clause?

Thank you,


CUBA has its own parser of JPQL which is needed for analyzing and transforming queries before sending them to EclipseLink. Unfortunately, not all features of EclipseLink’s JPQL are correctly handled at the moment. We’ll try to implement EXTRACT function in the near future - thank you for reporting the problem. You can track the progress in the linked YouTrack issue.

1 Like

Great. Looking forward to seeing EXTRACT implementation in Cuba very soon.

With this simple EXTRACT approach, I believe Chart via Value Datasource, the performance should be much improved and controller resources usage (CPU/memory) will be a lot less, since we do not need huge amount of data to be returned to controller for the processing which can be done on database engine itself. Definitely it is a great feature, hope to see it in reality SOON.

Thanks a lot for the help,

1 Like

EXTRACT support would be huge help. +1

On a second thought, an enhanced EXTRACT function with more flexibility might be more efficient and helpful.

The standard EXTRACT function looks like:

group by EXTRACT(HOUR, o.createTs)

Here “HOUR” could be “YEAR”, “MONTH”, “DAY”, “MINUTE”, “SECOND”, “MILLISECOND” …

in real application, the need to group order records might not be one of the above. i.e., the application wants to group by “Half hour(30 minutes)”, or “10Minutes”, or “5Second” instead of group by “HOUR”, “MINUTE”, “SECOND”. Thus if (MINUTE, o.createTs) could be designed to be more flexible that allow user to specify a value like:
group by EXTRACT(MINUTE, o.createTs, 10) <<<<< 10 is the value that user can specify. It means group every 10 minutes

This would be a great enhancement for more efficient application.

Just bring this up for Cuba team to consider when implement it if possible.


Hi Mike,

Since the platform version 6.5.0, the EXTRACT function is supported.


1 Like

:ticket: See the following issue in our bug tracker: