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

Shrinking decimal or double field precision behaves differently #41281

Closed
jacktd9 opened this issue Feb 10, 2023 · 0 comments · Fixed by #41555
Closed

Shrinking decimal or double field precision behaves differently #41281

jacktd9 opened this issue Feb 10, 2023 · 0 comments · Fixed by #41555
Assignees
Labels
affects-5.1 This bug affects 5.1.x versions. 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 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 affects-6.6 severity/major sig/sql-infra SIG: SQL Infra type/bug This issue is a bug.

Comments

@jacktd9
Copy link

jacktd9 commented Feb 10, 2023

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

We found that TiDB has different behavior reducing the precision of DECIMAL and DOUBLE after ALTER TABLE MODIFY/CHANGE COLUMN syntax:

  • For DOUBLE types, the new data will be changed but the old data will be kept the same. For example:
alter table r1 modify column `d` double(4,1) NOT NULL COMMENT '';

2、For DECIMAL types, both new and old data will be changed. For example:

alter table r1 modify column `a` decimal(20,2) DEFAULT '0.000000';

However, in MySQL, the new and old data will be always changed for DOUBLE and DECIMAL types.

MySQL [test]> 
CREATE TABLE `r1` (
      `id` int(11) NOT NULL COMMENT '',
      `a` **DECIMAL(20,6)** DEFAULT '0.000000' COMMENT '',
      `b` varchar(32) DEFAULT NULL COMMENT '',
      `d` **double(4,2)** NOT NULL COMMENT '',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
Query OK, 0 rows affected (0.56 sec)
MySQL [test]> insert into r1 values(1, 1.111111, '111', 11.11);
Query OK, 1 row affected (0.05 sec)
MySQL [test]> insert into r1 values(2, 2.222222, '222', 22.22);
Query OK, 1 row affected (0.05 sec)
MySQL [test]> select * from r1;
+----+----------+------+-------+
| id | a        | b    | d     |
+----+----------+------+-------+
|  1 | 1.111111 | 111  | 11.11 |
|  2 | 2.222222 | 222  | 22.22 |
+----+----------+------+-------+
2 rows in set (0.04 sec)
MySQL [test]> alter table r1 modify column `d` double(4,1) NOT NULL COMMENT '';
Query OK, 0 rows affected (0.55 sec)
MySQL [test]> select * from r1;
+----+----------+------+-------+
| id | a        | b    | d     |
+----+----------+------+-------+
|  1 | 1.111111 | 111  | **11.11** |
|  2 | 2.222222 | 222  | **22.22** |
+----+----------+------+-------+
2 rows in set (0.05 sec)
MySQL [test]> show create table r1\G
*************************** 1. row ***************************
       Table: r1
Create Table: CREATE TABLE `r1` (
  `id` int(11) NOT NULL,
  `a` decimal(20,6) DEFAULT '0.000000',
  `b` varchar(32) DEFAULT NULL,
  `d` double(4,1) NOT NULL,
  PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
1 row in set (0.05 sec)
MySQL [test]> insert into r1 values(3, 3.333333, '333', 33.33);
Query OK, 1 row affected (0.05 sec)
mysql>  select * from r1;
+----+----------+------+-------+
| id | a        | b    | d     |
+----+----------+------+-------+
|  1 | 1.111111 | 111  | 11.11 |
|  2 | 2.222222 | 222  | 22.22 |
|  3 | 3.333333 | 333  |  **33.3** |
+----+----------+------+-------+
3 rows in set (0.00 sec)
MySQL [test]> alter table r1 modify column `a` **decimal(20,2)** DEFAULT '0.000000';
Query OK, 0 rows affected, 3 warnings (3.05 sec)
mysql> show warnings;
+---------+------+-----------------------------------------------+
| Level   | Code | Message                                       |
+---------+------+-----------------------------------------------+
| Warning | 1292 | Truncated incorrect DECIMAL value: '1.111111' |
| Warning | 1292 | Truncated incorrect DECIMAL value: '1.111111' |
| Warning | 1292 | Truncated incorrect DECIMAL value: '1.111111' |
+---------+------+-----------------------------------------------+
3 rows in set (0.00 sec)
MySQL [test]> select * from r1;
+----+------+------+-------+
| id | a    | b    | d     |
+----+------+------+-------+
|  1 | **1.11** | 111  | 11.11 |
|  2 | **2.22** | 222  | 22.22 |
|  3 | **3.33** | 333  |  33.3 |
+----+------+------+-------+
3 rows in set (0.01 sec)

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

Same behavior as mysql

MySQL [test]> select * from r1;
+----+------+------+------+
| id | a    | b    | d    |
+----+------+------+------+
|  1 | 1.11 | 111  | 11.1 |
|  2 | 2.22 | 222  | 22.2 |
|  3 | 3.33 | 333  | 33.3 |
+----+------+------+------+
3 rows in set (0.00 sec)

3. What did you see instead (Required)

MySQL [test]> select * from r1;
+----+------+------+-------+
| id | a    | b    | d     |
+----+------+------+-------+
|  1 | 1.11 | 111  | **11.11** |
|  2 | 2.22 | 222  | **22.22** |
|  3 | 3.33 | 333  |  33.3 |
+----+------+------+-------+
3 rows in set (0.01 sec)

4. What is your TiDB version? (Required)

v5.4.1 & 6.5.0

@jacktd9 jacktd9 added the type/bug This issue is a bug. label Feb 10, 2023
@ti-chi-bot ti-chi-bot added may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.0 This bug maybe affects 5.0.x versions. may-affects-5.1 This bug maybe affects 5.1.x versions. 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.0 may-affects-6.1 may-affects-6.2 may-affects-6.3 may-affects-6.4 may-affects-6.5 may-affects-6.6 labels Feb 11, 2023
@zimulala zimulala removed may-affects-4.0 This bug maybe affects 4.0.x versions. may-affects-5.1 This bug maybe affects 5.1.x versions. 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-5.0 This bug maybe affects 5.0.x versions. may-affects-6.0 may-affects-6.1 may-affects-6.2 may-affects-6.3 may-affects-6.4 may-affects-6.5 labels Feb 17, 2023
@zimulala zimulala added affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 affects-6.6 affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. and removed may-affects-6.6 labels Feb 17, 2023
ti-chi-bot pushed a commit that referenced this issue Feb 20, 2023
…n operations when the number of decimal places is reduced. (#41555)

close #41281
ti-chi-bot added a commit that referenced this issue Mar 14, 2023
…n operations when the number of decimal places is reduced. (#41555) (#41592)

close #41281
ti-chi-bot added a commit that referenced this issue Mar 18, 2023
…n operations when the number of decimal places is reduced. (#41555) (#41593)

close #41281
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.1 This bug affects 5.1.x versions. 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 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 affects-6.6 severity/major sig/sql-infra SIG: SQL Infra type/bug This issue is a bug.
Projects
None yet
4 participants