Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug Report: Issues with IF function #19475

Open
LLuopeiqi opened this issue Nov 21, 2024 · 0 comments
Open

Bug Report: Issues with IF function #19475

LLuopeiqi opened this issue Nov 21, 2024 · 0 comments

Comments

@LLuopeiqi
Copy link

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) .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant