Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-41347

Wrong COUNT(*) result in correlated scalar subquery with multiple NULLs in a nullable UNIQUE key

    XMLWordPrintable

Details

    • Bug
    • Status: Confirmed (View Workflow)
    • Major
    • Resolution: Unresolved
    • 10.11, 11.4, 11.8, 12.3, 13.1, 12.3.3
    • 10.11, 11.4, 11.8, 12.3, 13.1
    • Optimizer
    • None
    • MariaDB 12.3.3, default optimizer_switch. Reproduced with both MyISAM and InnoDB.
    • Unexpected results
    • Correlated scalar COUNT(*) could return an incorrect result for multiple NULL values in a nullable UNIQUE key.

    Description

      A correlated scalar subquery with COUNT returns an incorrect result
      when a nullable UNIQUE column contains multiple NULL values.

      Tested on MariaDB 12.3.3 with the default optimizer_switch.
      The issue reproduces with both MyISAM and InnoDB.

      Minimal reproducer:

      CREATE TABLE n(c INT UNIQUE) ENGINE=MyISAM;
      INSERT INTO n VALUES (NULL), (NULL);

      SELECT (SELECT COUNT FROM n z WHERE z.c <=> x.k) AS scalar_count
      FROM (SELECT NULL AS k) x;

      SELECT COUNT AS direct_count
      FROM n WHERE c IS NULL;

      Actual result:

      scalar_count
      ------------
      1

      direct_count
      ------------
      2

      Expected result:

      scalar_count
      ------------
      2

      The nullable UNIQUE column permits both rows with c = NULL.

      In the correlated scalar subquery, x.k is NULL. Since <=> is
      NULL-safe equality, both rows where z.c is NULL satisfy:

      z.c <=> x.k

      Therefore, COUNT should return 2.

      The direct query:

      SELECT COUNT FROM n WHERE c IS NULL;

      correctly returns 2, while the correlated scalar form incorrectly
      returns 1.

      The same wrong result is reproduced when the table uses InnoDB
      instead of MyISAM.

      Attachments

        Issue Links

          Activity

            People

              psergei Sergei Petrunia
              Ann C Ann C
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.