Skip to content

Commit ddb0a34

Browse files
author
Commitfest Bot
committed
[CF 5981] Align tests for stored and virtual generated columns
This branch was automatically generated by a robot using patches from an email thread registered at: https://commitfest.postgresql.org/patch/5981 The branch will be overwritten each time a new patch version is posted to the thread, and also periodically to check for bitrot caused by changes on the master branch. Patch(es): https://www.postgresql.org/message-id/[email protected] Author(s): Yugo Nagata
2 parents b0fb2c6 + 5902d22 commit ddb0a34

File tree

4 files changed

+128
-15
lines changed

4 files changed

+128
-15
lines changed

src/test/regress/expected/generated_stored.out

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -679,14 +679,67 @@ INSERT INTO gtest21a (a) VALUES (1); -- ok
679679
INSERT INTO gtest21a (a) VALUES (0); -- violates constraint
680680
ERROR: null value in column "b" of relation "gtest21a" violates not-null constraint
681681
DETAIL: Failing row contains (0, null).
682-
CREATE TABLE gtest21b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
682+
-- also check with table constraint syntax
683+
CREATE TABLE gtest21ax (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED, CONSTRAINT cc NOT NULL b);
684+
INSERT INTO gtest21ax (a) VALUES (0); -- violates constraint
685+
ERROR: null value in column "b" of relation "gtest21ax" violates not-null constraint
686+
DETAIL: Failing row contains (0, null).
687+
INSERT INTO gtest21ax (a) VALUES (1); --ok
688+
-- SET EXPRESSION supports not null constraint
689+
ALTER TABLE gtest21ax ALTER COLUMN b SET EXPRESSION AS (nullif(a, 1)); --error
690+
ERROR: column "b" of relation "gtest21ax" contains null values
691+
DROP TABLE gtest21ax;
692+
CREATE TABLE gtest21ax (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
693+
ALTER TABLE gtest21ax ADD CONSTRAINT cc NOT NULL b;
694+
INSERT INTO gtest21ax (a) VALUES (0); -- violates constraint
695+
ERROR: null value in column "b" of relation "gtest21ax" violates not-null constraint
696+
DETAIL: Failing row contains (0, null).
697+
DROP TABLE gtest21ax;
698+
CREATE TABLE gtest21b (a int, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
683699
ALTER TABLE gtest21b ALTER COLUMN b SET NOT NULL;
684700
INSERT INTO gtest21b (a) VALUES (1); -- ok
685-
INSERT INTO gtest21b (a) VALUES (0); -- violates constraint
701+
INSERT INTO gtest21b (a) VALUES (2), (0); -- violates constraint
686702
ERROR: null value in column "b" of relation "gtest21b" violates not-null constraint
687703
DETAIL: Failing row contains (0, null).
704+
INSERT INTO gtest21b (a) VALUES (NULL); -- error
705+
ERROR: null value in column "b" of relation "gtest21b" violates not-null constraint
706+
DETAIL: Failing row contains (null, null).
688707
ALTER TABLE gtest21b ALTER COLUMN b DROP NOT NULL;
689708
INSERT INTO gtest21b (a) VALUES (0); -- ok now
709+
-- not-null constraint with partitioned table
710+
CREATE TABLE gtestnn_parent (
711+
f1 int,
712+
f2 bigint,
713+
f3 bigint GENERATED ALWAYS AS (nullif(f1, 1) + nullif(f2, 10)) STORED NOT NULL
714+
) PARTITION BY RANGE (f1);
715+
CREATE TABLE gtestnn_child PARTITION OF gtestnn_parent FOR VALUES FROM (1) TO (5);
716+
CREATE TABLE gtestnn_childdef PARTITION OF gtestnn_parent default;
717+
-- check the error messages
718+
INSERT INTO gtestnn_parent VALUES (2, 2, default), (3, 5, default), (14, 12, default); -- ok
719+
INSERT INTO gtestnn_parent VALUES (1, 2, default); -- error
720+
ERROR: null value in column "f3" of relation "gtestnn_child" violates not-null constraint
721+
DETAIL: Failing row contains (1, 2, null).
722+
INSERT INTO gtestnn_parent VALUES (2, 10, default); -- error
723+
ERROR: null value in column "f3" of relation "gtestnn_child" violates not-null constraint
724+
DETAIL: Failing row contains (2, 10, null).
725+
ALTER TABLE gtestnn_parent ALTER COLUMN f3 SET EXPRESSION AS (nullif(f1, 2) + nullif(f2, 11)); -- error
726+
ERROR: column "f3" of relation "gtestnn_child" contains null values
727+
INSERT INTO gtestnn_parent VALUES (10, 11, default); -- ok
728+
SELECT * FROM gtestnn_parent ORDER BY f1, f2, f3;
729+
f1 | f2 | f3
730+
----+----+----
731+
2 | 2 | 4
732+
3 | 5 | 8
733+
10 | 11 | 21
734+
14 | 12 | 26
735+
(4 rows)
736+
737+
-- test ALTER TABLE ADD COLUMN
738+
ALTER TABLE gtestnn_parent ADD COLUMN c int NOT NULL GENERATED ALWAYS AS (nullif(f1, 14) + nullif(f2, 10)) STORED; -- error
739+
ERROR: column "c" of relation "gtestnn_childdef" contains null values
740+
ALTER TABLE gtestnn_parent ADD COLUMN c int NOT NULL GENERATED ALWAYS AS (nullif(f1, 13) + nullif(f2, 5)) STORED; -- error
741+
ERROR: column "c" of relation "gtestnn_child" contains null values
742+
ALTER TABLE gtestnn_parent ADD COLUMN c int NOT NULL GENERATED ALWAYS AS (nullif(f1, 4) + nullif(f2, 6)) STORED; -- ok
690743
-- index constraints
691744
CREATE TABLE gtest22a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a / 2) STORED UNIQUE);
692745
INSERT INTO gtest22a VALUES (2);
@@ -847,6 +900,10 @@ CREATE TABLE gtest24r (a int PRIMARY KEY, b gtestdomain1range GENERATED ALWAYS A
847900
INSERT INTO gtest24r (a) VALUES (4); -- ok
848901
INSERT INTO gtest24r (a) VALUES (6); -- error
849902
ERROR: value for domain gtestdomain1 violates check constraint "gtestdomain1_check"
903+
CREATE TABLE gtest24at (a int PRIMARY KEY);
904+
ALTER TABLE gtest24at ADD COLUMN b gtestdomain1 GENERATED ALWAYS AS (a * 2) STORED; -- ok
905+
CREATE TABLE gtest24ata (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED);
906+
ALTER TABLE gtest24ata ALTER COLUMN b TYPE gtestdomain1; -- ok
850907
CREATE DOMAIN gtestdomainnn AS int CHECK (VALUE IS NOT NULL);
851908
CREATE TABLE gtest24nn (a int, b gtestdomainnn GENERATED ALWAYS AS (a * 2) STORED);
852909
INSERT INTO gtest24nn (a) VALUES (4); -- ok
@@ -1154,6 +1211,15 @@ DETAIL: Column "x" is a generated column.
11541211
ALTER TABLE gtest27 ALTER COLUMN x DROP DEFAULT; -- error
11551212
ERROR: column "x" of relation "gtest27" is a generated column
11561213
HINT: Use ALTER TABLE ... ALTER COLUMN ... DROP EXPRESSION instead.
1214+
-- test not-null checking during table rewrite
1215+
INSERT INTO gtest27 (a, b) VALUES (NULL, NULL);
1216+
ALTER TABLE gtest27
1217+
DROP COLUMN x,
1218+
ALTER COLUMN a TYPE bigint,
1219+
ALTER COLUMN b TYPE bigint,
1220+
ADD COLUMN x bigint GENERATED ALWAYS AS ((a + b) * 2) STORED NOT NULL; -- error
1221+
ERROR: column "x" of relation "gtest27" contains null values
1222+
DELETE FROM gtest27 WHERE a IS NULL AND b IS NULL;
11571223
-- It's possible to alter the column types this way:
11581224
ALTER TABLE gtest27
11591225
DROP COLUMN x,

src/test/regress/expected/generated_virtual.out

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ CREATE TYPE double_int as (a int, b int);
552552
CREATE TABLE gtest4 (
553553
a int,
554554
b double_int GENERATED ALWAYS AS ((a * 2, a * 3)) VIRTUAL
555-
);
555+
); -- fails, user-defined type
556556
ERROR: virtual generated column "b" cannot have a user-defined type
557557
DETAIL: Virtual generated columns that make use of user-defined types are not yet supported.
558558
--INSERT INTO gtest4 VALUES (1), (6);
@@ -808,12 +808,6 @@ CREATE TABLE gtest24nn (a int, b gtestdomainnn GENERATED ALWAYS AS (a * 2) VIRTU
808808
ERROR: virtual generated column "b" cannot have a domain type
809809
--INSERT INTO gtest24nn (a) VALUES (4); -- ok
810810
--INSERT INTO gtest24nn (a) VALUES (NULL); -- error
811-
-- using user-defined type not yet supported
812-
CREATE TABLE gtest24xxx (a gtestdomain1, b gtestdomain1, c int GENERATED ALWAYS AS (greatest(a, b)) VIRTUAL); -- error
813-
ERROR: generation expression uses user-defined type
814-
LINE 1: ...main1, b gtestdomain1, c int GENERATED ALWAYS AS (greatest(a...
815-
^
816-
DETAIL: Virtual generated columns that make use of user-defined types are not yet supported.
817811
-- typed tables (currently not supported)
818812
CREATE TYPE gtest_type AS (f1 integer, f2 text, f3 bigint);
819813
CREATE TABLE gtest28 OF gtest_type (f1 WITH OPTIONS GENERATED ALWAYS AS (f2 *2) VIRTUAL);
@@ -1484,6 +1478,12 @@ SELECT attrelid, attname, attgenerated FROM pg_attribute WHERE attgenerated NOT
14841478
--
14851479
-- these tests are specific to generated_virtual.sql
14861480
--
1481+
-- using user-defined type not yet supported
1482+
CREATE TABLE gtest24xxx (a gtestdomain1, b gtestdomain1, c int GENERATED ALWAYS AS (greatest(a, b)) VIRTUAL); -- error
1483+
ERROR: generation expression uses user-defined type
1484+
LINE 1: ...main1, b gtestdomain1, c int GENERATED ALWAYS AS (greatest(a...
1485+
^
1486+
DETAIL: Virtual generated columns that make use of user-defined types are not yet supported.
14871487
create table gtest32 (
14881488
a int primary key,
14891489
b int generated always as (a * 2),

src/test/regress/sql/generated_stored.sql

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,13 +340,47 @@ CREATE TABLE gtest21a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0
340340
INSERT INTO gtest21a (a) VALUES (1); -- ok
341341
INSERT INTO gtest21a (a) VALUES (0); -- violates constraint
342342

343-
CREATE TABLE gtest21b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
343+
-- also check with table constraint syntax
344+
CREATE TABLE gtest21ax (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED, CONSTRAINT cc NOT NULL b);
345+
INSERT INTO gtest21ax (a) VALUES (0); -- violates constraint
346+
INSERT INTO gtest21ax (a) VALUES (1); --ok
347+
-- SET EXPRESSION supports not null constraint
348+
ALTER TABLE gtest21ax ALTER COLUMN b SET EXPRESSION AS (nullif(a, 1)); --error
349+
DROP TABLE gtest21ax;
350+
351+
CREATE TABLE gtest21ax (a int PRIMARY KEY, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
352+
ALTER TABLE gtest21ax ADD CONSTRAINT cc NOT NULL b;
353+
INSERT INTO gtest21ax (a) VALUES (0); -- violates constraint
354+
DROP TABLE gtest21ax;
355+
356+
CREATE TABLE gtest21b (a int, b int GENERATED ALWAYS AS (nullif(a, 0)) STORED);
344357
ALTER TABLE gtest21b ALTER COLUMN b SET NOT NULL;
345358
INSERT INTO gtest21b (a) VALUES (1); -- ok
346-
INSERT INTO gtest21b (a) VALUES (0); -- violates constraint
359+
INSERT INTO gtest21b (a) VALUES (2), (0); -- violates constraint
360+
INSERT INTO gtest21b (a) VALUES (NULL); -- error
347361
ALTER TABLE gtest21b ALTER COLUMN b DROP NOT NULL;
348362
INSERT INTO gtest21b (a) VALUES (0); -- ok now
349363

364+
-- not-null constraint with partitioned table
365+
CREATE TABLE gtestnn_parent (
366+
f1 int,
367+
f2 bigint,
368+
f3 bigint GENERATED ALWAYS AS (nullif(f1, 1) + nullif(f2, 10)) STORED NOT NULL
369+
) PARTITION BY RANGE (f1);
370+
CREATE TABLE gtestnn_child PARTITION OF gtestnn_parent FOR VALUES FROM (1) TO (5);
371+
CREATE TABLE gtestnn_childdef PARTITION OF gtestnn_parent default;
372+
-- check the error messages
373+
INSERT INTO gtestnn_parent VALUES (2, 2, default), (3, 5, default), (14, 12, default); -- ok
374+
INSERT INTO gtestnn_parent VALUES (1, 2, default); -- error
375+
INSERT INTO gtestnn_parent VALUES (2, 10, default); -- error
376+
ALTER TABLE gtestnn_parent ALTER COLUMN f3 SET EXPRESSION AS (nullif(f1, 2) + nullif(f2, 11)); -- error
377+
INSERT INTO gtestnn_parent VALUES (10, 11, default); -- ok
378+
SELECT * FROM gtestnn_parent ORDER BY f1, f2, f3;
379+
-- test ALTER TABLE ADD COLUMN
380+
ALTER TABLE gtestnn_parent ADD COLUMN c int NOT NULL GENERATED ALWAYS AS (nullif(f1, 14) + nullif(f2, 10)) STORED; -- error
381+
ALTER TABLE gtestnn_parent ADD COLUMN c int NOT NULL GENERATED ALWAYS AS (nullif(f1, 13) + nullif(f2, 5)) STORED; -- error
382+
ALTER TABLE gtestnn_parent ADD COLUMN c int NOT NULL GENERATED ALWAYS AS (nullif(f1, 4) + nullif(f2, 6)) STORED; -- ok
383+
350384
-- index constraints
351385
CREATE TABLE gtest22a (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a / 2) STORED UNIQUE);
352386
INSERT INTO gtest22a VALUES (2);
@@ -419,6 +453,11 @@ CREATE TABLE gtest24r (a int PRIMARY KEY, b gtestdomain1range GENERATED ALWAYS A
419453
INSERT INTO gtest24r (a) VALUES (4); -- ok
420454
INSERT INTO gtest24r (a) VALUES (6); -- error
421455

456+
CREATE TABLE gtest24at (a int PRIMARY KEY);
457+
ALTER TABLE gtest24at ADD COLUMN b gtestdomain1 GENERATED ALWAYS AS (a * 2) STORED; -- ok
458+
CREATE TABLE gtest24ata (a int PRIMARY KEY, b int GENERATED ALWAYS AS (a * 2) STORED);
459+
ALTER TABLE gtest24ata ALTER COLUMN b TYPE gtestdomain1; -- ok
460+
422461
CREATE DOMAIN gtestdomainnn AS int CHECK (VALUE IS NOT NULL);
423462
CREATE TABLE gtest24nn (a int, b gtestdomainnn GENERATED ALWAYS AS (a * 2) STORED);
424463
INSERT INTO gtest24nn (a) VALUES (4); -- ok
@@ -530,6 +569,14 @@ ALTER TABLE gtest27 ALTER COLUMN x TYPE numeric;
530569
SELECT * FROM gtest27;
531570
ALTER TABLE gtest27 ALTER COLUMN x TYPE boolean USING x <> 0; -- error
532571
ALTER TABLE gtest27 ALTER COLUMN x DROP DEFAULT; -- error
572+
-- test not-null checking during table rewrite
573+
INSERT INTO gtest27 (a, b) VALUES (NULL, NULL);
574+
ALTER TABLE gtest27
575+
DROP COLUMN x,
576+
ALTER COLUMN a TYPE bigint,
577+
ALTER COLUMN b TYPE bigint,
578+
ADD COLUMN x bigint GENERATED ALWAYS AS ((a + b) * 2) STORED NOT NULL; -- error
579+
DELETE FROM gtest27 WHERE a IS NULL AND b IS NULL;
533580
-- It's possible to alter the column types this way:
534581
ALTER TABLE gtest27
535582
DROP COLUMN x,

src/test/regress/sql/generated_virtual.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ CREATE TYPE double_int as (a int, b int);
252252
CREATE TABLE gtest4 (
253253
a int,
254254
b double_int GENERATED ALWAYS AS ((a * 2, a * 3)) VIRTUAL
255-
);
255+
); -- fails, user-defined type
256256
--INSERT INTO gtest4 VALUES (1), (6);
257257
--SELECT * FROM gtest4;
258258

@@ -464,9 +464,6 @@ CREATE TABLE gtest24nn (a int, b gtestdomainnn GENERATED ALWAYS AS (a * 2) VIRTU
464464
--INSERT INTO gtest24nn (a) VALUES (4); -- ok
465465
--INSERT INTO gtest24nn (a) VALUES (NULL); -- error
466466

467-
-- using user-defined type not yet supported
468-
CREATE TABLE gtest24xxx (a gtestdomain1, b gtestdomain1, c int GENERATED ALWAYS AS (greatest(a, b)) VIRTUAL); -- error
469-
470467
-- typed tables (currently not supported)
471468
CREATE TYPE gtest_type AS (f1 integer, f2 text, f3 bigint);
472469
CREATE TABLE gtest28 OF gtest_type (f1 WITH OPTIONS GENERATED ALWAYS AS (f2 *2) VIRTUAL);
@@ -806,6 +803,9 @@ SELECT attrelid, attname, attgenerated FROM pg_attribute WHERE attgenerated NOT
806803
-- these tests are specific to generated_virtual.sql
807804
--
808805

806+
-- using user-defined type not yet supported
807+
CREATE TABLE gtest24xxx (a gtestdomain1, b gtestdomain1, c int GENERATED ALWAYS AS (greatest(a, b)) VIRTUAL); -- error
808+
809809
create table gtest32 (
810810
a int primary key,
811811
b int generated always as (a * 2),

0 commit comments

Comments
 (0)