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

The issue with combining IFNULL function

    XMLWordPrintable

Details

    • Bug
    • Status: Closed (View Workflow)
    • Critical
    • Resolution: Duplicate
    • 11.5.2
    • N/A
    • Optimizer
    • None

    Description

      The exported file for the database is in the attachment.

      --orignal sql
      SELECT O_TOTALPRICE, O_ORDERKEY, O_CUSTKEY 
      FROM ORDERS 
      WHERE IFNULL(
          O_CUSTKEY, 
          IFNULL(O_ORDERKEY, IFNULL(0.21501538554113775, O_ORDERDATE))
      ) <= O_CLERK
      GROUP BY O_TOTALPRICE, O_ORDERKEY, O_CUSTKEY;

      return 1448 rows

      --rewritten sql
      SELECT distinct O_TOTALPRICE, O_ORDERKEY , O_CUSTKEY
      FROM ORDERS 
      WHERE (O_CUSTKEY <= O_CLERK OR 
            (O_CUSTKEY IS NULL AND O_ORDERKEY <= O_CLERK) );

      reurn 0 row
      These two queries are logically equivalent because they both filter based on the relationship between O_CUSTKEY and O_CLERK while handling potential NULL values.

      In the original query, nested IFNULL functions are used to handle cases where O_CUSTKEY, O_ORDERKEY, O_ORDERDATE, or the constant 0.21501538554113775 may be NULL. If O_CUSTKEY is not NULL, the query checks O_CUSTKEY <= O_CLERK. If O_CUSTKEY is NULL, it moves on to evaluate O_ORDERKEY and other values.

      In the rewritten query, the same logic is explicitly expressed using OR and AND. It first checks whether O_CUSTKEY <= O_CLERK. If O_CUSTKEY is NULL, it evaluates whether O_ORDERKEY <= O_CLERK.

      The two SQL queries are logically equivalent, but they return different results, indicating the presence of a bug.

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              luopeiqi luopeiqi
              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.