Skip to content

Commit

Permalink
fix(c/driver/postgresql): add postgres type to cols created for numeric
Browse files Browse the repository at this point in the history
- introduce constant with key name
- introduce separate private method to add field-level method
  • Loading branch information
lupko committed Feb 5, 2024
1 parent 3828e0b commit a95d967
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions c/driver/postgresql/postgres_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ class PostgresType {
// ---- Numeric/Decimal-------------------
case PostgresTypeId::kNumeric:
NANOARROW_RETURN_NOT_OK(ArrowSchemaSetType(schema, NANOARROW_TYPE_STRING));
NANOARROW_RETURN_NOT_OK(AddPostgresTypeMetadata(schema));

break;

// ---- Binary/string --------------------
Expand Down Expand Up @@ -279,20 +281,13 @@ class PostgresType {
break;

case PostgresTypeId::kUserDefined:
default: {
default:
// For user-defined types or types we don't explicitly know how to deal with, we
// can still return the bytes postgres gives us and attach the type name as
// metadata
NANOARROW_RETURN_NOT_OK(ArrowSchemaSetType(schema, NANOARROW_TYPE_BINARY));
nanoarrow::UniqueBuffer buffer;
ArrowMetadataBuilderInit(buffer.get(), nullptr);
NANOARROW_RETURN_NOT_OK(ArrowMetadataBuilderAppend(
buffer.get(), ArrowCharView("ADBC:postgresql:typname"),
ArrowCharView(typname_.c_str())));
NANOARROW_RETURN_NOT_OK(
ArrowSchemaSetMetadata(schema, reinterpret_cast<char*>(buffer->data)));
NANOARROW_RETURN_NOT_OK(AddPostgresTypeMetadata(schema));
break;
}
}

NANOARROW_RETURN_NOT_OK(ArrowSchemaSetName(schema, field_name_.c_str()));
Expand All @@ -309,6 +304,19 @@ class PostgresType {
std::string typname_;
std::string field_name_;
std::vector<PostgresType> children_;

static constexpr const char* kPostgresTypeKey = "ADBC:postgresql:typname";

ArrowErrorCode AddPostgresTypeMetadata(ArrowSchema* schema) const {
nanoarrow::UniqueBuffer buffer;
ArrowMetadataBuilderInit(buffer.get(), nullptr);
NANOARROW_RETURN_NOT_OK(ArrowMetadataBuilderAppend(
buffer.get(), ArrowCharView(kPostgresTypeKey), ArrowCharView(typname_.c_str())));
NANOARROW_RETURN_NOT_OK(
ArrowSchemaSetMetadata(schema, reinterpret_cast<char*>(buffer->data)));

return NANOARROW_OK;
}
};

// Because type information is stored in a database's pg_type table, it can't
Expand Down

0 comments on commit a95d967

Please sign in to comment.