to_bytes silently rescales a Decimal with a negative scale
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
从 pyiceberg.conversions.to_bytes 入口点和 decimal_to_unscaled 开始,然后运行针对 DecimalType(10, 2) 提供的 Decimal 复现代码。完成标准是:不匹配的有符号 scale 被拒绝,而不是重新缩放;已经匹配类型 scale 的值仍能正确往返转换;检查对 pyiceberg/manifest.py 和 pyiceberg/io/pyarrow.py 的影响。
由索引模型根据 Issue 内容生成。
描述
to_bytes for DecimalType takes the absolute value of the exponent before comparing it
against the type's scale:
_, digits, exponent = value.as_tuple()
exponent = abs(int(exponent))
if exponent != primitive_type.scale:
raise ValueError(...)
A Decimal carries the negated scale as its exponent, so a value with a negative scale
passes this check as if it had the matching positive one. decimal_to_unscaled then uses
only the digits, and the exponent is dropped:
sign, digits, _ = value.as_tuple()
return int(Decimal((sign, digits, 0)).to_integral_value())
The value is written four orders of magnitude off, with no error.
Reproduction
from decimal import Decimal
from pyiceberg.conversions import to_bytes, from_bytes
from pyiceberg.types import DecimalType
t = DecimalType(10, 2)
print(from_bytes(t, to_bytes(t, Decimal("1E+2")))) # 0.01, expected 100.00
print(from_bytes(t, to_bytes(t, Decimal("5E+1")))) # 0.50 for decimal(10, 1) -> 0.5, expected 50.0
This is not an exotic input. Decimal.normalize() produces exactly this form:
Decimal("100").normalize() # Decimal('1E+2')
so a value that has been normalized, or that comes out of arithmetic that trims trailing
zeros, hits it.
Impact
to_bytes writes the lower_bounds and upper_bounds of a data file
(pyiceberg/manifest.py, _write_data_file_statistics), and the same conversion is used
in pyiceberg/io/pyarrow.py. A bound written as 0.01 instead of 100.00 makes scan
planning prune files that do hold matching rows, so a query silently returns fewer rows
than it should.
Values with a positive scale are unaffected: Decimal("100.00") round-trips correctly.
Suggested fix
Compare the signed scale, -exponent, against primitive_type.scale, so a mismatching
value is rejected instead of being silently rescaled. Rescaling the value to the type's
scale would be the other option, but that widens the contract of a function that today
requires an exact match.
Happy to open a PR.
- 主要语言
- Python
- 星标
- 1.1k
- 派生
- 589
- 平均合并
- 2 天 11 小时
- 30 天内合并 PR
- 75
环境准备
我们还没有检查这个项目的环境配置文件。先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apache/iceberg-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 70/100
apache/iceberg-python#4010 · 3 条评论 · 1 个 reaction ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
apache/iceberg-python#3979 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
apache/iceberg-python#3885 ·
维护者通常 1 天内回复
-
[Bug] PyArrowFileIO fails to propagate s3.ssl.ca-cert to pyarrow.fs.S3FileSystem tls_ca_file_path未关闭
难度 2/5 1-3 小时 新手友好度 76/100
apache/iceberg-python#3866 · 1 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
apache/iceberg-python#3836 · 1 条评论 ·
维护者通常 1 天内回复
查看 apache/iceberg-python 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
solana-foundation/pay-kit#341 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
nasa/python_cmr#123 ·
-
难度 1/5 1 小时以内 新手友好度 92/100
EleutherAI/lm-evaluation-harness#4243 ·
维护者通常 1 天内回复
-
area: dashboard bug perceived difficulty: 3
难度 2/5 1-3 小时 新手友好度 88/100
Nitjsefnie-Harness-Commons/daedalus#1179 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 88/100
cusp-ai-oss/tojax#17 ·