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

AssertionError during MOD operation if result scale is greater than maximum numeric scale

    XMLWordPrintableJSON

Details

    Description

      Adding the following test to RelToSqlConverterTest produces an AssertionFailure:

      @Test void testNumericScaleMod() {
          final String sql = "SELECT MOD(CAST(2 AS DECIMAL(39, 20)), 2)";
          final String expected = "";
          sql(sql).withPostgresqlModifiedDecimalTypeSystem()
              .ok(expected);
        }
      

      The exception is here:

      java.lang.AssertionError
      	at org.apache.calcite.rel.type.RelDataTypeSystem.deriveDecimalModType(RelDataTypeSystem.java:383)
      	at org.apache.calcite.sql.type.ReturnTypes.lambda$static$11(ReturnTypes.java:849)
      	at org.apache.calcite.sql.type.SqlTypeTransformCascade.inferReturnType(SqlTypeTransformCascade.java:58)
      	at org.apache.calcite.sql.type.SqlReturnTypeInferenceChain.inferReturnType(SqlReturnTypeInferenceChain.java:55)
      

      In this line:

      int scale = Math.max(s1, s2);
      assert scale <= getMaxNumericScale();
      

      I suspect that doing a Math.min with the getMaxNumericScale (instead of an Assert) is sufficient to fix this particular problem. The question is whether the compiler should really enforce the invariant that this assertion checks.

      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: