Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optional schema fields are always set to 0 #24

Merged
merged 1 commit into from
Sep 23, 2024

Conversation

stathva
Copy link
Contributor

@stathva stathva commented Sep 4, 2024

SchemaElement int32 optional fields like FieldId, Scale, Precision etc. are always serialised with value 0.
See for example the schema generated for the example in _examples/people/:

$> parquet-tools schema --format raw people.parquet

returns

{
    "name": "Root",
    "num_children": 13,
    "children": [
        {
            "type": "INT32",
            "type_length": 0,
            "repetition_type": "REQUIRED",
            "name": "Id",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "INT32",
            "type_length": 0,
            "repetition_type": "OPTIONAL",
            "name": "Age",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "INT64",
            "type_length": 0,
            "repetition_type": "REQUIRED",
            "name": "Happiness",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "INT64",
            "type_length": 0,
            "repetition_type": "OPTIONAL",
            "name": "Sadness",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "BYTE_ARRAY",
            "type_length": 0,
            "repetition_type": "OPTIONAL",
            "name": "Code",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "FLOAT",
            "type_length": 0,
            "repetition_type": "REQUIRED",
            "name": "Funkiness",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "FLOAT",
            "type_length": 0,
            "repetition_type": "OPTIONAL",
            "name": "Lameness",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "BOOLEAN",
            "type_length": 0,
            "repetition_type": "OPTIONAL",
            "name": "Keen",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "INT32",
            "type_length": 0,
            "repetition_type": "REQUIRED",
            "name": "Birthday",
            "converted_type": "UINT_32",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "INT64",
            "type_length": 0,
            "repetition_type": "OPTIONAL",
            "name": "Anniversary",
            "converted_type": "UINT_64",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "type": "INT32",
            "type_length": 0,
            "repetition_type": "OPTIONAL",
            "name": "Difficulty",
            "scale": 0,
            "precision": 0,
            "field_id": 0
        },
        {
            "repetition_type": "OPTIONAL",
            "name": "Hobby",
            "num_children": 2,
            "children": [
                {
                    "type": "BYTE_ARRAY",
                    "type_length": 0,
                    "repetition_type": "REQUIRED",
                    "name": "Name",
                    "scale": 0,
                    "precision": 0,
                    "field_id": 0
                },
                {
                    "type": "INT32",
                    "type_length": 0,
                    "repetition_type": "OPTIONAL",
                    "name": "Difficulty",
                    "scale": 0,
                    "precision": 0,
                    "field_id": 0
                }
            ]
        },
        {
            "repetition_type": "REPEATED",
            "name": "Friends",
            "num_children": 2,
            "children": [
                {
                    "type": "INT32",
                    "type_length": 0,
                    "repetition_type": "REQUIRED",
                    "name": "Id",
                    "scale": 0,
                    "precision": 0,
                    "field_id": 0
                },
                {
                    "type": "INT32",
                    "type_length": 0,
                    "repetition_type": "OPTIONAL",
                    "name": "Age",
                    "scale": 0,
                    "precision": 0,
                    "field_id": 0
                }
            ]
        }
    ]
}

This can have unintended consequences with any Parquet reader that explicitly uses the schema.
Additionally the fields are only serialised if they are not nil, see here.

With the proposed changes the schema for the example in _examples/people/ is:

{
    "name": "Root",
    "num_children": 13,
    "children": [
        {
            "type": "INT32",
            "repetition_type": "REQUIRED",
            "name": "Id"
        },
        {
            "type": "INT32",
            "repetition_type": "OPTIONAL",
            "name": "Age"
        },
        {
            "type": "INT64",
            "repetition_type": "REQUIRED",
            "name": "Happiness"
        },
        {
            "type": "INT64",
            "repetition_type": "OPTIONAL",
            "name": "Sadness"
        },
        {
            "type": "BYTE_ARRAY",
            "repetition_type": "OPTIONAL",
            "name": "Code"
        },
        {
            "type": "FLOAT",
            "repetition_type": "REQUIRED",
            "name": "Funkiness"
        },
        {
            "type": "FLOAT",
            "repetition_type": "OPTIONAL",
            "name": "Lameness"
        },
        {
            "type": "BOOLEAN",
            "repetition_type": "OPTIONAL",
            "name": "Keen"
        },
        {
            "type": "INT32",
            "repetition_type": "REQUIRED",
            "name": "Birthday",
            "converted_type": "UINT_32"
        },
        {
            "type": "INT64",
            "repetition_type": "OPTIONAL",
            "name": "Anniversary",
            "converted_type": "UINT_64"
        },
        {
            "type": "INT32",
            "repetition_type": "OPTIONAL",
            "name": "Difficulty"
        },
        {
            "repetition_type": "OPTIONAL",
            "name": "Hobby",
            "num_children": 2,
            "children": [
                {
                    "type": "BYTE_ARRAY",
                    "repetition_type": "REQUIRED",
                    "name": "Name"
                },
                {
                    "type": "INT32",
                    "repetition_type": "OPTIONAL",
                    "name": "Difficulty"
                }
            ]
        },
        {
            "repetition_type": "REPEATED",
            "name": "Friends",
            "num_children": 2,
            "children": [
                {
                    "type": "INT32",
                    "repetition_type": "REQUIRED",
                    "name": "Id"
                },
                {
                    "type": "INT32",
                    "repetition_type": "OPTIONAL",
                    "name": "Age"
                }
            ]
        }
    ]
}

@cswank
Copy link
Collaborator

cswank commented Sep 23, 2024

Thanks for this PR, and sorry for not noticing it for so long.

I'd like to understand the intent behind these fields, and maybe look into populating them correctly. I'm sure these changes are more correct than populating them all with zeros, so it looks like a good PR to merge. Thanks again.

@cswank cswank merged commit 5afc7c2 into parsyl:master Sep 23, 2024
1 check failed
@stathva
Copy link
Contributor Author

stathva commented Oct 1, 2024

Thanks for merging.

I'd like to understand the intent behind these fields, and maybe look into populating them correctly

There is some documentation about these fields here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants