to_bytes silently rescales a Decimal with a negative scale
まだ誰も着手していません。
評価
調査の方向性
pyiceberg.conversions.to_bytes のエントリポイントと decimal_to_unscaled から始め、続いて DecimalType(10, 2) 用に提供された Decimal の再現コードを実行します。完了条件は、一致しない符号付きスケールが再スケーリングされずに拒否される一方で、型のスケールにすでに一致している値は引き続き正しくラウンドトリップすることです。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日 4時間
- マージ済み PR(30日)
- 72
コントリビューションガイド
このリポジトリのコントリビューションガイドは索引されていません
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
apache/iceberg-python のほかの issue
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
apache/iceberg-python#3979 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/iceberg-python#3885 ·
-
[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 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/iceberg-python#3836 · コメント 1 件 ·
-
Cannot connect to a Hive Metastore with Kerberos when the principal's host differs from the URI host オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
apache/iceberg-python#3787 ·
apache/iceberg-python の issue をすべて見る
似ている issue
-
triage/confirmed
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
agentscope-ai/agentscope#2775 ·
-
comp/desktop P3 type/bug
難易度 1/5 1時間未満 初心者へのやさしさ 92/100
NousResearch/hermes-agent#118866 ·
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
apache/cloudstack#14222 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100