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

Mihai Budiu commented on CALCITE-5761:
--------------------------------------

The problem is in RexImpTable.FloorImplementor.implementSafe.

There's a switch statement looking like this:

    switch (timeUnitRange) {
        case YEAR:
        case ISOYEAR:
        case QUARTER:
        case MONTH:
        case WEEK:
        case DAY:
          final Expression dayOperand0 =
              preFloor ? call(operand0, type, TimeUnit.DAY) : operand0;
          return Expressions.call(floorMethod,
              translator.getLiteral(operand1), dayOperand0);
        default:
          return call(operand0, type, timeUnitRange.startUnit);
        }

It's fairly easy to just add a few more cases for DECADE-MILLENIUM, and the 
tests then pass.

However, I am worried about the 'default' label, which currently returns 
gibberish. It ends up calling SqlFunction.floor(int, int), which happens, I 
suspect because RexImpTable.map maps DATE_TRUNC to FLOOR. Instead or returning 
garbage an error should be given for the unhandled cases. What's the right way 
to return an error in this place?

It's not easy to debug this stuff because I suspect it goes through some 
dynamic code generation and class loading.

> DATE_TRUNC compile-time evaluation produces wrong results
> ---------------------------------------------------------
>
>                 Key: CALCITE-5761
>                 URL: https://issues.apache.org/jira/browse/CALCITE-5761
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.34.0
>            Reporter: Mihai Budiu
>            Priority: Minor
>
> Adding the following 3 tests to SqlOperatorTest.testDateTrunc produces 
> assertion failures.
> f.checkScalar("date_trunc(date '2015-02-19', decade)",
>         "2010-01-01", "DATE NOT NULL");
>     f.checkScalar("date_trunc(date '2015-02-19', century)",
>         "2001-01-01", "DATE NOT NULL");
>     f.checkScalar("date_trunc(date '2015-02-19', millennium)",
>         "1001-01-01", "DATE NOT NULL");
> Results produced:
> Query: values (date_trunc(date '2015-02-19', decade))
> Expected: is "2010-01-01"
>      but: was "2015-01-05"
> Query: values (date_trunc(date '2015-02-19', century))
> Expected: is "2001-01-01"
>      but: was "2012-09-17"
> Query: values (date_trunc(date '2015-02-19', millennium))
> Expected: is "1001-01-01"
>      but: was "2002-11-09"



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to