[ 
https://issues.apache.org/jira/browse/PHOENIX-1237?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14124780#comment-14124780
 ] 

James Taylor commented on PHOENIX-1237:
---------------------------------------

Thanks for the new tests, [~tzolkin]. Interested in fixing the 
CoalesceFunction, as you're already spot-on regarding the cause. You just need 
to use the PDataType.coerceBytes() function to convert the evaluation of the 
second parameter to the expected type. Something like this:
{code}
    @Override
    public boolean evaluate(Tuple tuple, ImmutableBytesWritable ptr) {
        boolean evaluated = children.get(0).evaluate(tuple, ptr);
        if (evaluated) {
            return true;
        }
        // Only evaluate the second child if we're no longer incrementally 
evaluating
        // the tuple (ensuring that we won't be able to evaluate the first 
child).
        if (tuple.isImmutable()) {
            Expression nextChild = children.get(1);
            if (nextChild.evaluate(tuple, ptr)) {
                // Coerce the type of the second child to the type of the first 
child
                getDataType().coerceBytes(ptr, nextChild.getDataType(), 
nextChild.getSortOrder(), getSortOrder());
            }
        }
        return false;
    }
{code}

> COALESCE Function - type of second parameter
> --------------------------------------------
>
>                 Key: PHOENIX-1237
>                 URL: https://issues.apache.org/jira/browse/PHOENIX-1237
>             Project: Phoenix
>          Issue Type: Bug
>    Affects Versions: 5.0.0, 3.1, 4.1
>            Reporter: Vaclav Loffelmann
>         Attachments: PHOENIX_1237.patch
>
>
> There is an issue with automatic set data type of second parameter to 
> coalesce function.
> For instance table:
> {code:sql}
> CREATE TABLE IF NOT EXISTS TEST_COALESCE(
> ID BIGINT NOT NULL,
> COUNT UNSIGNED_INT
> CONSTRAINT pk PRIMARY KEY(ID));
> {code}
> and query:
> {code:sql}
> SELECT COALESCE(SUM(COUNT), 0) FROM TEST_SUM GROUP BY ID;
> {code}
> This will cause in:
> {quote}SQLException: ERROR 507 (42846): Cannot convert type. COALESCE 
> expected UNSIGNED_INT, but got INTEGER {quote}
> Hence second parameter is typed to its input value (eg. 0 is signed int not 
> long), and long is expected. That is cause for ArrayIndexOutOfBoundsException 
> described in PHOENIX-1229.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to