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

ELEMENT function infers incorrect return type

VotersWatch issueWatchersLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    Description

      The ELEMENT function is defined in the documentation as follows: https://calcite.apache.org/docs/reference.html#collection-functions

      Returns the sole element of an array or multiset; null if the collection is empty; throws if it has more than one element.

      However, the type inference returns just the type of the element of the collection, without changing its nullability.

      The type inference is implemented as follows in SqlStdOperatorTable:

      public static final SqlFunction ELEMENT =
            SqlBasicFunction.create("ELEMENT",
                ReturnTypes.MULTISET_ELEMENT_NULLABLE,
                OperandTypes.COLLECTION);
      

      However, reading the definition of MULTISET_ELEMENT_NULLABLE in ReturnTypes.java:

       public static final SqlReturnTypeInference MULTISET_ELEMENT_NULLABLE =
            MULTISET.andThen(SqlTypeTransforms.TO_COLLECTION_ELEMENT_TYPE);
      

      we notice that it is not forced to be nullable. Probably the correct implementation would be

        public static final SqlReturnTypeInference MULTISET_ELEMENT_NULLABLE =
            MULTISET.andThen(SqlTypeTransforms.TO_COLLECTION_ELEMENT_TYPE)
                .andThen(SqlTypeTransforms.FORCE_NULLABLE);
      

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

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

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment