dynamobi/dynamobi — Issue #497: [FNL-11] Conditional code executes eagerly
Author: dynamobi-build
Created: 2011-12-30 03:29:49+00:00
State: closed
Labels: FNL

[reporter="jpham", created="Mon, 23 Jan 2006 14:48:33 -0500 (GMT-05:00)"]
Given a case expression

Case

when Cond1 then Value1

when Cond2 then Value2

....

else ElseValue

End

The values Value1, Value2, ..., ElseValue, are all precomputed, even before looking at Cond1, Cond2, ...

This is both inefficient, and may cause an error if certain values can only be calculated for certain conditions.

This problem is related to both the RexProgramBuilder, which generates programs. (eagerly)

And also the Rex => Calc translator which implements the program. (eagerly)

***

There is a different case, which relates to eager evaluation.

Here is an example:

Case

when x < -5 then x * 2

When x < 0 then x + 10

When x < 10 when sqrt(x) + x * 2

Else sqrt(x)

End

CSE would cause this to eagerly evaluate both 'x * 2' and 'sqrt(x)'. This may waste some CPU cycles, since not all of the

expressions are needed in all branches. In this case it is worse: if x is negative, sqrt will throw an exception which a correct

implementation would not throw.

(I am assuming that SQL's CASE operator has lazy evaluation semantics. Please correct me if I am wrong.)