pingcap / pingcap/docs

Bug Report: Issues with IF function

Open
#19,475 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
617
Forks
724
Avg merge
2d 10h
Merged PRs (30d)
223

Description

BugReport: Issues with IF function

version

8.3.0

Original sql

SELECT 
    O_CUSTKEY 
FROM 
    orders
WHERE 
    (
        O_COMMENT   IN (
            IF(
                -1801656545 IS NOT NULL, 
                -1801656545, 

            )
        )
    ) = 0.24036834733741486
GROUP BY 
    O_CUSTKEY ;

return 0 row

Rewritten sql

SELECT  O_CUSTKEY 
FROM orders
WHERE NOT EXISTS (
    SELECT 1
    WHERE O_COMMENT <> -1801656545 OR O_COMMENT IS NULL
) = 0.24036834733741486
GROUP BY  O_CUSTKEY ;

return 101 row

Analysis

These two queries are logically equivalent, although they are written differently.

Original Query: The original query uses the IF function to check if -1801656545 is not NULL. If it is not NULL, it returns -1801656545; otherwise, it would return NULL (though in this specific query, -1801656545 is never NULL). It then uses IN to check if O_COMMENT matches the returned value and compares the result with 0.24036834733741486 to filter rows.

Rewritten Query: In the rewritten query, the NOT EXISTS subquery is used to check if O_COMMENT is not equal to -1801656545 or is NULL, which is logically equivalent to the IN check in the original query. The final result of the NOT EXISTS subquery is then compared with 0.24036834733741486 to filter the rows.

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

How to repeat

The exported file for the database is in the attachment. : (https://github.com/LLuopeiqi/newtpcd/blob/main/tidb/tpcd.sql) .

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by loading the attached tidb/tpcd.sql data and running both SQL queries against TiDB 8.3.0, comparing their result sets and the IF/IN expression behavior. Done means identifying why logically equivalent queries differ and confirming that the same case produces consistent results after the fix.

Written by the indexing model from the issue text.

Assessment

Tech stack
sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.