pingcap / pingcap/tidb

Unexpected results when `CASE` and `IN` use together

Open
#43,624 1 comment 0 reactions 0 assignees View on GitHub
fuzz/sqlancer severity/moderate sig/execution type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

Consider the following program:
```
CREATE TABLE t0(c0 BOOL);

INSERT INTO t0 VALUES (true);

SELECT t0.c0 FROM t0 WHERE (((CASE (((CASE t0.c0 WHEN 6 THEN 0.03 ELSE t0.c0 END ))LIKE(t0.c0)) WHEN t0.c0 THEN 1 END ))AND( t0.c0 IN ((1)))); -- empty result

SELECT t0.c0 FROM t0 WHERE (((CASE (((CASE t0.c0 WHEN 6 THEN 0.03 ELSE t0.c0 END ))LIKE(t0.c0)) WHEN t0.c0 THEN 1 END ))AND( t0.c0 IN (SELECT t0.c0 FROM t0))); -- 1
```
These two queries are equivalent, because I run query `SELECT t0.c0 FROM t0;` and get result `1`. But the first query returns an empty result, I think it is unexpected.

For `CASE t0.c0 WHEN 6 THEN 0.03 ELSE t0.c0 END`, I tried `SELECT CASE t0.c0 WHEN 6 THEN 0.03 ELSE t0.c0 END FROM t0;` and get result `1`, then I replace this expression with `1` and get query `SELECT t0.c0 FROM t0 WHERE (((CASE (((1 ))LIKE(t0.c0)) WHEN t0.c0 THEN 1 END ))AND( t0.c0 IN ((1))));`. This query also returns `1`.

For `t0.c0 IN (1)`, I tried `SELECT t0.c0 IN (1) FROM t0;` and got result `1`. So the first query should not have an empty result.

### 2. What did you expect to see? (Required)
The first `SELECT` query returns `1`

### 3. What did you see instead (Required)
The first `SELECT` query returns an empty result.

### 4. What is your TiDB version? (Required)

```
Release Version: v7.2.0-alpha-108-gfd438de6a\nEdition: Community\nGit Commit Hash: fd438de6a89ec33ae7d6e3f2350a83939ebe172c\nGit Branch: master\nUTC Build Time: 2023-05-08 15:05:47\nGoVersion: go1.20.3\nRace Enabled: false\nTiKV Min Version: 6.2.0-alpha\nCheck Table Before Drop: false\nStore: unistore
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.