-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support the PostgreSQL domain (#532)
Domain types are basically user defined aliases of existing types. This starts supporting columns of such domain types pg_duckdb. One interesting thing is that domain types can also contain constraints, e.g. "an int that is less than 10". This doesn't really matter for reading them though, their actual representation is still always the same as the underlying type. It also doesn't matter when doing operations on them, because Postgres already changes a domain type to its underlying representation automatically. So any constraints on the domain don't apply to a result of an operation (e.g. addition). Fixes #514
- Loading branch information
1 parent
05e42f0
commit 8cf199b
Showing
8 changed files
with
208 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
create domain domainvarchar varchar(5) check (value is not null); | ||
create domain domainnumeric numeric(8,2) check (value is not null); | ||
create domain domainint4 int4 check (value > 0); | ||
create domain domaintext text check (value is not null); | ||
-- Test tables using domains | ||
create table basictest | ||
( testint4 domainint4 | ||
, testtext domaintext | ||
, testvarchar domainvarchar | ||
, testnumeric domainnumeric | ||
); | ||
-- In fact, when we carry out an INSERT operation, the check regarding "domain" is conducted by PostgreSQL rather than DuckDB. | ||
-- When we execute a SELECT operation and the field of the query is "domain", we will convert it to the corresponding base type | ||
-- and let DuckDB handle it. | ||
INSERT INTO basictest values ('88', 'haha', 'short', '123.12'); -- Good | ||
INSERT INTO basictest values ('88', 'haha', 'short text', '123.12'); -- Bad varchar | ||
ERROR: value too long for type character varying(5) | ||
INSERT INTO basictest values ('88', 'haha', 'short', '123.1212'); -- Truncate numeric | ||
-- not support. It will be converted to the following statement | ||
-- SELECT ('-5'::integer)::domainint4 AS domainint4 FROM pgduckdb.xxx.basictest | ||
SELECT (-5)::domainint4 FROM basictest; | ||
WARNING: (PGDuckDB/CreatePlan) Prepared query returned an error: 'Catalog Error: Type with name domainint4 does not exist! | ||
Did you mean "tinyint"? | ||
ERROR: value for domain domainint4 violates check constraint "domainint4_check" | ||
select * from basictest; | ||
testint4 | testtext | testvarchar | testnumeric | ||
----------+----------+-------------+------------- | ||
88 | haha | short | 123.12 | ||
88 | haha | short | 123.12 | ||
(2 rows) | ||
|
||
select testtext || testvarchar as concat, testnumeric + 42 as sum | ||
from basictest; | ||
concat | sum | ||
-----------+--------- | ||
hahashort | 165.120 | ||
hahashort | 165.120 | ||
(2 rows) | ||
|
||
select * from basictest where testtext = 'haha'; | ||
testint4 | testtext | testvarchar | testnumeric | ||
----------+----------+-------------+------------- | ||
88 | haha | short | 123.12 | ||
88 | haha | short | 123.12 | ||
(2 rows) | ||
|
||
select * from basictest where testvarchar = 'short'; | ||
testint4 | testtext | testvarchar | testnumeric | ||
----------+----------+-------------+------------- | ||
88 | haha | short | 123.12 | ||
88 | haha | short | 123.12 | ||
(2 rows) | ||
|
||
-- array_domain | ||
create domain domain_int_array as INT[]; | ||
CREATE TABLE domain_int_array_1d(a domain_int_array); | ||
INSERT INTO domain_int_array_1d SELECT CAST(a as domain_int_array) FROM (VALUES | ||
('{1, 2, 3}'), | ||
(NULL), | ||
('{4, 5, NULL, 7}'), | ||
('{}') | ||
) t(a); | ||
SELECT * FROM domain_int_array_1d; | ||
a | ||
-------------- | ||
{1,2,3} | ||
|
||
{4,5,NULL,7} | ||
{} | ||
(4 rows) | ||
|
||
CREATE TABLE domain_int_array_2d(a domainint4[]); | ||
INSERT INTO domain_int_array_2d SELECT CAST(a as domain_int_array) FROM (VALUES | ||
('{1, 2, 3}'), | ||
(NULL), | ||
('{4, 5, NULL, 7}'), | ||
('{}') | ||
) t(a); | ||
SELECT * FROM domain_int_array_2d; | ||
a | ||
-------------- | ||
{1,2,3} | ||
|
||
{4,5,NULL,7} | ||
{} | ||
(4 rows) | ||
|
||
drop table domain_int_array_2d; | ||
drop table domain_int_array_1d; | ||
drop domain domain_int_array; | ||
drop table basictest; | ||
drop domain domainvarchar restrict; | ||
drop domain domainnumeric restrict; | ||
drop domain domainint4 restrict; | ||
drop domain domaintext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
create domain domainvarchar varchar(5) check (value is not null); | ||
create domain domainnumeric numeric(8,2) check (value is not null); | ||
create domain domainint4 int4 check (value > 0); | ||
create domain domaintext text check (value is not null); | ||
|
||
-- Test tables using domains | ||
create table basictest | ||
( testint4 domainint4 | ||
, testtext domaintext | ||
, testvarchar domainvarchar | ||
, testnumeric domainnumeric | ||
); | ||
|
||
-- In fact, when we carry out an INSERT operation, the check regarding "domain" is conducted by PostgreSQL rather than DuckDB. | ||
-- When we execute a SELECT operation and the field of the query is "domain", we will convert it to the corresponding base type | ||
-- and let DuckDB handle it. | ||
INSERT INTO basictest values ('88', 'haha', 'short', '123.12'); -- Good | ||
INSERT INTO basictest values ('88', 'haha', 'short text', '123.12'); -- Bad varchar | ||
INSERT INTO basictest values ('88', 'haha', 'short', '123.1212'); -- Truncate numeric | ||
|
||
-- not support. It will be converted to the following statement | ||
-- SELECT ('-5'::integer)::domainint4 AS domainint4 FROM pgduckdb.xxx.basictest | ||
SELECT (-5)::domainint4 FROM basictest; | ||
|
||
select * from basictest; | ||
|
||
select testtext || testvarchar as concat, testnumeric + 42 as sum | ||
from basictest; | ||
|
||
select * from basictest where testtext = 'haha'; | ||
select * from basictest where testvarchar = 'short'; | ||
|
||
-- array_domain | ||
create domain domain_int_array as INT[]; | ||
CREATE TABLE domain_int_array_1d(a domain_int_array); | ||
INSERT INTO domain_int_array_1d SELECT CAST(a as domain_int_array) FROM (VALUES | ||
('{1, 2, 3}'), | ||
(NULL), | ||
('{4, 5, NULL, 7}'), | ||
('{}') | ||
) t(a); | ||
SELECT * FROM domain_int_array_1d; | ||
|
||
CREATE TABLE domain_int_array_2d(a domainint4[]); | ||
INSERT INTO domain_int_array_2d SELECT CAST(a as domain_int_array) FROM (VALUES | ||
('{1, 2, 3}'), | ||
(NULL), | ||
('{4, 5, NULL, 7}'), | ||
('{}') | ||
) t(a); | ||
SELECT * FROM domain_int_array_2d; | ||
|
||
drop table domain_int_array_2d; | ||
drop table domain_int_array_1d; | ||
drop domain domain_int_array; | ||
drop table basictest; | ||
drop domain domainvarchar restrict; | ||
drop domain domainnumeric restrict; | ||
drop domain domainint4 restrict; | ||
drop domain domaintext; |