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

Commitccfe28e

Browse files
peterejianhe-fun
andcommitted
Disallow generated columns in COPY WHERE clause
Stored generated columns are not yet computed when the filteringhappens, so we need to prohibit them to avoid incorrect behavior.Co-authored-by: jian he <jian.universality@gmail.com>Reviewed-by: Kirill Reshke <reshkekirill@gmail.com>Reviewed-by: Masahiko Sawada <sawada.mshk@gmail.com>Discussion:https://www.postgresql.org/message-id/flat/CACJufxHb8YPQ095R_pYDr77W9XKNaXg5Rzy-WP525mkq+hRM3g@mail.gmail.com
1 parentb8b37f0 commitccfe28e

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

‎src/backend/commands/copy.c‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
128128

129129
if (stmt->whereClause)
130130
{
131+
Bitmapset*expr_attrs=NULL;
132+
inti;
133+
131134
/* add nsitem to query namespace */
132135
addNSItemToQuery(pstate,nsitem, false, true, true);
133136

@@ -140,6 +143,40 @@ DoCopy(ParseState *pstate, const CopyStmt *stmt,
140143
/* we have to fix its collations too */
141144
assign_expr_collations(pstate,whereClause);
142145

146+
/*
147+
* Examine all the columns in the WHERE clause expression. When
148+
* the whole-row reference is present, examine all the columns of
149+
* the table.
150+
*/
151+
pull_varattnos(whereClause,1,&expr_attrs);
152+
if (bms_is_member(0-FirstLowInvalidHeapAttributeNumber,expr_attrs))
153+
{
154+
expr_attrs=bms_add_range(expr_attrs,
155+
1-FirstLowInvalidHeapAttributeNumber,
156+
RelationGetNumberOfAttributes(rel)-FirstLowInvalidHeapAttributeNumber);
157+
expr_attrs=bms_del_member(expr_attrs,0-FirstLowInvalidHeapAttributeNumber);
158+
}
159+
160+
i=-1;
161+
while ((i=bms_next_member(expr_attrs,i)) >=0)
162+
{
163+
AttrNumberattno=i+FirstLowInvalidHeapAttributeNumber;
164+
165+
Assert(attno!=0);
166+
167+
/*
168+
* Prohibit generated columns in the WHERE clause. Stored
169+
* generated columns are not yet computed when the filtering
170+
* happens.
171+
*/
172+
if (TupleDescAttr(RelationGetDescr(rel),attno-1)->attgenerated)
173+
ereport(ERROR,
174+
errcode(ERRCODE_INVALID_COLUMN_REFERENCE),
175+
errmsg("generated columns are not supported in COPY FROM WHERE conditions"),
176+
errdetail("Column \"%s\" is a generated column.",
177+
get_attname(RelationGetRelid(rel),attno, false)));
178+
}
179+
143180
whereClause=eval_const_expressions(NULL,whereClause);
144181

145182
whereClause= (Node*)canonicalize_qual((Expr*)whereClause, false);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ COPY gtest1 FROM stdin;
415415
COPY gtest1 (a, b) FROM stdin;
416416
ERROR: column "b" is a generated column
417417
DETAIL: Generated columns cannot be used in COPY.
418+
COPY gtest1 FROM stdin WHERE b <> 10;
419+
ERROR: generated columns are not supported in COPY FROM WHERE conditions
420+
DETAIL: Column "b" is a generated column.
421+
COPY gtest1 FROM stdin WHERE gtest1 IS NULL;
422+
ERROR: generated columns are not supported in COPY FROM WHERE conditions
423+
DETAIL: Column "b" is a generated column.
418424
SELECT * FROM gtest1 ORDER BY a;
419425
a | b
420426
---+---

‎src/test/regress/sql/generated.sql‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ COPY gtest1 FROM stdin;
189189

190190
COPY gtest1 (a, b)FROM stdin;
191191

192+
COPY gtest1FROM stdinWHERE b<>10;
193+
194+
COPY gtest1FROM stdinWHERE gtest1 ISNULL;
195+
192196
SELECT*FROM gtest1ORDER BY a;
193197

194198
TRUNCATE gtest3;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp