Mihai Budiu created CALCITE-6228:
------------------------------------

             Summary: ELEMENT function infers incorrect return type
                 Key: CALCITE-6228
                 URL: https://issues.apache.org/jira/browse/CALCITE-6228
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.36.0
            Reporter: Mihai Budiu


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

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

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:

{code:java}
public static final SqlFunction ELEMENT =
      SqlBasicFunction.create("ELEMENT",
          ReturnTypes.MULTISET_ELEMENT_NULLABLE,
          OperandTypes.COLLECTION);
{code}

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

{code:java}
 public static final SqlReturnTypeInference MULTISET_ELEMENT_NULLABLE =
      MULTISET.andThen(SqlTypeTransforms.TO_COLLECTION_ELEMENT_TYPE);
{code}

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

{code:java}
  public static final SqlReturnTypeInference MULTISET_ELEMENT_NULLABLE =
      MULTISET.andThen(SqlTypeTransforms.TO_COLLECTION_ELEMENT_TYPE)
          .andThen(SqlTypeTransforms.FORCE_NULLABLE);
{code}



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

Reply via email to