rdblue commented on code in PR #7873:
URL: https://github.com/apache/iceberg/pull/7873#discussion_r1263081051


##########
python/pyiceberg/avro/encoder.py:
##########
@@ -0,0 +1,175 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import decimal
+import struct
+from datetime import date, datetime, time
+
+from pyiceberg.avro import STRUCT_DOUBLE, STRUCT_FLOAT
+from pyiceberg.io import OutputStream
+from pyiceberg.utils.datetime import date_to_days, datetime_to_micros, 
time_object_to_micros
+
+
+class BinaryEncoder:
+    """Write leaf values."""
+
+    _output_stream: OutputStream
+
+    def __init__(self, output_stream: OutputStream) -> None:
+        self._output_stream = output_stream
+
+    def write(self, b: bytes) -> None:
+        self._output_stream.write(b)
+
+    def write_boolean(self, boolean: bool) -> None:
+        """A boolean is written as a single byte whose value is either 0 
(false) or 1 (true).
+
+        Args:
+            boolean: The boolean to write.
+        """
+        self.write(bytearray([bool(boolean)]))
+
+    def write_int(self, integer: int) -> None:
+        """Integer and long values are written using variable-length zig-zag 
coding."""
+        datum = (integer << 1) ^ (integer >> 63)
+        while (datum & ~0x7F) != 0:
+            self.write(bytearray([(datum & 0x7F) | 0x80]))
+            datum >>= 7
+        self.write(bytearray([datum]))
+
+    def write_float(self, f: float) -> None:
+        """A float is written as 4 bytes."""
+        self.write(STRUCT_FLOAT.pack(f))
+
+    def write_double(self, f: float) -> None:
+        """A double is written as 8 bytes."""
+        self.write(STRUCT_DOUBLE.pack(f))
+
+    def write_decimal_bytes(self, datum: decimal.Decimal) -> None:
+        """
+        Decimal in bytes are encoded as long.
+
+        Since size of packed value in bytes for signed long is 8, 8 bytes are 
written.
+        """
+        sign, digits, _ = datum.as_tuple()
+
+        unscaled_datum = 0
+        for digit in digits:
+            unscaled_datum = (unscaled_datum * 10) + digit
+
+        bits_req = unscaled_datum.bit_length() + 1
+        if sign:
+            unscaled_datum = (1 << bits_req) - unscaled_datum
+
+        bytes_req = bits_req // 8
+        padding_bits = ~((1 << bits_req) - 1) if sign else 0
+        packed_bits = padding_bits | unscaled_datum
+
+        bytes_req += 1 if (bytes_req << 3) < bits_req else 0
+        self.write_int(bytes_req)
+        for index in range(bytes_req - 1, -1, -1):
+            bits_to_write = packed_bits >> (8 * index)
+            self.write(bytearray([bits_to_write & 0xFF]))
+
+    def write_decimal_fixed(self, datum: decimal.Decimal, size: int) -> None:

Review Comment:
   Yeah, here it is:
   
   ```python
           unscaled_datum = int.from_bytes(data, byteorder="big", signed=True)
           return unscaled_to_decimal(unscaled_datum, scale)
   ```
   
   Maybe there's an encoder equivalent to `int.from_bytes` that we can use to 
simplify this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to