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

[ bug ]decimal超过16位会自动四舍五入 [ 已修复 ] #1308

Closed
ocpeng opened this issue Dec 29, 2021 · 3 comments
Closed

[ bug ]decimal超过16位会自动四舍五入 [ 已修复 ] #1308

ocpeng opened this issue Dec 29, 2021 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@ocpeng
Copy link
Contributor

ocpeng commented Dec 29, 2021

bug描述

SQL查询中,decimal超过16位,会自动四舍五入
例如我的数据类型是decimal,真实数据是173000003740634298,archery查询返回173000003740634300

bug复现

CREATE TABLE t1 (
id int(11) NOT NULL AUTO_INCREMENT,
phone varchar(13) DEFAULT NULL,
curdate datetime DEFAULT NULL,
pid decimal(18,0) DEFAULT NULL,
pid1 bigint DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8

mysql> select * from t1;
+----+--------------+---------------------+--------------------+--------------------+
| id | phone | curdate | pid | pid2 |
+----+--------------+---------------------+--------------------+--------------------+
| 1 | 18611130824 | NULL | 173000003740634298 | 173000003740634298 |
.....
| 19 | 186111130824 | 2021-12-07 22:52:27 | 173000003740634298 | 173000003740634298 |
+----+--------------+---------------------+--------------------+--------------------+
17 rows in set (0.00 sec)`

archery 查询返回:
image
捕获日志:
image

问题分析

可以看到,
真实数据返回,pid字段返回decimal,序列化之后返回int
而pid2字段返回int,序列化之后返回str
原因是js遇到2**53也就是超过16位时候会自动四舍五入,原始代码只对int做了str处理,没有对decimal处理,原始代码:sql/query.py
return HttpResponse(json.dumps(result, cls=ExtendJSONEncoderFTime, bigint_as_string=True),

解决方案

加入对decimal处理,如下解决:
return HttpResponse(json.dumps(result, cls=ExtendJSONEncoderFTime, bigint_as_string=True, use_decimal=False),

@ocpeng ocpeng changed the title [ bug ]decimal超过16位会自动四舍五入,已修复 [ bug ]decimal超过16位会自动四舍五入 [ 已修复 ] Dec 29, 2021
@LeoQuote
Copy link
Collaborator

你好,代码方便提一个pr吗

@hhyo
Copy link
Owner

hhyo commented Dec 30, 2021

#1250

@hhyo hhyo added the duplicate This issue or pull request already exists label Dec 30, 2021
@ocpeng
Copy link
Contributor Author

ocpeng commented Dec 30, 2021

作者已经在v1.8.2修复。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants