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

Julian Hyde commented on CALCITE-3881:
--------------------------------------

Your fix only works for 0 <= months < 24. You should make it work for any 
months value, including negative. Which probably means calling {{floorMod}}.

I think the fix should be in {{SqlFunctions.addMonths}} (Calcite), not 
ymdToUnixDate (Avatica). ymdToUnixDate assumes that month is between 1 and 12, 
and it should continue to do so. Its javadoc should state what it needs; 
perhaps there should be an assert or throw IllegalArgumentException.

> SqlFunctions#addMonths yields incorrect results in some corner case
> -------------------------------------------------------------------
>
>                 Key: CALCITE-3881
>                 URL: https://issues.apache.org/jira/browse/CALCITE-3881
>             Project: Calcite
>          Issue Type: Bug
>          Components: avatica
>    Affects Versions: avatica-1.16.0
>            Reporter: Zhenghua Gao
>            Priority: Major
>              Labels: pull-request-available
>          Time Spent: 10m
>  Remaining Estimate: 0h
>
> SqlFunctions#addMonths use DateTimeUtils#ymdToUnixDate to calculate the 
> JDN(julian day number). But in some corner cases it yields incorrent results. 
> The root cause is: the algorithm of DateTimeUtils#ymdToUnixDate requires 
> reasonable month(1 to 12)[1], but SqlFunctions#addMonths may pass in a month 
> out of the reasonable range.
> BTW: I didn't find the reference of the original paper of the algorithm, but 
> an jdn explanation. Please correct me if anyone can find the original paper.
>  
> The following case can reproduce the bug:
> addMonth('2019-09-01', 6) should yield '2020-03-01'
> {code:java}
> @Test public void testAddMonths() { 
>   checkAddMonths(2019, 9, 1, 2020, 3, 1, 6); 
> } {code}
>  
> [1] 
> [http://www.cs.utsa.edu/~cs1063/projects/Spring2011/Project1/jdn-explanation.html]



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

Reply via email to