Skip to content

Commit

Permalink
make the integer OID mapping more generic so it can be used for other…
Browse files Browse the repository at this point in the history
… integer types
  • Loading branch information
Tishj committed May 29, 2024
1 parent 7f24a9d commit 5db8188
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
1 change: 0 additions & 1 deletion expected/array_type_support.out
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ INSERT INTO char_array_1d SELECT CAST(a as CHAR[]) FROM (VALUES
('{}')
) t(a);
SELECT * FROM char_array_1d;
ERROR: (DuckDB/ConvertPostgresToDuckColumnType) Unsupported quack type: 1014
DROP TABLE int_array_1d;
DROP TABLE bool_array_1d;
DROP TABLE char_array_1d;
2 changes: 1 addition & 1 deletion sql/array_type_support.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ SELECT * FROM bool_array_1d;
-- CHAR (single dimension)
CREATE TABLE char_array_1d(a CHAR[]);
INSERT INTO char_array_1d SELECT CAST(a as CHAR[]) FROM (VALUES
('{b, b, c}'),
('{a, b, c}'),
(NULL),
('{t, f, Z, A}'),
('{}')
Expand Down
52 changes: 43 additions & 9 deletions src/quack_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ namespace quack {
struct CharArray {
public:
static ArrayType *ConstructArray(Datum *datums, bool *nulls, int *count, int *lower_bound) {
return construct_md_array(datums, nulls, 1, count, lower_bound, CHAROID, 1, true, 'c');
return construct_md_array(datums, nulls, 1, count, lower_bound, CHAROID, 1, true, 'c');
}
static duckdb::LogicalTypeId ExpectedType() {
return duckdb::LogicalTypeId::TINYINT;
return duckdb::LogicalTypeId::TINYINT;
}
static Datum ConvertToPostgres(const duckdb::Value &val) {
return Datum(val.GetValue<int8_t>());
return Datum(val.GetValue<int8_t>());
}
};

Expand All @@ -46,20 +46,54 @@ struct BoolArray {
return duckdb::LogicalTypeId::BOOLEAN;
}
static Datum ConvertToPostgres(const duckdb::Value &val) {
return Int32GetDatum(val.GetValue<bool>());
return Datum(val.GetValue<bool>());
}
};

struct Int4Array {
template <int32_t OID>
struct PostgresIntegerOIDMapping {
};

template <>
struct PostgresIntegerOIDMapping<CHAROID> {
static constexpr int32_t postgres_oid = CHAROID;
using physical_type = int8_t;
static constexpr duckdb::LogicalTypeId duck_type_id = duckdb::LogicalTypeId::TINYINT;
};

template <>
struct PostgresIntegerOIDMapping<INT2OID> {
static constexpr int32_t postgres_oid = INT2OID;
using physical_type = int16_t;
static constexpr duckdb::LogicalTypeId duck_type_id = duckdb::LogicalTypeId::SMALLINT;
};

template <>
struct PostgresIntegerOIDMapping<INT4OID> {
static constexpr int32_t postgres_oid = INT4OID;
using physical_type = int32_t;
static constexpr duckdb::LogicalTypeId duck_type_id = duckdb::LogicalTypeId::INTEGER;
};

template <>
struct PostgresIntegerOIDMapping<INT8OID> {
static constexpr int32_t postgres_oid = INT8OID;
using physical_type = int64_t;
static constexpr duckdb::LogicalTypeId duck_type_id = duckdb::LogicalTypeId::BIGINT;
};

template <class MAPPING>
struct PODArray {
using physical_type = typename MAPPING::physical_type;
public:
static ArrayType *ConstructArray(Datum *datums, bool *nulls, int *count, int *lower_bound) {
return construct_md_array(datums, nulls, 1, count, lower_bound, INT4OID, sizeof(int32_t), true, 'i');
return construct_md_array(datums, nulls, 1, count, lower_bound, MAPPING::postgres_oid, sizeof(physical_type), true, 'i');
}
static duckdb::LogicalTypeId ExpectedType() {
return duckdb::LogicalTypeId::INTEGER;
return MAPPING::duck_type_id;
}
static Datum ConvertToPostgres(const duckdb::Value &val) {
return Int32GetDatum(val.GetValue<int32_t>());
return Int32GetDatum(val.GetValue<physical_type>());
}
};

Expand Down Expand Up @@ -299,7 +333,7 @@ ConvertDuckToPostgresValue(TupleTableSlot *slot, duckdb::Value &value, idx_t col
break;
}
case INT4ARRAYOID: {
ConvertDuckToPostgresArray<Int4Array>(slot, value, col);
ConvertDuckToPostgresArray<PODArray<PostgresIntegerOIDMapping<INT4OID>>>(slot, value, col);
break;
}
default:
Expand Down

0 comments on commit 5db8188

Please sign in to comment.