Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-5919

Compile-time implementation of EXTRACT ignores sub-millisecond values

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Minor
    • Resolution: Unresolved
    • 1.35.0
    • None
    • core
    • None

    Description

      When enabling the optimization rule PROJECT_REDUCE_EXPRESSIONS the compile-time evaluation of an expression like EXTRACT(MICROSECONDS FROM TIME '13:30:25.575401') will produce a result up to milliseconds only, irrespective of the type system provided. (This query will evaluate to 25575000 instead of 25575401).

      The bug is in RexImpTable.ExtractImplementor, here:

      case MILLISECOND:
            case MICROSECOND:
            case NANOSECOND:
              if (sqlTypeName == SqlTypeName.DATE) {
                return Expressions.constant(0L);
              }
              operand = mod(operand, TimeUnit.MINUTE.multiplier.longValue(), !isIntervalType); // << BUG
              return Expressions.multiply(
                  operand, Expressions.constant((long) (1 / unit.multiplier.doubleValue())));
      

      The mod operation uses a multiplier for MINUTE that is expressed in milliseconds, so it always truncates away values below milliseconds. The multiplier seems to assume that the type system precision for TIME is set to 3, which is the default value, but may be overridden.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              mbudiu Mihai Budiu
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated: