Skip to content

Commit

Permalink
refactor(c/driver/postgresql): No naked new in copy/reader.h (#1503)
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAyd authored Feb 1, 2024
1 parent 1b04cdf commit 53bfd1a
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions c/driver/postgresql/copy/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -702,18 +702,17 @@ static inline ArrowErrorCode ErrorCantConvert(ArrowError* error,
return EINVAL;
}

static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
ArrowSchema* schema,
PostgresCopyFieldReader** out,
ArrowError* error) {
static inline ArrowErrorCode MakeCopyFieldReader(
const PostgresType& pg_type, ArrowSchema* schema,
std::unique_ptr<PostgresCopyFieldReader>* out, ArrowError* error) {
ArrowSchemaView schema_view;
NANOARROW_RETURN_NOT_OK(ArrowSchemaViewInit(&schema_view, schema, nullptr));

switch (schema_view.type) {
case NANOARROW_TYPE_BOOL:
switch (pg_type.type_id()) {
case PostgresTypeId::kBool:
*out = new PostgresCopyBooleanFieldReader();
*out = std::make_unique<PostgresCopyBooleanFieldReader>();
return NANOARROW_OK;
default:
return ErrorCantConvert(error, pg_type, schema_view);
Expand All @@ -722,7 +721,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case NANOARROW_TYPE_INT16:
switch (pg_type.type_id()) {
case PostgresTypeId::kInt2:
*out = new PostgresCopyNetworkEndianFieldReader<int16_t>();
*out = std::make_unique<PostgresCopyNetworkEndianFieldReader<int16_t>>();
return NANOARROW_OK;
default:
return ErrorCantConvert(error, pg_type, schema_view);
Expand All @@ -733,7 +732,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case PostgresTypeId::kInt4:
case PostgresTypeId::kOid:
case PostgresTypeId::kRegproc:
*out = new PostgresCopyNetworkEndianFieldReader<int32_t>();
*out = std::make_unique<PostgresCopyNetworkEndianFieldReader<int32_t>>();
return NANOARROW_OK;
default:
return ErrorCantConvert(error, pg_type, schema_view);
Expand All @@ -742,7 +741,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case NANOARROW_TYPE_INT64:
switch (pg_type.type_id()) {
case PostgresTypeId::kInt8:
*out = new PostgresCopyNetworkEndianFieldReader<int64_t>();
*out = std::make_unique<PostgresCopyNetworkEndianFieldReader<int64_t>>();
return NANOARROW_OK;
default:
return ErrorCantConvert(error, pg_type, schema_view);
Expand All @@ -751,7 +750,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case NANOARROW_TYPE_FLOAT:
switch (pg_type.type_id()) {
case PostgresTypeId::kFloat4:
*out = new PostgresCopyNetworkEndianFieldReader<uint32_t>();
*out = std::make_unique<PostgresCopyNetworkEndianFieldReader<uint32_t>>();
return NANOARROW_OK;
default:
return ErrorCantConvert(error, pg_type, schema_view);
Expand All @@ -760,7 +759,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case NANOARROW_TYPE_DOUBLE:
switch (pg_type.type_id()) {
case PostgresTypeId::kFloat8:
*out = new PostgresCopyNetworkEndianFieldReader<uint64_t>();
*out = std::make_unique<PostgresCopyNetworkEndianFieldReader<uint64_t>>();
return NANOARROW_OK;
default:
return ErrorCantConvert(error, pg_type, schema_view);
Expand All @@ -774,10 +773,10 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case PostgresTypeId::kBpchar:
case PostgresTypeId::kName:
case PostgresTypeId::kEnum:
*out = new PostgresCopyBinaryFieldReader();
*out = std::make_unique<PostgresCopyBinaryFieldReader>();
return NANOARROW_OK;
case PostgresTypeId::kNumeric:
*out = new PostgresCopyNumericFieldReader();
*out = std::make_unique<PostgresCopyNumericFieldReader>();
return NANOARROW_OK;
default:
return ErrorCantConvert(error, pg_type, schema_view);
Expand All @@ -786,7 +785,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case NANOARROW_TYPE_BINARY:
// No need to check pg_type here: we can return the bytes of any
// Postgres type as binary.
*out = new PostgresCopyBinaryFieldReader();
*out = std::make_unique<PostgresCopyBinaryFieldReader>();
return NANOARROW_OK;

case NANOARROW_TYPE_LIST:
Expand All @@ -799,16 +798,15 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
return EINVAL;
}

auto array_reader = std::unique_ptr<PostgresCopyArrayFieldReader>(
new PostgresCopyArrayFieldReader());
auto array_reader = std::make_unique<PostgresCopyArrayFieldReader>();
array_reader->Init(pg_type);

PostgresCopyFieldReader* child_reader;
std::unique_ptr<PostgresCopyFieldReader> child_reader;
NANOARROW_RETURN_NOT_OK(MakeCopyFieldReader(
pg_type.child(0), schema->children[0], &child_reader, error));
array_reader->InitChild(std::unique_ptr<PostgresCopyFieldReader>(child_reader));
array_reader->InitChild(std::move(child_reader));

*out = array_reader.release();
*out = std::move(array_reader);
return NANOARROW_OK;
}
default:
Expand All @@ -827,19 +825,17 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
return EINVAL;
}

auto record_reader = std::unique_ptr<PostgresCopyRecordFieldReader>(
new PostgresCopyRecordFieldReader());
auto record_reader = std::make_unique<PostgresCopyRecordFieldReader>();
record_reader->Init(pg_type);

for (int64_t i = 0; i < pg_type.n_children(); i++) {
PostgresCopyFieldReader* child_reader;
std::unique_ptr<PostgresCopyFieldReader> child_reader;
NANOARROW_RETURN_NOT_OK(MakeCopyFieldReader(
pg_type.child(i), schema->children[i], &child_reader, error));
record_reader->AppendChild(
std::unique_ptr<PostgresCopyFieldReader>(child_reader));
record_reader->AppendChild(std::move(child_reader));
}

*out = record_reader.release();
*out = std::move(record_reader);
return NANOARROW_OK;
}
default:
Expand All @@ -849,12 +845,13 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case NANOARROW_TYPE_DATE32: {
// 2000-01-01
constexpr int32_t kPostgresDateEpoch = 10957;
*out = new PostgresCopyNetworkEndianFieldReader<int32_t, kPostgresDateEpoch>();
*out = std::make_unique<
PostgresCopyNetworkEndianFieldReader<int32_t, kPostgresDateEpoch>>();
return NANOARROW_OK;
}

case NANOARROW_TYPE_TIME64: {
*out = new PostgresCopyNetworkEndianFieldReader<int64_t>();
*out = std::make_unique<PostgresCopyNetworkEndianFieldReader<int64_t>>();
return NANOARROW_OK;
}

Expand All @@ -864,8 +861,8 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case PostgresTypeId::kTimestamptz: {
// 2000-01-01 00:00:00.000000 in microseconds
constexpr int64_t kPostgresTimestampEpoch = 946684800000000;
*out = new PostgresCopyNetworkEndianFieldReader<int64_t,
kPostgresTimestampEpoch>();
*out = std::make_unique<
PostgresCopyNetworkEndianFieldReader<int64_t, kPostgresTimestampEpoch>>();
return NANOARROW_OK;
}
default:
Expand All @@ -874,7 +871,7 @@ static inline ArrowErrorCode MakeCopyFieldReader(const PostgresType& pg_type,
case NANOARROW_TYPE_INTERVAL_MONTH_DAY_NANO:
switch (pg_type.type_id()) {
case PostgresTypeId::kInterval: {
*out = new PostgresCopyIntervalFieldReader();
*out = std::make_unique<PostgresCopyIntervalFieldReader>();
return NANOARROW_OK;
}
default:
Expand Down Expand Up @@ -940,10 +937,10 @@ class PostgresCopyStreamReader {

for (int64_t i = 0; i < root_type.n_children(); i++) {
const PostgresType& child_type = root_type.child(i);
PostgresCopyFieldReader* child_reader;
std::unique_ptr<PostgresCopyFieldReader> child_reader;
NANOARROW_RETURN_NOT_OK(
MakeCopyFieldReader(child_type, schema_->children[i], &child_reader, error));
root_reader_.AppendChild(std::unique_ptr<PostgresCopyFieldReader>(child_reader));
root_reader_.AppendChild(std::move(child_reader));
}

NANOARROW_RETURN_NOT_OK(root_reader_.InitSchema(schema_.get()));
Expand Down

0 comments on commit 53bfd1a

Please sign in to comment.