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

feat: Implement serialize/deserialize for datatypes #6

Merged
merged 15 commits into from
Jul 24, 2023

Conversation

JanKaul
Copy link
Collaborator

@JanKaul JanKaul commented Jul 21, 2023

This PR adds the support to serialize/deserialize the iceberg datatypes.

Copy link
Contributor

@Fokko Fokko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work @JanKaul

src/spec/datatypes.rs Outdated Show resolved Hide resolved
src/spec/datatypes.rs Show resolved Hide resolved
src/spec/datatypes.rs Show resolved Hide resolved
src/spec/datatypes.rs Outdated Show resolved Hide resolved
src/spec/datatypes.rs Show resolved Hide resolved
pub fn get_name(&self, name: &str) -> Option<&StructField> {
self.fields.iter().find(|field| field.name == name)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Python and Java we also have convenient formatting of the struct. I think in rust this is the Display trait. A struct gets formatted as struct<string, int, long, double> based on the fields. Maybe also something to consider for rust

Copy link
Collaborator Author

@JanKaul JanKaul Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've included it in this PR. How is the formatting for maps and lists?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map: map<string, double>
List: list<string>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add this in a separate PR? This way we can get this one in.

src/spec/datatypes.rs Show resolved Hide resolved
/// Time of day without date or timezone.
Time,
/// Timestamp without timezone
Timestamp,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spark uses Timestamp to represent timestamp with timezone, TimestampNtz to represent timestamp without timezone

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to approach in this pr since it's same as iceberg's spec.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also prefer to stick to the iceberg spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the negation rather confusing, on top of that, we should encourage people to use timestamps without timezones =)

src/spec/datatypes.rs Outdated Show resolved Hide resolved
src/spec/datatypes.rs Outdated Show resolved Hide resolved
src/error.rs Show resolved Hide resolved
src/error.rs Show resolved Hide resolved
/// Time of day without date or timezone.
Time,
/// Timestamp without timezone
Timestamp,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to approach in this pr since it's same as iceberg's spec.

src/spec/datatypes.rs Outdated Show resolved Hide resolved
src/spec/datatypes.rs Outdated Show resolved Hide resolved
src/spec/datatypes.rs Show resolved Hide resolved
#[serde(rename = "struct", tag = "type")]
pub struct StructType {
/// Struct fields
pub fields: Vec<StructField>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about making this field private here? The get/get_name has a linear searching algorithm, it maybe inefficient for large structs with hundreds of fields. We may need a hashmap to index name/id to fields.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can create a lookup table to speed up the field access. Should I include it in this PR or should I create a new PR for it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I include it in this PR or should I create a new PR for it?

I think it's not a block of merging this PR. We can add a new issue to track this instead.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's not a block of merging this PR. We can add a new issue to track this instead.

I'm ok with putting lookup table in later PR, just suggest to make fields private for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lookup table for "id" is probably most important. Would you also create a lookup table for "name"?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, accessing by name is also quite frequently in query optimizer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Python we have both, and we cache them. A Python example:

def test_schema_index_by_name_visitor(table_schema_nested: Schema) -> None:
    """Test index_by_name visitor function"""
    table_schema_nested = schema.Schema(
        NestedField(field_id=1, name="foo", field_type=StringType(), required=False),
        NestedField(field_id=2, name="bar", field_type=IntegerType(), required=True),
        NestedField(field_id=3, name="baz", field_type=BooleanType(), required=False),
        NestedField(
            field_id=4,
            name="qux",
            field_type=ListType(element_id=5, element_type=StringType(), element_required=True),
            required=True,
        ),
        NestedField(
            field_id=6,
            name="quux",
            field_type=MapType(
                key_id=7,
                key_type=StringType(),
                value_id=8,
                value_type=MapType(key_id=9, key_type=StringType(), value_id=10, value_type=IntegerType(), value_required=True),
                value_required=True,
            ),
            required=True,
        ),
        NestedField(
            field_id=11,
            name="location",
            field_type=ListType(
                element_id=12,
                element_type=StructType(
                    NestedField(field_id=13, name="latitude", field_type=FloatType(), required=False),
                    NestedField(field_id=14, name="longitude", field_type=FloatType(), required=False),
                ),
                element_required=True,
            ),
            required=True,
        ),
        NestedField(
            field_id=15,
            name="person",
            field_type=StructType(
                NestedField(field_id=16, name="name", field_type=StringType(), required=False),
                NestedField(field_id=17, name="age", field_type=IntegerType(), required=True),
            ),
            required=False,
        ),
        schema_id=1,
        identifier_field_ids=[1],
    )
    index = schema.index_by_name(table_schema_nested)
    assert index == {
        "foo": 1,
        "bar": 2,
        "baz": 3,
        "qux": 4,
        "qux.element": 5,
        "quux": 6,
        "quux.key": 7,
        "quux.value": 8,
        "quux.value.key": 9,
        "quux.value.value": 10,
        "location": 11,
        "location.element": 12,
        "location.element.latitude": 13,
        "location.element.longitude": 14,
        "location.latitude": 13,
        "location.longitude": 14,
        "person": 15,
        "person.name": 16,
        "person.age": 17,
    }

Let's do a separate PR!

Copy link
Collaborator

@liurenjie1024 liurenjie1024 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Copy link
Member

@Xuanwo Xuanwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let's go!

minor: how about rename PR title to feat: Implement serialize/deserialize for datatypes?

@JanKaul JanKaul changed the title serialize/deserialize datatypes feat: Implement serialize/deserialize for datatypes Jul 24, 2023
@Fokko Fokko merged commit 1c7a93d into apache:main Jul 24, 2023
6 checks passed
@Fokko
Copy link
Contributor

Fokko commented Jul 24, 2023

Thank you @JanKaul for the PR! And @liurenjie1024, @ConeyLiu and @Xuanwo for the review!

@JanKaul
Copy link
Collaborator Author

JanKaul commented Jul 24, 2023

Thank you all for the review and the great input.

@JanKaul JanKaul deleted the datatypes branch August 2, 2023 18:16
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.

5 participants