Skip to content

Unexpected Results of IN expression With NATURAL RIGHT JOIN #49476

Closed

Description

@suyZhong

Bug Report

1. Minimal reproduce step (Required)

CREATE TABLE t0(c0 INT);
CREATE TABLE t1(c0 BOOLEAN);
INSERT INTO t0 (c0) VALUES (0);

SELECT * FROM t1 NATURAL RIGHT JOIN t0; -- 0
SELECT (false IN (t1.c0, t0.c0)) FROM t1 NATURAL RIGHT JOIN t0; -- 1
SELECT * FROM t1 NATURAL RIGHT JOIN t0 WHERE (false IN (t1.c0, t0.c0));
-- Expected: 0
-- Actual: Empty set

2. What did you expect to see? (Required)

The third SELECT returns an empty set, which is surprising: If the result of second query is 1 (TRUE), the value of the IN expression should be 1, and thus the third query should return 0.

Not sure if the issue is related to #49108

3. What did you see instead (Required)

4. What is your TiDB version? (Required)

Release Version: v7.6.0-alpha
Edition: Community
Git Commit Hash: 918df0ae50568ebc54bd6d89f2d9a49bc9b79b06
Git Branch: heads/refs/tags/v7.6.0-alpha
UTC Build Time: 2023-12-14 04:15:47
GoVersion: go1.21.5
Race Enabled: false
Check Table Before Drop: false
Store: unistore |

Activity

  1. suyZhong commented on Dec 16, 2023

    @suyZhong
    Author

    /label fuzz/sqlancer

  2. winoros commented on May 13, 2024

    @winoros
    Member

    This one is related to the null-rejective checking.
    Cannot solve in a short time.

  3. ghazalfamilyusa commented on May 13, 2024

    @ghazalfamilyusa
    Contributor

    As @winoros said, it is caused by null rejection logic which incorrectly converts the right outer join to inner join.
    See plan below (plan 1). The equivalent query explain SELECT * FROM t1 NATURAL RIGHT JOIN t0 WHERE (false IN (t1.c0) or false in (t0.c0)) works OK (see plan 2). The null rejection logic executes incorrectly false in (0,NULL) as false since false = 0 is true. But, it does executes false in (0) or false in (null) as true.

    Plan1
    explain format="brief" SELECT * FROM t1 NATURAL RIGHT JOIN t0 WHERE (false IN (t1.c0, t0.c0))

    +---------------------------+---------+-----------+---------------+------------------------------------------------------------------------------------------+
    | id | estRows | task | access object | operator info |
    +---------------------------+---------+-----------+---------------+------------------------------------------------------------------------------------------+
    | HashJoin | 1.00 | root | | inner join, equal:[eq(test.t1.c0, test.t0.c0)], other cond:in(0, test.t1.c0, test.t0.c0) |
    | ├─TableReader(Build) | 1.00 | root | | data:Selection |
    | │ └─Selection | 1.00 | cop[tikv] | | not(isnull(test.t0.c0)) |
    | │ └─TableFullScan | 1.00 | cop[tikv] | table:t0 | keep order:false, stats:pseudo |
    | └─TableReader(Probe) | 1.00 | root | | data:Selection |
    | └─Selection | 1.00 | cop[tikv] | | not(isnull(test.t1.c0)) |
    | └─TableFullScan | 1.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
    +---------------------------+---------+-----------+---------------+------------------------------------------------------------------------------------------+

    Plan2
    explain format="brief" SELECT * FROM t1 NATURAL RIGHT JOIN t0 WHERE (false IN (t1.c0) or false in (t0.c0))

    +-------------------------------+---------+-----------+---------------+------------------------------------------------------+
    | id | estRows | task | access object | operator info |
    +-------------------------------+---------+-----------+---------------+------------------------------------------------------+
    | Projection | 0.80 | root | | test.t0.c0 |
    | └─Selection | 0.80 | root | | or(eq(0, test.t1.c0), eq(0, test.t0.c0)) |
    | └─HashJoin | 1.00 | root | | right outer join, equal:[eq(test.t1.c0, test.t0.c0)] |
    | ├─TableReader(Build) | 1.00 | root | | data:Selection |
    | │ └─Selection | 1.00 | cop[tikv] | | not(isnull(test.t1.c0)) |
    | │ └─TableFullScan | 1.00 | cop[tikv] | table:t1 | keep order:false, stats:pseudo |
    | └─TableReader(Probe) | 1.00 | root | | data:TableFullScan |
    | └─TableFullScan | 1.00 | cop[tikv] | table:t0 | keep order:false, stats:pseudo |
    +-------------------------------+---------+-----------+---------------+------------------------------------------------------+

  4. added
    affects-6.5This bug affects the 6.5.x(LTS) versions.
    affects-7.1This bug affects the 7.1.x(LTS) versions.
    affects-7.5This bug affects the 7.5.x(LTS) versions.
    on May 19, 2024
  5. added
    affects-6.1This bug affects the 6.1.x(LTS) versions.
    and removed
    may-affects-5.4This bug maybe affects 5.4.x versions.
    on May 19, 2024
  6. added a commit that references this issue on May 21, 2024
    61a2981
  7. added a commit that references this issue on May 23, 2024
    6b5476d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

affects-6.1This bug affects the 6.1.x(LTS) versions.affects-6.5This bug affects the 6.5.x(LTS) versions.affects-7.1This bug affects the 7.1.x(LTS) versions.affects-7.5This bug affects the 7.5.x(LTS) versions.affects-8.1This bug affects the 8.1.x(LTS) versions.fuzz/sqlancerseverity/majorsig/plannerSIG: Plannertype/bugThe issue is confirmed as a bug.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions