Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(c/driver/postgresql): update with C++17 conventions #1540

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions c/driver/postgresql/connection.cc
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ AdbcStatusCode PostgresConnectionGetStatisticsImpl(PGconn* conn, const char* db_

for (PqResultRow row : result_helper) {
auto reltuples = row[5].ParseDouble();
if (!reltuples.first) {
if (!reltuples) {
SetError(error, "[libpq] Invalid double value in reltuples: '%s'", row[5].data);
return ADBC_STATUS_INTERNAL;
}
Expand All @@ -946,8 +946,7 @@ AdbcStatusCode PostgresConnectionGetStatisticsImpl(PGconn* conn, const char* db_
CHECK_NA(INTERNAL,
ArrowArrayAppendInt(statistics_key_col, ADBC_STATISTIC_ROW_COUNT_KEY),
error);
CHECK_NA(INTERNAL, ArrowArrayAppendDouble(value_float64_col, reltuples.second),
error);
CHECK_NA(INTERNAL, ArrowArrayAppendDouble(value_float64_col, *reltuples), error);
CHECK_NA(INTERNAL,
ArrowArrayFinishUnionElement(statistics_value_col, kStatsVariantFloat64),
error);
Expand All @@ -957,7 +956,7 @@ AdbcStatusCode PostgresConnectionGetStatisticsImpl(PGconn* conn, const char* db_
}

auto null_frac = row[2].ParseDouble();
if (!null_frac.first) {
if (!null_frac) {
SetError(error, "[libpq] Invalid double value in null_frac: '%s'", row[2].data);
return ADBC_STATUS_INTERNAL;
}
Expand All @@ -973,18 +972,16 @@ AdbcStatusCode PostgresConnectionGetStatisticsImpl(PGconn* conn, const char* db_
CHECK_NA(INTERNAL,
ArrowArrayAppendInt(statistics_key_col, ADBC_STATISTIC_NULL_COUNT_KEY),
error);
CHECK_NA(
INTERNAL,
ArrowArrayAppendDouble(value_float64_col, null_frac.second * reltuples.second),
error);
CHECK_NA(INTERNAL,
ArrowArrayAppendDouble(value_float64_col, *null_frac * *reltuples), error);
CHECK_NA(INTERNAL,
ArrowArrayFinishUnionElement(statistics_value_col, kStatsVariantFloat64),
error);
CHECK_NA(INTERNAL, ArrowArrayAppendInt(statistics_is_approximate_col, 1), error);
CHECK_NA(INTERNAL, ArrowArrayFinishElement(db_schema_statistics_items), error);

auto average_byte_width = row[3].ParseDouble();
if (!average_byte_width.first) {
if (!average_byte_width) {
SetError(error, "[libpq] Invalid double value in avg_width: '%s'", row[3].data);
return ADBC_STATUS_INTERNAL;
}
Expand All @@ -1001,8 +998,7 @@ AdbcStatusCode PostgresConnectionGetStatisticsImpl(PGconn* conn, const char* db_
INTERNAL,
ArrowArrayAppendInt(statistics_key_col, ADBC_STATISTIC_AVERAGE_BYTE_WIDTH_KEY),
error);
CHECK_NA(INTERNAL,
ArrowArrayAppendDouble(value_float64_col, average_byte_width.second),
CHECK_NA(INTERNAL, ArrowArrayAppendDouble(value_float64_col, *average_byte_width),
error);
CHECK_NA(INTERNAL,
ArrowArrayFinishUnionElement(statistics_value_col, kStatsVariantFloat64),
Expand All @@ -1011,7 +1007,7 @@ AdbcStatusCode PostgresConnectionGetStatisticsImpl(PGconn* conn, const char* db_
CHECK_NA(INTERNAL, ArrowArrayFinishElement(db_schema_statistics_items), error);

auto n_distinct = row[4].ParseDouble();
if (!n_distinct.first) {
if (!n_distinct) {
SetError(error, "[libpq] Invalid double value in avg_width: '%s'", row[4].data);
return ADBC_STATUS_INTERNAL;
}
Expand All @@ -1031,13 +1027,11 @@ AdbcStatusCode PostgresConnectionGetStatisticsImpl(PGconn* conn, const char* db_
// > the column. If less than zero, the negative of the number of
// > distinct values divided by the number of rows.
// https://www.postgresql.org/docs/current/view-pg-stats.html
CHECK_NA(
INTERNAL,
ArrowArrayAppendDouble(value_float64_col,
n_distinct.second > 0
? n_distinct.second
: (std::fabs(n_distinct.second) * reltuples.second)),
error);
CHECK_NA(INTERNAL,
ArrowArrayAppendDouble(
value_float64_col,
*n_distinct > 0 ? *n_distinct : (std::fabs(*n_distinct) * *reltuples)),
error);
CHECK_NA(INTERNAL,
ArrowArrayFinishUnionElement(statistics_value_col, kStatsVariantFloat64),
error);
Expand Down
8 changes: 4 additions & 4 deletions c/driver/postgresql/result_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#pragma once

#include <cassert>
#include <optional>
#include <string>
#include <utility>
#include <vector>
Expand All @@ -33,14 +34,13 @@ struct PqRecord {
const int len;
const bool is_null;

// XXX: can't use optional due to R
std::pair<bool, double> ParseDouble() const {
std::optional<double> ParseDouble() const {
char* end;
double result = std::strtod(data, &end);
if (errno != 0 || end == data) {
return std::make_pair(false, 0.0);
return std::nullopt;
}
return std::make_pair(true, result);
return result;
}
};

Expand Down
2 changes: 1 addition & 1 deletion c/driver/postgresql/statement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ AdbcStatusCode PostgresStatement::SetupReader(struct AdbcError* error) {

// Initialize the copy reader and infer the output schema (i.e., error for
// unsupported types before issuing the COPY query)
reader_.copy_reader_.reset(new PostgresCopyStreamReader());
reader_.copy_reader_ = std::make_unique<PostgresCopyStreamReader>();
reader_.copy_reader_->Init(root_type);
struct ArrowError na_error;
int na_res = reader_.copy_reader_->InferOutputSchema(&na_error);
Expand Down
Loading