-
Notifications
You must be signed in to change notification settings - Fork 94
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
feat(c/driver/postgresql): Enable basic connect/query workflow for Redshift #2219
Open
paleolimbot
wants to merge
21
commits into
apache:main
Choose a base branch
from
paleolimbot:c-driver-postgresql-redshift
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bc69e99
tire kicking
paleolimbot 3ae81b1
handle redshift versioning
paleolimbot 07cf6b6
set use copy by default
paleolimbot 148f44d
nix previous type
paleolimbot ae2f052
missing headers
paleolimbot 1ac3bc6
Update c/driver/postgresql/connection.cc
paleolimbot 98c633f
Update c/driver/postgresql/connection.cc
paleolimbot c220c81
better parsing
paleolimbot 646d08c
use result helper
paleolimbot 98d54f8
use helpers for building type table
paleolimbot afedae1
condense type query logic
paleolimbot 4eb70e0
move detail function
paleolimbot 775d4cd
use status
paleolimbot d443eda
pass on vendor to opaque output type
paleolimbot d47ee45
slightly better type query
paleolimbot 56b80d1
allow type lookups to fail
paleolimbot 8a11b39
try to execute a few queries
paleolimbot 038204d
format
paleolimbot 0828f00
try some static casts for msvc
paleolimbot 73af17a
one more
paleolimbot 562c1c1
slightly better enum name
paleolimbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ | |
|
||
#include "connection.h" | ||
|
||
#include <array> | ||
#include <cassert> | ||
#include <cinttypes> | ||
#include <cmath> | ||
|
@@ -478,19 +479,35 @@ AdbcStatusCode PostgresConnection::GetInfo(struct AdbcConnection* connection, | |
for (size_t i = 0; i < info_codes_length; i++) { | ||
switch (info_codes[i]) { | ||
case ADBC_INFO_VENDOR_NAME: | ||
infos.push_back({info_codes[i], "PostgreSQL"}); | ||
if (RedshiftVersion()[0] > 0) { | ||
infos.emplace_back(info_codes[i], "Redshift"); | ||
} else { | ||
infos.push_back({info_codes[i], "PostgreSQL"}); | ||
} | ||
|
||
break; | ||
case ADBC_INFO_VENDOR_VERSION: { | ||
const char* stmt = "SHOW server_version_num"; | ||
auto result_helper = PqResultHelper{conn_, std::string(stmt)}; | ||
RAISE_STATUS(error, result_helper.Execute()); | ||
auto it = result_helper.begin(); | ||
if (it == result_helper.end()) { | ||
SetError(error, "[libpq] PostgreSQL returned no rows for '%s'", stmt); | ||
return ADBC_STATUS_INTERNAL; | ||
if (RedshiftVersion()[0] > 0) { | ||
std::array<int, 3> version = RedshiftVersion(); | ||
std::string version_string = std::to_string(version[0]) + "." + | ||
std::to_string(version[1]) + "." + | ||
std::to_string(version[2]); | ||
infos.emplace_back(info_codes[i], std::move(version_string)); | ||
|
||
} else { | ||
// Gives a version in the form 140000 instead of 14.0.0 | ||
const char* stmt = "SHOW server_version_num"; | ||
auto result_helper = PqResultHelper{conn_, std::string(stmt)}; | ||
RAISE_STATUS(error, result_helper.Execute()); | ||
auto it = result_helper.begin(); | ||
if (it == result_helper.end()) { | ||
SetError(error, "[libpq] PostgreSQL returned no rows for '%s'", stmt); | ||
return ADBC_STATUS_INTERNAL; | ||
} | ||
const char* server_version_num = (*it)[0].data; | ||
infos.push_back({info_codes[i], server_version_num}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's another spot |
||
} | ||
const char* server_version_num = (*it)[0].data; | ||
infos.push_back({info_codes[i], server_version_num}); | ||
|
||
break; | ||
} | ||
case ADBC_INFO_DRIVER_NAME: | ||
|
@@ -1136,4 +1153,12 @@ AdbcStatusCode PostgresConnection::SetOptionInt(const char* key, int64_t value, | |
return ADBC_STATUS_NOT_IMPLEMENTED; | ||
} | ||
|
||
std::array<int, 3> PostgresConnection::PostgreSQLVersion() { | ||
return database_->PostgreSQLVersion(); | ||
} | ||
|
||
std::array<int, 3> PostgresConnection::RedshiftVersion() { | ||
return database_->RedshiftVersion(); | ||
} | ||
|
||
} // namespace adbcpq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,8 @@ | |
|
||
#include "database.h" | ||
|
||
#include <array> | ||
#include <charconv> | ||
#include <cinttypes> | ||
#include <cstring> | ||
#include <memory> | ||
|
@@ -28,6 +30,7 @@ | |
#include <nanoarrow/nanoarrow.h> | ||
|
||
#include "driver/common/utils.h" | ||
#include "result_helper.h" | ||
|
||
namespace adbcpq { | ||
|
||
|
@@ -54,8 +57,19 @@ AdbcStatusCode PostgresDatabase::GetOptionDouble(const char* option, double* val | |
} | ||
|
||
AdbcStatusCode PostgresDatabase::Init(struct AdbcError* error) { | ||
// Connect to validate the parameters. | ||
return RebuildTypeResolver(error); | ||
// Connect to initialize the version information and build the type table | ||
PGconn* conn = nullptr; | ||
RAISE_ADBC(Connect(&conn, error)); | ||
|
||
AdbcStatusCode code = InitVersions(conn, error); | ||
if (code != ADBC_STATUS_OK) { | ||
RAISE_ADBC(Disconnect(&conn, nullptr)); | ||
return code; | ||
} | ||
|
||
code = RebuildTypeResolver(conn, error); | ||
RAISE_ADBC(Disconnect(&conn, nullptr)); | ||
return code; | ||
} | ||
|
||
AdbcStatusCode PostgresDatabase::Release(struct AdbcError* error) { | ||
|
@@ -123,19 +137,88 @@ AdbcStatusCode PostgresDatabase::Disconnect(PGconn** conn, struct AdbcError* err | |
return ADBC_STATUS_OK; | ||
} | ||
|
||
namespace { | ||
|
||
// Parse an individual version in the form of "xxx.xxx.xxx". | ||
// If the version components aren't numeric, they will be zero. | ||
std::array<int, 3> ParseVersion(std::string_view version) { | ||
std::array<int, 3> out{}; | ||
size_t component = 0; | ||
size_t component_begin = 0; | ||
size_t component_end = 0; | ||
|
||
// While there are remaining version components and we haven't reached the end of the | ||
// string | ||
while (component_begin < version.size() && component < out.size()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a good use case for str::find here? |
||
// Find the next character that marks a version component separation or the end of the | ||
// string | ||
while (component_end < version.size() && version[component_end] != '.' && | ||
version[component_end] != '-') { | ||
component_end++; | ||
} | ||
|
||
// Try to parse the component as an integer (assigning zero if this fails) | ||
int value = 0; | ||
std::from_chars(version.data() + component_begin, version.data() + component_end, | ||
value); | ||
out[component] = value; | ||
|
||
// Move on to the next component | ||
component_begin = component_end + 1; | ||
component_end = component_begin; | ||
component++; | ||
} | ||
|
||
return out; | ||
} | ||
|
||
// Parse the PostgreSQL version() string that looks like: | ||
// PostgreSQL 8.0.2 on i686-pc-linux-gnu, compiled by GCC gcc (GCC) 3.4.2 20041017 (Red | ||
// Hat 3.4.2-6.fc3), Redshift 1.0.77467 | ||
std::array<int, 3> ParsePrefixedVersion(std::string_view version_info, | ||
std::string_view prefix) { | ||
size_t pos = version_info.find(prefix); | ||
if (pos == version_info.npos) { | ||
return {0, 0, 0}; | ||
} | ||
|
||
// Skip the prefix and any leading whitespace | ||
pos += prefix.size(); | ||
while (pos < version_info.size() && version_info[pos] == ' ') { | ||
++pos; | ||
} | ||
|
||
return ParseVersion(version_info.substr(pos)); | ||
} | ||
|
||
} // namespace | ||
|
||
AdbcStatusCode PostgresDatabase::InitVersions(PGconn* conn, struct AdbcError* error) { | ||
PqResultHelper helper(conn, "SELECT version();"); | ||
RAISE_STATUS(error, helper.Execute()); | ||
if (helper.NumRows() != 1 || helper.NumColumns() != 1) { | ||
SetError(error, "Expected 1 row and 1 column for SELECT version(); but got %d/%d", | ||
helper.NumRows(), helper.NumColumns()); | ||
return ADBC_STATUS_INTERNAL; | ||
} | ||
|
||
std::string_view version_info = helper.Row(0)[0].value(); | ||
postgres_server_version_ = ParsePrefixedVersion(version_info, "PostgreSQL"); | ||
redshift_server_version_ = ParsePrefixedVersion(version_info, "Redshift"); | ||
|
||
return ADBC_STATUS_OK; | ||
} | ||
|
||
// Helpers for building the type resolver from queries | ||
static inline int32_t InsertPgAttributeResult( | ||
PGresult* result, const std::shared_ptr<PostgresTypeResolver>& resolver); | ||
|
||
static inline int32_t InsertPgTypeResult( | ||
PGresult* result, const std::shared_ptr<PostgresTypeResolver>& resolver); | ||
|
||
AdbcStatusCode PostgresDatabase::RebuildTypeResolver(struct AdbcError* error) { | ||
PGconn* conn = nullptr; | ||
AdbcStatusCode final_status = Connect(&conn, error); | ||
if (final_status != ADBC_STATUS_OK) { | ||
return final_status; | ||
} | ||
AdbcStatusCode PostgresDatabase::RebuildTypeResolver(PGconn* conn, | ||
struct AdbcError* error) { | ||
AdbcStatusCode final_status = ADBC_STATUS_OK; | ||
|
||
// We need a few queries to build the resolver. The current strategy might | ||
// fail for some recursive definitions (e.g., arrays of records of arrays). | ||
|
@@ -162,8 +245,8 @@ SELECT | |
typname, | ||
typreceive, | ||
typbasetype, | ||
typarray, | ||
typrelid | ||
typrelid, | ||
typarray | ||
FROM | ||
pg_catalog.pg_type | ||
WHERE | ||
|
@@ -172,6 +255,28 @@ ORDER BY | |
oid | ||
)"; | ||
|
||
const std::string kTypeQueryNoArrays = R"( | ||
SELECT | ||
oid, | ||
typname, | ||
typreceive, | ||
typbasetype, | ||
typrelid | ||
FROM | ||
pg_catalog.pg_type | ||
WHERE | ||
(typreceive != 0 OR typname = 'aclitem') AND typtype != 'r' | ||
ORDER BY | ||
oid | ||
)"; | ||
|
||
std::string type_query; | ||
if (redshift_server_version_[0] == 0) { | ||
type_query = kTypeQuery; | ||
} else { | ||
type_query = kTypeQueryNoArrays; | ||
} | ||
|
||
// Create a new type resolver (this instance's type_resolver_ member | ||
// will be updated at the end if this succeeds). | ||
auto resolver = std::make_shared<PostgresTypeResolver>(); | ||
|
@@ -192,7 +297,7 @@ ORDER BY | |
// Attempt filling the resolver a few times to handle recursive definitions. | ||
int32_t max_attempts = 3; | ||
for (int32_t i = 0; i < max_attempts; i++) { | ||
result = PQexec(conn, kTypeQuery.c_str()); | ||
result = PQexec(conn, type_query.c_str()); | ||
ExecStatusType pq_status = PQresultStatus(result); | ||
if (pq_status == PGRES_TUPLES_OK) { | ||
InsertPgTypeResult(result, resolver); | ||
|
@@ -208,12 +313,6 @@ ORDER BY | |
} | ||
} | ||
|
||
// Disconnect since PostgreSQL connections can be heavy. | ||
{ | ||
AdbcStatusCode status = Disconnect(&conn, error); | ||
if (status != ADBC_STATUS_OK) final_status = status; | ||
} | ||
|
||
if (final_status == ADBC_STATUS_OK) { | ||
type_resolver_ = std::move(resolver); | ||
} | ||
|
@@ -256,6 +355,7 @@ static inline int32_t InsertPgAttributeResult( | |
static inline int32_t InsertPgTypeResult( | ||
PGresult* result, const std::shared_ptr<PostgresTypeResolver>& resolver) { | ||
int num_rows = PQntuples(result); | ||
int num_cols = PQnfields(result); | ||
PostgresTypeResolver::Item item; | ||
int32_t n_added = 0; | ||
|
||
|
@@ -266,10 +366,16 @@ static inline int32_t InsertPgTypeResult( | |
const char* typreceive = PQgetvalue(result, row, 2); | ||
const uint32_t typbasetype = static_cast<uint32_t>( | ||
std::strtol(PQgetvalue(result, row, 3), /*str_end=*/nullptr, /*base=*/10)); | ||
const uint32_t typarray = static_cast<uint32_t>( | ||
std::strtol(PQgetvalue(result, row, 4), /*str_end=*/nullptr, /*base=*/10)); | ||
const uint32_t typrelid = static_cast<uint32_t>( | ||
std::strtol(PQgetvalue(result, row, 5), /*str_end=*/nullptr, /*base=*/10)); | ||
std::strtol(PQgetvalue(result, row, 4), /*str_end=*/nullptr, /*base=*/10)); | ||
|
||
uint32_t typarray; | ||
if (num_cols == 6) { | ||
typarray = static_cast<uint32_t>( | ||
std::strtol(PQgetvalue(result, row, 5), /*str_end=*/nullptr, /*base=*/10)); | ||
} else { | ||
typarray = 0; | ||
} | ||
|
||
// Special case the aclitem because it shows up in a bunch of internal tables | ||
if (strcmp(typname, "aclitem") == 0) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have been clearer but all of the push_back's here I think are better with emplace_back
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mind either way, but most advice I read tends to suggest only using emplace back in specific cases (e.g., https://abseil.io/tips/112 ).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C++...what a language.
Well in this case either is likely fine. I am under the impression that emplace_back would avoid any calls to the move constructor of the list element, along with any move constructors that need to be called when the vector is resized. In this particular case it probably doesn't make a difference; maybe something to just look at when performance is more critical
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find any existing
emplace_back()
usage so I changed these back. We can always reevaluate!