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

Knut Anders Hatlen updated DERBY-1576:
--------------------------------------

    Attachment: d1576-4a.diff

The attached patch [^d1576-4a.diff] lifts the restriction that the case operand 
cannot be an untyped parameter.

The original problem was that the case operand would forget its previously 
inferred type each time it was rebound, so that its final type would be the 
type of the last when operand, rather than the union of all the when operand 
types.

The patch solves this by rewriting the query tree at the beginning of the bind 
phase so that all occurrences of the case operand are replaced with dummy 
parameter nodes, one dummy node per when clause in the case expression. This 
way, when the when clauses are bound, each dummy parameter node is bound once, 
so we avoid the problem with the previous type being forgotten when the node is 
rebound.

At the end of the bind phase, we can go through the list of dummy nodes, find 
the dominant type of those nodes, set the type of the case operand to that 
type, and finally reinsert the case operand into the query tree.

The patch touches these files:

*impl/sql/compile/ConditionalNode.java*

- bindCaseOperand: Temporarily replace the case operand with dummy parameter 
nodes.

- bindExpression: After binding the test conditions, check the types that the 
dummy parameter nodes were bound to. If they are not compatible, raise an 
error. If they are compatible, set the type of the case operand to the dominant 
type, and reinsert it into the tree.

*impl/sql/compile/ReplaceNodeVisitor.java*

- New visitor implementation that replaces a node with some other node. Used 
for reinserting the case operand into the tree.

*impl/sql/compile/ValueNodeList.java*

- getDominantTypeServices: Improve the method so that it works with lists that 
consists entirely of nodes that require type from context, as long as they 
already have been bound to a type.

*loc/messages.xml*
*shared/common/reference/SQLState.java*

- Remove the error message that used to be raised if the case operand was an 
untyped parameter.

*tests/lang/CaseExpressionTest.java*

- Add tests that use untyped parameters in the case operand.

> Extend the CASE expression syntax for "simple case"
> ---------------------------------------------------
>
>                 Key: DERBY-1576
>                 URL: https://issues.apache.org/jira/browse/DERBY-1576
>             Project: Derby
>          Issue Type: Improvement
>          Components: SQL
>            Reporter: Christian d'Heureuse
>            Assignee: Knut Anders Hatlen
>            Priority: Minor
>              Labels: derby_triage10_11
>         Attachments: d1576-1a.diff, d1576-2a.diff, d1576-3a.diff, 
> d1576-4a.diff, simple-simple.diff
>
>
> The ISO/IEC 9075-2:1999 SQL standard describes two kinds of CASE expressions: 
> "simple case" and "searched case".
> The current Derby version supports "searched case" but not "simple case".
> The syntax for "simple case" is:
>    CASE Expression
>       WHEN Expression THEN Expression
>     [ WHEN Expression THEN Expression ]
>       ...
>       ELSE ElseExpression
>    END
> Example:
>    VALUES
>       CASE 4
>          WHEN 1 THEN 'one'
>          WHEN 2 THEN 'two'
>          WHEN 3 THEN 'three'
>          ELSE 'many'
>       END



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

Reply via email to