Skip to content

Logical type UUID with type "fixed" do not serialize to fixed 16 #53

Closed

Description

@Tipnos

Avro 1.12.0 specifies that UUID logical types annotates to either String or Fixed 16.

The example below serialized the UUID to a String and not a Fixed 16

#[derive(Debug, Serialize, Deserialize)]
struct Comment {
    id: Uuid,
}

impl AvroSchema for Comment {
    fn get_schema() -> Schema {
        Schema::parse_str(
            r#"{
                "type" : "record",
                "name" : "Comment",
                "fields" : [ {
                  "name" : "id",
                  "type" : {
                    "type" : "fixed",
                    "size" : 16,
                    "logicalType" : "uuid",
                    "name": "FixedUUID" // if we remove the name attribute schema parsing fail. The name attribute is not in avro 1.12.0 specification
                  }
                } ]
             }"#,
        )
        .expect("Invalid Comment Avro schema")
    }
}

let payload = Comment {
        id: "de2df598-9948-4988-b00a-a41c0e287398".parse().unwrap(),
};
let mut buffer = Vec::new();
SpecificSingleObjectWriter::<Comment>::with_capacity(10)
    .unwrap()
    .write_ref(&payload, &mut buffer)
    .unwrap();

Do you plan to support it in the near future?

Activity

  1. martin-g commented on Nov 14, 2024

    @martin-g
    Member

    I will try to take a look next week!
    Any help is welcome! Providing a PR will make sure it is supported in nearest future! ;-)

  2. added a commit that references this issue on Nov 19, 2024
  3. martin-g commented on Nov 19, 2024

    @martin-g
    Member

    Added the test case to the suite - #55
    It works just fine locally.
    I guess it has been fixed with #9

  4. added 2 commits that reference this issue on Nov 19, 2024
  5. Tipnos commented on Nov 21, 2024

    @Tipnos
    Author

    I had a look a the test you added, it checks that it serializes successfully but doesn't check the payload content/size to validate that uuid is encoded as 16 bytes.

    I just hex dump the content of your test, the payload length is 47 bytes instead of 26.

    Why 26:

    • 2 bytes of Avro marker
    • 8 bytes of schema fingerprint
    • 16 bytes for the uuid

    In the actual 47 bytes, the UUID takes 37 bytes which is its number of characters.

  6. martin-g commented on Nov 21, 2024

    @martin-g
    Member

    This deserves a new issue.
    The bug here is that AvroSchema derived Schema does not support logical types at all at the moment ... This was not correct. The issue is somewhere else.

  7. martin-g commented on Nov 21, 2024

    @martin-g
    Member

    The problem here is that SpecificSingleObjectWriter::<Comment>::with_capacity(10)?.write_ref(&payload, &mut buffer) uses Serde and Uuid's serde support serializes it to String, i.e. it calls fn serialize_str(self, v: &str) -> Result<Self::Ok, Self::Error> and without support for external Schema there is no way for the Avro impl to know that it should create apache_avro::types::Value::Fixed instead of apache_avro::types::Value::String and this leads to 37 bytes instead of 16.

    If you have idea how to tell Serde to use an external Schema I'll be glad to explore it!

    Otherwise I'd suggest to use the avro way.

  8. added 4 commits that reference this issue on Dec 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions