You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avro.apache.org by "Brian Lachniet (JIRA)" <ji...@apache.org> on 2019/08/16 23:19:00 UTC

[jira] [Commented] (AVRO-2509) Avro schema default value to null, should it be "null" (with quotes) or null (without quotes)?

    [ https://issues.apache.org/jira/browse/AVRO-2509?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16909473#comment-16909473 ] 

Brian Lachniet commented on AVRO-2509:
--------------------------------------

Hi [~kt_hk]!

I'm guessing that you posted [this question on Stack Overflow|https://stackoverflow.com/q/57520685/389899] as well. I've copied my answer from there:

To default a value to null, you must specify {{"default": null}}. See the table in the [Complex Types|http://avro.apache.org/docs/1.9.0/spec.html#schema_complex]section of the specification for examples values of each type.
{quote}Does it matter what the data type is?
{quote}
*Yes*, the type matters. As you can see in the [Primitive Types|http://avro.apache.org/docs/1.9.0/spec.html#schema_primitive] section of the specification, {{null}} is its own type. You may not set an {{int}}, {{string}}, or any other primitive type to {{null}}.

If you want a "nullable" field, use [Unions|http://avro.apache.org/docs/1.9.0/spec.html#Unions]. For example:

{{{ "type": ["null", "int"], "default": null }}}

This dictates that this data may either be an {{int}} or it may be a {{null}}. It defaults to {{null}}. When specifying defaults for unions, remember that the default must match the first type in the union.

From [the specification|http://avro.apache.org/docs/1.9.0/spec.html#Unions]:
{quote}(Note that when a default value is specified for a record field whose type is a union, the type of the default value must match the first element of the union. Thus, for unions containing "null", the "null" is usually listed first, since the default value of such unions is typically null.)
{quote}

> Avro schema default value to null, should it be "null" (with quotes) or null (without quotes)?
> ----------------------------------------------------------------------------------------------
>
>                 Key: AVRO-2509
>                 URL: https://issues.apache.org/jira/browse/AVRO-2509
>             Project: Apache Avro
>          Issue Type: Task
>          Components: java
>            Reporter: KT
>            Priority: Trivial
>
> HI,  
>    I am new to Avro and trying to define a schema. Wondering if I am trying to default a value to null, should it be: 
>    a. "default": null
>    b. "default": "null"
>    Does it matter what the data type is? like for int then the default will be "default": null, but when it's for a string then the default should be "default":"null"? or it doesn't matter? 
>  
> Thanks
> KT



--
This message was sent by Atlassian JIRA
(v7.6.14#76016)