File size: 13,077 Bytes
f0f4f2b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# 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=W0212
from typing import Any, Dict
import pytest
from pyiceberg.schema import Schema
from pyiceberg.types import (
BinaryType,
BooleanType,
DateType,
DecimalType,
FixedType,
IntegerType,
ListType,
LongType,
MapType,
NestedField,
StringType,
StructType,
)
from pyiceberg.utils.schema_conversion import AvroSchemaConversion
def test_avro_to_iceberg(avro_schema_manifest_file_v1: Dict[str, Any]) -> None:
iceberg_schema = AvroSchemaConversion().avro_to_iceberg(avro_schema_manifest_file_v1)
expected_iceberg_schema = Schema(
NestedField(
field_id=500, name="manifest_path", field_type=StringType(), required=True, doc="Location URI with FS scheme"
),
NestedField(field_id=501, name="manifest_length", field_type=LongType(), required=True, doc="Total file size in bytes"),
NestedField(field_id=502, name="partition_spec_id", field_type=IntegerType(), required=True, doc="Spec ID used to write"),
NestedField(
field_id=503,
name="added_snapshot_id",
field_type=LongType(),
required=False,
doc="Snapshot ID that added the manifest",
),
NestedField(
field_id=504, name="added_data_files_count", field_type=IntegerType(), required=False, doc="Added entry count"
),
NestedField(
field_id=505, name="existing_data_files_count", field_type=IntegerType(), required=False, doc="Existing entry count"
),
NestedField(
field_id=506, name="deleted_data_files_count", field_type=IntegerType(), required=False, doc="Deleted entry count"
),
NestedField(
field_id=507,
name="partitions",
field_type=ListType(
element_id=508,
element_type=StructType(
NestedField(
field_id=509,
name="contains_null",
field_type=BooleanType(),
required=True,
doc="True if any file has a null partition value",
),
NestedField(
field_id=518,
name="contains_nan",
field_type=BooleanType(),
required=False,
doc="True if any file has a nan partition value",
),
NestedField(
field_id=510,
name="lower_bound",
field_type=BinaryType(),
required=False,
doc="Partition lower bound for all files",
),
NestedField(
field_id=511,
name="upper_bound",
field_type=BinaryType(),
required=False,
doc="Partition upper bound for all files",
),
),
element_required=True,
),
required=False,
doc="Summary for each partition",
),
NestedField(field_id=512, name="added_rows_count", field_type=LongType(), required=False, doc="Added rows count"),
NestedField(field_id=513, name="existing_rows_count", field_type=LongType(), required=False, doc="Existing rows count"),
NestedField(field_id=514, name="deleted_rows_count", field_type=LongType(), required=False, doc="Deleted rows count"),
schema_id=1,
identifier_field_ids=[],
)
assert iceberg_schema == expected_iceberg_schema
def test_avro_list_required_primitive() -> None:
avro_schema = {
"type": "record",
"name": "avro_schema",
"fields": [
{
"name": "array_with_string",
"type": {
"type": "array",
"items": "string",
"default": [],
"element-id": 101,
},
"field-id": 100,
},
],
}
expected_iceberg_schema = Schema(
NestedField(
field_id=100,
name="array_with_string",
field_type=ListType(element_id=101, element_type=StringType(), element_required=True),
required=True,
),
schema_id=1,
)
iceberg_schema = AvroSchemaConversion().avro_to_iceberg(avro_schema)
assert expected_iceberg_schema == iceberg_schema
def test_avro_list_wrapped_primitive() -> None:
avro_schema = {
"type": "record",
"name": "avro_schema",
"fields": [
{
"name": "array_with_string",
"type": {
"type": "array",
"items": {"type": "string"},
"default": [],
"element-id": 101,
},
"field-id": 100,
},
],
}
expected_iceberg_schema = Schema(
NestedField(
field_id=100,
name="array_with_string",
field_type=ListType(element_id=101, element_type=StringType(), element_required=True),
required=True,
),
schema_id=1,
)
iceberg_schema = AvroSchemaConversion().avro_to_iceberg(avro_schema)
assert expected_iceberg_schema == iceberg_schema
def test_avro_list_required_record() -> None:
avro_schema = {
"type": "record",
"name": "avro_schema",
"fields": [
{
"name": "array_with_record",
"type": {
"type": "array",
"items": {
"type": "record",
"name": "r101",
"fields": [
{
"name": "contains_null",
"type": "boolean",
"field-id": 102,
},
{
"name": "contains_nan",
"type": ["null", "boolean"],
"field-id": 103,
},
],
},
"element-id": 101,
},
"field-id": 100,
}
],
}
expected_iceberg_schema = Schema(
NestedField(
field_id=100,
name="array_with_record",
field_type=ListType(
element_id=101,
element_type=StructType(
NestedField(field_id=102, name="contains_null", field_type=BooleanType(), required=True),
NestedField(field_id=103, name="contains_nan", field_type=BooleanType(), required=False),
),
element_required=True,
),
required=True,
),
schema_id=1,
identifier_field_ids=[],
)
iceberg_schema = AvroSchemaConversion().avro_to_iceberg(avro_schema)
assert expected_iceberg_schema == iceberg_schema
def test_resolve_union() -> None:
with pytest.raises(TypeError) as exc_info:
AvroSchemaConversion()._resolve_union(["null", "string", "long"])
assert "Non-optional types aren't part of the Iceberg specification" in str(exc_info.value)
def test_nested_type() -> None:
# In the case a primitive field is nested
assert AvroSchemaConversion()._convert_schema({"type": {"type": "string"}}) == StringType()
def test_map_type() -> None:
avro_type = {
"type": "map",
"values": ["null", "long"],
"key-id": 101,
"value-id": 102,
}
actual = AvroSchemaConversion()._convert_schema(avro_type)
expected = MapType(key_id=101, key_type=StringType(), value_id=102, value_type=LongType(), value_required=False)
assert actual == expected
def test_fixed_type() -> None:
avro_type = {"type": "fixed", "size": 22}
actual = AvroSchemaConversion()._convert_schema(avro_type)
expected = FixedType(22)
assert actual == expected
def test_unknown_primitive() -> None:
with pytest.raises(TypeError) as exc_info:
avro_type = "UnknownType"
AvroSchemaConversion()._convert_schema(avro_type)
assert "Unknown type: UnknownType" in str(exc_info.value)
def test_unknown_complex_type() -> None:
with pytest.raises(TypeError) as exc_info:
avro_type = {
"type": "UnknownType",
}
AvroSchemaConversion()._convert_schema(avro_type)
assert "Unknown type: {'type': 'UnknownType'}" in str(exc_info.value)
def test_convert_field_without_field_id() -> None:
with pytest.raises(ValueError) as exc_info:
avro_field = {
"name": "contains_null",
"type": "boolean",
}
AvroSchemaConversion()._convert_field(avro_field)
assert "Cannot convert field, missing field-id" in str(exc_info.value)
def test_convert_record_type_without_record() -> None:
with pytest.raises(ValueError) as exc_info:
avro_field = {"type": "non-record", "name": "avro_schema", "fields": []}
AvroSchemaConversion()._convert_record_type(avro_field)
assert "Expected record type, got" in str(exc_info.value)
def test_avro_list_missing_element_id() -> None:
avro_type = {
"name": "array_with_string",
"type": {
"type": "array",
"items": "string",
"default": [],
# "element-id": 101,
},
"field-id": 100,
}
with pytest.raises(ValueError) as exc_info:
AvroSchemaConversion()._convert_array_type(avro_type)
assert "Cannot convert array-type, missing element-id:" in str(exc_info.value)
def test_convert_decimal_type() -> None:
avro_decimal_type = {"type": "bytes", "logicalType": "decimal", "precision": 19, "scale": 25}
actual = AvroSchemaConversion()._convert_logical_type(avro_decimal_type)
expected = DecimalType(precision=19, scale=25)
assert actual == expected
def test_convert_date_type() -> None:
avro_logical_type = {"type": "int", "logicalType": "date"}
actual = AvroSchemaConversion()._convert_logical_type(avro_logical_type)
assert actual == DateType()
def test_unknown_logical_type() -> None:
"""Test raising a ValueError when converting an unknown logical type as part of an Avro schema conversion"""
avro_logical_type = {"type": "bytes", "logicalType": "date"}
with pytest.raises(ValueError) as exc_info:
AvroSchemaConversion()._convert_logical_type(avro_logical_type)
assert "Unknown logical/physical type combination:" in str(exc_info.value)
def test_logical_map_with_invalid_fields() -> None:
avro_type = {
"type": "array",
"logicalType": "map",
"items": {
"type": "record",
"name": "k101_v102",
"fields": [
{"name": "key", "type": "int", "field-id": 101},
{"name": "value", "type": "string", "field-id": 102},
{"name": "other", "type": "bytes", "field-id": 103},
],
},
}
with pytest.raises(ValueError) as exc_info:
AvroSchemaConversion()._convert_logical_map_type(avro_type)
assert "Invalid key-value pair schema:" in str(exc_info.value)
def test_iceberg_to_avro_manifest_list(avro_schema_manifest_file_v1: Dict[str, Any]) -> None:
"""Round trip the manifest list"""
iceberg_schema = AvroSchemaConversion().avro_to_iceberg(avro_schema_manifest_file_v1)
avro_result = AvroSchemaConversion().iceberg_to_avro(iceberg_schema, schema_name="manifest_file")
assert avro_schema_manifest_file_v1 == avro_result
def test_iceberg_to_avro_manifest(avro_schema_manifest_entry: Dict[str, Any]) -> None:
"""Round trip the manifest itself"""
iceberg_schema = AvroSchemaConversion().avro_to_iceberg(avro_schema_manifest_entry)
avro_result = AvroSchemaConversion().iceberg_to_avro(iceberg_schema, schema_name="manifest_entry")
assert avro_schema_manifest_entry == avro_result
|