You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@calcite.apache.org by "Rui Wang (Jira)" <ji...@apache.org> on 2019/11/21 18:16:00 UTC

[jira] [Commented] (CALCITE-3529) TIMESTAMPDIFF function may overflow when handle unit of MICROSECOND/NANOSECOND

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

Rui Wang commented on CALCITE-3529:
-----------------------------------

I had similar problem in BeamSQL, and in Beam community we had several discussions there. In Java world, due to wide-used Joda library, usually "timestamp" only supports up to millisecond precision. In order to support beyond millis, essentially we need extra storage to save sub-millis, which might be just another int.

Back to Calcite sql function implementation. I believe currently relevant implementation converts "timestamp" to long and assume it's millisecond. 

> TIMESTAMPDIFF function may overflow when handle unit of MICROSECOND/NANOSECOND
> ------------------------------------------------------------------------------
>
>                 Key: CALCITE-3529
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3529
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.21.0
>            Reporter: Zhenghua Gao
>            Priority: Major
>
> For unit of NANOSECOND, the TIMESTAMPDIFF function returns a BIGINT, which may overflow since BIGINT can't cover the huge range of nanosecond. 
> For unit of MICROSECOND, the TIMESTAMPDIFF function returns a INTEGER, which may overflow too.
> TIMESTAMPDIFF(MICROSECOND, TIMESTAMP '9999-12-31 23:59:59.999', TIMESTAMP '0001-01-01 00:00:00.000') should returns '315537897599999000' [1], calcite returns '879764032'. 
>  
> TIMESTAMPDIFF(NANOSECOND, TIMESTAMP '9999-12-31 23:59:59.999', TIMESTAMP '0001-01-01 00:00:00.000') should returns '315537897599999000*000*' which is bigger than LONG.MAX_VALUE, calcite returns '-1943248345937622528'.
>  
> My suggestion is:
>  # Change the return type to BIGINT for unit of MICROSECOND
>  # Disable support for unit of NANOSECOND or give a RISK message in the document of TIMESTAMPDIFF
> [1] the value is calculated by MySQL 5.6 from [http://sqlfiddle.com/]



--
This message was sent by Atlassian Jira
(v8.3.4#803005)