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

MariaDB returns inconsistent results for a simple CASE predicate in WHERE when its WHEN operand contains double negation on a string column

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Major
    • Resolution: Duplicate
    • 12.3.2
    • N/A
    • Optimizer
    • None
    • MariaDB Server 12.3.2-MariaDB
      Windows 11 64-bit
      Both test columns use utf8mb4 with utf8mb4_bin collation
    • Not for Release Notes
    • A query using a simple CASE expression with a double-negated string column in the WHEN operand may incorrectly filter out a row that evaluates to true when the same expression is projected.

    Description

      Description

      The same simple CASE expression evaluates to 1 when selected as a value, but the row is excluded when the expression is used as a WHERE condition. The query that counts rows matching the predicate returns 0, while counting the true values of that same predicate in the select list returns 1. I verified the discrepancy repeatedly on MariaDB 12.3.2.

      Reproduction

      CREATE DATABASE mariadb_case_when_repro;
      USE mariadb_case_when_repro;
       
      CREATE TABLE t (
          a VARCHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin,
          b VARCHAR(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin
      );
       
      INSERT INTO t VALUES ('x', 'y');
       
      SELECT
          a,
          b,
          CASE a WHEN NOT NOT b THEN 1 ELSE 0 END AS case_value,
          IF(CASE a WHEN NOT NOT b THEN 1 ELSE 0 END, 1, 0) AS projected_truth
      FROM t;
       
      SELECT COUNT(*) AS where_count
      FROM t
      WHERE CASE a WHEN NOT NOT b THEN 1 ELSE 0 END;
       
      SELECT SUM(IF(CASE a WHEN NOT NOT b THEN 1 ELSE 0 END, 1, 0))
             AS projected_count
      FROM t;
      

      Expected Result

      The first query returns case_value = 1 and projected_truth = 1. Since the predicate evaluates to true for the only row, both counts should be 1:
      where_count = 1
      projected_count = 1

      Actual Result

      On MariaDB 12.3.2:
      case_value = 1
      projected_truth = 1
      where_count = 0
      projected_count = 1

      Thus, the row is returned as a true predicate in the projection, but is filtered out when the same predicate is used in WHERE.

      Cross-Database Check

      With the same table and statements, MySQL 8.0.46, MySQL 8.4.7, Percona Server 8.4.11, and TiDB 8.5.8 return 1 for both counts.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              123 zhang hanshuo
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Git Integration

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