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

Incompatible behaviors on display width #45338

Closed
sayJason opened this issue Jul 13, 2023 · 2 comments · Fixed by #56463
Closed

Incompatible behaviors on display width #45338

sayJason opened this issue Jul 13, 2023 · 2 comments · Fixed by #56463
Labels
affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects the 5.4.x(LTS) versions. affects-6.1 This bug affects the 6.1.x(LTS) versions. affects-6.5 This bug affects the 6.5.x(LTS) versions. affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-7.5 This bug affects the 7.5.x(LTS) versions. affects-8.1 This bug affects the 8.1.x(LTS) versions. severity/major sig/execution SIG execution type/bug The issue is confirmed as a bug.

Comments

@sayJason
Copy link

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

I try to store the same value in a TINYINT column but with different display widths, TiDB returns different values.

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (c1 TINYINT(1) NOT NULL);
INSERT INTO t1 VALUES (-127);
SELECT c1 FROM t1 WHERE CAST(IFNULL(c1, '-') AS CHAR); -- {}, wrong

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (c1 TINYINT(2) NOT NULL);
INSERT INTO t1 VALUES (-127);
SELECT c1 FROM t1 WHERE CAST(IFNULL(c1, '-') AS CHAR); -- {-127}, right

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (c1 TINYINT(3) NOT NULL);
INSERT INTO t1 VALUES (-127);
SELECT c1 FROM t1 WHERE CAST(IFNULL(c1, '-') AS CHAR); -- {-127}, right

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

These three SELECTs return the same value -127.
I try the above example in MySQL, they all return -127.

3. What did you see instead (Required)

The first returns empty, while the other two return -127.

4. What is your TiDB version? (Required)

Release Version: v7.2.0
Edition: Community
Git Commit Hash: 9fd5f4a
Git Branch: heads/refs/tags/v7.2.0
UTC Build Time: 2023-06-27 15:04:42
GoVersion: go1.20.5
Race Enabled: false
Check Table Before Drop: false
Store: tikv

@sayJason sayJason added the type/bug The issue is confirmed as a bug. label Jul 13, 2023
@sayJason
Copy link
Author

I refer to MySQL Manual, which states that "The display width does not constrain the range of values that can be stored in the column." But such statement does not figure out if display width changes the evaluation of columns.

@aytrack
Copy link
Contributor

aytrack commented Jul 14, 2023

like #44786

[10:29:54]TiDB root:test> explain select CAST(IFNULL(c1, '-') AS CHAR) from t2;
+---------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
| id                  | estRows | task      | access object | operator info                                                                                                 |
+---------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
| Projection_3        | 1.00    | root      |               | cast(cast(test.t2.c1, varchar(2) BINARY CHARACTER SET utf8 COLLATE utf8_general_ci), var_string(5))->Column#3 |
| └─TableReader_5     | 1.00    | root      |               | data:TableFullScan_4                                                                                          |
|   └─TableFullScan_4 | 1.00    | cop[tikv] | table:t2      | keep order:false, stats:pseudo                                                                                |
+---------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
3 rows in set
Time: 0.004s
[10:29:57]TiDB root:test> explain select CAST(IFNULL(c1, '-') AS CHAR) from t1;
+---------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
| id                  | estRows | task      | access object | operator info                                                                                                 |
+---------------------+---------+-----------+---------------+---------------------------------------------------------------------------------------------------------------+
| Projection_3        | 1.00    | root      |               | cast(cast(test.t1.c1, varchar(1) BINARY CHARACTER SET utf8 COLLATE utf8_general_ci), var_string(5))->Column#3 |
| └─TableReader_5     | 1.00    | root      |               | data:TableFullScan_4                                                                                          |
|   └─TableFullScan_4 | 1.00    | cop[tikv] | table:t1      | keep order:false, stats:pseudo                                                                                |
+---------------------+---------+-----------+---------------+-------------------------------

@ti-chi-bot ti-chi-bot bot added may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-6.1 may-affects-6.5 may-affects-7.1 labels Jul 14, 2023
@ti-chi-bot ti-chi-bot added the affects-7.5 This bug affects the 7.5.x(LTS) versions. label Oct 23, 2023
@ti-chi-bot ti-chi-bot added the affects-8.1 This bug affects the 8.1.x(LTS) versions. label Apr 9, 2024
@dveeden dveeden added affects-6.1 This bug affects the 6.1.x(LTS) versions. affects-6.5 This bug affects the 6.5.x(LTS) versions. affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-5.2 This bug affects 5.2.x versions. affects-5.4 This bug affects the 5.4.x(LTS) versions. affects-5.3 This bug affects 5.3.x versions. and removed may-affects-6.1 may-affects-6.5 may-affects-7.1 may-affects-5.2 This bug maybe affects 5.2.x versions. may-affects-5.4 This bug maybe affects 5.4.x versions. may-affects-5.3 This bug maybe affects 5.3.x versions. labels Oct 9, 2024
@ti-chi-bot ti-chi-bot bot closed this as completed in e017e1b Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects the 5.4.x(LTS) versions. affects-6.1 This bug affects the 6.1.x(LTS) versions. affects-6.5 This bug affects the 6.5.x(LTS) versions. affects-7.1 This bug affects the 7.1.x(LTS) versions. affects-7.5 This bug affects the 7.5.x(LTS) versions. affects-8.1 This bug affects the 8.1.x(LTS) versions. severity/major sig/execution SIG execution type/bug The issue is confirmed as a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants