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

Commit07a6fa9

Browse files
committed
Fixed this problem and added regression tests in domain.sql.
Also:- Changed header file order (alphabetical)- Changed to m = attnum - 1 in binary copy code for consistencyRod Taylor
1 parent24bebf0 commit07a6fa9

File tree

3 files changed

+118
-14
lines changed

3 files changed

+118
-14
lines changed

‎src/backend/commands/copy.c

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*
88
*
99
* IDENTIFICATION
10-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.171 2002/09/04 20:31:14 momjian Exp $
10+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.172 2002/09/20 03:52:50 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -28,18 +28,20 @@
2828
#include"commands/copy.h"
2929
#include"commands/trigger.h"
3030
#include"executor/executor.h"
31-
#include"rewrite/rewriteHandler.h"
3231
#include"libpq/libpq.h"
32+
#include"mb/pg_wchar.h"
3333
#include"miscadmin.h"
34+
#include"nodes/makefuncs.h"
35+
#include"parser/parse_coerce.h"
3436
#include"parser/parse_relation.h"
37+
#include"rewrite/rewriteHandler.h"
3538
#include"tcop/pquery.h"
3639
#include"tcop/tcopprot.h"
3740
#include"utils/acl.h"
3841
#include"utils/builtins.h"
3942
#include"utils/relcache.h"
4043
#include"utils/lsyscache.h"
4144
#include"utils/syscache.h"
42-
#include"mb/pg_wchar.h"
4345

4446
#defineISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
4547
#defineOCTVALUE(c) ((c) - '0')
@@ -744,6 +746,8 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
744746
num_defaults;
745747
FmgrInfo*in_functions;
746748
Oid*elements;
749+
bool*isDomain;
750+
boolhasDomain= false;
747751
inti;
748752
List*cur;
749753
Oidin_func_oid;
@@ -796,13 +800,24 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
796800
defexprs= (Node**)palloc(sizeof(Node*)*num_phys_attrs);
797801
in_functions= (FmgrInfo*)palloc(num_phys_attrs*sizeof(FmgrInfo));
798802
elements= (Oid*)palloc(num_phys_attrs*sizeof(Oid));
803+
isDomain= (bool*)palloc(num_phys_attrs*sizeof(bool));
799804

800805
for (i=0;i<num_phys_attrs;i++)
801806
{
802807
/* We don't need info for dropped attributes */
803808
if (attr[i]->attisdropped)
804809
continue;
805810

811+
/* Test for the base type */
812+
if (getBaseType(attr[i]->atttypid)!=attr[i]->atttypid)
813+
{
814+
hasDomain= true;
815+
isDomain[i]= true;
816+
}
817+
else
818+
isDomain[i]= false;
819+
820+
/* Fetch the input function */
806821
in_func_oid= (Oid)GetInputFunction(attr[i]->atttypid);
807822
fmgr_info(in_func_oid,&in_functions[i]);
808823
elements[i]=GetTypeElement(attr[i]->atttypid);
@@ -1011,6 +1026,7 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
10111026
foreach(cur,attnumlist)
10121027
{
10131028
intattnum=lfirsti(cur);
1029+
intm=attnum-1;
10141030

10151031
i++;
10161032

@@ -1019,9 +1035,9 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
10191035
elog(ERROR,"COPY BINARY: unexpected EOF");
10201036
if (fld_size==0)
10211037
continue;/* it's NULL; nulls[attnum-1] already set */
1022-
if (fld_size!=attr[attnum-1]->attlen)
1038+
if (fld_size!=attr[m]->attlen)
10231039
elog(ERROR,"COPY BINARY: sizeof(field %d) is %d, expected %d",
1024-
i, (int)fld_size, (int)attr[attnum-1]->attlen);
1040+
i, (int)fld_size, (int)attr[m]->attlen);
10251041
if (fld_size==-1)
10261042
{
10271043
/* varlena field */
@@ -1040,9 +1056,9 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
10401056
fp);
10411057
if (CopyGetEof(fp))
10421058
elog(ERROR,"COPY BINARY: unexpected EOF");
1043-
values[attnum-1]=PointerGetDatum(varlena_ptr);
1059+
values[m]=PointerGetDatum(varlena_ptr);
10441060
}
1045-
elseif (!attr[attnum-1]->attbyval)
1061+
elseif (!attr[m]->attbyval)
10461062
{
10471063
/* fixed-length pass-by-reference */
10481064
Pointerrefval_ptr;
@@ -1052,7 +1068,7 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
10521068
CopyGetData(refval_ptr,fld_size,fp);
10531069
if (CopyGetEof(fp))
10541070
elog(ERROR,"COPY BINARY: unexpected EOF");
1055-
values[attnum-1]=PointerGetDatum(refval_ptr);
1071+
values[m]=PointerGetDatum(refval_ptr);
10561072
}
10571073
else
10581074
{
@@ -1067,10 +1083,56 @@ CopyFrom(Relation rel, List *attnumlist, bool binary, bool oids,
10671083
CopyGetData(&datumBuf,fld_size,fp);
10681084
if (CopyGetEof(fp))
10691085
elog(ERROR,"COPY BINARY: unexpected EOF");
1070-
values[attnum-1]=fetch_att(&datumBuf, true,fld_size);
1086+
values[m]=fetch_att(&datumBuf, true,fld_size);
10711087
}
10721088

1073-
nulls[attnum-1]=' ';
1089+
nulls[m]=' ';
1090+
}
1091+
}
1092+
1093+
/* Deal with domains */
1094+
if (hasDomain)
1095+
{
1096+
ParseState*pstate;
1097+
pstate=make_parsestate(NULL);
1098+
1099+
foreach(cur,attnumlist)
1100+
{
1101+
intattnum=lfirsti(cur);
1102+
intm=attnum-1;
1103+
1104+
Const*con;
1105+
Node*node;
1106+
boolisNull= (nulls[m]=='n');
1107+
1108+
/* This is not a domain, so lets skip it */
1109+
if (!isDomain[m])
1110+
continue;
1111+
1112+
/*
1113+
* This is a domain. As such, we must process it's input
1114+
* function and coerce_type_constraints. The simplest way
1115+
* of doing that is to allow coerce_type to accomplish its
1116+
* job from an unknown constant
1117+
*/
1118+
1119+
/* Create a constant */
1120+
con=makeConst(attr[m]->atttypid,
1121+
attr[m]->attlen,
1122+
values[m],
1123+
isNull,
1124+
attr[m]->attbyval,
1125+
false,/* not a set */
1126+
false);/* not coerced */
1127+
1128+
/* Process constraints */
1129+
node=coerce_type_constraints(pstate, (Node*)con,
1130+
attr[m]->atttypid, true);
1131+
1132+
values[m]=ExecEvalExpr(node,econtext,
1133+
&isNull,NULL);
1134+
1135+
nulls[m]=isNull ?'n' :' ';
10741136
}
10751137
}
10761138

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ INSERT INTO basictest values ('88', 'haha', 'short', '123.12'); -- Good
3737
INSERT INTO basictest values ('88', 'haha', 'short text', '123.12'); -- Bad varchar
3838
ERROR: value too long for type character varying(5)
3939
INSERT INTO basictest values ('88', 'haha', 'short', '123.1212'); -- Truncate numeric
40+
-- Test copy
41+
COPY basictest (testvarchar) FROM stdin; -- fail
42+
ERROR: copy: line 1, value too long for type character varying(5)
43+
lost synchronization with server, resetting connection
44+
COPY basictest (testvarchar) FROM stdin;
4045
select * from basictest;
4146
testint4 | testtext | testvarchar | testnumeric
4247
----------+----------+-------------+-------------
4348
88 | haha | short | 123.12
4449
88 | haha | short | 123.12
45-
(2 rows)
50+
| | short |
51+
(3 rows)
4652

4753
-- check that domains inherit operations from base types
4854
select testtext || testvarchar as concat, testnumeric + 42 as sum
@@ -51,7 +57,8 @@ from basictest;
5157
-----------+--------
5258
hahashort | 165.12
5359
hahashort | 165.12
54-
(2 rows)
60+
|
61+
(3 rows)
5562

5663
drop table basictest;
5764
drop domain domainvarchar restrict;
@@ -111,12 +118,18 @@ ERROR: Domain dnotnull does not allow NULL values
111118
INSERT INTO nulltest values ('a', 'b', NULL, 'd');
112119
ERROR: ExecInsert: Fail to add null value in not null attribute col3
113120
INSERT INTO nulltest values ('a', 'b', 'c', NULL); -- Good
121+
-- Test copy
122+
COPY nulltest FROM stdin; --fail
123+
ERROR: copy: line 1, CopyFrom: Fail to add null value in not null attribute col3
124+
lost synchronization with server, resetting connection
125+
COPY nulltest FROM stdin;
114126
select * from nulltest;
115127
col1 | col2 | col3 | col4
116128
------+------+------+------
117129
a | b | c | d
118130
a | b | c |
119-
(2 rows)
131+
a | b | c |
132+
(3 rows)
120133

121134
-- Test out coerced (casted) constraints
122135
SELECT cast('1' as dnotnull);
@@ -156,13 +169,16 @@ NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'defaulttest_pkey
156169
insert into defaulttest default values;
157170
insert into defaulttest default values;
158171
insert into defaulttest default values;
172+
-- Test defaults with copy
173+
COPY defaulttest(col5) FROM stdin;
159174
select * from defaulttest;
160175
col1 | col2 | col3 | col4 | col5 | col6 | col7 | col8
161176
------+------+------+------+------+------+------+-------
162177
3 | 12 | 5 | 1 | 3 | 88 | 8000 | 12.12
163178
3 | 12 | 5 | 2 | 3 | 88 | 8000 | 12.12
164179
3 | 12 | 5 | 3 | 3 | 88 | 8000 | 12.12
165-
(3 rows)
180+
3 | 12 | 5 | 4 | 42 | 88 | 8000 | 12.12
181+
(4 rows)
166182

167183
drop sequence ddef4_seq;
168184
drop table defaulttest;

‎src/test/regress/sql/domain.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ create table basictest
3535
INSERT INTO basictestvalues ('88','haha','short','123.12');-- Good
3636
INSERT INTO basictestvalues ('88','haha','short text','123.12');-- Bad varchar
3737
INSERT INTO basictestvalues ('88','haha','short','123.1212');-- Truncate numeric
38+
39+
-- Test copy
40+
COPY basictest (testvarchar)FROM stdin;-- fail
41+
notsoshorttext
42+
\.
43+
44+
COPY basictest (testvarchar)FROM stdin;
45+
short
46+
\.
47+
3848
select*from basictest;
3949

4050
-- check that domains inherit operations from base types
@@ -84,6 +94,16 @@ INSERT INTO nulltest values (NULL, 'b', 'c', 'd');
8494
INSERT INTO nulltestvalues ('a',NULL,'c','d');
8595
INSERT INTO nulltestvalues ('a','b',NULL,'d');
8696
INSERT INTO nulltestvalues ('a','b','c',NULL);-- Good
97+
98+
-- Test copy
99+
COPY nulltestFROM stdin;--fail
100+
ab\Nd
101+
\.
102+
103+
COPY nulltestFROM stdin;
104+
abc\N
105+
\.
106+
87107
select*from nulltest;
88108

89109
-- Test out coerced (casted) constraints
@@ -119,6 +139,12 @@ create table defaulttest
119139
insert into defaulttest defaultvalues;
120140
insert into defaulttest defaultvalues;
121141
insert into defaulttest defaultvalues;
142+
143+
-- Test defaults with copy
144+
COPY defaulttest(col5)FROM stdin;
145+
42
146+
\.
147+
122148
select*from defaulttest;
123149

124150
dropsequence ddef4_seq;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp