Fokko commented on code in PR #4920: URL: https://github.com/apache/iceberg/pull/4920#discussion_r890203627
########## python/src/iceberg/avro/reader.py: ########## @@ -0,0 +1,284 @@ +# 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. +# pylint: disable=W0621 +""" +Avro reader for reading Avro files +""" +from __future__ import annotations + +import json +from dataclasses import dataclass, field +from io import SEEK_SET +from typing import ( + Any, + Dict, + List, + Type, + Union, +) + +from iceberg.avro.codec import KNOWN_CODECS, Codec, NullCodec +from iceberg.avro.decoder import BinaryDecoder +from iceberg.files import StructProtocol +from iceberg.io.base import InputFile, InputStream +from iceberg.schema import Schema, SchemaVisitor, visit +from iceberg.types import ( + BinaryType, + BooleanType, + DateType, + DecimalType, + DoubleType, + FixedType, + FloatType, + IntegerType, + ListType, + LongType, + MapType, + NestedField, + PrimitiveType, + StringType, + StructType, + TimestampType, + TimestamptzType, + TimeType, +) +from iceberg.utils.schema_conversion import AvroSchemaConversion + + +@dataclass(frozen=True) +class AvroStructProtocol(StructProtocol): + _data: List[Union[Any, StructProtocol]] = field(default_factory=list) + + def set(self, pos: int, value: Any) -> None: + self._data[pos] = value + + def get(self, pos: int) -> Any: + return self._data[pos] + + +class _AvroReader(SchemaVisitor[Union[AvroStructProtocol, Any]]): + _skip: bool = False + + def __init__(self, decoder: BinaryDecoder): + self._decoder = decoder + + def schema(self, schema: Schema, struct_result: Union[AvroStructProtocol, Any]) -> Union[AvroStructProtocol, Any]: + return struct_result + + def struct(self, struct: StructType, field_results: List[Union[AvroStructProtocol, Any]]) -> Union[AvroStructProtocol, Any]: + return AvroStructProtocol(field_results) + + def before_field(self, field: NestedField) -> None: + if field.is_optional: + pos = self._decoder.read_long() + # We now assume that null is first (which is often the case) + if int(pos) == 0: + self._skip = True + + def field(self, field: NestedField, field_result: Union[AvroStructProtocol, Any]) -> Union[AvroStructProtocol, Any]: + return field_result + + def before_list_element(self, element: NestedField) -> None: + self._skip = True + + def list(self, list_type: ListType, element_result: Union[AvroStructProtocol, Any]) -> Union[AvroStructProtocol, Any]: + read_items = [] + block_count = self._decoder.read_long() + while block_count != 0: + if block_count < 0: + block_count = -block_count + # We ignore the block size for now + _ = self._decoder.read_long() + for _ in range(block_count): + read_items.append(visit(list_type.element_type, self)) + block_count = self._decoder.read_long() + return read_items + + def before_map_key(self, key: NestedField) -> None: + self._skip = True + + def before_map_value(self, value: NestedField) -> None: + self._skip = True + + def map( + self, map_type: MapType, key_result: Union[AvroStructProtocol, Any], value_result: Union[AvroStructProtocol, Any] + ) -> Union[AvroStructProtocol, Any]: + read_items = {} + + block_count = self._decoder.read_long() + if block_count < 0: + block_count = -block_count + # We ignore the block size for now + _ = self._decoder.read_long() + + # The Iceberg non-string implementation with an array of records: + while block_count != 0: + for _ in range(block_count): + key = visit(map_type.key_type, self) + read_items[key] = visit(map_type.value_type, self) + block_count = self._decoder.read_long() + + return read_items + + def primitive(self, primitive: PrimitiveType) -> Union[AvroStructProtocol, Any]: Review Comment: A dispatch inside of a dispatch, I like it 👍🏻 -- 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]
