Add tests for ALTER COLUMN .. DROP EXPRESSION

This PR adds tests for dropping defaults for generated
columns. This is done by executing
ALTER TABLE .. ALTER COLUMN .. DROP EXPRESSION
This feature is introduced in PG13.
This commit is contained in:
gayyappan 2021-02-10 12:30:40 -05:00 committed by gayyappan
parent 6d679a14d0
commit 0d99592b20
7 changed files with 182 additions and 81 deletions

View File

@ -1,43 +0,0 @@
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
--
-- Generated columns support tests
--
CREATE TABLE foo (
a INT NOT NULL,
b INT,
c INT GENERATED ALWAYS AS (a + b) STORED
);
SELECT table_name FROM create_hypertable('foo', 'a', chunk_time_interval=>10);
table_name
------------
foo
(1 row)
INSERT INTO foo(a, b) VALUES(1, 2);
INSERT INTO foo(a, b) VALUES(2, 3);
-- Ensure generated column cannot be updated
\set ON_ERROR_STOP 0
INSERT INTO foo VALUES(3, 5, 8);
ERROR: cannot insert into column "c"
\set ON_ERROR_STOP 1
SELECT * FROM foo ORDER BY a;
a | b | c
---+---+---
1 | 2 | 3
2 | 3 | 5
(2 rows)
DROP TABLE foo;
-- Ensure that generated column cannot be used for partitioning
-- Generated as expression
CREATE TABLE bar (
a INT NOT NULL,
b INT GENERATED ALWAYS AS (a + 123) STORED
);
\set ON_ERROR_STOP 0
SELECT table_name FROM create_hypertable('bar', 'a', 'b', 2, chunk_time_interval=>10);
ERROR: invalid partitioning column
\set ON_ERROR_STOP 1
DROP TABLE bar;

View File

@ -113,7 +113,6 @@ endif()
if ((${PG_VERSION_MAJOR} GREATER_EQUAL "12"))
list(APPEND TEST_FILES
generated_columns.sql
misc.sql
tableam.sql
)

View File

@ -1,37 +0,0 @@
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
--
-- Generated columns support tests
--
CREATE TABLE foo (
a INT NOT NULL,
b INT,
c INT GENERATED ALWAYS AS (a + b) STORED
);
SELECT table_name FROM create_hypertable('foo', 'a', chunk_time_interval=>10);
INSERT INTO foo(a, b) VALUES(1, 2);
INSERT INTO foo(a, b) VALUES(2, 3);
-- Ensure generated column cannot be updated
\set ON_ERROR_STOP 0
INSERT INTO foo VALUES(3, 5, 8);
\set ON_ERROR_STOP 1
SELECT * FROM foo ORDER BY a;
DROP TABLE foo;
-- Ensure that generated column cannot be used for partitioning
-- Generated as expression
CREATE TABLE bar (
a INT NOT NULL,
b INT GENERATED ALWAYS AS (a + 123) STORED
);
\set ON_ERROR_STOP 0
SELECT table_name FROM create_hypertable('bar', 'a', 'b', 2, chunk_time_interval=>10);
\set ON_ERROR_STOP 1
DROP TABLE bar;

View File

@ -0,0 +1,65 @@
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
CREATE TABLE gencol_tab (
a INT NOT NULL,
b INT,
c INT GENERATED ALWAYS AS (a + b) STORED
);
SELECT table_name FROM create_hypertable('gencol_tab', 'a', chunk_time_interval=>10);
table_name
------------
gencol_tab
(1 row)
INSERT INTO gencol_tab(a, b) VALUES(1, 2);
INSERT INTO gencol_tab(a, b) VALUES(2, 3);
-- Ensure generated column cannot be updated
\set ON_ERROR_STOP 0
INSERT INTO gencol_tab VALUES(3, 5, 8);
ERROR: cannot insert into column "c"
\set ON_ERROR_STOP 1
SELECT * FROM gencol_tab ORDER BY a;
a | b | c
---+---+---
1 | 2 | 3
2 | 3 | 5
(2 rows)
DROP TABLE gencol_tab;
-- Ensure that generated column cannot be used for partitioning
-- Generated as expression
CREATE TABLE gencol_test (
a INT NOT NULL,
b INT GENERATED ALWAYS AS (a + 123) STORED
);
\set ON_ERROR_STOP 0
SELECT table_name FROM create_hypertable('gencol_test', 'a', 'b', 2, chunk_time_interval=>10);
ERROR: invalid partitioning column
\set ON_ERROR_STOP 1
-- check if default generated expression can be dropped (works on >= PG13)
SELECT table_name FROM create_hypertable('gencol_test', 'a', chunk_time_interval=>10);
table_name
-------------
gencol_test
(1 row)
SELECT attname, atthasdef, attidentity, attgenerated, attnotnull
FROM pg_attribute where attname = 'b' and attrelid = 'gencol_test'::regclass;
attname | atthasdef | attidentity | attgenerated | attnotnull
---------+-----------+-------------+--------------+------------
b | t | | s | f
(1 row)
\set ON_ERROR_STOP 0
ALTER TABLE gencol_test ALTER COLUMN b DROP EXPRESSION;
ERROR: syntax error at or near "EXPRESSION" at character 45
\set ON_ERROR_STOP 1
SELECT attname, atthasdef, attidentity, attgenerated, attnotnull
FROM pg_attribute where attname = 'b' and attrelid = 'gencol_test'::regclass;
attname | atthasdef | attidentity | attgenerated | attnotnull
---------+-----------+-------------+--------------+------------
b | t | | s | f
(1 row)
DROP TABLE gencol_test;

View File

@ -0,0 +1,64 @@
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
CREATE TABLE gencol_tab (
a INT NOT NULL,
b INT,
c INT GENERATED ALWAYS AS (a + b) STORED
);
SELECT table_name FROM create_hypertable('gencol_tab', 'a', chunk_time_interval=>10);
table_name
------------
gencol_tab
(1 row)
INSERT INTO gencol_tab(a, b) VALUES(1, 2);
INSERT INTO gencol_tab(a, b) VALUES(2, 3);
-- Ensure generated column cannot be updated
\set ON_ERROR_STOP 0
INSERT INTO gencol_tab VALUES(3, 5, 8);
ERROR: cannot insert into column "c"
\set ON_ERROR_STOP 1
SELECT * FROM gencol_tab ORDER BY a;
a | b | c
---+---+---
1 | 2 | 3
2 | 3 | 5
(2 rows)
DROP TABLE gencol_tab;
-- Ensure that generated column cannot be used for partitioning
-- Generated as expression
CREATE TABLE gencol_test (
a INT NOT NULL,
b INT GENERATED ALWAYS AS (a + 123) STORED
);
\set ON_ERROR_STOP 0
SELECT table_name FROM create_hypertable('gencol_test', 'a', 'b', 2, chunk_time_interval=>10);
ERROR: invalid partitioning column
\set ON_ERROR_STOP 1
-- check if default generated expression can be dropped (works on >= PG13)
SELECT table_name FROM create_hypertable('gencol_test', 'a', chunk_time_interval=>10);
table_name
-------------
gencol_test
(1 row)
SELECT attname, atthasdef, attidentity, attgenerated, attnotnull
FROM pg_attribute where attname = 'b' and attrelid = 'gencol_test'::regclass;
attname | atthasdef | attidentity | attgenerated | attnotnull
---------+-----------+-------------+--------------+------------
b | t | | s | f
(1 row)
\set ON_ERROR_STOP 0
ALTER TABLE gencol_test ALTER COLUMN b DROP EXPRESSION;
\set ON_ERROR_STOP 1
SELECT attname, atthasdef, attidentity, attgenerated, attnotnull
FROM pg_attribute where attname = 'b' and attrelid = 'gencol_test'::regclass;
attname | atthasdef | attidentity | attgenerated | attnotnull
---------+-----------+-------------+--------------+------------
b | f | | | f
(1 row)
DROP TABLE gencol_test;

View File

@ -12,6 +12,12 @@ set(TEST_TEMPLATES_SHARED
ordered_append_join.sql.in
)
if ((${PG_VERSION_MAJOR} GREATER_EQUAL "12"))
list(APPEND TEST_TEMPLATES_SHARED
generated_columns.sql.in
)
endif()
# Regression tests that vary with PostgreSQL version. Generated test
# files are put in the original source directory since all tests must
# be in the same directory. These files are updated when the template

View File

@ -0,0 +1,47 @@
-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
CREATE TABLE gencol_tab (
a INT NOT NULL,
b INT,
c INT GENERATED ALWAYS AS (a + b) STORED
);
SELECT table_name FROM create_hypertable('gencol_tab', 'a', chunk_time_interval=>10);
INSERT INTO gencol_tab(a, b) VALUES(1, 2);
INSERT INTO gencol_tab(a, b) VALUES(2, 3);
-- Ensure generated column cannot be updated
\set ON_ERROR_STOP 0
INSERT INTO gencol_tab VALUES(3, 5, 8);
\set ON_ERROR_STOP 1
SELECT * FROM gencol_tab ORDER BY a;
DROP TABLE gencol_tab;
-- Ensure that generated column cannot be used for partitioning
-- Generated as expression
CREATE TABLE gencol_test (
a INT NOT NULL,
b INT GENERATED ALWAYS AS (a + 123) STORED
);
\set ON_ERROR_STOP 0
SELECT table_name FROM create_hypertable('gencol_test', 'a', 'b', 2, chunk_time_interval=>10);
\set ON_ERROR_STOP 1
-- check if default generated expression can be dropped (works on >= PG13)
SELECT table_name FROM create_hypertable('gencol_test', 'a', chunk_time_interval=>10);
SELECT attname, atthasdef, attidentity, attgenerated, attnotnull
FROM pg_attribute where attname = 'b' and attrelid = 'gencol_test'::regclass;
\set ON_ERROR_STOP 0
ALTER TABLE gencol_test ALTER COLUMN b DROP EXPRESSION;
\set ON_ERROR_STOP 1
SELECT attname, atthasdef, attidentity, attgenerated, attnotnull
FROM pg_attribute where attname = 'b' and attrelid = 'gencol_test'::regclass;
DROP TABLE gencol_test;