src/backend/networking/rpc_utils.cpp
/*
 * @brief Parse TupleDesc Message and create Postgres's TupleDesc
 * @param  TupleDescMsg&
 * @return TupleDesc
 *
 * Note: This method is deprecated, but we can refer it when using nested protobuf msg
 */


This method is deprecated, but we can refer it when using nested protobuf msg
This method is deprecated but we can refer it when using nested protobuf msg
std::unique_ptr<tupleDesc> ParseTupleDescMsg(const TupleDescMsg& tuple_desc_msg) {
    std::unique_ptr<tupleDesc> tuple_desc(new tupleDesc);
    int attrs_count = tuple_desc_msg.natts();
    if (attrs_count > 0) {
        Form_pg_attribute attrs[attrs_count];
        Form_pg_attribute* ppattrs = attrs;
        assert(tuple_desc_msg.attrs_size() == attrs_count);
        for (int it = 0; it < attrs_count; it++) {
            attrs[it] = (Form_pg_attribute)malloc(sizeof(FormData_pg_attribute));
            attrs[it]->attalign = assert_range_cast_same<int8, int32>(
                    tuple_desc_msg.attrs(it).attalign());
            attrs[it]->attbyval = tuple_desc_msg.attrs(it).attbyval();
            attrs[it]->attcacheoff = tuple_desc_msg.attrs(it).attcacheoff();
            attrs[it]->attcollation = tuple_desc_msg.attrs(it).attcollation();
            attrs[it]->atthasdef = tuple_desc_msg.attrs(it).atthasdef();
            attrs[it]->attinhcount = tuple_desc_msg.attrs(it).attinhcount();
            attrs[it]->attisdropped = tuple_desc_msg.attrs(it).attisdropped();
            attrs[it]->attislocal = tuple_desc_msg.attrs(it).attislocal();
            attrs[it]->attlen = assert_range_cast_same<int16, int32>(
                    tuple_desc_msg.attrs(it).attlen());
            std::string attname = tuple_desc_msg.attrs(it).attname();
            attname.copy(attrs[it]->attname.data,NAMEDATALEN);
            attrs[it]->attndims = tuple_desc_msg.attrs(it).attndims();
            attrs[it]->attnotnull = tuple_desc_msg.attrs(it).attnotnull();
            attrs[it]->attnum = assert_range_cast_same<int16, int32>(
                    tuple_desc_msg.attrs(it).attnum());
            attrs[it]->attrelid = tuple_desc_msg.attrs(it).attrelid();
            attrs[it]->attstattarget = tuple_desc_msg.attrs(it).attstattarget();
            attrs[it]->attstorage = assert_range_cast_same<int8, int32>(
                    tuple_desc_msg.attrs(it).attstorage());
            attrs[it]->atttypid = tuple_desc_msg.attrs(it).atttypid();
            attrs[it]->atttypmod = tuple_desc_msg.attrs(it).atttypmod();
        }
        tuple_desc->attrs = ppattrs;
    } else {
        tuple_desc->attrs = NULL;
    }
    tuple_desc->natts = attrs_count;
    tuple_desc->tdhasoid = tuple_desc_msg.tdhasoid();
    tuple_desc->tdrefcount = tuple_desc_msg.tdrefcount();
    tuple_desc->tdtypeid = tuple_desc_msg.tdtypeid();
    tuple_desc->tdtypmod = tuple_desc_msg.tdtypmod();
    if (tuple_desc_msg.has_constr()) {
        AttrDefault* attrdef = (AttrDefault*)malloc(sizeof(AttrDefault));
        ConstrCheck* constrch = (ConstrCheck*)malloc(sizeof(ConstrCheck));
        TupleConstr* tuple_constr = (TupleConstr*)malloc(sizeof(TupleConstr));
        std::string adbinss = tuple_desc_msg.constr().adbin();
        std::string ccbinss = tuple_desc_msg.constr().ccbin();
        std::string ccnamess = tuple_desc_msg.constr().ccname();
        char* adbin = (char*)malloc( adbinss.size() + 1 );
        char* ccbin = (char*)malloc( ccbinss.size() + 1 );
        char* ccname = (char*)malloc( ccnamess.size() + 1 );
        adbinss.copy(adbin, adbinss.size());
        adbin[adbinss.size()] = '\0';
        ccbinss.copy(adbin, ccbinss.size());
        adbin[ccbinss.size()] = '\0';
        ccnamess.copy(adbin, ccnamess.size());
        adbin[ccnamess.size()] = '\0';
        attrdef->adbin = adbin;
        attrdef->adnum = tuple_desc_msg.constr().adnum();
        constrch->ccbin = ccbin;
        constrch->ccname = ccname;
        constrch->ccnoinherit = tuple_desc_msg.constr().ccnoinherit();
        constrch->ccvalid = tuple_desc_msg.constr().ccvalid();
        tuple_constr->check = constrch;
        tuple_constr->defval = attrdef;
        tuple_constr->has_not_null = tuple_desc_msg.constr().has_not_null();
        tuple_constr->num_check = assert_range_cast_same<uint16, uint32>(
                tuple_desc_msg.constr().num_check());
        tuple_constr->num_defval = assert_range_cast_same<uint16, uint32>(
                tuple_desc_msg.constr().num_defval());
        tuple_desc->constr = tuple_constr;
    } else {
        tuple_desc->constr = NULL;
    }
    return std::move(tuple_desc);
}
std :: unique_ptr < tupleDesc > ParseTupleDescMsg ( const TupleDescMsg & tuple_desc_msg ) { std :: unique_ptr < tupleDesc > tuple_desc ( new tupleDesc ) ; int attrs_count = tuple_desc_msg . natts ( ) ; if ( attrs_count > 0 ) { Form_pg_attribute attrs [ attrs_count ] ; Form_pg_attribute * ppattrs = attrs ; assert ( tuple_desc_msg . attrs_size ( ) == attrs_count ) ; for ( int it = 0 ; it < attrs_count ; it ++ ) { attrs [ it ] = ( Form_pg_attribute ) malloc ( sizeof ( FormData_pg_attribute ) ) ; attrs [ it ] -> attalign = assert_range_cast_same < int8 , int32 > ( tuple_desc_msg . attrs ( it ) . attalign ( ) ) ; attrs [ it ] -> attbyval = tuple_desc_msg . attrs ( it ) . attbyval ( ) ; attrs [ it ] -> attcacheoff = tuple_desc_msg . attrs ( it ) . attcacheoff ( ) ; attrs [ it ] -> attcollation = tuple_desc_msg . attrs ( it ) . attcollation ( ) ; attrs [ it ] -> atthasdef = tuple_desc_msg . attrs ( it ) . atthasdef ( ) ; attrs [ it ] -> attinhcount = tuple_desc_msg . attrs ( it ) . attinhcount ( ) ; attrs [ it ] -> attisdropped = tuple_desc_msg . attrs ( it ) . attisdropped ( ) ; attrs [ it ] -> attislocal = tuple_desc_msg . attrs ( it ) . attislocal ( ) ; attrs [ it ] -> attlen = assert_range_cast_same < int16 , int32 > ( tuple_desc_msg . attrs ( it ) . attlen ( ) ) ; std :: string attname = tuple_desc_msg . attrs ( it ) . attname ( ) ; attname . copy ( attrs [ it ] -> attname . data , NAMEDATALEN ) ; attrs [ it ] -> attndims = tuple_desc_msg . attrs ( it ) . attndims ( ) ; attrs [ it ] -> attnotnull = tuple_desc_msg . attrs ( it ) . attnotnull ( ) ; attrs [ it ] -> attnum = assert_range_cast_same < int16 , int32 > ( tuple_desc_msg . attrs ( it ) . attnum ( ) ) ; attrs [ it ] -> attrelid = tuple_desc_msg . attrs ( it ) . attrelid ( ) ; attrs [ it ] -> attstattarget = tuple_desc_msg . attrs ( it ) . attstattarget ( ) ; attrs [ it ] -> attstorage = assert_range_cast_same < int8 , int32 > ( tuple_desc_msg . attrs ( it ) . attstorage ( ) ) ; attrs [ it ] -> atttypid = tuple_desc_msg . attrs ( it ) . atttypid ( ) ; attrs [ it ] -> atttypmod = tuple_desc_msg . attrs ( it ) . atttypmod ( ) ; } tuple_desc -> attrs = ppattrs ; } else { tuple_desc -> attrs = NULL ; } tuple_desc -> natts = attrs_count ; tuple_desc -> tdhasoid = tuple_desc_msg . tdhasoid ( ) ; tuple_desc -> tdrefcount = tuple_desc_msg . tdrefcount ( ) ; tuple_desc -> tdtypeid = tuple_desc_msg . tdtypeid ( ) ; tuple_desc -> tdtypmod = tuple_desc_msg . tdtypmod ( ) ; if ( tuple_desc_msg . has_constr ( ) ) { AttrDefault * attrdef = ( AttrDefault * ) malloc ( sizeof ( AttrDefault ) ) ; ConstrCheck * constrch = ( ConstrCheck * ) malloc ( sizeof ( ConstrCheck ) ) ; TupleConstr * tuple_constr = ( TupleConstr * ) malloc ( sizeof ( TupleConstr ) ) ; std :: string adbinss = tuple_desc_msg . constr ( ) . adbin ( ) ; std :: string ccbinss = tuple_desc_msg . constr ( ) . ccbin ( ) ; std :: string ccnamess = tuple_desc_msg . constr ( ) . ccname ( ) ; char * adbin = ( char * ) malloc ( adbinss . size ( ) + 1 ) ; char * ccbin = ( char * ) malloc ( ccbinss . size ( ) + 1 ) ; char * ccname = ( char * ) malloc ( ccnamess . size ( ) + 1 ) ; adbinss . copy ( adbin , adbinss . size ( ) ) ; adbin [ adbinss . size ( ) ] = ' \0 ' ; ccbinss . copy ( adbin , ccbinss . size ( ) ) ; adbin [ ccbinss . size ( ) ] = ' \0 ' ; ccnamess . copy ( adbin , ccnamess . size ( ) ) ; adbin [ ccnamess . size ( ) ] = ' \0 ' ; attrdef -> adbin = adbin ; attrdef -> adnum = tuple_desc_msg . constr ( ) . adnum ( ) ; constrch -> ccbin = ccbin ; constrch -> ccname = ccname ; constrch -> ccnoinherit = tuple_desc_msg . constr ( ) . ccnoinherit ( ) ; constrch -> ccvalid = tuple_desc_msg . constr ( ) . ccvalid ( ) ; tuple_constr -> check = constrch ; tuple_constr -> defval = attrdef ; tuple_constr -> has_not_null = tuple_desc_msg . constr ( ) . has_not_null ( ) ; tuple_constr -> num_check = assert_range_cast_same < uint16 , uint32 > ( tuple_desc_msg . constr ( ) . num_check ( ) ) ; tuple_constr -> num_defval = assert_range_cast_same < uint16 , uint32 > ( tuple_desc_msg . constr ( ) . num_defval ( ) ) ; tuple_desc -> constr = tuple_constr ; } else { tuple_desc -> constr = NULL ; } return std :: move ( tuple_desc ) ; }
@brief Parse TupleDesc Message and create Postgres's TupleDesc
 @param  TupleDescMsg&
 @return TupleDesc
@brief Parse TupleDesc Message and create Postgres ' s TupleDesc @param TupleDescMsg& @return TupleDesc
/*
     * We didn't use CreateTupleDesc, which is a Postgres function. CreateTupleDesc brings error
     * And if we use postgres create, it is a malloc inside. When should we FreeTupleDesc?
     * So we use std::unique_ptr, and move the ownership to the caller, then we don't need to
     * care the free problem
     */ //===----------------------------------------------------------------------===// //   Parse and create attrs structure //===----------------------------------------------------------------------===// // Get the number of attrs // Here attalign is char in postgres, so we convert it // It is int16 in postgres, so we convert it // It is int16 in postgres, so we convert it // It is char in postgres, so we convert it //===----------------------------------------------------------------------===// //    Set the basic value type //===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===// //   Parse and create TupeDescCons structure //===----------------------------------------------------------------------===// // Note the three char* () is ended with \0 in postgres, such as appendStringInfoChar. // So they are safe to be set in protobuf (as bytes) with on "size" parameter // But when convert them back to char* we should set \0 at the end of char* // Set AttrDefault // int16 // Set ConstrCheck // Set TupleConstr // uint16 // uint16 // Set Constr