Skip to content

Incorrect result for IS NOT NULL predicate over UNION ALL query #14352

Closed

Description

@findepi

Describe the bug

Incorrect result for IS NOT NULL predicate over UNION ALL query

To Reproduce

SELECT
    a,
    a IS NOT NULL
FROM (
    -- second column, even though it's not selected, is necessary to reproduce this bug
    SELECT 'foo' AS a, 3 AS b
    UNION ALL
    SELECT NULL AS a, 4 AS b
)

DF

+------+---------------+
| a    | a IS NOT NULL |
+------+---------------+
| NULL | true  😱       |
| foo  | true          |
+------+---------------+

Expected behavior

NULL valued checked with IS NOT NULL should return false

trino> SELECT
    ->     a,
    ->     a IS NOT NULL
    -> FROM (
    ->     SELECT 'foo' AS a, 3 AS b
    ->     UNION ALL
    ->     SELECT NULL AS a, 4 AS b
    -> );
  a   | _col1
------+-------
 foo  | true
 NULL | false

Additional context

No response

Activity

  1. findepi commented on Jan 29, 2025

    @findepi
    MemberAuthor

    This is actually two separate bugs:

    • initial plan for the Union already has the field marked as not-nullable, because the plan builder simply takes first input schema as the union schema (thus ignoring actual nullability). This alone maybe is corrected somewhere later, as the query without b projection does not reproduce the bug.
    • even if the plan is constructed correctly (by whatever means), the column pruning optimizer will do the same, replace union schema with the first input's schema (thus ignoring actual nullability).

    Both bugs are being fixed in #14356.
    The second led me to believe the idea that we can "reconstruct schema", while very convenient, is not solid, thus #14357.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions