pingcap / tidb

TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
https://pingcap.com
Apache License 2.0
37.27k stars 5.85k forks source link

Unexpected result when comparing `TIMESTAMP` null values #56294

Open suyZhong opened 1 month ago

suyZhong commented 1 month ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

DROP DATABASE IF EXISTS db0;
CREATE DATABASE db0;
USE db0;

CREATE  TABLE  t0(c0 INT, c1 TIMESTAMP);
INSERT INTO t0(c0) VALUES (1);
CREATE INDEX t0i0 ON t0(c1 , c0); -- necessary to trigger the bug

SELECT t0.c0 FROM t0; -- 1

SELECT t0.c0 FROM t0 WHERE (t0.c1<>(CAST(1 AS YEAR))); -- 1 (unexpected)
SELECT t0.c0 FROM t0 WHERE (((t0.c1<>(CAST(1 AS YEAR)))) IS NULL); -- 1

2. What did you expect to see? (Required)

As t0.c1 is NULL, then the expression t0.c1<>(CAST(1 AS YEAR)) should also be evaluated to NULL, and thus this query should return no row. However, 1 is returned. Have tried to search past issues but haven't found a similar one. Kindly lemme know if it's a duplicate.

MySQL 8.0 works well on it.

3. What did you see instead (Required)

As mentioned above.

4. What is your TiDB version? (Required)

elect tidb_version();
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tidb_version()                                                                                                                                                                                                                                                       |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Release Version: v8.4.0-alpha-244-ga5e07a2
Edition: Community
Git Commit Hash: a5e07a2ed360f29216c912775ce482f536f4102b
Git Branch: HEAD
UTC Build Time: 2024-09-25 05:10:21
GoVersion: go1.21.13
Race Enabled: false
Check Table Before Drop: false
Store: unistore |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
suyZhong commented 1 month ago

/label fuzz/sqlancer

joechenrh commented 1 month ago

20240926-141635 Seems like the built key range is strange. (EncodeIndexKey omits the error from EncodeKey).

windtalker commented 1 month ago

Change the label to sig/planner since it is caused by build key range.