Skip to content

sql/opt: incorrect query results with virtual table lookup semi join #91012

@rafiss

Description

@rafiss

Describe the problem

There's a problem that seems to happen when a virtual table lookup semi join is planned. See below.

                              info
-----------------------------------------------------------------
  distribution: local
  vectorized: true

  • group (scalar)
  │
  └── • virtual table lookup join
      │ table: pg_class@pg_class_oid_idx
      │ equality: (attrelid) = (oid)
      │ pred: relname = 'b'
      │
      └── • virtual table lookup join (semi)
          │ table: pg_type@pg_type_oid_idx
          │ equality: (atttypid) = (oid)
          │ pred: typname = 'int8'
          │
          └── • filter
              │ filter: (NOT attnotnull) AND (attname = 'a_id')
              │
              └── • virtual table
                    table: pg_attribute@primary

To Reproduce

Start a fresh cluster, then:

-- setup
CREATE TABLE b (id INT, a_id INT);

root@localhost:26257/defaultdb> 
SELECT
  count(*)
FROM
  pg_class AS t INNER JOIN pg_attribute AS a ON t.oid = a.attrelid
WHERE
  a.attnotnull = 'f'
  AND a.attname = 'a_id'
  AND t.relname = 'b'
  AND a.atttypid
    IN (
        SELECT
          oid
        FROM
          pg_type
        WHERE
          typname = ANY (ARRAY['int8'])
      );
  count
---------
      0
(1 row)

If you force a hash join, then a count of 1 is returned.

root@localhost:26257/defaultdb> 
SELECT
  count(*)
FROM
  pg_class AS t
  INNER HASH JOIN pg_attribute AS a ON t.oid = a.attrelid
WHERE
  a.attnotnull = 'f'
  AND a.attname = 'a_id'
  AND t.relname = 'b'
  AND a.atttypid
    IN (
        SELECT
          oid
        FROM
          pg_type
        WHERE
          typname = ANY (ARRAY['int8'])
      );
  count
---------
      1
(1 row)

Expected behavior
The query should return a count of 1.

Environment:
Reproduced on v22.2.0-beta.2 and v22.1.8.

Jira issue: CRDB-21073
Epic: CRDB-23454

Metadata

Metadata

Assignees

Labels

A-sql-pgcompatSemantic compatibility with PostgreSQLA-sql-vtablesVirtual tables - pg_catalog, information_schema etcC-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.S-0-visible-logical-errorDatabase stores inconsistent data in some cases, or queries return invalid results silently.T-sql-queriesSQL Queries Teambranch-masterFailures and bugs on the master branch.branch-release-22.1Used to mark GA and release blockers, technical advisories, and bugs for 22.1branch-release-22.2Used to mark GA and release blockers, technical advisories, and bugs for 22.2

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions