From a95d9678662d87ccdf0bb8e18c9d18c768f4c65e Mon Sep 17 00:00:00 2001 From: Lubo Slivka Date: Mon, 5 Feb 2024 20:07:11 +0100 Subject: [PATCH] fix(c/driver/postgresql): add postgres type to cols created for numeric - introduce constant with key name - introduce separate private method to add field-level method --- c/driver/postgresql/postgres_type.h | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/c/driver/postgresql/postgres_type.h b/c/driver/postgresql/postgres_type.h index e8de6e1ba4..a5d390b0ba 100644 --- a/c/driver/postgresql/postgres_type.h +++ b/c/driver/postgresql/postgres_type.h @@ -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 -------------------- @@ -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(buffer->data))); + NANOARROW_RETURN_NOT_OK(AddPostgresTypeMetadata(schema)); break; - } } NANOARROW_RETURN_NOT_OK(ArrowSchemaSetName(schema, field_name_.c_str())); @@ -309,6 +304,19 @@ class PostgresType { std::string typname_; std::string field_name_; std::vector 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(buffer->data))); + + return NANOARROW_OK; + } }; // Because type information is stored in a database's pg_type table, it can't