Fokko commented on code in PR #8067:
URL: https://github.com/apache/iceberg/pull/8067#discussion_r1279780654
##########
python/pyiceberg/avro/encoder.py:
##########
@@ -64,112 +65,40 @@ def write_decimal_bytes(self, datum: decimal.Decimal) ->
None:
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]))
+ unscaled_datum = decimal_to_unscaled(datum)
+ size = (unscaled_datum.bit_length() + 7) // 8
+ bytes_datum = unscaled_datum.to_bytes(length=size, byteorder="big",
signed=True)
+ self.write_bytes(bytes_datum)
def write_decimal_fixed(self, datum: decimal.Decimal, size: int) -> None:
"""Decimal in fixed are encoded as size of fixed bytes."""
- 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
- size_in_bits = size * 8
- offset_bits = size_in_bits - bits_req
-
- mask = 2**size_in_bits - 1
- bit = 1
- for _ in range(bits_req):
- mask ^= bit
- bit <<= 1
-
- if bits_req < 8:
- bytes_req = 1
- else:
- bytes_req = bits_req // 8
- if bits_req % 8 != 0:
- bytes_req += 1
- if sign:
- unscaled_datum = (1 << bits_req) - unscaled_datum
- unscaled_datum = mask | unscaled_datum
- for index in range(size - 1, -1, -1):
- bits_to_write = unscaled_datum >> (8 * index)
- self.write(bytearray([bits_to_write & 0xFF]))
- else:
- for _ in range(offset_bits // 8):
- self.write(b"\x00")
- for index in range(bytes_req - 1, -1, -1):
- bits_to_write = unscaled_datum >> (8 * index)
- self.write(bytearray([bits_to_write & 0xFF]))
+ unscaled_datum = decimal_to_unscaled(datum)
+ bytes_datum = unscaled_datum.to_bytes(length=size, byteorder="big",
signed=True)
+ self.write(bytes_datum)
def write_bytes(self, b: bytes) -> None:
"""Bytes are encoded as a long followed by that many bytes of data."""
self.write_int(len(b))
- self.write(struct.pack(f"{len(b)}s", b))
-
- def write_bytes_fixed(self, b: bytes) -> None:
- """Writes fixed number of bytes."""
- self.write(struct.pack(f"{len(b)}s", b))
+ self.write(b)
def write_utf8(self, s: str) -> None:
"""A string is encoded as a long followed by that many bytes of UTF-8
encoded character data."""
self.write_bytes(s.encode("utf-8"))
- def write_date_int(self, d: date) -> None:
- """
- Encode python date object as int.
-
- It stores the number of days from the unix epoch, 1 January 1970 (ISO
calendar).
- """
- self.write_int(date_to_days(d))
-
- def write_time_millis_int(self, dt: time) -> None:
- """
- Encode python time object as int.
+ def write_uuid(self, uuid: UUID) -> None:
+ """Write UUID as a fixed[16].
- It stores the number of milliseconds from midnight, 00:00:00.000
+ The uuid logical type represents a random generated universally unique
identifier (UUID).
+ An uuid logical type annotates an Avro string. The string has to
conform with RFC-4122.
"""
- self.write_int(int(time_object_to_micros(dt) / 1000))
+ if len(uuid.bytes) != 16:
+ raise ValueError(f"Expected UUID to have 16 bytes, got:
len({uuid.bytes!r})")
+ return self.write(uuid.bytes)
- def write_time_micros_long(self, dt: time) -> None:
+ def write_time_micros(self, dt: time) -> None:
Review Comment:
I agree, this should be handled by the writer, not the encoder. Another one
to clean up, less is more :)
--
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]