TIBCOSoftware / jasperreports

JasperReports® - Free Java Reporting Library
https://community.jaspersoft.com/downloads/community-edition/
GNU Lesser General Public License v3.0
1.07k stars 404 forks source link

Variable expressions are evaluated multiple times #467

Closed tcleenewerck closed 3 months ago

tcleenewerck commented 3 months ago

This report requires a CSV Data adaptor to run. The contents of the csv file is:

"id";"name" id1 ; a id2 ; b id3 ; c

A counter has been defined that prints out how many times an expression of variable has been excuted. This is the result in the jss console:

Executing the variable expression for record id1 previous value null Executing the variable expression for record id1 previous value 0.0 Executing the variable expression for record id2 previous value 1.0 Executing the variable expression for record id2 previous value 1.0 Executing the variable expression for record id3 previous value 2.0 Executing the variable expression for record id3 previous value 2.0

As you can see the expression gets executed twice, which is bad for performance and difficult to debug.

Also it is potentially giving rise to errors if a variable depends on other data stored in a parameter. So if a variable computation has side effects because it uses and stores data in a parameter, doing the calculation twice will result in differents outcomes.

If the resetType is set to none, then an additional evaluation occurs. A variable is then executed three times.

I've attached an example report.

Archief.zip

teodord commented 3 months ago

We know the variable expressions are evaluated twice. It works like this by design. If you look into the source code, you notice there is the notion of "estimating" the values. Estimation is essential to anticipate group breaks. Once a group break is estimated to happen, action is take for the actual group break to be performed, which means group level reinitializations need to be performed and all sorts of group level events are fired. The program works like this since day one, which was decades ago. If you want to perform custom calculations, I would recommend you use scriptlets feature. Trying to force custom calculations through specially crafted variable expressions is not supposed to work.

I hope this helps. Teodor