Skip to content

Commit

Permalink
add UUID type support
Browse files Browse the repository at this point in the history
  • Loading branch information
Tishj committed May 22, 2024
1 parent 7da0e22 commit 86352d8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
16 changes: 16 additions & 0 deletions expected/type_support.out
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ INFO: HUGEINT
123456789.000000000000000000000001
(3 rows)

-- UUID
CREATE TABLE uuid_tbl(a UUID);
INSERT INTO uuid_tbl SELECT CAST(a as UUID) FROM (VALUES
('80bf0be9-89be-4ef8-bc58-fc7d691c5544'),
(NULL),
('00000000-0000-0000-0000-000000000000')
) t(a);
SELECT * FROM uuid_tbl;
a
--------------------------------------
80bf0be9-89be-4ef8-bc58-fc7d691c5544

00000000-0000-0000-0000-000000000000
(3 rows)

DROP TABLE chr;
DROP TABLE small;
DROP TABLE intgr;
Expand All @@ -220,3 +235,4 @@ DROP TABLE smallint_numeric;
DROP TABLE integer_numeric;
DROP TABLE bigint_numeric;
DROP TABLE hugeint_numeric;
DROP TABLE uuid_tbl;
10 changes: 10 additions & 0 deletions sql/type_support.sql
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ INSERT INTO hugeint_numeric SELECT a FROM (VALUES
) t(a);
SELECT * FROM hugeint_numeric;

-- UUID
CREATE TABLE uuid_tbl(a UUID);
INSERT INTO uuid_tbl SELECT CAST(a as UUID) FROM (VALUES
('80bf0be9-89be-4ef8-bc58-fc7d691c5544'),
(NULL),
('00000000-0000-0000-0000-000000000000')
) t(a);
SELECT * FROM uuid_tbl;

DROP TABLE chr;
DROP TABLE small;
DROP TABLE intgr;
Expand All @@ -122,3 +131,4 @@ DROP TABLE smallint_numeric;
DROP TABLE integer_numeric;
DROP TABLE bigint_numeric;
DROP TABLE hugeint_numeric;
DROP TABLE uuid_tbl;
38 changes: 36 additions & 2 deletions src/quack_types.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "duckdb.hpp"
#include "duckdb/common/extra_type_info.hpp"
#include "duckdb/common/types/uuid.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -8,6 +9,7 @@ extern "C" {
#include "catalog/pg_type.h"
#include "executor/tuptable.h"
#include "utils/numeric.h"
#include "utils/uuid.h"
}

#include "quack/types/decimal.hpp"
Expand Down Expand Up @@ -191,6 +193,24 @@ ConvertDuckToPostgresValue(TupleTableSlot *slot, duckdb::Value &value, idx_t col
slot->tts_values[col] = datum;
break;
}
case UUIDOID: {
D_ASSERT(value.type().id() == duckdb::LogicalTypeId::UUID);
D_ASSERT(value.type().InternalType() == duckdb::PhysicalType::INT128);
auto duckdb_uuid = value.GetValue<hugeint_t>();
pg_uuid_t *postgres_uuid = (pg_uuid_t *) palloc(sizeof(pg_uuid_t));

duckdb_uuid.upper ^= (uint64_t(1) << 63);
// Convert duckdb_uuid to bytes and store in postgres_uuid.data
uint8_t *uuid_bytes = (uint8_t *)&duckdb_uuid;

for (int i = 0; i < UUID_LEN; ++i) {
postgres_uuid->data[i] = uuid_bytes[UUID_LEN - 1 - i];
}

auto datum = UUIDPGetDatum(postgres_uuid);
slot->tts_values[col] = datum;
break;
}
default:
elog(ERROR, "(DuckDB/ConvertDuckToPostgresValue) Unsuported quack type: %d", oid);
}
Expand Down Expand Up @@ -242,6 +262,8 @@ ConvertPostgresToDuckColumnType(Oid type, int32_t typmod) {
}
return duckdb::LogicalType::DECIMAL(precision, scale);
}
case UUIDOID:
return duckdb::LogicalTypeId::UUID;
default:
elog(ERROR, "(DuckDB/ConvertPostgresToDuckColumnType) Unsupported quack type: %d", type);
}
Expand Down Expand Up @@ -275,6 +297,8 @@ GetPostgresDuckDBType(duckdb::LogicalTypeId type) {
case duckdb::LogicalTypeId::DECIMAL: {
return NUMERICOID;
}
case duckdb::LogicalTypeId::UUID:
return UUIDOID;
default:
elog(ERROR, "(DuckDB/GetPostgresDuckDBType) Unsupported quack type: %d", static_cast<int>(type));
}
Expand Down Expand Up @@ -436,9 +460,19 @@ ConvertPostgresToDuckValue(Datum value, duckdb::Vector &result, idx_t offset) {
}
break;
}
case duckdb::LogicalTypeId::UUID: {
auto uuid = DatumGetPointer(value);
hugeint_t duckdb_uuid;
D_ASSERT(UUID_LEN == sizeof(hugeint_t));
for (idx_t i = 0; i < UUID_LEN; i++) {
((uint8_t*)&duckdb_uuid)[UUID_LEN-1-i] = ((uint8_t*)uuid)[i];
}
duckdb_uuid.upper ^= (uint64_t(1) << 63);
Append(result, duckdb_uuid, offset);
break;
}
default:
elog(ERROR, "(DuckDB/ConvertPostgresToDuckValue) Unsupported quack type: %d",
static_cast<int>(result.GetType().id()));
elog(ERROR, "(DuckDB/ConvertPostgresToDuckValue) Unsupported quack type: %s", duckdb::EnumUtil::ToChars(result.GetType().id()));
break;
}
}
Expand Down

0 comments on commit 86352d8

Please sign in to comment.