You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@calcite.apache.org by "Mihai Budiu (Jira)" <ji...@apache.org> on 2024/12/19 23:57:00 UTC

[jira] [Created] (CALCITE-6741) SqlToRelConverter.convertInToOr is unsound

Mihai Budiu created CALCITE-6741:
------------------------------------

             Summary: SqlToRelConverter.convertInToOr is unsound
                 Key: CALCITE-6741
                 URL: https://issues.apache.org/jira/browse/CALCITE-6741
             Project: Calcite
          Issue Type: Bug
          Components: core
    Affects Versions: 1.38.0
            Reporter: Mihai Budiu


This function converts an expression into another expression which does not have the same type.

The function convertInToOr has the following JavaDoc:

{code:java}
/* Converts "x IN (1, 2, ...)" to "x=1 OR x=2 OR ..." */
{code}

This is unsound when x is not nullable and the list contains NULL.
The "in" expression can never evaluate to NULL in that case, whereas the converted expression can.

As an example, consider this test case, which is a simplified version of a test from Sql Logic Test:

{code:sql}
SELECT CASE WHEN 1 NOT IN ( NULL, COUNT(*) ) THEN 1 END
{code}

The plan produced for this query is as follows:

{code}
    LogicalProject(EXPR$0=[CASE(CAST(AND(null, <>(1, $0))):BOOLEAN NOT NULL, 1, null:INTEGER)]), id = 202
      LogicalAggregate(group=[{}], agg#0=[COUNT()]), id = 200
        LogicalValues(tuples=[[{ 0 }]]), id = 160
{code}

Notice that there's a CAST(AND ...): BOOLEAN NOT NULL. Unfortunately the AND expression as written is nullable, so this cast is unsound.

I am thinking that the correct expansion should use IS_NOT_DISTINCT_FROM instead of EQUALS.





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