Skip to content

Unions got Error: Validation when call writer.append(record)? #191

@jianchen2580

Description

@jianchen2580

code:

use avro_rs::{
    types::Record, Codec, Reader, Schema,
    Writer, Error,
};

fn main() -> Result<(), Error> {

    let raw_schema = r#"
    {
        "type": "record",
        "name": "test",
        "fields": [
            {"name": "a", "type": "long", "default": 42},
            {"name": "b", "type": "string"},
            {"name": "c", "type": ["string", "null"], "default": null}
        ]
    }
"#;

    let schema = Schema::parse_str(raw_schema)?;

    println!("{:?}", schema);

    let mut writer = Writer::with_codec(&schema, Vec::new(), Codec::Deflate);

    let mut record = Record::new(writer.schema()).unwrap();
    record.put("a", 27i64);
    record.put("b", "bar");
    record.put("c", "foo");
    writer.append(record)?;
    let input = writer.into_inner()?;
    let reader = Reader::with_schema(&schema, &input[..])?;
    for record in reader {
        println!("{:?}", record?);
    }
    Ok(())
}
$ cargo run 
   Compiling aa v0.1.0 (/private/tmp/aa)
    Finished dev [unoptimized + debuginfo] target(s) in 2.03s
     Running `target/debug/aa`
Record { name: Name { name: "test", namespace: None, aliases: None }, doc: None, fields: [RecordField { name: "a", doc: None, default: Some(Number(42)), schema: Long, order: Ascending, position: 0 }, RecordField { name: "b", doc: None, default: None, schema: String, order: Ascending, position: 1 }, RecordField { name: "c", doc: None, default: Some(Null), schema: Union(UnionSchema { schemas: [String, Null], variant_index: {Null: 1, String: 0} }), order: Ascending, position: 2 }], lookup: {"a": 0, "b": 1, "c": 2} }
Error: Validation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions