Use daterange and YMD in without_overlaps tests instead of tsrange.

This makes things a lot easier to read, especially when we get to the
FOREIGN KEY tests later.

Author: Paul A. Jungwirth <pj@illuminatedcomputing.com>
Discussion: https://www.postgresql.org/message-id/flat/CA+renyUApHgSZF9-nd-a0+OPGharLQLO=mDHcY4_qQ0+noCUVg@mail.gmail.com
This commit is contained in:
Peter Eisentraut 2024-03-19 10:17:03 +01:00
parent 794f10f6b9
commit a88c800deb
2 changed files with 62 additions and 56 deletions

View File

@ -3,12 +3,13 @@
-- We leave behind several tables to test pg_dump etc:
-- temporal_rng, temporal_rng2,
-- temporal_fk_rng2rng.
SET datestyle TO ISO, YMD;
--
-- test input parser
--
-- PK with no columns just WITHOUT OVERLAPS:
CREATE TABLE temporal_rng (
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng_pk PRIMARY KEY (valid_at WITHOUT OVERLAPS)
);
ERROR: constraint using WITHOUT OVERLAPS needs at least two columns
@ -34,7 +35,7 @@ CREATE TABLE temporal_rng (
-- use an int4range instead of an int.
-- (The rangetypes regression test uses the same trick.)
id int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
);
\d temporal_rng
@ -42,7 +43,7 @@ CREATE TABLE temporal_rng (
Column | Type | Collation | Nullable | Default
----------+-----------+-----------+----------+---------
id | int4range | | not null |
valid_at | tsrange | | not null |
valid_at | daterange | | not null |
Indexes:
"temporal_rng_pk" PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
@ -63,7 +64,7 @@ SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'te
CREATE TABLE temporal_rng2 (
id1 int4range,
id2 int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS)
);
\d temporal_rng2
@ -72,7 +73,7 @@ CREATE TABLE temporal_rng2 (
----------+-----------+-----------+----------+---------
id1 | int4range | | not null |
id2 | int4range | | not null |
valid_at | tsrange | | not null |
valid_at | daterange | | not null |
Indexes:
"temporal_rng2_pk" PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS)
@ -115,7 +116,7 @@ Indexes:
-- UNIQUE with no columns just WITHOUT OVERLAPS:
CREATE TABLE temporal_rng3 (
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng3_uq UNIQUE (valid_at WITHOUT OVERLAPS)
);
ERROR: constraint using WITHOUT OVERLAPS needs at least two columns
@ -138,7 +139,7 @@ HINT: You must specify an operator class for the index or define a default oper
-- UNIQUE with one column plus a range:
CREATE TABLE temporal_rng3 (
id int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS)
);
\d temporal_rng3
@ -146,7 +147,7 @@ CREATE TABLE temporal_rng3 (
Column | Type | Collation | Nullable | Default
----------+-----------+-----------+----------+---------
id | int4range | | |
valid_at | tsrange | | |
valid_at | daterange | | |
Indexes:
"temporal_rng3_uq" UNIQUE (id, valid_at WITHOUT OVERLAPS)
@ -167,7 +168,7 @@ DROP TABLE temporal_rng3;
CREATE TABLE temporal_rng3 (
id1 int4range,
id2 int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng3_uq UNIQUE (id1, id2, valid_at WITHOUT OVERLAPS)
);
\d temporal_rng3
@ -176,7 +177,7 @@ CREATE TABLE temporal_rng3 (
----------+-----------+-----------+----------+---------
id1 | int4range | | |
id2 | int4range | | |
valid_at | tsrange | | |
valid_at | daterange | | |
Indexes:
"temporal_rng3_uq" UNIQUE (id1, id2, valid_at WITHOUT OVERLAPS)
@ -209,7 +210,7 @@ DROP TYPE textrange2;
DROP TABLE temporal_rng;
CREATE TABLE temporal_rng (
id int4range,
valid_at tsrange
valid_at daterange
);
ALTER TABLE temporal_rng
ADD CONSTRAINT temporal_rng_pk
@ -217,7 +218,7 @@ ALTER TABLE temporal_rng
-- PK with USING INDEX (not possible):
CREATE TABLE temporal3 (
id int4range,
valid_at tsrange
valid_at daterange
);
CREATE INDEX idx_temporal3_uq ON temporal3 USING gist (id, valid_at);
ALTER TABLE temporal3
@ -231,7 +232,7 @@ DROP TABLE temporal3;
-- UNIQUE with USING INDEX (not possible):
CREATE TABLE temporal3 (
id int4range,
valid_at tsrange
valid_at daterange
);
CREATE INDEX idx_temporal3_uq ON temporal3 USING gist (id, valid_at);
ALTER TABLE temporal3
@ -245,7 +246,7 @@ DROP TABLE temporal3;
-- UNIQUE with USING [UNIQUE] INDEX (possible but not a temporal constraint):
CREATE TABLE temporal3 (
id int4range,
valid_at tsrange
valid_at daterange
);
CREATE UNIQUE INDEX idx_temporal3_uq ON temporal3 (id, valid_at);
ALTER TABLE temporal3
@ -258,7 +259,7 @@ CREATE TABLE temporal3 (
id int4range
);
ALTER TABLE temporal3
ADD COLUMN valid_at tsrange,
ADD COLUMN valid_at daterange,
ADD CONSTRAINT temporal3_pk
PRIMARY KEY (id, valid_at WITHOUT OVERLAPS);
DROP TABLE temporal3;
@ -267,7 +268,7 @@ CREATE TABLE temporal3 (
id int4range
);
ALTER TABLE temporal3
ADD COLUMN valid_at tsrange,
ADD COLUMN valid_at daterange,
ADD CONSTRAINT temporal3_uq
UNIQUE (id, valid_at WITHOUT OVERLAPS);
DROP TABLE temporal3;
@ -275,17 +276,17 @@ DROP TABLE temporal3;
-- test PK inserts
--
-- okay:
INSERT INTO temporal_rng VALUES ('[1,1]', tsrange('2018-01-02', '2018-02-03'));
INSERT INTO temporal_rng VALUES ('[1,1]', tsrange('2018-03-03', '2018-04-04'));
INSERT INTO temporal_rng VALUES ('[2,2]', tsrange('2018-01-01', '2018-01-05'));
INSERT INTO temporal_rng VALUES ('[3,3]', tsrange('2018-01-01', NULL));
INSERT INTO temporal_rng VALUES ('[1,1]', daterange('2018-01-02', '2018-02-03'));
INSERT INTO temporal_rng VALUES ('[1,1]', daterange('2018-03-03', '2018-04-04'));
INSERT INTO temporal_rng VALUES ('[2,2]', daterange('2018-01-01', '2018-01-05'));
INSERT INTO temporal_rng VALUES ('[3,3]', daterange('2018-01-01', NULL));
-- should fail:
INSERT INTO temporal_rng VALUES ('[1,1]', tsrange('2018-01-01', '2018-01-05'));
INSERT INTO temporal_rng VALUES ('[1,1]', daterange('2018-01-01', '2018-01-05'));
ERROR: conflicting key value violates exclusion constraint "temporal_rng_pk"
DETAIL: Key (id, valid_at)=([1,2), ["Mon Jan 01 00:00:00 2018","Fri Jan 05 00:00:00 2018")) conflicts with existing key (id, valid_at)=([1,2), ["Tue Jan 02 00:00:00 2018","Sat Feb 03 00:00:00 2018")).
INSERT INTO temporal_rng VALUES (NULL, tsrange('2018-01-01', '2018-01-05'));
DETAIL: Key (id, valid_at)=([1,2), [2018-01-01,2018-01-05)) conflicts with existing key (id, valid_at)=([1,2), [2018-01-02,2018-02-03)).
INSERT INTO temporal_rng VALUES (NULL, daterange('2018-01-01', '2018-01-05'));
ERROR: null value in column "id" of relation "temporal_rng" violates not-null constraint
DETAIL: Failing row contains (null, ["Mon Jan 01 00:00:00 2018","Fri Jan 05 00:00:00 2018")).
DETAIL: Failing row contains (null, [2018-01-01,2018-01-05)).
INSERT INTO temporal_rng VALUES ('[3,3]', NULL);
ERROR: null value in column "valid_at" of relation "temporal_rng" violates not-null constraint
DETAIL: Failing row contains ([3,4), null).
@ -311,7 +312,7 @@ DROP TABLE temporal3;
--
CREATE TABLE temporal3 (
id int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
);
ALTER TABLE temporal3 ALTER COLUMN valid_at DROP NOT NULL;
@ -339,22 +340,22 @@ INSERT INTO temporal_partitioned VALUES
SELECT * FROM temporal_partitioned ORDER BY id, valid_at;
id | valid_at | name
-------+-------------------------+-------
[1,2) | [01-01-2000,02-01-2000) | one
[1,2) | [02-01-2000,03-01-2000) | one
[3,4) | [01-01-2000,01-01-2010) | three
[1,2) | [2000-01-01,2000-02-01) | one
[1,2) | [2000-02-01,2000-03-01) | one
[3,4) | [2000-01-01,2010-01-01) | three
(3 rows)
SELECT * FROM tp1 ORDER BY id, valid_at;
id | valid_at | name
-------+-------------------------+------
[1,2) | [01-01-2000,02-01-2000) | one
[1,2) | [02-01-2000,03-01-2000) | one
[1,2) | [2000-01-01,2000-02-01) | one
[1,2) | [2000-02-01,2000-03-01) | one
(2 rows)
SELECT * FROM tp2 ORDER BY id, valid_at;
id | valid_at | name
-------+-------------------------+-------
[3,4) | [01-01-2000,01-01-2010) | three
[3,4) | [2000-01-01,2010-01-01) | three
(1 row)
DROP TABLE temporal_partitioned;
@ -374,22 +375,23 @@ INSERT INTO temporal_partitioned VALUES
SELECT * FROM temporal_partitioned ORDER BY id, valid_at;
id | valid_at | name
-------+-------------------------+-------
[1,2) | [01-01-2000,02-01-2000) | one
[1,2) | [02-01-2000,03-01-2000) | one
[3,4) | [01-01-2000,01-01-2010) | three
[1,2) | [2000-01-01,2000-02-01) | one
[1,2) | [2000-02-01,2000-03-01) | one
[3,4) | [2000-01-01,2010-01-01) | three
(3 rows)
SELECT * FROM tp1 ORDER BY id, valid_at;
id | valid_at | name
-------+-------------------------+------
[1,2) | [01-01-2000,02-01-2000) | one
[1,2) | [02-01-2000,03-01-2000) | one
[1,2) | [2000-01-01,2000-02-01) | one
[1,2) | [2000-02-01,2000-03-01) | one
(2 rows)
SELECT * FROM tp2 ORDER BY id, valid_at;
id | valid_at | name
-------+-------------------------+-------
[3,4) | [01-01-2000,01-01-2010) | three
[3,4) | [2000-01-01,2010-01-01) | three
(1 row)
DROP TABLE temporal_partitioned;
RESET datestyle;

View File

@ -4,6 +4,8 @@
-- temporal_rng, temporal_rng2,
-- temporal_fk_rng2rng.
SET datestyle TO ISO, YMD;
--
-- test input parser
--
@ -11,7 +13,7 @@
-- PK with no columns just WITHOUT OVERLAPS:
CREATE TABLE temporal_rng (
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng_pk PRIMARY KEY (valid_at WITHOUT OVERLAPS)
);
@ -37,7 +39,7 @@ CREATE TABLE temporal_rng (
-- use an int4range instead of an int.
-- (The rangetypes regression test uses the same trick.)
id int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
);
\d temporal_rng
@ -49,7 +51,7 @@ SELECT pg_get_indexdef(conindid, 0, true) FROM pg_constraint WHERE conname = 'te
CREATE TABLE temporal_rng2 (
id1 int4range,
id2 int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng2_pk PRIMARY KEY (id1, id2, valid_at WITHOUT OVERLAPS)
);
\d temporal_rng2
@ -79,7 +81,7 @@ CREATE TABLE temporal_mltrng (
-- UNIQUE with no columns just WITHOUT OVERLAPS:
CREATE TABLE temporal_rng3 (
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng3_uq UNIQUE (valid_at WITHOUT OVERLAPS)
);
@ -102,7 +104,7 @@ CREATE TABLE temporal_rng3 (
CREATE TABLE temporal_rng3 (
id int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng3_uq UNIQUE (id, valid_at WITHOUT OVERLAPS)
);
\d temporal_rng3
@ -114,7 +116,7 @@ DROP TABLE temporal_rng3;
CREATE TABLE temporal_rng3 (
id1 int4range,
id2 int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal_rng3_uq UNIQUE (id1, id2, valid_at WITHOUT OVERLAPS)
);
\d temporal_rng3
@ -140,7 +142,7 @@ DROP TYPE textrange2;
DROP TABLE temporal_rng;
CREATE TABLE temporal_rng (
id int4range,
valid_at tsrange
valid_at daterange
);
ALTER TABLE temporal_rng
ADD CONSTRAINT temporal_rng_pk
@ -149,7 +151,7 @@ ALTER TABLE temporal_rng
-- PK with USING INDEX (not possible):
CREATE TABLE temporal3 (
id int4range,
valid_at tsrange
valid_at daterange
);
CREATE INDEX idx_temporal3_uq ON temporal3 USING gist (id, valid_at);
ALTER TABLE temporal3
@ -160,7 +162,7 @@ DROP TABLE temporal3;
-- UNIQUE with USING INDEX (not possible):
CREATE TABLE temporal3 (
id int4range,
valid_at tsrange
valid_at daterange
);
CREATE INDEX idx_temporal3_uq ON temporal3 USING gist (id, valid_at);
ALTER TABLE temporal3
@ -171,7 +173,7 @@ DROP TABLE temporal3;
-- UNIQUE with USING [UNIQUE] INDEX (possible but not a temporal constraint):
CREATE TABLE temporal3 (
id int4range,
valid_at tsrange
valid_at daterange
);
CREATE UNIQUE INDEX idx_temporal3_uq ON temporal3 (id, valid_at);
ALTER TABLE temporal3
@ -184,7 +186,7 @@ CREATE TABLE temporal3 (
id int4range
);
ALTER TABLE temporal3
ADD COLUMN valid_at tsrange,
ADD COLUMN valid_at daterange,
ADD CONSTRAINT temporal3_pk
PRIMARY KEY (id, valid_at WITHOUT OVERLAPS);
DROP TABLE temporal3;
@ -194,7 +196,7 @@ CREATE TABLE temporal3 (
id int4range
);
ALTER TABLE temporal3
ADD COLUMN valid_at tsrange,
ADD COLUMN valid_at daterange,
ADD CONSTRAINT temporal3_uq
UNIQUE (id, valid_at WITHOUT OVERLAPS);
DROP TABLE temporal3;
@ -204,14 +206,14 @@ DROP TABLE temporal3;
--
-- okay:
INSERT INTO temporal_rng VALUES ('[1,1]', tsrange('2018-01-02', '2018-02-03'));
INSERT INTO temporal_rng VALUES ('[1,1]', tsrange('2018-03-03', '2018-04-04'));
INSERT INTO temporal_rng VALUES ('[2,2]', tsrange('2018-01-01', '2018-01-05'));
INSERT INTO temporal_rng VALUES ('[3,3]', tsrange('2018-01-01', NULL));
INSERT INTO temporal_rng VALUES ('[1,1]', daterange('2018-01-02', '2018-02-03'));
INSERT INTO temporal_rng VALUES ('[1,1]', daterange('2018-03-03', '2018-04-04'));
INSERT INTO temporal_rng VALUES ('[2,2]', daterange('2018-01-01', '2018-01-05'));
INSERT INTO temporal_rng VALUES ('[3,3]', daterange('2018-01-01', NULL));
-- should fail:
INSERT INTO temporal_rng VALUES ('[1,1]', tsrange('2018-01-01', '2018-01-05'));
INSERT INTO temporal_rng VALUES (NULL, tsrange('2018-01-01', '2018-01-05'));
INSERT INTO temporal_rng VALUES ('[1,1]', daterange('2018-01-01', '2018-01-05'));
INSERT INTO temporal_rng VALUES (NULL, daterange('2018-01-01', '2018-01-05'));
INSERT INTO temporal_rng VALUES ('[3,3]', NULL);
--
@ -239,7 +241,7 @@ DROP TABLE temporal3;
CREATE TABLE temporal3 (
id int4range,
valid_at tsrange,
valid_at daterange,
CONSTRAINT temporal3_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS)
);
@ -288,3 +290,5 @@ SELECT * FROM temporal_partitioned ORDER BY id, valid_at;
SELECT * FROM tp1 ORDER BY id, valid_at;
SELECT * FROM tp2 ORDER BY id, valid_at;
DROP TABLE temporal_partitioned;
RESET datestyle;