This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-4384-a385453ae11a19eb426d0a0fa51afc6bb1015c4f in repository https://gitbox.apache.org/repos/asf/iceberg-python.git
commit ab0afe71c07ecb15d274cf1554ebb56e1745da75 Author: zn-danielbariudin <[email protected]> AuthorDate: Tue Nov 17 20:41:25 2026 +0000 Avro: Cache the Avro to Iceberg schema conversion per schema string (#4384) * Avro: Cache the Avro to Iceberg schema conversion per schema string * Avro: Cap the schema conversion cacher at 32 enteries --- pyiceberg/avro/file.py | 10 +++++++--- tests/avro/test_file.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pyiceberg/avro/file.py b/pyiceberg/avro/file.py index 4b23c30ac..8a0f39a06 100644 --- a/pyiceberg/avro/file.py +++ b/pyiceberg/avro/file.py @@ -25,6 +25,7 @@ import os from collections.abc import Callable from dataclasses import dataclass from enum import Enum +from functools import lru_cache from types import TracebackType from typing import ( Generic, @@ -71,6 +72,11 @@ META_SCHEMA = StructType( _SCHEMA_KEY = "avro.schema" +@lru_cache(maxsize=32) +def _avro_schema_string_to_iceberg(avro_schema_string: str) -> Schema: + return AvroSchemaConversion().avro_to_iceberg(json.loads(avro_schema_string)) + + class AvroFileHeader(Record): @property def magic(self) -> bytes: @@ -99,9 +105,7 @@ class AvroFileHeader(Record): def get_schema(self) -> Schema: if _SCHEMA_KEY in self.meta: - avro_schema_string = self.meta[_SCHEMA_KEY] - avro_schema = json.loads(avro_schema_string) - return AvroSchemaConversion().avro_to_iceberg(avro_schema) + return _avro_schema_string_to_iceberg(self.meta[_SCHEMA_KEY]) else: raise ValueError("No schema found in Avro file headers") diff --git a/tests/avro/test_file.py b/tests/avro/test_file.py index d0598c8b8..595fceb58 100644 --- a/tests/avro/test_file.py +++ b/tests/avro/test_file.py @@ -99,6 +99,28 @@ def test_missing_schema() -> None: assert "No schema found in Avro file headers" in str(exc_info.value) +def test_get_schema_reuses_conversion_for_identical_schema_strings() -> None: + schema_string = ( + '{"type": "record", "name": "manifest_entry", "fields": [' + '{"name": "status", "type": "int", "field-id": 0}, ' + '{"name": "snapshot_id", "type": ["null", "long"], "default": null, "field-id": 1}]}' + ) + other_schema_string = ( + '{"type": "record", "name": "manifest_entry", "fields": [{"name": "status", "type": "int", "field-id": 0}]}' + ) + + header = AvroFileHeader(bytes(0), {"avro.schema": schema_string}, bytes(16)) + same_schema_header = AvroFileHeader(bytes(0), {"avro.schema": schema_string}, bytes(16)) + other_schema_header = AvroFileHeader(bytes(0), {"avro.schema": other_schema_string}, bytes(16)) + + assert header.get_schema() == Schema( + NestedField(field_id=0, name="status", field_type=IntegerType(), required=True), + NestedField(field_id=1, name="snapshot_id", field_type=LongType(), required=False), + ) + assert header.get_schema() is same_schema_header.get_schema() + assert other_schema_header.get_schema() is not header.get_schema() + + # helper function to serialize our objects to dicts to enable # direct comparison with the dicts returned by fastavro def todict(obj: Any) -> Any:
