File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -59,9 +59,9 @@ def bytes_required(value: Union[int, Decimal]) -> int:
5959 int: the minimum number of bytes needed to serialize the value.
6060 """
6161 if isinstance (value , int ):
62- return (value .bit_length () + 7 ) // 8
62+ return (value .bit_length () + 8 ) // 8
6363 elif isinstance (value , Decimal ):
64- return (decimal_to_unscaled (value ).bit_length () + 7 ) // 8
64+ return (decimal_to_unscaled (value ).bit_length () + 8 ) // 8
6565
6666 raise ValueError (f"Unsupported value: { value } " )
6767
Original file line number Diff line number Diff line change 1414# KIND, either express or implied. See the License for the
1515# specific language governing permissions and limitations
1616# under the License.
17+ from decimal import Decimal
18+
1719import pytest
1820
19- from pyiceberg .utils .decimal import decimal_required_bytes
21+ from pyiceberg .utils .decimal import decimal_required_bytes , decimal_to_bytes
2022
2123
2224def test_decimal_required_bytes () -> None :
@@ -38,3 +40,10 @@ def test_decimal_required_bytes() -> None:
3840 with pytest .raises (ValueError ) as exc_info :
3941 decimal_required_bytes (precision = - 1 )
4042 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