From eafdeeef211ae04c5f312e791f64a5520d2b3b5b Mon Sep 17 00:00:00 2001 From: Leo-XM-Zeng Date: Mon, 23 Dec 2024 21:52:52 +0800 Subject: [PATCH] Supports the PostgreSQL domain --- src/pgduckdb_types.cpp | 6 ++++-- test/regression/expected/domain.out | 31 +++++++++++++++++++++++++++++ test/regression/schedule | 1 + test/regression/sql/domain.sql | 23 +++++++++++++++++++++ 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 test/regression/expected/domain.out create mode 100644 test/regression/sql/domain.sql diff --git a/src/pgduckdb_types.cpp b/src/pgduckdb_types.cpp index ca234352..4bca4706 100644 --- a/src/pgduckdb_types.cpp +++ b/src/pgduckdb_types.cpp @@ -808,7 +808,9 @@ numeric_typmod_scale(int32 typmod) { duckdb::LogicalType ConvertPostgresToBaseDuckColumnType(Form_pg_attribute &attribute) { - switch (attribute->atttypid) { + /* If it's a domain, look at the base type instead */ + auto type_id = getBaseType(attribute->atttypid); + switch (type_id) { case BOOLOID: case BOOLARRAYOID: return duckdb::LogicalTypeId::BOOLEAN; @@ -867,7 +869,7 @@ ConvertPostgresToBaseDuckColumnType(Form_pg_attribute &attribute) { case REGCLASSARRAYOID: return duckdb::LogicalTypeId::UINTEGER; default: - return duckdb::LogicalType::USER("UnsupportedPostgresType (Oid=" + std::to_string(attribute->atttypid) + ")"); + return duckdb::LogicalType::USER("UnsupportedPostgresType (Oid=" + std::to_string(type_id) + ")"); } } diff --git a/test/regression/expected/domain.out b/test/regression/expected/domain.out new file mode 100644 index 00000000..6f59624d --- /dev/null +++ b/test/regression/expected/domain.out @@ -0,0 +1,31 @@ +create domain domainvarchar varchar(5); +create domain domainint4 int4; +create domain domaintext text; +create table basictest + ( testint4 domainint4 + , testtext domaintext + , testvarchar domainvarchar + ); +INSERT INTO basictest values ('88', 'haha', 'short'); -- Good +INSERT INTO basictest values ('88', 'haha', 'short text'); -- Bad varchar +ERROR: value too long for type character varying(5) +INSERT INTO basictest values ('888', 'haha', 'short'); +select * from basictest; + testint4 | testtext | testvarchar +----------+----------+------------- + 88 | haha | short + 888 | haha | short +(2 rows) + +select testtext || testvarchar as concat, testint4 + 12 as sum +from basictest; + concat | sum +-----------+----- + hahashort | 100 + hahashort | 900 +(2 rows) + +drop table basictest; +drop domain domainvarchar restrict; +drop domain domainint4 restrict; +drop domain domaintext; diff --git a/test/regression/schedule b/test/regression/schedule index 896686b6..d0ade0a4 100644 --- a/test/regression/schedule +++ b/test/regression/schedule @@ -26,3 +26,4 @@ test: transaction_errors test: secrets test: prepare test: function +test: domain diff --git a/test/regression/sql/domain.sql b/test/regression/sql/domain.sql new file mode 100644 index 00000000..219fd3c0 --- /dev/null +++ b/test/regression/sql/domain.sql @@ -0,0 +1,23 @@ +create domain domainvarchar varchar(5); +create domain domainint4 int4; +create domain domaintext text; + +create table basictest + ( testint4 domainint4 + , testtext domaintext + , testvarchar domainvarchar + ); + +INSERT INTO basictest values ('88', 'haha', 'short'); -- Good +INSERT INTO basictest values ('88', 'haha', 'short text'); -- Bad varchar +INSERT INTO basictest values ('888', 'haha', 'short'); + +select * from basictest; + +select testtext || testvarchar as concat, testint4 + 12 as sum +from basictest; + +drop table basictest; +drop domain domainvarchar restrict; +drop domain domainint4 restrict; +drop domain domaintext;