[
https://issues.apache.org/jira/browse/CALCITE-6741?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17907244#comment-17907244
]
Julian Hyde commented on CALCITE-6741:
--------------------------------------
My inclination, too, is that this function is fine. We do want "x IN (1, 2)" to
return UNKNOWN for certain values of x, but if you expand to "x IS NOT DISTINCT
FROM 1 OR x IS NOT DISTINCT FROM 2" it will never return UNKNOWN.
Note that "x NOT IN (subquery)" has horrendous semantics if 'subquery' happens
to return a NULL (and perhaps some other values). We want to preserve those
horrendous semantics because the SQL standard requires it. Those semantics are,
again, based on expanding to 'NOT (x = v1 OR x = v2 ... OR x = vN)', where the
vI are the values returned by the subquery and may include nulls.
> 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
> Priority: Minor
>
> 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)