Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7241911

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 avoidshift/reduce conflicts because of the similar syntax for identitycolumns. The existing code just ignored this, incorrectly. Add anexplicit error check and a bespoke error message.Reported-by: Justin Pryzby <pryzby@telsasoft.com>
1 parent4ae7f02 commit7241911

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

‎src/backend/parser/gram.y

Lines changed: 13 additions & 0 deletions
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
|REFERENCESqualified_nameopt_column_listkey_matchkey_actions

‎src/test/regress/expected/generated.out

Lines changed: 5 additions & 0 deletions
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

Lines changed: 3 additions & 0 deletions
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
CREATETABLEgtest_err_7c (aintPRIMARY KEY, bint GENERATED ALWAYSAS ((SELECT a)) STORED);
3838
CREATETABLEgtest_err_7d (aintPRIMARY KEY, bint GENERATED ALWAYSAS (generate_series(1, a)) STORED);
3939

40+
-- GENERATED BY DEFAULT not allowed
41+
CREATETABLEgtest_err_8 (aintPRIMARY KEY, bint GENERATED BY DEFAULTAS (a*2) STORED);
42+
4043
INSERT INTO gtest1VALUES (1);
4144
INSERT INTO gtest1VALUES (2, DEFAULT);
4245
INSERT INTO gtest1VALUES (3,33);-- error

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp