Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 7241911

Browse files
committed
Catch syntax error in generated column definition
The syntax GENERATED BY DEFAULT AS (expr) is not allowed but we have to accept it in the grammar to avoid shift/reduce conflicts because of the similar syntax for identity columns. The existing code just ignored this, incorrectly. Add an explicit error check and a bespoke error message. Reported-by: Justin Pryzby <pryzby@telsasoft.com>
1 parent 4ae7f02 commit 7241911

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/backend/parser/gram.y

+13
Original file line numberDiff line numberDiff line change
@@ -3505,6 +3505,19 @@ ColConstraintElem:
35053505
n->raw_expr = $5;
35063506
n->cooked_expr = NULL;
35073507
n->location = @1;
3508+
3509+
/*
3510+
* Can't do this in the grammar because of shift/reduce
3511+
* conflicts. (IDENTITY allows both ALWAYS and BY
3512+
* DEFAULT, but generated columns only allow ALWAYS.) We
3513+
* can also give a more useful error message and location.
3514+
*/
3515+
if ($2 != ATTRIBUTE_IDENTITY_ALWAYS)
3516+
ereport(ERROR,
3517+
(errcode(ERRCODE_SYNTAX_ERROR),
3518+
errmsg("for a generated column, GENERATED ALWAYS must be specified"),
3519+
parser_errposition(@2)));
3520+
35083521
$$ = (Node *)n;
35093522
}
35103523
| REFERENCES qualified_name opt_column_list key_match key_actions

src/test/regress/expected/generated.out

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ CREATE TABLE gtest_err_7d (a int PRIMARY KEY, b int GENERATED ALWAYS AS (generat
8585
ERROR: set-returning functions are not allowed in column generation expressions
8686
LINE 1: ...7d (a int PRIMARY KEY, b int GENERATED ALWAYS AS (generate_s...
8787
^
88+
-- GENERATED BY DEFAULT not allowed
89+
CREATE TABLE gtest_err_8 (a int PRIMARY KEY, b int GENERATED BY DEFAULT AS (a * 2) STORED);
90+
ERROR: for a generated column, GENERATED ALWAYS must be specified
91+
LINE 1: ...E gtest_err_8 (a int PRIMARY KEY, b int GENERATED BY DEFAULT...
92+
^
8893
INSERT INTO gtest1 VALUES (1);
8994
INSERT INTO gtest1 VALUES (2, DEFAULT);
9095
INSERT INTO gtest1 VALUES (3, 33); -- error

src/test/regress/sql/generated.sql

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ CREATE TABLE gtest_err_7b (a int PRIMARY KEY, b int GENERATED ALWAYS AS (row_num
3737
CREATE TABLE gtest_err_7c (a int PRIMARY KEY, b int GENERATED ALWAYS AS ((SELECT a)) STORED);
3838
CREATE TABLE gtest_err_7d (a int PRIMARY KEY, b int GENERATED ALWAYS AS (generate_series(1, a)) STORED);
3939

40+
-- GENERATED BY DEFAULT not allowed
41+
CREATE TABLE gtest_err_8 (a int PRIMARY KEY, b int GENERATED BY DEFAULT AS (a * 2) STORED);
42+
4043
INSERT INTO gtest1 VALUES (1);
4144
INSERT INTO gtest1 VALUES (2, DEFAULT);
4245
INSERT INTO gtest1 VALUES (3, 33); -- error

0 commit comments

Comments
 (0)