#71700·tidb

expression: IN(...) AND x <> '' returns a row even though x <> '' evaluates to false

Author: AnnCallaCreated Sep 29, 2026Updated Sep 29, 2026
Labelstype/bugcontributionsig/executionseverity/criticalfirst-time-contributormay-affects-7.5may-affects-8.1may-affects-8.5

Bug Report

1. Minimal reproduce step (Required)

Tested on TiDB 8.5.8.

sql
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 <> ''          = 0

However, the query containing the conjunction:

sql
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

sql
x IN (' ', 'a') AND x <> ''

should evaluate to FALSE for the row where x = '', because:

x <> '' = 0

Therefore:

sql
SELECT COUNT(*)
FROM t
WHERE x IN (' ', 'a')
  AND x <> '';

should return:

0

and:

sql
SELECT HEX(x)
FROM t
WHERE x IN (' ', 'a')
  AND x <> '';

should return no rows.

This is also consistent with the control query:

sql
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
1

while the equivalent OR control returns:

control_count
0

Likewise:

sql
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.8