You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Michael Lim <mi...@gmail.com> on 2014/09/30 06:39:26 UTC

Avro C API: Union has no selected branch

Hi Avro users,

I'm having trouble with the avro_value_set_branch function. Hoping anyone
can chime in here.

Schema for a node in a binary tree:
 {
   "type": "record",
   "name": "node",
   "fields": [
                 {"name": "featureIndex", "type": "int"},
                 {"name": "split", "type": "double"},
                 {"name": "value", "type": "double"},
                 {"name": "left", "type": ["node", "null"]},
                 {"name": "right", "type": ["node", "null"]}
   ]
}

Here's the code snippet leading up to the error.

avro_value_t node;
avro_value_t field;
avro_value_t branch;
avro_generic_value_new(valueIface, &node); //valueIface was instantiated
with the schema below
avro_value_get_by_name(&node, "left", &field, NULL);
avro_value_t leftNode = some other node that I'd like to copy into the
"left" field of the "node" variable

avro_value_set_branch(&field, 0, &branch); //no error here
avro_value_copy(&branch, &leftNode); //"Union has no selected branch" error
is thrown here

But it works if I do:
avro_value_set_branch(&field, 1, &branch);
avro_value_set_null(&branch);

The only other example I could find is this
<http://mail-archives.apache.org/mod_mbox/avro-user/201209.mbox/%3CCAB75mSU7ALxrB50TUjhqf0krJqk7=ugRtfXK8Zo584Tx9wKFqA@mail.gmail.com%3E>.
Thanks
in advance for looking.

Re: Avro C API: Union has no selected branch

Posted by Michael Lim <mi...@gmail.com>.
Thanks Mika for your reply.

I tried avro_value_get_current_branch(&field, &branch), but now the error
is thrown here for me. I am using Avro 1.7.7.


On Tue, Sep 30, 2014 at 7:02 AM, Mika Ristimaki <mi...@gmail.com>
wrote:

> Hi,
>
> Not sure if this works but you can try:
>
> On 30 Sep 2014, at 07:39, Michael Lim <mi...@gmail.com> wrote:
> > avro_value_t node;
> > avro_value_t field;
> > avro_value_t branch;
> > avro_generic_value_new(valueIface, &node); //valueIface was instantiated
> with the schema below
> > avro_value_get_by_name(&node, "left", &field, NULL);
> > avro_value_t leftNode = some other node that I'd like to copy into the
> "left" field of the "node" variable
> >
>
> avro_value_get_current_branch(&field, &branch);
> avro_value_copy(&branch, &leftNode);
>
> Instead of the stuff below. I know it looks bit strange but at least it
> works for me.
>
> -Mika
>
> > avro_value_set_branch(&field, 0, &branch); //no error here
> > avro_value_copy(&branch, &leftNode); //"Union has no selected branch"
> error is thrown here
> >
> > But it works if I do:
> > avro_value_set_branch(&field, 1, &branch);
> > avro_value_set_null(&branch);
> >
> > The only other example I could find is this. Thanks in advance for
> looking.
>
>

Re: Avro C API: Union has no selected branch

Posted by Mika Ristimaki <mi...@gmail.com>.
Hi,

Not sure if this works but you can try:

On 30 Sep 2014, at 07:39, Michael Lim <mi...@gmail.com> wrote:
> avro_value_t node;
> avro_value_t field;
> avro_value_t branch;
> avro_generic_value_new(valueIface, &node); //valueIface was instantiated with the schema below
> avro_value_get_by_name(&node, "left", &field, NULL);
> avro_value_t leftNode = some other node that I'd like to copy into the "left" field of the "node" variable
> 

avro_value_get_current_branch(&field, &branch);
avro_value_copy(&branch, &leftNode);

Instead of the stuff below. I know it looks bit strange but at least it works for me. 

-Mika

> avro_value_set_branch(&field, 0, &branch); //no error here
> avro_value_copy(&branch, &leftNode); //"Union has no selected branch" error is thrown here
> 
> But it works if I do:
> avro_value_set_branch(&field, 1, &branch);
> avro_value_set_null(&branch);
> 
> The only other example I could find is this. Thanks in advance for looking.


Re: Avro C API: Union has no selected branch

Posted by Douglas Creager <do...@creagertino.net>.
> I'm having trouble with the avro_value_set_branch function. Hoping anyone
> can chime in here.
>
> avro_value_copy(&branch, &leftNode);
> //"Union has no selected branch" error is thrown here

You can only get that kind of error from an
avro_value_get_current_branch call.  `branch` and `leftNode` should be
records, so avro_value_copy shouldn't try to call that function on
`branch` or `leftNode` directly.  But part of copying a record is
copying its fields, so avro_value_copy will at some point try to call
get_current_branch on `leftNode`'s `left` and `right` children.  My
hunch is that's where the error is coming from.  Can you send us a
snippet on how `leftNode` is created?  You'll want to make sure to call
set_branch(1) and set_null() on both children of any leaf node, for
instance.

–doug