Pash0002
Pash0002

Reputation: 110

Apache Avro Union type

I'm using the Avro 1.11.0 library to write data into the Avro files using Python 3.7. I'm having some doubts about the union type of the Avro. Please find below the two schemas.

{
    "name" : "name",
    "type" : ["null", "string"],
    "columnName" : "name",
}

{
    "name" : "name",
    "type" : ["string", "null"],
    "columnName" : "name",
}

First schema contains union type as "type" : ["null", "string"] and second schema contains union type as "type" : ["string", "null"]. So is there any difference between the above mentioned schemas?

Upvotes: 0

Views: 7301

Answers (1)

Scott
Scott

Reputation: 2074

The only difference is that the specification states that if you want to use a default value, it should correspond to the first type in the union.

For example, these would be valid:

{
    "name" : "name",
    "type" : ["null", "string"],
    "columnName" : "name",
    "default": null,
}

{
    "name" : "name",
    "type" : ["string", "null"],
    "columnName" : "name",
    "default": "foo",
}

But these would not:

{
    "name" : "name",
    "type" : ["null", "string"],
    "columnName" : "name",
    "default": "foo",
}

{
    "name" : "name",
    "type" : ["string", "null"],
    "columnName" : "name",
    "default": null,
}

Since a union that includes null tends to mean something like an optional field, most people would put null as the first option in the union so that they can set the default value to null.

Upvotes: 5

Related Questions