expression: IN(...) AND x <> '' returns a row even though x <> '' evaluates to false
Bug Report
1. Minimal reproduce step (Required)
Tested on TiDB 8.5.8.
CREATE TABLE t(x TEXT);
INSERT INTO t VALUES ('');
SELECT
HEX(x) AS hex_x,
x IN (' ', 'a') AS in_result,
x <> '' AS ne_empty
FROM t;
SELECT COUNT(*) AS wrong_count
FROM t
WHERE x IN (' ', 'a')
AND x <> '';
SELECT COUNT(*) AS control_count
FROM t
WHERE (x = ' ' OR x = 'a')
AND x <> '';
SELECT HEX(x) AS wrong_row
FROM t
WHERE x IN (' ', 'a')
AND x <> '';
SELECT HEX(x) AS control_row
FROM t
WHERE (x = ' ' OR x = 'a')
AND x <> '';
EXPLAIN
SELECT x
FROM t
WHERE x IN (' ', 'a')
AND x <> '';The first query shows that, for the only row in the table:
x IN (' ', 'a') = 1
x <> '' = 0However, the query containing the conjunction:
x IN (' ', 'a') AND x <> ''still returns the row.
The equivalent control using explicit OR does not return the row.
2. What did you expect to see? (Required)
The predicate
x IN (' ', 'a') AND x <> ''should evaluate to FALSE for the row where x = '', because:
x <> '' = 0Therefore:
SELECT COUNT(*)
FROM t
WHERE x IN (' ', 'a')
AND x <> '';should return:
0and:
SELECT HEX(x)
FROM t
WHERE x IN (' ', 'a')
AND x <> '';should return no rows.
This is also consistent with the control query:
WHERE (x = ' ' OR x = 'a')
AND x <> ''which returns no rows.
3. What did you see instead? (Required)
TiDB incorrectly returns one row for the IN form:
wrong_count
1while the equivalent OR control returns:
control_count
0Likewise:
SELECT HEX(x)
FROM t
WHERE x IN (' ', 'a')
AND x <> '';returns the row with the empty string, whereas the OR form returns no rows.
This is inconsistent with evaluating the individual predicates directly, where x <> '' is FALSE.
The behavior appears to be specific to the IN expression combined with the additional conjunct under the string collation semantics.
4. What is your TiDB version? (Required)
TiDB v8.5.8Source: pingcap/tidb