Skip to content

Commit

Permalink
Deserialize initial-default and write-default (#1432)
Browse files Browse the repository at this point in the history
Ensures that these attributes are correctly applied to the NestedField when reading an Iceberg schema json file.
  • Loading branch information
paulcichonski authored Dec 17, 2024
1 parent 54b08ee commit b0ea716
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
4 changes: 2 additions & 2 deletions pyiceberg/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ def __init__(
data["type"] = data["type"] if "type" in data else field_type
data["required"] = required
data["doc"] = doc
data["initial-default"] = initial_default
data["write-default"] = write_default
data["initial-default"] = data["initial-default"] if "initial-default" in data else initial_default
data["write-default"] = data["write-default"] if "write-default" in data else write_default
super().__init__(**data)

def __str__(self) -> str:
Expand Down
29 changes: 29 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,35 @@ def table_schema_simple() -> Schema:
)


@pytest.fixture(scope="session")
def table_schema_with_full_nested_fields() -> Schema:
return schema.Schema(
NestedField(
field_id=1,
name="foo",
field_type=StringType(),
required=False,
doc="foo doc",
initial_default="foo initial",
write_default="foo write",
),
NestedField(
field_id=2, name="bar", field_type=IntegerType(), required=True, doc="bar doc", initial_default=42, write_default=43
),
NestedField(
field_id=3,
name="baz",
field_type=BooleanType(),
required=False,
doc="baz doc",
initial_default=True,
write_default=False,
),
schema_id=1,
identifier_field_ids=[2],
)


@pytest.fixture(scope="session")
def table_schema_nested() -> Schema:
return schema.Schema(
Expand Down
12 changes: 6 additions & 6 deletions tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,17 +421,17 @@ def __getitem__(self, pos: int) -> Any:
assert inner_accessor.get(container) == "name"


def test_serialize_schema(table_schema_simple: Schema) -> None:
actual = table_schema_simple.model_dump_json()
expected = """{"type":"struct","fields":[{"id":1,"name":"foo","type":"string","required":false},{"id":2,"name":"bar","type":"int","required":true},{"id":3,"name":"baz","type":"boolean","required":false}],"schema-id":1,"identifier-field-ids":[2]}"""
def test_serialize_schema(table_schema_with_full_nested_fields: Schema) -> None:
actual = table_schema_with_full_nested_fields.model_dump_json()
expected = """{"type":"struct","fields":[{"id":1,"name":"foo","type":"string","required":false,"doc":"foo doc","initial-default":"foo initial","write-default":"foo write"},{"id":2,"name":"bar","type":"int","required":true,"doc":"bar doc","initial-default":42,"write-default":43},{"id":3,"name":"baz","type":"boolean","required":false,"doc":"baz doc","initial-default":true,"write-default":false}],"schema-id":1,"identifier-field-ids":[2]}"""
assert actual == expected


def test_deserialize_schema(table_schema_simple: Schema) -> None:
def test_deserialize_schema(table_schema_with_full_nested_fields: Schema) -> None:
actual = Schema.model_validate_json(
"""{"type": "struct", "fields": [{"id": 1, "name": "foo", "type": "string", "required": false}, {"id": 2, "name": "bar", "type": "int", "required": true}, {"id": 3, "name": "baz", "type": "boolean", "required": false}], "schema-id": 1, "identifier-field-ids": [2]}"""
"""{"type": "struct", "fields": [{"id": 1, "name": "foo", "type": "string", "required": false, "doc": "foo doc", "initial-default": "foo initial", "write-default": "foo write"}, {"id": 2, "name": "bar", "type": "int", "required": true, "doc": "bar doc", "initial-default": 42, "write-default": 43}, {"id": 3, "name": "baz", "type": "boolean", "required": false, "doc": "baz doc", "initial-default": true, "write-default": false}], "schema-id": 1, "identifier-field-ids": [2]}"""
)
expected = table_schema_simple
expected = table_schema_with_full_nested_fields
assert actual == expected


Expand Down

0 comments on commit b0ea716

Please sign in to comment.