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

array with union records throws exception #148

Open
gaborbernat opened this issue Jul 16, 2020 · 0 comments
Open

array with union records throws exception #148

gaborbernat opened this issue Jul 16, 2020 · 0 comments
Labels

Comments

@gaborbernat
Copy link

gaborbernat commented Jul 16, 2020

Reproduce with an example:

use avro_rs::Schema;

fn main() {
    // let raw = fs::read_to_string("/Users/bgabor8/git/dqc/data-benchmark/trans-3.1.avsc").unwrap();
    // let schema = Schema::parse_str(&raw).unwrap();
    // println!("{:?}", schema);

    let raw = r#"
    {
      "name": "Data",
      "type": "record",
      "fields": [
        {
          "name": "sources",
          "type": {
            "type": "array",
            "items": [
              {
                "name": "Url",
                "type": "record",
                "fields": [{ "name": "url", "type": "string" }]
              },
              {
                "name": "Source",
                "type": "record",
                "fields": [{ "name": "data", "type": "string"}]
              }
            ]
          }
        }
      ]
    }
    "#;
    Schema::parse_str(raw).unwrap();
}
value: ParseSchemaError("Unions cannot contain duplicate types")

For reference the python avro behaviour (demonstrating that the construction is valid):

import json
from io import BytesIO

from avro.io import BinaryEncoder, DatumWriter, DatumReader, BinaryDecoder
from avro.schema import parse

json_schema_raw = {
    "name": "Data",
    "type": "record",
    "fields": [
        {
            "name": "sources",
            "type": {
                "type": "array",
                "items": [
                    {"name": "Url", "type": "record", "fields": [{"name": "url", "type": "string"}]},
                    {"name": "Source", "type": "record", "fields": [{"name": "data", "type": "string"}]},
                ],
            },
        }
    ],
}
schema = parse(json.dumps(json_schema_raw))

writer = DatumWriter()
with BytesIO() as serialized_binary:
    binary_encoder = BinaryEncoder(serialized_binary)
    writer.write_record(schema, {"sources": [{"url": "a"}, {"data": "b"}]}, binary_encoder)
    raw = serialized_binary.getvalue()

print(raw)

reader = DatumReader()
with BytesIO(raw) as binary_input:
    binary_decoder = BinaryDecoder(binary_input)
    record = reader.read_record(schema, schema, binary_decoder)
print(record)
b'\x04\x00\x02a\x02\x02b\x00'
{'sources': [{'url': 'a'}, {'data': 'b'}]}

The issue seems to be here https://github.com/flavray/avro-rs/blob/master/src/schema.rs#L330, while iterating through the schema records the SchemaKind::from(schema) identifies both Url and Source from above as Schema::Null, hence the duplicate error.

@poros poros added the bug label Jul 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants