You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@avro.apache.org by Mark Petronic <ma...@gmail.com> on 2019/11/19 17:03:46 UTC

Accessing default value in schema

I am getting back into C++ but don't have much experience with templates so
I am a reaching out for a hint here. I am trying to iterate over all the
NodePrt nodes starting with the root node using avro::NodePtr& root =
schema.root(); But I just cannot figure out how to code up the method call
to get the default value from a primitive whose schema defines one. For
example, a simple schema like this:

{
    "name": "simple_schema",
    "type": "record",
    "fields": [
        {
            "name": "int_stat",
            "type": "long",
            "default": 999
        }
    ]
}

I want to get my hands on that 999 value. Here is a snippet of a method
that gets a NodePtr to an Avro record, retrieves the defaultValueAt(i) just
fine, but how do I code up the call to retrieve the value() 999?

   1. What should the return value look like given the default could be
   various types depending on the schema?
   2. What should this call look like?  default_value.value();

Thanks for any insight and guidance!!!

void Encoder::encode_record(
        const avro::NodePtr& n,
        const csv_data_map& csv_data,
        avro::EncoderPtr encoder) const
{
    size_t c = n->leaves();
    cout << "Processing Avro record, name = " << n->name() << endl;
    for (size_t i = 0; i < c; ++i) {
        avro::GenericDatum default_value = n->defaultValueAt(i);
        // THIS IS THE PROBLEM PART I CANNOT FIGURE OUT
        default_value.value();
        encode_type(n->leafAt(i), n->nameAt(i), csv_data, encoder,
default_value);
    }
}

Building file: ../src/encoder.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP
-MF"src/encoder.d" -MT"src/encoder.o" -o "src/encoder.o"
"../src/encoder.cpp"
../src/encoder.cpp: In member function ‘void Encoder::encode_record(const
NodePtr&, const csv_data_map&, avro::EncoderPtr) const’:
../src/encoder.cpp:257:29: error: no matching function for call to
‘avro::GenericDatum::value()’
         default_value.value();
                             ^
../src/encoder.cpp:257:29: note: candidates are:
In file included from ../src/encoder.cpp:7:0:
/usr/local/include/avro/GenericDatum.hh:98:35: note: template<class T>
const T& avro::GenericDatum::value() const
     template<typename T> const T& value() const;
                                   ^
/usr/local/include/avro/GenericDatum.hh:98:35: note:   template argument
deduction/substitution failed:
../src/encoder.cpp:257:29: note:   couldn't deduce template parameter ‘T’
         default_value.value();
                             ^
In file included from ../src/encoder.cpp:7:0:
/usr/local/include/avro/GenericDatum.hh:109:29: note: template<class T> T&
avro::GenericDatum::value()
     template<typename T> T& value();
                             ^
/usr/local/include/avro/GenericDatum.hh:109:29: note:   template argument
deduction/substitution failed:
../src/encoder.cpp:257:29: note:   couldn't deduce template parameter ‘T’
         default_value.value();
                             ^
make: *** [src/encoder.o] Error 1

Re: Accessing default value in schema

Posted by Mark Petronic <ma...@gmail.com>.
Thank you very much for your reply. That was very helpful and solved my
issue. I need:

int32_t n = default.value<int32_t>();

On Tue, Nov 19, 2019, 12:28 PM Dan Schmitt <da...@gmail.com> wrote:

> It can't tell from your use what type to return/cast the value to so
> its failing.
>
>     default_value.value<int64_t>();
>
> will try to do it as an int64_t to match the long.  Alternatively you
> should be able to do
>
>     int64_t value = default_value.value();
>
> and it can guess the type there.
>
> I suspect your problem lives in encode_type and what it's doing with
> the last parameter.
>
> On Tue, Nov 19, 2019 at 11:04 AM Mark Petronic <ma...@gmail.com>
> wrote:
> >
> > I am getting back into C++ but don't have much experience with templates
> so I am a reaching out for a hint here. I am trying to iterate over all the
> NodePrt nodes starting with the root node using avro::NodePtr& root =
> schema.root(); But I just cannot figure out how to code up the method call
> to get the default value from a primitive whose schema defines one. For
> example, a simple schema like this:
> >
> > {
> >     "name": "simple_schema",
> >     "type": "record",
> >     "fields": [
> >         {
> >             "name": "int_stat",
> >             "type": "long",
> >             "default": 999
> >         }
> >     ]
> > }
> >
> > I want to get my hands on that 999 value. Here is a snippet of a method
> that gets a NodePtr to an Avro record, retrieves the defaultValueAt(i) just
> fine, but how do I code up the call to retrieve the value() 999?
> >
> > What should the return value look like given the default could be
> various types depending on the schema?
> > What should this call look like?  default_value.value();
> >
> > Thanks for any insight and guidance!!!
> >
> > void Encoder::encode_record(
> >         const avro::NodePtr& n,
> >         const csv_data_map& csv_data,
> >         avro::EncoderPtr encoder) const
> > {
> >     size_t c = n->leaves();
> >     cout << "Processing Avro record, name = " << n->name() << endl;
> >     for (size_t i = 0; i < c; ++i) {
> >         avro::GenericDatum default_value = n->defaultValueAt(i);
> >         // THIS IS THE PROBLEM PART I CANNOT FIGURE OUT
> >         default_value.value();
> >         encode_type(n->leafAt(i), n->nameAt(i), csv_data, encoder,
> default_value);
> >     }
> > }
> >
> > Building file: ../src/encoder.cpp
> > Invoking: GCC C++ Compiler
> > g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP
> -MF"src/encoder.d" -MT"src/encoder.o" -o "src/encoder.o"
> "../src/encoder.cpp"
> > ../src/encoder.cpp: In member function ‘void
> Encoder::encode_record(const NodePtr&, const csv_data_map&,
> avro::EncoderPtr) const’:
> > ../src/encoder.cpp:257:29: error: no matching function for call to
> ‘avro::GenericDatum::value()’
> >          default_value.value();
> >                              ^
> > ../src/encoder.cpp:257:29: note: candidates are:
> > In file included from ../src/encoder.cpp:7:0:
> > /usr/local/include/avro/GenericDatum.hh:98:35: note: template<class T>
> const T& avro::GenericDatum::value() const
> >      template<typename T> const T& value() const;
> >                                    ^
> > /usr/local/include/avro/GenericDatum.hh:98:35: note:   template argument
> deduction/substitution failed:
> > ../src/encoder.cpp:257:29: note:   couldn't deduce template parameter ‘T’
> >          default_value.value();
> >                              ^
> > In file included from ../src/encoder.cpp:7:0:
> > /usr/local/include/avro/GenericDatum.hh:109:29: note: template<class T>
> T& avro::GenericDatum::value()
> >      template<typename T> T& value();
> >                              ^
> > /usr/local/include/avro/GenericDatum.hh:109:29: note:   template
> argument deduction/substitution failed:
> > ../src/encoder.cpp:257:29: note:   couldn't deduce template parameter ‘T’
> >          default_value.value();
> >                              ^
> > make: *** [src/encoder.o] Error 1
> >
> >
>

Re: Accessing default value in schema

Posted by Dan Schmitt <da...@gmail.com>.
It can't tell from your use what type to return/cast the value to so
its failing.

    default_value.value<int64_t>();

will try to do it as an int64_t to match the long.  Alternatively you
should be able to do

    int64_t value = default_value.value();

and it can guess the type there.

I suspect your problem lives in encode_type and what it's doing with
the last parameter.

On Tue, Nov 19, 2019 at 11:04 AM Mark Petronic <ma...@gmail.com> wrote:
>
> I am getting back into C++ but don't have much experience with templates so I am a reaching out for a hint here. I am trying to iterate over all the NodePrt nodes starting with the root node using avro::NodePtr& root = schema.root(); But I just cannot figure out how to code up the method call to get the default value from a primitive whose schema defines one. For example, a simple schema like this:
>
> {
>     "name": "simple_schema",
>     "type": "record",
>     "fields": [
>         {
>             "name": "int_stat",
>             "type": "long",
>             "default": 999
>         }
>     ]
> }
>
> I want to get my hands on that 999 value. Here is a snippet of a method that gets a NodePtr to an Avro record, retrieves the defaultValueAt(i) just fine, but how do I code up the call to retrieve the value() 999?
>
> What should the return value look like given the default could be various types depending on the schema?
> What should this call look like?  default_value.value();
>
> Thanks for any insight and guidance!!!
>
> void Encoder::encode_record(
>         const avro::NodePtr& n,
>         const csv_data_map& csv_data,
>         avro::EncoderPtr encoder) const
> {
>     size_t c = n->leaves();
>     cout << "Processing Avro record, name = " << n->name() << endl;
>     for (size_t i = 0; i < c; ++i) {
>         avro::GenericDatum default_value = n->defaultValueAt(i);
>         // THIS IS THE PROBLEM PART I CANNOT FIGURE OUT
>         default_value.value();
>         encode_type(n->leafAt(i), n->nameAt(i), csv_data, encoder, default_value);
>     }
> }
>
> Building file: ../src/encoder.cpp
> Invoking: GCC C++ Compiler
> g++ -std=c++0x -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/encoder.d" -MT"src/encoder.o" -o "src/encoder.o" "../src/encoder.cpp"
> ../src/encoder.cpp: In member function ‘void Encoder::encode_record(const NodePtr&, const csv_data_map&, avro::EncoderPtr) const’:
> ../src/encoder.cpp:257:29: error: no matching function for call to ‘avro::GenericDatum::value()’
>          default_value.value();
>                              ^
> ../src/encoder.cpp:257:29: note: candidates are:
> In file included from ../src/encoder.cpp:7:0:
> /usr/local/include/avro/GenericDatum.hh:98:35: note: template<class T> const T& avro::GenericDatum::value() const
>      template<typename T> const T& value() const;
>                                    ^
> /usr/local/include/avro/GenericDatum.hh:98:35: note:   template argument deduction/substitution failed:
> ../src/encoder.cpp:257:29: note:   couldn't deduce template parameter ‘T’
>          default_value.value();
>                              ^
> In file included from ../src/encoder.cpp:7:0:
> /usr/local/include/avro/GenericDatum.hh:109:29: note: template<class T> T& avro::GenericDatum::value()
>      template<typename T> T& value();
>                              ^
> /usr/local/include/avro/GenericDatum.hh:109:29: note:   template argument deduction/substitution failed:
> ../src/encoder.cpp:257:29: note:   couldn't deduce template parameter ‘T’
>          default_value.value();
>                              ^
> make: *** [src/encoder.o] Error 1
>
>