src/wire/packet_manager.cpp
size_t PacketManager::ReadParamValue(
    InputPacket *pkt, int num_params, std::vector<int32_t> &param_types,
    std::vector<std::pair<int, std::string>> &bind_parameters,
    std::vector<type::Value> &param_values, std::vector<int16_t> &formats) {
  auto begin = pkt->ptr;
  ByteBuf param;
  for (int param_idx = 0; param_idx < num_params; param_idx++) {
    int param_len = PacketGetInt(pkt, 4);
    // BIND packet NULL parameter case
    if (param_len == -1) {
      // NULL mode
      auto peloton_type = PostgresValueTypeToPelotonValueType(
          static_cast<PostgresValueType>(param_types[param_idx]));
      bind_parameters[param_idx] =
          std::make_pair(peloton_type, std::string(""));
      param_values[param_idx] =
          type::ValueFactory::GetNullValueByType(peloton_type);
    } else {
      PacketGetBytes(pkt, param_len, param);

      if (formats[param_idx] == 0) {
        // TEXT mode
        std::string param_str = std::string(std::begin(param), std::end(param));
        bind_parameters[param_idx] =
            std::make_pair(type::Type::VARCHAR, param_str);
        if (PostgresValueTypeToPelotonValueType(
                (PostgresValueType)param_types[param_idx]) ==
            type::Type::VARCHAR) {
          param_values[param_idx] =
              type::ValueFactory::GetVarcharValue(param_str);
        } else {
          param_values[param_idx] =
              (type::ValueFactory::GetVarcharValue(param_str))
                  .CastAs(PostgresValueTypeToPelotonValueType(
                      (PostgresValueType)param_types[param_idx]));
        }
        PL_ASSERT(param_values[param_idx].GetTypeId() != type::Type::INVALID);
      } else {
        // BINARY mode
        switch (static_cast<PostgresValueType>(param_types[param_idx])) {
          case PostgresValueType::INTEGER: {
            int int_val = 0;
            for (size_t i = 0; i < sizeof(int); ++i) {
              int_val = (int_val << 8) | param[i];
            }
            bind_parameters[param_idx] =
                std::make_pair(type::Type::INTEGER, std::to_string(int_val));
            param_values[param_idx] =
                type::ValueFactory::GetIntegerValue(int_val).Copy();
            break;
          }
          case PostgresValueType::BIGINT: {
            int64_t int_val = 0;
            for (size_t i = 0; i < sizeof(int64_t); ++i) {
              int_val = (int_val << 8) | param[i];
            }
            bind_parameters[param_idx] =
                std::make_pair(type::Type::BIGINT, std::to_string(int_val));
            param_values[param_idx] =
                type::ValueFactory::GetBigIntValue(int_val).Copy();
            break;
          }
          case PostgresValueType::DOUBLE: {
            double float_val = 0;
            unsigned long buf = 0;
            for (size_t i = 0; i < sizeof(double); ++i) {
              buf = (buf << 8) | param[i];
            }
            PL_MEMCPY(&float_val, &buf, sizeof(double));
            bind_parameters[param_idx] =
                std::make_pair(type::Type::DECIMAL, std::to_string(float_val));
            param_values[param_idx] =
                type::ValueFactory::GetDecimalValue(float_val).Copy();
            break;
          }
          case PostgresValueType::VARBINARY: {
            bind_parameters[param_idx] = std::make_pair(
                type::Type::VARBINARY,
                std::string(reinterpret_cast<char *>(&param[0]), param_len));
            param_values[param_idx] = type::ValueFactory::GetVarbinaryValue(
                &param[0], param_len, true);
            break;
          }
          default: {
            LOG_ERROR("Do not support data type: %d", param_types[param_idx]);
            break;
          }
        }
        PL_ASSERT(param_values[param_idx].GetTypeId() != type::Type::INVALID);
      }
    }
  }
  auto end = pkt->ptr;
  return end - begin;
}
// For consistency, this function assumes the input vectors has the correct size
For consistency, this function assumes the input vectors has the correct size
For consistency this function assumes the input vectors has the correct size
size_t PacketManager::ReadParamValue(
    InputPacket *pkt, int num_params, std::vector<int32_t> &param_types,
    std::vector<std::pair<int, std::string>> &bind_parameters,
    std::vector<type::Value> &param_values, std::vector<int16_t> &formats) {
  auto begin = pkt->ptr;
  ByteBuf param;
  for (int param_idx = 0; param_idx < num_params; param_idx++) {
    int param_len = PacketGetInt(pkt, 4);
    if (param_len == -1) {
      auto peloton_type = PostgresValueTypeToPelotonValueType(
          static_cast<PostgresValueType>(param_types[param_idx]));
      bind_parameters[param_idx] =
          std::make_pair(peloton_type, std::string(""));
      param_values[param_idx] =
          type::ValueFactory::GetNullValueByType(peloton_type);
    } else {
      PacketGetBytes(pkt, param_len, param);
      if (formats[param_idx] == 0) {
        std::string param_str = std::string(std::begin(param), std::end(param));
        bind_parameters[param_idx] =
            std::make_pair(type::Type::VARCHAR, param_str);
        if (PostgresValueTypeToPelotonValueType(
                (PostgresValueType)param_types[param_idx]) ==
            type::Type::VARCHAR) {
          param_values[param_idx] =
              type::ValueFactory::GetVarcharValue(param_str);
        } else {
          param_values[param_idx] =
              (type::ValueFactory::GetVarcharValue(param_str))
                  .CastAs(PostgresValueTypeToPelotonValueType(
                      (PostgresValueType)param_types[param_idx]));
        }
        PL_ASSERT(param_values[param_idx].GetTypeId() != type::Type::INVALID);
      } else {
        switch (static_cast<PostgresValueType>(param_types[param_idx])) {
          case PostgresValueType::INTEGER: {
            int int_val = 0;
            for (size_t i = 0; i < sizeof(int); ++i) {
              int_val = (int_val << 8) | param[i];
            }
            bind_parameters[param_idx] =
                std::make_pair(type::Type::INTEGER, std::to_string(int_val));
            param_values[param_idx] =
                type::ValueFactory::GetIntegerValue(int_val).Copy();
            break;
          }
          case PostgresValueType::BIGINT: {
            int64_t int_val = 0;
            for (size_t i = 0; i < sizeof(int64_t); ++i) {
              int_val = (int_val << 8) | param[i];
            }
            bind_parameters[param_idx] =
                std::make_pair(type::Type::BIGINT, std::to_string(int_val));
            param_values[param_idx] =
                type::ValueFactory::GetBigIntValue(int_val).Copy();
            break;
          }
          case PostgresValueType::DOUBLE: {
            double float_val = 0;
            unsigned long buf = 0;
            for (size_t i = 0; i < sizeof(double); ++i) {
              buf = (buf << 8) | param[i];
            }
            PL_MEMCPY(&float_val, &buf, sizeof(double));
            bind_parameters[param_idx] =
                std::make_pair(type::Type::DECIMAL, std::to_string(float_val));
            param_values[param_idx] =
                type::ValueFactory::GetDecimalValue(float_val).Copy();
            break;
          }
          case PostgresValueType::VARBINARY: {
            bind_parameters[param_idx] = std::make_pair(
                type::Type::VARBINARY,
                std::string(reinterpret_cast<char *>(&param[0]), param_len));
            param_values[param_idx] = type::ValueFactory::GetVarbinaryValue(
                &param[0], param_len, true);
            break;
          }
          default: {
            LOG_ERROR("Do not support data type: %d", param_types[param_idx]);
            break;
          }
        }
        PL_ASSERT(param_values[param_idx].GetTypeId() != type::Type::INVALID);
      }
    }
  }
  auto end = pkt->ptr;
  return end - begin;
}
size_t PacketManager :: ReadParamValue ( InputPacket * pkt , int num_params , std :: vector < int32_t > & param_types , std :: vector < std :: pair < int , std :: string > > & bind_parameters , std :: vector < type :: Value > & param_values , std :: vector < int16_t > & formats ) { auto begin = pkt -> ptr ; ByteBuf param ; for ( int param_idx = 0 ; param_idx < num_params ; param_idx ++ ) { int param_len = PacketGetInt ( pkt , 4 ) ; if ( param_len == -1 ) { auto peloton_type = PostgresValueTypeToPelotonValueType ( static_cast < PostgresValueType > ( param_types [ param_idx ] ) ) ; bind_parameters [ param_idx ] = std :: make_pair ( peloton_type , std :: string ( " " ) ) ; param_values [ param_idx ] = type :: ValueFactory :: GetNullValueByType ( peloton_type ) ; } else { PacketGetBytes ( pkt , param_len , param ) ; if ( formats [ param_idx ] == 0 ) { std :: string param_str = std :: string ( std :: begin ( param ) , std :: end ( param ) ) ; bind_parameters [ param_idx ] = std :: make_pair ( type :: Type :: VARCHAR , param_str ) ; if ( PostgresValueTypeToPelotonValueType ( ( PostgresValueType ) param_types [ param_idx ] ) == type :: Type :: VARCHAR ) { param_values [ param_idx ] = type :: ValueFactory :: GetVarcharValue ( param_str ) ; } else { param_values [ param_idx ] = ( type :: ValueFactory :: GetVarcharValue ( param_str ) ) . CastAs ( PostgresValueTypeToPelotonValueType ( ( PostgresValueType ) param_types [ param_idx ] ) ) ; } PL_ASSERT ( param_values [ param_idx ] . GetTypeId ( ) != type :: Type :: INVALID ) ; } else { switch ( static_cast < PostgresValueType > ( param_types [ param_idx ] ) ) { case PostgresValueType :: INTEGER : { int int_val = 0 ; for ( size_t i = 0 ; i < sizeof ( int ) ; ++ i ) { int_val = ( int_val << 8 ) | param [ i ] ; } bind_parameters [ param_idx ] = std :: make_pair ( type :: Type :: INTEGER , std :: to_string ( int_val ) ) ; param_values [ param_idx ] = type :: ValueFactory :: GetIntegerValue ( int_val ) . Copy ( ) ; break ; } case PostgresValueType :: BIGINT : { int64_t int_val = 0 ; for ( size_t i = 0 ; i < sizeof ( int64_t ) ; ++ i ) { int_val = ( int_val << 8 ) | param [ i ] ; } bind_parameters [ param_idx ] = std :: make_pair ( type :: Type :: BIGINT , std :: to_string ( int_val ) ) ; param_values [ param_idx ] = type :: ValueFactory :: GetBigIntValue ( int_val ) . Copy ( ) ; break ; } case PostgresValueType :: DOUBLE : { double float_val = 0 ; unsigned long buf = 0 ; for ( size_t i = 0 ; i < sizeof ( double ) ; ++ i ) { buf = ( buf << 8 ) | param [ i ] ; } PL_MEMCPY ( & float_val , & buf , sizeof ( double ) ) ; bind_parameters [ param_idx ] = std :: make_pair ( type :: Type :: DECIMAL , std :: to_string ( float_val ) ) ; param_values [ param_idx ] = type :: ValueFactory :: GetDecimalValue ( float_val ) . Copy ( ) ; break ; } case PostgresValueType :: VARBINARY : { bind_parameters [ param_idx ] = std :: make_pair ( type :: Type :: VARBINARY , std :: string ( reinterpret_cast < char * > ( & param [ 0 ] ) , param_len ) ) ; param_values [ param_idx ] = type :: ValueFactory :: GetVarbinaryValue ( & param [ 0 ] , param_len , true ) ; break ; } default : { LOG_ERROR ( " " , param_types [ param_idx ] ) ; break ; } } PL_ASSERT ( param_values [ param_idx ] . GetTypeId ( ) != type :: Type :: INVALID ) ; } } } auto end = pkt -> ptr ; return end - begin ; }
For consistency, this function assumes the input vectors has the correct size
For consistency this function assumes the input vectors has the correct size
// BIND packet NULL parameter case // NULL mode // TEXT mode // BINARY mode