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

Issues with combining coalesce function

    XMLWordPrintable

Details

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

    Description

      The exported file for the database is in the attachment.

      --orignal sql
      SELECT l_extendedprice
      FROM lineitem
      WHERE (l_comment IN ( COALESCE(1198529099, lineitem.l_discount) )  ) 
      	= 0.05420610582877583
      GROUP BY l_extendedprice;

      return 0 row

      --rewritten sql
      SELECT L_EXTENDEDPRICE
      FROM LINEITEM
      WHERE NOT EXISTS (
          SELECT 1
          WHERE L_COMMENT <> 1198529099 OR L_COMMENT IS NULL
      ) = 0.05420610582877583
      GROUP BY L_EXTENDEDPRICE;

      return 4467 row

      These two queries are logically equivalent, although they use different syntax and expressions.

      Original query: In the original query, COALESCE(1198529099, lineitem.l_discount) returns the first non-NULL value between 1198529099 and lineitem.l_discount. Then, l_comment IN (...) checks if l_comment equals this value. After that, the query filters the rows that satisfy this condition and compares the result with 0.05420610582877583.

      Rewritten query: The rewritten query uses a NOT EXISTS subquery to implement similar logic. The NOT EXISTS checks if there is no record where l_comment is not equal to 1198529099 or is NULL. If no such record exists, it returns the rows that meet the condition. The result is also compared to 0.05420610582877583.

      The logic of the rewritten query is the same as the original query. Unexpectedly, the number of returned rows is different, 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.