[ 
https://issues.apache.org/jira/browse/DERBY-6569?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Knut Anders Hatlen updated DERBY-6569:
--------------------------------------

    Attachment: d6569-1a.diff

The attached patch, [^d6569-1a.diff], attempts to fix this issue by introducing 
a new node type, called CachedValueNode, that can be used to wrap nodes that 
get inserted multiple places in a rewritten tree, and makes them evaluate only 
once.

CachedValueNode is just a thin wrapper around a ValueNode, and for the most 
part it just forwards calls to the wrapped ValueNode. The only piece of own 
logic in the new class is located in the generateExpression() method. That 
method generates different code on the first call than it does on subsequent 
calls. On the first call, it sets up a field in the activation class, and 
generates code that evaluates the expression in the wrapped node and stores the 
result in the field. On subsequent calls, it only generates code to read the 
cached value from the field.

All the regression tests pass with the patch. I'm not 100% sure that there 
aren't more ValueNode methods that need to be overridden, but I haven't found 
any case yet where this approach breaks down.

I believe the CachedValueNode class will be useful for implementing simple case 
expressions (DERBY-1576).

> NULLIF may return incorrect results if first operand calls non-deterministic 
> function
> -------------------------------------------------------------------------------------
>
>                 Key: DERBY-6569
>                 URL: https://issues.apache.org/jira/browse/DERBY-6569
>             Project: Derby
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 10.10.2.0
>            Reporter: Knut Anders Hatlen
>            Assignee: Knut Anders Hatlen
>         Attachments: d6569-1a.diff
>
>
> The SQL standard doesn't allow non-deterministic function calls in the 
> operands of NULLIF. Derby does however allow such calls, but the results may 
> not be as one might expect.
> Take an expression such as NULLIF(expr, 1). It shouldn't ever return 1. If 
> expr is 1, it should return NULL, and if expr is not 1, it should return expr.
> If expr contains a call to a non-deterministic function, it may actually end 
> up returning 1 sometimes:
> {noformat}
> ij> SELECT NULLIF(INT(RANDOM()*2), 1) FROM SYS.SYSTABLES;
> 1          
> -----------
> 1          
> 1          
> 1          
> NULL       
> NULL       
> NULL       
> NULL       
> 0          
> 1          
> NULL       
> NULL       
> 0          
> 0          
> NULL       
> 0          
> 1          
> 0          
> NULL       
> 1          
> 0          
> NULL       
> NULL       
> NULL       
> 23 rows selected
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to