File tree Expand file tree Collapse file tree 2 files changed +12
-3
lines changed Expand file tree Collapse file tree 2 files changed +12
-3
lines changed Original file line number Diff line number Diff line change @@ -59,9 +59,9 @@ def bytes_required(value: Union[int, Decimal]) -> int:
59
59
int: the minimum number of bytes needed to serialize the value.
60
60
"""
61
61
if isinstance (value , int ):
62
- return (value .bit_length () + 7 ) // 8
62
+ return (value .bit_length () + 8 ) // 8
63
63
elif isinstance (value , Decimal ):
64
- return (decimal_to_unscaled (value ).bit_length () + 7 ) // 8
64
+ return (decimal_to_unscaled (value ).bit_length () + 8 ) // 8
65
65
66
66
raise ValueError (f"Unsupported value: { value } " )
67
67
Original file line number Diff line number Diff line change 14
14
# KIND, either express or implied. See the License for the
15
15
# specific language governing permissions and limitations
16
16
# under the License.
17
+ from decimal import Decimal
18
+
17
19
import pytest
18
20
19
- from pyiceberg .utils .decimal import decimal_required_bytes
21
+ from pyiceberg .utils .decimal import decimal_required_bytes , decimal_to_bytes
20
22
21
23
22
24
def test_decimal_required_bytes () -> None :
@@ -38,3 +40,10 @@ def test_decimal_required_bytes() -> None:
38
40
with pytest .raises (ValueError ) as exc_info :
39
41
decimal_required_bytes (precision = - 1 )
40
42
assert "(0, 40]" in str (exc_info .value )
43
+
44
+
45
+ def test_decimal_to_bytes () -> None :
46
+ # Check the boundary between 2 and 3 bytes.
47
+ # 2 bytes has a minimum of -32,768 and a maximum value of 32,767 (inclusive).
48
+ assert decimal_to_bytes (Decimal ('32767.' )) == b'\x7f \xff '
49
+ assert decimal_to_bytes (Decimal ('32768.' )) == b'\x00 \x80 \x00 '
You can’t perform that action at this time.
0 commit comments