Skip to content

Commit

Permalink
Supports the PostgreSQL domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo-XM-Zeng committed Dec 23, 2024
1 parent 3b34bee commit eafdeee
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/pgduckdb_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) + ")");
}
}

Expand Down
31 changes: 31 additions & 0 deletions test/regression/expected/domain.out
Original file line number Diff line number Diff line change
@@ -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;
1 change: 1 addition & 0 deletions test/regression/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ test: transaction_errors
test: secrets
test: prepare
test: function
test: domain
23 changes: 23 additions & 0 deletions test/regression/sql/domain.sql
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit eafdeee

Please sign in to comment.